Re: Using Shift with Parameters in a subroutine
by Anonymous Monk on Oct 20, 2014 at 09:11 UTC
|
my( $first, $second, $last ) = @_;
| [reply] [d/l] |
|
|
| [reply] |
|
|
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
| [reply] [d/l] [select] |
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.
| [reply] [d/l] [select] |
|
|
| [reply] |
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.
| [reply] |
|
|
| [reply] |
|
|
| [reply] |
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.
| [reply] |
|
|
thanks Jim. that's definitely a case in point then.
| [reply] |
Re: Using Shift with Parameters in a subroutine
by Anonymous Monk on Oct 20, 2014 at 09:17 UTC
|
| [reply] |
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.
| [reply] [d/l] |
|
|
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) | [reply] [d/l] [select] |
|
|
| [reply] |
|
|
ok. ill improvise then. thanks.
the 10 parameters was purely hypothetical , as a programming possibility. thats all.
| [reply] |