[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: unset does not act as expected on namerefs
From: |
Greg Wooledge |
Subject: |
Re: unset does not act as expected on namerefs |
Date: |
Tue, 2 Jun 2015 11:37:32 -0400 |
User-agent: |
Mutt/1.4.2.3i |
On Tue, Jun 02, 2015 at 11:16:27AM -0400, Shawn Wilson wrote:
> I would argue that a nameref is a variable type.
I suppose you're right, insofar as it has its own special rules that
you have to know about.
A lot of people wanted declare -n to be more than it is. It's really
just syntactic sugar that doesn't actually let you do anything you
couldn't do already (with printf -v for assignment, and ${!foo} or eval
for reference). So, like declare -i, it's something you can completely
disregard if you're writing new scripts, and not maintaining other
people's scripts.
Ksh's nameref is completely different. With one of ksh's two different
kinds of functions, you actually CAN use ksh nameref to pass a value
back to the caller without variable name collisions.
$ bash -c 'f() { local x=from_f; g x; }; g() { declare -n x="$1"; echo "$x"; };
f'
bash: warning: x: circular name reference
$ ksh -c 'f() { typeset x=from_f; g x; }; g() { typeset -n x="$1"; echo "$x";
}; f'
ksh: typeset: x: invalid self reference
$ ksh -c 'function f { typeset x=from_f; g x; }; function g { typeset -n
x="$1"; echo "$x"; }; f'
from_f