[32574] in Perl-Users-Digest

home help back first fref pref prev next nref lref last post

Perl-Users Digest, Issue: 3844 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sun Dec 23 16:09:36 2012

Date: Sun, 23 Dec 2012 13:09:04 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Sun, 23 Dec 2012     Volume: 11 Number: 3844

Today's topics:
    Re: Examining the existence of a hash key instantiates  <hjp-usenet2@hjp.at>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

----------------------------------------------------------------------

Date: Sun, 23 Dec 2012 15:09:51 +0100
From: "Peter J. Holzer" <hjp-usenet2@hjp.at>
Subject: Re: Examining the existence of a hash key instantiates higher level keys
Message-Id: <slrnkde45f.6cu.hjp-usenet2@hrunkner.hjp.at>

On 2012-12-19 09:52, Willem <willem@turtle.stack.nl> wrote:
> Peter J. Holzer wrote:
> ) On 2012-12-18 23:06, Willem <willem@turtle.stack.nl> 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
| |   | hjp@hjp.at         | die Satzbestandteile des Satzes nicht mehr
__/   | http://www.hjp.at/ | zusammenpaßt. -- Ralph Babel


------------------------------

Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>


Administrivia:

To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.

Back issues are available via anonymous ftp from
ftp://cil-www.oce.orst.edu/pub/perl/old-digests. 

#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.


------------------------------
End of Perl-Users Digest V11 Issue 3844
***************************************


home help back first fref pref prev next nref lref last post