* Should declare -p add a new declaration inside a function? @ 2008-09-13 14:52 Rocky Bernstein 2008-09-13 19:55 ` Peter Stephenson 0 siblings, 1 reply; 5+ messages in thread From: Rocky Bernstein @ 2008-09-13 14:52 UTC (permalink / raw) To: Zsh Hackers' List [-- Attachment #1: Type: text/plain, Size: 621 bytes --] It appears that using "declare" or "typeset" with the -p (print) flag will add a new declaration inside a function. Is this really desirable? It seems to reduce the usefulness of -p. Here's an example: declare -a xx xx=(an array) bug() { echo xx is \"${xx[@]}\" before \"declare -p xx\" echo 'declare -p xx:' declare -p xx echo xx is now \"${xx[@]}\" in bug echo '====' } bug echo 'declare -p xx outside bug:' declare -p xx Running this produces: xx is "an array" before "declare -p xx" declare -p xx: typeset xx='' xx is now "" in bug ==== declare -p xx outside bug: typeset -a xx xx=(an array) [-- Attachment #2: Type: text/html, Size: 933 bytes --] ^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Should declare -p add a new declaration inside a function? 2008-09-13 14:52 Should declare -p add a new declaration inside a function? Rocky Bernstein @ 2008-09-13 19:55 ` Peter Stephenson 2008-09-14 0:48 ` Rocky Bernstein 0 siblings, 1 reply; 5+ messages in thread From: Peter Stephenson @ 2008-09-13 19:55 UTC (permalink / raw) To: Zsh Hackers' List On Sat, 13 Sep 2008 10:52:35 -0400 "Rocky Bernstein" <rocky.bernstein@gmail.com> wrote: > It appears that using "declare" or "typeset" with the -p (print) flag will > add a new declaration inside a function. Is this really desirable? It seems > to reduce the usefulness of -p. That sure as heck looks like a bug to me. It shouldn't ever create anything, even if the variable doesn't exist; it's purely for information. I don't think "declare -p" has had much love and attention; it doesn't look like it's ever worked with arguments other than patterns (with -m). Index: Src/builtin.c =================================================================== RCS file: /cvsroot/zsh/zsh/Src/builtin.c,v retrieving revision 1.205 diff -u -r1.205 builtin.c --- Src/builtin.c 11 Sep 2008 17:14:39 -0000 1.205 +++ Src/builtin.c 13 Sep 2008 19:49:33 -0000 @@ -2473,10 +2473,17 @@ /* Take arguments literally. Don't glob */ while ((asg = getasg(*argv++))) { - if (!typeset_single(name, asg->name, - (Param) (paramtab == realparamtab ? - gethashnode2(paramtab, asg->name) : - paramtab->getnode(paramtab, asg->name)), + HashNode hn = (paramtab == realparamtab ? + gethashnode2(paramtab, asg->name) : + paramtab->getnode(paramtab, asg->name)); + if (OPT_ISSET(ops,'p')) { + if (hn) + printparamnode(hn, printflags); + else + zwarnnam(name, "no such variable: %s", asg->name); + continue; + } + if (!typeset_single(name, asg->name, (Param)hn, func, on, off, roff, asg->value, NULL, ops, 0)) returnval = 1; Index: Test/B02typeset.ztst =================================================================== RCS file: /cvsroot/zsh/zsh/Test/B02typeset.ztst,v retrieving revision 1.18 diff -u -r1.18 B02typeset.ztst --- Test/B02typeset.ztst 16 Dec 2007 14:05:16 -0000 1.18 +++ Test/B02typeset.ztst 13 Sep 2008 19:49:33 -0000 @@ -444,3 +444,12 @@ 0:Lower case conversion, does not apply to values used internally >lower >value of $lower + + typeset -a array + array=(foo bar) + fn() { typeset -p array nonexistent; } + fn +0:declare -p shouldn't create scoped values +>typeset -a array +>array=(foo bar) +?fn:typeset: no such variable: nonexistent -- Peter Stephenson <p.w.stephenson@ntlworld.com> Web page now at http://homepage.ntlworld.com/p.w.stephenson/ ^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Should declare -p add a new declaration inside a function? 2008-09-13 19:55 ` Peter Stephenson @ 2008-09-14 0:48 ` Rocky Bernstein 2008-09-15 8:53 ` Peter Stephenson 0 siblings, 1 reply; 5+ messages in thread From: Rocky Bernstein @ 2008-09-14 0:48 UTC (permalink / raw) To: Peter Stephenson; +Cc: Zsh Hackers' List [-- Attachment #1: Type: text/plain, Size: 3318 bytes --] Thanks. This helps a lot. A couple things to consider. Right now if the variable is not declared, declare -p returns 0. Should it be nonzero as is the case if you give a bad option? Is there a way to figure out or get a list of local variables versus non-local variables versus global variables? By "local" I mean those that were defined in the most recent scope and by "global" I mean those defined outside of any function nesting, and by "non-local" I mean "not defined in the immediate scope." Thanks again On Sat, Sep 13, 2008 at 3:55 PM, Peter Stephenson < p.w.stephenson@ntlworld.com> wrote: > On Sat, 13 Sep 2008 10:52:35 -0400 > "Rocky Bernstein" <rocky.bernstein@gmail.com> wrote: > > It appears that using "declare" or "typeset" with the -p (print) flag > will > > add a new declaration inside a function. Is this really desirable? It > seems > > to reduce the usefulness of -p. > > That sure as heck looks like a bug to me. It shouldn't ever create > anything, even if the variable doesn't exist; it's purely for > information. I don't think "declare -p" has had much love and > attention; it doesn't look like it's ever worked with arguments > other than patterns (with -m). > > Index: Src/builtin.c > =================================================================== > RCS file: /cvsroot/zsh/zsh/Src/builtin.c,v > retrieving revision 1.205 > diff -u -r1.205 builtin.c > --- Src/builtin.c 11 Sep 2008 17:14:39 -0000 1.205 > +++ Src/builtin.c 13 Sep 2008 19:49:33 -0000 > @@ -2473,10 +2473,17 @@ > > /* Take arguments literally. Don't glob */ > while ((asg = getasg(*argv++))) { > - if (!typeset_single(name, asg->name, > - (Param) (paramtab == realparamtab ? > - gethashnode2(paramtab, asg->name) : > - paramtab->getnode(paramtab, > asg->name)), > + HashNode hn = (paramtab == realparamtab ? > + gethashnode2(paramtab, asg->name) : > + paramtab->getnode(paramtab, asg->name)); > + if (OPT_ISSET(ops,'p')) { > + if (hn) > + printparamnode(hn, printflags); > + else > + zwarnnam(name, "no such variable: %s", asg->name); > + continue; > + } > + if (!typeset_single(name, asg->name, (Param)hn, > func, on, off, roff, asg->value, NULL, > ops, 0)) > returnval = 1; > Index: Test/B02typeset.ztst > =================================================================== > RCS file: /cvsroot/zsh/zsh/Test/B02typeset.ztst,v > retrieving revision 1.18 > diff -u -r1.18 B02typeset.ztst > --- Test/B02typeset.ztst 16 Dec 2007 14:05:16 -0000 1.18 > +++ Test/B02typeset.ztst 13 Sep 2008 19:49:33 -0000 > @@ -444,3 +444,12 @@ > 0:Lower case conversion, does not apply to values used internally > >lower > >value of $lower > + > + typeset -a array > + array=(foo bar) > + fn() { typeset -p array nonexistent; } > + fn > +0:declare -p shouldn't create scoped values > +>typeset -a array > +>array=(foo bar) > +?fn:typeset: no such variable: nonexistent > > > -- > Peter Stephenson <p.w.stephenson@ntlworld.com> > Web page now at http://homepage.ntlworld.com/p.w.stephenson/ > [-- Attachment #2: Type: text/html, Size: 5080 bytes --] ^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Should declare -p add a new declaration inside a function? 2008-09-14 0:48 ` Rocky Bernstein @ 2008-09-15 8:53 ` Peter Stephenson 2008-09-15 15:58 ` Rocky Bernstein 0 siblings, 1 reply; 5+ messages in thread From: Peter Stephenson @ 2008-09-15 8:53 UTC (permalink / raw) To: Zsh Hackers' List On Sat, 13 Sep 2008 20:48:56 -0400 "Rocky Bernstein" <rocky.bernstein@gmail.com> wrote: > A couple things to consider. Right now if the variable is not declared, > declare -p returns 0. Should it be nonzero as is the case if you give a bad > option? Yes, I missed that the first time. > Is there a way to figure out or get a list of local variables versus > non-local variables versus global variables? By "local" I mean those that > were defined in the most recent scope and by "global" I mean those defined > outside of any function nesting, and by "non-local" I mean "not defined in > the immediate scope." You can use $parameters: fn() { local foo print ${(k)parameters[(R)*local*]} } ${(k)parameters[(R)^*local*]} (with extendedglob) gets you the global parameters. There's no way of telling which function level a variable is local at. Index: Src/builtin.c =================================================================== RCS file: /cvsroot/zsh/zsh/Src/builtin.c,v retrieving revision 1.206 diff -u -r1.206 builtin.c --- Src/builtin.c 13 Sep 2008 19:59:14 -0000 1.206 +++ Src/builtin.c 15 Sep 2008 08:52:40 -0000 @@ -2479,8 +2479,10 @@ if (OPT_ISSET(ops,'p')) { if (hn) printparamnode(hn, printflags); - else + else { zwarnnam(name, "no such variable: %s", asg->name); + returnval = 1; + } continue; } if (!typeset_single(name, asg->name, (Param)hn, Index: Test/B02typeset.ztst =================================================================== RCS file: /cvsroot/zsh/zsh/Test/B02typeset.ztst,v retrieving revision 1.19 diff -u -r1.19 B02typeset.ztst --- Test/B02typeset.ztst 13 Sep 2008 19:59:14 -0000 1.19 +++ Test/B02typeset.ztst 15 Sep 2008 08:52:40 -0000 @@ -449,7 +449,7 @@ array=(foo bar) fn() { typeset -p array nonexistent; } fn -0:declare -p shouldn't create scoped values +1:declare -p shouldn't create scoped values >typeset -a array >array=(foo bar) ?fn:typeset: no such variable: nonexistent -- Peter Stephenson <pws@csr.com> Software Engineer CSR PLC, Churchill House, Cambridge Business Park, Cowley Road Cambridge, CB4 0WZ, UK Tel: +44 (0)1223 692070 ^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Should declare -p add a new declaration inside a function? 2008-09-15 8:53 ` Peter Stephenson @ 2008-09-15 15:58 ` Rocky Bernstein 0 siblings, 0 replies; 5+ messages in thread From: Rocky Bernstein @ 2008-09-15 15:58 UTC (permalink / raw) To: Peter Stephenson; +Cc: Zsh Hackers' List [-- Attachment #1: Type: text/plain, Size: 2942 bytes --] On Mon, Sep 15, 2008 at 4:53 AM, Peter Stephenson <pws@csr.com> wrote: > On Sat, 13 Sep 2008 20:48:56 -0400 > "Rocky Bernstein" <rocky.bernstein@gmail.com> wrote: > > A couple things to consider. Right now if the variable is not declared, > > declare -p returns 0. Should it be nonzero as is the case if you give a > bad > > option? > > Yes, I missed that the first time. Thanks. As many times in the past, this helps a lot. I had tried to parse error output in a regular expression but that's problematic and not as simple. > > > > Is there a way to figure out or get a list of local variables versus > > non-local variables versus global variables? By "local" I mean those that > > were defined in the most recent scope and by "global" I mean those > defined > > outside of any function nesting, and by "non-local" I mean "not defined > in > > the immediate scope." > > You can use $parameters: > > fn() { > local foo > print ${(k)parameters[(R)*local*]} > } Interesting. This seems to show any variables defined local somewhere in the call stack (not just the immediate one). > > ${(k)parameters[(R)^*local*]} (with extendedglob) gets you the global > parameters. There's no way of telling which function level a variable is > local at. There are a number of possibilities for tracking what variable goes with what stack frame. Probably best to defer deciding on how exactly to deal with. > > > Index: Src/builtin.c > =================================================================== > RCS file: /cvsroot/zsh/zsh/Src/builtin.c,v > retrieving revision 1.206 > diff -u -r1.206 builtin.c > --- Src/builtin.c 13 Sep 2008 19:59:14 -0000 1.206 > +++ Src/builtin.c 15 Sep 2008 08:52:40 -0000 > @@ -2479,8 +2479,10 @@ > if (OPT_ISSET(ops,'p')) { > if (hn) > printparamnode(hn, printflags); > - else > + else { > zwarnnam(name, "no such variable: %s", asg->name); > + returnval = 1; > + } > continue; > } > if (!typeset_single(name, asg->name, (Param)hn, > Index: Test/B02typeset.ztst > =================================================================== > RCS file: /cvsroot/zsh/zsh/Test/B02typeset.ztst,v > retrieving revision 1.19 > diff -u -r1.19 B02typeset.ztst > --- Test/B02typeset.ztst 13 Sep 2008 19:59:14 -0000 1.19 > +++ Test/B02typeset.ztst 15 Sep 2008 08:52:40 -0000 > @@ -449,7 +449,7 @@ > array=(foo bar) > fn() { typeset -p array nonexistent; } > fn > -0:declare -p shouldn't create scoped values > +1:declare -p shouldn't create scoped values > >typeset -a array > >array=(foo bar) > ?fn:typeset: no such variable: nonexistent > > > -- > Peter Stephenson <pws@csr.com> Software Engineer > CSR PLC, Churchill House, Cambridge Business Park, Cowley Road > Cambridge, CB4 0WZ, UK Tel: +44 (0)1223 692070 > [-- Attachment #2: Type: text/html, Size: 4964 bytes --] ^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2008-09-15 15:59 UTC | newest] Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2008-09-13 14:52 Should declare -p add a new declaration inside a function? Rocky Bernstein 2008-09-13 19:55 ` Peter Stephenson 2008-09-14 0:48 ` Rocky Bernstein 2008-09-15 8:53 ` Peter Stephenson 2008-09-15 15:58 ` Rocky Bernstein
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).