8ddcabb37071d63899383f8bf3dbaefb1cfecacd
[p5sagit/p5-mst-13.2.git] / pod / perlfaq3.pod
1 =head1 NAME
2
3 perlfaq3 - Programming Tools
4
5 =head1 DESCRIPTION
6
7 This section of the FAQ answers questions related to programmer tools
8 and programming support.
9
10 =head2 How do I do (anything)?
11
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:
15
16         Basics          perldata, perlvar, perlsyn, perlop, perlsub
17         Execution       perlrun, perldebug
18         Functions       perlfunc
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)
28
29 A crude table of contents for the Perl manpage set is found in L<perltoc>.
30
31 =head2 How can I use Perl interactively?
32
33 The typical approach uses the Perl debugger, described in the
34 perldebug(1) manpage, on an "empty" program, like this:
35
36     perl -de 42
37
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.
42
43 =head2 Is there a Perl shell?
44
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/ .
51
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.
57
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
61 be what you want.
62
63 =head2 How do I find which modules are installed on my system?
64
65 From the command line, you can use the C<cpan> command's C<-l> switch:
66
67         $ cpan -l
68
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:
71
72         $ cpan -a
73
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).
78
79         use ExtUtils::Installed;
80
81         my $inst    = ExtUtils::Installed->new();
82         my @modules = $inst->modules();
83
84 If you want a list of all of the Perl module filenames, you
85 can use File::Find::Rule.
86
87         use File::Find::Rule;
88
89         my @files = File::Find::Rule->
90                 extras({follow => 1})->
91                 file()->
92                 name( '*.pm' )->
93                 in( @INC )
94                 ;
95
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.
98
99         use File::Find;
100         my @files;
101
102         find(
103             {
104                 wanted => sub {
105                     push @files, $File::Find::fullname
106                         if -f $File::Find::fullname && /\.pm$/
107                 },
108                 follow => 1,
109                 follow_skip => 2,
110             },
111             @INC
112         );
113
114         print join "\n", @files;
115
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).
121
122         $ perldoc Module::Name
123
124 You can also try to include the module in a one-liner to see if
125 perl finds it.
126
127         $ perl -MModule::Name -e1
128
129 =head2 How do I debug my Perl programs?
130
131 (contributed by brian d foy)
132
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>
137 and L<warnings>.
138
139         #!/usr/bin/perl
140         use strict;
141         use warnings;
142
143 Beyond that, the simplest debugger is the C<print> function. Use it
144 to look at values as you run your program:
145
146         print STDERR "The value is [$value]\n";
147
148 The C<Data::Dumper> module can pretty-print Perl data structures:
149
150         use Data::Dumper qw( Dumper );
151         print STDERR "The hash is " . Dumper( \%hash ) . "\n";
152
153 Perl comes with an interactive debugger, which you can start with the
154 C<-d> switch. It's fully explained in L<perldebug>.
155
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.
158
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).
163
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).
166
167 =head2 How do I profile my Perl programs?
168
169 (contributed by brian d foy, updated Fri Jul 25 12:22:26 PDT 2008)
170
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:
174
175         perl -d:DProf program.pl
176
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
180 C<Devel::DProf>.
181
182         dprofpp
183
184 You can also do the profiling and reporting in one step with the C<-p>
185 switch to <dprofpp>:
186
187         dprofpp -p program.pl
188
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:
192
193         perl -d:NYTProf some_perl.pl
194
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:
198
199         nytprofhtml
200
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.
204
205 You can read more about profiling in I<Programming Perl>, chapter 20,
206 or I<Mastering Perl>, chapter 5.
207
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 .
213
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 .
218
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 .
224
225 =head2 How do I cross-reference my Perl programs?
226
227 The B::Xref module can be used to generate cross-reference reports
228 for Perl programs.
229
230     perl -MO=Xref[,OPTIONS] scriptname.plx
231
232 =head2 Is there a pretty-printer (formatter) for Perl?
233
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
239
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:
248
249     set ai sw=4
250     map! ^O {^M}^[O^T
251
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
257
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
260 documents.
261
262 =head2 Is there a ctags for Perl?
263
264 (contributed by brian d foy)
265
266 Ctags uses an index to quickly find things in source code, and many
267 popular editors support ctags for several different languages,
268 including Perl.
269
270 Exuberent ctags supports Perl: http://ctags.sourceforge.net/
271
272 You might also try pltags: http://www.mscha.com/pltags.zip
273
274 =head2 Is there an IDE or Windows Perl Editor?
275
276 Perl programs are just plain text, so any editor will do.
277
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.
281
282 If you want an IDE, check the following (in alphabetical order, not
283 order of preference):
284
285 =over 4
286
287 =item Eclipse
288
289 http://e-p-i-c.sf.net/
290
291 The Eclipse Perl Integration Project integrates Perl
292 editing/debugging with Eclipse.
293
294 =item Enginsite
295
296 http://www.enginsite.com/
297
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.
301
302 =item Komodo
303
304 http://www.ActiveState.com/Products/Komodo/
305
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.
309
310 =item Open Perl IDE
311
312 http://open-perl-ide.sourceforge.net/
313
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.
317
318 =item OptiPerl
319
320 http://www.optiperl.com/
321
322 OptiPerl is a Windows IDE with simulated CGI environment, including
323 debugger and syntax highlighting editor.
324
325 =item Padre
326
327 http://padre.perlide.org/
328
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
331 License.
332
333 =item PerlBuilder
334
335 http://www.solutionsoft.com/perl.htm
336
337 PerlBuilder is an integrated development environment for Windows that
338 supports Perl development.
339
340 =item visiPerl+
341
342 http://helpconsulting.net/visiperl/
343
344 From Help Consulting, for Windows.
345
346 =item Visual Perl
347
348 http://www.activestate.com/Products/Visual_Perl/
349
350 Visual Perl is a Visual Studio.NET plug-in from ActiveState.
351
352 =item Zeus
353
354 http://www.zeusedit.com/lookmain.html
355
356 Zeus for Window is another Win32 multi-language editor/IDE
357 that comes with support for Perl:
358
359 =back
360
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.
365
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