zsh-users
 help / color / mirror / code / Atom feed
From: "Benjamin R. Haskell" <zsh@benizi.com>
To: Zsh Users <zsh-users@zsh.org>
Subject: Re: Function-scoped parameters?
Date: Wed, 23 Feb 2011 13:27:06 -0500 (EST)	[thread overview]
Message-ID: <alpine.LNX.2.01.1102231323000.2792@hp> (raw)
In-Reply-To: <alpine.LNX.2.01.1102231257420.2792@hp>

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


  reply	other threads:[~2011-02-23 18:27 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-02-23 18:03 Benjamin R. Haskell
2011-02-23 18:27 ` Benjamin R. Haskell [this message]
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

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=alpine.LNX.2.01.1102231323000.2792@hp \
    --to=zsh@benizi.com \
    --cc=zsh-users@zsh.org \
    /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).