01-23-06 07:55 AM
2006-01-21, 17:56(+00), kj:
>
>
>
>
>
> I have an "aliases" file that is sourced at start up. As the name
> suggests, this file defines various aliases and functions.
>
> I would like to define some variables that have the rest of the
> file as their "lexical scope", shadowing preexisting variables if
> necessary. For example, if I define, within this file, the variables
> FOO=1 and BAR=2, and then define the function
>
> demo () {
> echo foo: $FOO
> echo bar: $BAR
> }
>
> then I should be able to do this:
>
> % BAR=100
> % source aliases
> % demo
> foo: 1
> bar: 2
> % echo $FOO # FOO is undefined now
>
> % echo $BAR # BAR retains pre-existing value
> 100
>
> Notice that the demo function still uses the value of BAR defined
> within the aliases file, but this value does not affect the value
> of the BAR variable defined outside of the file.
>
> Is it possible to do this with a zsh script?
[...]
Only function and subshells can affect the scope. So use
functions for instance.
aliases.zsh:
<<
demo() {
print -r -- $BAR
..
}
main() {
local FOO BAR
BAR=200
alias whatever=...
}
main[vbcol=seagreen]
BAR=100
source aliases.zsh
demo
--
Stéphane
[ Post a follow-up to this message ]
|