Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Re: Examining the existence of a hash key instantiates higher level keys

Reply
Thread Tools

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

 
 
Matt Hicks
Guest
Posts: n/a
 
      12-18-2012
> 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...tovivification
http://en.wikipedia.org/wiki/Autovivification

The CPAN also has a module to disable the behavior.

http://search.cpan.org/~vpit/autoviv...ivification.pm
 
Reply With Quote
 
 
 
 
Willem
Guest
Posts: n/a
 
      12-18-2012
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...tovivification
)
) 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
 
Reply With Quote
 
 
 
 
C.DeRykus
Guest
Posts: n/a
 
      12-19-2012
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
 
Reply With Quote
 
Peter J. Holzer
Guest
Posts: n/a
 
      12-19-2012
On 2012-12-18 23:06, Willem <(E-Mail Removed)> 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:umper -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:umper -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
| | | http://www.velocityreviews.com/forums/(E-Mail Removed) | die Satzbestandteile des Satzes nicht mehr
__/ | http://www.hjp.at/ | zusammenpaßt. -- Ralph Babel
 
Reply With Quote
 
Peter J. Holzer
Guest
Posts: n/a
 
      12-19-2012
On 2012-12-19 05:49, C.DeRykus <(E-Mail Removed)> 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
| | | (E-Mail Removed) | die Satzbestandteile des Satzes nicht mehr
__/ | http://www.hjp.at/ | zusammenpaßt. -- Ralph Babel
 
Reply With Quote
 
Peter Makholm
Guest
Posts: n/a
 
      12-19-2012
"C.DeRykus" <(E-Mail Removed)> 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
 
Reply With Quote
 
Willem
Guest
Posts: n/a
 
      12-19-2012
Peter J. Holzer wrote:
) On 2012-12-18 23:06, Willem <(E-Mail Removed)> 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
 
Reply With Quote
 
Peter J. Holzer
Guest
Posts: n/a
 
      12-23-2012
On 2012-12-19 09:52, Willem <(E-Mail Removed)> wrote:
> Peter J. Holzer wrote:
> ) On 2012-12-18 23:06, Willem <(E-Mail Removed)> 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
| | | (E-Mail Removed) | die Satzbestandteile des Satzes nicht mehr
__/ | http://www.hjp.at/ | zusammenpaßt. -- Ralph Babel
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Examining the existence of a hash key instantiates higher level keys Rainer Weikusat Perl Misc 5 12-19-2012 09:55 PM
Hash key types and equality of hash keys Tim McDaniel Perl Misc 2 03-01-2012 06:32 PM
hash of hash of hash of hash in c++ rp C++ 1 11-10-2011 04:45 PM
hash key to var name of value hash key value Une bévue Ruby 5 08-10-2006 04:05 PM
Accessing higher security level from higher security level nderose@gmail.com Cisco 0 07-11-2005 10:20 PM



Advertisments