develooper Front page | perl.perl5.porters | Postings from December 2008

Restore array length on scope end when an nonexistent array elementwas localized

From:
Vincent Pit
Date:
December 28, 2008 06:28
Subject:
Restore array length on scope end when an nonexistent array elementwas localized
Message ID:
49578CF0.1090106@profvince.com
Consider

    perl -e '@a = qw/a b/; { local $a[2] = "c" } print scalar(@a),"\n"'

This prints 3, because the localized array element is just undefined on
scope end, and not deleted as localized hash elements are.
The attached set of 3 patches fixes this by saving an array delete on
the stack when the localized array element does not exist in the upper
scope :
- 0001 factors in a macro the logic currently used in pp_helem and
pp_hslice for determining if it worth calling exist and delete on the
hash (i.e. wether the hash is not magical or if it's tied and has EXISTS
and DELETE methods).
- 0002 implements save_adelete/SAVEADELETE that save an array element
delete on the stack (just like what SAVEDELETE() does for hashes)
- 0003 uses the new macro in pp_aelem and pp_aslice to decide if we can
try to save an array delete. If we can, and that the localized index
didn't exist in the old array, then an array delete is saved instead of
an array element restore (i.e. save_aelem()). It also adds tests to
t/op/local.t

Vincent.



nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About