Prev: Confusion about the smart matching operator
Next: FAQ 5.19 How can I open a file with a leading ">" or trailing blanks?
From: sln on 25 Jul 2010 16:56 On Fri, 23 Jul 2010 09:40:06 -0700 (PDT), Dilbert <dilbert1999(a)gmail.com> wrote: >In perl 5.12.1, with reference to the exist function "perldoc -f >exist" ( see also http://perldoc.perl.org/functions/exists.html ) it >says > >>> [...] >>> Although the mostly deeply nested array or hash will >>> not spring into existence just because its existence >>> was tested, any intervening ones will. Thus $ref->{"A"} >>> and $ref->{"A"}->{"B"} will spring into existence due to >>> the existence test for the $key element above. >>> [...] >>> This surprising autovivification in what does not at first >>> --or even second-- glance appear to be an lvalue context >>> may be fixed in a future release. > >Has this particular case of surprising autovivification always >existed, even in perl 5.10 or 5.8 ? This may be a workaround (suprisingly nitpicky to do). -sln ------------------- use strict; use warnings; use Data::Dumper; my $x; $x->{''}{''}{2} = ''; $x->{a} {''}{c}{d} = undef; print Dumper( $x ); print "1> pass = ", scalar deep_exists( $x, 'a', undef, 'c'), "\n"; print "2> pass = ", scalar deep_exists( $x, '' , undef), "\n"; print "3> pass = ", scalar deep_exists( $x, '' , undef, '2', ''), "\n"; my ($pass, $found) = deep_exists($x, 'a', '', 'c', 'd'); print "4> pass = $pass, found = $found\n"; print "5> pass = ", scalar deep_exists( $x), "\n"; print "6> pass = ", scalar deep_exists(), "\n"; exit 0; ## sub deep_exists { return (0,0) unless @_; my $t = shift; my $count = map { my $key = $_ // ''; ( ref($t) eq "HASH" and exists $t->{$key} ) ? $t = $t->{$key} : () } @_; my $res = (@_ && $count == @_) ? 1 : 0; return wantarray ? ($res, $count) : $res; }
From: Ilya Zakharevich on 25 Jul 2010 17:08
On 2010-07-25, Willem <willem(a)turtle.stack.nl> wrote: > ) For each person for whom it is not surprising, how many for which it is? > For each person for whom it is surprising, how many for which it is not? 0, up to experiment's errors. But you know this already; why ask? Puzzled, Ilya |