From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 14876 invoked from network); 15 Sep 2008 15:59:07 -0000 X-Spam-Checker-Version: SpamAssassin 3.2.5 (2008-06-10) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-2.6 required=5.0 tests=AWL,BAYES_00,HTML_MESSAGE autolearn=ham version=3.2.5 Received: from news.dotsrc.org (HELO a.mx.sunsite.dk) (130.225.247.88) by ns1.primenet.com.au with SMTP; 15 Sep 2008 15:59:07 -0000 Received-SPF: none (ns1.primenet.com.au: domain at sunsite.dk does not designate permitted sender hosts) Received: (qmail 68897 invoked from network); 15 Sep 2008 15:58:59 -0000 Received: from sunsite.dk (130.225.247.90) by a.mx.sunsite.dk with SMTP; 15 Sep 2008 15:58:59 -0000 Received: (qmail 22092 invoked by alias); 15 Sep 2008 15:58:53 -0000 Mailing-List: contact zsh-workers-help@sunsite.dk; run by ezmlm Precedence: bulk X-No-Archive: yes X-Seq: 25674 Received: (qmail 22074 invoked from network); 15 Sep 2008 15:58:53 -0000 Received: from bifrost.dotsrc.org (130.225.254.106) by sunsite.dk with SMTP; 15 Sep 2008 15:58:53 -0000 Received: from py-out-1112.google.com (py-out-1112.google.com [64.233.166.177]) by bifrost.dotsrc.org (Postfix) with ESMTP id 3A38E80271AE for ; Mon, 15 Sep 2008 17:58:36 +0200 (CEST) Received: by py-out-1112.google.com with SMTP id u77so1658645pyb.23 for ; Mon, 15 Sep 2008 08:58:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:message-id:date:from:to :subject:cc:in-reply-to:mime-version:content-type:references; bh=rKMiVPwanKdEdEaG4fMRpa3aWvS1n+GM07xtMcsG3GY=; b=tsSkoGnUoiRgNpF5rEmmqi+dVgia09LpT6ELPHNdL9KSN4yoEPYM38oXLz8BV8s4vV RQhY7CNxwQRUvY7VKIW2p5cxLyyAjLUUQOTS+KNNU9E2YdMoUO/rdtTff095fFcp2AtK sNYIVO5TQNMJldNmq24Ph6Nlt2/wdIMbaJmDM= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=message-id:date:from:to:subject:cc:in-reply-to:mime-version :content-type:references; b=jpXYqbx1NAGzoM500bB+61fpfj9vLEMECY4ccxnxU3j9QTK/NrKjuKqJnCTR27PMzL kYZXiAj9nCeejNNgW/9pRdV74U/X/1Y3Lxcp8Zj6G0/JJ20aBSCY2SAuEwSmo7G/9CwG aEsQ31dtrJhZM+YzSQkgYUCuwoGnb3hspj69o= Received: by 10.114.56.4 with SMTP id e4mr6352607waa.0.1221494308605; Mon, 15 Sep 2008 08:58:28 -0700 (PDT) Received: by 10.114.159.2 with HTTP; Mon, 15 Sep 2008 08:58:28 -0700 (PDT) Message-ID: <6cd6de210809150858x54ebc2a2i9032ea6b5ccde922@mail.gmail.com> Date: Mon, 15 Sep 2008 11:58:28 -0400 From: "Rocky Bernstein" To: "Peter Stephenson" Subject: Re: Should declare -p add a new declaration inside a function? Cc: "Zsh Hackers' List" In-Reply-To: <20080915095311.26f8a863@news01> MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_Part_4224_20201204.1221494308604" References: <6cd6de210809130752p4a5dc634v6144b0475e2cc569@mail.gmail.com> <20080913205540.4d80fcf6@pws-pc> <6cd6de210809131748y5dba13dbpef40377e95632206@mail.gmail.com> <20080915095311.26f8a863@news01> X-Virus-Scanned: ClamAV 0.92.1/8248/Mon Sep 15 17:28:54 2008 on bifrost X-Virus-Status: Clean ------=_Part_4224_20201204.1221494308604 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline On Mon, Sep 15, 2008 at 4:53 AM, Peter Stephenson wrote: > On Sat, 13 Sep 2008 20:48:56 -0400 > "Rocky Bernstein" 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 Software Engineer > CSR PLC, Churchill House, Cambridge Business Park, Cowley Road > Cambridge, CB4 0WZ, UK Tel: +44 (0)1223 692070 > ------=_Part_4224_20201204.1221494308604 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline


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

------=_Part_4224_20201204.1221494308604--