Beefy Boxes and Bandwidth Generously Provided by pair Networks Cowboy Neal with Hat
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Hope a subroutine will return undef by default

by LanX (Abbot)
on Feb 11, 2014 at 16:34 UTC ( #1074440=note: print w/ replies, xml ) Need Help??


in reply to Hope a subroutine will return undef by default

there is no pragma to do this.

A naked return; at the end is IMHO the best workaround.

Of course one could write a module which parses all functions during one of the BEGIN-phases and wraps a naked return if there is no return as final statement, but this is IMHO far too vulnerable for production. ( update and wouldn't work for dynamically defined functions)

Cheers Rolf

( addicted to the Perl Programming Language)

) It returns an empty list in list context which produces an undef in scalar context.


Comment on Re: Hope a subroutine will return undef by default
Select or Download Code
Re^2: Hope a subroutine will return undef by default
by tobyink (Abbot) on Feb 11, 2014 at 22:59 UTC

    I'm starting to prefer simply adding this to the end of the sub:

    ();
    use Moops; class Cow :rw { has name => (default => 'Ermintrude') }; say Cow->new->name
      well if you really wanna "golf" better skip the semicolon and safe 33% of the necessary characters. ;-)

      Cheers Rolf

      ( addicted to the Perl Programming Language)

        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
        It is a good habit to omit semicolons after the commands that should not be followed by anything else (return, next, etc.). If you accidentaly add something, you get an error.
        لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2014-04-17 07:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (440 votes), past polls