zsh-workers
 help / color / mirror / code / Atom feed
* [BUG] functions can't create global readonly variables
@ 2016-01-02  3:27 Martijn Dekker
  2016-01-02  4:27 ` Bart Schaefer
  0 siblings, 1 reply; 7+ messages in thread
From: Martijn Dekker @ 2016-01-02  3:27 UTC (permalink / raw)
  To: zsh-workers

It is apparently impossible to create global readonly variables from
within a function.

If a variable is created with a command like
    readonly var=value
then the entire variable gets a function-local scope, as if 'local' were
used.

If a global variable is set first, then set to readonly with 'readonly'
or 'typeset -r', then the read-only attribute only lasts within the
function, and the global variable remains read/write.

I don't know if this is intended behaviour for native zsh mode; the
'zshbuiltins' man page does not mention anything about this. But it is
certainly a bug for POSIX/'emulate sh' mode.

Steps to reproduce:

$ fn() { readonly var=foo; var=bar; }
$ fn
fn: read-only variable: var
$ echo $var

$ var=writtento
$ echo $var
writtento
$ fn() { var=foo; readonly var; var=bar; }
$ fn
fn: read-only variable: var
$ echo $var
foo
$ var=writtento
$ echo $var
writtento

Confirmed in zsh 4.1.1 through current.

Thanks,

- Martijn


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2016-01-03 19:21 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-01-02  3:27 [BUG] functions can't create global readonly variables Martijn Dekker
2016-01-02  4:27 ` Bart Schaefer
2016-01-02 18:18   ` Peter Stephenson
2016-01-02 20:36     ` Bart Schaefer
2016-01-03 19:00       ` Peter Stephenson
2016-01-03 19:21         ` Bart Schaefer
2016-01-02 19:39   ` Martijn Dekker

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).