From: Stephane Chazelas <stephane@chazelas.org>
To: Bart Schaefer <schaefer@brasslantern.com>
Cc: Zsh hackers list <zsh-workers@zsh.org>
Subject: Re: "typeset -p" and no_GLOBAL_EXPORT, other misc.
Date: Tue, 12 Mar 2024 08:49:26 +0000 [thread overview]
Message-ID: <20240312084926.d6vbk75ozud7i2jm@chazelas.org> (raw)
In-Reply-To: <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
next prev parent reply other threads:[~2024-03-12 8:49 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-03-12 4:13 Bart Schaefer
2024-03-12 8:49 ` Stephane Chazelas [this message]
2024-03-12 18:32 ` Bart Schaefer
2024-03-12 20:06 ` Stephane Chazelas
2024-03-12 20:31 ` Bart Schaefer
2024-03-12 20:48 ` Stephane Chazelas
2024-03-12 21:02 ` Bart Schaefer
2024-03-12 20:26 ` Stephane Chazelas
2024-03-12 20:38 ` Bart Schaefer
2024-03-12 20:58 ` Stephane Chazelas
2024-03-12 21:03 ` Bart Schaefer
2024-03-13 0:52 ` {PATCH] (for real this time) " Bart Schaefer
2024-03-14 2:11 ` [PATCH] More "typeset -p" and GLOBAL_EXPORT Bart Schaefer
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20240312084926.d6vbk75ozud7i2jm@chazelas.org \
--to=stephane@chazelas.org \
--cc=schaefer@brasslantern.com \
--cc=zsh-workers@zsh.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://git.vuxu.org/mirror/zsh/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).