Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: "typeset -p" and no_GLOBAL_EXPORT, other misc.
- X-seq: zsh-workers 52729
- From: Stephane Chazelas <stephane@xxxxxxxxxxxx>
- To: Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx>
- Cc: Zsh hackers list <zsh-workers@xxxxxxx>
- Subject: Re: "typeset -p" and no_GLOBAL_EXPORT, other misc.
- Date: Tue, 12 Mar 2024 08:49:26 +0000
- Archived-at: <https://zsh.org/workers/52729>
- In-reply-to: <CAH+w=7YBMPe_6qZPt8-CR_EhJPGbnyiNNAguGcWiD3K7nLtB9A@mail.gmail.com>
- List-id: <zsh-workers.zsh.org>
- Mail-followup-to: Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx>, Zsh hackers list <zsh-workers@xxxxxxx>
- References: <CAH+w=7YBMPe_6qZPt8-CR_EhJPGbnyiNNAguGcWiD3K7nLtB9A@mail.gmail.com>
2024-03-11 21:13:32 -0700, Bart Schaefer:
[...]
> To even start fixing this mess, we'd have to explain what a function
> considers "local" and what "global". Are all "inherited" scopes
> global, so local is the current scope only? Gaah.
[...]
IMO, typeset -g should only mean: don't make it local, do not
instantiate a new variable in the current scope.
And we certainly don't want bash's typeset -g behaviour where it
means *the outermost scope*, which in shells with dynamic
scoping is essentially what "global" means, but it's not
particularly useful to single it out.
For instance, in bash:
bash-5.3$ integer() { typeset -gi -- "$@"; }
bash-5.3$ integer i=1+1
bash-5.3$ echo $i
2
fine, but:
bash-5.3$ f() { local i; integer i=2+2; echo "$i"; }
bash-5.3$ f
bash-5.3$ echo $i
4
That "integer" changed the type of the global (outer-most) i
variable instead of that of its caller.
IOW, most of the usages of "typeset -g" like with that "integer"
above is when you want to change attributes of a variable (set
the type with typeset which bash renamed to declare) without
wanting to make it local, like export or readonly do in
Bourne-like shells (except zsh for the latter), not change the
type of the variable in the outermost scope which I can't think
why you would want to.
In mksh or yash, readonly var is like typeset -gr var.
In bash, readonly var is like mksh/yash's typeset -gr var, but
unlike its own typeset -gr:
~$ bash -c 'b() { local a=1; a; echo $a; }; a() { readonly a=2; echo $a; }; a=0; b; echo $a'
2
2
0
~$ bash -c 'b() { local a=1; a; echo $a; }; a() { typeset -gr a=2; echo $a; }; a=0; b; echo $a'
1
1
2
~$ mksh -c 'b() { local a=1; a; echo $a; }; a() { readonly a=2; echo $a; }; a=0; b; echo $a'
2
2
0
~$ mksh -c 'b() { local a=1; a; echo $a; }; a() { typeset -gr a=2; echo $a; }; a=0; b; echo $a'
2
2
0
In zsh, readonly var, when not emulating other shells is more
like typeset -r:
~$ zsh -c 'b() { local a=1; a; echo $a; }; a() { typeset -gr a=2; echo $a; }; a=0; b; echo $a'
2
2
0
~$ zsh -c 'b() { local a=1; a; echo $a; }; a() { readonly a=2; echo $a; }; a=0; b; echo $a'
2
1
0
~$ zsh -c 'b() { local a=1; a; echo $a; }; a() { typeset -r a=2; echo $a; }; a=0; b; echo $a'
2
1
0
ksh93 does static scoping, so things are different there anyway,
there's only a global scope and one local scope not a stack of
them, so -g can mean "global" without ambiguity and functions
cannot access the variables of their caller unless they're
exported.
~$ ksh -c 'function b { typeset a=1; a; echo $a; }; function a { typeset -gr a=2; echo $a; }; a=0; b; echo $a'
2
1
2
~$ ksh -c 'function b { typeset a=1; a; echo $a; }; function a { readonly a=2; echo $a; }; a=0; b; echo $a'
2
1
2
~$ ksh -c 'function b { typeset -x a=1; a; echo $a; }; function a { readonly a=2; echo $a; }; a=0; b; echo $a'
2
1
0
~$ ksh -c 'function b { typeset -x a=1; a; echo $a; a=3; }; function a { readonly a=2; echo $a; }; a=0; b; echo $a'
2
1
0
readonly is still not equivalent to typeset -gr as it affects
the variable in the scope where it currently exists (whether
local, global or exported from an ancestor), not only in the
global scope:
$ ksh -c 'function a { typeset a=1; typeset -gr a; echo $a; }; a=0; a; echo $a; a=2'
1
0
ksh: a: is read only
$ ksh -c 'function a { typeset a=1; readonly a; echo $a; }; a=0; a; echo $a; a=2'
1
0
--
Stephane
Messages sorted by:
Reverse Date,
Date,
Thread,
Author