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

Re: Function-scoped parameters?



On Wed, 23 Feb 2011, Sebastian Stark wrote:

Am 23.02.2011 um 19:03 schrieb Benjamin R. Haskell:

This doesn't work, probably unsurprisingly. But is there a way to accomplish it?

function () {
   emulate -L zsh
   local x=asdf
   trap 'echo x is ${x:-unset}' EXIT
}

The use case is that I want to assign a local parameter 'temp' to be set to the name of a temporary file. If anything goes wrong in the function, I'd like that temporary file to be removed, but I don't want 'temp' to leak out of the function scope.

e.g.:
dosomething () {
   emulate -L zsh
   local temp=$(mktemp)
   setopt err_return
   trap '(( $+temp )) && rm $temp' EXIT
   # ...
}

Would "setopt localtraps" be of any help?


From the documentation for 'emulate':

[...] If the -L option is given, the options LOCAL_OPTIONS and LOCAL_TRAPS will be set as well[...]

So, it's already set by `emulate -L zsh`.

--
Best,
Ben



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