Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Re: Hash (not) returned by subroutine

by tobyink (Canon)
on Jan 27, 2020 at 12:06 UTC ( #11111925=note: print w/replies, xml ) Need Help??

in reply to Hash (not) returned by subroutine

Further to soonix's answer, if you wish to not return anything from a function, use:

sub get_data { ...; return; # undef in scalar context or the empty list in list conte +xt }


sub get_data { ...; return undef; # undef in scalar context and a one-item list conta +ining undef in list context }

Usually the former is preferred.

For object-oriented modules, if you don't have anything useful to return for a method, return $self is a good idea because it allows method calls to be easily chained, like:

sub enable_warnings { my $self = shift; $self->{warnings} = 1; return $self; } sub enable_errors { my $self = shift; $self->{errors} = 1; return $self; } sub disable_warnings { my $self = shift; $self->{warnings} = 0; return $self; } sub disable_errors { my $self = shift; $self->{errors} = 0; return $self; } sub run_process { my $self = shift; ...; return $result; } # Now instead of doing this: $widget->enable_warnings; $widget->enable_errors; my $result = $widget->run_process; # We can do this: my $result = $widget->enable_warnings->enable_errors->run_process;

A final note. Doing this:

sub bleh { return $result; }

Is actually very slightly slower than:

sub bleh { $result; }

So if you have a small function that gets called a lot and you want to optimize, removing return and just allowing the last value evaluated to fall through as the return value may give you a slight boost.

Replies are listed 'Best First'.
Re^2: Hash (not) returned by subroutine
by choroba (Archbishop) on Jan 27, 2020 at 16:15 UTC
    > may give you a slight boost

    Note that perl5200delta says:

    In certain situations, when return is the last statement in a subroutine's main scope, it will be optimized out.

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
      "...certain situations...return...optimized out"

      Doesn’t such a method make the behavior of a programming language unpredictable? Regards, Karl

      «The Crux of the Biscuit is the Apostrophe»

      perl -MCrypt::CBC -E 'say Crypt::CBC->new(-key=>'kgb',-cipher=>"Blowfish")->decrypt_hex($ENV{KARL});'Help

        Doesn’t such a method make the behavior of a programming language unpredictable?

        Not if perl is sure that the situation allows optimizing out.

        I would blindly assume that all returns could be optimized out in this script:

        sub foo { # do something return; } sub bar { # do something return 'a scalar'; } sub baz { # do something return qw( a list ); } foo(); bar(); baz(); exit();


        Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://11111925]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (7)
As of 2020-03-30 19:14 GMT
Find Nodes?
    Voting Booth?
    To "Disagree to disagree" means to:

    Results (176 votes). Check out past polls.