zsh-workers
 help / color / mirror / code / Atom feed
* unset and locals
@ 1996-08-04  8:07 Bart Schaefer
  0 siblings, 0 replies; only message in thread
From: Bart Schaefer @ 1996-08-04  8:07 UTC (permalink / raw)
  To: zsh-workers

This is so weird I'm having a hard time coming up with an example.  Consider
these two functions:

one() {
  local x=1
  echo one $x before two
  two
  echo one $x after two
}

two() {
   local x=2
   echo two $x before unset
   unset x
   echo two $x after unset
   unset x
   echo two $x after repeat of unset
}

Executing "one" in zsh produces the output:

one 1 before two
two 2 before unset
two after unset
two after repeat of unset
one 1 after two

Now consider changing "two" as follows:

unlocal() {
  unset $1
}

two() {
   local x=2
   echo two $x before unlocal
   unlocal x
   echo two $x after unlocal
   unlocal x
   echo two $x after repeat of unlocal
}

This produces the output:

one 1 before two
two 2 before unlocal
two 1 after unlocal
two after repeat of unlocal
one after two

Egad!  By this strategem, "two" has succeeded in first unhiding (which is
good) and then unsetting (which in this instance is bad) a local variable
scoped in "one"!

Is there another way to "unhide" a variable from a higher scope?  A way
that is guaranteed not to accidentally *unset* that same variable?

-- 
Bart Schaefer                             Brass Lantern Enterprises
http://www.well.com/user/barts            http://www.nbn.com/people/lantern

New male in /home/schaefer:
>N  2 Justin William Schaefer  Sat May 11 03:43  53/4040  "Happy Birthday"


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~1996-08-04  8:40 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1996-08-04  8:07 unset and locals Bart Schaefer

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