Why I Left Perl
leancrew.com
leancrew.com
On a side note, the very things that the author complains about are the same things that have been motivating me to learn Perl recently. My previous experience with it had been exposure to badly-written sysadmin scripts and clever one-liners. However, the stuff I've seen recently (particularly chromatic's "Modern Perl" book [1] and the Enlightened Perl Organisation [2]) has shown me that it's possible to write Perl that's downright elegant, without resorting to the ugly mess that many people think of when they hear "Perl script".
As an outsider, I don't have a niche that perl fits, that something else with a clearer purpose doesn't already cover. I appreciate it for it's contributions to language development, but that's about it.
Ruby and Python are both good for sysadmin scripting, web frameworks, and big apps. So is Perl.
The fact that use strict exists is hardly a flaw. It's a useful tool, and one that many of us use most of the time. The fact that is doesn't exist for Ruby or Python seems like the flaw.
But if Python did add it, I wouldn't be surprised to see it default to off for a long time because of backwards compatibility, just like Perl.
People seem to hate typing "my" to declare a variable, but that's fine. I appreciate the number of times that's caught bugs for me, and I appreciate the clarity of scoping it allows for. I never really figured out scoping in Python, because you never declare variables. In Python, you could write:
def foo():
def reset():
x = 0
def inc():
x += 1
def print():
print x
And it would compile, but not be what you mean. In Perl, you'd write: sub foo {
my $reset = sub { my $x; $x = 0 }
my $inc = sub { my $x; $x++ }
...
}
and know exactly what's going on because you had to explicitly declare $x. You can instantly tell that the Perl is broken. But the Python looks like it works (but doesn't). That, to me, is a big deal.Of course, it doesn't actually matter because you're not supposed to program like that in Python. But if you chose to anyway, ...
I also remember the day that I made a trivial error that strict would have instantly caught, and took out Bloomberg's ftp server as a result. That was also the day that I was converted.
It is important to know what strict does and doesn't do. It is not a panacea. But that said, it is a very good default to have on. But most days it catches problems for me, and they are usually real bugs. Keeping it quiet involves very little work. And my experience seems typical.
I believe him when he says that he has lots of working scripts that don't use it. However I also believe that a habit of using it would improve his productivity. And based on past experience, I'd be willing to bet that if he put strict on those scripts, he'd find and fix at least one real bug. (And probably a lot more.)
Given how useful strict is, why shouldn't it be a default "best practice" that gets recommended? This is not nagging. This is sharing useful information that makes people's life better. I've had people thank me for it.
But yeah - I wrote that only as a devil's advocate :)
The Perl community has pretty clearly collectively decided that "use strict" is the thing to do. I firmly believe the only reason it isn't the default behavior of Perl is because the core community holds backwards compatibility in such high regard.
And to be honest, people constantly pointing out the same advice over and over gets old. An occasional, "Hey, did you know..." with some new advice is cool, but people have been beating a dead horse with "use strict" and "use warnings". If somebody isn't using strict, it's not because they don't know about it.
In Perl if I have a typo in my variable names or function calls, it gets flagged by strict vars or subs respectively.
As for people not using strict, someone who is not using strict at this point in Perl either has very good reason (eg Damian Conway) or is sabotaging themselves.
Compare what happens when running:
perl -e "print \"\$bob = $bob\";"
perl -e "use strict;print \"\$bob = $bob\";"
python -c "print('bob = ', bob)"
Python throws an exception and warns about the undeclared variable automatically. It has the same behavior as "use strict", and I didn't have to do anything to get it.Perl makes the special case that's bad 99.999% of the time (you've basically said as much yourself) the default. At the very least, reverse the situation, make "use strict" implied, and add a "use unsafe" or something.
You had to use -c; how often do you see Python programmers do so?
With that said, strict mode should be the default for Perl 5 programs. Having to know the magic incantation to ask the compiler to help you is a barrier to novices.
How many Perl programs launch with -e?
-c cmd : program passed in as string (terminates option list)
Edit: fixed formatting.The default behavior that you demonstrated from Python is a run-time exception. Until you've gone through every code path, you have no idea whether you're going to trigger that exception. (Even 100% test suite coverage is not enough to guarantee that you won't blow up.) In even a moderate sized program, this can take a good while.
By contrast Perl's behavior with strict is to blow up at compile time, before I begin running the program. This significantly shortens the edit/debug cycle for my most common mistakes.
If you want the equivalent in Python, you have to use external tools like PyChecker.
As for the defaults, there is nothing as hard to change as a misfeature. Everyone agrees that the default would be better if it was reversed, but there is no way to do so without losing backwards compatibility. And given how much critical infrastructure runs on Perl, backwards compatibility matters a lot.
I'm firmly of the opinion that telling everybody (especially inexperienced Perl programmers) "you've got to use strict and warnings" is the right approach - with the unstated acknowledgement that if you know what you're doing and are prepared to accept the risks, you'll occasionally choose to ignore that advice.
It's a couple thousand word memoir, about 1/2 comprised of reasons he's not using to "leave Perl," before telling us that it's Perl people who bug him. He needs to get together with that "no semicolons in Javascript" guy in another HN post.
And actually it is not only blog posts - journalists often do the same thing, but maybe they don't so often write about their own lives.
Ironically, the user that banned me runs a blog whose most recent blog post was regarding "Exit statuses and how $? works". So I guess my question wasn't so stupid after all.
I have heard that #perl on OFTC is slightly more sane and helpful, but can't attest for that personally.
Edit: grammar correction.
Work doesn't happen in a vacuum, and when you're dealing with other people, some kind of standard is necessary, even if it does seem a little annoying at times.
But you are going to find people in the Python world telling you to abide by PEP8. And saying that you should be using virtualenv. And saying that you should not use (module-level) globals. And saying that you should be using various checking tools. And the checking tools may say annoying things (some of which will be good points about your code).
And I'm kind of one of those people, as well; not saying that there aren't reasons to break a rule, or that it instantly makes your code crap if you do, but I understand these arguments.
And a vocal number of those on Python IRC channels tend to be dicks to newbies or people with unusual ideas.
So I guess I would question whether Python is going to give you what you want. I can't speak much to Ruby's culture.
Use the tools you like which make your life better, and if it makes your life better then ignore other people's unsolicited opinions on the internet and just do your thing.
So the typical question is "I'm trying to do something a bit odd, can I?" vs "Help, that unlabeled lever I bumped into just blew my foot off."
As opposed to Perl? Are you trolling or can you list a few insane things in e.g. the Perl Best Practices book?
Edit: page 470 of Perl Best Practices (also pg 429 and 431) -- recommends using strict and warnings. It is hardly controversial or insane, it catches bugs.
I'm not making the point it's impossible to write good Perl, because I know it's possible, I'm saying it's easy to write bad Perl - much easier than in most other languages I've seen.
There are advantages and disadvantages with everything. Some of Perl's advantages today are CPAN, Moose and quite extensible syntax.
This is a good example of those three advantages:
http://search.cpan.org/~flora/MooseX-Declare-0.35/lib/MooseX...
(Note the optional typing of method parameters and attributes.)
I have to add "my" to a variable to get reasonably-sane scoping, the default is that everything's global.
Perl's defaults tend more to the insane side of the spectrum, especially in comparison with Python (who's entire design philosophy is a reaction to Perl). Not that there's anything wrong with that, and if you've done mostly perl I'm sure it seems natural, but compared to conventions across every other mainstream language it seems crazy.
To be precise if strict is not enabled then any variable declared without a "my" becomes a package variable.
With use strict enabled then any variable declared without my, our or state will give a compilation error.
> Perl's defaults tend more to the insane side of the spectrum, especially in comparison with Python
Actually Perl with use strict is the sanest IMHO. Because this means that Perl avoids the sloppy (but serious) errors that can easily creep into code in languages that don't have explicit scoping / variable declaration: http://news.ycombinator.com/item?id=3087990 | http://news.ycombinator.com/item?id=3587659
> This was stupid advice, but it was common. Perl’s defaults were as much a part of the language as regular expressions and were just as useful. I’d written dozens of scripts that relied, for example, on variables springing to life when first referenced, and they’d all been working perfectly for years. It’s not hackwork to take advantage of a language’s documented features.
The point here is not it will break the script but maintaining the script becomes a nightmare. that's why it's a bad practice. Clarity should always be favored over magic in programming.
Compilers are good enough for type systems to be advantages, now.