Re: static vs. dynamic scoping

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 10 November 2010 16:13, Eric Blake <eblake@xxxxxxxxxx> wrote:
> [redirecting back to the list, so others can benefit]
>
> On 11/10/2010 02:16 AM, Marc Herbert wrote:
>> Le 09/11/2010 21:52, Eric Blake a écrit :
>>> I'm trying to standardize the notion of local variables for the next
>>> revision of POSIX, but before I can do so, I need some feedback on two
>>> general aspects:
>>> [...]
>>> Here's a sample shell script that illustrates the difference between the
>>> two scoping methods.
>>
>> Hi Eric,
>>
>>   I found your sample script quite confusing. To make your point, does
>> this script really need to:
>> - use unquoted language keywords as string values?
>
> No; I could have used other strings.
>
>> - use deprecated "typeset" instead of declare?
>
> Yes - the current Austin Group thoughts are to standardize 'typeset' and
> NOT 'local', since 'typeset' can be used with arguments outside of
> functions, and more existing shells provide 'typeset' than 'local' (dash
> being the odd one out) or 'declare'.  Shells can continue to provide
> 'local' as a synonym for the most basic use of typeset.
>
>> - use the not (or less?) standard "function" keyword?
>
> Yes - ksh93 ONLY supports function-local scoping when using the function
> keyword, rather than when using POSIX functions (although David Korn
> agreed that if POSIX standardizes function-local scoping, he'd make the
> next build of ksh support it in POSIX functions).
>
> So, here's the example again, with those points addressed:
>
> # Demonstrate ksh local scoping is static - requires ksh's 'function'
>
> $ ksh -c 'function f1 { typeset a=temp; f2; echo "in f1: $a"; };
> function f2 { echo "in f2: $a"; a=changed; }; a=global; f1; echo "top
> level: $a"'
> in f2: global
> in f1: temp
> top level: changed
>
> # Demonstrate that with POSIX functions, ksh has global scoping
>
> $ ksh -c 'f1 () { typeset a=temp; f2; echo "in f1: $a"; }; f2 () { echo
> "in f2: $a"; a=changed; }; a=global; f1; echo "top level: $a"'in f2: temp
> in f1: changed
> top level: changed
>
> # Demonstrate that dash local scoping is currently dynamic
>
> $ dash -c 'f1 () { local a=temp; f2; echo "in f1: $a"; }; f2 () { echo
> "in f2: $a"; a=changed; }; a=global; f1; echo "top level: $a"'
> in f2: temp
> in f1: changed
> top level: global

Why is the debate static-vs-dynamic scoping coming up again? People
like Ken Thompson, who wrote the very first UNIX shell, Douglas
McIlroy, inventor of the UNIX pipelines and many others of the fathers
of UNIX have criticized the concept of dynamic scoping in ksh88
loudly, in USENIX meetings and elsewhere. Even David Korn admitted
that it was a grave design error and corrected it for the next major
version of the korn shell, which became ksh93.

With this background I doubt any proposal for dynamic scoping will
make it into the next POSIX standard. It's simply not considered a
sane design for a modern computer language anymore and bash's
emulation of ksh88's dynamic scoping can hardly be used as precedent
in Austin Group proposals after ksh88 itself was abandoned in 1992.

Ced
-- 
Cedric Blancher <cedric.blancher@xxxxxxxxxxxxxx>
Institute Pasteur
--
To unsubscribe from this list: send the line "unsubscribe dash" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[Index of Archives]     [LARTC]     [Bugtraq]     [Yosemite Forum]     [Photo]

  Powered by Linux