Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Re^3: Why does exists cause autovivication?

by Porculus (Hermit)
on Jan 03, 2008 at 22:00 UTC ( #660326=note: print w/replies, xml ) Need Help??

in reply to Re^2: Why does exists cause autovivication?
in thread Why does exists cause autovivication?

Thy careless writing, brother, doth bring pain unto mine eyes, for verily thou mixest the high and low modes of address, like unto a novice that mistaketh his sigils, as "@foo[0]" &c. Wherefore I commend unto thee that when thou wouldst chastise thy brethren so, thou shouldst say instead "How thou discreditest thyself", or "how ye discredit yourself".

I think I agree with your point, though. Autovivification is one of those features that's both a strength and a weakness of Perl. A strength, because it's a very practical feature that makes life a bit easier for you and me; a weakness, because people who suffer from a surfeit of the wrong sort of laziness use it as an example of how Perl is "too hard".

  • Comment on Re^3: Why does exists cause autovivication?

Replies are listed 'Best First'.
Re^4: Why does exists cause autovivication?
by demerphq (Chancellor) on Jan 04, 2008 at 15:46 UTC

    Whoops! :-) Thanks for the correction. (And ++ for style :-)


Re^4: Why does exists cause autovivication?
by Anonymous Monk on Jul 19, 2017 at 20:47 UTC
    Even the Perl people call autovivication - specifically when using exists - surprising behaviour:


    Near the bottom:
    "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."

    I would not expect something that just returns true or false to modify the data that is passed to it. I know that people are going to say that it is not modifying the data passed to it - because you are actually dereferencing the hash elements in the CALL to exists() - not within exists itself. However, there must be some way to prevent it from happening when calling exists(). If any one of the lower hash keys does not exist in the hash being tested, then obviously the one you're testing for does not, and exists() should just return false.

      It's not that hard.

      Either, instead of exists, use Data::Diver (by tye):

      $ perl -Mstrict -MData::Diver=Dive -MData::Dumper -wE ' my $h; $h->{foo}->{bar}->{baz} = "qux"; say Dumper $h; say Dive( $h, qw/ foo bar baz / ) ? 1 : 0; say Dive( $h, qw/ foo NOT baz / ) ? 1 : 0; say Dumper $h; ' $VAR1 = { 'foo' => { 'bar' => { 'baz' => 'qux' } } }; 1 0 $VAR1 = { 'foo' => { 'bar' => { 'baz' => 'qux' } } };

      Or, if you want to use exists, just test for each level that you are not sure exists before going any deeper:

      $ perl -Mstrict -MData::Dumper -wE ' my $h; $h->{foo}->{bar}->{baz} = "qux"; say Dumper $h; say( (exists $h->{foo}->{bar}->{baz}) ? 1 : 0 ); say( (exists $h->{foo}->{NOT} && exists $h->{foo}->{NOT}->{baz}) ? 1 : + 0 ); say Dumper $h; ' $VAR1 = { 'foo' => { 'bar' => { 'baz' => 'qux' } } }; 1 0 $VAR1 = { 'foo' => { 'bar' => { 'baz' => 'qux' } } };

      The way forward always starts with a minimal test.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://660326]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (2)
As of 2018-08-15 01:35 GMT
Find Nodes?
    Voting Booth?
    Asked to put a square peg in a round hole, I would:

    Results (158 votes). Check out past polls.