in reply to Trouble passing hash into function

SUMMARY: How do I take a hash being passed into a function as @_ and convert it to a hash I can do something with, like %myhash?
If that's what you really want to do, you don't have to do very much at all
hashy( %x ); sub hashy { my %myhash = @_ }

That should do what you want.

Replies are listed 'Best First'.
Re^2: Trouble passing hash into function
by dbonneville (Acolyte) on Sep 05, 2007 at 23:02 UTC
    That's what I thought would work but here is what's happening:
    sub test_parser { use Data::Dumper; #print Dumper(@_); my %test = @_; print Dumper(%test); }
    If I comment out the second print and uncomment the first print, the @_ dumps fine. When I try to set %test, I get this error:

    Reference found where even-sized list expected at Debugger.pm line 8.
    I'm sending the hash from a master .pl file into the function like this:

    Debugger::test_parser(\%competitors_master);
    What else could be wrong?

    Thanks,

    Doug

      You are passing a hash reference, not a hash (which gets flattened into a list).

      The first dump works as expected because it sees a list containing one element which is the reference to the hash you passed in.

      The my %test = @_; and generates an error because @_ contains only one element - the reference to the hash. Instead you should my $test = shift; and work with the reference.


      DWIM is Perl's answer to Gödel