Re: static vs. dynamic scoping

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


[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

-- 
Eric Blake   eblake@xxxxxxxxxx    +1-801-349-2682
Libvirt virtualization library http://libvirt.org

Attachment: signature.asc
Description: OpenPGP digital signature


[LARTC]     [Bugtraq]     [Yosemite Forum]     [Photo]

Powered by Linux