While I contemplate on this new pale white design of use.perl.org, hoping it's either temporary or half of what it's meant to be, I wanted to share an argument I had yesterday at $work.
A co-worker presented me a problem:
34 sub scan_array { 35 my ( $item, $arrayref ) = @_; 36 37 # does the $item and $_ equal 38 if ( any { lc($item) eq lc($_) } @{$arrayref} ) || 39 # or does $item is part of $_ but not equal to it 40 ( any { $_ =~ /$item/i } @{$arrayref} ) { 41 return 1; 42 } 43 elsif { 44 return 1; 45 } 46 47 return 0; 48 } syntax error at tests_include.pl line 38, near ") ||" Global symbol "$arrayref" requires explicit package name at tests_include.pl line 40. syntax error at tests_include.pl line 40, near "} )
"why can't it find the arrayref ?!"
Can you spot the error? I have. The if()
is missing the encompassing brackets. What actually happened is that perl is thinking that the if()
statement ended with the first closing bracket, and then could not interpret the ||
after. Instead of running if (CONDITION || CONDITION)
, it ran IF (CONDITION) || CONDITION
and according to the syntax of Perl, this shouldn't work, and it didn't.
After explaining to my co-worker (who is indeed a talented programmer) the simple error, he claimed that Perl is stupid because this is the way it works. Now, I should note that I don't really care when people rag on Perl since they're plainly missing something very awesome. I merely explain and move on, albeit often I ignore when I sense a large concentration of ignorance or "showoffness". However, this annoyed me. Since I have a deep interest in optimal writing, programming patterns and coherent coding styles, I found this particularly annoying, as it is simply a matter of incorrect syntax usage, clearly not a language problem.
I stated simply that in almost every language, you have brackets around the if()
, and when he proclaimed that in Pascal (of all languages, Pascal? Really?) it is not like that, I simply reaffirmed that Pascal decided not to have that, good for Pascal. However, Pascal just uses the next then
to understand when the if
ends. That's all. Perl decided to be more pro-active about this and allow more readable code (readable? Perl? YES!). That's also why Perl has mandatory code brackets even on a single if()
. Perl however, does allow you to remove both the code brackets and if
condition brackets on postfix if()
s.
This argument (as many other stupid geek arguments) ensued and enrolled two other developers who both suggested that what I say makes the most sense but that even if you could write it without the condition brackets, it wouldn't be as understandable, and that's an additional minus on its own.
If you want to complain about a language, try to pick better things to complain about. I could give you half a dozen things I don't like about Perl (I think...) which are waaaaay better to complain about besides the fact that "it makes me write an additional pair of brackets around the if
, what the fuck?!" <-- exact quote OMG.
Obligatory CPAN: looks like that subroutine can be replaced with Tie::CPHash.
I would disagree
xsawyerx on 2009-06-24T15:11:29
The point is not that "Perl cannot do it", at least to me. The point is that if you want to pick a reason why "Perl sucks"(tm), there are far better reasons to exclaim that than the fact that it chose to have explicit parenthesis on
if
conditions.This is the same tone of things as "Perl sucks because if I have only one statement for an
if
block, I have to still put code blocks, and then I have a line with just one curly bracket!!!"[1] Excuse the exclamation marks (trying to make it seem realistic, did it work?), but I have really heard people say that...Actually, the same co-worker also complained that Perl has
lc
instead oflowercase
. The common thing about all these complaints, at least to me, is that they are nitpicking at something which wasn't (and still isn't) a mistake. It's the intent of the language. Perl can do that, it just decided not to, for the same reason Larry keeps explaining why he wants a code block even for a singleif
. It's not that Perl can't changelc
tolowercase
orto_lowercase_letter_instead_of_the_first_parameter_of_this_function_here
. It's because Perl (and perhaps I should say Larry) decided not to.I don't know if I explained myself well, sometimes I don't seem to.
[1]
if ( CONDITION ) {
... CODE ...
} # ADDITIONAL CURLY BRACKET OMFG!11