Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > gnu.bash.bug > #14906

Re: assignment to a nameref that resolves to the temporary environment

From Chet Ramey <chet.ramey@case.edu>
Newsgroups gnu.bash.bug
Subject Re: assignment to a nameref that resolves to the temporary environment
Date 2018-12-11 15:37 -0500
Organization ITS, Case Western Reserve University
Message-ID <mailman.5606.1544560641.1284.bug-bash@gnu.org> (permalink)
References <CAMu=BroJdmD2mLX2EFgi3sJfb+viaYkijnQoOfkbje_i-nGK8w@mail.gmail.com>

Show all headers | View raw


On 12/9/18 1:28 AM, Grisha Levit wrote:
> When a nameref local to a higher function scope points to a variable in the
> temporary environment, that nameref (correctly, it seems) expands to the
> value
> of the target variable from the temporary environment. However, assignment
> to
> the nameref modifies the variable from the higher scope, bypassing the
> tempenv:
> 
>     $ a() { local -n ref=var; var=tmp b; echo $ref; }
>     $ b() { ref=$ref; }
>     $ var=foo; a
>     tmp

Here's what happens when you execute `b'. The variable `var' is put into
the temporary environment with value `tmp'. The resolution for $ref looks
at the current environment, finds nothing, looks at the previous
environment (a), and finds a nameref with value `var'. It *restarts* the
lookup to resolve `var', finds it in the temporary environment, and expands
to `tmp'. So that's the value, and the assignment becomes `ref=tmp'.

The assignment code goes looking for `ref' and finds it in the previous
context as a nameref with value `var'. This is where the asymmetry occurs.
When performing an assignment, the shell starts at the context where the
nameref is found, rather than starting at the original context -- which in
this case, would have found the variable in b's temporary environment.

Do you think it's reasonable to have this restriction, given the nature of
bash's dynamic variable scoping, or should it restart the lookup from the
original variable context? You can get yourself into some pretty nasty
nameref loops if you go back to the original context.

Chet

-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer
		 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRU    chet@case.edu    http://tiswww.cwru.edu/~chet/

Back to gnu.bash.bug | Previous | Next | Find similar | Unroll thread


Thread

Re: assignment to a nameref that resolves to the temporary environment Chet Ramey <chet.ramey@case.edu> - 2018-12-11 15:37 -0500

csiph-web