in reply to More efficient ways to pass values to a sub routine

Hi Anonymous,

send_1 is a good example of passing named parameters to a sub with a hash. (Update: Technically, what is happening is that the subroutine is passed a flat list of key+value pairs, since using the => operator doesn't automatically make something a hash. However, once you say my (%args) = @_;, that flat list of key+value pairs is stored in a hash.)

However, in send_2, in the call of the sub (send_2( { name => ... } )) what you are passing the sub is a reference to an anonymous hash, whereas the implementation of sub send_2 is expecting a flat list of positional parameters. The correct way to call the sub send_2 in your example code would be:

send_2( $name, $pic, 'test' );

If instead you wanted to implement arguments passed in as a hash reference, you'd need to rewrite sub send_2 like this (untested):

sub send_2 { my ($args) = @_; my $name = $args->{ name } || ''; my $filehandle = $args->{ filehandle } || ''; my $id = $args->{ id } || ''; }

Update: See "Use Rule 2" in perlreftut for an explanation of the $args->{...} syntax. Note that it could also be written $$args{...}.

Hope this helps,
-- Hauke D