NetWallah has asked for the wisdom of the Perl Monks concerning the following question:
I'm guessing there is some coding design pattern with a best practice that wiser monks recognize here, and enlighten me : WWLD ? ( What would Larry do ?)use strict; my $href; fill_hashref ($href); print qq|TEST 1: $_\t$href->{$_}| for keys %$href; # Prints NOTHING my $empty_hash_href={}; fill_hashref ($empty_hash_href); print qq|TEST 2: $_\t$empty_hash_href->{$_}\n| for keys %$empty_hash_h +ref; # Works as expected sub fill_hashref{ my ($lref) = @_; $lref->{RETURNVALUE}="I would like to see this"; }
Update: Just to clarify - what I'm struggling with is that I occasionally forget to initialize the href passed in, and get bad results.
I am aware that an alternate solution is to use something like this in the sub:
But that has a certain ugliness (smell ?) to it, and was looking for a prettier, more aromatic solution.$_[0]->{WHATEVER}="blah";
While I am at it - how would this look/behave in perl6 ?
Syntactic sugar causes cancer of the semicolon. --Alan Perlis
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Populating an undefined hashref in a sub
by ikegami (Patriarch) on Dec 11, 2010 at 07:08 UTC | |
|
Re: Populating an undefined hashref in a sub
by NetWallah (Canon) on Dec 11, 2010 at 07:24 UTC | |
by Anonymous Monk on Dec 11, 2010 at 08:31 UTC | |
|
Re: Populating an undefined hashref in a sub
by Anonymous Monk on Dec 11, 2010 at 06:42 UTC |