sub test_function {
my $ref = shift; # this function takes one argument, a ref to an ARRAY
# Note: you could do extra manual checking of function arguments.
# Not necessarily recommended: shown for educational purposes.
defined($ref) or die "oops: you did not pass any arguments";
@_ and die "oops: this function takes exactly one argument";
####
use strict;
use warnings;
use feature 'say';
sub test_function {
my $ref = shift; # this function takes one argument, a ref to an ARRAY
# Note: you could do extra manual checking of function arguments.
# Not necessarily recommended: shown for educational purposes.
defined($ref) or die "oops: you did not pass any arguments";
@_ and die "oops: this function takes exactly one argument";
}
say "Calling test_function with no arguments:";
eval {
test_function();
};
say $@;
say "Calling test_function with a single undefined argument:";
eval {
test_function(undef);
};
say $@;
##
##
X:\>perl foo.pl
Calling test_function with no arguments:
oops: you did not pass any arguments at foo.pl line 10.
Calling test_function with a single undefined argument:
oops: you did not pass any arguments at foo.pl line 10.
X:\>
##
##
use strict;
use warnings;
use feature 'say';
use Carp qw( croak );
sub test_function {
@_==1 or croak "oops: this function takes exactly one argument";
my $ref = shift; # this function takes one argument, a ref to an ARRAY
# Note: you could do extra manual checking of function arguments.
# Not necessarily recommended: shown for educational purposes.
defined($ref) or croak "oops: argument is not defined";
}
say "Calling test_function with no arguments:";
eval {
test_function();
};
say $@;
say "Calling test_function with an undefined argument:";
eval {
test_function(undef);
};
say $@;
##
##
X:\>perl foo.pl
Calling test_function with no arguments:
oops: this function takes exactly one argument at foo.pl line 7
main::test_function() called at foo.pl line 17
eval {...} called at foo.pl line 16
Calling test_function with an undefined argument:
oops: argument is not defined at foo.pl line 12
main::test_function(undef) called at foo.pl line 23
eval {...} called at foo.pl line 22
X:\>