Dual-license your content for inclusion in The Perl 5 Wiki using this HOWTO, or join us for a chat on irc.freenode.net#PerlNet.
Freenode Sharp Perl FAQ
From PerlNet
This is the FAQ for Freenode's #perl channel.
People
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 the paste bot, and "Life with CPAN".
Channel Operators
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 is 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. I am often looking for short term perl contracts, ideally telecommute
Paladin
Paladin is a Canadian, living currently in New Westminster, BC. He is only around sporadically depending on the pressures of Real Life.
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:
- The Perl Beginners' Site
- Perl for Newbies - a series of presentations with free onlne slides, introducing the core Perl language to beginners.
- 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.
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
alanhaggai
alanhaggai is a programmer and web developer who got converted to Perl. He maintains a website at http://alanhaggai.org/ 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.
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. It is Chris62vw's bot and the codebase is in Chrisbot (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
An announcement bot for the CPAN - the Comprehensive Perl Archive Network. Also can be queried. Its daddy is Zoffix.
GumbyBRAIN
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
Commonly Asked Questions
What are the Rules of Conduct?
- Don't flood the channel with your paste. Instead use a pastebot - type perlbot paste.
- Be civil and friendly.
- 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.
- Whenever someone helps you then:
- Thank them!
- Bump their karma. For example if johndoe helps you then say johndoe++.
- 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.
- 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 #buubot.
I've become an Operator. What should I do now?
- Don't remain an operator for too long. Become an operator only to change the topic or to kick or 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.
- You should kick someone only if:
- They are flooding the channel.
- They have been warned about being annoying or abusive at least twice, and still continue. Do not randomly kick people without warning them. We don't want #perl to be turned into another EF-Net.
- You should ban someone only if:
- They continuously join after being kicked several times.
- They are spammers who spam the channel with ads or other things like that.
- 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.
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.
- On asking for help
- Why questions go unanswered
- How to ask questions the smart way
- How (Not) To Ask A Question
What is Considered On-Topic?
Almost everything, but not all. Things which are frowned upon are:
- Overly graphical depictions of pornography or violence.
- Annoying trolling, preaching or sex advice.
- Flooding the channel with large pastes.
- Too much bot-playing.
- 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:
- Perl. (Naturally).
- Computing in general. (UNIX, Linux, Mac OS X, history of computing, etc.)
- Other programming languages - C and C++; Perl alternatives like PHP, Python or Ruby; Lisp and Scheme; Haskell, O'Caml, etc.; Assembler; and more exotic languages.
- We still try to focus on Perl, though.
- Geography, culture, linguistics, languages, etc.
- Science.
- Philosophy.
- Popular Culture
- Etc.
The channel #perlcafe was an attempt to move off-topic and advanced discussion out of #perl. However, it didn't work, but OTOH, #perlcafe now hosts a lot of discussion that we feel should better be kept out of #perl.
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. To quote Aesop: "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:
- 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.
- If you're using Unix or a Unix-like operating system, check that your program is set to be executable (chmod a+x <file>).
- 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 to do?
- Turn on warnings. Add a -w to your she-bang line #!/usr/bin/perl -w and fix your code so no warnings are emitted.
- Turn on strict. Add the line use strict; under the she-bang line and fix the errors that creates.
- Use the perl debugger - perl -d, etc. to see where exactly it fails.
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:
- HTML::TokeParser - a basic tokenising HTML parser.
- HTML::TreeBuilder - an abstraction for searching the HTML document as a tree.
- XML::LibXML - has an HTML Parser with a DOM/XPath interface.
Can I write Graphical User-Interfaces (GUIs) in Perl? And How?
Yes, you can. Here's how:
- Perl/Tk - an old cross-platform toolkit, with a few limitations and a poor look and feel. Should be very flexible.
- wxPerl - a cross-platform GUI toolkit that uses the system's native controls.
- 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.
- 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:
- Convert the included code to be a Perl module (a .pm file). Then use or require it.
- Invoke the perl script as a separte 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:
- Mark Jason Dominus' "Coping with Scoping"
- Randal L. Schwartz' "Variables and Scoping"
- 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 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 do:
char buffer[1024]; sprintf(buffer, "%d Days until %s's birthday.", days, name);
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 smart 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 Why Learn Perl.
Is Perl better than PHP, Python, Ruby, etc.?
See the above question.
Which method to do $TASK_X 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:
- You can always Benchmark and see. See the cpan:Benchmark module which is part of the core.
- Usually, the performance gained by doing micro-optimisations in Perl are not going to be worth the trouble.
- 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 cpan:Inline-C 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 wikipedia: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).
Some Related IRC Channels on Freenode
#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:
- Café-speak ("teh hawt", "I buyed", "teh ghey", "n u") — you get the idea.
- People discussing their love-lives (or their lack of them), their co-workers, their friends, and their enemies.
- Unusual humour and lots of cursing (particularly at each other).
- Discussing both obscure and everyday political issues.
- 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 its own page at Freenode Sharp Perlcafe.
#perl6
Discussion for the development of Perl 6 and Pugs, 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.
- lispcafe has its own page on the Common Lisp wiki.
Our Milestone Pictures
Here are our milestone pictures:

