Odd number of elements in hash assignment with default

后端 未结 2 1178
逝去的感伤
逝去的感伤 2020-12-22 00:36

Greeting Dear Community.

I\'m trying to make a sub in perl that takes a hash and a debug flag which defaults to zero. However I keep getting this error

2条回答
  •  伪装坚强ぢ
    2020-12-22 01:14

    You want to pass your hashes by value to your subroutine and this creates the problem. Try to pass your hash %h2 by reference instead (notice the \ before the %):

    &hash2print(\%h2, 1);
    

    Then in your sub hash2print, you can get the hash back in the following way:

    sub hash2print {
        (my $hashref, my $debug) = @_;
        my %HoH = %$hashref; # dereference $hashref to get back the hash
        ...
    

    You can read more about references here if you don't understand the concepts behind them.

提交回复
热议问题