* Function-scoped parameters?
@ 2011-02-23 18:03 Benjamin R. Haskell
2011-02-23 18:27 ` Benjamin R. Haskell
` (2 more replies)
0 siblings, 3 replies; 6+ messages in thread
From: Benjamin R. Haskell @ 2011-02-23 18:03 UTC (permalink / raw)
To: Zsh Users
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
# ...
}
--
Best,
Ben
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Function-scoped parameters?
2011-02-23 18:03 Function-scoped parameters? Benjamin R. Haskell
@ 2011-02-23 18:27 ` Benjamin R. Haskell
2011-02-23 19:17 ` Sebastian Stark
2011-02-23 20:29 ` Bart Schaefer
2 siblings, 0 replies; 6+ messages in thread
From: Benjamin R. Haskell @ 2011-02-23 18:27 UTC (permalink / raw)
To: Zsh Users
On Wed, 23 Feb 2011, Benjamin R. Haskell wrote:
> 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
> # ...
> }
>
Hmm. The following seems to do what I want, but seems kludgy and
zsh-specific (which might be fine, but this seems like something other
shells should be able to do). (Main problem might be that I tend to
code as if I'm using Perl even when I'm not.)
dosomething () {
emulate -L zsh
setopt err_return
local temp=$(mktemp)
function () {
trap '(( $+temp )) && rm $temp' EXIT
# ... do stuff ...
}
# at this point, $temp has always been cleaned up (error or no)
}
dosomething blah blah
# at this point, $temp is not leaked
--
Best,
Ben
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Function-scoped parameters?
2011-02-23 18:03 Function-scoped parameters? Benjamin R. Haskell
2011-02-23 18:27 ` Benjamin R. Haskell
@ 2011-02-23 19:17 ` Sebastian Stark
2011-02-23 20:01 ` Benjamin R. Haskell
2011-02-23 20:29 ` Bart Schaefer
2 siblings, 1 reply; 6+ messages in thread
From: Sebastian Stark @ 2011-02-23 19:17 UTC (permalink / raw)
To: Benjamin R. Haskell; +Cc: Zsh Users
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?
Sebastian
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Function-scoped parameters?
2011-02-23 19:17 ` Sebastian Stark
@ 2011-02-23 20:01 ` Benjamin R. Haskell
0 siblings, 0 replies; 6+ messages in thread
From: Benjamin R. Haskell @ 2011-02-23 20:01 UTC (permalink / raw)
To: Sebastian Stark; +Cc: Zsh Users
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
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Function-scoped parameters?
2011-02-23 18:03 Function-scoped parameters? Benjamin R. Haskell
2011-02-23 18:27 ` Benjamin R. Haskell
2011-02-23 19:17 ` Sebastian Stark
@ 2011-02-23 20:29 ` Bart Schaefer
2011-02-23 21:27 ` Benjamin R. Haskell
2 siblings, 1 reply; 6+ messages in thread
From: Bart Schaefer @ 2011-02-23 20:29 UTC (permalink / raw)
To: Zsh Users
On Wednesday, February 23, 2011, Benjamin R. Haskell <zsh@benizi.com> wrote:
> 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.
That's what "always" is for.
dosomething() {
emulate -L zsh
local temp=$(mktemp)
setopt err_return
{
# ...
} always {
(( $+temp )) && rm $temp
}
}
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Function-scoped parameters?
2011-02-23 20:29 ` Bart Schaefer
@ 2011-02-23 21:27 ` Benjamin R. Haskell
0 siblings, 0 replies; 6+ messages in thread
From: Benjamin R. Haskell @ 2011-02-23 21:27 UTC (permalink / raw)
To: Bart Schaefer; +Cc: Zsh Users
[-- Attachment #1: Type: TEXT/PLAIN, Size: 666 bytes --]
On Wed, 23 Feb 2011, Bart Schaefer wrote:
> On Wednesday, February 23, 2011, Benjamin R. Haskell <zsh@benizi.com> wrote:
>> 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.
>
> That's what "always" is for.
>
> dosomething() {
> emulate -L zsh
> local temp=$(mktemp)
> setopt err_return
> {
> # ...
> } always {
> (( $+temp )) && rm $temp
> }
> }
>
... Whoa. ...
Still wondering what other shells do, but man, I love Zsh.
--
Best,
Ben
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2011-02-23 21:28 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-02-23 18:03 Function-scoped parameters? Benjamin R. Haskell
2011-02-23 18:27 ` Benjamin R. Haskell
2011-02-23 19:17 ` Sebastian Stark
2011-02-23 20:01 ` Benjamin R. Haskell
2011-02-23 20:29 ` Bart Schaefer
2011-02-23 21:27 ` Benjamin R. Haskell
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).