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


Groups > gnu.bash.bug > #16836

Re: Incorrect / Inconsistent behavior with nameref assignments in functions

Path csiph.com!goblin2!goblin1!goblin.stu.neva.ru!usenet.stanford.edu!not-for-mail
From Oğuz <oguzismailuysal@gmail.com>
Newsgroups gnu.bash.bug
Subject Re: Incorrect / Inconsistent behavior with nameref assignments in functions
Date Fri, 28 Aug 2020 23:08:31 +0300
Lines 86
Approved bug-bash@gnu.org
Message-ID <mailman.1633.1598645316.2469.bug-bash@gnu.org> (permalink)
References <a20e4692-69b3-9836-4861-3e822e407ef7@binarus.de> <20200828152846.GI931@eeg.ccf.org> <CAH7i3Lpq3dWx55mGcuu4TK0gQaLCFBQwcoaeWR_-tunwxK6ijQ@mail.gmail.com> <8313a366-6ecd-5e87-5552-6a4e0fe18028@binarus.de> <CAH7i3LoPa-uepKMLMOvytgXSG9fvJed1hgV1VDGpK=kgm3vLrg@mail.gmail.com>
NNTP-Posting-Host lists.gnu.org
Mime-Version 1.0
Content-Type text/plain; charset="UTF-8"
Content-Transfer-Encoding quoted-printable
X-Trace usenet.stanford.edu 1598645317 17039 209.51.188.17 (28 Aug 2020 20:08:37 GMT)
X-Complaints-To action@cs.stanford.edu
Cc "bug-bash@gnu.org" <bug-bash@gnu.org>
To Binarus <lists@binarus.de>
Envelope-to bug-bash@gnu.org
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=wbh0kMeekEC3jQNrKJccpCBrOAXNHPzO+tTAyrde5s0=; b=t5+FxrLbloFpI3M3ccsiomSa6i4KKe946zEbHJKZ8UnhperZ+GyRi+m/Jv0GfxKBR8 Z707UB+/f4RdO4/95zI0WHZw2mykFUeA6UE4Dlmpk7EiGqz9Bs9SwrQfHmpJQYB3kDQ7 8BVirGkKyigYTEvIf4kbVZmoUbZThDBbpBkKO90QaQL2vjvNKDicvnM2CVjKL7IDhAsN YltR/KZbPQtN44X4GxWEOPxvseaCJVr0BZLjNBhRKmB78gD43dIOR+sj81PbAJ/7JyXV v8+SC++DDyc4+s1JvJhCHqsO0CtgoFzm0D2YpEx3cMT4erI+LU+GoAa994shZnmoIbY8 qP7A==
X-Google-DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=wbh0kMeekEC3jQNrKJccpCBrOAXNHPzO+tTAyrde5s0=; b=fRIPrqzAzCjxpW4hPQaDX++3yfnQhAIYu/O4Ot4yQmCoxnARG1+mk/mF7gARi3gQzh 1iS3d8JzOkTpil0hXlgUpRbhppk0IqNMbvu8GX30BPgzw+NR2dD9Koa/KeZJBJRHpqIg HKL7KZBGke4E4HYtJ6MVM6jqg02UbU/uuII9IQ1lLvGJkZCMgdvauEvX3r3owN0MhM2k 9DT4DxUv1JgDRA0j9KM+GiqLIx72acjAedP9Yndb5zS8msJaAuJnYbvJ0Kl5hwy7issd CPFte7f8wQ11CneGiaZe/iRyn4vG/ifuM3XfejBwzOW9GtzN0FT/QxP7rm8TYLFHDrfk d+Ow==
X-Gm-Message-State AOAM533rKRqdtSwbz5+JAtGvFO3EsKSH3HxnxXBSDaU0TTQrbEc5+ozp o/z2ThVL0Hv5l7bXzJzbCta1D8NayXs9AMmYb+lpQFbu
X-Google-Smtp-Source ABdhPJwz1I/A28k5SjlPPu79GH+l7VsrD942YvZa3W6J60YqfFxL5k/NtcHKXSAHs5+d30XbiYlXnT1d05PxRCH58hA=
X-Received by 2002:a05:6214:954:: with SMTP id dn20mr252821qvb.122.1598645312178; Fri, 28 Aug 2020 13:08:32 -0700 (PDT)
In-Reply-To <8313a366-6ecd-5e87-5552-6a4e0fe18028@binarus.de>
Received-SPF pass client-ip=2607:f8b0:4864:20::f32; envelope-from=oguzismailuysal@gmail.com; helo=mail-qv1-xf32.google.com
X-detected-operating-system by eggs.gnu.org: No matching host in p0f cache. That's all we know.
X-Spam_score_int -20
X-Spam_score -2.1
X-Spam_bar --
X-Spam_report (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, HTML_MESSAGE=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no
X-Spam_action no action
X-Content-Filtered-By Mailman/MimeDel 2.1.23
X-BeenThere bug-bash@gnu.org
X-Mailman-Version 2.1.23
Precedence list
List-Id Bug reports for the GNU Bourne Again SHell <bug-bash.gnu.org>
List-Unsubscribe <https://lists.gnu.org/mailman/options/bug-bash>, <mailto:bug-bash-request@gnu.org?subject=unsubscribe>
List-Archive <https://lists.gnu.org/archive/html/bug-bash>
List-Post <mailto:bug-bash@gnu.org>
List-Help <mailto:bug-bash-request@gnu.org?subject=help>
List-Subscribe <https://lists.gnu.org/mailman/listinfo/bug-bash>, <mailto:bug-bash-request@gnu.org?subject=subscribe>
X-Mailman-Original-Message-ID <CAH7i3LoPa-uepKMLMOvytgXSG9fvJed1hgV1VDGpK=kgm3vLrg@mail.gmail.com>
X-Mailman-Original-References <a20e4692-69b3-9836-4861-3e822e407ef7@binarus.de> <20200828152846.GI931@eeg.ccf.org> <CAH7i3Lpq3dWx55mGcuu4TK0gQaLCFBQwcoaeWR_-tunwxK6ijQ@mail.gmail.com> <8313a366-6ecd-5e87-5552-6a4e0fe18028@binarus.de>
Xref csiph.com gnu.bash.bug:16836

Show key headers only | View raw


28 Ağustos 2020 Cuma tarihinde Binarus <lists@binarus.de> yazdı:

>
>
> On 28.08.2020 17:37, Oğuz wrote:
> > 28 Ağustos 2020 Cuma tarihinde Greg Wooledge <wooledg@eeg.ccf.org>
> yazdı:
> >
> >> 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.
> >>
> >>
> > These scripts yield identical output on bash-5.1 though.
>
> Thank you very much for testing! This is interesting. I couldn't get my
> hands on a system with 5.1 yet.
>
>
5.1 is still in beta phase.


> With 5.1, do both scripts behave like SCRIPT 1 with the older versions
> or like SCRIPT 2 with the older versions?


They behave like SCRIPT 2 as expected, local `myArray' is populated with
the contents of global `myArray'. _identical_ was the wrong word though,
the second script doesn't copy the integer attribute.


> >> 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
> >>   ...
> >> }
> >>
> >>
> > This doesn't make the slightest sense. What is the point of having local
> > variables then?
>
> Or namerefs ... I totally agree. Either locals or namerefs are just
> unusable given that situation; you have to choose between them.
>
> Thank you very much, and best regards,
>
> Binarus
>
>

-- 
Oğuz

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


Thread

Re: Incorrect / Inconsistent behavior with nameref assignments in functions Oğuz <oguzismailuysal@gmail.com> - 2020-08-28 23:08 +0300

csiph-web