zsh-workers
 help / color / mirror / code / Atom feed
From: Martijn Dekker <martijn@inlv.org>
To: zsh-workers@zsh.org
Subject: [BUG] functions can't create global readonly variables
Date: Sat, 02 Jan 2016 04:27:15 +0100	[thread overview]
Message-ID: <56874393.1010001@inlv.org> (raw)

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


             reply	other threads:[~2016-01-02  3:27 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-01-02  3:27 Martijn Dekker [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=56874393.1010001@inlv.org \
    --to=martijn@inlv.org \
    --cc=zsh-workers@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).