bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: undo local variable with unset


From: Poor Yorick
Subject: Re: undo local variable with unset
Date: Tue, 6 Jan 2015 03:44:17 +0000
User-agent: Mutt/1.5.21 (2010-09-15)

On Mon, Jan 05, 2015 at 03:25:56PM -0500, Greg Wooledge wrote:
> On Mon, Jan 05, 2015 at 01:14:53PM -0700, Poor Yorick wrote:
> > It's nice to be able to create a function like unlocal that moves 
> > local
> > variables out of the way, but I'm wondering if it can be relied on in 
> > future
> > versions:
> 
> Chet can give the authoritative answer about future directions, but I find
> it fascinating that you seem to have re-invented (part of) Freddy Vulto's
> upvar suite:
> 
> http://www.fvue.nl/wiki/Bash:_Passing_variables_by_reference

That link seems to be dead, but I got the concept from "Passing variables by
reference conflicts with local", Freddy Vulto, et al, 2010-04-30:

    http://lists.gnu.org/archive/html/bug-bash/2010-05/msg00000.html

My use case is for a function to call a function that sources a script, without
any danger of variable assignments in the sourced script getting intercepted by
local variables, so I came up with this pattern:

f1 () {
    local a b c
    #do stuff with local variables
    set -- "$a" "$b" "$c" "$item"

    #nope, this doesn't work because it doesn't list local variables that
    #are currently undefined
    #unlocal $(locals)

    unlocal a b c item 
    function_that_sources_something
    local a="$1" b="$2" c="$3" item="4"
    #do more stuff with local variables
}

Maybe someday bash will grow upvar and uplevel commands, a-la Tcl.  Or maybe
people will just quit trying to write Tcl code in Bash ;)

-- 
Yorick


 




reply via email to

[Prev in Thread] Current Thread [Next in Thread]