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

Hi ,

Is there any way to compress both these statements to one line ? It would only seem rational that we have a way to do so, especially if i am passing 10 references to my subroutine.

sub foo ( my $query_ref = shift(@_); my $session_ref = shift(@_); #do something return \$someref; }

Not directly related to the above, but going through the man pages , i see there is no formal parameter support , at least in perl 5.10.1 (which is my customer version ) . I heard we have subroutine signature support in perl5.20. if so can you point me to the man pages for that ?

Replies are listed 'Best First'.
Re: Using Shift with Parameters in a subroutine
by Anonymous Monk on Oct 20, 2014 at 09:11 UTC
      thanks.! :D
        If you just want to scoop the first couple of parameters, you can do something like this:
        sub f { my ($x,$y,@z) = @_; print "$x\n$y\n@z\n"; } f("first","second","third","fourth");
        Prints:
        first second third fourth
Re: Using Shift with Parameters in a subroutine
by DanBev (Scribe) on Oct 20, 2014 at 09:18 UTC
    Hi Ron, if I have correctly understood your request, you can get multiple parameters in this way
    sub foo { my ($query_ref, $session_ref) = @_; }
    . Generally speaking, if you have 10 parameters, and you call mysub(1,2,3,4,5,6,7..10), you can take the parameters in various ways
    sub mysub { my a = shift; #1 my $b = shift; #2 ... and so on
    sub mysub { my @p = @_; #the array 1,2,3....10 ...
    and in your case
    sub mysub { my ($a, $b, $c, ....$j) = @_; #$a = 1, $b = 2, ... $j = 10
    I hope you help.
      thanks DanBev. !
Re: Using Shift with Parameters in a subroutine
by Corion (Patriarch) on Oct 20, 2014 at 09:15 UTC

    The new signatures are documented in perlsub since 5.20, under Signatures.

    The same, or a rather similar functionality can be enabled employing a source filter, but I think that the failure modes of a source-filter (introducing hard to debug syntax errors) are not really worth the nicer syntax for older Perl versions.

      FYI: Method::Signatures (and a few others) don't use source filters. Instead they use the keyword API introduced in Perl 5.14.

Re: Using Shift with Parameters in a subroutine
by Jim (Curate) on Oct 20, 2014 at 17:12 UTC
    It would only seem rational that we have a way to [condense argument passing], especially if i am passing 10 references to my subroutine. … I see there is no formal parameter support, at least in perl 5.10.1 (which is my customer version).

    The Perl Best Practice titled "Named Arguments" applies in the case you describe:  "Use a hash of named arguments for any subroutine that has more than three parameters."

    This Best Practice is in Chapter 9, Subroutines, which is available online here:  http://oreilly.com/catalog/perlbp/chapter/ch09.pdf. "Named Arguments" is on pp. 182-3.

      thanks Jim. that's definitely a case in point then.
Re: Using Shift with Parameters in a subroutine
by Anonymous Monk on Oct 20, 2014 at 09:17 UTC
Re: Using Shift with Parameters in a subroutine
by Marshall (Canon) on Oct 21, 2014 at 20:42 UTC
    It appears to me that you are only passing 2 refs to the sub.
    sub foo{ my ($query_ref, $session_ref) = @_; }
    These refs should be references to hash tables.

    In general, passing 10 parameters to a sub is not a good idea. Better is reference to a hash.

      Just to be thorough,

      my ($one, $two) = _@;

      works once, but ignores the side effect of shift (removing the arguments from the @_ array).

      As Marshall says, using a reference to an array would be the best way to deal with a lot of parameters, but if you wanted to, say, process two at a time, you could do

      while (my ($one,$two) = splice @_, 0, 2) { #do something }
      (you'll get an uninitialized value warning if you supply an odd number of parameters)
        thanks. i didnt realise this earlier, about the impact on @_ .
        Do not wait to strike when the iron is hot! Make it hot by striking - WB Yeats
      ok. ill improvise then. thanks. the 10 parameters was purely hypothetical , as a programming possibility. thats all.