I'm looking to make a subroutine mysub
which should behave such that the following two calls are effectively the same.
mysub(["values", "in", "a", "list"]);
mysub("Passing", "scalar", "values");
What is the proper syntax to make this happen?
I'm looking to make a subroutine mysub
which should behave such that the following two calls are effectively the same.
mysub(["values", "in", "a", "list"]);
mysub("Passing", "scalar", "values");
What is the proper syntax to make this happen?
Check if @_
contains a single array reference.
sub mysub {
if ( @_ == 1 && ref( $_[0] ) eq 'ARRAY' ) {
# Single array ref
} else {
# A list
}
}
The if
clause checks that only one argument was passed and that the argument is an array reference using ref
. To make sure that the cases are the same:
sub mysub {
if ( @_ == 1 && ref( $_[0] ) eq 'ARRAY' ) {
@_ = @{ $_[0] };
}
# Rest of the code
}