zsh-workers
 help / color / mirror / code / Atom feed
* problem redeclaring path variable (ksh incompatibility)
@ 2005-03-17 14:31 Michael Wardle
  2005-03-17 16:50 ` Bart Schaefer
  0 siblings, 1 reply; 6+ messages in thread
From: Michael Wardle @ 2005-03-17 14:31 UTC (permalink / raw)
  To: zsh-workers

Hi

I have a script that uses the identifier "path" local to a function.  It 
works as intended in bash and all the versions of ksh I've tried it on, 
but not in zsh, which gives an error message similar to this:
addpath:typeset:6: path: can't assign initial value for array

It turns out that neither "typeset path=" nor "typeset path=value" 
create a local scalar, which doesn't meet my expectations.  Strangely, 
however, "typeset path" does.

It also came as a bit of a surprise to me that "path" should be declared 
at all when zsh is emulating sh or ksh, but I don't see a good reason to 
remove that.

A test case follows in case you will consider investigating this problem 
and modifying zsh to act as bash does:
----------
#!/bin/zsh -p
# test case for zsh dynamic scoping of "path" variable
# Michael Wardle <michael@endbracket.net>
# invoked using -p to ignore user rc files

[ -n "$ZSH_VERSION" ] && emulate ksh    # arrays are 0 indexed, etc.
set -e                                  # exit on error

func()
{
         # create a dynamically scoped variable in function scope
         # the identifier exists at global scope as an array but is
         # redeclared here as a scalar
         #typeset path                   # this works
         typeset path="local0"           # this doesn't work
         path="local0"
         path="$path local1"
         echo "Inside function, values are:"
         set -- $path
         for elem
         do
                 echo "$elem"
         done
}

# create a dynamically scoped variable in global scope
typeset -a path
#export path            # path being exported doesn't matter
path[0]=global0
path[1]=global1
# display the globally scoped values
echo "Outside function, values are:"
set -- ${path[@]}
for elem
do
         echo "$elem"
done

func

# test that the globally scoped values are restored
echo "Outside function, values are:"
set -- ${path[@]}
for elem
do
         echo "$elem"
done
----------


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2005-03-18 10:52 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-03-17 14:31 problem redeclaring path variable (ksh incompatibility) Michael Wardle
2005-03-17 16:50 ` Bart Schaefer
2005-03-17 17:28   ` Bart Schaefer
2005-03-17 21:42   ` Michael Wardle
2005-03-18  2:42     ` Bart Schaefer
2005-03-18 10:52   ` Peter Stephenson

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