Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Source vs require #273

Merged
merged 3 commits into from
Aug 25, 2019
Merged

Source vs require #273

merged 3 commits into from
Aug 25, 2019

Conversation

odino
Copy link
Collaborator

@odino odino commented Aug 24, 2019

Now require is not an alias of source aymore.

source, like in bash, imports a script and gives it full
access to the global environment.

require instead imports the script, but does not allow it
to read or set variables at the global level. If a required
scripts needs to "pass" variables to the caller, it can simply
return them and they will be available to th caller.

Example -- say this is our module, ip-finder.abs:

return f() {
  return `curl icanhazip.com`
}

Then we can simply use it like this:

ip_finder = require("ip-finder.abs")

echo("My IP is %s", ip_finder())

I have also made an important change while we wait for modules and the ABS package manager to be implemented (cc @mingwho): I've made require(...) use paths that are relative to the current script, just like in NodeJS etc.

To illustrate the problem:

/A
  /dir/B
  /dir/C

Say that A imports B, and B imports C. If you use source, B will have to use source("./dir/C") as source paths are always relative to the initial execution context.
With require B instead can require("C") since the import path is always relative to the current script. When @mingwho works on the installer, we'll have to tweak how we locate the required script making sure we can include installed modules.

With these changes, I've also gotten rid of the globalEnv variable which is a global we use to access the main ABS execution environment. I replaced it making sure that the env is being passed around as needed.

Now `require` is not an alias of `source` aymore.

`source`, like in bash, imports a script and gives it full
access to the global environment.

`require` instead imports the script, but does not allow it
to read or set variables at the global level. If a required
scripts needs to "pass" variables to the caller, it can simply
`return` them and they will be available to th caller.

Example -- say this is our module, `ip-finder.abs`:

```
return f() {
  return `curl icanhazip.com`
}
```

Then we can simply use it like this:

```
ip_finder = require("ip-finder.abs")

echo("My IP is %s", ip_finder())
```
@odino odino added this to the 1.8.x milestone Aug 24, 2019
@odino odino merged commit 5fb8d57 into 1.8.x Aug 25, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant