[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
local_array_declare_-g suggested workaround
From: |
Vicente Couce Díaz |
Subject: |
local_array_declare_-g suggested workaround |
Date: |
Thu, 20 Dec 2012 23:09:02 +0100 |
Bash 4.2.36(1)-release (i486-pc-linux-gnu), Debian wheezy (bash 4.2-4)
Given this example:
f() {
local -a v
local -a w
g
echo "f: ${v[@]}, ${w[@]}"
}
g() {
aux=v
declare -ga "$aux=( asdf fdsa )"
declare -ga w=( asdf fdsa )
echo "f: ${v[@]}, ${w[@]}"
}
f
echo "FIN: ${v[@]}, ${w[@]}"
Result:
g: , asdf fdsa
f: , asdf fdsa
FIN: asdf fdsa,
Expected:
g: asdf fdsa, asdf fdsa
f: asdf fdsa, asdf fdsa
FIN: ,
If I declare a variable local in a function, then it should be kept in the
same scope when using 'declare -g' in a child context. This happens with
$w, but not with $v, in this case the variable becomes global and no more
local to the f call, so hidden there by the local homonyme.
This happens when employing -a and -g and the only difference is that the
definition is quoted.
After some hints from ormaaj in Freenode, a little research and debugging
with the sources, I found a possible workaround, Im not an experienced c++
developper, so maybe its not well written and Im not aware of the possible
side effects, but I attach a patch that solves the issue.
I dont know if its a bug or not, because this feature is indeed not
documented (the quoted way), but its often used and I find it usefull for
writting libs, anyway, its implemented but with this little unconsistent
behavior, so why not to clear it?
If there are side effects as make_new_array_variable is used in some other
places, it could be added a new one. Anyway, I almost copied the table
lookup from bind_variable, so too many errors shouldnt be there.
Well, after trying my first example, Ive found the first problem, $w didnt
wrok anymore. I really dont understand why, but decided to split the
original function and gave the new one the temporal name
make_aux_new_array_variable, could be make_cntxt_array_variable, but
shouldnt be needed at all I guess. I attach a test code too.
So if some one could check this out, it would be cool, thanks.
local_array_declare_-g.patch
Description: Binary data
bash-array-local-indirection-test.sh
Description: Bourne shell script
- local_array_declare_-g suggested workaround,
Vicente Couce Díaz <=