From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 2778 invoked by alias); 23 Feb 2011 18:27:36 -0000 Mailing-List: contact zsh-users-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Users List List-Post: List-Help: X-Seq: 15822 Received: (qmail 10549 invoked from network); 23 Feb 2011 18:27:34 -0000 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham version=3.3.1 Received-SPF: pass (ns1.primenet.com.au: SPF record at benizi.com designates 64.130.10.15 as permitted sender) Date: Wed, 23 Feb 2011 13:27:06 -0500 (EST) From: "Benjamin R. Haskell" To: Zsh Users Subject: Re: Function-scoped parameters? In-Reply-To: Message-ID: References: User-Agent: Alpine 2.01 (LNX 1266 2009-07-14) MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII; format=flowed 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