Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re^3: Hope a subroutine will return undef by default

by LanX (Chancellor)
on Feb 11, 2014 at 23:04 UTC ( #1074533=note: print w/replies, xml ) Need Help??


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

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)

  • Comment on Re^3: Hope a subroutine will return undef by default

Replies are listed 'Best First'.
Re^4: Hope a subroutine will return undef by default
by choroba (Chancellor) on Feb 11, 2014 at 23:56 UTC
    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.
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re^4: Hope a subroutine will return undef by default
by tobyink (Abbot) on Feb 11, 2014 at 23:47 UTC

    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
      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)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1074533]
help
Chatterbox?
[Corion]: Yay. Traditional finance situation averted. Bonds can be quoted in amounts (1_000_000 EUR) or per unit (1 unit). And a traditional error is to trade 2_000_000 piece when you meant to trade 2_000_000 EUR.
[Corion]: (one of my scripts simply catches high amounts and I phone people making that trade, ideally before the payment is due)
[Corion]: The sad thing is that my script sits at the end of the pipeline and can only look at the payments due today or tomorrow basically, while there are many more systems further up in the pipeline
[GotToBTru]: better late than never, I guess
[Corion]: GotToBTru: Sure - there is a long and sad story of many frantic cleanups that led us to implement this notification ;)

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (10)
As of 2017-03-29 11:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Should Pluto Get Its Planethood Back?



    Results (347 votes). Check out past polls.