Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

say I have some("something");, that sends a sub 1 peoce of information, but how would I send it two types of information. and in that sub
sub some { $1 = shift; $2 = shift; }

edited: Wed Jun 26 15:40:04 2002 by jeffa - corrected title misspelling

Replies are listed 'Best First'.
Re: Sne ding Information to Sub
by erasei (Pilgrim) on Jun 25, 2002 at 20:46 UTC
    Probably the easiest:
    some("Foo", "Bar"); sub some { my ($one, $two) = @_; }
    I am sure many others will most faster, cooler, and more elegant ways.. but that works, and is easy to read.

    Update: Maybe I wasn't reading that right the first time.. I think you maybe wanted to send a scalar as one argument, and an array (just examples) as the other? In any event, it would work the same:

    my @alphabet = ('A' .. 'Z'); some("Foo", @alphabet); sub some { my ($one, @alpha) = @_; }
      This works as erasei describes:       some("Foo", @alphabet); ...but be careful. You will be tempted on another occasion to do one of the following:
      some(@alphabet, "Foo"); # BAD! some("Foo", @alphabet, @alpha2); # BAD!
      These fail because the first array encountered in the parameter list will gobble up all the remaining values.

      So you can do    some("Foo", @alphabet); and it will work because the only array in the list is the last parameter (same rule for hashes), but some would argue that it could be the beginning of a bad habit -- because later you have to be especially alert if you add another parameter for this sub.

      The option is to pass a reference to the array.

      some("Foo", \@alphabet); sub some { my ($one, $alpha_aref) = @_; my $letter_b = $alpha_aref->[1]; }
      Thus allowing you to pass as many arrays and hashes as you may need and mix them with scalars in any order.
Re: Sne ding Information to Sub
by thor (Priest) on Jun 26, 2002 at 12:24 UTC
    You can use the method that you stated, however you cannot use the variables you used. $1, $2, et al are special variables used to catpure in a regular expression. You cannot assign to them explicitly. I'll also head off the next logical avenue: don't use $a and $b; they are special variables used by the sort function. You can assign to them, but it affect sort if you do. I would suggest using descriptive variable names.

    thor