1=head1 NAME
2
3perlfaq3 - Programming Tools
4
5=head1 VERSION
6
7version 5.021010
8
9=head1 DESCRIPTION
10
11This section of the FAQ answers questions related to programmer tools
12and programming support.
13
14=head2 How do I do (anything)?
15
16Have you looked at CPAN (see L<perlfaq2>)? The chances are that
17someone has already written a module that can solve your problem.
18Have you read the appropriate manpages? Here's a brief index:
19
20=over 4
21
22=item Basics
23
24=over 4
25
26=item L<perldata> - Perl data types
27
28=item L<perlvar> - Perl pre-defined variables
29
30=item L<perlsyn> - Perl syntax
31
32=item L<perlop> - Perl operators and precedence
33
34=item L<perlsub> - Perl subroutines
35
36=back
37
38
39=item Execution
40
41=over 4
42
43=item L<perlrun> - how to execute the Perl interpreter
44
45=item L<perldebug> - Perl debugging
46
47=back
48
49
50=item Functions
51
52=over 4
53
54=item L<perlfunc> - Perl builtin functions
55
56=back
57
58=item Objects
59
60=over 4
61
62=item L<perlref> - Perl references and nested data structures
63
64=item L<perlmod> - Perl modules (packages and symbol tables)
65
66=item L<perlobj> - Perl objects
67
68=item L<perltie> - how to hide an object class in a simple variable
69
70=back
71
72
73=item Data Structures
74
75=over 4
76
77=item L<perlref> - Perl references and nested data structures
78
79=item L<perllol> - Manipulating arrays of arrays in Perl
80
81=item L<perldsc> - Perl Data Structures Cookbook
82
83=back
84
85=item Modules
86
87=over 4
88
89=item L<perlmod> - Perl modules (packages and symbol tables)
90
91=item L<perlmodlib> - constructing new Perl modules and finding existing ones
92
93=back
94
95
96=item Regexes
97
98=over 4
99
100=item L<perlre> - Perl regular expressions
101
102=item L<perlfunc> - Perl builtin functions>
103
104=item L<perlop> - Perl operators and precedence
105
106=item L<perllocale> - Perl locale handling (internationalization and localization)
107
108=back
109
110
111=item Moving to perl5
112
113=over 4
114
115=item L<perltrap> - Perl traps for the unwary
116
117=item L<perl>
118
119=back
120
121
122=item Linking with C
123
124=over 4
125
126=item L<perlxstut> - Tutorial for writing XSUBs
127
128=item L<perlxs> - XS language reference manual
129
130=item L<perlcall> - Perl calling conventions from C
131
132=item L<perlguts> - Introduction to the Perl API
133
134=item L<perlembed> - how to embed perl in your C program
135
136=back
137
138=item Various
139
140L<http://www.cpan.org/misc/olddoc/FMTEYEWTK.tgz>
141(not a man-page but still useful, a collection of various essays on
142Perl techniques)
143
144=back
145
146A crude table of contents for the Perl manpage set is found in L<perltoc>.
147
148=head2 How can I use Perl interactively?
149
150The typical approach uses the Perl debugger, described in the
151L<perldebug(1)> manpage, on an "empty" program, like this:
152
153    perl -de 42
154
155Now just type in any legal Perl code, and it will be immediately
156evaluated. You can also examine the symbol table, get stack
157backtraces, check variable values, set breakpoints, and other
158operations typically found in symbolic debuggers.
159
160You can also use L<Devel::REPL> which is an interactive shell for Perl,
161commonly known as a REPL - Read, Evaluate, Print, Loop. It provides
162various handy features.
163
164=head2 How do I find which modules are installed on my system?
165
166From the command line, you can use the C<cpan> command's C<-l> switch:
167
168    $ cpan -l
169
170You can also use C<cpan>'s C<-a> switch to create an autobundle file
171that C<CPAN.pm> understands and can use to re-install every module:
172
173    $ cpan -a
174
175Inside a Perl program, you can use the L<ExtUtils::Installed> module to
176show all installed distributions, although it can take awhile to do
177its magic. The standard library which comes with Perl just shows up
178as "Perl" (although you can get those with L<Module::CoreList>).
179
180    use ExtUtils::Installed;
181
182    my $inst    = ExtUtils::Installed->new();
183    my @modules = $inst->modules();
184
185If you want a list of all of the Perl module filenames, you
186can use L<File::Find::Rule>:
187
188    use File::Find::Rule;
189
190    my @files = File::Find::Rule->
191        extras({follow => 1})->
192        file()->
193        name( '*.pm' )->
194        in( @INC )
195        ;
196
197If you do not have that module, you can do the same thing
198with L<File::Find> which is part of the standard library:
199
200    use File::Find;
201    my @files;
202
203    find(
204        {
205        wanted => sub {
206            push @files, $File::Find::fullname
207            if -f $File::Find::fullname && /\.pm$/
208        },
209        follow => 1,
210        follow_skip => 2,
211        },
212        @INC
213    );
214
215    print join "\n", @files;
216
217If you simply need to check quickly to see if a module is
218available, you can check for its documentation. If you can
219read the documentation the module is most likely installed.
220If you cannot read the documentation, the module might not
221have any (in rare cases):
222
223    $ perldoc Module::Name
224
225You can also try to include the module in a one-liner to see if
226perl finds it:
227
228    $ perl -MModule::Name -e1
229
230(If you don't receive a "Can't locate ... in @INC" error message, then Perl
231found the module name you asked for.)
232
233=head2 How do I debug my Perl programs?
234
235(contributed by brian d foy)
236
237Before you do anything else, you can help yourself by ensuring that
238you let Perl tell you about problem areas in your code. By turning
239on warnings and strictures, you can head off many problems before
240they get too big. You can find out more about these in L<strict>
241and L<warnings>.
242
243    #!/usr/bin/perl
244    use strict;
245    use warnings;
246
247Beyond that, the simplest debugger is the C<print> function. Use it
248to look at values as you run your program:
249
250    print STDERR "The value is [$value]\n";
251
252The L<Data::Dumper> module can pretty-print Perl data structures:
253
254    use Data::Dumper qw( Dumper );
255    print STDERR "The hash is " . Dumper( \%hash ) . "\n";
256
257Perl comes with an interactive debugger, which you can start with the
258C<-d> switch. It's fully explained in L<perldebug>.
259
260If you'd like a graphical user interface and you have L<Tk>, you can use
261C<ptkdb>. It's on CPAN and available for free.
262
263If you need something much more sophisticated and controllable, Leon
264Brocard's L<Devel::ebug> (which you can call with the C<-D> switch as C<-Debug>)
265gives you the programmatic hooks into everything you need to write your
266own (without too much pain and suffering).
267
268You can also use a commercial debugger such as Affrus (Mac OS X), Komodo
269from Activestate (Windows and Mac OS X), or EPIC (most platforms).
270
271=head2 How do I profile my Perl programs?
272
273(contributed by brian d foy, updated Fri Jul 25 12:22:26 PDT 2008)
274
275The C<Devel> namespace has several modules which you can use to
276profile your Perl programs.
277
278The L<Devel::NYTProf> (New York Times Profiler) does both statement
279and subroutine profiling. It's available from CPAN and you also invoke
280it with the C<-d> switch:
281
282    perl -d:NYTProf some_perl.pl
283
284It creates a database of the profile information that you can turn into
285reports. The C<nytprofhtml> command turns the data into an HTML report
286similar to the L<Devel::Cover> report:
287
288    nytprofhtml
289
290You might also be interested in using the L<Benchmark> to
291measure and compare code snippets.
292
293You can read more about profiling in I<Programming Perl>, chapter 20,
294or I<Mastering Perl>, chapter 5.
295
296L<perldebguts> documents creating a custom debugger if you need to
297create a special sort of profiler. brian d foy describes the process
298in I<The Perl Journal>, "Creating a Perl Debugger",
299L<http://www.ddj.com/184404522> , and "Profiling in Perl"
300L<http://www.ddj.com/184404580> .
301
302Perl.com has two interesting articles on profiling: "Profiling Perl",
303by Simon Cozens, L<http://www.perl.com/lpt/a/850> and "Debugging and
304Profiling mod_perl Applications", by Frank Wiles,
305L<http://www.perl.com/pub/a/2006/02/09/debug_mod_perl.html> .
306
307Randal L. Schwartz writes about profiling in "Speeding up Your Perl
308Programs" for I<Unix Review>,
309L<http://www.stonehenge.com/merlyn/UnixReview/col49.html> , and "Profiling
310in Template Toolkit via Overriding" for I<Linux Magazine>,
311L<http://www.stonehenge.com/merlyn/LinuxMag/col75.html> .
312
313=head2 How do I cross-reference my Perl programs?
314
315The L<B::Xref> module can be used to generate cross-reference reports
316for Perl programs.
317
318    perl -MO=Xref[,OPTIONS] scriptname.plx
319
320=head2 Is there a pretty-printer (formatter) for Perl?
321
322L<Perl::Tidy> comes with a perl script L<perltidy> which indents and
323reformats Perl scripts to make them easier to read by trying to follow
324the rules of the L<perlstyle>. If you write Perl, or spend much time reading
325Perl, you will probably find it useful.
326
327Of course, if you simply follow the guidelines in L<perlstyle>,
328you shouldn't need to reformat. The habit of formatting your code
329as you write it will help prevent bugs. Your editor can and should
330help you with this. The perl-mode or newer cperl-mode for emacs
331can provide remarkable amounts of help with most (but not all)
332code, and even less programmable editors can provide significant
333assistance. Tom Christiansen and many other VI users swear by
334the following settings in vi and its clones:
335
336    set ai sw=4
337    map! ^O {^M}^[O^T
338
339Put that in your F<.exrc> file (replacing the caret characters
340with control characters) and away you go. In insert mode, ^T is
341for indenting, ^D is for undenting, and ^O is for blockdenting--as
342it were. A more complete example, with comments, can be found at
343L<http://www.cpan.org/authors/id/TOMC/scripts/toms.exrc.gz>
344
345=head2 Is there an IDE or Windows Perl Editor?
346
347Perl programs are just plain text, so any editor will do.
348
349If you're on Unix, you already have an IDE--Unix itself. The Unix
350philosophy is the philosophy of several small tools that each do one
351thing and do it well. It's like a carpenter's toolbox.
352
353If you want an IDE, check the following (in alphabetical order, not
354order of preference):
355
356=over 4
357
358=item Eclipse
359
360L<http://e-p-i-c.sf.net/>
361
362The Eclipse Perl Integration Project integrates Perl
363editing/debugging with Eclipse.
364
365=item Enginsite
366
367L<http://www.enginsite.com/>
368
369Perl Editor by EngInSite is a complete integrated development
370environment (IDE) for creating, testing, and  debugging  Perl scripts;
371the tool runs on Windows 9x/NT/2000/XP or later.
372
373=item Kephra
374
375L<http://kephra.sf.net>
376
377GUI editor written in Perl using wxWidgets and Scintilla with lots of smaller features.
378Aims for a UI based on Perl principles like TIMTOWTDI and "easy things should be easy,
379hard things should be possible".
380
381=item Komodo
382
383L<http://www.ActiveState.com/Products/Komodo/>
384
385ActiveState's cross-platform (as of October 2004, that's Windows, Linux,
386and Solaris), multi-language IDE has Perl support, including a regular expression
387debugger and remote debugging.
388
389=item Notepad++
390
391L<http://notepad-plus.sourceforge.net/>
392
393=item Open Perl IDE
394
395L<http://open-perl-ide.sourceforge.net/>
396
397Open Perl IDE is an integrated development environment for writing
398and debugging Perl scripts with ActiveState's ActivePerl distribution
399under Windows 95/98/NT/2000.
400
401=item OptiPerl
402
403L<http://www.optiperl.com/>
404
405OptiPerl is a Windows IDE with simulated CGI environment, including
406debugger and syntax-highlighting editor.
407
408=item Padre
409
410L<http://padre.perlide.org/>
411
412Padre is cross-platform IDE for Perl written in Perl using wxWidgets to provide
413a native look and feel. It's open source under the Artistic License. It
414is one of the newer Perl IDEs.
415
416=item PerlBuilder
417
418L<http://www.solutionsoft.com/perl.htm>
419
420PerlBuilder is an integrated development environment for Windows that
421supports Perl development.
422
423=item visiPerl+
424
425L<http://helpconsulting.net/visiperl/index.html>
426
427From Help Consulting, for Windows.
428
429=item Visual Perl
430
431L<http://www.activestate.com/Products/Visual_Perl/>
432
433Visual Perl is a Visual Studio.NET plug-in from ActiveState.
434
435=item Zeus
436
437L<http://www.zeusedit.com/lookmain.html>
438
439Zeus for Windows is another Win32 multi-language editor/IDE
440that comes with support for Perl.
441
442=back
443
444For editors: if you're on Unix you probably have vi or a vi clone
445already, and possibly an emacs too, so you may not need to download
446anything. In any emacs the cperl-mode (M-x cperl-mode) gives you
447perhaps the best available Perl editing mode in any editor.
448
449If you are using Windows, you can use any editor that lets you work
450with plain text, such as NotePad or WordPad. Word processors, such as
451Microsoft Word or WordPerfect, typically do not work since they insert
452all sorts of behind-the-scenes information, although some allow you to
453save files as "Text Only". You can also download text editors designed
454specifically for programming, such as Textpad (
455L<http://www.textpad.com/> ) and UltraEdit ( L<http://www.ultraedit.com/> ),
456among others.
457
458If you are using MacOS, the same concerns apply. MacPerl (for Classic
459environments) comes with a simple editor. Popular external editors are
460BBEdit ( L<http://www.barebones.com/products/bbedit/> ) or Alpha (
461L<http://www.his.com/~jguyer/Alpha/Alpha8.html> ). MacOS X users can use
462Unix editors as well.
463
464=over 4
465
466=item GNU Emacs
467
468L<http://www.gnu.org/software/emacs/windows/ntemacs.html>
469
470=item MicroEMACS
471
472L<http://www.microemacs.de/>
473
474=item XEmacs
475
476L<http://www.xemacs.org/Download/index.html>
477
478=item Jed
479
480L<http://space.mit.edu/~davis/jed/>
481
482=back
483
484or a vi clone such as
485
486=over 4
487
488=item Vim
489
490L<http://www.vim.org/>
491
492=item Vile
493
494L<http://dickey.his.com/vile/vile.html>
495
496=back
497
498The following are Win32 multilanguage editor/IDEs that support Perl:
499
500=over 4
501
502=item MultiEdit
503
504L<http://www.MultiEdit.com/>
505
506=item SlickEdit
507
508L<http://www.slickedit.com/>
509
510=item ConTEXT
511
512L<http://www.contexteditor.org/>
513
514=back
515
516There is also a toyedit Text widget based editor written in Perl
517that is distributed with the Tk module on CPAN. The ptkdb
518( L<http://ptkdb.sourceforge.net/> ) is a Perl/Tk-based debugger that
519acts as a development environment of sorts. Perl Composer
520( L<http://perlcomposer.sourceforge.net/> ) is an IDE for Perl/Tk
521GUI creation.
522
523In addition to an editor/IDE you might be interested in a more
524powerful shell environment for Win32. Your options include
525
526=over 4
527
528=item bash
529
530from the Cygwin package ( L<http://cygwin.com/> )
531
532=item zsh
533
534L<http://www.zsh.org/>
535
536=back
537
538Cygwin is covered by the GNU General Public
539License (but that shouldn't matter for Perl use). Cygwin
540contains (in addition to the shell) a comprehensive set
541of standard Unix toolkit utilities.
542
543=over 4
544
545=item BBEdit and TextWrangler
546
547are text editors for OS X that have a Perl sensitivity mode
548( L<http://www.barebones.com/> ).
549
550=back
551
552=head2 Where can I get Perl macros for vi?
553
554For a complete version of Tom Christiansen's vi configuration file,
555see L<http://www.cpan.org/authors/Tom_Christiansen/scripts/toms.exrc.gz> ,
556the standard benchmark file for vi emulators. The file runs best with nvi,
557the current version of vi out of Berkeley, which incidentally can be built
558with an embedded Perl interpreter--see L<http://www.cpan.org/src/misc/> .
559
560=head2 Where can I get perl-mode or cperl-mode for emacs?
561X<emacs>
562
563Since Emacs version 19 patchlevel 22 or so, there have been both a
564perl-mode.el and support for the Perl debugger built in. These should
565come with the standard Emacs 19 distribution.
566
567Note that the perl-mode of emacs will have fits with C<"main'foo">
568(single quote), and mess up the indentation and highlighting. You
569are probably using C<"main::foo"> in new Perl code anyway, so this
570shouldn't be an issue.
571
572For CPerlMode, see L<http://www.emacswiki.org/cgi-bin/wiki/CPerlMode>
573
574=head2 How can I use curses with Perl?
575
576The Curses module from CPAN provides a dynamically loadable object
577module interface to a curses library. A small demo can be found at the
578directory L<http://www.cpan.org/authors/Tom_Christiansen/scripts/rep.gz> ;
579this program repeats a command and updates the screen as needed, rendering
580B<rep ps axu> similar to B<top>.
581
582=head2 How can I write a GUI (X, Tk, Gtk, etc.) in Perl?
583X<GUI> X<Tk> X<Wx> X<WxWidgets> X<Gtk> X<Gtk2> X<CamelBones> X<Qt>
584
585(contributed by Ben Morrow)
586
587There are a number of modules which let you write GUIs in Perl. Most
588GUI toolkits have a perl interface: an incomplete list follows.
589
590=over 4
591
592=item Tk
593
594This works under Unix and Windows, and the current version doesn't
595look half as bad under Windows as it used to. Some of the gui elements
596still don't 'feel' quite right, though. The interface is very natural
597and 'perlish', making it easy to use in small scripts that just need a
598simple gui. It hasn't been updated in a while.
599
600=item Wx
601
602This is a Perl binding for the cross-platform wxWidgets toolkit
603( L<http://www.wxwidgets.org> ). It works under Unix, Win32 and Mac OS X,
604using native widgets (Gtk under Unix). The interface follows the C++
605interface closely, but the documentation is a little sparse for someone
606who doesn't know the library, mostly just referring you to the C++
607documentation.
608
609=item Gtk and Gtk2
610
611These are Perl bindings for the Gtk toolkit ( L<http://www.gtk.org> ). The
612interface changed significantly between versions 1 and 2 so they have
613separate Perl modules. It runs under Unix, Win32 and Mac OS X (currently
614it requires an X server on Mac OS, but a 'native' port is underway), and
615the widgets look the same on every platform: i.e., they don't match the
616native widgets. As with Wx, the Perl bindings follow the C API closely,
617and the documentation requires you to read the C documentation to
618understand it.
619
620=item Win32::GUI
621
622This provides access to most of the Win32 GUI widgets from Perl.
623Obviously, it only runs under Win32, and uses native widgets. The Perl
624interface doesn't really follow the C interface: it's been made more
625Perlish, and the documentation is pretty good. More advanced stuff may
626require familiarity with the C Win32 APIs, or reference to MSDN.
627
628=item CamelBones
629
630CamelBones ( L<http://camelbones.sourceforge.net> ) is a Perl interface to
631Mac OS X's Cocoa GUI toolkit, and as such can be used to produce native
632GUIs on Mac OS X. It's not on CPAN, as it requires frameworks that
633CPAN.pm doesn't know how to install, but installation is via the
634standard OSX package installer. The Perl API is, again, very close to
635the ObjC API it's wrapping, and the documentation just tells you how to
636translate from one to the other.
637
638=item Qt
639
640There is a Perl interface to TrollTech's Qt toolkit, but it does not
641appear to be maintained.
642
643=item Athena
644
645Sx is an interface to the Athena widget set which comes with X, but
646again it appears not to be much used nowadays.
647
648=back
649
650=head2 How can I make my Perl program run faster?
651
652The best way to do this is to come up with a better algorithm. This
653can often make a dramatic difference. Jon Bentley's book
654I<Programming Pearls> (that's not a misspelling!)  has some good tips
655on optimization, too. Advice on benchmarking boils down to: benchmark
656and profile to make sure you're optimizing the right part, look for
657better algorithms instead of microtuning your code, and when all else
658fails consider just buying faster hardware. You will probably want to
659read the answer to the earlier question "How do I profile my Perl
660programs?" if you haven't done so already.
661
662A different approach is to autoload seldom-used Perl code. See the
663AutoSplit and AutoLoader modules in the standard distribution for
664that. Or you could locate the bottleneck and think about writing just
665that part in C, the way we used to take bottlenecks in C code and
666write them in assembler. Similar to rewriting in C, modules that have
667critical sections can be written in C (for instance, the PDL module
668from CPAN).
669
670If you're currently linking your perl executable to a shared
671I<libc.so>, you can often gain a 10-25% performance benefit by
672rebuilding it to link with a static libc.a instead. This will make a
673bigger perl executable, but your Perl programs (and programmers) may
674thank you for it. See the F<INSTALL> file in the source distribution
675for more information.
676
677The undump program was an ancient attempt to speed up Perl program by
678storing the already-compiled form to disk. This is no longer a viable
679option, as it only worked on a few architectures, and wasn't a good
680solution anyway.
681
682=head2 How can I make my Perl program take less memory?
683
684When it comes to time-space tradeoffs, Perl nearly always prefers to
685throw memory at a problem. Scalars in Perl use more memory than
686strings in C, arrays take more than that, and hashes use even more. While
687there's still a lot to be done, recent releases have been addressing
688these issues. For example, as of 5.004, duplicate hash keys are
689shared amongst all hashes using them, so require no reallocation.
690
691In some cases, using substr() or vec() to simulate arrays can be
692highly beneficial. For example, an array of a thousand booleans will
693take at least 20,000 bytes of space, but it can be turned into one
694125-byte bit vector--a considerable memory savings. The standard
695Tie::SubstrHash module can also help for certain types of data
696structure. If you're working with specialist data structures
697(matrices, for instance) modules that implement these in C may use
698less memory than equivalent Perl modules.
699
700Another thing to try is learning whether your Perl was compiled with
701the system malloc or with Perl's builtin malloc. Whichever one it
702is, try using the other one and see whether this makes a difference.
703Information about malloc is in the F<INSTALL> file in the source
704distribution. You can find out whether you are using perl's malloc by
705typing C<perl -V:usemymalloc>.
706
707Of course, the best way to save memory is to not do anything to waste
708it in the first place. Good programming practices can go a long way
709toward this:
710
711=over 4
712
713=item Don't slurp!
714
715Don't read an entire file into memory if you can process it line
716by line. Or more concretely, use a loop like this:
717
718    #
719    # Good Idea
720    #
721    while (my $line = <$file_handle>) {
722       # ...
723    }
724
725instead of this:
726
727    #
728    # Bad Idea
729    #
730    my @data = <$file_handle>;
731    foreach (@data) {
732        # ...
733    }
734
735When the files you're processing are small, it doesn't much matter which
736way you do it, but it makes a huge difference when they start getting
737larger.
738
739=item Use map and grep selectively
740
741Remember that both map and grep expect a LIST argument, so doing this:
742
743        @wanted = grep {/pattern/} <$file_handle>;
744
745will cause the entire file to be slurped. For large files, it's better
746to loop:
747
748        while (<$file_handle>) {
749                push(@wanted, $_) if /pattern/;
750        }
751
752=item Avoid unnecessary quotes and stringification
753
754Don't quote large strings unless absolutely necessary:
755
756        my $copy = "$large_string";
757
758makes 2 copies of $large_string (one for $copy and another for the
759quotes), whereas
760
761        my $copy = $large_string;
762
763only makes one copy.
764
765Ditto for stringifying large arrays:
766
767    {
768    local $, = "\n";
769    print @big_array;
770    }
771
772is much more memory-efficient than either
773
774    print join "\n", @big_array;
775
776or
777
778    {
779    local $" = "\n";
780    print "@big_array";
781    }
782
783
784=item Pass by reference
785
786Pass arrays and hashes by reference, not by value. For one thing, it's
787the only way to pass multiple lists or hashes (or both) in a single
788call/return. It also avoids creating a copy of all the contents. This
789requires some judgement, however, because any changes will be propagated
790back to the original data. If you really want to mangle (er, modify) a
791copy, you'll have to sacrifice the memory needed to make one.
792
793=item Tie large variables to disk
794
795For "big" data stores (i.e. ones that exceed available memory) consider
796using one of the DB modules to store it on disk instead of in RAM. This
797will incur a penalty in access time, but that's probably better than
798causing your hard disk to thrash due to massive swapping.
799
800=back
801
802=head2 Is it safe to return a reference to local or lexical data?
803
804Yes. Perl's garbage collection system takes care of this so
805everything works out right.
806
807    sub makeone {
808        my @a = ( 1 .. 10 );
809        return \@a;
810    }
811
812    for ( 1 .. 10 ) {
813        push @many, makeone();
814    }
815
816    print $many[4][5], "\n";
817
818    print "@many\n";
819
820=head2 How can I free an array or hash so my program shrinks?
821
822(contributed by Michael Carman)
823
824You usually can't. Memory allocated to lexicals (i.e. my() variables)
825cannot be reclaimed or reused even if they go out of scope. It is
826reserved in case the variables come back into scope. Memory allocated
827to global variables can be reused (within your program) by using
828undef() and/or delete().
829
830On most operating systems, memory allocated to a program can never be
831returned to the system. That's why long-running programs sometimes re-
832exec themselves. Some operating systems (notably, systems that use
833mmap(2) for allocating large chunks of memory) can reclaim memory that
834is no longer used, but on such systems, perl must be configured and
835compiled to use the OS's malloc, not perl's.
836
837In general, memory allocation and de-allocation isn't something you can
838or should be worrying about much in Perl.
839
840See also "How can I make my Perl program take less memory?"
841
842=head2 How can I make my CGI script more efficient?
843
844Beyond the normal measures described to make general Perl programs
845faster or smaller, a CGI program has additional issues. It may be run
846several times per second. Given that each time it runs it will need
847to be re-compiled and will often allocate a megabyte or more of system
848memory, this can be a killer. Compiling into C B<isn't going to help
849you> because the process start-up overhead is where the bottleneck is.
850
851There are three popular ways to avoid this overhead. One solution
852involves running the Apache HTTP server (available from
853L<http://www.apache.org/> ) with either of the mod_perl or mod_fastcgi
854plugin modules.
855
856With mod_perl and the Apache::Registry module (distributed with
857mod_perl), httpd will run with an embedded Perl interpreter which
858pre-compiles your script and then executes it within the same address
859space without forking. The Apache extension also gives Perl access to
860the internal server API, so modules written in Perl can do just about
861anything a module written in C can. For more on mod_perl, see
862L<http://perl.apache.org/>
863
864With the FCGI module (from CPAN) and the mod_fastcgi
865module (available from L<http://www.fastcgi.com/> ) each of your Perl
866programs becomes a permanent CGI daemon process.
867
868Finally, L<Plack> is a Perl module and toolkit that contains PSGI middleware,
869helpers and adapters to web servers, allowing you to easily deploy scripts which
870can continue running, and provides flexibility with regards to which web server
871you use. It can allow existing CGI scripts to enjoy this flexibility and
872performance with minimal changes, or can be used along with modern Perl web
873frameworks to make writing and deploying web services with Perl a breeze.
874
875These solutions can have far-reaching effects on your system and on the way you
876write your CGI programs, so investigate them with care.
877
878See also
879L<http://www.cpan.org/modules/by-category/15_World_Wide_Web_HTML_HTTP_CGI/> .
880
881=head2 How can I hide the source for my Perl program?
882
883Delete it. :-) Seriously, there are a number of (mostly
884unsatisfactory) solutions with varying levels of "security".
885
886First of all, however, you I<can't> take away read permission, because
887the source code has to be readable in order to be compiled and
888interpreted. (That doesn't mean that a CGI script's source is
889readable by people on the web, though--only by people with access to
890the filesystem.)  So you have to leave the permissions at the socially
891friendly 0755 level.
892
893Some people regard this as a security problem. If your program does
894insecure things and relies on people not knowing how to exploit those
895insecurities, it is not secure. It is often possible for someone to
896determine the insecure things and exploit them without viewing the
897source. Security through obscurity, the name for hiding your bugs
898instead of fixing them, is little security indeed.
899
900You can try using encryption via source filters (Starting from Perl
9015.8 the Filter::Simple and Filter::Util::Call modules are included in
902the standard distribution), but any decent programmer will be able to
903decrypt it. You can try using the byte code compiler and interpreter
904described later in L<perlfaq3>, but the curious might still be able to
905de-compile it. You can try using the native-code compiler described
906later, but crackers might be able to disassemble it. These pose
907varying degrees of difficulty to people wanting to get at your code,
908but none can definitively conceal it (true of every language, not just
909Perl).
910
911It is very easy to recover the source of Perl programs. You simply
912feed the program to the perl interpreter and use the modules in
913the B:: hierarchy. The B::Deparse module should be able to
914defeat most attempts to hide source. Again, this is not
915unique to Perl.
916
917If you're concerned about people profiting from your code, then the
918bottom line is that nothing but a restrictive license will give you
919legal security. License your software and pepper it with threatening
920statements like "This is unpublished proprietary software of XYZ Corp.
921Your access to it does not give you permission to use it blah blah
922blah."  We are not lawyers, of course, so you should see a lawyer if
923you want to be sure your license's wording will stand up in court.
924
925=head2 How can I compile my Perl program into byte code or C?
926
927(contributed by brian d foy)
928
929In general, you can't do this. There are some things that may work
930for your situation though. People usually ask this question
931because they want to distribute their works without giving away
932the source code, and most solutions trade disk space for convenience.
933You probably won't see much of a speed increase either, since most
934solutions simply bundle a Perl interpreter in the final product
935(but see L<How can I make my Perl program run faster?>).
936
937The Perl Archive Toolkit ( L<http://par.perl.org/> ) is Perl's
938analog to Java's JAR. It's freely available and on CPAN (
939L<http://search.cpan.org/dist/PAR/> ).
940
941There are also some commercial products that may work for you, although
942you have to buy a license for them.
943
944The Perl Dev Kit ( L<http://www.activestate.com/Products/Perl_Dev_Kit/> )
945from ActiveState can "Turn your Perl programs into ready-to-run
946executables for HP-UX, Linux, Solaris and Windows."
947
948Perl2Exe ( L<http://www.indigostar.com/perl2exe.htm> ) is a command line
949program for converting perl scripts to executable files. It targets both
950Windows and Unix platforms.
951
952=head2 How can I get C<#!perl> to work on [MS-DOS,NT,...]?
953
954For OS/2 just use
955
956    extproc perl -S -your_switches
957
958as the first line in C<*.cmd> file (C<-S> due to a bug in cmd.exe's
959"extproc" handling). For DOS one should first invent a corresponding
960batch file and codify it in C<ALTERNATE_SHEBANG> (see the
961F<dosish.h> file in the source distribution for more information).
962
963The Win95/NT installation, when using the ActiveState port of Perl,
964will modify the Registry to associate the C<.pl> extension with the
965perl interpreter. If you install another port, perhaps even building
966your own Win95/NT Perl from the standard sources by using a Windows port
967of gcc (e.g., with cygwin or mingw32), then you'll have to modify
968the Registry yourself. In addition to associating C<.pl> with the
969interpreter, NT people can use: C<SET PATHEXT=%PATHEXT%;.PL> to let them
970run the program C<install-linux.pl> merely by typing C<install-linux>.
971
972Under "Classic" MacOS, a perl program will have the appropriate Creator and
973Type, so that double-clicking them will invoke the MacPerl application.
974Under Mac OS X, clickable apps can be made from any C<#!> script using Wil
975Sanchez' DropScript utility: L<http://www.wsanchez.net/software/> .
976
977I<IMPORTANT!>: Whatever you do, PLEASE don't get frustrated, and just
978throw the perl interpreter into your cgi-bin directory, in order to
979get your programs working for a web server. This is an EXTREMELY big
980security risk. Take the time to figure out how to do it correctly.
981
982=head2 Can I write useful Perl programs on the command line?
983
984Yes. Read L<perlrun> for more information. Some examples follow.
985(These assume standard Unix shell quoting rules.)
986
987    # sum first and last fields
988    perl -lane 'print $F[0] + $F[-1]' *
989
990    # identify text files
991    perl -le 'for(@ARGV) {print if -f && -T _}' *
992
993    # remove (most) comments from C program
994    perl -0777 -pe 's{/\*.*?\*/}{}gs' foo.c
995
996    # make file a month younger than today, defeating reaper daemons
997    perl -e '$X=24*60*60; utime(time(),time() + 30 * $X,@ARGV)' *
998
999    # find first unused uid
1000    perl -le '$i++ while getpwuid($i); print $i'
1001
1002    # display reasonable manpath
1003    echo $PATH | perl -nl -072 -e '
1004    s![^/+]*$!man!&&-d&&!$s{$_}++&&push@m,$_;END{print"@m"}'
1005
1006OK, the last one was actually an Obfuscated Perl Contest entry. :-)
1007
1008=head2 Why don't Perl one-liners work on my DOS/Mac/VMS system?
1009
1010The problem is usually that the command interpreters on those systems
1011have rather different ideas about quoting than the Unix shells under
1012which the one-liners were created. On some systems, you may have to
1013change single-quotes to double ones, which you must I<NOT> do on Unix
1014or Plan9 systems. You might also have to change a single % to a %%.
1015
1016For example:
1017
1018    # Unix (including Mac OS X)
1019    perl -e 'print "Hello world\n"'
1020
1021    # DOS, etc.
1022    perl -e "print \"Hello world\n\""
1023
1024    # Mac Classic
1025    print "Hello world\n"
1026     (then Run "Myscript" or Shift-Command-R)
1027
1028    # MPW
1029    perl -e 'print "Hello world\n"'
1030
1031    # VMS
1032    perl -e "print ""Hello world\n"""
1033
1034The problem is that none of these examples are reliable: they depend on the
1035command interpreter. Under Unix, the first two often work. Under DOS,
1036it's entirely possible that neither works. If 4DOS was the command shell,
1037you'd probably have better luck like this:
1038
1039  perl -e "print <Ctrl-x>"Hello world\n<Ctrl-x>""
1040
1041Under the Mac, it depends which environment you are using. The MacPerl
1042shell, or MPW, is much like Unix shells in its support for several
1043quoting variants, except that it makes free use of the Mac's non-ASCII
1044characters as control characters.
1045
1046Using qq(), q(), and qx(), instead of "double quotes", 'single
1047quotes', and `backticks`, may make one-liners easier to write.
1048
1049There is no general solution to all of this. It is a mess.
1050
1051[Some of this answer was contributed by Kenneth Albanowski.]
1052
1053=head2 Where can I learn about CGI or Web programming in Perl?
1054
1055For modules, get the CGI or LWP modules from CPAN. For textbooks,
1056see the two especially dedicated to web stuff in the question on
1057books. For problems and questions related to the web, like "Why
1058do I get 500 Errors" or "Why doesn't it run from the browser right
1059when it runs fine on the command line", see the troubleshooting
1060guides and references in L<perlfaq9> or in the CGI MetaFAQ:
1061
1062    L<http://www.perl.org/CGI_MetaFAQ.html>
1063
1064Looking in to L<Plack> and modern Perl web frameworks is highly recommended,
1065though; web programming in Perl has evolved a long way from the old days of
1066simple CGI scripts.
1067
1068=head2 Where can I learn about object-oriented Perl programming?
1069
1070A good place to start is L<perlootut>, and you can use L<perlobj> for
1071reference.
1072
1073A good book on OO on Perl is the "Object-Oriented Perl"
1074by Damian Conway from Manning Publications, or "Intermediate Perl"
1075by Randal Schwartz, brian d foy, and Tom Phoenix from O'Reilly Media.
1076
1077=head2 Where can I learn about linking C with Perl?
1078
1079If you want to call C from Perl, start with L<perlxstut>,
1080moving on to L<perlxs>, L<xsubpp>, and L<perlguts>. If you want to
1081call Perl from C, then read L<perlembed>, L<perlcall>, and
1082L<perlguts>. Don't forget that you can learn a lot from looking at
1083how the authors of existing extension modules wrote their code and
1084solved their problems.
1085
1086You might not need all the power of XS. The Inline::C module lets
1087you put C code directly in your Perl source. It handles all the
1088magic to make it work. You still have to learn at least some of
1089the perl API but you won't have to deal with the complexity of the
1090XS support files.
1091
1092=head2 I've read perlembed, perlguts, etc., but I can't embed perl in my C program; what am I doing wrong?
1093
1094Download the ExtUtils::Embed kit from CPAN and run `make test'. If
1095the tests pass, read the pods again and again and again. If they
1096fail, see L<perlbug> and send a bug report with the output of
1097C<make test TEST_VERBOSE=1> along with C<perl -V>.
1098
1099=head2 When I tried to run my script, I got this message. What does it mean?
1100
1101A complete list of Perl's error messages and warnings with explanatory
1102text can be found in L<perldiag>. You can also use the splain program
1103(distributed with Perl) to explain the error messages:
1104
1105    perl program 2>diag.out
1106    splain [-v] [-p] diag.out
1107
1108or change your program to explain the messages for you:
1109
1110    use diagnostics;
1111
1112or
1113
1114    use diagnostics -verbose;
1115
1116=head2 What's MakeMaker?
1117
1118(contributed by brian d foy)
1119
1120The L<ExtUtils::MakeMaker> module, better known simply as "MakeMaker",
1121turns a Perl script, typically called C<Makefile.PL>, into a Makefile.
1122The Unix tool C<make> uses this file to manage dependencies and actions
1123to process and install a Perl distribution.
1124
1125=head1 AUTHOR AND COPYRIGHT
1126
1127Copyright (c) 1997-2010 Tom Christiansen, Nathan Torkington, and
1128other authors as noted. All rights reserved.
1129
1130This documentation is free; you can redistribute it and/or modify it
1131under the same terms as Perl itself.
1132
1133Irrespective of its distribution, all code examples here are in the public
1134domain. You are permitted and encouraged to use this code and any
1135derivatives thereof in your own programs for fun or for profit as you
1136see fit. A simple comment in the code giving credit to the FAQ would
1137be courteous but is not required.
1138