<LI><A HREF="#how do i do (anything)">How do I do (anything)?</A></LI>
<LI><A HREF="#how can i use perl interactively">How can I use Perl interactively?</A></LI>
<LI><A HREF="#is there a perl shell">Is there a Perl shell?</A></LI>
<LI><A HREF="#how do i debug my perl programs">How do I debug my Perl programs?</A></LI>
<LI><A HREF="#how do i profile my perl programs">How do I profile my Perl programs?</A></LI>
<LI><A HREF="#how do i crossreference my perl programs">How do I cross-reference my Perl programs?</A></LI>
<LI><A HREF="#is there a prettyprinter (formatter) for perl">Is there a pretty-printer (formatter) for Perl?</A></LI>
<LI><A HREF="#is there a ctags for perl">Is there a ctags for Perl?</A></LI>
<LI><A HREF="#is there an ide or windows perl editor">Is there an IDE or Windows Perl Editor?</A></LI>
<LI><A HREF="#where can i get perl macros for vi">Where can I get Perl macros for vi?</A></LI>
<LI><A HREF="#where can i get perlmode for emacs">Where can I get perl-mode for emacs?</A></LI>
<LI><A HREF="#how can i use curses with perl">How can I use curses with Perl?</A></LI>
<LI><A HREF="#how can i use x or tk with perl">How can I use X or Tk with Perl?</A></LI>
<LI><A HREF="#how can i generate simple menus without using cgi or tk">How can I generate simple menus without using CGI or Tk?</A></LI>
<LI><A HREF="#what is undump">What is undump?</A></LI>
<LI><A HREF="#how can i make my perl program run faster">How can I make my Perl program run faster?</A></LI>
<LI><A HREF="#how can i make my perl program take less memory">How can I make my Perl program take less memory?</A></LI>
<LI><A HREF="#is it unsafe to return a pointer to local data">Is it unsafe to return a pointer to local data?</A></LI>
<LI><A HREF="#how can i free an array or hash so my program shrinks">How can I free an array or hash so my program shrinks?</A></LI>
<LI><A HREF="#how can i make my cgi script more efficient">How can I make my CGI script more efficient?</A></LI>
<LI><A HREF="#how can i hide the source for my perl program">How can I hide the source for my Perl program?</A></LI>
<LI><A HREF="#how can i compile my perl program into byte code or c">How can I compile my Perl program into byte code or C?</A></LI>
<LI><A HREF="#how can i compile perl into java">How can I compile Perl into Java?</A></LI>
<LI><A HREF="#how can i get #!perl to work on [msdos,nt,...]">How can I get <CODE>#!perl</CODE> to work on [MS-DOS,NT,...]?</A></LI>
<LI><A HREF="#can i write useful perl programs on the command line">Can I write useful Perl programs on the command line?</A></LI>
<LI><A HREF="#why don't perl oneliners work on my dos/mac/vms system">Why don't Perl one-liners work on my DOS/Mac/VMS system?</A></LI>
<LI><A HREF="#where can i learn about cgi or web programming in perl">Where can I learn about CGI or Web programming in Perl?</A></LI>
<LI><A HREF="#where can i learn about objectoriented perl programming">Where can I learn about object-oriented Perl programming?</A></LI>
<LI><A HREF="#where can i learn about linking c with perl [h2xs, xsubpp]">Where can I learn about linking C with Perl? [h2xs, xsubpp]</A></LI>
<LI><A HREF="#i've read perlembed, perlguts, etc., but i can't embed perl in">I've read perlembed, perlguts, etc., but I can't embed perl in</A></LI>
<LI><A HREF="#when i tried to run my script, i got this message. what does it">When I tried to run my script, I got this message. What does it</A></LI>
Various <A HREF="http://www.perl.com/CPAN/doc/FMTEYEWTK/index.html">http://www.perl.com/CPAN/doc/FMTEYEWTK/index.html</A>
(not a man-page but still useful)</PRE>
<P>A crude table of contents for the Perl man page set is found in <A HREF="../../lib/Pod/perltoc.html">the perltoc manpage</A>.</P>
<P>
<H2><A NAME="how can i use perl interactively">How can I use Perl interactively?</A></H2>
<P>The typical approach uses the Perl debugger, described in the
<CODE>perldebug(1)</CODE> man page, on an ``empty'' program, like this:</P>
<PRE>
perl -de 42</PRE>
<P>Now just type in any legal Perl code, and it will be immediately
evaluated. You can also examine the symbol table, get stack
backtraces, check variable values, set breakpoints, and other
operations typically found in symbolic debuggers.</P>
<P>
<H2><A NAME="is there a perl shell">Is there a Perl shell?</A></H2>
<P>In general, no. The Shell.pm module (distributed with Perl) makes
Perl try commands which aren't part of the Perl language as shell
commands. perlsh from the source distribution is simplistic and
uninteresting, but may still be what you want.</P>
<P>
<H2><A NAME="how do i debug my perl programs">How do I debug my Perl programs?</A></H2>
<P>Have you tried <CODE>use warnings</CODE> or used <CODE>-w</CODE>? They enable warnings
for dubious practices.</P>
<P>Have you tried <CODE>use strict</CODE>? It prevents you from using symbolic
references, makes you predeclare any subroutines that you call as bare
words, and (probably most importantly) forces you to predeclare your
variables with <A HREF="../../lib/Pod/perlfunc.html#item_my"><CODE>my</CODE></A> or <A HREF="../../lib/Pod/perlfunc.html#item_our"><CODE>our</CODE></A> or <CODE>use vars</CODE>.</P>
<P>Did you check the returns of each and every system call? The operating
system (and thus Perl) tells you whether they worked or not, and if not
why.</P>
<PRE>
open(FH, "> /etc/cantwrite")
or die "Couldn't write to /etc/cantwrite: $!\n";</PRE>
<P>Did you read <A HREF="../../lib/Pod/perltrap.html">the perltrap manpage</A>? It's full of gotchas for old and new Perl
programmers, and even has sections for those of you who are upgrading
from languages like <EM>awk</EM> and <EM>C</EM>.</P>
<P>Have you tried the Perl debugger, described in <A HREF="../../lib/Pod/perldebug.html">the perldebug manpage</A>? You can
step through your program and see what it's doing and thus work out
why what it's doing isn't what it should be doing.</P>
<P>
<H2><A NAME="how do i profile my perl programs">How do I profile my Perl programs?</A></H2>
<P>You should get the Devel::DProf module from CPAN, and also use
Benchmark.pm from the standard distribution. Benchmark lets you time
specific portions of your code, while Devel::DProf gives detailed
breakdowns of where your code spends its time.</P>
<P>Here's a sample use of Benchmark:</P>
<PRE>
use Benchmark;</PRE>
<PRE>
@junk = `cat /etc/motd`;
$count = 10_000;</PRE>
<PRE>
timethese($count, {
'map' => sub { my @a = @junk;
map { s/a/b/ } @a;
return @a
},
'for' => sub { my @a = @junk;
local $_;
for (@a) { s/a/b/ };
return @a },
});</PRE>
<P>This is what it prints (on one machine--your results will be dependent
on your hardware, operating system, and the load on your machine):</P>
<PRE>
Benchmark: timing 10000 iterations of for, map...
for: 4 secs ( 3.97 usr 0.01 sys = 3.98 cpu)
map: 6 secs ( 4.97 usr 0.00 sys = 4.97 cpu)</PRE>
<P>Be aware that a good benchmark is very hard to write. It only tests the
data you give it, and really proves little about differing complexities
of contrasting algorithms.</P>
<P>
<H2><A NAME="how do i crossreference my perl programs">How do I cross-reference my Perl programs?</A></H2>
<P>The B::Xref module, shipped with the new, alpha-release Perl compiler
(not the general distribution prior to the 5.005 release), can be used
to generate cross-reference reports for Perl programs.</P>
<PRE>
perl -MO=Xref[,OPTIONS] scriptname.plx</PRE>
<P>
<H2><A NAME="is there a prettyprinter (formatter) for perl">Is there a pretty-printer (formatter) for Perl?</A></H2>
<P>There is no program that will reformat Perl as much as <CODE>indent(1)</CODE> does
for C. The complex feedback between the scanner and the parser (this
feedback is what confuses the vgrind and emacs programs) makes it
challenging at best to write a stand-alone Perl parser.</P>
<P>Of course, if you simply follow the guidelines in <A HREF="../../lib/Pod/perlstyle.html">the perlstyle manpage</A>, you
shouldn't need to reformat. The habit of formatting your code as you
write it will help prevent bugs. Your editor can and should help you
with this. The perl-mode for emacs can provide a remarkable amount of
help with most (but not all) code, and even less programmable editors
can provide significant assistance. Tom swears by the following
settings in vi and its clones:</P>
<PRE>
set ai sw=4
map! ^O {^M}^[O^T</PRE>
<P>Now put that in your <EM>.exrc</EM> file (replacing the caret characters
with control characters) and away you go. In insert mode, ^T is
for indenting, ^D is for undenting, and ^O is for blockdenting --
as it were. If you haven't used the last one, you're missing
a lot. A more complete example, with comments, can be found at
<P>If you are used to using the <EM>vgrind</EM> program for printing out nice code
to a laser printer, you can take a stab at this using
<A HREF="http://www.perl.com/CPAN/doc/misc/tips/working.vgrind.entry,">http://www.perl.com/CPAN/doc/misc/tips/working.vgrind.entry,</A> but the
results are not particularly satisfying for sophisticated code.</P>
<P>The a2ps at <A HREF="http://www.infres.enst.fr/%7Edemaille/a2ps/">http://www.infres.enst.fr/%7Edemaille/a2ps/</A> does lots of things
related to generating nicely printed output of documents.</P>
<P>
<H2><A NAME="is there a ctags for perl">Is there a ctags for Perl?</A></H2>
<P>There's a simple one at
<A HREF="http://www.perl.com/CPAN/authors/id/TOMC/scripts/ptags.gz">http://www.perl.com/CPAN/authors/id/TOMC/scripts/ptags.gz</A> which may do
the trick. And if not, it's easy to hack into what you want.</P>
<P>
<H2><A NAME="is there an ide or windows perl editor">Is there an IDE or Windows Perl Editor?</A></H2>
<P>If you're on Unix, you already have an IDE -- Unix itself. This powerful
IDE derives from its interoperability, flexibility, and configurability.
If you really want to get a feel for Unix-qua-IDE, the best thing to do
is to find some high-powered programmer whose native language is Unix.
Find someone who has been at this for many years, and just sit back
and watch them at work. They have created their own IDE, one that
suits their own tastes and aptitudes. Quietly observe them edit files,
move them around, compile them, debug them, test them, etc. The entire
development *is* integrated, like a top-of-the-line German sports car:
functional, powerful, and elegant. You will be absolutely astonished
at the speed and ease exhibited by the native speaker of Unix in his
home territory. The art and skill of a virtuoso can only be seen to be
believed. That is the path to mastery -- all these cobbled little IDEs
are expensive toys designed to sell a flashy demo using cheap tricks,
and being optimized for immediate but shallow understanding rather than
enduring use, are but a dim palimpsest of real tools.</P>
<P>In short, you just have to learn the toolbox. However, if you're not
on Unix, then your vendor probably didn't bother to provide you with
a proper toolbox on the so-called complete system that you forked out
your hard-earned cash on.</P>
<P>PerlBuilder (XXX URL to follow) is an integrated development environment
for Windows that supports Perl development. Perl programs are just plain
text, though, so you could download emacs for Windows (???) or a vi clone
(vim) which runs on for win32 (http://www.cs.vu.nl/%7Etmgil/vi.html).
If you're transferring Windows files to Unix, be sure to transfer in
ASCII mode so the ends of lines are appropriately mangled.</P>
<P>
<H2><A NAME="where can i get perl macros for vi">Where can I get Perl macros for vi?</A></H2>
<P>For a complete version of Tom Christiansen's vi configuration file,
see <A HREF="http://www.perl.com/CPAN/authors/Tom_Christiansen/scripts/toms.exrc.gz,">http://www.perl.com/CPAN/authors/Tom_Christiansen/scripts/toms.exrc.gz,</A>
the standard benchmark file for vi emulators. This runs best with nvi,
the current version of vi out of Berkeley, which incidentally can be built
with an embedded Perl interpreter -- see <A HREF="http://www.perl.com/CPAN/src/misc.">http://www.perl.com/CPAN/src/misc.</A></P>
<P>
<H2><A NAME="where can i get perlmode for emacs">Where can I get perl-mode for emacs?</A></H2>
<P>Since Emacs version 19 patchlevel 22 or so, there have been both a
perl-mode.el and support for the Perl debugger built in. These should
come with the standard Emacs 19 distribution.</P>
<P>In the Perl source directory, you'll find a directory called ``emacs'',
which contains a cperl-mode that color-codes keywords, provides
context-sensitive help, and other nifty things.</P>
<P>Note that the perl-mode of emacs will have fits with <CODE>"main'foo"</CODE>
(single quote), and mess up the indentation and highlighting. You
are probably using <CODE>"main::foo"</CODE> in new Perl code anyway, so this
shouldn't be an issue.</P>
<P>
<H2><A NAME="how can i use curses with perl">How can I use curses with Perl?</A></H2>
<P>The Curses module from CPAN provides a dynamically loadable object
module interface to a curses library. A small demo can be found at the
module, which is curses-based, can help with this.</P>
<P>
<H2><A NAME="what is undump">What is undump?</A></H2>
<P>See the next questions.</P>
<P>
<H2><A NAME="how can i make my perl program run faster">How can I make my Perl program run faster?</A></H2>
<P>The best way to do this is to come up with a better algorithm. This
can often make a dramatic difference. Chapter 8 in the Camel has some
efficiency tips in it you might want to look at. Jon Bentley's book
``Programming Pearls'' (that's not a misspelling!) has some good tips
on optimization, too. Advice on benchmarking boils down to: benchmark
and profile to make sure you're optimizing the right part, look for
better algorithms instead of microtuning your code, and when all else
fails consider just buying faster hardware.</P>
<P>A different approach is to autoload seldom-used Perl code. See the
AutoSplit and AutoLoader modules in the standard distribution for
that. Or you could locate the bottleneck and think about writing just
that part in C, the way we used to take bottlenecks in C code and
write them in assembler. Similar to rewriting in C is the use of
modules that have critical sections written in C (for instance, the
PDL module from CPAN).</P>
<P>In some cases, it may be worth it to use the backend compiler to
produce byte code (saving compilation time) or compile into C, which
will certainly save compilation time and sometimes a small amount (but
not much) execution time. See the question about compiling your Perl
programs for more on the compiler--the wins aren't as obvious as you'd
hope.</P>
<P>If you're currently linking your perl executable to a shared <EM>libc.so</EM>,
you can often gain a 10-25% performance benefit by rebuilding it to
link with a static libc.a instead. This will make a bigger perl
executable, but your Perl programs (and programmers) may thank you for
it. See the <EM>INSTALL</EM> file in the source distribution for more
information.</P>
<P>Unsubstantiated reports allege that Perl interpreters that use sfio
outperform those that don't (for I/O intensive applications). To try
this, see the <EM>INSTALL</EM> file in the source distribution, especially
the ``Selecting File I/O mechanisms'' section.</P>
<P>The undump program was an old attempt to speed up your Perl program
by storing the already-compiled form to disk. This is no longer
a viable option, as it only worked on a few architectures, and
wasn't a good solution anyway.</P>
<P>
<H2><A NAME="how can i make my perl program take less memory">How can I make my Perl program take less memory?</A></H2>
<P>When it comes to time-space tradeoffs, Perl nearly always prefers to
throw memory at a problem. Scalars in Perl use more memory than
strings in C, arrays take more than that, and hashes use even more. While
there's still a lot to be done, recent releases have been addressing
these issues. For example, as of 5.004, duplicate hash keys are
shared amongst all hashes using them, so require no reallocation.</P>
<P>In some cases, using <A HREF="../../lib/Pod/perlfunc.html#item_substr"><CODE>substr()</CODE></A> or <A HREF="../../lib/Pod/perlfunc.html#item_vec"><CODE>vec()</CODE></A> to simulate arrays can be
highly beneficial. For example, an array of a thousand booleans will
take at least 20,000 bytes of space, but it can be turned into one
125-byte bit vector for a considerable memory savings. The standard
Tie::SubstrHash module can also help for certain types of data
structure. If you're working with specialist data structures
(matrices, for instance) modules that implement these in C may use
less memory than equivalent Perl modules.</P>
<P>Another thing to try is learning whether your Perl was compiled with
the system malloc or with Perl's builtin malloc. Whichever one it
is, try using the other one and see whether this makes a difference.
Information about malloc is in the <EM>INSTALL</EM> file in the source
distribution. You can find out whether you are using perl's malloc by
typing <CODE>perl -V:usemymalloc</CODE>.</P>
<P>
<H2><A NAME="is it unsafe to return a pointer to local data">Is it unsafe to return a pointer to local data?</A></H2>
<P>No, Perl's garbage collection system takes care of this.</P>
<PRE>
sub makeone {
my @a = ( 1 .. 10 );
return \@a;
}</PRE>
<PRE>
for $i ( 1 .. 10 ) {
push @many, makeone();
}</PRE>
<PRE>
print $many[4][5], "\n";</PRE>
<PRE>
print "@many\n";</PRE>
<P>
<H2><A NAME="how can i free an array or hash so my program shrinks">How can I free an array or hash so my program shrinks?</A></H2>
<P>You can't. On most operating systems, memory allocated to a program
can never be returned to the system. That's why long-running programs
sometimes re-exec themselves. Some operating systems (notably,
FreeBSD and Linux) allegedly reclaim large chunks of memory that is no
longer used, but it doesn't appear to happen with Perl (yet). The Mac
appears to be the only platform that will reliably (albeit, slowly)
return memory to the OS.</P>
<P>We've had reports that on Linux (Redhat 5.1) on Intel, <CODE>undef
$scalar</CODE> will return memory to the system, while on Solaris 2.6 it
won't. In general, try it yourself and see.</P>
<P>However, judicious use of <A HREF="../../lib/Pod/perlfunc.html#item_my"><CODE>my()</CODE></A> on your variables will help make sure
that they go out of scope so that Perl can free up their storage for
use in other parts of your program. A global variable, of course, never
goes out of scope, so you can't get its space automatically reclaimed,
although undef()ing and/or delete()ing it will achieve the same effect.
In general, memory allocation and de-allocation isn't something you can
or should be worrying about much in Perl, but even this capability
(preallocation of data types) is in the works.</P>
<P>
<H2><A NAME="how can i make my cgi script more efficient">How can I make my CGI script more efficient?</A></H2>
<P>Beyond the normal measures described to make general Perl programs
faster or smaller, a CGI program has additional issues. It may be run
several times per second. Given that each time it runs it will need
to be re-compiled and will often allocate a megabyte or more of system
memory, this can be a killer. Compiling into C <STRONG>isn't going to help
you</STRONG> because the process start-up overhead is where the bottleneck is.</P>
<P>There are two popular ways to avoid this overhead. One solution
involves running the Apache HTTP server (available from
<A HREF="http://www.apache.org/)">http://www.apache.org/)</A> with either of the mod_perl or mod_fastcgi
plugin modules.</P>
<P>With mod_perl and the Apache::Registry module (distributed with
mod_perl), httpd will run with an embedded Perl interpreter which
pre-compiles your script and then executes it within the same address
space without forking. The Apache extension also gives Perl access to
the internal server API, so modules written in Perl can do just about
anything a module written in C can. For more on mod_perl, see
<H2><A NAME="where can i learn about objectoriented perl programming">Where can I learn about object-oriented Perl programming?</A></H2>
<P>A good place to start is <A HREF="../../lib/Pod/perltoot.html">the perltoot manpage</A>, and you can use <A HREF="../../lib/Pod/perlobj.html">the perlobj manpage</A> and
<A HREF="../../lib/Pod/perlbot.html">the perlbot manpage</A> for reference. Perltoot didn't come out until the 5.004
release, but you can get a copy (in pod, html, or postscript) from
<H2><A NAME="where can i learn about linking c with perl [h2xs, xsubpp]">Where can I learn about linking C with Perl? [h2xs, xsubpp]</A></H2>
<P>If you want to call C from Perl, start with <A HREF="../../lib/Pod/perlxstut.html">the perlxstut manpage</A>,
moving on to <A HREF="../../lib/Pod/perlxs.html">the perlxs manpage</A>, <EM>xsubpp</EM>, and <A HREF="../../lib/Pod/perlguts.html">the perlguts manpage</A>. If you want to
call Perl from C, then read <A HREF="../../lib/Pod/perlembed.html">the perlembed manpage</A>, <A HREF="../../lib/Pod/perlcall.html">the perlcall manpage</A>, and
<A HREF="../../lib/Pod/perlguts.html">the perlguts manpage</A>. Don't forget that you can learn a lot from looking at
how the authors of existing extension modules wrote their code and
solved their problems.</P>
<P>
<H2><A NAME="i've read perlembed, perlguts, etc., but i can't embed perl in my c program, what am i doing wrong">I've read perlembed, perlguts, etc., but I can't embed perl in
my C program, what am I doing wrong?</A></H2>
<P>Download the ExtUtils::Embed kit from CPAN and run `make test'. If
the tests pass, read the pods again and again and again. If they
fail, see <EM>perlbug</EM> and send a bug report with the output of
<CODE>make test TEST_VERBOSE=1</CODE> along with <CODE>perl -V</CODE>.</P>
<P>
<H2><A NAME="when i tried to run my script, i got this message. what does it mean">When I tried to run my script, I got this message. What does it
mean?</A></H2>
<P>A complete list of Perl's error messages and warnings with explanatory
text can be found in <A HREF="../../lib/Pod/perldiag.html">the perldiag manpage</A>. You can also use the splain program
(distributed with Perl) to explain the error messages:</P>
<PRE>
perl program 2>diag.out
splain [-v] [-p] diag.out</PRE>
<P>or change your program to explain the messages for you:</P>