[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 13:21:18 +0200 |
User-agent: |
Mutt/1.5.18 (2008-05-17) |
On 100501 12:40, Pierre Gaston wrote:
> On Sat, May 1, 2010 at 12:26 PM, Dennis Williamson wrote:
> > As Chet said, use internal variables that are unlikely to conflict.
> You can use workarounds like:
> printf -v $a "%s" foobar
> read $a <<< "%s"
The problem with obfucscated internal variables I think is that my
library code becomes unnecessary obfuscated. That's why I'm thinking an
additional call-layer (workaround 2 in my original mail) is an
improvement in that the private library code "_blackbox()" can use
simple, readable variables without restrictions/conflicts, while the
public layer "blackbox()" checks conflicts, which - unlikely as they
might be - I'd like to return to my library users as a known error
instead of silent failing.
Revised example, replacing eval with printf -v, thanks:
# Param: $1 variable name to return value to
# Private library function. Do not call directly. See blackbox()
_blackbox() {
# Just don't declare "local __1"
local a b c d e f g h i j
# ...
# Lots of complicated library code here
# ...
# Return value
printf -v $1 %s b
}
# Param: $1 variable name to return value to
# Public library function
blackbox() {
local __1
_blackbox __1
[[ $1 == __1 ]] && echo "ERROR: variable name conflicts"\
"with local variable: $1"
printf -v $1 %s "$__1"
}
blackbox a; echo $a # Outputs "b" all right
blackbox __1 # Outputs error
d='ls /;true'; blackbox "$d" # No more oops
Freddy Vulto
http://fvue.nl/wiki/Bash:_passing_variables_by_reference
- 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, Pierre Gaston, 2010/05/01
- 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, 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
- Re: Passing variables by reference conflicts with local, Chet Ramey, 2010/05/05
- Re: Passing variables by reference conflicts with local, Chet Ramey, 2010/05/04
- Re: Passing variables by reference conflicts with local, Chet Ramey, 2010/05/02