NetWallah has asked for the wisdom of the Perl Monks concerning the following question:

This is a query about best practice in approaching this problem. The code below illustrates the problem, and the solution I have used (pre-defining a hashref).
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"; }
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 ?)

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:

$_[0]->{WHATEVER}="blah";
But that has a certain ugliness (smell ?) to it, and was looking for a prettier, more aromatic solution.

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

    But that has a certain ugliness (smell ?) to it, and was looking for a prettier, more aromatic solution.

    Yet that's what you're saying you want to do. You could cover the smell with an alias, but there's no avoiding passing the variable by reference if you want to pass the variable by reference.

    Update: Here's a simple way of doing it:

    sub fill_hashref{ my $lref = $_[0] //= {}; $lref->{RETURNVALUE}="I would like to see this"; }
Re: Populating an undefined hashref in a sub
by NetWallah (Canon) on Dec 11, 2010 at 07:24 UTC
    Thank you, anonymonk and ikegami - for pointing out that it was indeed a pass-by-ref, and can be dressed up, and perfumed with non-native syntax.

    That passes the glamour and olfactory bar.

    Any perl6 pointers ?

    Update: Thanks, ikegami for the native syntax!

    Update2: Hmm - I noticed you changed from ||= to //=
    Any harm in this case with||= ? I need this to work on pre 5.10 perl.

         Syntactic sugar causes cancer of the semicolon.        --Alan Perlis

      Any harm in this case with||= ? I need this to work on pre 5.10 perl.
      No harm using 'or' instead of 'defined-or' in Perl 5 if the value is always a reference: references are never false values.
Re: Populating an undefined hashref in a sub
by Anonymous Monk on Dec 11, 2010 at 06:42 UTC
    You could do
    fill_hashref (my $href = []);
    Aliasing is the trick, observe
    use DDS; my @snot = 1 .. 2; Dump(\@snot); blot(@snot); Dump(\@snot); glot(@snot); Dump(\@snot); sub blot { $_++ for @_; # @_ holds aliases, $_ also aliases return; } sub glot { my( @copies ) = @_; $_++ for @copies; return; } __END__ $ARRAY1 = [ 1, 2 ]; $ARRAY1 = [ 2, 3 ]; $ARRAY1 = [ 2, 3 ];
    See Data::Alias for
    alias my ($x, $y) = @_; # named aliases to arguments