For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > June 2005 > $p{"Bryan"}{"age"} = 31









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author $p{"Bryan"}{"age"} = 31
Bryan R Harris

2005-05-31, 3:57 pm



Is there anything wrong with out of the blue making a statement like this:

$p{"Bryan"}{"age"} = 31;

I thought perl required me to use the anonymous hash composer:

$p{"Bryan"} = { "age" => 31 };

.... but the first example seems to work. So I was just wondering if that's
okay, or if it's a no-no.

(I'm learning PHP now, and there seem to be a lot of no-nos in PHP that
aren't intuitive.)

- B


Bob Showalter

2005-05-31, 3:57 pm

Bryan R Harris wrote:
> Is there anything wrong with out of the blue making a statement like
> this:
>
> $p{"Bryan"}{"age"} = 31;


That's fine, as long as $p{Bryan} doesn't exist or is already a hashref.
See: http://c2.com/cgi/wiki?AutoVivification.

For safety, be sure to "use strict" so you don't end up treating $p{Bryan}
as a soft reference:

$p{Bryan} = 'foo';
$p{Bryan}{age} = '31'; # this is like $foo{age} = 31

Also, the quotes are not strictly required.

>
> I thought perl required me to use the anonymous hash composer:
>
> $p{"Bryan"} = { "age" => 31 };


That's fine too, but not exactly equivalent.
Randal L. Schwartz

2005-06-02, 8:56 pm

>>>>> "Bob" == Bob Showalter <Bob_Showalter@taylorwhite.com> writes:

Bob> That's fine, as long as $p{Bryan} doesn't exist or is already a hashref.
Bob> See: http://c2.com/cgi/wiki?AutoVivification.

And <http://www.stonehenge.com/merlyn/UnixReview/col44.html>.

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
Bryan R Harris

2005-06-03, 3:56 am



Excellent! Perl is so !

Thanks, Randal.

- B


>
> Bob> That's fine, as long as $p{Bryan} doesn't exist or is already a hashref.
> Bob> See: http://c2.com/cgi/wiki?AutoVivification.
>
> And <http://www.stonehenge.com/merlyn/UnixReview/col44.html>.



Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com