3 perlfaq3 - Programming Tools
7 This section of the FAQ answers questions related to programmer tools
8 and programming support.
10 =head2 How do I do (anything)?
12 Have you looked at CPAN (see L<perlfaq2>)? The chances are that
13 someone has already written a module that can solve your problem.
14 Have you read the appropriate manpages? Here's a brief index:
16 Basics perldata, perlvar, perlsyn, perlop, perlsub
17 Execution perlrun, perldebug
19 Objects perlref, perlmod, perlobj, perltie
20 Data Structures perlref, perllol, perldsc
21 Modules perlmod, perlmodlib, perlsub
22 Regexes perlre, perlfunc, perlop, perllocale
23 Moving to perl5 perltrap, perl
24 Linking w/C perlxstut, perlxs, perlcall, perlguts, perlembed
25 Various http://www.cpan.org/misc/olddoc/FMTEYEWTK.tgz
26 (not a man-page but still useful, a collection
27 of various essays on Perl techniques)
29 A crude table of contents for the Perl manpage set is found in L<perltoc>.
31 =head2 How can I use Perl interactively?
33 The typical approach uses the Perl debugger, described in the
34 perldebug(1) manpage, on an "empty" program, like this:
38 Now just type in any legal Perl code, and it will be immediately
39 evaluated. You can also examine the symbol table, get stack
40 backtraces, check variable values, set breakpoints, and other
41 operations typically found in symbolic debuggers.
43 =head2 Is there a Perl shell?
45 The psh (Perl sh) is currently at version 1.8. The Perl Shell is a shell
46 that combines the interactive nature of a Unix shell with the power of
47 Perl. The goal is a full featured shell that behaves as expected for
48 normal shell activity and uses Perl syntax and functionality for
49 control-flow statements and other things. You can get psh at
50 http://sourceforge.net/projects/psh/ .
52 Zoidberg is a similar project and provides a shell written in perl,
53 configured in perl and operated in perl. It is intended as a login shell
54 and development environment. It can be found at
55 http://pardus-larus.student.utwente.nl/~pardus/projects/zoidberg/
56 or your local CPAN mirror.
58 The Shell.pm module (distributed with Perl) makes Perl try commands
59 which aren't part of the Perl language as shell commands. perlsh from
60 the source distribution is simplistic and uninteresting, but may still
63 =head2 How do I find which modules are installed on my system?
65 From the command line, you can use the C<cpan> command's C<-l> switch:
69 You can also use C<cpan>'s C<-a> switch to create an autobundle file
70 that C<CPAN.pm> understands and cna use to re-install every module:
74 Inside a Perl program, you can use the ExtUtils::Installed module to
75 show all installed distributions, although it can take awhile to do
76 its magic. The standard library which comes with Perl just shows up
77 as "Perl" (although you can get those with Module::CoreList).
79 use ExtUtils::Installed;
81 my $inst = ExtUtils::Installed->new();
82 my @modules = $inst->modules();
84 If you want a list of all of the Perl module filenames, you
85 can use File::Find::Rule.
89 my @files = File::Find::Rule->
90 extras({follow => 1})->
96 If you do not have that module, you can do the same thing
97 with File::Find which is part of the standard library.
105 push @files, $File::Find::fullname
106 if -f $File::Find::fullname && /\.pm$/
114 print join "\n", @files;
116 If you simply need to quickly check to see if a module is
117 available, you can check for its documentation. If you can
118 read the documentation the module is most likely installed.
119 If you cannot read the documentation, the module might not
120 have any (in rare cases).
122 $ perldoc Module::Name
124 You can also try to include the module in a one-liner to see if
127 $ perl -MModule::Name -e1
129 =head2 How do I debug my Perl programs?
131 (contributed by brian d foy)
133 Before you do anything else, you can help yourself by ensuring that
134 you let Perl tell you about problem areas in your code. By turning
135 on warnings and strictures, you can head off many problems before
136 they get too big. You can find out more about these in L<strict>
143 Beyond that, the simplest debugger is the C<print> function. Use it
144 to look at values as you run your program:
146 print STDERR "The value is [$value]\n";
148 The C<Data::Dumper> module can pretty-print Perl data structures:
150 use Data::Dumper qw( Dumper );
151 print STDERR "The hash is " . Dumper( \%hash ) . "\n";
153 Perl comes with an interactive debugger, which you can start with the
154 C<-d> switch. It's fully explained in L<perldebug>.
156 If you'd like a graphical user interface and you have Tk, you can use
157 C<ptkdb>. It's on CPAN and available for free.
159 If you need something much more sophisticated and controllable, Leon
160 Brocard's Devel::ebug (which you can call with the -D switch as -Debug)
161 gives you the programmatic hooks into everything you need to write your
162 own (without too much pain and suffering).
164 You can also use a commercial debugger such as Affrus (Mac OS X), Komodo
165 from Activestate (Windows and Mac OS X), or EPIC (most platforms).
167 =head2 How do I profile my Perl programs?
169 (contributed by brian d foy, updated Fri Jul 25 12:22:26 PDT 2008)
171 The C<Devel> namespace has several modules which you can use to
172 profile your Perl programs. The C<Devel::DProf> module comes with Perl
173 and you can invoke it with the C<-d> switch:
175 perl -d:DProf program.pl
177 After running your program under C<DProf>, you'll get a F<tmon.out> file
178 with the profile data. To look at the data, you can turn it into a
179 human-readable report with the C<dprofpp> program that comes with
184 You can also do the profiling and reporting in one step with the C<-p>
187 dprofpp -p program.pl
189 The C<Devel::NYTProf> (New York Times Profiler) does both statement
190 and subroutine profiling. It's available from CPAN and you also invoke
191 it with the C<-d> switch:
193 perl -d:NYTProf some_perl.pl
195 Like C<DProf>, it creates a database of the profile information that you
196 can turn into reports. The C<nytprofhtml> command turns the data into
197 an HTML report similar to the C<Devel::Cover> report:
201 CPAN has several other profilers that you can invoke in the same
202 fashion. You might also be interested in using the C<Benchmark> to
203 measure and compare code snippets.
205 You can read more about profiling in I<Programming Perl>, chapter 20,
206 or I<Mastering Perl>, chapter 5.
208 L<perldebguts> documents creating a custom debugger if you need to
209 create a special sort of profiler. brian d foy describes the process
210 in I<The Perl Journal>, "Creating a Perl Debugger",
211 http://www.ddj.com/184404522 , and "Profiling in Perl"
212 http://www.ddj.com/184404580 .
214 Perl.com has two interesting articles on profiling: "Profiling Perl",
215 by Simon Cozens, http://www.perl.com/lpt/a/850 and "Debugging and
216 Profiling mod_perl Applications", by Frank Wiles,
217 http://www.perl.com/pub/a/2006/02/09/debug_mod_perl.html .
219 Randal L. Schwartz writes about profiling in "Speeding up Your Perl
220 Programs" for I<Unix Review>,
221 http://www.stonehenge.com/merlyn/UnixReview/col49.html , and "Profiling
222 in Template Toolkit via Overriding" for I<Linux Magazine>,
223 http://www.stonehenge.com/merlyn/LinuxMag/col75.html .
225 =head2 How do I cross-reference my Perl programs?
227 The B::Xref module can be used to generate cross-reference reports
230 perl -MO=Xref[,OPTIONS] scriptname.plx
232 =head2 Is there a pretty-printer (formatter) for Perl?
234 Perltidy is a Perl script which indents and reformats Perl scripts
235 to make them easier to read by trying to follow the rules of the
236 L<perlstyle>. If you write Perl scripts, or spend much time reading
237 them, you will probably find it useful. It is available at
238 http://perltidy.sourceforge.net
240 Of course, if you simply follow the guidelines in L<perlstyle>,
241 you shouldn't need to reformat. The habit of formatting your code
242 as you write it will help prevent bugs. Your editor can and should
243 help you with this. The perl-mode or newer cperl-mode for emacs
244 can provide remarkable amounts of help with most (but not all)
245 code, and even less programmable editors can provide significant
246 assistance. Tom Christiansen and many other VI users swear by
247 the following settings in vi and its clones:
252 Put that in your F<.exrc> file (replacing the caret characters
253 with control characters) and away you go. In insert mode, ^T is
254 for indenting, ^D is for undenting, and ^O is for blockdenting--as
255 it were. A more complete example, with comments, can be found at
256 http://www.cpan.org/authors/id/TOMC/scripts/toms.exrc.gz
258 The a2ps http://www-inf.enst.fr/%7Edemaille/a2ps/black+white.ps.gz does
259 lots of things related to generating nicely printed output of
262 =head2 Is there a ctags for Perl?
264 (contributed by brian d foy)
266 Ctags uses an index to quickly find things in source code, and many
267 popular editors support ctags for several different languages,
270 Exuberent ctags supports Perl: http://ctags.sourceforge.net/
272 You might also try pltags: http://www.mscha.com/pltags.zip
274 =head2 Is there an IDE or Windows Perl Editor?
276 Perl programs are just plain text, so any editor will do.
278 If you're on Unix, you already have an IDE--Unix itself. The UNIX
279 philosophy is the philosophy of several small tools that each do one
280 thing and do it well. It's like a carpenter's toolbox.
282 If you want an IDE, check the following (in alphabetical order, not
283 order of preference):
289 http://e-p-i-c.sf.net/
291 The Eclipse Perl Integration Project integrates Perl
292 editing/debugging with Eclipse.
296 http://www.enginsite.com/
298 Perl Editor by EngInSite is a complete integrated development
299 environment (IDE) for creating, testing, and debugging Perl scripts;
300 the tool runs on Windows 9x/NT/2000/XP or later.
304 http://www.ActiveState.com/Products/Komodo/
306 ActiveState's cross-platform (as of October 2004, that's Windows, Linux,
307 and Solaris), multi-language IDE has Perl support, including a regular expression
308 debugger and remote debugging.
312 http://open-perl-ide.sourceforge.net/
314 Open Perl IDE is an integrated development environment for writing
315 and debugging Perl scripts with ActiveState's ActivePerl distribution
316 under Windows 95/98/NT/2000.
320 http://www.optiperl.com/
322 OptiPerl is a Windows IDE with simulated CGI environment, including
323 debugger and syntax highlighting editor.
327 http://padre.perlide.org/
329 Padre is cross-platform IDE for Perl written in Perl using the the wxWidgets
330 to provide a native look and feel. It's open source under the Artistic
335 http://www.solutionsoft.com/perl.htm
337 PerlBuilder is an integrated development environment for Windows that
338 supports Perl development.
342 http://helpconsulting.net/visiperl/
344 From Help Consulting, for Windows.
348 http://www.activestate.com/Products/Visual_Perl/
350 Visual Perl is a Visual Studio.NET plug-in from ActiveState.
354 http://www.zeusedit.com/lookmain.html
356 Zeus for Window is another Win32 multi-language editor/IDE
357 that comes with support for Perl:
361 For editors: if you're on Unix you probably have vi or a vi clone
362 already, and possibly an emacs too, so you may not need to download
363 anything. In any emacs the cperl-mode (M-x cperl-mode) gives you
364 perhaps the best available Perl editing mode in any editor.
366 If you are using Windows, you can use any editor that lets you work
367 with plain text, such as NotePad or WordPad. Word processors, such as
368 Microsoft Word or WordPerfect, typically do not work since they insert
369 all sorts of behind-the-scenes information, although some allow you to
370 save files as "Text Only". You can also download te