sub do_nothing { ... # return 'nothing' return (); }
This style grew out of a discussion I had with rob_au about when you need to use the
operator (typically very rarely).wantarray ? : ;
However, I started to question whether this was really expressing what I wanted to do in most cases.
I wrote this code to test different ways of returning 'nothing' or failure, in list and scalar contexts.
and the output from this is#!/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; }
So what do we learn from these ?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 = [];
Well, you _probably_ rarely want to do this
given the fact that in list context this results in a list with one element, whose value is undefined.sub routine { return undef; }
Also, when operating in list context, you probably want to rarely do this
as this also results in a one element list, whose value is zero, rather than 'nothing'.sub routine { return 0; }
So the functions empty() and nothing() seem correct in most contexts, so which should you use ?
I then benchmarked the code thus
with these results#!/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) @ 11 +363636.36/s (n=10000000) empty: 2 wallclock secs ( 1.09 usr + 0.00 sys = 1.09 CPU) @ 91 +74311.93/s (n=10000000) nothing: 0 wallclock secs ( 0.50 usr + 0.00 sys = 0.50 CPU) @ 20 +000000.00/s (n=10000000) zero: 0 wallclock secs ( 0.06 usr + 0.00 sys = 0.06 CPU) @ 16 +6666666.67/s (n=10000000)
(Love that negative sys time - is Chronos gonna come for me to get it back ??)
So if we take the fact that _most_ of the time only empty() or nothing() will perform correctly, we can see that return; is more than twice as fast as return ();. This can probably be attributed to the construction, copy and destruction of the list in empty().
So if return; is just as right as return(); , but twice as fast, when would you use return(); ?
I feel that if your subroutine is constructing and returning a list, then return(); would result in more readable code e.g.
This to me _reads_ clearly that an empty list is being returned by a function that operates on lists.sub build_list { my ($arg1, arg2, ...) = @_; my @list = (); ... if (something) { return @list; } else { return (); } }
Correspondingly, return; would be better in functions that deal with scalars, as we dont have to worry about the context.
+++++++++++++++++
#!/usr/bin/perl
use warnings;use strict;use brain;
Edited by Chady -- added readmore tag.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Functions that return nothing, nada, failure...
by tkil (Monk) on Jun 01, 2004 at 07:30 UTC | |
by adrianh (Chancellor) on Jun 01, 2004 at 10:54 UTC | |
by tkil (Monk) on Jun 01, 2004 at 20:03 UTC | |
|
Re: Functions that return nothing, nada, failure...
by graff (Chancellor) on Jun 01, 2004 at 05:01 UTC | |
|
Re: Functions that return nothing, nada, failure...
by hossman (Prior) on Jun 01, 2004 at 06:24 UTC | |
|
Re: Functions that return nothing, nada, failure...
by adrianh (Chancellor) on Jun 01, 2004 at 09:51 UTC | |
|
Re: Functions that return nothing, nada, failure... (depends)
by tye (Sage) on Jun 01, 2004 at 15:17 UTC | |
by leriksen (Curate) on Jun 02, 2004 at 01:28 UTC |