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 basicallyBEGIN{require Some::Module} Some::Module::import();
so usinguse
in a conditional will always require it, butrequire
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 mentionrequire
?Disclaimer: Last time I wrote any Perl was in 2007 and I learned Perl in the mid to late 90s.
-
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 thanuse
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 aBEGIN { @INC = ... }
, so you can still useuse
.
-
Perl, the docs
There are a number of performance sins in those docs. At least they aren't dangerous like php is/was!
-
The only time(s) I can remember using
require
rather thanuse
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 aBEGIN { @INC = ... }
, so you can still useuse
.... 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, ...).
-
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.
-
-
I'm not familiar with this keyword. :P
Because writing out
else if
in full would be too much typing.
-
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? :^)
-
-
Perl
elsif
is still more verbose than bashelif
! And more concise than phpelseif
.
-
Hey, you've successfully pointed out a way that PHP doesn't suck in less than one line of text! Have a .
-
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?
-
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?
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.
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 …*
-
This is PHP, it sucks in every respect!
-
-
-
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.🎐
-
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:
-
You may have intended to say that he pointed out something about PHP that doesn't suck
Since he 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.
-
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.
-
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.
-
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?
-
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'selsif
because going between them makes additional "remember the differences" - perl was first so it gets the de facto standard.
-
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.
-
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?
-
Perl
elsif
is still more verbose than bashelif
! And more concise than phpelseif
.
Shall I create a language who's name begins withp
, and have the even more terseef
as a keyword? It's shorter thatelseif
,elsif
, andelif
!INB4 the Bad/Evil Ideas thread is
Actually, scrap the 'begins with
p
'; I'll just call itP
-
PHP still sucks donkey balls but in one respect it sucks marginally less than another language that also sucks?
Indeed.
-
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.
-
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.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.
-
Shall I create a language who's name begins with
p
, and have the even more terseef
as a keyword?[...]
Actually, scrap the 'begins with
p
'; I'll just call itP
I'm eagerly awaiting P++
-
Shall I create a language who's name begins with
p
, and have the even more terseef
as a keyword? It's shorter thatelseif
,elsif
, andelif
!Ef that!
-
-
maybe ($condition) { // Code here } doyoumind { // Code here } otherwise { // Code here }
Filed under: Inspired by this