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

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



On Sat, 13 Sep 2008 20:48:56 -0400
"Rocky Bernstein" <rocky.bernstein@xxxxxxxxx> 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@xxxxxxx>                  Software Engineer
CSR PLC, Churchill House, Cambridge Business Park, Cowley Road
Cambridge, CB4 0WZ, UK                          Tel: +44 (0)1223 692070



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