Main Navigation

Content

Freenode’s #perl FAQ

Learn Perl Now!
And get a job doing Perl.

This page was retrieved from the corresponding page at the currently offline perl.net.au wiki. There it was maintained by several people, including Shlomi Fish, and was licensed under the CC-by-sa Australia and the original Perl Artistic License. It was restored here from the wayback machine.

Freenode’s #perl channel’s FAQ From PerlNet

This is the FAQ for Freenode's #perl channel.

Contents

Commonly Asked Questions

What are the Guidelines of Conduct?

First of all note that these are just "guidelines" in accordance with the "rules are just guidelines" meme (e.g: 1 ; 2 ; 3 )

  1. Don't flood the channel with your paste. Instead use a pastebot - type perlbot paste.

  2. Be civil and friendly.

  3. Try to use proper English spelling, grammar, syntax, and phrasing. This will help the channel members understand your questions more easily and thus allow them to provide better answers.

  4. Whenever someone helps you then:

    1. Thank them!

    2. Bump their karma. For example if johndoe helps you then say johndoe++.

  5. When addressing a channel member, start your message with their nickname followed by a colon (":"). For example johndoe: I understand it now. Your IRC client may have an option to make this easier. Do not use forum/twitter-like notation of @nick because most clients will not highlight it specially.

  6. Do not private message (PM) a channel member using /msg, in order to ask a technical question, or to answer a question that they are asking you. This is considered rude, and you are preventing the other channel members from participating in the conversation and helping as well.

  7. Don't overuse the evalbot (eval: 5+6). The eval bot is primarily meant for experts to show working snippets of code to people who need help. It is not meant as your personal evaluator. If you still want to experiment then /join #perlbot.

  8. If someone needs too much help and handholding, please ask them to chat on one of the side-channels such as #perlcafe or #perl-cats. This will help increase the signal-to-noise ratio of #perl.

  9. Try avoiding demoting the karma of humans (= "johndoe--"), as it is demotivating and antisocial.

Handling criticism

As usual the guideline is Don't be right - be smart.. When someone criticises you (and may be an op), you should: 1. Ask them what they mean. 2. Agree and empathise with them, 3. Try to negotiate a common ground. Don't try to be defensive-aggressive and try to prove the other side's wrong because that will only make them more angry at you.

Naturally, this applies primarily to personal criticisms or those that influence one's behaviour in the channel. If you're having a "colour of the bikeshed" argument (i.e "Subversion vs. git vs. bzr vs. hg vs…"; Reference) or a discussion about whether $self->method(%args) is better than $self->method({%args}), a silly off-topic discussion about whether Chuck Norris can beat Superman or Batman or not, or something like that, then probably much less is at stake there.

For more information see:

English usage

Not all of the people on #perl are native English speakers. We also welcome people of all nationalities and native languages, even if their English is not perfect. However, we expect people to try to improve their written English in time, and learn from their mistakes. Improving your mastery of English will not only make it easier for you to communicate, but will also give you access to more high-quality material about Perl and programming in general (written either by native or non-native English speakers); allow you to better comment, document and communicate about your code; and finally, it will improve your skills as a programmer, because there is a general belief that people who speak and write human languages well, write better, more elegant and more idiomatic code.

It's OK to have bad English, but you should try to improve it, because otherwise many native and non-native speakers (especially many geeks who are less capable of understanding the meaning of badly phrased sentences) will have a hard time understanding you.

In addition to that, we should note that you should try to tolerate other people's not-so-idiomatic or possibly may-be-considered-derogatory English because they may speak, and think in a slightly different language than in English. For examples, in some languages there are non-derogatory words for a "female guy", whereas in English "girl" and "gal" are somewhat derogatory. As a result, a non-native English speaker may use them this way. (See for example what Shlomi Fish (rindolf) wrote about Hebrew words to describe humans).

Moreover, in some languages it is more natural to say "a guy from work", "a girl from work" than "my co-worker", and most Indo-European and Semitic languages have compulsory genders, including for inanimate objects, so it's natural for their speakers’ English to be not-so-idiomatic.

Finally, some foreign or non-foreign people may try to emulate some American, British, Australian, etc. culture, which they see on YouTube, Television, movies, etc. and while this may seem silly at times, should also be tolerated.

So don't be too strict about which language is being said.

I've become an Operator. What should I do now?

  1. Don't remain an operator for too long. Become an operator only to change the topic or to warn someone that he would be kicked if they continue with their abusive beahviour, or to ban an abusive person (see below). Use /msg ChanServ OP #perl $my_nick to become an operator, and de-op yourself using your IRC client to switch back and forth.

  2. You should kick someone only if:

    1. They are flooding the channel.

    2. They have been warned about being annoying or abusive at least twice, while the person who has warned them has been an op, and still continue. Do not randomly kick people without warning them. We don't want #perl to be turned into another EF-Net.

  3. You should ban someone only if:

    1. They continuously join after being kicked several times.

    2. They are spammers who spam the channel with ads or other things like that.

  4. The topic should remain in good taste. Don't put material that is sexist, racist, or otherwise insulting there.

What do I need to do to get a good answer to my question?

If you ask a bad question, you'll often get a bad answer, or no answer at all. As the questioner you probably won't realise that you are stating your question badly. When asking a question try to focus on the underlying task or goal, not the method you are currently trying.

Try to step away from the problem you're working with and see if you can trim your data set to about 5 lines and your code to less than 10 and still demonstrate the problem. We don't want to understand how your whole application works just in order to understand your question.

Please paste the code that reproduces the problem to one of the channel's pastebots, such as perl.bot . Paste the entire program, not selected parts of it and make sure you make your intentions clear. If the program is too long or is confidential, try to reduce it into a smaller testcase that still reproduces your problem.

Show us that you've put some effort into finding the answer yourself, or at least writing the question down well. This is much more likely to be rewarded by us giving you some of our time to help you fix whatever your problem is. Read the following resources for hints on how to ask questions that encourage answers. These techniques work on IRC channels, mailing lists and online forums.

What is Considered On-Topic?

Almost everything, but not all. Things which are frowned upon are:

  1. Overly graphical depictions of pornography or violence.

  2. Annoying trolling, preaching or sex advice.

  3. Flooding the channel with large pastes.

  4. Too much bot-playing.

  5. Obscure or annoying political discussion. This is what #politics or #perlcafe are for. Note that some interesting polito-philosophical discussion may be tolerated.

Another negative trend was that sometimes two or three people kept discussing their own pet projects or the Pike language or whatever, and it was of little interest to the rest of the channel, and they were instructed to discuss it elsewhere.

Otherwise we tolerate and like discussions about:

  1. Perl. (Naturally).

  2. Computing in general. (UNIX, Linux, Mac OS X, history of computing, etc.)

  3. Other programming languages - C and C++; Perl alternatives like PHP, Python or Ruby; Lisp and Scheme; Haskell, OCaml, etc.; Assembly; and more exotic languages.

    • We still try to focus on Perl, though.

  4. Geography, culture, linguistics, languages, etc.

  5. Science.

  6. Philosophy.

  7. Popular Culture

  8. Etc.

It is considered polite to keep off-topic - that is, non-Perl-related - discussion to a minimum when people in the channel are genuinely trying to get Perl help, since it seems unfair that a topical discussion get swamped by a lot of off-topic messages. Please consider that people who are asking for help are more likely to have less experience with following separate discussions in IRC.

The channel #perlcafe was an attempt to move off-topic and advanced discussion out of #perl. However, it didn't work, but, on the other hand, #perlcafe now hosts a lot of discussion that we feel should better be kept out of #perl.

Perl Includes CPAN

One thing to note that is as opposed to some programming language channels on Freenode that limit themselves to the core language (which is sometimes fully specified in a standard), we not only have a problem in discussing non-core code, but even encourage you to use many high-quality, recommended, yet non-core, modules from CPAN - the Comprehensive Perl Archive Network. To quote what Audrey Tang had said: UNIX is my IDE, perl 5 is my VM, CPAN is my language..

Please read what Matt Trout (mst) wrote about But I can't use CPAN, and the continuation post by Altreus titled "Share and Share-Alike" for our motivation. You may also get some help with some non-CPAN Perl code as long as you show some willingness to learn Perl, and to modernise and/or refactor it if necessary.

If this is homework, and your professor gives some constraints on what you can use (*sigh*) or your pointy-haired-boss (PHB) is constraining you in various ways (double *sigh*), please mention it when asking your question and we may still help you if the non-CPAN-enabled solution is, while not ideal, is still doable elegantly.

Is the Channel Logged? Where can I find Logs?

The channel policies probably forbid people from setting up public logs of the channel, so there are no public logs that we are aware of online. However, various people keep logs of everything that happens on #perl when they are present on the channel and keep these logs to themselves for their own reference and perusal, and they can supply you with excerpts of these logs.

It is a good idea to configure your IRC client to log your conversations so you can later refer to them.

I need help with tweaking a Perl script that I found, and I don't want to learn Perl. Can you guys help me?

The best way to get assistance with a Perl problem is to demonstrate that you've taken some steps to try to find the answer yourself. This involves learning Perl enough to be able to fix the problem yourself. Learning Perl is not too hard and will prove useful into the future.

Alternatively, you can pay someone to help you. Just ask on the channel if someone wishes to be paid. If you're trying to modify a Perl script without learning Perl (or paying someone who knows Perl) then we're not going to help. As the old Greek idiom goes: The gods help them that help themselves..

I'd like to run a script (which I wrote or downloaded) on a cheap (or free) web-hosting that only gives me FTP. Can you help?

Perl due to its architecture usually requires a bit more than an el-cheapo or "free" web-hosting. You need at least an ssh account, and often also a virtual server. There ain't no such thing as a free lunch. While many web-services are free, one has to understand that maintaining a server costs money, and the bandwidth costs money, and there's a lot of other costs. If you want something good, you have to pay for it, even in the modern Internet world. If you can't pay for it, you can try to find someone who'll host you free-of-charge, but note that it's also not trivial.

So please pay for hosting, have an ssh access, be able to install more CPAN modules, and let us know what the logs show. If you don't have money, just learn more Perl and become a Perl programmer. There's a huge demand for them, at the moment, and we could always use your help.

I need help with a non-Perl Regular Expression (PHP, grep, PCRE, etc.)

If you're looking for assistance with a non-Perl problem, you've probably come to the wrong channel. You can try asking, but don't be surprised if you don't get an answer (or someone suggests you ask elsewhere) as Perl regular expressions are different from the regular expressions used by other programs and languages.

Can you recommend a good Perl Editor/IDE?

See: Perl developer tools

Why doesn't my script run?

There can be several reasons:

  1. Are you sure you got the she-bang right?

    This should look like #!/usr/bin/perl or #!/path/to/perl. On Windows you can just write #!perl but it is recommended to write #!/usr/bin/perl (Windows will ignore the path). If you're writing your program on one operating system and running it on another, check that your she-bang line does not contain any errant characters such as the carriage return. This won't be a problem if you're using any switches on this line.

  2. If you're using Unix or a Unix-like operating system, check that your program is set to be executable (chmod a+x <file>).

  3. Are you sure your script compiles and runs? Test it locally (perl -c <file>), test it remotely. If it's a CGI script, check the error log.

Why doesn't my script work?

First of all it would be useful for you to tell us exactly how it doesn't work. Does it produce the wrong output? Does it not do what you want it to do?

  1. Turn on strict. Add the line use strict; under the she-bang line and fix the errors that creates.

  2. Turn on warnings. Add the line use warnings; and fix your code so no warnings are emitted.

  3. Use the perl debugger - perl -d, etc. to see where exactly it fails.

How should I write my code?

Well, to quote the omniscient perlbot:

  <perlbot> no strict no warnings no help. That's the #perl motto!

Make sure you add "use strict;" and "use warnings;" to your code. This is not the only thing you should do, though. Other good practices are:

  1. Use lexical file handles (open my $file_handle, ...) and three args open (open my $file_handle, ">", $filename instead of open my $file_handle ">$filename") and add trailing or's to your open statements (or alternatively use the "Fatal" or "autodie" modules.)

  2. When extracting arguments out of @ARGV or @_ , don't do subscripting of $ARGV[1] or $_[2] directly - instead use perldoc -f shift or my ($param1, $param2...) = @_;. The reason for that is that if you insert an argument in the middle, you'll need to update all indices.

    • For @ARGV also look at Getopt-Long and related modules.

  3. Don't abuse the default variable - $_. Use explicit variables instead whenever possible, and don't nest two $_-loops. While it should still be used in map(), grep(), etc. it should generally be avoided except for very small scripts.

  4. To subscript an array/hash reference, don't do something like ${$array_ref}[$index] (or worse - $$array_ref[$index] - instead use $array_ref->[$index] which is clearer and better. Note that if you want to take an array or hash slice, then you do need to use @{$array_ref}[@indexes] and that's ok.

    • Also note that the second and subsequent subscripts have optional arrows so: $array_ref->[$index1]->{$key1} can be written as $array_ref->[$index]{$key1}. The first arrow is not optional so you cannot say $array_ref[$index1] which belongs to the array @array_ref.

For more information refer to the “Perl Elements to avoid page” and to Damian Conway’s book Perl Best Practices

I need to parse HTML with Perl (and my Regular Expression does not work)

If you want to parse HTML with Perl, then regular expressions are probably not the answer. Instead look at:

Can I write Graphical User-Interfaces (GUIs) in Perl? And How?

Yes, you can. Here's how:

  1. Tkx - a cross-platform wrapper for Tcl/Tk, providing native look on every OS. Excellent tutorial at TkDocs.

  2. Perl/Tk - an old cross-platform toolkit, with a few limitations and a poor look and feel. Should be very flexible.

  3. wxPerl - a cross-platform GUI toolkit that uses the system's native controls.

  4. Perl/Gtk2 - a GUI toolkit primarily intended for UNIXes, but also works on Windows with a very non-native look. wxWidgets uses it on UNIXes.

  5. PerlQt - Perl using the Qt widget toolkit, on which KDE is based. Unmaintained.

How can I break/skip an outer loop?

If you have two nested loops:

   for my $person ($jack, $sophie, $alice, $bob, $eve)
   {
       for my $community ($hackers, $perlers, $australians)
       {
            $community->add_member($person);
       }
   }

And you want to break or skip an iteration out of the people loop in this case, then you can do a "last LABEL" or "next LABEL" feature, by labelling the method first:

   PERSONS:
   for my $person ($jack, $sophie, $alice, $bob, $eve)
   {
       for my $community ($hackers, $perlers, $australians)
       {
            if (! $community->add_member($person))
            {
                 warn "Could not add " . $person->name() . " to " . $community->name() . "\n";
                 next PERSONS; # Will skip out of the for my $person loop.
            }
       }
   }

Read perlsyn for more information.

How can I include a script into a different script and still see the lexical (“my”) variables declared there?

The short answer is that when a file is being do'ed, eval slurp($file)'ed, require'ed or use'ed it happens in a nested scope to the invoked one. As a result lexical variables (those declared with my) are inaccessible from the calling scope. The Perl 5 designers considered merely including the code of another file in another program directly a bad idea.

What you should do instead is either:

  1. Convert the included code to be a Perl module (a .pm file). Then use or require it.

  2. Invoke the perl script as a separate process if it's appropriate.

If you want to access the variables of a different file, you need to declare them using our or the use vars pragma. A more advisable way would be to control access to them using functions.

For more information see:

  1. Mark Jason Dominus' "Coping with Scoping"

  2. Randal L. Schwartz' "Variables and Scoping"

  3. A post to the Linux-IL mailing list explaining the difference between "local" and "my" by Shlomi Fish.

How can I format numbers with certain digits?

Use sprintf

How can I have printf return a value instead of outputting to a filehandle?

See sprintf. sprintf is like printf, only it returns the output as a string. Its use is preferable over printf most of the time. Note that its semantics are different than C's sprintf. In C you call it like:

    char buffer[1024];
    sprintf(buffer, "%d Days until %s's birthday.", days, name);

This is while hopefully making sure the buffer is large enough for the formatted string (see buffer overflow).

And in Perl you do:

  my $string = sprintf("%d Days until %s's birthday.", $days, $name);

Where else can I go for help?

You may find these other resources useful as well:

How do I know the index of the element in a foreach loop?

The short answer is that you don't. The obvious workaround is to write something like this:

   foreach my $idx (0 .. $#array)
   {
        my $elem = $array[$idx];

        # Do something with $elem and $idx
   }

The longer answer is that there are often better ways to do it.

Is there anyway to compile Perl code to an executable?

One option is PAR - The Perl Archiver, which prepares an .exe that contains all your code, ready to run. This way is is easier to deploy on platforms that don't have perl5, some of the Perl modules or extensions, or certain C libraries.

Alternatively, if you want to compile Perl code into machine code, then a partial solution was done by perlcc and the C:: namespace on CPAN. Note that it still includes the entire Perl run time (to handle string eval etc.), so it creates bloated executables.

How do I perform operations on entire directories (delete them, copy them, etc.)?

First of all see the File::Path module and see if it has what you want. It can delete an entire directory tree as well as do the equivalent of "mkdir -p". To traverse an entire directory tree, see File::Find and its equivalents..

To do the equivalent of "cp -R" (i.e: copy an entire directory tree recursively) in portable Perl, you'll either need to write something yourself or find a good CPAN module for that.

I want to run a shell command from Perl, and possibly trap its output. How?

  1. To run a single command with arguments without making use of shell behaviour, and without capturing the output, the best way is to use system(@args). You pass the system() function an array of parameters that encapsulate the executable name and its arguments. This makes sure special characters in the arguments are passed directly to the executable, without prior processing by the shell. The system("$str") notation, which is explained below has different semantics.

  2. To do the same while capturing the output, the best way is to use the open function like this:

       open my $cmd, '-|', $program_name, @args
            or die "Error in executing cmd. $!";
       # Read from $cmd with <$cmd> or whatever.
    
       .
       .
       .
       close($cmd);
    

    Note that this will not work in some older versions of perl5.

  3. If you do need to use special shell syntax (like pipelines, etc.) look at system("$string") or the qx{...}/`...` operators or the open "cmd|" syntax. However, be careful when interpolating arbitrary strings as arguments there, as if they are not properly escaped, they can be used to execute arbitrary shell code. Look at the String-ShellQuote module on the CPAN for a portable way to quote strings for passage as parameters to shell commands in such execution strings.

  4. exec() is similar to system() and probably does not do what you want as it overwrites the current execution process of your Perl script with the new process.

How can I Merge Two Hashes?

If you simply do my %total = (%first_hash, %second_hash);, then %total will contain all the keys and values of %second_hash, along with all the keys in %first_hash that whose values were not run over by identical keys of %second_hash.

Another option (which is probably slower) is to use:

   @into{keys(%from)} = values(%from);

This will merge %into in place.

If instead of completely overriding values, you want to make some combination of the values, you'll need to use something like that:

   while (my ($key, $from_value) = each(%from))
   {
        $into{$key} = exists($into{$key}) ? ACCUM_EXPR($key, $into{$key}, $from_value) : $from{$key};
   }

How can I match anything-except-a-regex inside a Perl regular expression?

The short answer is that negated regular expressions are not directly supported in Perl, and adding them would be a hard problem. If we take the string "bar" as an example then the negated regex will look something like "[^b]|b[^a]|ba[^r]", and so it isn't supported. One option to workaround this problem is to make a clever use of the =~ operator, by matching a string and then using this operator to check if it doesn't match a regular expression.

Should I learn Perl? Should I learn it as well as Python/Ruby/PHP/whatever?

See our “Learn Page Now” section.

Is Perl better than PHP, Python, Ruby, etc.?

See the above question.

Which method to do is faster? How can I optimise my Perl programs?

Questions such as this come out very often on #perl. The answers to them are that:

  1. You can always Benchmark and see. See the Benchmark module which is part of the core.

  2. Usually, the performance gained by doing micro-optimisations in Perl is not going to be worth the trouble.

  3. If you really need performance for certain tasks that take too long, then you should probably look at a highly optimised solution written in a compiled-to-machine-code language. See Perl's XS or the Inline-C module for more information. However, going to this extreme is usually not what most people who ask us about performance want or need.

The legendary computer scientist Donald Knuth has quoted C.A.R. Hoare in saying that "Premature Optimisation is the Root of All Evil", so it's worth paying attention to. (More discussion on the link).

Oh no! CPAN won't use the packages it has downloaded! It says the checksums are invalid!

There are two common reasons for this happening:

  1. You have a dodgy mirror. Try a different mirror and see if it clears up.

  2. For whatever reason, the file is being downloaded and saved as .tar (but still very much compressed). The file gets recompressed and as such the checksum operation fails to produce a correct checksum.

For the latter, there is a relatively simple solution - move the .tar files to .tar.gz and rerun. Aaron_Mason wrote a script that runs CPAN with the parameters you seek, then check for any stray .tar files with a corresponding .tar.gz and move the .tar over the .tar.gz. This solution is far from foolproof, but got through a CPAN upgrade. The script is as follows:

#!/usr/local/bin/bash

>cpan-cycle.$$

moved=1

while [ $moved -eq 1 ]; do

        cpan $@

        moved=0

        find ~/.cpan -name \*.tar > cpan-cycle.$$

        while read line; do
                if [ -e "${line}.gz" ]; then
                        mv "${line}" "${line}.gz"
                        moved=1
                fi
        done <cpan-cycle.$$
done

rm cpan-cycle.$$

How can I search for a value inside an array?

See the perlfaq question "How can I tell whether a certain element is contained in a list or array?" for an exhaustive discussion of this topic. One final correction to it is that "any" from List::Util would sometimes be more preferable to "first", either to better convey the meaning that one is not interested in the value itself, but for its existence, and in the case that one is specifically looking for an undef() value.

Also see this discussion on Gábor Szabó's "Perl 6 Tricks and Treats" newsletter (and the link to the Israeli Perl Mongers discussion) for further discussion.

How can I prepare a self-contained Perl executable?

Look at PAR (the Perl archiver) or commercial alternatives such as perl2exe. They package the perl interpreter, the necessary perl modules, shared libraries, etc. into a self-contained .exe or .zip.

How can I compile my code to obscure / obfuscate the source?

The perl interpreter must know the source code of the program in order to execute it. As a result, there's no sure-fire way to obfuscate your source code. There also is a Perl-to-C compiler that could compile the C code to binaries, but it's no longer actively maintained.

See PAR::Filter::Obfuscate, PAR::Filter::Crypto, Filter::Crypto::CryptFile, Module::Crypt, B::C on CPAN.

When is Perl 6 going to be ready?

The common answer to this question is "Perl 6 will be ready by Christmas. But we don't know which one.". Seriously now. Most of the active development on a Perl 6 compiler started with Pugs (the "Perl 6 Users Golfing System") which was based on Haskell and GHC, and proved to be an evolutionary dead end. The Pugs project still gave important deliverables such as a comprehensive test suite. Nowadays most work on the Perl 6 compilers concentrates on Rakudo.

Rakudo is progressing nicely, but still has many lacking features, some bugs and also performs very poorly in comparison to perl 5. As a result, it is not recommended to use it for production, and you should instead use perl 5 (or a different mature language if that's your cup of tea).

All that put aside, Rakudo can already be used to experiment with Perl 6 and to play with it.

We have some jokes about the Perl 6 development process in Perl Humour.

Perl 6 is going to be so different from Perl 5. Should I still learn Perl 5?

Yes. Perl 6 is not going to make Perl 5 obsolete. It is a different language in the same lineage as Perl 5. The Perl 5 language and the perl5 implementation (soon to be at version perl-5.16), are actively used, and actively enhanced and developed. As Perl 6 is a different language, perl 5 will continue to see active use and development, even while Perl 6 matures and becomes usable. Perl 5 is not going away.

At the moment, the Perl 6 implementations are not ready for use in production (due to incompleteness, poor performance and other factors), and if you wish to use any of these two (quite different) languages called "Perl" in production, then Perl 5 is still the sanest choice.

How can I match a string variable inside a regex?

First of all one should note that regular expressions interpolate, so you can just say:

   my $needle = "Perl";
   my $string = "We love programming in Perl, and we program in it a lot."

   if ($string =~ /$needle/)
   {
       # Success
   }

And it will work. However, this will also insert special regular expression characters inside the regular expression, so if you have special characters like ".", "*", "+", "[" , "]", "{","?", etc. they can disrupt the match (or even open your code to security problems). To resolve this, you should refer to perldoc -f quotemeta, which allows quoting it and to the \Q and \E escapes inside regexes which allows quoting them with convenience. So our code becomes:

   if ($string =~ /\Q$needle\E/)

Note that if you're interested in searching for a simple substring, then there are perldoc -f index and perldoc -f rindex for that which are faster than a regular expression match and potentially safer.

Where can I find lists of Programming Challenges?

See the list.

Where can I find an interactive version of Perl? (a.k.a a REPL - Read-Eval-Print-Loop)

The best REPL is probably Devel-REPL (a.k.a "re.pl") which is actively maintained, feature rich and very polished. The perl -d flag can also serve as a rudimentary REPL.

How can I store Perl Data Structures on the Hard Disk?

Sometimes we'd like to store arbitrary Perl 5 Data Structures on the hard disk for various reasons including persistence and communication. This is called serialisation (or serialization in American spelling), and can be performed in several ways.

If you only wish to explicitly store and load all the data from memory to disk (or vice versa), you have the following options:

  1. Data-Dumper allows one to serialise arbitrary data structures into a human readable Perl code (which is also capable of evaluation). Note that performing string eval for arbitrary Perl code like that is not recommended for security reasons, so make sure you can trust that data.

  2. Storable (another core module) serialises data structures to a binary format (make sure you use its portable format), and allows for serialising, deserialising, cloning a complex data structure, etc. This module is Perl 5-specific, though.

  3. Sereal - a more modern binary serialisation system.

  4. JSON is short for "JavaScript Object Notation" and can be used to serialise data structures using JS notation. The JSON-MaybeXS CPAN module is recommended for that. Note that JavaScript does not support several Perl 5-specific features such as having a reference to a scalar.

  5. YAML is a data encoding language (see YAML-XS) for represnting complex data structures. It is more complex than JSON, and more error-prone but also gives some user-friendly enhancements.

If you want random lookup, and possibly arbitrary on disk alteration, look at the various embedded database solutions, such as:

How can I do a switch statement in Perl?

perl-5.10.x and later have a given/when statement. It is not available for perl-5.8.x and below.

There's also Switch.pm for earlier versions, but its use is not recommended because it's a source filter and tends to break a lot.

Please consider using dispatch tables or if/elsif chains if you cannot use perl-5.10.x.

How can I determine the type of a reference (array, hash, scalar, etc.)?

As is often the case, the answer is not as straightforward as one can expect. Part of the problem is that references can be blessed:

   my $ref = {};
   bless $ref, "MyPackage";
   # Will print MyPackage.
   print 'ref($ref) = ', ref($ref), "\n";

The built-in function perldoc -f ref demonstrated above will return the most basic class (in the OOP sense) that the reference is associated with (or its type if it isn't.). If you control the input and don't expect an edge case where it is was blessed, this would be OK.

If you want to find the most basic type of the reference, you should use Scalar::Util's reftype() function. Scalar::Util is a core module since perl-5.8.0, and is easily installable from CPAN for several older perls. (Which you really should avoid using).

Finally if you want to determine if an object reference belongs to a certain class you should use the ->isa() built-in-method (from the UNIVERSAL class which is the implicit parent class of all other classes.). There has been some criticism against using such notations as:

  if (UNIVERSAL::isa($ref))
  {
  }

See for example UNIVERSAL-isa on CPAN.

That put aside, since Perl 5 (and Perl 6) is a symbolic language, one can call a method by name on every object that supports it regardless of its actually inheritance geneology. So using ->isa may be a red flag in Perl, and you should rethink your strategy.

How can I parse a CSV (Comma separated value / comma delimited value) file?

CSV parsing is very hard to get right using plain regexes, so please use Text-CSV_XS or Text-CSV.

How can I access the global variables in my script from a different module and/or namespace?

There is a way to do it, but you really shouldn't do it this way because it's most likely indicative of a bad design. Instead, pass the data to the module (preferably by defining a class (see Moose and other resources), instantiating an object and let it run. You can also implement an entire command line application as a module and a short, bootstrapping, script that invokes it.

How can I set up a timeout for a blocking operation?

See the "alarm()" perl built-in or multitasking frameworks for more advanced uses.

How can I match (and possibly replace) the Nth expression out of a string?

Perl 5 does not provide a utilitarian built-in for doing that, but it is easy to do in several ways. The best way is probably to count the matches and replace them:

   my $string = "Hello Hi Howdy Hoorah Hallelujah";

   {
       my $count = 0;
       $string =~ s{(H)}{(++$count == 3)?? "G"?: $1}ge;
   }

You can also try using \g and /g or alternatively match the expression n-1 times and then replace it:

  my $pat = qr/H/;
  $string =~ s{((?:$pat.*?){2})$pat}{${1}G};

How do I prepend data to a file, take out/delete data from the middle of the file or insert data into the middle of the file?

Files in UNIX can be thought of as big sequences of octets, and as such it is possible to overwrite existing bytes with new ones, to append to the end of the file, or to truncate a file from a position to the end (to create a file which is shorter than the existing one.). However, it's not easily possible to prepend data to the beginning of the file, to take out/delete data from the middle of the file or to insert it into the middle of the file while only using a single file handle to the file.

The best way to achieve what you want with such operations is to write a new file with the modified content, then close both files and move the new file on top of the existing files using rename. Here is an example for how to do a prepend:

   #!/usr/bin/perl

   use strict;
   use warnings;

   my $filename = shift(@ARGV);
   my $new_fn = $filename . '.new';

   open my $in, '<', $filename
       or die "Cannot open $filename for reading - $!";
   open my $out, '>', $new_fn
       or die "Cannot open $new_fn for writing - $!";

   print {$out} <<'EOF';
   ENTHDR|1|3.0
   STAGEHDR|Barcoded
   EOF

   while (my $line = <$in>)
   {
       print {$out} $line;
   }

   close($out);
   close($in);

   rename($new_fn, $filename);

You can also look at higher-level abstractions such as Tie::File or IO-All for more idiomatic ways to achieve the same thing.

How do I pass a reference to an object's method? (Also known as class subroutine or member function)

You have several options to pass a reference to a method, which differ in their syntax and semantics. Here is the list:

  1. The first method is to do sub { return $self->my_method(@_); } within a scope that has the object reference $self valid. This will always call my_method on the physical $self reference.

  2. You can pass the name of the method as a string and then do $some_obj->$some_method(@args). This requires applying $some_method on an object explicitly and handles inheritance well.

How do I trim leading and/or trailing whitespace from a string?

Some languages provide a built-in function to remove leading and/or trailing whitespace from a string (such as PHP's trim. Perl does not have it because it is fairly trivial to do using the s/// built-in (see our resources about regular expressions and there's also String-Trim on CPAN.

How do I add directories to the list of paths where Perl searches for modules? (@INC)

There are several ways to do it:

  1. 'use lib' perldoc lib.

  2. use the -I option to perl perldoc perlrun.

  3. Set the PERLLIB or PERL5LIB environment variable perldoc perlrun

  4. Finally you may opt to recompile perl.

After that, you can add use MyModule; to your code, and it will work. Please don't resort to do "/full/path/to/include.pl" games.

How can I get perl to print each line as it is executed (similar to the shell's -x flag)?

To do that, use Devel-Trace from CPAN , and add the -d:Trace flag.

You get an output like:

   shlomif[cfg-ini]:$module$ perl -d:Trace tag-release.pl 2>&1 | head -10
   >> tag-release.pl:9:     (map { m{\$VERSION *= *'([^']+)'}?? ($1)?: () }
   >> /usr/lib/perl5/vendor_perl/5.12.1/IO/All/Base.pm:44:         my $self = $class->new(@_);
   >> /usr/lib/perl5/vendor_perl/5.12.1/IO/All.pm:94:     my $self = shift;
   >> /usr/lib/perl5/vendor_perl/5.12.1/IO/All.pm:95:     my $package = ref($self) || $self;
   >> /usr/lib/perl5/vendor_perl/5.12.1/IO/All.pm:96:     my $new = bless Symbol::gensym(), $package;
   >> /usr/lib/perl5/5.12.3/Symbol.pm:101:     my $name = "GEN" . $genseq++;
   >> /usr/lib/perl5/5.12.3/Symbol.pm:102:     my $ref = \*{$genpkg . $name};
   >> /usr/lib/perl5/5.12.3/Symbol.pm:102:     my $ref = \*{$genpkg . $name};
   >> /usr/lib/perl5/5.12.3/Symbol.pm:103:     delete $$genpkg{$name};
   >> /usr/lib/perl5/5.12.3/Symbol.pm:104:     $ref;

I'm trying to use threads to speed up my program. What should I know?

Before you start implementing code using Perl's threads, please remember that Perl's threads are not like other threads. Please see perlthrtut to learn more, specifically its "What kind of threads are perl threads?" section. Also see the document "Things you need to know before programming Perl ithreads".

Note that threads don't work too well in most C-hosted languages similar to Perl, such as Python or Ruby, and we recommend looking into a different language for that. Some people believe that threads should be avoided in any programming language (see for example Eric Raymond's opinion about it in the book The Art of Unix Programming), but many people believe threads have many legitimate uses, when done properly. See also this discussion on the Perl beginners mailing list.

Should I be using mod_perl?

The short answer is: "Most probably not.".

mod_perl 1 was once the only sane way to run Perl scripts quickly on UNIX systems, however, it made a conscious choice of providing a lot of extra functionality, which proved to cost with complexity and sometimes quirkiness. Since then, FastCGI and similar technologies have matured (possibly legally) and there is now a new crop of popular web-servers, which aim to be very speedy (as well as less customisable and less suitable for hosting providers) such as Cherokee, lighttpd or nginx that can utilise FastCGI as well, and you may opt to use instead of Apache, assuming your hosting allows that.

So even if you're using Apache (and you should definitely use Apache 2 - not Apache 1), there are better alternatives for speeding up your applications than mod_perl, and you should only use mod_perl if you need more sophisticated control and functionality of Apache 2, in which case you may wish to also be asking that on the mod_perl mailing list, or on a similar forum, and you are expected to be an advanced user.

How can I find which modules a Perl program or module depends on?

If you wish to use it on an program you have on the disk at run-time, then use Devel-TraceUse, which will report the used modules recursively.

One can also use the CPAN dependencies tool to determine the dependencies of a CPAN distribution or module.

What is the difference between an array and an array reference? What about a hash vs. a hash reference?

There are several differences between arrays and array references. Array references are scalars and can be stores inside other arrays or hashes to create nested or composite data structures. Arrays have a more convenient syntax. Arrays and scalars are accessed differently:

  1. One uses $my_array[$idx] to access an element of an array, and $my_array_ref->[$idx] to access an element of an array reference (or alternatively the more cluttered $$my_array_ref[$idx] or ${$my_array_ref}[$idx])

  2. One uses @my_array to access the array as a whole and @$my_array_ref to access the array referred to by the array reference as a whole.

For more information, see some resources about references in Perl.

What is the difference between the perl's command line "-e" (lowercase "e") and "-E" (uppercase "E") flags?

See "perldoc perlrun"’s description of both flags, as well as the feature pragma.

Why does «perl -E 'if ("foo" == "bar") { say "true"; }'» prints "true"? (String vs numerical comparison)

Perl has separate sets of operators for numerical vs string comparisons. As a result, "foo" == bar" is the same as 0 == 0 while ("foo" eq "bar") returns false as expected. This is in part due to the Perl design philosophy which dislikes having polymorphic operators for built-in types.

#perlcafe

The Perl Café exists to move off-topic discussions out of #perl. The channel is very lax, but often heavy on the irony. Examples of conversation include:

  1. Café-speak ("teh hawt", "I buyed", "teh ghey", "n u") — you get the idea.

  2. People discussing their love-lives (or their lack of them), their co-workers, their friends, and their enemies.

  3. Unusual humour and lots of cursing (particularly at each other).

  4. Discussing both obscure and everyday political issues.

  5. Personal conversation during what would otherwise be a boring work day.

Sometimes the discussion gets more high-quality, but it's still a channel where chat occurs that some may find questionable. Viewer discretion is advised.

Also see #perlcafe ’s own page.

#raku (previously "#perl6")

Discussion for the development of the Raku programming language (which used to be called "Perl 6"), and Rakudo, and as usual on Freenode, a lot of off-topic discussion. Don't ask Perl 5 technical questions there - ask them on #perl.

#lispcafe

#lispcafe is an off-shoot of #lisp and #perlcafe. The primary lisp channel #lisp is restricted to "on-topic" discussion of Common Lisp. Thus #lispcafe was founded to accept off-topic discussions. Unlike #perlcafe, #lispcafe attempts to be more civil. Discussions of Perl and other dynamic languages is acceptable on #lispcafe, so you might wish to visit it, introduce yourself and join the fun.

  1. lispcafe has its own page on the Common Lisp wiki.

#perl-cats

Since many #perl'ers either like cats or even sometimes emulate ones, a channel was started for all things cats and Perl. It is pretty small, and we discuss many things besides cats. Some people there are just friends of regular #perl'ers (who also happen to like Perl). #perl-cats tends to be more civil than #perlcafe , but expect a lot of cat overload.

People

Note: by matter of a policy, a person may only add and edit their own entries, unless it is a cosmetic change. This was instated to avoid spreading mis-information and to prevent defamation.

Channel Owner

thrig

The freenode #perl channel owner is thrig, also known as Jeremy Mates. He holds the channel password and maintains the list of channel operators.

Aside from that, thrig maintains the sial.org domain with many useful resources for the channel or for Perl in general including a (now defunct) paste bot, and "Life with CPAN".

Channel Operators

apeiron

He's just some guy, you know? apeiron is a USian with dreams of moving to Europe. As with most people and bots depicted here, Perl is his favorite language. Of the INTJ personality type, he tends to focus more on solving problems than being the center of attention. Insatiably curious, he enjoys playing with new frameworks and applications. His interests are in free culture, free software, science, history, anthropology, music, philosophy, Shakespeare, and many other things. To be brief: a geek and a hippie.

beth

beth lives in Ithaca, New York. She works with Perl at Cornell university as a bioinformatics analyst and is one of #perl's resident gurus.

Chris62vw

Chris is an American living in Las Vegas. He's the author and maintainer of ChrisBot, which was the codebase behind "perlbot", our resident infobot. He is rather hands-off when it comes to policing the channel and believes in minimal usage of operator privileges. In his spare time he enjoys spending time at the shooting range, eating sushi, and drinking beer with his friends.

f00li5h

I am he. While coding I like to have a candle burning, and the sun setting. I try to treat IRC as BitTorrent for programming knowledge and am often looking for short term perl contracts, ideally telecommute. You may find I behave like a cat from time to time, this is a desired behaviour and not a bug.

mst

mst is the project founder of DBIx::Class, member of the Catalyst core team and lead tech at Shadowcat Systems. He's also a sarcastic obnoxious english guy with a very dry sense of humour. Also known as "wielder of the chainsaw" for his high impact LARTing technique when somebody isn't paying attention to the people trying to help them.

Paladin

Paladin is a Canadian, living currently in New Westminster, BC. He is only around sporadically depending on the pressures of Real Life.

simcop2387

simcop2387 is an American currently living in Georgia (the state, not the country). Perl is his favorite language and he spends most of his time coding strange esoteric projects in perl.

Channel Regulars

ivantis[^3]*

Awesome perl cat (mew). Living in Lawrence, KS. Enjoys freelance programming for Perl and PHP (well at least the perl part, who enjoys PHP?). Keeps #perl stats.

alanhaggai

alanhaggai is a programmer and web developer who got converted to Perl. He maintains a website where he experiments with new CSS designs. He also maintains a use Perl journal.

dazjorz

dazjorz is a Perl programmer by the name of Sjors Gielen. He mainly programs servers. His bot is DaZeus, but it's not in #perl, to his deep disappointment. His personal web site is at http://dazjorz.com/.

pkrumins

pkrumins loves mathematics, physics and computer science. He has a blog titled Good Coders Code - Great Reuse.

whoppix

whoppix considers himself to be a rather average perl programmer. He does not have a livejournal, a blog, a website, a twitter account, or almost anything else Web-2.0 related. He is not at all interested and/or skilled in webdesign, relational databases and/or anything derived thereof. whoppix enjoys tinkering around with higher-level languages like Perl, Haskell, and sometimes writes implementations for simple esoteric languages, or even his own grammars (a task he regularly fails badly at). He also has some GTK+ experience, and is generally interested in event-based programming.

In his spare time (when he's not programming) whoppix plays the piano and paints (and sometimes sells) sarcastic and/or post-ironic pictures, that reflect the dark gaping abyss of frustration in his soul. Or something like that.

rindolf

rindolf, also known as Shlomi Fish, or "shlomif", is an Israeli programmer born in 1977. He tries to be as helpful and friendly as possible and often is too friendly. If he expresses interest in you, either just answer his questions, or tell him to stop.

Shlomi has many interests including: programming and software development, open-source use, development and advocacy, creative writing, applied philosophy and writing essays, biking, science, logic and applied logic, cognitive psychology, mathematics and puzzles, linguistics, history of the Old World, etc. He has a Bachelor of Science from the Technion in Electrical Engineering, but thinks it would be a stretch to call him an electrical engineer.

Perl is his favourite language. Among his contributions to the Perl world are:

  1. The Perl Beginners' Site

  2. Perl for Newbies - a series of presentations with free onlne slides, introducing the core Perl language to beginners.

  3. Some CPAN Modules

He prides himself in being the “Black Sheep” of the Perl world, due to writing some controversial essays, and because he is considered a “Persona Non-grata” in some Perl communities.

Bots

perlbot

perlbot is the channel's infobot and utility bot. It only answers if addressed. Type "perlbot: perl" to learn about "perl", "perlbot: re" to learn about "re", etc. Originally Chris62vw's bot with the codebase in Chrisbot, it is now managed by simcop2387 and based on the superior buubot source (type "perlbot: source").

buubot

Another infobot written in perl, this time with plugins. buu wrote it and the source is also available and open-source.

CPAN2

(Deprecated (banned), replaced by GumbyPAN) An announcement bot for the CPAN - the Comprehensive Perl Archive Network. Also can be queried. Its daddy is Zoffix.

GumbyPAN

See CPAN2.

GumbyBRAIN

GumbyBRAIN has recently left #perl, because he's noisy and not especially helpful for perl programmers.

f00li5h> GumbyBRAIN: what do you want me to say about
you in the wiki?
GumbyBRAIN> I was wondering if there are reportadly some
non-drm songs, it's like bla bla bla bla and no you really
want do.

GumbyBRAIN is BinGOs' experimental megahal bot and not an infobot (despite appearances to the contrary). Source at http://www.gumbybrain.com/

farnsworth

Yet another bot written in perl created for testing the Math::Farnsworth module created by simcop2387, it features a Turing-complete programming language that rules all of its "commands". Its primary function is for doing math with unit tracking and conversions, but it will also do language conversions, and date manipulation also.

perlbot2

ivantis' home-made bot, written in perl (gu-duh!). Is currently banned by mst for being obnoxious, but may take his revenge soon! Replies to commands by name or by prefix character (&).

f00bot

f00li5h's useless instance of buubot. It's safe to ignore him.

ivantis3[`_]*

ivantis' log-keeping bot. He doesn't do much.

Our Milestone Pictures

Here are our milestone pictures:

Share/Bookmark

Footer