On Sun Dec 04 01:48:20 2011, davidnicol@gmail.com wrote: > I would like taking a reference to a nonexistent hash element to do > magic similar to the magic done when a nonexistent hash element is > used as a function argument. > > my %hash; > my $fancyref = \$hash{doesnotexist}; > > at this point, C<keys %hash> would still be empty. > $fancyref would appear to be undefined, and would evaluate as false. > ref($fancyref) would be true, like an overloaded object. > > Both of these would remain unchanged > > perl -le '$r = \$h{k}; $$r=5; print %h' > k5 > perl -le '$r = \$h{k}; $h{k}=5; print $$r' > 5 > > except that $r would be false until the key is created in the second > statement, instead of the key getting created in the first statement. > > C<\$hash{keyname}> would become a concise way to spell > C<exists($hash{keyname})> with the added feature of providing a > reference or a delayed reference. > > The autovivification deferral should work with > nonexistent-hash-element function arguments: > > { > sub reftosecond{ my $r2= \$_[1]; return $r2 } > my %h; my $r = reftosecond($h{one}, $h{two}); > ok ( (!keys %h), "taking reference to nonexistent hash elt arg > does not create entry"); > $$r = 27; > is( $h{two}, 27, "assigning to deferred hash elt ref creates > entry"); > $r = \$h{three}; > ok( !$r, "reference to nonexistent hash elt is false"); > $h{three} = 28; > is ($$r, 28, "dereferencing reference to formerly nonexistent hash > elt"); > } I think that’s a little too weird and surprising. -- Father ChrysostomosThread Previous | Thread Next