Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Does eval cause that much of a performance hit?

by perrin (Chancellor)
on May 06, 2009 at 05:24 UTC ( #762167=note: print w/replies, xml ) Need Help??


in reply to Does eval cause that much of a performance hit?

they tend to be older guys and prefer things like a three line if construct than a trailing if

Listen to your elders! They're right. And don't let me catch you writing this:

my $foo = $bar if $baz;

Replies are listed 'Best First'.
Re^2: Does eval cause that much of a performance hit?
by BrowserUk (Pope) on May 06, 2009 at 08:50 UTC

    What are the alternatives:

    my $foo; if( $baz ) { $foo = $bar; }

    Or

    my $foo; $foo = $bar if $baz;

    Of course, if you're setting $foo, then presumably your gonna use it at some point later. And unless your enamoured with testing every variable for undef prior to using it, then in most cases there is a sensible default that it can be initialised to. In which case:

    my $foo = 0; $foo = $bar if $baz;

    Of course, that can be done "long hand" also if that's you preference.

    And that's the point. Using this particular flaw in the Perl semantics as a justifiction for a style preference is disingenuous.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      What are the alternatives

      An alternative to your alternatives

      my $foo = defined($baz) ? $bar : undef;

      or, with a sensible default, as an explicit undef is pretty dumb,

      my $foo = defined($baz) ? $bar : 0;

      This saves at least a nextstate and an assignment, for no loss of readability.

      • another intruder with the mooring in the heart of the Perl

        I agree with you, except that we've now moved so far from the OPs point: prefer things like a three line if construct than a trailing if., and perrin's response that we're almost on a different subject. 'sides which, the same people that eshew the trailing if also tend to reject the ternary as too complex/confusing/unclear/short.

        as an explicit undef is pretty dumb,

        I'm not sure where you saw the explicit undef?


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.

      I was teasing. I use trailing if, although only in certain circumstances:

      • it fits on one line without any obfuscation and
      • it's an interrupt, like a next or return.

      But the flaw I pointed out with "my...if" is serious and scary and everyone should be aware of it.

        I apologise for missing the humour. And you're right about "my..if".

Re^2: Does eval cause that much of a performance hit?
by Anonymous Monk on May 06, 2009 at 08:06 UTC
    They're only right about that one case :) the rest is subjective

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (7)
As of 2019-12-06 18:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Strict and warnings: which comes first?



    Results (157 votes). Check out past polls.

    Notices?