zsh-users
 help / color / mirror / code / Atom feed
From: "Bart Schaefer" <schaefer@candle.brasslantern.com>
To: "Andrej Borsenkow" <Andrej.Borsenkow@mow.siemens.ru>,
	"ZSH users mailing list" <zsh-users@sunsite.auc.dk>
Subject: Re: Local variables in sourced scripts
Date: Thu, 29 Jun 2000 15:31:20 +0000	[thread overview]
Message-ID: <1000629153121.ZM7765@candle.brasslantern.com> (raw)
In-Reply-To: <000101bfe1c0$86504080$21c9ca95@mow.siemens.ru>

On Jun 29,  3:52pm, Andrej Borsenkow wrote:
} Subject: Local variables in sourced scripts
}
} Am I the only one who finds it useful? I'd really appreciate them in rc
} scripts - I always forget to unset temporary variables there and no fear
} to clobber some existing ones ...

You can't have it both ways.  In shell functions all variables are local
unless either explicitly declared global (with "typeset -g", and that only
works in 3.1.6+) or assigned-to without declaring.  

The problem is that you can't create any variables other than plain scalars
and simple arrays without declaring them.  If you make variables local to
rc files, then all declarations of integers, floats, associative arrays,
unique arrays ("typeset -U"), etc. must use -g, which will probably break
every nontrivial .zshrc on the planet (I know it'd screw mine up big time).

} Related question - I think, local functions were at least discussed
} sometimes. What is current status?

You can make a function with local scope, but only if it has the same
name as another function with global scope.  Like this:

    zmodload -i zsh/parameter
    function uselocalfn {
	local -A +h functions
	eval "function localfn {
	    echo this is the $1 local function
	}"
	localfn
    }

zagzig[104] localfn
zsh: command not found: localfn
zagzig[105] uselocalfn first
this is the first local function
zagzig[106] localfn
this is the first local function
zagzig[107] uselocalfn second
this is the second local function
zagzig[108] localfn
this is the first local function

This happens because the `functions' parameter has its value restored by
individual assignment to each associative array element.  Consequently,
only elements that existed before the local copy was created have their
old value restored; newly-created elements do not get erased.

Of course it's incredibly inefficient to redefine every global function
every time you want to use a single local function, so this probably is
in serious need of improvement.  Another side-effect is that when the
global value of $functions is restored, every autoloaded (aka undefined)
function turns into a defined function.  They still work as if they're
autoloaded, thanks to the magic of "autoload -X", but they no longer are
flagged as undefined.

-- 
Bart Schaefer                                 Brass Lantern Enterprises
http://www.well.com/user/barts              http://www.brasslantern.com

Zsh: http://www.zsh.org | PHPerl Project: http://phperl.sourceforge.net   


      reply	other threads:[~2000-06-29 15:41 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2000-06-29 11:52 Andrej Borsenkow
2000-06-29 15:31 ` Bart Schaefer [this message]

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=1000629153121.ZM7765@candle.brasslantern.com \
    --to=schaefer@candle.brasslantern.com \
    --cc=Andrej.Borsenkow@mow.siemens.ru \
    --cc=zsh-users@sunsite.auc.dk \
    /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).