Re: Examining the existence of a hash key instantiates higher level keys

Discussion in 'Perl Misc' started by Matt Hicks, Dec 18, 2012.

  1. Matt Hicks

    Matt Hicks Guest

    Matt Hicks, Dec 18, 2012
    #1
    1. Advertising

  2. Matt Hicks

    Willem Guest

    Re: Examining the existence of a hash key instantiates higher levelkeys

    Henry Law wrote:
    ) On 18/12/12 21:55, Matt Hicks wrote:
    )>> In other words, examining the existence of a subkey of a key that
    )>> doesn't itself exist calls the higher level key into being.
    )>
    )> This is called autovivification. See
    )>
    )> http://perldoc.perl.org/perlglossary.html#autovivification
    )
    ) I should have read the reference more clearly before my post a few
    ) minutes ago. The situation here isn't quite the same as the one in my
    ) program. In the quoted example
    )
    ) $a[5][5][5][5][5] = "quintet"
    )
    ) It's clear that for the text item to exist at the fifth level the other
    ) four have to exist too. It's therefore logical to create them; how
    ) could it be otherwise.
    )
    ) But in my program all I was doing was examining the existence of
    ) $hash->{three}{one}{a} when $hash->{three} didn't exist.

    But surely, $hash->{three} must exist, for otherwise, how could you examine
    the existence of an element inside it? So therefore it was created for you.

    By examining the existence of $hash->{three}->{one}->{a}, you are
    assuming thah $hash->{three} is a hash reference, which contains
    $hash->{three}->{one}, also a hash reference. As it turns out, they
    were not, but by assuming that they are, they come into existence.


    PS: Any other language would have thrown an exception.


    SaSW, Willem
    --
    Disclaimer: I am in no way responsible for any of the statements
    made in the above text. For all I know I might be
    drugged or something..
    No I'm not paranoid. You all think I'm paranoid, don't you !
    #EOT
    Willem, Dec 18, 2012
    #2
    1. Advertising

  3. Matt Hicks

    C.DeRykus Guest

    On Tuesday, December 18, 2012 2:15:59 PM UTC-8, Henry Law wrote:
    > On 18/12/12 21:55, Matt Hicks wrote:
    >
    > >> In other words, examining the existence of a subkey of a key that

    >
    > >> doesn't itself exist calls the higher level key into being.

    >
    > >

    >
    > > This is called autovivification. See

    >
    >
    >
    > That something illogical happens is irritating; to find that it's got a
    >
    > name doesn't make it any less so!
    >
    >


    I think this behaviour will probably be modified/modifiable at some point.

    From perldoc -f exist:
    ...
    undef $ref;
    if (exists $ref->{"Some key"}) { }
    print $ref; # prints HASH(0x80d3d5c)

    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.

    --
    Charles DeRykus
    C.DeRykus, Dec 19, 2012
    #3
  4. Re: Examining the existence of a hash key instantiates higher levelkeys

    On 2012-12-18 23:06, Willem <> wrote:
    > Henry Law wrote:
    > ) But in my program all I was doing was examining the existence of
    > ) $hash->{three}{one}{a} when $hash->{three} didn't exist.
    >
    > But surely, $hash->{three} must exist, for otherwise, how could you
    > examine the existence of an element inside it?


    If $hash->{three} doesn't exist, $hash->{three}{one}{a} can't exist
    either, so exists($hash->{three}{one}{a}) could return false as soon as
    it detects that $hash->{three} doesn't exist. There is no need to create
    those intermediate levels.

    > PS: Any other language would have thrown an exception.


    Perl does throw an exception instead of autovivifying in a similar case:

    % perl -Mstrict -MData::Dumper -E '
    my $hash = {};
    say %{ $hash->{three}{one} };
    say Dumper $hash'
    Can't use an undefined value as a HASH reference at -e line 3.

    But curiosly, it doesn't if I get try to get the keys of the
    non-existent hash:

    % perl -Mstrict -MData::Dumper -E '
    my $hash = {};
    say keys %{ $hash->{three}{one} };
    say Dumper $hash'

    $VAR1 = {
    'three' => {
    'one' => {}
    }
    };


    hp

    --
    _ | Peter J. Holzer | Fluch der elektronischen Textverarbeitung:
    |_|_) | Sysadmin WSR | Man feilt solange an seinen Text um, bis
    | | | | die Satzbestandteile des Satzes nicht mehr
    __/ | http://www.hjp.at/ | zusammenpaßt. -- Ralph Babel
    Peter J. Holzer, Dec 19, 2012
    #4
  5. Re: Examining the existence of a hash key instantiates higher levelkeys

    On 2012-12-19 05:49, C.DeRykus <> wrote:
    > I think this behaviour will probably be modified/modifiable at some point.
    >
    > From perldoc -f exist:
    > ...
    > undef $ref;
    > if (exists $ref->{"Some key"}) { }
    > print $ref; # prints HASH(0x80d3d5c)
    >
    > 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.


    Ah, I missed that when we last had a discussion about autovivification.
    Might have been useful.

    hp


    --
    _ | Peter J. Holzer | Fluch der elektronischen Textverarbeitung:
    |_|_) | Sysadmin WSR | Man feilt solange an seinen Text um, bis
    | | | | die Satzbestandteile des Satzes nicht mehr
    __/ | http://www.hjp.at/ | zusammenpaßt. -- Ralph Babel
    Peter J. Holzer, Dec 19, 2012
    #5
  6. "C.DeRykus" <> writes:

    > From perldoc -f exist:
    > ...


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


    It is not really specific to exists(). It is probably mostly noted in
    the exists() documentation because it might be counter intuitive to the
    specific use case for exists.

    In general function arguments are considered lvalue context, which
    implies autovivification. The reason is that the @_ elements are aliased
    to the arguments, which requires the arguments to sort of exists.

    I think that the general consensus is that it *is* inconvenient and that
    it might have been better if the autovivification only happended is
    someone actually used the alias in an actual lvalue context. But I guess
    that it is not trivial to implement and it might not be inconvenient
    enough to break backwards compatibility.

    //Makholm
    Peter Makholm, Dec 19, 2012
    #6
  7. Matt Hicks

    Willem Guest

    Re: Examining the existence of a hash key instantiates higher levelkeys

    Peter J. Holzer wrote:
    ) On 2012-12-18 23:06, Willem <> wrote:
    )> Henry Law wrote:
    )> ) But in my program all I was doing was examining the existence of
    )> ) $hash->{three}{one}{a} when $hash->{three} didn't exist.
    )>
    )> But surely, $hash->{three} must exist, for otherwise, how could you
    )> examine the existence of an element inside it?
    )
    ) If $hash->{three} doesn't exist, $hash->{three}{one}{a} can't exist
    ) either,

    If $hash->{three} doesn't exist, then $hash->{three}{one}{a} is *meaningless*.
    The only way it can have meaning is for $hash->{three} to be a hashref.

    This: exists $hash->{three}{one}{a}

    Means: Take element 'three' of hashref $hash, which is a hashref.
    In this hashref, take element 'one', which is a hashref.
    In this hashref, take element 'a' and look if it exists.

    So, you're telling Perl that $hash->{three} and $hash->{three}{one} are
    hashrefs, by using them as such.

    You are thinking of multi-level hashes as a single entity when they're not.


    SaSW, Willem
    --
    Disclaimer: I am in no way responsible for any of the statements
    made in the above text. For all I know I might be
    drugged or something..
    No I'm not paranoid. You all think I'm paranoid, don't you !
    #EOT
    Willem, Dec 19, 2012
    #7
  8. Re: Examining the existence of a hash key instantiates higher levelkeys

    On 2012-12-19 09:52, Willem <> wrote:
    > Peter J. Holzer wrote:
    > ) On 2012-12-18 23:06, Willem <> wrote:
    > )> Henry Law wrote:
    > )> ) But in my program all I was doing was examining the existence of
    > )> ) $hash->{three}{one}{a} when $hash->{three} didn't exist.
    > )>
    > )> But surely, $hash->{three} must exist, for otherwise, how could you
    > )> examine the existence of an element inside it?
    > )
    > ) If $hash->{three} doesn't exist, $hash->{three}{one}{a} can't exist
    > ) either,
    >
    > If $hash->{three} doesn't exist, then $hash->{three}{one}{a} is *meaningless*.
    > The only way it can have meaning is for $hash->{three} to be a hashref.


    1. The context was "testing the existence". The answer to the question
    "does $hash->{three}{one}{a} exist?" is clearly "no, it does not
    exist", whether you go through the trouble of creating the
    intermediate levels or not. So creating these levels not necessary to
    answer the question.

    2. Nothing is per se "meaningless" or "meaningful" in a programming
    language. Every expression only has the meaning given to it by the
    language designer(s).

    If $hash->{three} doesn't exist, then $hash->{three}{one}{a} could be
    undef or evaluating the expression could throw an exception (or it
    could start playing the towers of hanoi, or something - as I said,
    it's the language designer's choice - but some choices are generally
    useful but aren't).

    In both cases the result can be obtained without creating the
    intermediate levels, because you can return or die as soon as you
    encounter the non-existent element. Nothing after that element can
    alter the result, you don't need to create those levels to see that.


    > This: exists $hash->{three}{one}{a}
    >
    > Means: Take element 'three' of hashref $hash, which is a hashref.
    > In this hashref, take element 'one', which is a hashref.
    > In this hashref, take element 'a' and look if it exists.
    >
    > So, you're telling Perl that $hash->{three} and $hash->{three}{one} are
    > hashrefs, by using them as such.
    >
    > You are thinking of multi-level hashes as a single entity when they're not.


    No, I am thinking about how such an expression could be evaluated in a
    saner language. I know how perl does it, so any argument "it is like
    this therefore it must be like this" is missing the point. You aren't
    telling me anything I haven't known for 15 years. And sentences like

    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.

    in the docs together with the inconsistencies confirm my conviction that
    autovivification in Perl is mostly an accident of implementation and not
    a well considered feature.

    hp


    --
    _ | Peter J. Holzer | Fluch der elektronischen Textverarbeitung:
    |_|_) | Sysadmin WSR | Man feilt solange an seinen Text um, bis
    | | | | die Satzbestandteile des Satzes nicht mehr
    __/ | http://www.hjp.at/ | zusammenpaßt. -- Ralph Babel
    Peter J. Holzer, Dec 23, 2012
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. pabbu
    Replies:
    8
    Views:
    720
    Marc Boyer
    Nov 7, 2005
  2. rp
    Replies:
    1
    Views:
    516
    red floyd
    Nov 10, 2011
  3. Une bévue
    Replies:
    5
    Views:
    148
    Une bévue
    Aug 10, 2006
  4. Tim McDaniel

    Hash key types and equality of hash keys

    Tim McDaniel, Mar 1, 2012, in forum: Perl Misc
    Replies:
    2
    Views:
    789
    Tim McDaniel
    Mar 1, 2012
  5. Rainer Weikusat
    Replies:
    5
    Views:
    221
    Rainer Weikusat
    Dec 19, 2012
Loading...

Share This Page