bug-bash
[Top][All Lists]
Advanced

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

Re: Incorrect / Inconsistent behavior with nameref assignments in functi


From: Binarus
Subject: Re: Incorrect / Inconsistent behavior with nameref assignments in functions
Date: Fri, 28 Aug 2020 18:20:04 +0200
User-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:68.0) Gecko/20100101 Thunderbird/68.9.0


On 28.08.2020 17:28, Greg Wooledge wrote:
> On Fri, Aug 28, 2020 at 10:56:34AM +0200, Binarus wrote:
>> #!/bin/bash
>>
>> function Dummy() {
>>
>>   local -n namerefArray="$1"
>>   local -a -i myArray=("${namerefArray[@]}")
>>
>>   local -p
>> }
>>
>> declare -a -i myArray=('1' '2' '3')
> 
> You've got a local variable with the same name as the global variable
> that you're attempting to pass by reference.  This will not work.
> 
> Namerefs (declare -n) in bash are *not* like uplevel commands in Tcl.
> They cause the referenced variable name to be evaluated just like any
> other variable would be, starting at the current function scope, then
> going up to the caller, and so on.
> 
> If you want to use namerefs in a function in bash, you MUST go out of
> your way to minimize the chances of a collision between the caller's
> variable refererance and ANY local variable of the function.  Not just
> the nameref itself, but any other incidental variables used in the
> function.  (As you aptly demonstrated here.)
> 
> So, you can't write functions like this:
> 
> func1() {
>   declare -n ref="$1"
>   local i
>   ...
> }
> 
> Instead, you need crazy things like this:
> 
> func1() {
>   declare -n _func1_ref="$1"
>   local _func1_i
>   ...
> }

I had worked that out myself so far and have setup a naming convention
in my script similar to your proposition before posting the bug. I
needed a while to recognize the problem since I wouldn't have expected
it because it renders namerefs nearly useless.

However, the main question is why leaving away the -a and -i in the
second script makes things work as expected. Can we rely on that
behavior and circumvent the problem that way? Both scripts should give
the same output, shouldn't they? So it's a bug they don't, and we can't
rely on it?

For reference, I also have posted a question about it on SO yesterday
(nicer code formatting):
https://stackoverflow.com/questions/63629172/weird-behavior-when-assigning-local-namerefs-to-local-variables-in-functions?noredirect=1#comment112528357_63629172

> And then you just have to pray that the caller respects you enough not
> to use variables named with _func1_ prefixes.
> 
> There is no 100% bulletproof solution to this issue.
> 
> See also <https://mywiki.wooledge.org/BashProgramming#Functions>.

Since I am the caller :-), this won't impose a problem. I have
excessively commented that script.

However, this whole thing does not make much sense. I have put too much
work into this script; otherwise, I'd dump it and restart with Perl or
Python.

Thank you very much, and best regards,

Binarus



reply via email to

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