in reply to Passing a scalar and hash as subroutine arguments

How wrong am I?

Grab a copy of the free book Modern Perl , it explains that yes , everything does flatten into @_ , its an array of scalars ( "aliases" )

  • Comment on Re: Passing a scalar and hash as subroutine arguments

Replies are listed 'Best First'.
Re^2: Passing a scalar and hash as subroutine arguments
by Preceptor (Deacon) on Jan 25, 2014 at 09:47 UTC

    Yes - everything does get 'squashed'. Internally, an array and a hash are much the same - it's just a hash is an array with an even number of paired values. So you can coerce from hash to array and back again. That's what's happening here.

    Probably the easiest way of handling passing a whole hash into a subroutine is by reference. E.g. invoke your sub with:

    my_sub ( $value, \%myhash );

    And within the sub:

    my ( $value, $hashref ) = @_; foreach my $key ( keys %$hashref ) { print "$key = $$hashref{'$key'}\n"; }

    Although you should note that a has passed by reference - if you modify it within the sub, it'll modify the 'source instance'. Read 'perlref' for more detail.