sub do_nothing {
...
# return 'nothing'
return ();
}
####
wantarray ? : ;
####
#!/usr/bin/perl -w
use strict;
use Benchmark;
use Data::Dumper;
my $scalar;
my @list;
my %funcs = ( nothing => \¬hing,
_undef => \&_undef,
empty => \&empty,
zero => \&zero,
);
foreach my $func (keys %funcs) {
no strict 'refs';
my $scalar = &{$funcs{$func}}();
print STDERR "scalar context $func == ", Dumper($scalar), "\n";
my @list = &{$funcs{$func}}();
print STDERR "list context $func == ", Dumper(\@list), "\n";
}
sub nothing {
return ;
}
sub _undef {
return undef;
}
sub empty {
return ();
}
sub zero {
return 0;
}
####
scalar context _undef == $VAR1 = undef;
list context _undef == $VAR1 = [
undef
];
scalar context zero == $VAR1 = 0;
list context zero == $VAR1 = [
0
];
scalar context empty == $VAR1 = undef;
list context empty == $VAR1 = [];
scalar context nothing == $VAR1 = undef;
list context nothing == $VAR1 = [];
####
sub routine {
return undef;
}
####
sub routine {
return 0;
}
####
#!/usr/bin/perl -w
use strict;
use Benchmark;
use Data::Dumper;
my $scalar;
my @list;
my %funcs = ( nothing => \¬hing,
_undef => \&_undef,
empty => \&empty,
zero => \&zero,
);
timethese(10000000, \%funcs);
sub nothing {
return ;
}
sub _undef {
return undef;
}
sub empty {
return ();
}
sub zero {
return 0;
}
####
Benchmark: timing 10000000 iterations of _undef, empty, nothing, zero...
_undef: 2 wallclock secs ( 0.89 usr + -0.01 sys = 0.88 CPU) @ 11363636.36/s (n=10000000)
empty: 2 wallclock secs ( 1.09 usr + 0.00 sys = 1.09 CPU) @ 9174311.93/s (n=10000000)
nothing: 0 wallclock secs ( 0.50 usr + 0.00 sys = 0.50 CPU) @ 20000000.00/s (n=10000000)
zero: 0 wallclock secs ( 0.06 usr + 0.00 sys = 0.06 CPU) @ 166666666.67/s (n=10000000)
####
sub build_list {
my ($arg1, arg2, ...) = @_;
my @list = ();
...
if (something) {
return @list;
} else {
return ();
}
}