1perlfaq3(3)           User Contributed Perl Documentation          perlfaq3(3)
2
3
4

NAME

6       perlfaq3 - Programming Tools
7

VERSION

9       version 5.20210520
10

DESCRIPTION

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