43 points · by bigmac · 17y ago · 61 comments
I respectfully disagree. Reading perl requires understanding a significant amount of perl-specific trivia for any given block of code, that's why it is hard to read.
http://shootout.alioth.debian.org/ Binary trees problem.
Perl:
sub item_check {
my ($tree) = @_;
return $tree->[2] unless (defined $tree->[0]);
return $tree->[2] + item_check($tree->[0]) - item_check($tree->[1]);
}
Lua: local function ItemCheck(tree)
if tree == 3 then
return tree[1] + ItemCheck(tree[2]) - ItemCheck(tree[3])
else
return tree[1]
end
end
These demonstrate the REALITY of what it's like reading perl code vs. other languages. Count the number of different symbols you must know in order to understand the perl code, versus the number of symbols you must know to understand the lua code. Anyone familiar with functions, variables, and standard control flow will be able to read the lua with no problem. If you look at other code snippets, like Javascript and C#, you'd amend the comment to say that anyone familiar with methods and object-oriented programming will be able to read the code with no problem.With perl, you have to know perl. You have to know perl's control flow ("unless"), you have to know perl references(->), you have to know perl's parameter passing semantics and the meaning of @_, and you have to know perl's variable prefixes.
That's why perl is hard to read. That's why people who use perl all the time don't understand why it drives everyone else crazy. They have all that crap in their heads already. You never smell your own bad breath. Style guides don't fix this problem.
You have to know perl's control flow ("unless"
unless is that difficult to understand? Its plain english has totally understandable semantics without reading a lick of documentation about perl. you have to know perl references(->)
This is just like C++ and an arrow is pretty dang readable and plain to me. Perhaps even more informative than a period. you have to know perl's variable prefixes.
I might be convinced to give you this one but really reading the perl code isn't that difficult even with 3 extra symbols to know. Even not knowing the symbols the code example you show is both readable and concise.Yes perl may have some quirks but for the most part it "Does exactly what you think it would by reading it". That was a design goal of the language after all.
Of course anyone could write perl code that is incomprehensible and makes use of perls semantics to obfuscate their purpose (using every example you cite) but that is with effort on their part and significant effort in some cases. And just to drive the point home may I present lua code that is way harder to read than any perl code I've ever seen even in an obfuscated perl contest: http://www.corsix.org/content/obfuscated-lua
To reiterate, no one in their right mind would right lua code like that except as an exercize and experiment. Same thing goes for perl. The perl code you show above is perfectly readable. Unless does exactly what you expect it to. -> arrows mean exactly what you would expect them to. Arrays start at 0 exactly like most programmers expect them to. and on top of that its readable. The only point you bring up that I think supports you argument is the @_ and argument passing semantics. But again that's only one and as another poster pointed out Lua has at least one also in it's array indexing.
sub bottom_up_tree {
my($depth) = @_;
my @pool;
push @pool, [undef, undef, -$_] foreach 0..2**$depth-1;
foreach my $exponent (reverse(0..($depth-1))) {
push @pool, [reverse(splice(@pool, 0, 2)), $_]
foreach reverse(-(2**$exponent-1) .. 0);
}
return $pool[0];
}I've never seen code in any language doing that kind of things to an array -- without lots of comments.
Strange that you didn't publish the equivalent Lua code here...? So you aren't trolling? :-)
Edit: Removed reasoned argument. Waste on what must be a troll.
Edit: Also. Double asterisk and square braces goes straight back to ol' C. You are a troll.
Who cares? Why does that matter? Do you really hire or recruit people to work on your projects merely on the basis of reading code with no problem?
I care a lot more about being able to understand the problem domain and maintain the code than merely being able to read it by way of familiarity with an Algol-derived language with class-based OO support and dynamic typing.
> With perl, you have to know perl.
... just like every other programming language ever written. (Oops! Lua's array indices customarily start at one; good luck with those fencepost errors by people who can merely read the Lua code but don't know Lua.)
For example, my co-workers and I find it easy and intuitive to work with lists in Python
xs = [5, 6]
xs.append(7)
print xs[2] # prints 7
xs.append([]) # easy to append lists to other lists
xs[3].append(6) # easy to append to the inner list
some_func(12, "hello", xs, True) # easy to pass to a function
In Perl this would be way more complicated for reasons that are obvious to someone who knows Perl well, but a mystery to people who use Perl casually. $xs = [5, 6]; # must be a reference
push @$xs, 7 # deference $xs as a list
print "$xs->[2]\n" # still not too bad
push @$xs, [] # add a nested list reference
push @{$xs->[3]}, 6 # omgwtf?!
some_func(12, "hello", $xs, 1) # not too bad
Understanding "push @{$xs->[3]}, 6" is asking a lot just to deal with a nested list that you need to pass to a function, especially compared to the easily understandable "xs[3].append(6)". For that matter, it's asking a lot to make you understand the difference between a list and a list reference. It's asking a lot to ask to understand the concept of "evaluating in a ___ context", especially when other languages manage to do the same things without these concepts.Does it need to be more complicated, or are you perhaps trying to make the code look like Python? While there are good reasons for using references to pass lists to functions, it seems clearer (and more Perl-ish) to do the transformations on a list rather than a reference:
my @xs = (5, 6); # use a list directly
push @xs, 7;
print $xs[2];
push @xs, [];
push @{$xs[3]}, 6; # treat $xs[3] as array
some_func(12, "hello", \@xs, 1);
While one could probably argue that pushing to the inner array is clearer in Python, the rest seems just as clear. And if one was writing for an audience not comfortable with references, one could always use a temp variable for clarity: my @inner;
push @xs, \@inner;
push @inner, 8;
For that matter, you could do the same for your 'omgwtf' line: my $inner = $xs->[3];
push @$inner, 9;
While there's nothing wrong with the Python, they strike me as pretty comparable in clarity. push @$xs, 7;
with $xs->append(7);Maybe the first time you see it, for at most two minutes once in your entire life.
I kind of wonder if the people who freak out about this syntax stuff have ever studied a foreign language. Do they scream and run out of the room every time they hit an alien grammar construct?
All that is dwarfed by learning the language specific libraries, no matter which language you use.
And by the way, use strict/warnings, so you aren't bitten by a spelling error for variable names. :-)
Edit: BTW 2, how do you do one-liners in Python? :-)
No, but everyone we hire must first pass through that stage. To become a Perl expert one must first spend quite some time as a Perl beginner.
Surely being able to read the code makes it easy to maintain: the reason you might use perl is for existing libraries/support and the level of abstraction available. Language design has a direct impact in maintainability.
I've never written code intended for complete novices to maintain. I don't care if people who have never programmed in the language before can maintain code written in the language without learning the language. I think that's an effective way to ruin your code and your project and any long-term plans you have around the project.
I care deeply about maintainability and learnability, but I care not one whit if someone who's never programmed in Perl can look at real-world Perl code and suss out exactly what it does and why based on... guesswork? Intuition? Experience with languages that are not Perl?
Goladus might not be a troll, but according to his own statements he doesn't write enough Perl to really know it. He just sprouts lots of opinions on things he don't know.
Edit: A few words for clarity.
Edit I might also note that Goladus' second code example did a complex algorithm without comments in a way that could be classed obfuscated.
Ask the guy who made the slides debunking the "myth".
This is not about hiring or recruiting people. This is simply about language readability.
I have been using perl longer than any other major language except C, and I still have to review basic syntax crap every time I have to read perl code.
... just like every other programming language ever written.
Not true. Perl requires significantly more language-specific trivia than most other languages, and for marginal benefit. Differentiation between scalar, list, and hash is not especially useful most of the time. It doesn't significantly help readability (or writability) and makes the code more complicated.
A nonstandard default array index is one piece of trivia. In fact, it can be deduced from the example I gave, and if you aren't sure you can check. I highlighted several in that one perl example.
I have been using Perl longer than any other major langauge, and I never have to review "basic syntax crap". Not to make a personal attack, but perhaps the problem is not Perl.
>>I highlighted several in that one tiny perl example.
You can highlight lots of examples by choosing how to write the code. The Perl code could be very similar to the Lua.
But, yeah... the linguistic inspirations gives more syntactic ways to write things. That is not only bad, but (as TFA said), you better follow the "Best Practices" book.
All that is anyway dwarfed by the language specific libraries you use, as soon as you do something interesting.
Personally, I think the worst part of Perl is all the painful trolling you have to suffer on the discussion web sites...
>>A nonstandard default array index is one piece of trivia.
Hmm... that would in fact be really weird. I have lots of habitual loops over indexes etc which I'd have to change, lots of edge cases. It would be like driving a car where you turn the wheel left to turn right -- but only on Mondays.
I can guess that you mean the length, but if that's the case, why don't you write "length(tree) == 3"? If we are just going to guess to understand, then we have the same problem with Lua that we have with Perl. With Perl, you can guess that $array->[number] is an array lookup, that the $ in "$foo" is essentially meaningless, and so on.
In other words, Lua and Perl are both hard to read.
* http://search.cpan.org/dist/Devel-NYTProf/
* http://blog.timbunce.org/2008/07/15/nytprof-v2-a-major-advan...
For more testing stuff checkout the Perl5 wiki: http://www.perlfoundation.org/perl5/index.cgi?testing
I have no doubt that Perl can be written in a disciplined and reasonable manner, readable to people familiar with its idioms, and not especially bug-prone. I cringe every time I come across Perl code because I've never actually come across any real Perl that's written that way. Or maybe I have, but I've always gotten discouraged with the language before mastering enough of the syntax to master the idioms, so I can't really tell.
This is not a problem with Python. It's easy to learn to read Python, and I have yet to come across really bad Python in practice. Good coding seems to flow easily from the way the language is made.
Not enough.
Consider this, though: how much Perl code "in the wild" is the result of programming novices who learned just enough Perl to do a job without actually having to learn how to program or how to write Perl code well or how to write maintainable code?
I suspect that it's far more than you might think.
If that's true, then the complaint is that Perl is too easy to write.
Python and Ruby are not that different, and neither is that different from Perl.
If you want to learn about prototype-based OO systems, fine, but there's a reason that only JavaScript (and Self) use prototype-based OO :P
C should be learned so that you are not tempted to ever use it for anything important.
Disclaimer: I quite like JavaScript.
You may not end up using it all that frequently, but an hour or two seeing how another programming language does something is usually interesting perspective, imo.
Since Ruby/Python/Perl are really similar, I'd recommend that you learn something completely different instead.
Disclaimer: I like Perl. What makes Perl fun for me is to a large part that the linguistics influence results in Perl breaking most "normal" laws for programming language design, but still works. I go to work smiling.
That was a design decision, not influence from natural languages? Fun.