=head1 NAME
-perlfaq3 - Programming Tools ($Revision: 10127 $)
+perlfaq3 - Programming Tools
=head1 DESCRIPTION
Zoidberg is a similar project and provides a shell written in perl,
configured in perl and operated in perl. It is intended as a login shell
-and development environment. It can be found at http://zoidberg.sf.net/
+and development environment. It can be found at
+http://pardus-larus.student.utwente.nl/~pardus/projects/zoidberg/
or your local CPAN mirror.
The Shell.pm module (distributed with Perl) makes Perl try commands
=head2 How do I find which modules are installed on my system?
-You can use the ExtUtils::Installed module to show all installed
-distributions, although it can take awhile to do its magic. The
-standard library which comes with Perl just shows up as "Perl" (although
-you can get those with Module::CoreList).
+From the command line, you can use the C<cpan> command's C<-l> switch:
+
+ $ cpan -l
+
+You can also use C<cpan>'s C<-a> switch to create an autobundle file
+that C<CPAN.pm> understands and can use to re-install every module:
+
+ $ cpan -a
+
+Inside a Perl program, you can use the ExtUtils::Installed module to
+show all installed distributions, although it can take awhile to do
+its magic. The standard library which comes with Perl just shows up
+as "Perl" (although you can get those with C<Module::CoreList>).
use ExtUtils::Installed;
my @modules = $inst->modules();
If you want a list of all of the Perl module filenames, you
-can use File::Find::Rule.
+can use C<File::Find::Rule>:
use File::Find::Rule;
- my @files = File::Find::Rule->file()->name( '*.pm' )->in( @INC );
+ my @files = File::Find::Rule->
+ extras({follow => 1})->
+ file()->
+ name( '*.pm' )->
+ in( @INC )
+ ;
If you do not have that module, you can do the same thing
-with File::Find which is part of the standard library.
-
- use File::Find;
- my @files;
-
- find(
- sub {
- push @files, $File::Find::name
- if -f $File::Find::name && /\.pm$/
- },
-
- @INC
- );
+with File::Find which is part of the standard library:
+
+ use File::Find;
+ my @files;
+
+ find(
+ {
+ wanted => sub {
+ push @files, $File::Find::fullname
+ if -f $File::Find::fullname && /\.pm$/
+ },
+ follow => 1,
+ follow_skip => 2,
+ },
+ @INC
+ );
print join "\n", @files;
available, you can check for its documentation. If you can
read the documentation the module is most likely installed.
If you cannot read the documentation, the module might not
-have any (in rare cases).
+have any (in rare cases):
- prompt% perldoc Module::Name
+ $ perldoc Module::Name
You can also try to include the module in a one-liner to see if
-perl finds it.
+perl finds it:
- perl -MModule::Name -e1
+ $ perl -MModule::Name -e1
=head2 How do I debug my Perl programs?
=head2 How do I profile my Perl programs?
-You should get the Devel::DProf module from the standard distribution
-(or separately on CPAN) and also use Benchmark.pm from the standard
-distribution. The Benchmark module lets you time specific portions of
-your code, while Devel::DProf gives detailed breakdowns of where your
-code spends its time.
+(contributed by brian d foy, updated Fri Jul 25 12:22:26 PDT 2008)
+
+The C<Devel> namespace has several modules which you can use to
+profile your Perl programs. The C<Devel::DProf> module comes with Perl
+and you can invoke it with the C<-d> switch:
+
+ perl -d:DProf program.pl
+
+After running your program under C<DProf>, you'll get a F<tmon.out> file
+with the profile data. To look at the data, you can turn it into a
+human-readable report with the C<dprofpp> program that comes with
+C<Devel::DProf>.
+
+ dprofpp
+
+You can also do the profiling and reporting in one step with the C<-p>
+switch to <dprofpp>:
+
+ dprofpp -p program.pl
+
+The C<Devel::NYTProf> (New York Times Profiler) does both statement
+and subroutine profiling. It's available from CPAN and you also invoke
+it with the C<-d> switch:
-Here's a sample use of Benchmark:
+ perl -d:NYTProf some_perl.pl
- use Benchmark;
+Like C<DProf>, it creates a database of the profile information that you
+can turn into reports. The C<nytprofhtml> command turns the data into
+an HTML report similar to the C<Devel::Cover> report:
- @junk = `cat /etc/motd`;
- $count = 10_000;
+ nytprofhtml
- timethese($count, {
- 'map' => sub { my @a = @junk;
- map { s/a/b/ } @a;
- return @a },
- 'for' => sub { my @a = @junk;
- for (@a) { s/a/b/ };
- return @a },
- });
+CPAN has several other profilers that you can invoke in the same
+fashion. You might also be interested in using the C<Benchmark> to
+measure and compare code snippets.
-This is what it prints (on one machine--your results will be dependent
-on your hardware, operating system, and the load on your machine):
+You can read more about profiling in I<Programming Perl>, chapter 20,
+or I<Mastering Perl>, chapter 5.
- Benchmark: timing 10000 iterations of for, map...
- for: 4 secs ( 3.97 usr 0.01 sys = 3.98 cpu)
- map: 6 secs ( 4.97 usr 0.00 sys = 4.97 cpu)
+L<perldebguts> documents creating a custom debugger if you need to
+create a special sort of profiler. brian d foy describes the process
+in I<The Perl Journal>, "Creating a Perl Debugger",
+http://www.ddj.com/184404522 , and "Profiling in Perl"
+http://www.ddj.com/184404580 .
-Be aware that a good benchmark is very hard to write. It only tests the
-data you give it and proves little about the differing complexities
-of contrasting algorithms.
+Perl.com has two interesting articles on profiling: "Profiling Perl",
+by Simon Cozens, http://www.perl.com/lpt/a/850 and "Debugging and
+Profiling mod_perl Applications", by Frank Wiles,
+http://www.perl.com/pub/a/2006/02/09/debug_mod_perl.html .
+
+Randal L. Schwartz writes about profiling in "Speeding up Your Perl
+Programs" for I<Unix Review>,
+http://www.stonehenge.com/merlyn/UnixReview/col49.html , and "Profiling
+in Template Toolkit via Overriding" for I<Linux Magazine>,
+http://www.stonehenge.com/merlyn/LinuxMag/col75.html .
=head2 How do I cross-reference my Perl programs?
and Solaris), multi-language IDE has Perl support, including a regular expression
debugger and remote debugging.
+=item Notepad++
+
+http://notepad-plus.sourceforge.net/
+
=item Open Perl IDE
http://open-perl-ide.sourceforge.net/
OptiPerl is a Windows IDE with simulated CGI environment, including
debugger and syntax highlighting editor.
+=item Padre
+
+http://padre.perlide.org/
+
+Padre is cross-platform IDE for Perl written in Perl using wxWidgets to provide
+a native look and feel. It's open source under the Artistic License.
+
=item PerlBuilder
http://www.solutionsoft.com/perl.htm
-PerlBuidler is an integrated development environment for Windows that
+PerlBuilder is an integrated development environment for Windows that
supports Perl development.
=item visiPerl+
perhaps the best available Perl editing mode in any editor.
If you are using Windows, you can use any editor that lets you work
-with plain text, such as NotePad or WordPad. Word processors, such as
+with plain text, such as NotePad or WordPad. Word processors, such as
Microsoft Word or WordPerfect, typically do not work since they insert
all sorts of behind-the-scenes information, although some allow you to
save files as "Text Only". You can also download text editors designed
http://www.textpad.com/ ) and UltraEdit ( http://www.ultraedit.com/ ),
among others.
-If you are using MacOS, the same concerns apply. MacPerl (for Classic
+If you are using MacOS, the same concerns apply. MacPerl (for Classic
environments) comes with a simple editor. Popular external editors are
BBEdit ( http://www.bbedit.com/ ) or Alpha (
http://www.his.com/~jguyer/Alpha/Alpha8.html ). MacOS X users can use
to use Perl as the scripting language. nvi is not alone in this,
though: at least also vim and vile offer an embedded Perl.
-The following are Win32 multilanguage editor/IDESs that support Perl:
+The following are Win32 multilanguage editor/IDEs that support Perl:
=over 4
http://www.slickedit.com/
+=item ConTEXT
+
+http://www.contexteditor.org/
+
=back
There is also a toyedit Text widget based editor written in Perl
=item Ksh
-from the MKS Toolkit ( http://www.mks.com/ ), or the Bourne shell of
+from the MKS Toolkit ( http://www.mkssoftware.com/ ), or the Bourne shell of
the U/WIN environment ( http://www.research.att.com/sw/tools/uwin/ )
=item Tcsh
=back
MKS and U/WIN are commercial (U/WIN is free for educational and
-research purposes), Cygwin is covered by the GNU Public License (but
-that shouldn't matter for Perl use). The Cygwin, MKS, and U/WIN all
-contain (in addition to the shells) a comprehensive set of standard
-UNIX toolkit utilities.
+research purposes), Cygwin is covered by the GNU General Public
+License (but that shouldn't matter for Perl use). The Cygwin, MKS,
+and U/WIN all contain (in addition to the shells) a comprehensive set
+of standard UNIX toolkit utilities.
If you're transferring text files between Unix and Windows using FTP
be sure to transfer them in ASCII mode so the ends of lines are
are text editors for Mac OS that have a Perl sensitivity mode
( http://web.barebones.com/ ).
-
=back
-Pepper and Pe are programming language sensitive text editors for Mac
-OS X and BeOS respectively ( http://www.hekkelman.com/ ).
-
=head2 Where can I get Perl macros for vi?
For a complete version of Tom Christiansen's vi configuration file,
=item Wx
-This is a Perl binding for the cross-platform wxWidgets toolkit
-L<http://www.wxwidgets.org>. It works under Unix, Win32 and Mac OS X,
+This is a Perl binding for the cross-platform wxWidgets toolkit
+( http://www.wxwidgets.org ). It works under Unix, Win32 and Mac OS X,
using native widgets (Gtk under Unix). The interface follows the C++
interface closely, but the documentation is a little sparse for someone
who doesn't know the library, mostly just referring you to the C++
=item Gtk and Gtk2
-These are Perl bindings for the Gtk toolkit L<http://www.gtk.org>. The
+These are Perl bindings for the Gtk toolkit ( http://www.gtk.org ). The
interface changed significantly between versions 1 and 2 so they have
separate Perl modules. It runs under Unix, Win32 and Mac OS X (currently
it requires an X server on Mac OS, but a 'native' port is underway), and
=item CamelBones
-CamelBones L<http://camelbones.sourceforge.net> is a Perl interface to
+CamelBones ( http://camelbones.sourceforge.net ) is a Perl interface to
Mac OS X's Cocoa GUI toolkit, and as such can be used to produce native
GUIs on Mac OS X. It's not on CPAN, as it requires frameworks that
CPAN.pm doesn't know how to install, but installation is via the
Ditto for stringifying large arrays:
- {
- local $, = "\n";
- print @big_array;
- }
+ {
+ local $, = "\n";
+ print @big_array;
+ }
is much more memory-efficient than either
- print join "\n", @big_array;
+ print join "\n", @big_array;
or
- {
- local $" = "\n";
- print "@big_array";
- }
+ {
+ local $" = "\n";
+ print "@big_array";
+ }
=item * Pass by reference
cannot be reclaimed or reused even if they go out of scope. It is
reserved in case the variables come back into scope. Memory allocated
to global variables can be reused (within your program) by using
-undef()ing and/or delete().
+undef() and/or delete().
On most operating systems, memory allocated to a program can never be
returned to the system. That's why long-running programs sometimes re-
=head1 REVISION
-Revision: $Revision: 10127 $
+Revision: $Revision$
-Date: $Date: 2007-10-27 21:40:20 +0200 (Sat, 27 Oct 2007) $
+Date: $Date$
See L<perlfaq> for source control details and availability.
=head1 AUTHOR AND COPYRIGHT
-Copyright (c) 1997-2007 Tom Christiansen, Nathan Torkington, and
+Copyright (c) 1997-2009 Tom Christiansen, Nathan Torkington, and
other authors as noted. All rights reserved.
This documentation is free; you can redistribute it and/or modify it