P
Patrick Hartman
Hi all, I was working on a program today and made a sub routine that I
was passing a few scalars and also a hash to as arguments. This is
what I initially tried:
my %foo = qw(pepsi cola doctor pepper seven up);
my $bar = 'soda is yummy!';
&drink(%foo,$bar);
sub drink {
my (%sodas,$opinion) = @_;
# do whatever here
print "yummy!";
}
The result was not what I was expecting. The $bar value that I was
expecting to be the $opinion scalar ended up as a extra key in the
%sodas hash as undef, and $opinion was empty. I played around a little
and found that if I reverse the order so the scalar is passed before
the hash, it worked like I was hoping:
my %foo = qw(pepsi cola doctor pepper seven up);
my $bar = 'soda is yummy!';
&drink($bar,%foo);
sub drink {
my ($opinion,%sodas) = @_;
# do whatever here
print "yummy!";
}
Is there a rule as to what order to pass types to a subroutine? I'm a
little confused on this one. Thanks,
Patrick
was passing a few scalars and also a hash to as arguments. This is
what I initially tried:
my %foo = qw(pepsi cola doctor pepper seven up);
my $bar = 'soda is yummy!';
&drink(%foo,$bar);
sub drink {
my (%sodas,$opinion) = @_;
# do whatever here
print "yummy!";
}
The result was not what I was expecting. The $bar value that I was
expecting to be the $opinion scalar ended up as a extra key in the
%sodas hash as undef, and $opinion was empty. I played around a little
and found that if I reverse the order so the scalar is passed before
the hash, it worked like I was hoping:
my %foo = qw(pepsi cola doctor pepper seven up);
my $bar = 'soda is yummy!';
&drink($bar,%foo);
sub drink {
my ($opinion,%sodas) = @_;
# do whatever here
print "yummy!";
}
Is there a rule as to what order to pass types to a subroutine? I'm a
little confused on this one. Thanks,
Patrick