
The joy of Perl - kamaal
http://www.salon.com/1998/10/13/feature_269/
======
jgfoot
This article is from 1998. A lot has changed since then: Ruby, alternate JVM
languages like Clojure and Scala, C#, Perl 6... If I were to make a sweeping
historical characterization, I would say that Perl was exactly what people
needed in the 1990s when everything was new and nothing worked with anything
else and everyone needed lots of duct tape and Perl's wonderful concise syntax
made excellent duct tape. I used it during that period, but I just don't use
it much anymore.

~~~
pavelkaroukin
Perl 5 also changed a lot since then. And it is definitely not just duct tape
anymore (if it ever been)

------
einhverfr
I wonder how many others caught Wall quoting Lewis Carrol....

I suppose Carrol and Tolstoy are to be quoted when asking a natural language
guy about computer languages.... ;-)

Honestly, for the last 6 years I have been doing mostly Perl programming (and
managing an open source project largely written in Perl) and if I can't easily
read a contribution easily I push back.... Perl doesn't have to be hard to
read. I would say, however, that keeping things minimalistic is a real virtue
in the language.

~~~
kamaal
Well why does Perl win:

Ready for this?

The way I see, Any other way of doing regular expressions sucks more badly. In
fact if you are look to replace your regular expressions with something else
you are likely to inherit a thick verbose pile of junk. Now why do regular
expressions work.

Look at this thing below:

    
    
        $line =~ m/<some regex>/;
    

This can also be written as,

    
    
        $line =~ m'<some regex>';
    

or

    
    
        $line =~ /<some regex>/;
    

This matches the line with <some regex>, Why is this special? Because it just
allowed to you match a line without first compiling the regex, and then
matching it. Not just that it saved you from writing several tens of lines of
code before using a regex which you generally have to do in other languages.
So you can do something like:

    
    
        if ($line =~ /regex/)
        {
        }
    

or

    
    
        do_something() if $line =~ /regex/;
    

This is a bit of natural language way..

In case of file operations:

    
    
        while(<FILE>)
        {
            chomp;
            #Do your stuff
        }
    

There are some amazing things that Perl does like

    
    
        my @file_content = `cat somefile`;
    

Suddenly all contents are in an array in your program.

Worried about removing new lines from that array? Just do

    
    
        chomp(@file_content)
    

Want to go over that file now? Just do

    
    
        foreach my $line(@file_content)
        {
            #Do your stuff here
        }
    

Want to deal with that line? You have,

    
    
        my @fields = split( /<some regex>/, $line );
    

Want to do more matching/extracting from lines?

    
    
        my @processing = map { do_something($_) } @fields;
    

Want to find something in that array?

    
    
        my @results = grep { /some regex/ } @fields;
    

Want to handle exceptions?

    
    
        eval
        {
            #Some operation
        }
        if $@
        {
            #Handle exception
        }
    
        open( FILEHANDLE , '<' , $file ) or die "Cannot open file";
    

Just felt the need to create a small database?

    
    
        use strict;
        use warnings;
        
        use DB_File;
        use Fcntl;
    
        my %some_hash
        tie (%some_hash, "DB_File", $dbm_file, O_RDONLY or die "Unable to open dbm file $dbm_file: $!";
        #change your hash
        untie(%some_hash);
    

You just got a key-value database.

Want to execute a shell command?

    
    
        `/some/command/execute`;
    

or

    
    
        system('/some/commmand/execute');
    

Need to create a function?

    
    
        sub function_name
        {
            my ($var1,$var2..) = @_;
    
        }
    

Need to create a Class or a Package?

    
    
        package Name;
    
        #do stuff here
    
        1;
    

You can also create anonymous functions

    
    
        my $function = sub {};
        $function->();
    

This also means you can create a closure:

    
    
        sub function1
        {
            my $function = sub {#does something};
            return \$function;
        }
    

In latest versions of Perl, there are some cool features like:

    
    
        given($some_data_structure) #what ever that is array, reference etc
        {
            when ( #some match operation ) { #do something }
            ...
            ...
        }
    

this serves both as a smart matching loop/switch statment.

All the above gives you quick scripting tools.

You also have your usual set of ternary operator,if..else, you can also quote
regular expressions.

Now why did I write this, I wrote this to tell that. Perl's motto is to get
stuff done. In a no BS way. It adapts to your style, its Object oriented, its
functional etc. You decide the style the language bends to you.

You get Best of the OO features from
Moose(<http://search.cpan.org/~doy/Moose-2.0402/lib/Moose.pm>), You get
modules like Devel::Declare and DBIx::Class, You also get a framework like
catalyst. At the same time you can write in funtional programming techniques.
You can do cool stuff like Memoization and higher order parsing. For more
information read Mark Jason Dominus's Higher order Perl - (free ebook)
<http://hop.perl.plover.com/>

At the same time you as a novice can begin with programming with what you
understand Read Chromatic's Modern Perl Book - (free ebook) -
<http://onyxneon.com/books/modern_perl/>

You can do Object oriented programming, Functional programming, Declarative
programming all without shoe horning things using frameworks. You can build
big to medium sized apps. Its difficult to demonstrate all that in a post,
Hence read MJD's book.

But at the very same time you can use it as a scripting language in which you
can cook scripts are at a very quick pace, You can quickly prototype, solve
data/text processing problems. You can make it behave like awk, sed and what
not. You can create quick databases.

No wonder its still among the top programming languages in the world even
after 25 years since its release.

Last but not the least, Its killer app : <http://www.cpan.org/>

~~~
caw
I agree Perl is great for everything listed except:

    
    
        sub function_name
        {
            my ($var1,$var2..) = @_;
    
        }
    

I know why methods are like that, but it doesn't make me any less unhappy to
have to read a function in order to figure out what types and what order I
need to pass values to a function.

There's a few Perl programmers at work who end up using just a bunch of global
variables instead of doing pass by reference or returning values from subs.

sub do_something { # modify %my_global_hash }

It's just strange working with Perl after working in more OO languages. But,
like you said, Perl adapts to your style. I want it more like other languages
I've written in, whereas the people I work with don't.

Getopts::Long is amazing for parsing command line, and I don't know how I
would work without being able to do the inline if and unless statements.

~~~
gatlin
<https://metacpan.org/module/Function::Parameters>

There are other modules out there like this, including one that bakes it into
Moose methods. This also highlights a cool Perl feature: hack able syntax.

~~~
sciurus
There are lots of other modules

<https://metacpan.org/module/Method::Signatures>
<https://metacpan.org/module/MooseX::Method::Signatures>
<https://metacpan.org/module/Method::Signatures::Simple>

and many more. In this case, there might be too many ways to do it.

------
RyanMcGreal
> “We sell the bestselling books on both Perl and Python,” adds O’Reilly, “and
> what we see is a continued healthy growth in the use of Perl, while Python
> use is flat. Perl is orders of magnitude more widely used than Python, and
> we see no signs of that changing, at least based on book sales.”

I wonder if the difference in book sales is at least partly an artifact of the
relative difficulty to learn Perl compared to Python.

~~~
knieveltech
I find perl substantially more approachable than Python, provided I don't have
to read anyone else's code.

~~~
RyanMcGreal
> provided I don't have to read anyone else's code.

That's a hell of a caveat. :) As I get older, I find I spend more time reading
code than writing it (even though most of it's my own), and I find Python's
sheer readability indispensable.

~~~
kamaal
Its not, basically because if you have bad programmers working for your then
you have bigger problems than choosing a language.

And if you have good programmers, then readability isn't a problem. Not even a
thing to think about.

~~~
berntb
>>And if you have good programmers, then readability isn't a problem

Hrm, I'd argue against that.

I know better programmers than me, whose code I _hate_ to work with. Also for
Python.

But you seem to define "good programmer" as someone who also comments and
documents, so I guess you're by definition correct. :-)

Edit: Yeah, "itsnotme". The fault is rather the idiot bosses that let the
smart idiots get away with it... But with non-idiot bosses, you won't have
readability problems in Perl either.

~~~
kamaal
According to me good code is what makes the comment unnecessary. And writing
such a thing requires understanding many things. And those things are language
agnostic.

Like some one mentioned in this thread, languages tend to be judged by the
quality of code produced by their worst user.

For once you can write unreadable code in any language. Even in C and Lisp. In
fact there you can create bigger problems and more unreadable code.

~~~
berntb
You _still_ needs comments that tell what the content of a code file is
supposed to do, no matter how clear the code is.

(And in scripting languages which doesn't declare parameters, like Python and
old style Perl, you need to document what the parameters are.)

------
16s
I do have to admit that Perl had a fun, free-spirited feeling to it that I've
not found in any other programing or scripting language. It was fun. Python is
close, but a bit too rigid and takes itself too seriously.

Edit: I don't mean that as a criticism of Python. I use it daily and like it.
It's just that Perl _was_ more fun. That's all I mean to say.

~~~
bane
Python does an admirable job of being pretty fun to hack in (you're right, not
as much fun as Perl, but not bad), but the end result is that the code is far
more readable for others and I think works a bit better in large scale
production environments. Perl has come a long way as a response, but there's
no doubt that Python ends up with cleaner code.

~~~
einhverfr
Perl is still an _excellent_ glue language.

The way we are moving towards things in LedgerSMB is:

Web and file format (csv, latex, etc) templates in TemplateToolkit

Everything that can be done as set operations in the db in the process of
storing/retrieving info is done in PL/PGSQL.

A brief framework written in Perl

And the rest of the Perl code essentially is written to be very light-weight
automation glue between these layers.

------
lukejduncan
FYI the article is from 1998. I did a double take when I saw this line.

"Wall has Caller ID hooked up to his computer, which he has instructed to emit
unique sounds for each incoming call. What good is Caller ID, asks Wall, if
you have to walk all the way over to the telephone to see who is calling?"

------
drats
The readability point is key, will anyone ever write an article extolling "the
joy of someone else's script whipped up Perl"? Red Hat and Ubuntu both use
Python more than Perl when they want something over and above Bash.

~~~
kamaal
I've never had problems reading Perl anymore than I've had reading code in
some other language.

~~~
mtts
You've been lucky, then. I love Perl, but there are so many ways to do it
you're bound to run into someone who does something in an entirely different
way than you. The worst experience I had with this was a colleague who knew
the entire Camel book by heart and insisted on using every damn trick there
was. He was fired very quickly as the code he produced was impossible for a
more normal person to understand

~~~
rjbond3rd
Your anecdote implies that we judge the language by its worst user :)

------
kinleyd
I'm a Ruby guy, with no experience with Perl - so no comments on that front.
The part I really like about this story is David Filo's gesture of gratitude
to Larry Wall. That's a great karma story that warms the heart.

~~~
draegtun
You may also find this video of interest: _Clay Shirky on Love, Internet
Style_ <http://www.youtube.com/watch?v=Xe1TZaElTAs>

~~~
kinleyd
Thanks, that was an inspiring video. I've already come to appreciate the power
of community (love, according to Clay :), in communities like dd-wrt and
cyanogenmod where the quality of collectively contributed firmware beats by
miles that provided by the parent hardware vendors. And what's even better is
that those enjoying these benefits are willing to give back - best example was
when cyanogenmod called for contributions to buy new servers recently. The
response was overwhelming and they received more than they needed in no time
at all.

The other nice story carried on HN was how Linux Torvalds was remembered by
the Red Hat folks with a nice payback in shares as a thank you for everything
he did.

The internet really helps too, allowing a dollar here and a dollar there to
really add up when genuinely appreciative people make voluntary donations.
Bram Cohen is a good example here, who receives well deserved donations from
all the folks who love bittorrent.

I believe this model of giving (and the return karma) augurs well for society.
There is room for a lot of hope and optimism.

