[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Passing variables by reference conflicts with local
From: |
Dennis Williamson |
Subject: |
Re: Passing variables by reference conflicts with local |
Date: |
Sat, 1 May 2010 07:53:35 -0500 |
Wouldn't you want your name collision test before your call to the
private function - just to save the wasted call? (And to have its
message redirected to stderr and have a "return 1" or other non-zero
value?) Otherwise, I think your idea is a good one, especially if the
public function can be as simple as that.
Thanks, Pierre, by the way, for the workarounds. I hadn't considered
using indirection that way.
On Sat, May 1, 2010 at 6:21 AM, Freddy Vulto <fvulto@gmail.com> wrote:
> 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, 2010/05/01
- Re: Passing variables by reference conflicts with local,
Dennis Williamson <=
- 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
- Re: Passing variables by reference conflicts with local, Matthew Woehlke, 2010/05/03