Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Re^2: Why does exists cause autovivication?

by demerphq (Chancellor)
on Dec 29, 2007 at 14:17 UTC ( #659489=note: print w/replies, xml ) Need Help??

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

Oh how ye discredit thyself! Auto-viv is much more useful than you make it sound and it only rarely causes problems, usually because someone hasn't managed to understand how it works.


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

Replies are listed 'Best First'.
Re^3: Why does exists cause autovivication?
by planetscape (Chancellor) on Dec 31, 2007 at 11:06 UTC
Re^3: Why does exists cause autovivication?
by Porculus (Hermit) on Jan 03, 2008 at 22:00 UTC

    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".

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


      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://659489]
and the pool shimmers...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2018-06-19 01:27 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (111 votes). Check out past polls.