views:

2025

answers:

10
+43  Q: 

Perl Myths

I keep seeing people trip over common misconceptions of how Perl exists and what it does.

There are generally 2 types of Perl Myth.

  • Type 1: Things that people think about the language itself, that are not true
  • Type 2: Behaviors people exhibit when using the language, which generally derive from lack of common sense.

The first one might be a bit straight forward, the second might need a little more explanation. An example of this is the types that inevitably result in people reinventing wheels due to not understanding what CPAN is, or how it works, such as the common "I'm not allowed to install modules" argument.

This question is in the nature of these related Perl tips

What common myths do you keep seeing in Perl?

Due to the nature of myths requiring evidence, its probably best to use an assertion/explanation/reference format of some sort.

+40  A: 

MYTH: I'm not allowed to install modules

The Basics

Generally this is not the case, modules are generally just text files.

You just put these text files on your system, and then tell Perl how to find them.

If you have a system where you are not allowed to write text-files, period, then you are not programming anything.

If management decree that you are not allowed 3rd party code of any form, then you may have an argument, and possibly poor management. For the cases where the modules require Binary components built that's quite understandable, but there are generally good alternatives in Pure Perl.

The Alternatives

This is a wonderful guide on how you can set up locally visible modules in Perl without requiring any form of root account.

For the most trivial of modules, you can install only the very basic parts you need by hand.

cd  /project
mkdir lib
cd lib
# Say the 'package' in the file is "Foo::Bar::Baz" 
mkdir -p Foo/Bar 
cd Foo/Bar 
wget  $url -O Baz.pm 
cd /project 

And there you have it, you've installed a module into the lib directory of whatever project needs it. Its admittedly a bit messy to do it that way, but it works.

All thats needed is to do in your code:

 #!/usr/bin/perl 
 #   /project/foo.pl 
 # 
     use strict; 
     use warnings; 
     use lib 'lib'; 
     use Foo::Bar::Baz; # Works!

And it should be smooth from there.

Not Using CPAN is a Bad Idea Indeed

Perl modules uploaded to CPAN generally have an entire fleet of testers on different operating systems building them, and running their tests, as well as committing bug reports and contributions for weird edge cases.

The only time you should NOT use an existing module, is when you KNOW as a matter of FACT you can do a better job.

In such cases, its recommended to write your own module, and upload it to CPAN, so that you can benefit from the team of testers who will run it, find bugs, and report them.

If you're really lucky, they'll report bugs with working patches.

Kent Fredric
Note: if anybody thinks this is incomplete, or could be extended upon, elaborated on, go ahead, its community wiki for a reason, initial attempts always suck. Make it better, I won't be offended.
Kent Fredric
That modules may be text files is irrelevant. There are a lot of places where they are wary, for good reason, of external code. Ignoring the real risks is as stupid as beleiving untrue things. Blaming management for proper risk management only makes the problem worse.
brian d foy
Good points, but I'm unsure how to forge them into the statement, if somebody can add a statement covering 3rd party threat assessment rationale vs management "3rd party code is right out, regardless" wrt CPAN stuff that would be awesome.
Kent Fredric
If it were whory PHP 3rd party code glued together with spit and unmaintained and not updated ever that makes perfect sense, but CPAN is on average way better than that.
Kent Fredric
@brian: Right on! Management has to be on guard against techie enthusiasms as well. If they catch wind that the entire perl world thinks that they're needlessly stuffy, when they think themselves prudent, it just is more likely to create a backlash against Perl.
Axeman
What if the PM contained some XS and I did not have GCC on the same system?
PoorLuzer
not having a working GCC does pose a bit of a problem, but if you can duplicate the system in a location you control so that you do have gcc, you can compile the module there and then ship it back.
Kent Fredric
many modules depend on compiled libraries. these can be compiled with gcc and shipped back as well, but the process will become more cumbersome
reinierpost
+15  A: 

The favorite one I saw (in an email sig, and only once so it doesn't really meet the "myths you keep seeing" criteria) was that Larry wrote Perl as a thin but incomprehensible layer on top of Python for people who wanted to guarantee their job security.

I know, I'm pretty certain Perl pre-dates Python as well, but it was pretty funny when I first saw it.

Evidence for Perl pre-dating Python:

Initial Check-in Message for Perl:

Author: Larry Wall 
Date:   Fri Dec 18 00:00:00 1987 +0000

    a "replacement" for awk and sed

    [  Perl is kind of designed to make awk and sed semi-obsolete.
       This posting will include the first 10 patches after the main
       source.  The following description is lifted from Larry's
       manpage. --r$  ]

       Perl is a interpreted language optimized for scanning
       arbitrary text files, extracting information from those text
       files, and printing reports based on that information.  It's
       also a good language for many system management tasks.  The
       language is intended to be practical (easy to use, efficient,
       complete) rather than beautiful (tiny, elegant, minimal).  It
       combines (in the author's opinion, anyway) some of the best
       features of C, sed, awk, and sh, so people familiar with those
       languages should have little difficulty with it.  (Language
       historians will also note some vestiges of csh, Pascal, and
       even BASIC-PLUS.) Expression syntax corresponds quite closely
       to C expression syntax.  If you have a problem that would
       ordinarily use sed or awk or sh, but it exceeds their
       capabilities or must run a little faster, and you don't want
       to write the silly thing in C, then perl may be for you.
       There are also translators to turn your sed and awk scripts
       into perl scripts.

Initial appearance of Python: 1991 [src]

paxdiablo
"so people familiar with those languages should have little difficulty with it" -- Oh, so Perl MYTHs got started together with Perl.
Windows programmer
@Windows programmer: you're missing the point, that was perl 1.0 , when it *was* much similar to the rest. Its *MILES* different *NOW*.
Kent Fredric
+39  A: 

Myth - By definition, anything written in Perl is spaghetti code

I think that this is the type 1 Perl myth. The problem is that it's also self-fulfilling, in at least two or three ways.

  1. If you already believe this, then the minute you see Perl code, with it's sigils and special variables and deferences, then - by god - it's spaghetti (not really, but you just can't be bothered to learn what you're looking at).
  2. If you already believe this, then why bother writing clean code? You only use Perl when you want something quick and dirty. You just throw something together under #!/usr/bin/perl and - by god - it's spaghetti (for real now, because you didn't take the time to make it better).
  3. The initial learning curve in Perl is a very mild one, but there's a steep climb up ahead. Many people stop early. It's easy to quickly learn enough to do a staggering amount. Everything in, say, Learning Perl (ie, no OO Perl, no references, no callbacks, no closures, nothing functional, minimal module use). However, this is really just a base. If you never learn more than this, then quite a lot of what you write may be spaghetti, since you will really have to twist the language to do larger things without references, modules, etc.
Telemachus
+1, but you might want to create a seperate answer for each myth. Community wiki so there's no annoyances with rep-whoring :)
Kent Fredric
Also, link of proof for point to *headdesk* http://stackoverflow.com/questions/754583/using-unixs-paste-and-awk-inside-perl
Kent Fredric
( The latter myth is also contradicted by the evidence for the 'its a python frontend' myth )
Kent Fredric
Yeah, I considered linking that way, but I wasn't sure if it wouldn't seem a bit aggro (as a colleague of mine says). I'm happy to separate, if that makes better sense. Or you should feel free to.
Telemachus
Although we don't get rep for things on CWiki, if I let people split their own articles I think it works better for preservation of originator of content, and thus, brownie points.
Kent Fredric
Cool. Done and thanks for cleaning up the split a bit.
Telemachus
+16  A: 

Myth - Perl is a shell scripting language

In terms of type 2 myths, one I bump into a lot is the (often unspoken) myth that Perl is really just Bash (Korn, Zsh, whatever) on steroids.

On Linux forums, I constantly see people using Perl to string together calls to awk, grep (not Perl's), sed, etc.

There are lots of problems here, but it ties back into some of the problems from the spaghetti myth.

If all you write are Perl scripts à la shell then, then no wonder some people have such a low opinion of the language.

Also again, it's somewhat easy to write such a script, but it's an unholy, unportable mess and a waste of a very powerful, often elegant language.

Telemachus
Um... perl *is* a scripting language. Is the myth that perl is *only* a scripting language?
Graeme Perrow
Hmm. I think that what I meant was that it's a myth to think of Perl as a _shell_ scripting language. (That's why I talk about stringing together calls to awk, grep, etc.) But if you think that "only" would help the title, go for it.
Telemachus
I think this myth comes from the awkward and may I say hacky way Perl implements almost all aspects of OO. I, for one, stayed away from OO Perl for a long time because of that.
Artem Russakovskii
@Artem Russakovskii Check out Moose http://p3rl.org/Moose
Kent Fredric
I use Perl as a shell scripting language quite a bit, and I don't see any reason not to use it as such. The language is capable of a lot more, and I use it for more, but why the prejudice against using it for scripting?
David Thornley
It's not a prejudice. I have reasons - some of which I mentioned in the post itself: it's messy, it's non-portable, it's error prone. Maybe we don't mean the same thing by "scripting". I mean when you put a whole bunch of calls to `system` under #!/usr/bin/perl. I don't see any point in that at all.
Telemachus
There is some truth to this.. Perl started out as a replacement for awk/sed/etc.. See the email quoted in http://stackoverflow.com/questions/755168/perl-myths/755210#755210 (although obviously Perl has changed and improved since then..)
dbr
That mail starts with this: "a _replacement_ for awk and sed." If you're saying that Perl is great for quick text wrangling, then I completely agree. My complaint is when people use the Perl interpreter only as a round-about way to invoke grep, awk, sed etc?
Telemachus
+23  A: 

Myth - Perl Is Dead

If there are a thousand elves in the forest, and you're not looking, are they dead?

Here's a random sampling of the number of idlers in a given set of channels on irc.freenode.org

  • ##javascript: 325
  • ##php : 736
  • #perl : 517
  • #ruby : 289

A meaningless sample of course, but still, not something I'd expect from a "dead" community.

But wait, Perl has its own entire dedicated IRC server!.

Yes, with several hundred channels.

And right at this moment there would be at least 250 users there alone, but yet again, it is a rather meaningless statistic.

( #stackoverflow on freenode only has 41 users, obviously, stackoverflow is dead )

Kent Fredric
This isn't a myth... just a wishful dream
Matthew Whited
Since my third perl program was an IRC bot (and I bet many other people did, too), I don't think that's really meaningful.
tstenner
A: 

Myth: perl is fast

Some people seem to think that perl is actually good for writing efficient programs. I've found this to be largely untrue.

While it is possible to get decent performance out of some aspects of perl, and it's possible to make simple programs which are fairly efficient (e.g. read each line of a file and do some regexp matching), perl performance degrades quite badly when it's used for programming in the large.

The main problem I find is that perl copies a string whenever you do anything - it doesn't have copy-on-write strings, nor are strings immutable (ala C#, Python, Java), instead it copies a string whenever you could possibly want the new string to be different (e.g. $b = $a usually copies the string), even if it is never subsequently modified.

This creates a lot of CPU load and fragments the heap.

Moreover, the way that perl is usually used makes this worse. Perl objects use a lot of resource, and perl hashes store one copy of the hash key string for each hash it appears in, so if you have a million hashes with a key of "hello", you'll store a million "hello"s. This means that data structures use a lot of memory. Too much for high performance applications, typically.

I'm not claiming that Python is faster - it's very difficult to rewrite a nontrivial program in a different language to compare, and I don't have enough detailed Python knowledge to be able to compare the guts objectively.

In things such as "The great language shootout", Perl fares relatively well, this is because the programs were written optimally, and the

MarkR
Translation: "I don't like Perl, and even though I don't have any real evidence, I think it's slow."There's a lot of generalizations and nitpicking, but no useful data to support your conclusion. Picking out a few specific cases where a language performs suboptimal doesn't make it slow. I could easily show you functions in Python, Java, C#, and other languages that are slow or expensive.I've seen applications written in Perl perform extremely well. In fact, at a previous job they rewrote a Perl application in Java, and after 3 years of development, the Java version is *still* slower.
Christopher Cashell
There is no absolute "fast" or "slow" in computing. Things can only be faster or slower in a very very specific context. Of course large matrix computation in Perl would be slower than in highly optimized C, and of course doing large-scale pattern matching and reporting is much faster in Perl than it could be with a bash script using calling greps and awks all the time. Perl is slow only for people who don't know/choose their tools.
Olfan
If you are using millions of hashes, you are doing it wrong. Go look up some optimizations.
Ape-inago
+46  A: 

Contrary to belief when a cat walks over one's keyboard, the resulting characters seldom produce executable Perl code.

Wabbitseason
lol this made my day!
hasen j
Have you tested this theory? Have you tried holding down shift for the cat while it walked so as to insert special symbols? lol
Earlz
+12  A: 

Myth: Perl is slow

You might think that as an "interpreted scripting language" Perl must be slow. In addition: perl provides easy access to hashes which are more flexible and convenient but much less efficient than structures in C or objects in C++.

The truth is: Perl is a very rich system. It has tools to process data very flexibly and conveniently. But it also has tools to process huge data sets as efficiently as C/C++; usually with much less programming effort (disclosure: right now, I do just this for a living).

First of all: the two tools to learn about are Inline::C (which allows you to easily include C code into your Perl programs) and PDL (conceived by some genus --- process large data matrices with the efficiency of C but without writing C code) and PDL::IO::FastRaw to keep your data in memory-mapped files.

The next ingredient is to be realistic about trade offs: Usually, you can trade some form of efficiency (CPU cycles/memory) by some other form of efficiency (flexibility/convenience). Writing a faster program honestly means paying more attention to the details of how the computer will do the work. You don't find a low-level programming language that is 10 times faster than perl in processing the same program. Instead, it forces you to always take the "fast" choices: pay programming hours to gain a few processing seconds.

Perl offers you a much more intelligent option: Write most of the program in perl (with all the benefits of Perl/CPAN) and optimize just the bottlenecks --- if needed by converting them to C.

PDL is an even better choice (at least if you are a mathematician and understand how to make use of it).

In my work, I usually find that after optimizing a small proportion of the program, I honestly can't justify optimizations in the remaining program because it doesn't take noticeable time to run. But exactly this part of the program (configuration; input/output) would be a real pain in C.

Summary: As compared to C, Perl gives you a choice what part of the program to optimize and what part to leave as-is. As compared to Java etc., Perl gives you access to both C-performance (and C-libraries) and CPAN libraries, and the interfaces to work with are much more convenient.

Disclosure: This approach involves some learning and attention to make right decisions what to optimize in your program --- as supposed to blindly comparing benchmarks in two different languages. But as I said: I do this for a living, not for entertainment.

Yaakov Belch
Perl is actually compiled to bytecode, not interpreted.
Brad Gilbert
Talking about that, are there _any_ serious so-called-interpreted languages out there, that are not really bytecode-interpreted?
sundar
+9  A: 

Myth: Perl Regular Expressions are PCRE

This is not directly Perl related, but lots and lots of help is sent looking Perl's way because of the "Perl" in "Perl Compatible Regular Expressions".

This is a myth, because

  1. PCRE is not Perl Compatible.
  2. PCRE is hardly regular.

If you go and ask a Perl programmer, you will get Perl specific answers, most of which will involve Perl specific libraries, because Perl programmers learnt long ago Regular Expressions are sub-optimal in many many cases and rely heavily on people to have solved the problems for them, and uploaded to the friendly CPAN library.

IE: Here is a small sample of reactions you will get when asking for a regular expression from a Perl group.

Parse HTML with a regular expression

Don't parse html with regular expressions! See HTML::Parser, and its subclasses: HTML::TokeParser, HTML::TokeParser::Simple, HTML::TreeBuilder(::Xpath)?, HTML::TableExtract, etc. See also http://htmlparsing.icenine.ca/ . If your response begins "that's overkill. i only want to..." you are wrong.

Parse CSV/xSV with a regular expression

use Text::CSV_XS, Text::xSV or DBD::CSV

Parse Email with a regular expression

Regexp::Common::Email::Address

Asking Perl for help with Regular Expressions makes about as much sense as asking Java people about JavaScript. ( In case you didn't know, they're not related except in name either )

Have a look at all the hairy options Perl supports

http://perldoc.perl.org/perlre.html

If you want these features, ( which we'll ultimately try helping you to use on your PCRE machine, only to discover it doesn't work because of PCRE ), you'll need ACTUAL Perl.

Kent Fredric
Perhaps you should add that PCRE is used a lot in PHP, and PHP is Perl's illegitimate daughter who grew up in the same way Perl did, and is now getting most of the action in web development, and it's PHP programmers who keep popping into #perl channels with PCRE questions without even realizing that Perl is a language rather than a PHP feature.
reinierpost
+2  A: 

Myth: perl is only for parsing text files

A lot of people think about perl as a language only to parse some text files, or maybe to write web backend in it. But via its modules, perl is lot more than that, for example my favourite is that you can write nice Gtk2 apps.

Pozsár Balázs