zsh's 'typeset -U path' wipes out path/PATH

zsh's 'typeset -U path' wipes out path/PATH

Post by kj » Mon, 27 Dec 2004 00:44:33



I've found a bug (or at best a very perverse "feature") in zsh; it
can be illustrated by the following three short scripts:

# script_A
PATH=/usr/local/bin:/usr/bin:/bin
echo $#path
typeset -U path
echo $#path
# eof

# script_B
source script_A
# eof

# script_C
c_fxn () { source script_A }
c_fxn
# eof

Note that both the contents of script_B and the body of the function
c_fxn defined in script_C consist of the same one line, namely
"source script_A".  Now,

% source script_B
3
3
% source script_C
3
0

In words, when script_A is sourced within a script that is itself
being sourced, typeset -U path preserves the components of PATH
(or at least their number), but if script_A is sourced within the
body of a *function*, calling the function causes the expression
typeset -U path to *clear* the contents of PATH.

Please-please-please don't tell me this is a feature!  I'd lose
all faith in the designers of zsh if this turns out to be a feature!

More importantly, how does one get around this problem.  I've tried
saving the value of $path before calling 'typeset -U' on it, and
restoring it afterwards, but the results have been disastrous (I've
tried too many variants to describe them all).

kj

--
NOTE: In my address everything before the first period is backwards;
and the last period, and everything after it, should be discarded.

 
 
 

zsh's 'typeset -U path' wipes out path/PATH

Post by KKramsc » Mon, 27 Dec 2004 05:24:02



Quote:>I've found a bug (or at best a very perverse "feature") in zsh; it
>can be illustrated by the following three short scripts:
># script_A
>PATH=/usr/local/bin:/usr/bin:/bin
>echo $#path
>typeset -U path
>echo $#path
># eof
># script_B
>source script_A
># eof
># script_C
>c_fxn () { source script_A }
>c_fxn
># eof
>Note that both the contents of script_B and the body of the function
>c_fxn defined in script_C consist of the same one line, namely
>"source script_A".  Now,
>% source script_B
>3
>3
>% source script_C
>3
>0
>In words, when script_A is sourced within a script that is itself
>being sourced, typeset -U path preserves the components of PATH
>(or at least their number), but if script_A is sourced within the
>body of a *function*, calling the function causes the expression
>typeset -U path to *clear* the contents of PATH.
>Please-please-please don't tell me this is a feature!  I'd lose
>all faith in the designers of zsh if this turns out to be a feature!

Neither bug nor feature.  It's just a fact.  Within a function you
have a whole new scope, and typeset basically cleans a variable's clock.

Quote:>More importantly, how does one get around this problem.  I've tried
>saving the value of $path before calling 'typeset -U' on it, and
>restoring it afterwards, but the results have been disastrous (I've
>tried too many variants to describe them all).

Well, I don't know what problems you ran into, but this works for
me

  local -a hold
  hold=($path)
  typeset -U path       # 'declare -U path' looks/sounds better to me
  path=($hold)

Caveat: I'm new at this; there are probably better ways.

        Karl

--
Sent from a spam-bucket account; I check it once in a blue moon.  If
you still want to e-mail me, cut out the extension from my address,
and make the obvious substitutions on what's left.