Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
[BUG] functions can't create global readonly variables
- X-seq: zsh-workers 37484
- From: Martijn Dekker <martijn@xxxxxxxx>
- To: zsh-workers@xxxxxxx
- Subject: [BUG] functions can't create global readonly variables
- Date: Sat, 02 Jan 2016 04:27:15 +0100
- List-help: <mailto:zsh-workers-help@zsh.org>
- List-id: Zsh Workers List <zsh-workers.zsh.org>
- List-post: <mailto:zsh-workers@zsh.org>
- Mailing-list: contact zsh-workers-help@xxxxxxx; run by ezmlm
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
Messages sorted by:
Reverse Date,
Date,
Thread,
Author