zsh-workers
 help / color / mirror / code / Atom feed
From: Michael Wardle <michael@endbracket.net>
To: zsh-workers@sunsite.dk
Subject: problem redeclaring path variable (ksh incompatibility)
Date: Fri, 18 Mar 2005 01:31:38 +1100	[thread overview]
Message-ID: <423994CA.4010609@endbracket.net> (raw)

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


             reply	other threads:[~2005-03-17 14:33 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-03-17 14:31 Michael Wardle [this message]
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

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=423994CA.4010609@endbracket.net \
    --to=michael@endbracket.net \
    --cc=zsh-workers@sunsite.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).