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


Groups > gnu.bash.bug > #16260

Re: Local variable names clash with global read-only variable names.

From worley@alum.mit.edu (Dale R. Worley)
Newsgroups gnu.bash.bug
Subject Re: Local variable names clash with global read-only variable names.
Date 2020-04-30 22:43 -0400
Message-ID <mailman.1733.1588301045.3066.bug-bash@gnu.org> (permalink)
References <be8dd110-c269-be88-b14c-8359e4c547f5@case.edu> <87wo5wgys6.fsf@hobgoblin.ariadne.com>

Show all headers | View raw


>>>     f() { local x=a; }
>>>     declare -r x
>>>     f  # bash: local: x: readonly variable
>>>
>>>   This^^^ should not fail; it hinders reusability of shell functions and makes
>>>   them context-dependent.

It's "natural" to think that a variable that is local to a function
should be independent of however it is declared globally.  The problem
is that bash local variables aren't lexically scoped -- when you enter
the function, the variable acquires the new value, but any function that
is called by this function sees the new value, even if that function
wasn't declared inside the function that declared the variable local.
So if a local declaration could override a readonly declaration, the new
value could be seen by code unrelated the function that did the
override.

This is a common issue in language design.  The Perl language originally
only had "local" declarations that behaved the same way as bash local
declarations.  But the above behavior got to be so much of a problem for
large programs that Perl added a separate lexically-scoped local
declaration.

Dale

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


Thread

Re: Local variable names clash with global read-only variable names. worley@alum.mit.edu (Dale R. Worley) - 2020-04-30 22:43 -0400

csiph-web