>>>>> "PH" == Patrick Hartman <> writes:
PH> Hi all, I was working on a program today and made a sub routine that I
PH> was passing a few scalars and also a hash to as arguments. This is
PH> what I initially tried:
PH> my %foo = qw(pepsi cola doctor pepper seven up);
PH> my $bar = 'soda is yummy!';
PH> &drink(%foo,$bar);
read perldoc perlsub. that isn't doing what you think it does.
PH> sub drink {
PH> my (%sodas,$opinion) = @_;
actually that is the bad line. the hash will slurp in all of @_ (and
should give you an odd count warning IF you used warnings which you
should do). nothing will ever be assigned to $opinion.
PH> The result was not what I was expecting. The $bar value that I was
PH> expecting to be the $opinion scalar ended up as a extra key in the
PH> %sodas hash as undef, and $opinion was empty. I played around a little
PH> and found that if I reverse the order so the scalar is passed before
PH> the hash, it worked like I was hoping:
PH> my %foo = qw(pepsi cola doctor pepper seven up);
PH> my $bar = 'soda is yummy!';
PH> &drink($bar,%foo);
PH> sub drink {
PH> my ($opinion,%sodas) = @_;
PH> # do whatever here
PH> print "yummy!";
PH> }
PH> Is there a rule as to what order to pass types to a subroutine? I'm a
PH> little confused on this one. Thanks,
no rule about order. just you the first array or hash in the left side
of any assignment gets all the rest of the values from the right
side. so if you must have one, it should be the last thing on the left
side. or use references to pass multiple aggregates around.
uri
--
Uri Guttman ------
--------
http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ----
http://bestfriendscocoa.com ---------