| 1 | =head1 NAME
|
|---|
| 2 |
|
|---|
| 3 | perlfaq3 - Programming Tools ($Revision: 1.56 $, $Date: 2005/12/31 00:54:37 $)
|
|---|
| 4 |
|
|---|
| 5 | =head1 DESCRIPTION
|
|---|
| 6 |
|
|---|
| 7 | This section of the FAQ answers questions related to programmer tools
|
|---|
| 8 | and programming support.
|
|---|
| 9 |
|
|---|
| 10 | =head2 How do I do (anything)?
|
|---|
| 11 |
|
|---|
| 12 | Have you looked at CPAN (see L<perlfaq2>)? The chances are that
|
|---|
| 13 | someone has already written a module that can solve your problem.
|
|---|
| 14 | Have you read the appropriate manpages? Here's a brief index:
|
|---|
| 15 |
|
|---|
| 16 | Basics perldata, perlvar, perlsyn, perlop, perlsub
|
|---|
| 17 | Execution perlrun, perldebug
|
|---|
| 18 | Functions perlfunc
|
|---|
| 19 | Objects perlref, perlmod, perlobj, perltie
|
|---|
| 20 | Data Structures perlref, perllol, perldsc
|
|---|
| 21 | Modules perlmod, perlmodlib, perlsub
|
|---|
| 22 | Regexes perlre, perlfunc, perlop, perllocale
|
|---|
| 23 | Moving to perl5 perltrap, perl
|
|---|
| 24 | Linking w/C perlxstut, perlxs, perlcall, perlguts, perlembed
|
|---|
| 25 | Various http://www.cpan.org/misc/olddoc/FMTEYEWTK.tgz
|
|---|
| 26 | (not a man-page but still useful, a collection
|
|---|
| 27 | of various essays on Perl techniques)
|
|---|
| 28 |
|
|---|
| 29 | A crude table of contents for the Perl manpage set is found in L<perltoc>.
|
|---|
| 30 |
|
|---|
| 31 | =head2 How can I use Perl interactively?
|
|---|
| 32 |
|
|---|
| 33 | The typical approach uses the Perl debugger, described in the
|
|---|
| 34 | perldebug(1) manpage, on an "empty" program, like this:
|
|---|
| 35 |
|
|---|
| 36 | perl -de 42
|
|---|
| 37 |
|
|---|
| 38 | Now just type in any legal Perl code, and it will be immediately
|
|---|
| 39 | evaluated. You can also examine the symbol table, get stack
|
|---|
| 40 | backtraces, check variable values, set breakpoints, and other
|
|---|
| 41 | operations typically found in symbolic debuggers.
|
|---|
| 42 |
|
|---|
| 43 | =head2 Is there a Perl shell?
|
|---|
| 44 |
|
|---|
| 45 | The psh (Perl sh) is currently at version 1.8. The Perl Shell is a shell
|
|---|
| 46 | that combines the interactive nature of a Unix shell with the power of
|
|---|
| 47 | Perl. The goal is a full featured shell that behaves as expected for
|
|---|
| 48 | normal shell activity and uses Perl syntax and functionality for
|
|---|
| 49 | control-flow statements and other things. You can get psh at
|
|---|
| 50 | http://sourceforge.net/projects/psh/ .
|
|---|
| 51 |
|
|---|
| 52 | Zoidberg is a similar project and provides a shell written in perl,
|
|---|
| 53 | configured in perl and operated in perl. It is intended as a login shell
|
|---|
| 54 | and development environment. It can be found at http://zoidberg.sf.net/
|
|---|
| 55 | or your local CPAN mirror.
|
|---|
| 56 |
|
|---|
| 57 | The Shell.pm module (distributed with Perl) makes Perl try commands
|
|---|
| 58 | which aren't part of the Perl language as shell commands. perlsh from
|
|---|
| 59 | the source distribution is simplistic and uninteresting, but may still
|
|---|
| 60 | be what you want.
|
|---|
| 61 |
|
|---|
| 62 | =head2 How do I find which modules are installed on my system?
|
|---|
| 63 |
|
|---|
| 64 | You can use the ExtUtils::Installed module to show all installed
|
|---|
| 65 | distributions, although it can take awhile to do its magic. The
|
|---|
| 66 | standard library which comes with Perl just shows up as "Perl" (although
|
|---|
| 67 | you can get those with Module::CoreList).
|
|---|
| 68 |
|
|---|
| 69 | use ExtUtils::Installed;
|
|---|
| 70 |
|
|---|
| 71 | my $inst = ExtUtils::Installed->new();
|
|---|
| 72 | my @modules = $inst->modules();
|
|---|
| 73 |
|
|---|
| 74 | If you want a list of all of the Perl module filenames, you
|
|---|
| 75 | can use File::Find::Rule.
|
|---|
| 76 |
|
|---|
| 77 | use File::Find::Rule;
|
|---|
| 78 |
|
|---|
| 79 | my @files = File::Find::Rule->file()->name( '*.pm' )->in( @INC );
|
|---|
| 80 |
|
|---|
| 81 | If you do not have that module, you can do the same thing
|
|---|
| 82 | with File::Find which is part of the standard library.
|
|---|
| 83 |
|
|---|
| 84 | use File::Find;
|
|---|
| 85 | my @files;
|
|---|
| 86 |
|
|---|
| 87 | find(
|
|---|
| 88 | sub {
|
|---|
| 89 | push @files, $File::Find::name
|
|---|
| 90 | if -f $File::Find::name && /\.pm$/
|
|---|
| 91 | },
|
|---|
| 92 |
|
|---|
| 93 | @INC
|
|---|
| 94 | );
|
|---|
| 95 |
|
|---|
| 96 | print join "\n", @files;
|
|---|
| 97 |
|
|---|
| 98 | If you simply need to quickly check to see if a module is
|
|---|
| 99 | available, you can check for its documentation. If you can
|
|---|
| 100 | read the documentation the module is most likely installed.
|
|---|
| 101 | If you cannot read the documentation, the module might not
|
|---|
| 102 | have any (in rare cases).
|
|---|
| 103 |
|
|---|
| 104 | prompt% perldoc Module::Name
|
|---|
| 105 |
|
|---|
| 106 | You can also try to include the module in a one-liner to see if
|
|---|
| 107 | perl finds it.
|
|---|
| 108 |
|
|---|
| 109 | perl -MModule::Name -e1
|
|---|
| 110 |
|
|---|
| 111 | =head2 How do I debug my Perl programs?
|
|---|
| 112 |
|
|---|
| 113 | Have you tried C<use warnings> or used C<-w>? They enable warnings
|
|---|
| 114 | to detect dubious practices.
|
|---|
| 115 |
|
|---|
| 116 | Have you tried C<use strict>? It prevents you from using symbolic
|
|---|
| 117 | references, makes you predeclare any subroutines that you call as bare
|
|---|
| 118 | words, and (probably most importantly) forces you to predeclare your
|
|---|
| 119 | variables with C<my>, C<our>, or C<use vars>.
|
|---|
| 120 |
|
|---|
| 121 | Did you check the return values of each and every system call? The operating
|
|---|
| 122 | system (and thus Perl) tells you whether they worked, and if not
|
|---|
| 123 | why.
|
|---|
| 124 |
|
|---|
| 125 | open(FH, "> /etc/cantwrite")
|
|---|
| 126 | or die "Couldn't write to /etc/cantwrite: $!\n";
|
|---|
|
|---|