Commit | Line | Data |
68dc0745 |
1 | =head1 NAME |
2 | |
109f0441 |
3 | perlfaq3 - Programming Tools |
68dc0745 |
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. |
3958b146 |
14 | Have 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 |
29 | A 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 | |
33 | The typical approach uses the Perl debugger, described in the |
b432a672 |
34 | perldebug(1) manpage, on an "empty" program, like this: |
68dc0745 |
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 |
92c2ed05 |
41 | operations typically found in symbolic debuggers. |
68dc0745 |
42 | |
43 | =head2 Is there a Perl shell? |
44 | |
a05e4845 |
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/ . |
55e174a4 |
51 | |
f3b9614f |
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 |
109f0441 |
54 | and development environment. It can be found at |
55 | http://pardus-larus.student.utwente.nl/~pardus/projects/zoidberg/ |
f3b9614f |
56 | or your local CPAN mirror. |
57 | |
55e174a4 |
58 | The Shell.pm module (distributed with Perl) makes Perl try commands |
a05e4845 |
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. |
68dc0745 |
62 | |
49d635f9 |
63 | =head2 How do I find which modules are installed on my system? |
64 | |
109f0441 |
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). |
49d635f9 |
78 | |
79 | use ExtUtils::Installed; |
197aec24 |
80 | |
49d635f9 |
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; |
197aec24 |
88 | |
109f0441 |
89 | my @files = File::Find::Rule-> |
90 | extras({follow => 1})-> |
91 | file()-> |
92 | name( '*.pm' )-> |
93 | in( @INC ) |
94 | ; |
49d635f9 |
95 | |
96 | If you do not have that module, you can do the same thing |
197aec24 |
97 | with 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 |
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 |
197aec24 |
118 | read the documentation the module is most likely installed. |
49d635f9 |
119 | If you cannot read the documentation, the module might not |
120 | have any (in rare cases). |
121 | |
109f0441 |
122 | $ perldoc Module::Name |
49d635f9 |
123 | |
124 | You can also try to include the module in a one-liner to see if |
125 | perl 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 | |
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 |
ac9dac7f |
135 | on warnings and strictures, you can head off many problems before |
500071f4 |
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; |
ac9dac7f |
142 | |
500071f4 |
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"; |
68dc0745 |
147 | |
500071f4 |
148 | The 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 |
153 | Perl comes with an interactive debugger, which you can start with the |
154 | C<-d> switch. It's fully explained in L<perldebug>. |
68dc0745 |
155 | |
500071f4 |
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. |
68dc0745 |
158 | |
c195e131 |
159 | If you need something much more sophisticated and controllable, Leon |
500071f4 |
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). |
92c2ed05 |
163 | |
500071f4 |
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). |
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 | |
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 |
68dc0745 |
183 | |
109f0441 |
184 | You can also do the profiling and reporting in one step with the C<-p> |
185 | switch to <dprofpp>: |
92c2ed05 |
186 | |
109f0441 |
187 | dprofpp -p program.pl |
92c2ed05 |
188 | |
109f0441 |
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: |
92c2ed05 |
192 | |
109f0441 |
193 | perl -d:NYTProf some_perl.pl |
92c2ed05 |
194 | |
109f0441 |
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: |
92c2ed05 |
198 | |
109f0441 |
199 | nytprofhtml |
92c2ed05 |
200 | |
109f0441 |
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 . |
65acb1b1 |
224 | |
68dc0745 |
225 | =head2 How do I cross-reference my Perl programs? |
226 | |
197aec24 |
227 | The B::Xref module can be used to generate cross-reference reports |
83ded9ee |
228 | for 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 |
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: |
65acb1b1 |
248 | |
249 | set ai sw=4 |
d92eb7b0 |
250 | map! ^O {^M}^[O^T |
65acb1b1 |
251 | |
55e174a4 |
252 | Put that in your F<.exrc> file (replacing the caret characters |
65acb1b1 |
253 | with control characters) and away you go. In insert mode, ^T is |
ac9dac7f |
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 |
213329dd |
256 | http://www.cpan.org/authors/id/TOMC/scripts/toms.exrc.gz |
92c2ed05 |
257 | |
49d635f9 |
258 | The a2ps http://www-inf.enst.fr/%7Edemaille/a2ps/black+white.ps.gz does |
06a5f41f |
259 | lots of things related to generating nicely printed output of |
c195e131 |
260 | documents. |
65acb1b1 |
261 | |
d92eb7b0 |
262 | =head2 Is there a ctags for Perl? |
68dc0745 |
263 | |
b68463f7 |
264 | (contributed by brian d foy) |
265 | |
ac9dac7f |
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 | |
b68463f7 |
270 | Exuberent ctags supports Perl: http://ctags.sourceforge.net/ |
bc06af74 |
271 | |
b68463f7 |
272 | You might also try pltags: http://www.mscha.com/pltags.zip |
65acb1b1 |
273 | |
274 | =head2 Is there an IDE or Windows Perl Editor? |
275 | |
6641ed39 |
276 | Perl programs are just plain text, so any editor will do. |
277 | |
6641ed39 |
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 | |
28b41a80 |
282 | If you want an IDE, check the following (in alphabetical order, not |
283 | order of preference): |
68fbfbd7 |
284 | |
285 | =over 4 |
286 | |
28b41a80 |
287 | =item Eclipse |
288 | |
b68463f7 |
289 | http://e-p-i-c.sf.net/ |
290 | |
6670e5e7 |
291 | The Eclipse Perl Integration Project integrates Perl |
28b41a80 |
292 | editing/debugging with Eclipse. |
293 | |
b68463f7 |
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. |
28b41a80 |
301 | |
68fbfbd7 |
302 | =item Komodo |
303 | |
b68463f7 |
304 | http://www.ActiveState.com/Products/Komodo/ |
305 | |
28b41a80 |
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 |
b68463f7 |
308 | debugger and remote debugging. |
68fbfbd7 |
309 | |
ac1094a1 |
310 | =item Open Perl IDE |
311 | |
b68463f7 |
312 | http://open-perl-ide.sourceforge.net/ |
313 | |
ac1094a1 |
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 | |
28b41a80 |
318 | =item OptiPerl |
319 | |
b68463f7 |
320 | http://www.optiperl.com/ |
321 | |
322 | OptiPerl is a Windows IDE with simulated CGI environment, including |
323 | debugger and syntax highlighting editor. |
28b41a80 |
324 | |
109f0441 |
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 | |
5ca69f12 |
333 | =item PerlBuilder |
334 | |
b68463f7 |
335 | http://www.solutionsoft.com/perl.htm |
336 | |
109f0441 |
337 | PerlBuilder is an integrated development environment for Windows that |
b68463f7 |
338 | supports Perl development. |
8782d048 |
339 | |
68fbfbd7 |
340 | =item visiPerl+ |
341 | |
b68463f7 |
342 | http://helpconsulting.net/visiperl/ |
343 | |
ac1094a1 |
344 | From Help Consulting, for Windows. |
68fbfbd7 |
345 | |
28b41a80 |
346 | =item Visual Perl |
347 | |
b68463f7 |
348 | http://www.activestate.com/Products/Visual_Perl/ |
349 | |
28b41a80 |
350 | Visual Perl is a Visual Studio.NET plug-in from ActiveState. |
29b1171f |
351 | |
b68463f7 |
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: |
29b1171f |
358 | |
68fbfbd7 |
359 | =back |
360 | |
b68463f7 |
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 |
f12f5f55 |
370 | save files as "Text Only". You can also download te |