Why Love Perl

See WhyHatePerl for opposing viewpoints.



Implement a basic version of uniq. I.e., write a program that goes through its input files and prints out the unique lines it sees. Try to make it perform reasonably well. In Perl that is short enough that you can write it on the CommandLine:

  perl -ne 'print if 1 == ++$s{$_}' input files here
In most other languages this program would be substantially longer.

How would it look if instead of ' input file here, you would make it a directory and all its subdirectories? Would this be possible? -- DonaldNoyes

By and large, the savings with Perl are due to comparing a ScriptingLanguage with non-scripting languages like JavaLanguage and CeeLanguage. Perl does not have such a huge advantage against other scripting languages. (Particularly RubyLanguage.) -- BenTilly

perhaps UniqInManyProgrammingLanguages? to test both the original claim and this counter-claim?

I know this sort of thing gives perl a bad name, but we can golf that script down to

  perl -pe '$s{$_}++&&undef$_'
:-). My personal preference, however, would be

  perl -ne 'print unless $s{$_}++'
-- DaveWhipp

Incidentally, the uniq replacements above don't actually produce the same answers as the real uniq(1) command (the real uniq(1) requires the input to be sorted) try with the input file

  1
  2
  1
The reader is invited to draw his own conclusions on what if anything this says about the perl mindset ;-) -- DanBarlow

That brevity is more important than correctness? ;-)

That it's close to the ExtremeProgramming mindset? In that the code solved the stated problem, i.e., 'Implement a basic version of uniq.', instead of some other problem.

But it's not in Basic, it's in Perl! <ahem> But seriously, folks, if it doesn't meet the requirements, what good is it? Brevity is all well and fine, but correctness is a lot more important to my clients, methinks. Particularly when you look at the mess Perl makes of the argument set - it's composed entirely of Perl-unique descriptors. Wonderful to a Perl guy; a complete and impenetrable mystery to anybody else.

Just as in the futile arguing over the loverly characteristics of ExtensibleMarkupLanguage we are once more focusing on brevity and ignoring clarity. Why are we still doing this? "Doctor, it hurts when I do this." "Well, don't do that."

For what it's worth, it's easy to match the functionality of uniq more closely:

  perl -ne 'print unless $s eq $_; $s = $_'
That performs much more reasonably (memory-wise) than the first example. The first example will chew up memory on large files. You'd probably use the DB_File module or somesuch if you wanted the exact functionality of the first example.


Let's try that with slightly different words. "Perl has a DataStructure that it calls a list, which exposes an approximate subset of the functionality in a real LinkedList". As a LispLanguage programmer I really don't find this any more exciting than the news that it supports subroutines would be to a CeeLanguage programmer. (AwkLanguage programmers, however, are thrilled to have both.)

How about Perl shares six of the seven features of Lisp listed in the "What makes Lisp Different?" section of "Paradigms of Artificial Intelligence Programming" (http://hop.perl.plover.com/preface.html).

The thing to keep in mind about Lisp, however, isn't just that lists are FirstClass objects--it goes much deeper than that. In Lisp, programs are written as lists, and programs are literally just AST trees. This, in turn, makes it very easy to alter Lisp to make it fit the program domain. Languages that have syntax have trouble with this. Perl has incredibly dense and complicated syntax, so it's at the opposite end of the spectrum compared to Lisp. Because of this, it is highly unlikely that Perl will cross the threshold to become a Lisp. --Alpheus


I just saw http://cpan.uwinnipeg.ca/dist/Filter-Simple which is really impressive. The lispiest thing I've seen in perl.


Perl isn't a language designed to be placed in a museum and looked upon as a thing of beauty. It isn't designed to follow a particular decades-old aesthetic of neatness, one that's progressed through languages like PascalLanguage and and ModulaTwo and EiffelLanguage. A classic tenet of such neatness is that the core language should be minimal, and everything you need is in a library. As a purist, that agrees with me. But at the same time, a language is used to get things done. Having flat, immediate access to a large number of useful features and coding styles is what makes Perl useful. For example, there's no need to bring in a RegularExpression library, then have to precompile each expression, referencing it by a handle. There are also little, catchy constructs that cover common situations. "total++" in CeeLanguage isn't cryptic; it's clean and clear and it's shorter than "total = total + 1". You get used to the conciseness of idioms like:

  while (<>) {
     print if /START/ .. /STOP/;
  }
to print all the lines from START to STOP in a file, inclusive.

There's also a good use implicit information in Perl. You don't need to call a function to get the length of a list. You use a list in a scalar context. The information is already there, and using it leads to concise and free-flowing programs:

   $test = "a2b4c6d8";
   @list = ($test =~ /(.)/g);  # for a less general case use @list = split //, $test;
   %hash = @list;
This code builds a hash of (a,2),(b,4),(c,6),(d,8) from the $test string.

Perl isn't perfect. It falls down hard when it comes to nested DataStructures, even passing such structures to subroutines. But for a good chunk of everyday work, Perl is perfect.

-- JamesHague


It runs on just about any platform including Windows, Macs, VMS, most Unices (I am hesitant to say all but it has been on every Unix system I have seen) and MainFrames. Can you say 'Portable'?


I think that

should be refactored in just one sentence. We could take a dig at the ObjectOrientedProgramming crowd and say that PerlLanguage is an accurate model of how people think :)


Metaprogramming - perl has a lot of little, somewhat orthogonal tools that can play different parts in this.

Try running through the CatalystFramework tutorial, which uses DBIx::Class::Schema. Several of these are at work there, either visibly or behind the scenes.


CategoryPerl


EditText of this page (last edited January 7, 2014) or FindPage with title or text search