Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Return a hashref into another

by neilwatson (Priest)
on Jan 28, 2016 at 21:10 UTC ( [id://1153931]=perlquestion: print w/replies, xml ) Need Help??

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

Greetings, I have this code where tests one and two fail. what does the error in line 18 mean in this instance and why are those hash keys destroyed?

#!/usr/bin/perl use strict; use warnings; use Test::More; use Data::Dumper; my $master_ref = { one => "first", two => "second", }; sub return_hash_ref { return { three => "third", four => "forth" }; }; print "Before ".Dumper( $master_ref ); $master_ref = ( $master_ref, return_hash_ref() ); # line 18 print "After ".Dumper( $master_ref ); ok( $master_ref->{ one } eq 'first', 'one' ); ok( $master_ref->{ two } eq 'second', 'two' ); ok( $master_ref->{ three } eq 'third', 'three' ); ok( $master_ref->{ four } eq 'forth', 'four' ); done_testing;

Running it:

Useless use of private variable in void context at appendhashref.pl li +ne 18. Before $VAR1 = { 'two' => 'second', 'one' => 'first' }; After $VAR1 = { 'four' => 'forth', 'three' => 'third' }; Use of uninitialized value in string eq at appendhashref.pl line 21. not ok 1 - one # Failed test 'one' # at appendhashref.pl line 21. Use of uninitialized value in string eq at appendhashref.pl line 22. not ok 2 - two # Failed test 'two' # at appendhashref.pl line 22. ok 3 - three ok 4 - four 1..4 # Looks like you failed 2 tests of 4.

Neil Watson
watson-wilson.ca

Replies are listed 'Best First'.
Re: Return a hashref into another
by NetWallah (Canon) on Jan 28, 2016 at 21:21 UTC
    You are mixing scalars($master_ref) and lists ().

    Try this instead:

    $master_ref = { %$master_ref, %{return_hash_ref()} }; # line 18 # .. both sides of "=" are now scalars # hashrefs are expanded to hashes

            "I can cast out either one of your demons, but not both of them." -- the XORcist

Re: Return a hashref into another
by Anonymous Monk on Jan 28, 2016 at 21:24 UTC

    Line 18 currently looks like this:

    $master_ref = ( $master_ref, return_hash_ref() ); # line 18

    Change it to one of the two following options:

    my $returned = return_hash_ref(); @{$master_ref}{keys %$returned} = values %$returned; # OR my $returned = return_hash_ref(); $master_ref->{$_} = $returned->{$_} for keys %$returned;

    If you don't mind changing the underlying reference held by $master_ref (in other words, if nobody else is relying on $master_ref pointing to a specific hashref), you could do this:

    $master_ref = { %$master_ref, %{return_hash_ref()} };

    But if someone else also holds a reference to the same hash that $master_ref held, that relationship would be broken.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (4)
As of 2024-04-19 21:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found