Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Should declare -p add a new declaration inside a function?



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@xxxxxxxxxxxx> wrote:
On Sat, 13 Sep 2008 10:52:35 -0400
"Rocky Bernstein" <rocky.bernstein@xxxxxxxxx> 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@xxxxxxxxxxxx>
Web page now at http://homepage.ntlworld.com/p.w.stephenson/



Messages sorted by: Reverse Date, Date, Thread, Author