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