Perl: use vs require



  • Trying to optimise our application I found we were loading modules that have a lot of overhead that weren't generally required.

    (this is typed from memory)

    if ($some_condition) {
      if ($some_other_condition) {
        require Some::Module;
        my $tmp = Some::Module::something();
        # do something with $tmp
      } elsif ($something_else) {
        my $tmp = Some::Module::something();
        # do something else with $tmp
      }
    }
    

    See what happened there? Normally when the first conditon is true so it the second - but not always. This developer changed a use to a require without understanding the difference. Error was Undefined subroutine &Some::Module::something.

    Hint: use Some::Module; is basically BEGIN{require Some::Module} Some::Module::import(); so using use in a conditional will always require it, but require in a conditional means it may not be loaded.

    This change has saved a few tens of milliseconds in the !$some_conditon case, which over millions of page views is worth it.



  • To be fair, when you learn Perl, the docs tend to drill into your head to use use... how often do you see docs even mention require?

    Disclaimer: Last time I wrote any Perl was in 2007 and I learned Perl in the mid to late 90s.


  • mod

    Go back and forth from Perl to PhP regularly. You'll end up with require everywhere.



  • The only time(s) I can remember using require rather than use was when I needed to tinker with the search path in order to find a module (probably because I didn't have permission to install it in a standard location). Then I learned you can do that in a BEGIN { @INC = ... }, so you can still use use.



  • @powerlord said:

    Perl, the docs

    There are a number of performance sins in those docs. At least they aren't dangerous like php is/was!



  • @HardwareGeek said:

    The only time(s) I can remember using require rather than use was when I needed to tinker with the search path in order to find a module (probably because I didn't have permission to install it in a standard location). Then I learned you can do that in a BEGIN { @INC = ... }, so you can still use use.

    ... or use lib @LIST.

    That helps you get rid of the ugly BEGIN block and adds some small improvements over manipulating @INC directly (platform independent directory separators, warnings about questionable paths, ...).



  • @OffByOne said:

    use lib @LIST.

    Thanks. I kinda had the feeling I was forgetting an easier way to do it when I wrote that yesterday, but couldn't remember it, and didn't bother searching for it. Obviously, it's not something I need very often.


  • mod

    @Zemm said:

    elsif

    I'm not familiar with this keyword. :P


  • Discourse touched me in a no-no place

    @abarker said:

    I'm not familiar with this keyword. :P

    Because writing out else if in full would be too much typing.



  • @dkf said:

    Because writing out else if in full would be too much typing.

    Because in Perl that would have to be else { if with an extra } to match at the end.



  • Go requires braces around the bodies of if/else but still manages to allow else if without throwing a hissy fit.



  • local i
    
    -- ....
    
    
    if math.fmod(i, 15) == 0 then
      print("FizzBuzz")
    elseif math.fmod(i, 3) == 0 then
      print("Fizz")
    elseif math.fmod(i, 5) == 0 then
      print("Buzz")
    else
      print(i)
    end
    

    @PJH can we get lua added to synatax highlight list? :^)


  • Discourse touched me in a no-no place

    @riking said:

    @PJH can we get lua added to synatax highlight list?

    Done.



  • Perl elsif is still more verbose than bash elif! And more concise than php elseif.


  • mod

    Hey, you've successfully pointed out a way that PHP doesn't suck in less than one line of text! Have a :cookie:.


  • Winner of the 2016 Presidential Election

    Hey, all that demonstrated is that PHP sucks less than bash, not that PHP doesn't suck!

    Besides, this is perfectly cromulent:

    if ($variable)
    {
    
    }
    else if ($variable2)
    {
    
    }
    

    Now do you think PHP doesn't suck?


  • mod

    @Arantor said:

    Hey, all that demonstrated is that PHP sucks less than bash, not that PHP doesn't suck!

    Did I say that PHP doesn't suck as a whole?

    @abarker said:

    a way that PHP doesn't suck

    Nope, I said that @Zemm pointed out a single, non-sucky thing about PHP. That says nothing about the suckiness of PHP as a whole.

    @Arantor said:

    Besides, this is perfectly cromulent:

    if ($variable)
    {
    
    }
    else if ($variable2)
    {
    
    }
    ```</blockquote>
    
    That's valid in C# as well. I'd wager most C languages permit it. Does bad coding style make a language bad now? If so, then there isn't a single good language out there.
    
    *Edit: And new bug …*

  • Winner of the 2016 Presidential Election

    This is PHP, it sucks in every respect!


  • mod

    @Arantor said:

    This is PHP, it sucks in every respect!

    Sounds like anti-Stockholm Syndrome. :P


  • mod

    @abarker said:

    anti-Stockholm Syndrome.

    Lima syndrome? :trolleybus:



  • @abarker said:

    Did I say that PHP doesn't suck as a whole?
    You said that Zemm "successfully pointed out a way that PHP doesn't suck in less than one line of text." This states by implication that PHP does not suck. You may have intended to say that he pointed out something about PHP that doesn't suck, in which case the lack of suckiness would attach to the something instead of to PHP, but, phrased your way, the lack of suckiness attaches to PHP. And that's just plain wrong.

    🎐



  • @Yamikuronue said:

    Lima syndrome?

    I am really disappointed, but not surprised after thinking for a minute, that Stockholm's antipode is not only not Lima but that it is basically out in the middle of the Pacific/Southern ocean:



  • @TwelveBaud said:

    You may have intended to say that he pointed out something about PHP that doesn't suck

    Since he said:

    @TwelveBaud said:

    successfully pointed out a way that PHP doesn't suck

    ...then I think he did what you said. "A way" seems like pointing out something in particular.



  • @TwelveBaud said:

    You said that Zemm "successfully pointed out a way that PHP doesn't suck in less than one line of text." This states by implication that PHP does not suck.

    I disagree. I think that indicates there are ways in which PHP does not suck, hence that PHP does not suck completely. I do not think that indicates PHP does not suck at all.


  • mod

    @TwelveBaud said:

    You said that Zemm "successfully pointed out a way that PHP doesn't suck in less than one line of text." This states by implication that PHP does not suck. You may have intended to say that he pointed out something about PHP that doesn't suck, in which case the lack of suckiness would attach to the something instead of to PHP, but, phrased your way, the lack of suckiness attaches to PHP. And that's just plain wrong.

    Your emphasis missed two key words.

    successfully pointed out a way that PHP doesn't suck in less than one line of text.

    Those two words make your analysis crumble to dust.



  • @abarker said:

    Those two words make your analysis crumble to dust.
    Hardly. You imply that PHP doesn't suck, you imply that there's at least one way that supports that, and you express that the way Zecc mentioned is that way. Do I need to get out my 2nd-grade English textbook and start diagramming your sentence for you?


  • mod

    @TwelveBaud said:

    Hardly. You imply that PHP doesn't suck, you imply that there's at least one way that supports that, and you express that the way Zecc mentioned is that way. Do I need to get out my 2nd-grade English textbook and start diagramming your sentence for you?

    Belgium it! Don't try to tell me my intent!

    I implied nothing about the quality of PHP. You inferred additional meaning from my sentence, but I did not imply that PHP doesn't suck.



  • Guys, this is hilarious!

    FTR I think php's elseif sucks more than Perl's elsif because going between them makes additional "remember the differences" - perl was first so it gets the de facto standard.


  • Winner of the 2016 Presidential Election

    @PleegWat said:

    I disagree. I think that indicates there are ways in which PHP does not suck, hence that PHP does not suck completely. I do not think that indicates PHP does not suck at all.

    Lies.


  • Winner of the 2016 Presidential Election

    I read it as though that is what you were implying... but I can see where you're coming from too.

    So to clarify: PHP still sucks donkey balls but in one respect it sucks marginally less than another language that also sucks?


  • sockdevs

    @Zemm said:

    Perl elsif is still more verbose than bash elif! And more concise than php elseif.

    Shall I create a language who's name begins with p, and have the even more terse ef as a keyword? It's shorter that elseif, elsif, and elif! :smiling_imp:

    INB4 the Bad/Evil Ideas thread is :arrows:


    Actually, scrap the 'begins with p'; I'll just call it P :laughing:


  • mod

    @Arantor said:

    PHP still sucks donkey balls but in one respect it sucks marginally less than another language that also sucks?

    Indeed.


  • Winner of the 2016 Presidential Election

    It's also a chronic enabler of WTF - but they are actually trying to fix it.

    For example, in PHP 7 it will be possible to write code that can be verified for type safety by automated tools since it will be possible to dictate types of function/method args and return types, and enforce them. EVEN FOR SCALAR TYPES, something that has been missing in PHP for years. And they're even adding a strict mode to this so it actually gets enforced.


  • Winner of the 2016 Presidential Election

    @Arantor said:

    So to clarify: PHP still sucks donkey balls but in one respect it sucks marginally less than another language that also sucks?

    Yes. Like using . for concatenation instead of + like JavaScript does, which leads to all kinds of duck typing fun.

    @Arantor said:

    EVEN FOR SCALAR TYPES, something that has been missing in PHP for years.

    Thank fuck for that. Finally. See above for one of the prime reasons.



  • @RaceProUK said:

    Shall I create a language who's name begins with p, and have the even more terse ef as a keyword?

    [...]

    Actually, scrap the 'begins with p'; I'll just call it P :laughing:

    I'm eagerly awaiting P++ :stuck_out_tongue:



  • @RaceProUK said:

    Shall I create a language who's name begins with p, and have the even more terse ef as a keyword? It's shorter that elseif, elsif, and elif! :smiling_imp:

    Ef that!




  • Winner of the 2016 Presidential Election

    maybe ($condition) {
        // Code here
    } doyoumind {
        // Code here
    } otherwise {
        // Code here
    }
    

    Filed under: Inspired by this


Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.