Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

problem redeclaring path variable (ksh incompatibility)



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@xxxxxxxxxxxxxx>
# 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
----------



Messages sorted by: Reverse Date, Date, Thread, Author