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

[BUG] functions can't create global readonly variables



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