Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re^2: hash from sub directly into foreach loop

by GrandFather (Saint)
on Jun 30, 2006 at 11:30 UTC ( [id://558552]=note: print w/replies, xml ) Need Help??


in reply to Re: hash from sub directly into foreach loop
in thread hash from sub directly into foreach loop

Note that:

my $s = ();

is equivelent to

my $s = (undef);

which is equivelent to:

my $s;

The first use of a hash ref autovivifies the referred hash making the previous assignment redundant (and misleading). Consider:

use strict; use warnings; use Data::Dump::Streamer; my $s = (); Dump ($s); $s->{'a'} = 10 ; Dump ($s);

which prints:

$VAR1 = undef; $HASH1 = { a => 10 };

and note that the contents of $s changes from an undefined value to a hash ref.

If you really want to indicate intent with an assignment use:

my $s = {};

DWIM is Perl's answer to Gödel

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://558552]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (9)
As of 2024-04-19 13:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found