|
|
|
|
|
by dmytrish
3253 days ago
|
|
So, how exactly these different ways of getting sub arguments are useful? sub add1 {
my ($arg1, $arg2) = @_;
$arg1 + $arg2;
}
sub add2 {
my $arg1 = shift;
# possibly two screens of dense code here, then
my $arg2 = shift;
$arg1 + $arg2;
}
sub add3 {
$_[0] + $_[1]
}
# There may be some other ways to extract arguments
# that I am not aware of.
# It's possible to combine any of the methods!
And this is just argument access, one of the core language primitives. |
|
Because in the second (add2), you might do something interesting with the second (or third) arguments depending on the first. You might have optional arguments. Your arguments might be a list of items you don't know the size of. It allows you to develop what you need out of the extensible core mechanism. Modules can and have built on that.
> # There may be some other ways to extract arguments
There are. Now you can just do
My personal favorite is to use Function::Parameters[1] which allows named params, type constraints (runtime), default params, etc.1: https://metacpan.org/pod/Function::Parameters