http://www.perlmonks.org?node_id=1074545


in reply to Re^3: Hope a subroutine will return undef by default
in thread Hope a subroutine will return undef by default

My main concern is not one of brevity. The return keyword does two things: it establishes the return value of the function, and it also acts as flow-control, exiting the current function.

(); is a way to set the return value of a function to nothing without using a keyword that screams out "flow-control" to readers skimming the code.

Update: As an aside, in Kavorka there is a lot of Perl code re-writing that goes on. For example, writing something like this:

method foo ($x) { return $x + 1; }

Will actually be rewritten internally to something like:

sub foo { my $self = shift; my $x = @_>=1 ? shift(@_) : croak("Requires \$x"); return $x + 1; }

So far, so good. But what about this method:

method bar ($x) { }

We'd expect it to return nothing. But it's rewritten to something like:

sub bar { my $self = shift; my $x = @_>=1 ? shift(@_) : croak("Requires \$x"); }

Which would actually return $x! The solution? Kavorka inserts (); into the sub body after unpacking parameters from @_. It couldn't use return because that's flow-control, and would prevent the main body of the function from executing.

use Moops; class Cow :rw { has name => (default => 'Ermintrude') }; say Cow->new->name

Replies are listed 'Best First'.
Re^5: Hope a subroutine will return undef by default
by LanX (Saint) on Feb 11, 2014 at 23:58 UTC
    IMHO its still flow control (its an exit point), so no need to hide it.

    (); is fine for short lambdas, otherwise IMHO explicit is better than implicit.

    YMMV! :)

    Cheers Rolf

    ( addicted to the Perl Programming Language)