in reply to Concatenate anonymous hash ref

"." is string concatenation. Not what you want.

If you were dealing with hashes directly, you'd do

%x = ( %x, bar => "def", baz => "ghi" );

or better yet

@x{qw( bar baz )} = ( "def", "ghi" );

But you're dealing with references to hash, so you simply need to dereference first as per Dereferencing Syntax.

%$x = ( %$x, bar => "def", baz => "ghi" );
@$x{qw( bar baz )} = ( "def", "ghi" );

Update: Ah shoot! I had @x[...] where I should have had @x{...}. Fixed.

Replies are listed 'Best First'.
Re^2: Concatenate anonymous hash ref
by JavaFan (Canon) on Oct 14, 2008 at 19:11 UTC
    "." is string concatenation. Not what you want.

    But this is Perl. If you really, really want it, it can happen!

    use 5.010; use strict; use warnings; my $x = {foo => "abc"}; { package ChocolateSauce; use overload '.' => \&whipped_cream; sub whipped_cream { my ($adam, $eve) = @_[$_[2] ? (1, 0) : (0, 1)]; $$adam{$_} = $$eve{$_} for keys %$eve; $adam; } } bless $x => 'ChocolateSauce'; $x .= {bar => 'def', baz => 'ghi'}; while (my ($k, $v) = each %$x) { say "$k => $v"; } __END__ bar => def baz => ghi foo => abc