[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Passing variables by reference conflicts with local
From: |
Freddy Vulto |
Subject: |
Re: Passing variables by reference conflicts with local |
Date: |
Sat, 1 May 2010 10:18:03 +0200 |
User-agent: |
Mutt/1.5.18 (2008-05-17) |
On 100430 08:19, Greg Wooledge wrote:
> On Fri, Apr 30, 2010 at 12:02:58AM +0200, Freddy Vulto wrote:
> > Passing variables by reference however, has a caveat in that
> > local variables override the passing by reference, e.g.:
> >
> > t() {
> > local a
> > eval $1=b
> > }
> > unset a; t a; echo $a # Outputs nothing, expected "b"
>
> Why did you declare 'a' to be local if that's not what you wanted?
> Why did you expect 'b' to be output, if you used a local variable?
t() is an example library function which - as a black box - can contain
many local variables. Local 'a' is just an example.
I would like to call t(), and let it return me a filled variable by
reference, that is without polluting the global environment.
Problem is, all variables work except 'a' because t() happens to declare
this local - which I'm not supposed to know because t() is a black box.
Maybe the example is not clear because 'a' becomes global after all,
and is this a better example:
# Param: $1 variable name to return value to
blackbox() {
local a
eval $1=bar
}
This goes all right:
f() {
local b
blackbox b
echo $b
}
f # Echos "bar" all right
But if I change 'b' to 'a', this conflicts with blackbox() local 'a':
f() {
local a
blackbox a
echo $a
}
f # Outputs nothing unexpected
Freddy Vulto
http://fvue.nl/wiki/Bash:_passing_variables_by_reference
- Re: Passing variables by reference conflicts with local,
Freddy Vulto <=
- Re: Passing variables by reference conflicts with local, Dennis Williamson, 2010/05/01
- Re: Passing variables by reference conflicts with local, Pierre Gaston, 2010/05/01
- Re: Passing variables by reference conflicts with local, Freddy Vulto, 2010/05/01
- Re: Passing variables by reference conflicts with local, Dennis Williamson, 2010/05/01
- Re: Passing variables by reference conflicts with local, Freddy Vulto, 2010/05/01
- Re: Passing variables by reference conflicts with local, Dennis Williamson, 2010/05/01
- Re: Passing variables by reference conflicts with local, Freddy Vulto, 2010/05/02
- Re: Passing variables by reference conflicts with local, Dennis Williamson, 2010/05/02
- Re: Passing variables by reference conflicts with local, Freddy Vulto, 2010/05/03
- Re: Passing variables by reference conflicts with local, Freddy Vulto, 2010/05/04