In Perl_pad_check_dup(), use sv rather than name for diagnostics.
[p5sagit/p5-mst-13.2.git] / pod / perlfaq3.pod
CommitLineData
68dc0745 1=head1 NAME
2
109f0441 3perlfaq3 - Programming Tools
68dc0745 4
5=head1 DESCRIPTION
6
7This section of the FAQ answers questions related to programmer tools
8and programming support.
9
10=head2 How do I do (anything)?
11
12Have you looked at CPAN (see L<perlfaq2>)? The chances are that
13someone has already written a module that can solve your problem.
3958b146 14Have you read the appropriate manpages? Here's a brief index:
68dc0745 15
5a964f20 16 Basics perldata, perlvar, perlsyn, perlop, perlsub
17 Execution perlrun, perldebug
18 Functions perlfunc
68dc0745 19 Objects perlref, perlmod, perlobj, perltie
20 Data Structures perlref, perllol, perldsc
f102b883 21 Modules perlmod, perlmodlib, perlsub
d92eb7b0 22 Regexes perlre, perlfunc, perlop, perllocale
68dc0745 23 Moving to perl5 perltrap, perl
24 Linking w/C perlxstut, perlxs, perlcall, perlguts, perlembed
06a5f41f 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)
68dc0745 28
3958b146 29A crude table of contents for the Perl manpage set is found in L<perltoc>.
68dc0745 30
31=head2 How can I use Perl interactively?
32
33The typical approach uses the Perl debugger, described in the
b432a672 34perldebug(1) manpage, on an "empty" program, like this:
68dc0745 35
36 perl -de 42
37
38Now just type in any legal Perl code, and it will be immediately
39evaluated. You can also examine the symbol table, get stack
40backtraces, check variable values, set breakpoints, and other
92c2ed05 41operations typically found in symbolic debuggers.
68dc0745 42
43=head2 Is there a Perl shell?
44
a05e4845 45The psh (Perl sh) is currently at version 1.8. The Perl Shell is a shell
46that combines the interactive nature of a Unix shell with the power of
47Perl. The goal is a full featured shell that behaves as expected for
48normal shell activity and uses Perl syntax and functionality for
49control-flow statements and other things. You can get psh at
50http://sourceforge.net/projects/psh/ .
55e174a4 51
f3b9614f 52Zoidberg is a similar project and provides a shell written in perl,
53configured in perl and operated in perl. It is intended as a login shell
109f0441 54and development environment. It can be found at
55http://pardus-larus.student.utwente.nl/~pardus/projects/zoidberg/
f3b9614f 56or your local CPAN mirror.
57
55e174a4 58The Shell.pm module (distributed with Perl) makes Perl try commands
a05e4845 59which aren't part of the Perl language as shell commands. perlsh from
60the source distribution is simplistic and uninteresting, but may still
61be what you want.
68dc0745 62
49d635f9 63=head2 How do I find which modules are installed on my system?
64
109f0441 65From the command line, you can use the C<cpan> command's C<-l> switch:
66
67 $ cpan -l
68
69You can also use C<cpan>'s C<-a> switch to create an autobundle file
70that C<CPAN.pm> understands and cna use to re-install every module:
71
72 $ cpan -a
73
74Inside a Perl program, you can use the ExtUtils::Installed module to
75show all installed distributions, although it can take awhile to do
76its magic. The standard library which comes with Perl just shows up
77as "Perl" (although you can get those with Module::CoreList).
49d635f9 78
79 use ExtUtils::Installed;
197aec24 80
49d635f9 81 my $inst = ExtUtils::Installed->new();
82 my @modules = $inst->modules();
83
84If you want a list of all of the Perl module filenames, you
85can use File::Find::Rule.
86
87 use File::Find::Rule;
197aec24 88
109f0441 89 my @files = File::Find::Rule->
90 extras({follow => 1})->
91 file()->
92 name( '*.pm' )->
93 in( @INC )
94 ;
49d635f9 95
96If you do not have that module, you can do the same thing
197aec24 97with File::Find which is part of the standard library.
49d635f9 98
109f0441 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 );
49d635f9 113
a05e4845 114 print join "\n", @files;
197aec24 115
49d635f9 116If you simply need to quickly check to see if a module is
117available, you can check for its documentation. If you can
197aec24 118read the documentation the module is most likely installed.
49d635f9 119If you cannot read the documentation, the module might not
120have any (in rare cases).
121
109f0441 122 $ perldoc Module::Name
49d635f9 123
124You can also try to include the module in a one-liner to see if
125perl finds it.
126
109f0441 127 $ perl -MModule::Name -e1
197aec24 128
68dc0745 129=head2 How do I debug my Perl programs?
130
500071f4 131(contributed by brian d foy)
132
133Before you do anything else, you can help yourself by ensuring that
134you let Perl tell you about problem areas in your code. By turning
ac9dac7f 135on warnings and strictures, you can head off many problems before
500071f4 136they get too big. You can find out more about these in L<strict>
137and L<warnings>.
138
139 #!/usr/bin/perl
140 use strict;
141 use warnings;
ac9dac7f 142
500071f4 143Beyond that, the simplest debugger is the C<print> function. Use it
144to look at values as you run your program:
145
146 print STDERR "The value is [$value]\n";
68dc0745 147
500071f4 148The C<Data::Dumper> module can pretty-print Perl data structures:
68dc0745 149
ac9dac7f 150 use Data::Dumper qw( Dumper );
151 print STDERR "The hash is " . Dumper( \%hash ) . "\n";
152
500071f4 153Perl comes with an interactive debugger, which you can start with the
154C<-d> switch. It's fully explained in L<perldebug>.
68dc0745 155
500071f4 156If you'd like a graphical user interface and you have Tk, you can use
157C<ptkdb>. It's on CPAN and available for free.
68dc0745 158
c195e131 159If you need something much more sophisticated and controllable, Leon
500071f4 160Brocard's Devel::ebug (which you can call with the -D switch as -Debug)
161gives you the programmatic hooks into everything you need to write your
162own (without too much pain and suffering).
92c2ed05 163
500071f4 164You can also use a commercial debugger such as Affrus (Mac OS X), Komodo
165from Activestate (Windows and Mac OS X), or EPIC (most platforms).
68dc0745 166
167=head2 How do I profile my Perl programs?
168
109f0441 169(contributed by brian d foy, updated Fri Jul 25 12:22:26 PDT 2008)
170
171The C<Devel> namespace has several modules which you can use to
172profile your Perl programs. The C<Devel::DProf> module comes with Perl
173and you can invoke it with the C<-d> switch:
174
175 perl -d:DProf program.pl
176
177After running your program under C<DProf>, you'll get a F<tmon.out> file
178with the profile data. To look at the data, you can turn it into a
179human-readable report with the C<dprofpp> program that comes with
180C<Devel::DProf>.
181
182 dprofpp
68dc0745 183
109f0441 184You can also do the profiling and reporting in one step with the C<-p>
185switch to <dprofpp>:
92c2ed05 186
109f0441 187 dprofpp -p program.pl
92c2ed05 188
109f0441 189The C<Devel::NYTProf> (New York Times Profiler) does both statement
190and subroutine profiling. It's available from CPAN and you also invoke
191it with the C<-d> switch:
92c2ed05 192
109f0441 193 perl -d:NYTProf some_perl.pl
92c2ed05 194
109f0441 195Like C<DProf>, it creates a database of the profile information that you
196can turn into reports. The C<nytprofhtml> command turns the data into
197an HTML report similar to the C<Devel::Cover> report:
92c2ed05 198
109f0441 199 nytprofhtml
92c2ed05 200
109f0441 201CPAN has several other profilers that you can invoke in the same
202fashion. You might also be interested in using the C<Benchmark> to
203measure and compare code snippets.
204
205You can read more about profiling in I<Programming Perl>, chapter 20,
206or I<Mastering Perl>, chapter 5.
207
208L<perldebguts> documents creating a custom debugger if you need to
209create a special sort of profiler. brian d foy describes the process
210in I<The Perl Journal>, "Creating a Perl Debugger",
211http://www.ddj.com/184404522 , and "Profiling in Perl"
212http://www.ddj.com/184404580 .
213
214Perl.com has two interesting articles on profiling: "Profiling Perl",
215by Simon Cozens, http://www.perl.com/lpt/a/850 and "Debugging and
216Profiling mod_perl Applications", by Frank Wiles,
217http://www.perl.com/pub/a/2006/02/09/debug_mod_perl.html .
218
219Randal L. Schwartz writes about profiling in "Speeding up Your Perl
220Programs" for I<Unix Review>,
221http://www.stonehenge.com/merlyn/UnixReview/col49.html , and "Profiling
222in Template Toolkit via Overriding" for I<Linux Magazine>,
223http://www.stonehenge.com/merlyn/LinuxMag/col75.html .
65acb1b1 224
68dc0745 225=head2 How do I cross-reference my Perl programs?
226
197aec24 227The B::Xref module can be used to generate cross-reference reports
83ded9ee 228for Perl programs.
68dc0745 229
c8db1d39 230 perl -MO=Xref[,OPTIONS] scriptname.plx
68dc0745 231
232=head2 Is there a pretty-printer (formatter) for Perl?
233
55e174a4 234Perltidy is a Perl script which indents and reformats Perl scripts
235to make them easier to read by trying to follow the rules of the
236L<perlstyle>. If you write Perl scripts, or spend much time reading
237them, you will probably find it useful. It is available at
238http://perltidy.sourceforge.net
239
240Of course, if you simply follow the guidelines in L<perlstyle>,
241you shouldn't need to reformat. The habit of formatting your code
242as you write it will help prevent bugs. Your editor can and should
243help you with this. The perl-mode or newer cperl-mode for emacs
244can provide remarkable amounts of help with most (but not all)
245code, and even less programmable editors can provide significant
246assistance. Tom Christiansen and many other VI users swear by
247the following settings in vi and its clones:
65acb1b1 248
249 set ai sw=4
d92eb7b0 250 map! ^O {^M}^[O^T
65acb1b1 251
55e174a4 252Put that in your F<.exrc> file (replacing the caret characters
65acb1b1 253with control characters) and away you go. In insert mode, ^T is
ac9dac7f 254for indenting, ^D is for undenting, and ^O is for blockdenting--as
255it were. A more complete example, with comments, can be found at
213329dd 256http://www.cpan.org/authors/id/TOMC/scripts/toms.exrc.gz
92c2ed05 257
49d635f9 258The a2ps http://www-inf.enst.fr/%7Edemaille/a2ps/black+white.ps.gz does
06a5f41f 259lots of things related to generating nicely printed output of
c195e131 260documents.
65acb1b1 261
d92eb7b0 262=head2 Is there a ctags for Perl?
68dc0745 263
b68463f7 264(contributed by brian d foy)
265
ac9dac7f 266Ctags uses an index to quickly find things in source code, and many
267popular editors support ctags for several different languages,
268including Perl.
269
b68463f7 270Exuberent ctags supports Perl: http://ctags.sourceforge.net/
bc06af74 271
b68463f7 272You might also try pltags: http://www.mscha.com/pltags.zip
65acb1b1 273
274=head2 Is there an IDE or Windows Perl Editor?
275
6641ed39 276Perl programs are just plain text, so any editor will do.
277
6641ed39 278If you're on Unix, you already have an IDE--Unix itself. The UNIX
279philosophy is the philosophy of several small tools that each do one
280thing and do it well. It's like a carpenter's toolbox.
281
28b41a80 282If you want an IDE, check the following (in alphabetical order, not
283order of preference):
68fbfbd7 284
285=over 4
286
28b41a80 287=item Eclipse
288
b68463f7 289http://e-p-i-c.sf.net/
290
6670e5e7 291The Eclipse Perl Integration Project integrates Perl
28b41a80 292editing/debugging with Eclipse.
293
b68463f7 294=item Enginsite
295
296http://www.enginsite.com/
297
298Perl Editor by EngInSite is a complete integrated development
299environment (IDE) for creating, testing, and debugging Perl scripts;
300the tool runs on Windows 9x/NT/2000/XP or later.
28b41a80 301
68fbfbd7 302=item Komodo
303
b68463f7 304http://www.ActiveState.com/Products/Komodo/
305
28b41a80 306ActiveState's cross-platform (as of October 2004, that's Windows, Linux,
307and Solaris), multi-language IDE has Perl support, including a regular expression
b68463f7 308debugger and remote debugging.
68fbfbd7 309
ac1094a1 310=item Open Perl IDE
311
b68463f7 312http://open-perl-ide.sourceforge.net/
313
ac1094a1 314Open Perl IDE is an integrated development environment for writing
315and debugging Perl scripts with ActiveState's ActivePerl distribution
316under Windows 95/98/NT/2000.
317
28b41a80 318=item OptiPerl
319
b68463f7 320http://www.optiperl.com/
321
322OptiPerl is a Windows IDE with simulated CGI environment, including
323debugger and syntax highlighting editor.
28b41a80 324
109f0441 325=item Padre
326
327http://padre.perlide.org/
328
329Padre is cross-platform IDE for Perl written in Perl using the the wxWidgets
330to provide a native look and feel. It's open source under the Artistic
331License.
332
5ca69f12 333=item PerlBuilder
334
b68463f7 335http://www.solutionsoft.com/perl.htm
336
109f0441 337PerlBuilder is an integrated development environment for Windows that
b68463f7 338supports Perl development.
8782d048 339
68fbfbd7 340=item visiPerl+
341
b68463f7 342http://helpconsulting.net/visiperl/
343
ac1094a1 344From Help Consulting, for Windows.
68fbfbd7 345
28b41a80 346=item Visual Perl
347
b68463f7 348http://www.activestate.com/Products/Visual_Perl/
349
28b41a80 350Visual Perl is a Visual Studio.NET plug-in from ActiveState.
29b1171f 351
b68463f7 352=item Zeus
353
354http://www.zeusedit.com/lookmain.html
355
356Zeus for Window is another Win32 multi-language editor/IDE
357that comes with support for Perl:
29b1171f 358
68fbfbd7 359=back
360
b68463f7 361For editors: if you're on Unix you probably have vi or a vi clone
362already, and possibly an emacs too, so you may not need to download
363anything. In any emacs the cperl-mode (M-x cperl-mode) gives you
364perhaps the best available Perl editing mode in any editor.
365
366If you are using Windows, you can use any editor that lets you work
367with plain text, such as NotePad or WordPad. Word processors, such as
368Microsoft Word or WordPerfect, typically do not work since they insert
369all sorts of behind-the-scenes information, although some allow you to
f12f5f55 370save files as "Text Only". You can also download te