Tuesday, September 15, 2009

how to do pythonic perl: $ARG is not argh

Well, it had to happen sooner or later. I'm forced to maintain someone else's Perl program. I really don't like Perl. (And as I mentioned before, don't take it personally Perl-mongers.)

So here's my series of articles on how to make Perl more Pythonic. (I know this is heresy and blasphemy in some circles, but hey, it's a big world out there.)

One of my primary gripes is the use of $ variables. One in particular. $_ which is the default input and search space variable.

In fact, a lot of perl programmers will leave it out since it is implied by default. Which makes for totally unreadable code unless you know the secret handshake and secret password.

So, my first step to wading in the Perl pool is to use the alternative form of this variable:


So whenever you feel like using $_ or using $_ implicitly, please. Please use $ARG.


Stay tuned for more "how to do pythonic perl" articles.

Update: Here's the next parts to the Pythonic Perl series:


brunov said...

How about NO? $ARG is a horrible name for a variable. And the implicit usage of $_ is nothing less than beautiful.

It's the reason why you say "Hand me the book over there, the blue one" instead of "Hand me, Bruno Vecchi the Book: Apocalypses that it is on top of the Table in the middle of the room that we, Bruno Vecchi and you, John Doe, are currently standing at. The Book: Apocalypses is color blue".

I like python. When I went through the learning python book, and played with the examples, I tried to embrace the pythonic way of coding because I think that I'd be able to make the most out of the language that way. The same goes with Perl: don't try to fight the language, there's nothing wrong with using well known perl idioms and write perlish code. This doesn't mean, however, that your code should look like crap. But there's nothing wrong with saying:

say for @names;

instead of:

foreach my $name (@names) {
say $name;

rr said...

I prefer to go with the Perlism of: "There's more than one way to do it."

And the way I like goes by the Pythonic way of: "Explicit is better than implicit."

Going by your example, it would translate as "Give me." where as using $ARG would be: "Give me the blue book."

The first counts on the fact that everyone knows what it is you want when you don't say what it is.

But hey, it's a big world. Viva la difference, I say.

And if people truly believe in TMTOWTDI, then they should celebrate my desire to find Another Way. If the $ARG way is truly ugly, then maybe they should remove it from the language.

brunov said...
This comment has been removed by the author.
brunov said...

"Give me" translated to code would be:


Which is meaningless, since you didn't specify what to print. You do that by using the "for @array" construct, which unambiguously gives a list to iterate from. The only implicit thing, and a fairly obvious one, is that we are performing the action "say" over each element of the array. But the name of the array is given explicitly.

And the TIMTOWTDOI argument is more often than not used to justify ugly hacks, sloppiness and overall sub-par code. Of course you can use $ARG. You can also pass all of your production code through Acme::Bleach (*). Hey, it's Perl, right?

The point of my first comment was, if you *have to* write Perl code being a Python guy, it might be worth it to look around and see how people do things these days in the Perl community, and what Modern Perl looks like.

(*) http://search.cpan.org/~dconway/Acme-Bleach-1.12/lib/Acme/Bleach.pm

rr said...

I don't see why using $ARG instead of $_ explicitly or implicitly can be called an ugly hack.

And my point is that a community that celebrates TIMTOWTDI should welcome diversity of opinion.

Unless TIMTOWTDI actually means TIMTOWTDI as long as you do it our way.

You can't have it both ways. Larry uses the TIMTOWTDI to disparage other more militant programming languages. But when someone comes in programming in a way that is comfortable and useful for them, the community comes down with comments like that.

This is not a very public blog. In fact, I just use it for my personal notes. It's kind of like whistling while I work. I can find the little bits that I learn and put them here. And I do realize that the only reason that you and everyone else who's clicked this article in the past day are here is because of a twitter post mocking my article.


Whatever makes you happy. I just find it utterly hilarious that a suggestion that we use $ARG instead of impilicitly using $_ might be better for a code maintenance point is taken with such venom.

In the end, it says a lot lot more about you (and mr.twitter poster's) frame of mind than my code.

chorny said...

IMHO, $ARG is no better that $_. In cycle you should also used variables, named correctly, like $line, $i (for counter, maybe not better that $_).

For better readability try tools like perltidy, Perl::Critic, Moose, MooseX::Declare.

brunov said...

raoul, please don't take this as a personal attack. I'm sorry it made you feel that way; I'll refrain from commenting on your blog from now on.

I could comment on your last reasonings and how I think they aren't really a contradiction, but I'll pass, as you are obviously not in the mood for a discussion on this topic.

rr said...

chorny, absolutely. I agree that one should name the variables and not use the default input variables ($_, $ARG).

To be truly Pythonic, explicit is better than implicit.

But if you must use the default input variable, I prefer $ARG over $_ just because it's easier to read.

Matter of taste, so there's no right or wrong here.

Thanks for the tips about the Perl libraries. I will definitely look into those.

brunov, I'm always up for technical discussions. Personal attacks, I have no time for. We can agree to disagree as to what is better ($_ vs. $ARG). But for me $ARG is slightly better that$_. And explicitly naming your variables is infinitely better than either.

brunov said...

Please point me to the sentence in which I personally attacked you, because upon rereading my comments twice now, I cannot find it, nor I remember doing it in the first place.

rr said...

Ho hum.
Here you go:

And the TIMTOWTDOI argument is more often than not used to justify ugly hacks, sloppiness and overall sub-par code.

rr said...

Please help the victims of tropical storm Ketsana in the Philippines:

Donations to Red Cross Philippines: http://www.redcross.org.ph/Site/PNRC/wtd.aspx

Thank you.