From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 7127 invoked by alias); 29 Apr 2015 19:27:26 -0000 Mailing-List: contact zsh-workers-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Workers List List-Post: List-Help: X-Seq: 35000 Received: (qmail 7412 invoked from network); 29 Apr 2015 19:27:24 -0000 X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-0.5 required=5.0 tests=BAYES_00,DKIM_ADSP_CUSTOM_MED, FREEMAIL_FROM,NML_ADSP_CUSTOM_MED,RCVD_IN_DNSWL_LOW,RCVD_NUMERIC_HELO, SPF_HELO_PASS,T_FSL_HELO_BARE_IP_2 autolearn=no version=3.3.2 X-Injected-Via-Gmane: http://gmane.org/ To: zsh-workers@zsh.org From: Stephane Chazelas Subject: Re: [BUG] Can't mark unset variables as read-only Date: Wed, 29 Apr 2015 20:09:09 +0100 Message-ID: <20150429190909.GB5530@chaz.gmail.com> References: <55407BBF.6020401@inlv.org> <20150429113602.374240c7@pwslap01u.europe.root.pri> <150429065556.ZM31553@torch.brasslantern.com> <20150429154136.152b7b05@pwslap01u.europe.root.pri> <5540F9B6.5020805@case.edu> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Complaints-To: usenet@ger.gmane.org X-Gmane-NNTP-Posting-Host: 2.121.20.249 Content-Disposition: inline In-Reply-To: <5540F9B6.5020805@case.edu> User-Agent: Mutt/1.5.21 (2010-09-15) 2015-04-29 11:33:10 -0400, Chet Ramey: [...] > func() > { > local foo=bar > echo inside: $foo > } > > func > echo outside: $foo > > Bash gives the following output: > > ./x26: line 5: local: foo: readonly variable > inside: immutable > outside: immutable > > If the purpose of readonly is to make a particular name/value pair > immutable, I think that allowing a local variable to shadow it, > especially if you're going to export that local variable, is a bad thing. [...] I don't agree. The whole point of having local variables is to avoid problems with clashing namespaces. You can have: f() { local foo=1 # f's own variable g xxx echo "$foo" } g() { local foo=2 blah blah } (f and g possibly in different libraries) By declaring foo local, you make sure that you're not clashing with someone else's foo. You don't have to worry on how you name your functions like you do in POSIX sh (where you'd need to call one variable f_foo, and the other g_foo for intance). Above, if you do: f() { readonly foo=2 g xxx echo "$foo" } You're basically breaking g for no good reason. f's doesn't want the value of f to be modified, but g would not have modified it since it's declaring its *own* foo variable. Having said that, I don't remember ever using "readonly" in a script so I can't say I care much. -- Stephane