Initial commit of OpenSPARC T2 design and verification files.
[OpenSPARC-T2-DV] / tools / perl-5.8.0 / man / man1 / perlfaq3.1
CommitLineData
86530b38
AT
1.\" Automatically generated by Pod::Man v1.34, Pod::Parser v1.13
2.\"
3.\" Standard preamble:
4.\" ========================================================================
5.de Sh \" Subsection heading
6.br
7.if t .Sp
8.ne 5
9.PP
10\fB\\$1\fR
11.PP
12..
13.de Sp \" Vertical space (when we can't use .PP)
14.if t .sp .5v
15.if n .sp
16..
17.de Vb \" Begin verbatim text
18.ft CW
19.nf
20.ne \\$1
21..
22.de Ve \" End verbatim text
23.ft R
24.fi
25..
26.\" Set up some character translations and predefined strings. \*(-- will
27.\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left
28.\" double quote, and \*(R" will give a right double quote. | will give a
29.\" real vertical bar. \*(C+ will give a nicer C++. Capital omega is used to
30.\" do unbreakable dashes and therefore won't be available. \*(C` and \*(C'
31.\" expand to `' in nroff, nothing in troff, for use with C<>.
32.tr \(*W-|\(bv\*(Tr
33.ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'
34.ie n \{\
35. ds -- \(*W-
36. ds PI pi
37. if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch
38. if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch
39. ds L" ""
40. ds R" ""
41. ds C` ""
42. ds C' ""
43'br\}
44.el\{\
45. ds -- \|\(em\|
46. ds PI \(*p
47. ds L" ``
48. ds R" ''
49'br\}
50.\"
51.\" If the F register is turned on, we'll generate index entries on stderr for
52.\" titles (.TH), headers (.SH), subsections (.Sh), items (.Ip), and index
53.\" entries marked with X<> in POD. Of course, you'll have to process the
54.\" output yourself in some meaningful fashion.
55.if \nF \{\
56. de IX
57. tm Index:\\$1\t\\n%\t"\\$2"
58..
59. nr % 0
60. rr F
61.\}
62.\"
63.\" For nroff, turn off justification. Always turn off hyphenation; it makes
64.\" way too many mistakes in technical documents.
65.hy 0
66.if n .na
67.\"
68.\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2).
69.\" Fear. Run. Save yourself. No user-serviceable parts.
70. \" fudge factors for nroff and troff
71.if n \{\
72. ds #H 0
73. ds #V .8m
74. ds #F .3m
75. ds #[ \f1
76. ds #] \fP
77.\}
78.if t \{\
79. ds #H ((1u-(\\\\n(.fu%2u))*.13m)
80. ds #V .6m
81. ds #F 0
82. ds #[ \&
83. ds #] \&
84.\}
85. \" simple accents for nroff and troff
86.if n \{\
87. ds ' \&
88. ds ` \&
89. ds ^ \&
90. ds , \&
91. ds ~ ~
92. ds /
93.\}
94.if t \{\
95. ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u"
96. ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u'
97. ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u'
98. ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u'
99. ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u'
100. ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u'
101.\}
102. \" troff and (daisy-wheel) nroff accents
103.ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V'
104.ds 8 \h'\*(#H'\(*b\h'-\*(#H'
105.ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#]
106.ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H'
107.ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u'
108.ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#]
109.ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#]
110.ds ae a\h'-(\w'a'u*4/10)'e
111.ds Ae A\h'-(\w'A'u*4/10)'E
112. \" corrections for vroff
113.if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u'
114.if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u'
115. \" for low resolution devices (crt and lpr)
116.if \n(.H>23 .if \n(.V>19 \
117\{\
118. ds : e
119. ds 8 ss
120. ds o a
121. ds d- d\h'-1'\(ga
122. ds D- D\h'-1'\(hy
123. ds th \o'bp'
124. ds Th \o'LP'
125. ds ae ae
126. ds Ae AE
127.\}
128.rm #[ #] #H #V #F C
129.\" ========================================================================
130.\"
131.IX Title "PERLFAQ3 1"
132.TH PERLFAQ3 1 "2002-06-08" "perl v5.8.0" "Perl Programmers Reference Guide"
133.SH "NAME"
134perlfaq3 \- Programming Tools ($Revision: 1.22 $, $Date: 2002/05/06 13:11:13 $)
135.SH "DESCRIPTION"
136.IX Header "DESCRIPTION"
137This section of the \s-1FAQ\s0 answers questions related to programmer tools
138and programming support.
139.Sh "How do I do (anything)?"
140.IX Subsection "How do I do (anything)?"
141Have you looked at \s-1CPAN\s0 (see perlfaq2)? The chances are that
142someone has already written a module that can solve your problem.
143Have you read the appropriate manpages? Here's a brief index:
144.PP
145.Vb 12
146\& Basics perldata, perlvar, perlsyn, perlop, perlsub
147\& Execution perlrun, perldebug
148\& Functions perlfunc
149\& Objects perlref, perlmod, perlobj, perltie
150\& Data Structures perlref, perllol, perldsc
151\& Modules perlmod, perlmodlib, perlsub
152\& Regexes perlre, perlfunc, perlop, perllocale
153\& Moving to perl5 perltrap, perl
154\& Linking w/C perlxstut, perlxs, perlcall, perlguts, perlembed
155\& Various http://www.cpan.org/misc/olddoc/FMTEYEWTK.tgz
156\& (not a man-page but still useful, a collection
157\& of various essays on Perl techniques)
158.Ve
159.PP
160A crude table of contents for the Perl manpage set is found in perltoc.
161.Sh "How can I use Perl interactively?"
162.IX Subsection "How can I use Perl interactively?"
163The typical approach uses the Perl debugger, described in the
164\&\fIperldebug\fR\|(1) manpage, on an ``empty'' program, like this:
165.PP
166.Vb 1
167\& perl -de 42
168.Ve
169.PP
170Now just type in any legal Perl code, and it will be immediately
171evaluated. You can also examine the symbol table, get stack
172backtraces, check variable values, set breakpoints, and other
173operations typically found in symbolic debuggers.
174.Sh "Is there a Perl shell?"
175.IX Subsection "Is there a Perl shell?"
176In general, not yet. There is psh available at
177.PP
178.Vb 1
179\& http://www.focusresearch.com/gregor/psh
180.Ve
181.PP
182Which includes the following description:
183.PP
184.Vb 5
185\& The Perl Shell is a shell that combines the interactive nature
186\& of a Unix shell with the power of Perl. The goal is to eventually
187\& have a full featured shell that behaves as expected for normal
188\& shell activity. But, the Perl Shell will use Perl syntax and
189\& functionality for control-flow statements and other things.
190.Ve
191.PP
192The Shell.pm module (distributed with Perl) makes Perl try commands
193which aren't part of the Perl language as shell commands. perlsh
194from the source distribution is simplistic and uninteresting, but
195may still be what you want.
196.Sh "How do I debug my Perl programs?"
197.IX Subsection "How do I debug my Perl programs?"
198Have you tried \f(CW\*(C`use warnings\*(C'\fR or used \f(CW\*(C`\-w\*(C'\fR? They enable warnings
199to detect dubious practices.
200.PP
201Have you tried \f(CW\*(C`use strict\*(C'\fR? It prevents you from using symbolic
202references, makes you predeclare any subroutines that you call as bare
203words, and (probably most importantly) forces you to predeclare your
204variables with \f(CW\*(C`my\*(C'\fR, \f(CW\*(C`our\*(C'\fR, or \f(CW\*(C`use vars\*(C'\fR.
205.PP
206Did you check the return values of each and every system call? The operating
207system (and thus Perl) tells you whether they worked, and if not
208why.
209.PP
210.Vb 2
211\& open(FH, "> /etc/cantwrite")
212\& or die "Couldn't write to /etc/cantwrite: $!\en";
213.Ve
214.PP
215Did you read perltrap? It's full of gotchas for old and new Perl
216programmers and even has sections for those of you who are upgrading
217from languages like \fIawk\fR and \fIC\fR.
218.PP
219Have you tried the Perl debugger, described in perldebug? You can
220step through your program and see what it's doing and thus work out
221why what it's doing isn't what it should be doing.
222.Sh "How do I profile my Perl programs?"
223.IX Subsection "How do I profile my Perl programs?"
224You should get the Devel::DProf module from the standard distribution
225(or separately on \s-1CPAN\s0) and also use Benchmark.pm from the standard
226distribution. The Benchmark module lets you time specific portions of
227your code, while Devel::DProf gives detailed breakdowns of where your
228code spends its time.
229.PP
230Here's a sample use of Benchmark:
231.PP
232.Vb 1
233\& use Benchmark;
234.Ve
235.PP
236.Vb 2
237\& @junk = `cat /etc/motd`;
238\& $count = 10_000;
239.Ve
240.PP
241.Vb 10
242\& timethese($count, {
243\& 'map' => sub { my @a = @junk;
244\& map { s/a/b/ } @a;
245\& return @a
246\& },
247\& 'for' => sub { my @a = @junk;
248\& local $_;
249\& for (@a) { s/a/b/ };
250\& return @a },
251\& });
252.Ve
253.PP
254This is what it prints (on one machine\*(--your results will be dependent
255on your hardware, operating system, and the load on your machine):
256.PP
257.Vb 3
258\& Benchmark: timing 10000 iterations of for, map...
259\& for: 4 secs ( 3.97 usr 0.01 sys = 3.98 cpu)
260\& map: 6 secs ( 4.97 usr 0.00 sys = 4.97 cpu)
261.Ve
262.PP
263Be aware that a good benchmark is very hard to write. It only tests the
264data you give it and proves little about the differing complexities
265of contrasting algorithms.
266.Sh "How do I cross-reference my Perl programs?"
267.IX Subsection "How do I cross-reference my Perl programs?"
268The B::Xref module can be used to generate cross-reference reports
269for Perl programs.
270.PP
271.Vb 1
272\& perl -MO=Xref[,OPTIONS] scriptname.plx
273.Ve
274.Sh "Is there a pretty-printer (formatter) for Perl?"
275.IX Subsection "Is there a pretty-printer (formatter) for Perl?"
276Perltidy is a Perl script which indents and reformats Perl scripts
277to make them easier to read by trying to follow the rules of the
278perlstyle. If you write Perl scripts, or spend much time reading
279them, you will probably find it useful. It is available at
280http://perltidy.sourceforge.net
281.PP
282Of course, if you simply follow the guidelines in perlstyle,
283you shouldn't need to reformat. The habit of formatting your code
284as you write it will help prevent bugs. Your editor can and should
285help you with this. The perl-mode or newer cperl-mode for emacs
286can provide remarkable amounts of help with most (but not all)
287code, and even less programmable editors can provide significant
288assistance. Tom Christiansen and many other \s-1VI\s0 users swear by
289the following settings in vi and its clones:
290.PP
291.Vb 2
292\& set ai sw=4
293\& map! ^O {^M}^[O^T
294.Ve
295.PP
296Put that in your \fI.exrc\fR file (replacing the caret characters
297with control characters) and away you go. In insert mode, ^T is
298for indenting, ^D is for undenting, and ^O is for blockdenting\*(--
299as it were. A more complete example, with comments, can be found at
300http://www.cpan.org/authors/id/TOMC/scripts/toms.exrc.gz
301.PP
302The a2ps http://www\-inf.enst.fr/%7Edemaille/a2ps/black+white.ps does
303lots of things related to generating nicely printed output of
304documents, as does enscript at http://people.ssh.fi/mtr/genscript/ .
305.Sh "Is there a ctags for Perl?"
306.IX Subsection "Is there a ctags for Perl?"
307Recent versions of ctags do much more than older versions did.
308\&\s-1EXUBERANT\s0 \s-1CTAGS\s0 is available from http://ctags.sourceforge.net/
309and does a good job of making tags files for perl code.
310.PP
311There is also a simple one at
312http://www.cpan.org/authors/id/TOMC/scripts/ptags.gz which may do
313the trick. It can be easy to hack this into what you want.
314.Sh "Is there an \s-1IDE\s0 or Windows Perl Editor?"
315.IX Subsection "Is there an IDE or Windows Perl Editor?"
316Perl programs are just plain text, so any editor will do.
317.PP
318If you're on Unix, you already have an IDE\*(--Unix itself. The \s-1UNIX\s0
319philosophy is the philosophy of several small tools that each do one
320thing and do it well. It's like a carpenter's toolbox.
321.PP
322If you want an \s-1IDE\s0, check the following:
323.IP "Komodo" 4
324.IX Item "Komodo"
325ActiveState's cross-platform (as of April 2001 Windows and Linux),
326multi-language \s-1IDE\s0 has Perl support, including a regular expression
327debugger and remote debugging
328( http://www.ActiveState.com/Products/Komodo/index.html ). (Visual
329Perl, a Visual Studio.NET plug-in is currently (early 2001) in beta
330( http://www.ActiveState.com/Products/VisualPerl/index.html )).
331.IP "The Object System" 4
332.IX Item "The Object System"
333( http://www.castlelink.co.uk/object_system/ ) is a Perl web
334applications development \s-1IDE\s0, apparently for any platform
335that runs Perl.
336.IP "Open Perl \s-1IDE\s0" 4
337.IX Item "Open Perl IDE"
338( http://open\-perl\-ide.sourceforge.net/ )
339Open Perl \s-1IDE\s0 is an integrated development environment for writing
340and debugging Perl scripts with ActiveState's ActivePerl distribution
341under Windows 95/98/NT/2000.
342.IP "PerlBuilder" 4
343.IX Item "PerlBuilder"
344( http://www.solutionsoft.com/perl.htm ) is an integrated development
345environment for Windows that supports Perl development.
346.IP "visiPerl+" 4
347.IX Item "visiPerl+"
348( http://helpconsulting.net/visiperl/ )
349From Help Consulting, for Windows.
350.IP "OptiPerl" 4
351.IX Item "OptiPerl"
352( http://www.optiperl.com/ ) is a Windows \s-1IDE\s0 with simulated \s-1CGI\s0
353environment, including debugger and syntax highlighting editor.
354.PP
355For Windows there's also the
356.IP "CodeMagicCD" 4
357.IX Item "CodeMagicCD"
358( http://www.codemagiccd.com/ ) Collection of various programming
359tools for Windows: Perl (5.005_03), TclTk, Python, \s-1GNU\s0 programming
360tools, \s-1REBOL\s0, wxWindows toolkit, the MinGW \s-1GNU\s0 C/\*(C+ compiler, \s-1DJGPP\s0
361\&\s-1GNU\s0 C/\*(C+ compiler, Cint C interpreter, YaBasic.
362.PP
363For editors: if you're on Unix you probably have vi or a vi clone already,
364and possibly an emacs too, so you may not need to download anything.
365In any emacs the cperl-mode (M\-x cperl\-mode) gives you perhaps the
366best available Perl editing mode in any editor.
367.PP
368If you are using Windows, you can use any editor that lets
369you work with plain text, such as NotePad or WordPad. Word
370processors, such as Microsoft Word or WordPerfect, typically
371do not work since they insert all sorts of behind-the-scenes
372information, although some allow you to save files as \*(L"Text
373Only\*(R". You can also download text editors designed
374specifically for programming, such as Textpad
375( http://www.textpad.com/ ) and UltraEdit
376( http://www.ultraedit.com/ ), among others.
377.PP
378If you are using Mac \s-1OS\s0, the same concerns apply. MacPerl
379(for Classic environments) comes with a simple editor.
380Popular external editors are BBEdit ( http://www.bbedit.com/ )
381or Alpha ( http://www.kelehers.org/alpha/ ). Mac \s-1OS\s0 X users can
382use Unix editors as well.
383.IP "\s-1GNU\s0 Emacs" 4
384.IX Item "GNU Emacs"
385http://www.gnu.org/software/emacs/windows/ntemacs.html
386.IP "MicroEMACS" 4
387.IX Item "MicroEMACS"
388http://members.nbci.com/uemacs/
389.IP "XEmacs" 4
390.IX Item "XEmacs"
391http://www.xemacs.org/Download/index.html
392.PP
393or a vi clone such as
394.IP "Elvis" 4
395.IX Item "Elvis"
396ftp://ftp.cs.pdx.edu/pub/elvis/ http://www.fh\-wedel.de/elvis/
397.IP "Vile" 4
398.IX Item "Vile"
399http://vile.cx/
400.IP "Vim" 4
401.IX Item "Vim"
402http://www.vim.org/
403.Sp
404win32: http://www.cs.vu.nl/%7Etmgil/vi.html
405.PP
406For vi lovers in general, Windows or elsewhere:
407.PP
408.Vb 1
409\& http://www.thomer.com/thomer/vi/vi.html
410.Ve
411.PP
412nvi ( http://www.bostic.com/vi/ , available from \s-1CPAN\s0 in src/misc/) is
413yet another vi clone, unfortunately not available for Windows, but in
414\&\s-1UNIX\s0 platforms you might be interested in trying it out, firstly because
415strictly speaking it is not a vi clone, it is the real vi, or the new
416incarnation of it, and secondly because you can embed Perl inside it
417to use Perl as the scripting language. nvi is not alone in this,
418though: at least also vim and vile offer an embedded Perl.
419.PP
420The following are Win32 multilanguage editor/IDESs that support Perl:
421.IP "Codewright" 4
422.IX Item "Codewright"
423http://www.starbase.com/
424.IP "MultiEdit" 4
425.IX Item "MultiEdit"
426http://www.MultiEdit.com/
427.IP "SlickEdit" 4
428.IX Item "SlickEdit"
429http://www.slickedit.com/
430.PP
431There is also a toyedit Text widget based editor written in Perl
432that is distributed with the Tk module on \s-1CPAN\s0. The ptkdb
433( http://world.std.com/~aep/ptkdb/ ) is a Perl/tk based debugger that
434acts as a development environment of sorts. Perl Composer
435( http://perlcomposer.sourceforge.net/vperl.html ) is an \s-1IDE\s0 for Perl/Tk
436\&\s-1GUI\s0 creation.
437.PP
438In addition to an editor/IDE you might be interested in a more
439powerful shell environment for Win32. Your options include
440.IP "Bash" 4
441.IX Item "Bash"
442from the Cygwin package ( http://sources.redhat.com/cygwin/ )
443.IP "Ksh" 4
444.IX Item "Ksh"
445from the \s-1MKS\s0 Toolkit ( http://www.mks.com/ ), or the Bourne shell of
446the U/WIN environment ( http://www.research.att.com/sw/tools/uwin/ )
447.IP "Tcsh" 4
448.IX Item "Tcsh"
449ftp://ftp.astron.com/pub/tcsh/ , see also
450http://www.primate.wisc.edu/software/csh\-tcsh\-book/
451.IP "Zsh" 4
452.IX Item "Zsh"
453ftp://ftp.blarg.net/users/amol/zsh/ , see also http://www.zsh.org/
454.PP
455\&\s-1MKS\s0 and U/WIN are commercial (U/WIN is free for educational and
456research purposes), Cygwin is covered by the \s-1GNU\s0 Public License (but
457that shouldn't matter for Perl use). The Cygwin, \s-1MKS\s0, and U/WIN all
458contain (in addition to the shells) a comprehensive set of standard
459\&\s-1UNIX\s0 toolkit utilities.
460.PP
461If you're transferring text files between Unix and Windows using \s-1FTP\s0
462be sure to transfer them in \s-1ASCII\s0 mode so the ends of lines are
463appropriately converted.
464.PP
465On Mac \s-1OS\s0 the MacPerl Application comes with a simple 32k text editor
466that behaves like a rudimentary \s-1IDE\s0. In contrast to the MacPerl Application
467the \s-1MPW\s0 Perl tool can make use of the \s-1MPW\s0 Shell itself as an editor (with
468no 32k limit).
469.IP "BBEdit and BBEdit Lite" 4
470.IX Item "BBEdit and BBEdit Lite"
471are text editors for Mac \s-1OS\s0 that have a Perl sensitivity mode
472( http://web.barebones.com/ ).
473.IP "Alpha" 4
474.IX Item "Alpha"
475is an editor, written and extensible in Tcl, that nonetheless has
476built in support for several popular markup and programming languages
477including Perl and \s-1HTML\s0 ( http://alpha.olm.net/ ).
478.PP
479Pepper and Pe are programming language sensitive text editors for Mac
480\&\s-1OS\s0 X and BeOS respectively ( http://www.hekkelman.com/ ).
481.Sh "Where can I get Perl macros for vi?"
482.IX Subsection "Where can I get Perl macros for vi?"
483For a complete version of Tom Christiansen's vi configuration file,
484see http://www.cpan.org/authors/Tom_Christiansen/scripts/toms.exrc.gz ,
485the standard benchmark file for vi emulators. The file runs best with nvi,
486the current version of vi out of Berkeley, which incidentally can be built
487with an embedded Perl interpreter\*(--see http://www.cpan.org/src/misc/ .
488.Sh "Where can I get perl-mode for emacs?"
489.IX Subsection "Where can I get perl-mode for emacs?"
490Since Emacs version 19 patchlevel 22 or so, there have been both a
491perl\-mode.el and support for the Perl debugger built in. These should
492come with the standard Emacs 19 distribution.
493.PP
494In the Perl source directory, you'll find a directory called \*(L"emacs\*(R",
495which contains a cperl-mode that color-codes keywords, provides
496context-sensitive help, and other nifty things.
497.PP
498Note that the perl-mode of emacs will have fits with \f(CW"main'foo"\fR
499(single quote), and mess up the indentation and highlighting. You
500are probably using \f(CW"main::foo"\fR in new Perl code anyway, so this
501shouldn't be an issue.
502.Sh "How can I use curses with Perl?"
503.IX Subsection "How can I use curses with Perl?"
504The Curses module from \s-1CPAN\s0 provides a dynamically loadable object
505module interface to a curses library. A small demo can be found at the
506directory http://www.cpan.org/authors/Tom_Christiansen/scripts/rep ;
507this program repeats a command and updates the screen as needed, rendering
508\&\fBrep ps axu\fR similar to \fBtop\fR.
509.Sh "How can I use X or Tk with Perl?"
510.IX Subsection "How can I use X or Tk with Perl?"
511Tk is a completely Perl\-based, object-oriented interface to the Tk toolkit
512that doesn't force you to use Tcl just to get at Tk. Sx is an interface
513to the Athena Widget set. Both are available from \s-1CPAN\s0. See the
514directory http://www.cpan.org/modules/by\-category/08_User_Interfaces/
515.PP
516Invaluable for Perl/Tk programming are the Perl/Tk \s-1FAQ\s0 at
517http://w4.lns.cornell.edu/%7Epvhp/ptk/ptkTOC.html , the Perl/Tk Reference
518Guide available at
519http://www.cpan.org/authors/Stephen_O_Lidie/ , and the
520online manpages at
521http://www\-users.cs.umn.edu/%7Eamundson/perl/perltk/toc.html .
522.Sh "How can I generate simple menus without using \s-1CGI\s0 or Tk?"
523.IX Subsection "How can I generate simple menus without using CGI or Tk?"
524The http://www.cpan.org/authors/id/SKUNZ/perlmenu.v4.0.tar.gz
525module, which is curses\-based, can help with this.
526.Sh "How can I make my Perl program run faster?"
527.IX Subsection "How can I make my Perl program run faster?"
528The best way to do this is to come up with a better algorithm. This
529can often make a dramatic difference. Jon Bentley's book
530``Programming Pearls'' (that's not a misspelling!) has some good tips
531on optimization, too. Advice on benchmarking boils down to: benchmark
532and profile to make sure you're optimizing the right part, look for
533better algorithms instead of microtuning your code, and when all else
534fails consider just buying faster hardware. You will probably want to
535read the answer to the earlier question ``How do I profile my Perl programs?''
536if you haven't done so already.
537.PP
538A different approach is to autoload seldom-used Perl code. See the
539AutoSplit and AutoLoader modules in the standard distribution for
540that. Or you could locate the bottleneck and think about writing just
541that part in C, the way we used to take bottlenecks in C code and
542write them in assembler. Similar to rewriting in C,
543modules that have critical sections can be written in C (for instance, the
544\&\s-1PDL\s0 module from \s-1CPAN\s0).
545.PP
546In some cases, it may be worth it to use the backend compiler to
547produce byte code (saving compilation time) or compile into C, which
548will certainly save compilation time and sometimes a small amount (but
549not much) execution time. See the question about compiling your Perl
550programs for more on the compiler\*(--the wins aren't as obvious as you'd
551hope.
552.PP
553If you're currently linking your perl executable to a shared \fIlibc.so\fR,
554you can often gain a 10\-25% performance benefit by rebuilding it to
555link with a static libc.a instead. This will make a bigger perl
556executable, but your Perl programs (and programmers) may thank you for
557it. See the \fI\s-1INSTALL\s0\fR file in the source distribution for more
558information.
559.PP
560Unsubstantiated reports allege that Perl interpreters that use sfio
561outperform those that don't (for I/O intensive applications). To try
562this, see the \fI\s-1INSTALL\s0\fR file in the source distribution, especially
563the ``Selecting File I/O mechanisms'' section.
564.PP
565The undump program was an old attempt to speed up your Perl program
566by storing the already-compiled form to disk. This is no longer
567a viable option, as it only worked on a few architectures, and
568wasn't a good solution anyway.
569.Sh "How can I make my Perl program take less memory?"
570.IX Subsection "How can I make my Perl program take less memory?"
571When it comes to time-space tradeoffs, Perl nearly always prefers to
572throw memory at a problem. Scalars in Perl use more memory than
573strings in C, arrays take more than that, and hashes use even more. While
574there's still a lot to be done, recent releases have been addressing
575these issues. For example, as of 5.004, duplicate hash keys are
576shared amongst all hashes using them, so require no reallocation.
577.PP
578In some cases, using \fIsubstr()\fR or \fIvec()\fR to simulate arrays can be
579highly beneficial. For example, an array of a thousand booleans will
580take at least 20,000 bytes of space, but it can be turned into one
581125\-byte bit vector\*(--a considerable memory savings. The standard
582Tie::SubstrHash module can also help for certain types of data
583structure. If you're working with specialist data structures
584(matrices, for instance) modules that implement these in C may use
585less memory than equivalent Perl modules.
586.PP
587Another thing to try is learning whether your Perl was compiled with
588the system malloc or with Perl's builtin malloc. Whichever one it
589is, try using the other one and see whether this makes a difference.
590Information about malloc is in the \fI\s-1INSTALL\s0\fR file in the source
591distribution. You can find out whether you are using perl's malloc by
592typing \f(CW\*(C`perl \-V:usemymalloc\*(C'\fR.
593.PP
594Of course, the best way to save memory is to not do anything to waste
595it in the first place. Good programming practices can go a long way
596toward this:
597.IP "\(bu Don't slurp!" 4
598.IX Item "Don't slurp!"
599Don't read an entire file into memory if you can process it line
600by line. Or more concretely, use a loop like this:
601.Sp
602.Vb 6
603\& #
604\& # Good Idea
605\& #
606\& while (<FILE>) {
607\& # ...
608\& }
609.Ve
610.Sp
611instead of this:
612.Sp
613.Vb 7
614\& #
615\& # Bad Idea
616\& #
617\& @data = <FILE>;
618\& foreach (@data) {
619\& # ...
620\& }
621.Ve
622.Sp
623When the files you're processing are small, it doesn't much matter which
624way you do it, but it makes a huge difference when they start getting
625larger.
626.IP "\(bu Use map and grep selectively" 4
627.IX Item "Use map and grep selectively"
628Remember that both map and grep expect a \s-1LIST\s0 argument, so doing this:
629.Sp
630.Vb 1
631\& @wanted = grep {/pattern/} <FILE>;
632.Ve
633.Sp
634will cause the entire file to be slurped. For large files, it's better
635to loop:
636.Sp
637.Vb 3
638\& while (<FILE>) {
639\& push(@wanted, $_) if /pattern/;
640\& }
641.Ve
642.IP "\(bu Avoid unnecessary quotes and stringification" 4
643.IX Item "Avoid unnecessary quotes and stringification"
644Don't quote large strings unless absolutely necessary:
645.Sp
646.Vb 1
647\& my $copy = "$large_string";
648.Ve
649.Sp
650makes 2 copies of \f(CW$large_string\fR (one for \f(CW$copy\fR and another for the
651quotes), whereas
652.Sp
653.Vb 1
654\& my $copy = $large_string;
655.Ve
656.Sp
657only makes one copy.
658.Sp
659Ditto for stringifying large arrays:
660.Sp
661.Vb 4
662\& {
663\& local $, = "\en";
664\& print @big_array;
665\& }
666.Ve
667.Sp
668is much more memory-efficient than either
669.Sp
670.Vb 1
671\& print join "\en", @big_array;
672.Ve
673.Sp
674or
675.Sp
676.Vb 4
677\& {
678\& local $" = "\en";
679\& print "@big_array";
680\& }
681.Ve
682.IP "\(bu Pass by reference" 4
683.IX Item "Pass by reference"
684Pass arrays and hashes by reference, not by value. For one thing, it's
685the only way to pass multiple lists or hashes (or both) in a single
686call/return. It also avoids creating a copy of all the contents. This
687requires some judgment, however, because any changes will be propagated
688back to the original data. If you really want to mangle (er, modify) a
689copy, you'll have to sacrifice the memory needed to make one.
690.IP "\(bu Tie large variables to disk." 4
691.IX Item "Tie large variables to disk."
692For \*(L"big\*(R" data stores (i.e. ones that exceed available memory) consider
693using one of the \s-1DB\s0 modules to store it on disk instead of in \s-1RAM\s0. This
694will incur a penalty in access time, but that's probably better than
695causing your hard disk to thrash due to massive swapping.
696.Sh "Is it unsafe to return a pointer to local data?"
697.IX Subsection "Is it unsafe to return a pointer to local data?"
698No, Perl's garbage collection system takes care of this.
699.PP
700.Vb 4
701\& sub makeone {
702\& my @a = ( 1 .. 10 );
703\& return \e@a;
704\& }
705.Ve
706.PP
707.Vb 3
708\& for $i ( 1 .. 10 ) {
709\& push @many, makeone();
710\& }
711.Ve
712.PP
713.Vb 1
714\& print $many[4][5], "\en";
715.Ve
716.PP
717.Vb 1
718\& print "@many\en";
719.Ve
720.Sh "How can I free an array or hash so my program shrinks?"
721.IX Subsection "How can I free an array or hash so my program shrinks?"
722You usually can't. On most operating systems, memory
723allocated to a program can never be returned to the system.
724That's why long-running programs sometimes re-exec
725themselves. Some operating systems (notably, systems that
726use \fImmap\fR\|(2) for allocating large chunks of memory) can
727reclaim memory that is no longer used, but on such systems,
728perl must be configured and compiled to use the \s-1OS\s0's malloc,
729not perl's.
730.PP
731However, judicious use of \fImy()\fR on your variables will help make sure
732that they go out of scope so that Perl can free up that space for
733use in other parts of your program. A global variable, of course, never
734goes out of scope, so you can't get its space automatically reclaimed,
735although \fIundef()\fRing and/or \fIdelete()\fRing it will achieve the same effect.
736In general, memory allocation and de-allocation isn't something you can
737or should be worrying about much in Perl, but even this capability
738(preallocation of data types) is in the works.
739.Sh "How can I make my \s-1CGI\s0 script more efficient?"
740.IX Subsection "How can I make my CGI script more efficient?"
741Beyond the normal measures described to make general Perl programs
742faster or smaller, a \s-1CGI\s0 program has additional issues. It may be run
743several times per second. Given that each time it runs it will need
744to be re-compiled and will often allocate a megabyte or more of system
745memory, this can be a killer. Compiling into C \fBisn't going to help
746you\fR because the process start-up overhead is where the bottleneck is.
747.PP
748There are two popular ways to avoid this overhead. One solution
749involves running the Apache \s-1HTTP\s0 server (available from
750http://www.apache.org/ ) with either of the mod_perl or mod_fastcgi
751plugin modules.
752.PP
753With mod_perl and the Apache::Registry module (distributed with
754mod_perl), httpd will run with an embedded Perl interpreter which
755pre-compiles your script and then executes it within the same address
756space without forking. The Apache extension also gives Perl access to
757the internal server \s-1API\s0, so modules written in Perl can do just about
758anything a module written in C can. For more on mod_perl, see
759http://perl.apache.org/
760.PP
761With the \s-1FCGI\s0 module (from \s-1CPAN\s0) and the mod_fastcgi
762module (available from http://www.fastcgi.com/ ) each of your Perl
763programs becomes a permanent \s-1CGI\s0 daemon process.
764.PP
765Both of these solutions can have far-reaching effects on your system
766and on the way you write your \s-1CGI\s0 programs, so investigate them with
767care.
768.PP
769See http://www.cpan.org/modules/by\-category/15_World_Wide_Web_HTML_HTTP_CGI/ .
770.PP
771A non\-free, commercial product, ``The Velocity Engine for Perl'',
772(http://www.binevolve.com/ or http://www.binevolve.com/velocigen/ )
773might also be worth looking at. It will allow you to increase the
774performance of your Perl programs, running programs up to 25 times
775faster than normal \s-1CGI\s0 Perl when running in persistent Perl mode or 4
776to 5 times faster without any modification to your existing \s-1CGI\s0
777programs. Fully functional evaluation copies are available from the
778web site.
779.Sh "How can I hide the source for my Perl program?"
780.IX Subsection "How can I hide the source for my Perl program?"
781Delete it. :\-) Seriously, there are a number of (mostly
782unsatisfactory) solutions with varying levels of ``security''.
783.PP
784First of all, however, you \fIcan't\fR take away read permission, because
785the source code has to be readable in order to be compiled and
786interpreted. (That doesn't mean that a \s-1CGI\s0 script's source is
787readable by people on the web, though\*(--only by people with access to
788the filesystem.) So you have to leave the permissions at the socially
789friendly 0755 level.
790.PP
791Some people regard this as a security problem. If your program does
792insecure things and relies on people not knowing how to exploit those
793insecurities, it is not secure. It is often possible for someone to
794determine the insecure things and exploit them without viewing the
795source. Security through obscurity, the name for hiding your bugs
796instead of fixing them, is little security indeed.
797.PP
798You can try using encryption via source filters (Starting from Perl
7995.8 the Filter::Simple and Filter::Util::Call modules are included in
800the standard distribution), but any decent programmer will be able to
801decrypt it. You can try using the byte code compiler and interpreter
802described below, but the curious might still be able to de-compile it.
803You can try using the native-code compiler described below, but
804crackers might be able to disassemble it. These pose varying degrees
805of difficulty to people wanting to get at your code, but none can
806definitively conceal it (true of every language, not just Perl).
807.PP
808If you're concerned about people profiting from your code, then the
809bottom line is that nothing but a restrictive license will give you
810legal security. License your software and pepper it with threatening
811statements like ``This is unpublished proprietary software of \s-1XYZ\s0 Corp.
812Your access to it does not give you permission to use it blah blah
813blah.'' We are not lawyers, of course, so you should see a lawyer if
814you want to be sure your license's wording will stand up in court.
815.Sh "How can I compile my Perl program into byte code or C?"
816.IX Subsection "How can I compile my Perl program into byte code or C?"
817Malcolm Beattie has written a multifunction backend compiler,
818available from \s-1CPAN\s0, that can do both these things. It is included
819in the perl5.005 release, but is still considered experimental.
820This means it's fun to play with if you're a programmer but not
821really for people looking for turn-key solutions.
822.PP
823Merely compiling into C does not in and of itself guarantee that your
824code will run very much faster. That's because except for lucky cases
825where a lot of native type inferencing is possible, the normal Perl
826run-time system is still present and so your program will take just as
827long to run and be just as big. Most programs save little more than
828compilation time, leaving execution no more than 10\-30% faster. A few
829rare programs actually benefit significantly (even running several times
830faster), but this takes some tweaking of your code.
831.PP
832You'll probably be astonished to learn that the current version of the
833compiler generates a compiled form of your script whose executable is
834just as big as the original perl executable, and then some. That's
835because as currently written, all programs are prepared for a full
836\&\fIeval()\fR statement. You can tremendously reduce this cost by building a
837shared \fIlibperl.so\fR library and linking against that. See the
838\&\fI\s-1INSTALL\s0\fR podfile in the Perl source distribution for details. If
839you link your main perl binary with this, it will make it minuscule.
840For example, on one author's system, \fI/usr/bin/perl\fR is only 11k in
841size!
842.PP
843In general, the compiler will do nothing to make a Perl program smaller,
844faster, more portable, or more secure. In fact, it can make your
845situation worse. The executable will be bigger, your \s-1VM\s0 system may take
846longer to load the whole thing, the binary is fragile and hard to fix,
847and compilation never stopped software piracy in the form of crackers,
848viruses, or bootleggers. The real advantage of the compiler is merely
849packaging, and once you see the size of what it makes (well, unless
850you use a shared \fIlibperl.so\fR), you'll probably want a complete
851Perl install anyway.
852.Sh "How can I compile Perl into Java?"
853.IX Subsection "How can I compile Perl into Java?"
854You can also integrate Java and Perl with the
855Perl Resource Kit from O'Reilly and Associates. See
856http://www.oreilly.com/catalog/prkunix/ .
857.PP
858Perl 5.6 comes with Java Perl Lingo, or \s-1JPL\s0. \s-1JPL\s0, still in
859development, allows Perl code to be called from Java. See jpl/README
860in the Perl source tree.
861.ie n .Sh "How can I get ""#!perl"" to work on [\s-1MS\-DOS\s0,NT,...]?"
862.el .Sh "How can I get \f(CW#!perl\fP to work on [\s-1MS\-DOS\s0,NT,...]?"
863.IX Subsection "How can I get #!perl to work on [MS-DOS,NT,...]?"
864For \s-1OS/2\s0 just use
865.PP
866.Vb 1
867\& extproc perl -S -your_switches
868.Ve
869.PP
870as the first line in \f(CW\*(C`*.cmd\*(C'\fR file (\f(CW\*(C`\-S\*(C'\fR due to a bug in cmd.exe's
871`extproc' handling). For \s-1DOS\s0 one should first invent a corresponding
872batch file and codify it in \f(CW\*(C`ALTERNATIVE_SHEBANG\*(C'\fR (see the
873\&\fI\s-1INSTALL\s0\fR file in the source distribution for more information).
874.PP
875The Win95/NT installation, when using the ActiveState port of Perl,
876will modify the Registry to associate the \f(CW\*(C`.pl\*(C'\fR extension with the
877perl interpreter. If you install another port, perhaps even building
878your own Win95/NT Perl from the standard sources by using a Windows port
879of gcc (e.g., with cygwin or mingw32), then you'll have to modify
880the Registry yourself. In addition to associating \f(CW\*(C`.pl\*(C'\fR with the
881interpreter, \s-1NT\s0 people can use: \f(CW\*(C`SET PATHEXT=%PATHEXT%;.PL\*(C'\fR to let them
882run the program \f(CW\*(C`install\-linux.pl\*(C'\fR merely by typing \f(CW\*(C`install\-linux\*(C'\fR.
883.PP
884Macintosh Perl programs will have the appropriate Creator and
885Type, so that double-clicking them will invoke the Perl application.
886.PP
887\&\fI\s-1IMPORTANT\s0!\fR: Whatever you do, \s-1PLEASE\s0 don't get frustrated, and just
888throw the perl interpreter into your cgi-bin directory, in order to
889get your programs working for a web server. This is an \s-1EXTREMELY\s0 big
890security risk. Take the time to figure out how to do it correctly.
891.Sh "Can I write useful Perl programs on the command line?"
892.IX Subsection "Can I write useful Perl programs on the command line?"
893Yes. Read perlrun for more information. Some examples follow.
894(These assume standard Unix shell quoting rules.)
895.PP
896.Vb 2
897\& # sum first and last fields
898\& perl -lane 'print $F[0] + $F[-1]' *
899.Ve
900.PP
901.Vb 2
902\& # identify text files
903\& perl -le 'for(@ARGV) {print if -f && -T _}' *
904.Ve
905.PP
906.Vb 2
907\& # remove (most) comments from C program
908\& perl -0777 -pe 's{/\e*.*?\e*/}{}gs' foo.c
909.Ve
910.PP
911.Vb 2
912\& # make file a month younger than today, defeating reaper daemons
913\& perl -e '$X=24*60*60; utime(time(),time() + 30 * $X,@ARGV)' *
914.Ve
915.PP
916.Vb 2
917\& # find first unused uid
918\& perl -le '$i++ while getpwuid($i); print $i'
919.Ve
920.PP
921.Vb 3
922\& # display reasonable manpath
923\& echo $PATH | perl -nl -072 -e '
924\& s![^/+]*$!man!&&-d&&!$s{$_}++&&push@m,$_;END{print"@m"}'
925.Ve
926.PP
927\&\s-1OK\s0, the last one was actually an Obfuscated Perl Contest entry. :\-)
928.Sh "Why don't Perl one-liners work on my DOS/Mac/VMS system?"
929.IX Subsection "Why don't Perl one-liners work on my DOS/Mac/VMS system?"
930The problem is usually that the command interpreters on those systems
931have rather different ideas about quoting than the Unix shells under
932which the one-liners were created. On some systems, you may have to
933change single-quotes to double ones, which you must \fI\s-1NOT\s0\fR do on Unix
934or Plan9 systems. You might also have to change a single % to a %%.
935.PP
936For example:
937.PP
938.Vb 2
939\& # Unix
940\& perl -e 'print "Hello world\en"'
941.Ve
942.PP
943.Vb 2
944\& # DOS, etc.
945\& perl -e "print \e"Hello world\en\e""
946.Ve
947.PP
948.Vb 3
949\& # Mac
950\& print "Hello world\en"
951\& (then Run "Myscript" or Shift-Command-R)
952.Ve
953.PP
954.Vb 2
955\& # MPW
956\& perl -e 'print "Hello world\en"'
957.Ve
958.PP
959.Vb 2
960\& # VMS
961\& perl -e "print ""Hello world\en"""
962.Ve
963.PP
964The problem is that none of these examples are reliable: they depend on the
965command interpreter. Under Unix, the first two often work. Under \s-1DOS\s0,
966it's entirely possible that neither works. If 4DOS was the command shell,
967you'd probably have better luck like this:
968.PP
969.Vb 1
970\& perl -e "print <Ctrl-x>"Hello world\en<Ctrl-x>""
971.Ve
972.PP
973Under the Mac, it depends which environment you are using. The MacPerl
974shell, or \s-1MPW\s0, is much like Unix shells in its support for several
975quoting variants, except that it makes free use of the Mac's non-ASCII
976characters as control characters.
977.PP
978Using \fIqq()\fR, q(), and \fIqx()\fR, instead of \*(L"double quotes\*(R", 'single
979quotes', and `backticks`, may make one-liners easier to write.
980.PP
981There is no general solution to all of this. It is a mess.
982.PP
983[Some of this answer was contributed by Kenneth Albanowski.]
984.Sh "Where can I learn about \s-1CGI\s0 or Web programming in Perl?"
985.IX Subsection "Where can I learn about CGI or Web programming in Perl?"
986For modules, get the \s-1CGI\s0 or \s-1LWP\s0 modules from \s-1CPAN\s0. For textbooks,
987see the two especially dedicated to web stuff in the question on
988books. For problems and questions related to the web, like ``Why
989do I get 500 Errors'' or ``Why doesn't it run from the browser right
990when it runs fine on the command line'', see the troubleshooting
991guides and references in perlfaq9 or in the \s-1CGI\s0 MetaFAQ:
992.PP
993.Vb 1
994\& http://www.perl.org/CGI_MetaFAQ.html
995.Ve
996.Sh "Where can I learn about object-oriented Perl programming?"
997.IX Subsection "Where can I learn about object-oriented Perl programming?"
998A good place to start is perltoot, and you can use perlobj,
999perlboot, perltoot, perltooc, and perlbot for reference.
1000(If you are using really old Perl, you may not have all of these,
1001try http://www.perldoc.com/ , but consider upgrading your perl.)
1002.PP
1003A good book on \s-1OO\s0 on Perl is the \*(L"Object\-Oriented Perl\*(R"
1004by Damian Conway from Manning Publications,
1005http://www.manning.com/Conway/index.html
1006.Sh "Where can I learn about linking C with Perl? [h2xs, xsubpp]"
1007.IX Subsection "Where can I learn about linking C with Perl? [h2xs, xsubpp]"
1008If you want to call C from Perl, start with perlxstut,
1009moving on to perlxs, xsubpp, and perlguts. If you want to
1010call Perl from C, then read perlembed, perlcall, and
1011perlguts. Don't forget that you can learn a lot from looking at
1012how the authors of existing extension modules wrote their code and
1013solved their problems.
1014.Sh "I've read perlembed, perlguts, etc., but I can't embed perl in my C program; what am I doing wrong?"
1015.IX Subsection "I've read perlembed, perlguts, etc., but I can't embed perl in my C program; what am I doing wrong?"
1016Download the ExtUtils::Embed kit from \s-1CPAN\s0 and run `make test'. If
1017the tests pass, read the pods again and again and again. If they
1018fail, see perlbug and send a bug report with the output of
1019\&\f(CW\*(C`make test TEST_VERBOSE=1\*(C'\fR along with \f(CW\*(C`perl \-V\*(C'\fR.
1020.Sh "When I tried to run my script, I got this message. What does it mean?"
1021.IX Subsection "When I tried to run my script, I got this message. What does it mean?"
1022A complete list of Perl's error messages and warnings with explanatory
1023text can be found in perldiag. You can also use the splain program
1024(distributed with Perl) to explain the error messages:
1025.PP
1026.Vb 2
1027\& perl program 2>diag.out
1028\& splain [-v] [-p] diag.out
1029.Ve
1030.PP
1031or change your program to explain the messages for you:
1032.PP
1033.Vb 1
1034\& use diagnostics;
1035.Ve
1036.PP
1037or
1038.PP
1039.Vb 1
1040\& use diagnostics -verbose;
1041.Ve
1042.Sh "What's MakeMaker?"
1043.IX Subsection "What's MakeMaker?"
1044This module (part of the standard Perl distribution) is designed to
1045write a Makefile for an extension module from a Makefile.PL. For more
1046information, see ExtUtils::MakeMaker.
1047.SH "AUTHOR AND COPYRIGHT"
1048.IX Header "AUTHOR AND COPYRIGHT"
1049Copyright (c) 1997\-2002 Tom Christiansen and Nathan Torkington.
1050All rights reserved.
1051.PP
1052This documentation is free; you can redistribute it and/or modify it
1053under the same terms as Perl itself.
1054.PP
1055Irrespective of its distribution, all code examples here are in the public
1056domain. You are permitted and encouraged to use this code and any
1057derivatives thereof in your own programs for fun or for profit as you
1058see fit. A simple comment in the code giving credit to the \s-1FAQ\s0 would
1059be courteous but is not required.