LWN.net Logo

Perl far from dead, more popular than you think (Royal Pingdom)

Perl far from dead, more popular than you think (Royal Pingdom)

Posted Nov 6, 2009 22:31 UTC (Fri) by stijn (subscriber, #570)
In reply to: Perl far from dead, more popular than you think (Royal Pingdom) by rfunk
Parent article: Perl far from dead, more popular than you think (Royal Pingdom)

Let's try a different and for me more realistic scenario.

Perl:

my %dataframe = ();

while (<>) {
   my @F = split;
   push @{$dataframe{$F[0]}{$F[1]}}, { foo => $F[2], bar => $F[3] };
}

Ruby: ?

This uses Perl's autovivification features. They are not always suitable but a great feature nonetheless. If Ruby has something as succinct I will definitely start using it. I am not arguing that this particular feature places Perl above another language. But it's a feature I find very useful. That aside, Perl is a tool, and not a bad tool at all. I am a bit wary of perl6 though. Perhaps the offfshoots will be worthwhile (Parrot) but the language itself seems just to have too much syntax.


(Log in to post comments)

Perl far from dead, more popular than you think (Royal Pingdom)

Posted Nov 6, 2009 23:08 UTC (Fri) by foom (subscriber, #14868) [Link]

Python (assuming I've read the perl correctly: I don't know perl all that well):
import sys
dataframe = {}

for line in sys.stdin:
   F = line.split()
   dataframe.setdefault((F[0], F[1]), []).append({ 'foo': F[2], 'bar': F[3] })
or, you can also say the following, which might be closer to perl's "autovivification" (?):
import sys, collections
dataframe = collections.defaultdict(list)

for line in sys.stdin:
   F = line.split()
   dataframe[F[0], F[1]].append({ 'foo': F[2], 'bar': F[3] })
The disadvantage of python is that you don't get to use fun words like "autovivification".

Perl far from dead, more popular than you think (Royal Pingdom)

Posted Nov 7, 2009 22:01 UTC (Sat) by njs (guest, #40338) [Link]

IIRC, the Python equivalent to Perl's <> is actually 'import fileinput; for line in fileinput.input(): ...'

Perl far from dead, more popular than you think (Royal Pingdom)

Posted Nov 9, 2009 7:28 UTC (Mon) by jamesh (subscriber, #1159) [Link]

Really? I've never bothered using the fileinput module in my programs. What features of Perl's <> operator do you miss out on by using Python's standard file objects?

Perl far from dead, more popular than you think (Royal Pingdom)

Posted Nov 9, 2009 8:18 UTC (Mon) by njs (guest, #40338) [Link]

Just some of the standard perl magic -- '<>' doesn't give you lines from stdin, it gives you "if there are any arguments to the script, treat them as pathnames, open them one at a time, and if along the way you bump into a file named '-' then read stdin at that point, or if there aren't any arguments just read stdin". Which is a common convention for unix command line tools (see cat, tr, etc.).

I've never used fileinput either -- but people who are used to using <> to express all that magic might be annoyed at being told that sys.stdin was equivalent.

(Oh, and it also has features to emulate perl's '-i' switch and maybe some other bells and whistles, see the docs.)

Perl far from dead, more popular than you think (Royal Pingdom)

Posted Nov 10, 2009 15:36 UTC (Tue) by jamesh (subscriber, #1159) [Link]

Fair enough. I guess it'd depend on whether the original Perl program was intended to make use of all those features, or was just using it as a shorthand for <STDIN> (I think that's correct. It's been a long time since I've programmed in the language).

Perl far from dead, more popular than you think (Royal Pingdom)

Posted Nov 6, 2009 23:30 UTC (Fri) by rfunk (subscriber, #4054) [Link]

Ah, you want autobugification! :-) In my experience, autovivification causes as many problems as it solves. It's true that Ruby's autovivification abilities don't quite match Perl's. On the other hand, they're more powerful in some ways.
dataframe = Hash.new { |h,k| h[k] = {} } # 2-level hash autovivification

ARGF.read.each do |line|
  f = line.split
  d = dataframe[f[0]][f[1]] ||= []
  d.push({ :foo => f[2], :bar => f[3] })
end
See also: http://t-a-w.blogspot.com/2006/07/autovivification-in- ruby.html

Oh yeah, I hate Perl's "my" too. Automatic local scope is a good thing, and I find it much more useful to use the sigils for scoping than for typing.

Perl far from dead, more popular than you think (Royal Pingdom)

Posted Nov 6, 2009 23:48 UTC (Fri) by rfunk (subscriber, #4054) [Link]

Come to think of it, its closer to the Perl (and just better) to change the
"ARGF.read.each" to "ARGF.each_line". The .read.each version reads the whole
file at once, then loops over the result, while the .each_line version reads
one line at a time.

BTW, Perl was part of the initial inspiration for Ruby, so there are lots of
Perl-like features (and some Perl-lookalike features) in Ruby.

Perl far from dead, more popular than you think (Royal Pingdom)

Posted Nov 6, 2009 23:53 UTC (Fri) by stijn (subscriber, #570) [Link]

I seem to recall mentioning that 'autovivification' is not always suitable. The code is interesting -
I advise a more detached approach towards tools though.

Perl far from dead, more popular than you think (Royal Pingdom)

Posted Nov 9, 2009 1:18 UTC (Mon) by jmm82 (guest, #59425) [Link]

succinct = "Unmaintainable by anybody except the author"

I love writing throw-away perl scripts, but dread maintaining someone else's succinctness. I know everyone argues you can write maintainable perl code, but it leaves far too much room for someone to obfuscate the code into trash.

Perl far from dead, more popular than you think (Royal Pingdom)

Posted Nov 9, 2009 8:16 UTC (Mon) by niner (subscriber, #26151) [Link]

> I know everyone argues you can write maintainable perl code, but it leaves far too
much room for someone to obfuscate the code into trash.

But that's not different from any other language (that I know). I still have to maintain PHP
and Python code I inherited and no, none of these is any more maintainable than the
Perl code. And really, not even the Python stuff. Just pick completely non-descriptive
variable names and completely screw up your code structure and someone will feel real
pain.

Nowadays we create only Perl code. And part of the reason is, that we have a very fine
testing framework, Test::Pod::Coverage to test for documentation coverage,
Test::Perl::Critic to automatically uncover coding style errors which could lead to
unmaintainable code and Devel::Cover to test the test suite's coverage of the code. With
those tools and some architectural review before coders start programming, it's very hard
to write something unmaintainable.

Perl far from dead, more popular than you think (Royal Pingdom)

Posted Nov 9, 2009 16:44 UTC (Mon) by jmm82 (guest, #59425) [Link]

Every language has there "nooks and crannies", but perl goes out of its way to create as many "nooks and crannies" as possible and then has contests to promote their use. I am sure that there is some very robust perl code written within a constrained environment where maintainability is enforced.

I respect perl for its innovations in the field of scripting and regular expressions, but if I was starting a project from scratch, I would choose Python or PHP over perl. Perl is by no means close to dead, but neither is COBOL.

Copyright © 2012, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds