[svn:perlfaq] r11915 - perlfaq/trunk
[email protected] Wed, 1 Oct 2008 16:17:11 -0700 (PDT)
| Newsgroups | perl.cvs.perlfaq |
|---|---|
| Message-ID | <[email protected]> |
Author: comdog
Date: Wed Oct 1 16:17:11 2008
New Revision: 11915
Modified:
perlfaq/trunk/perlfaq1.pod
perlfaq/trunk/perlfaq2.pod
perlfaq/trunk/perlfaq3.pod
perlfaq/trunk/perlfaq4.pod
perlfaq/trunk/perlfaq5.pod
perlfaq/trunk/perlfaq6.pod
perlfaq/trunk/perlfaq7.pod
perlfaq/trunk/perlfaq8.pod
perlfaq/trunk/perlfaq9.pod
Log:
* Whitespace fix
Modified: perlfaq/trunk/perlfaq1.pod
==============================================================================
--- perlfaq/trunk/perlfaq1.pod (original)
+++ perlfaq/trunk/perlfaq1.pod Wed Oct 1 16:17:11 2008
@@ -107,8 +107,8 @@
=item *
-There is no Perl 6 release scheduled, but it will be available when
-it's ready. Stay tuned, but don't worry that you'll have to change
+There is no Perl 6 release scheduled, but it will be available when
+it's ready. Stay tuned, but don't worry that you'll have to change
major versions of Perl; no one is going to take Perl 5 away from you.
=item *
@@ -285,7 +285,7 @@
One bit. Oh, you weren't talking ASCII? :-) Larry now uses "Perl" to
signify the language proper and "perl" the implementation of it, i.e.
the current interpreter. Hence Tom's quip that "Nothing but perl can
-parse Perl."
+parse Perl."
Before the first edition of I<Programming perl>, people commonly
referred to the language as "perl", and its name appeared that way in
Modified: perlfaq/trunk/perlfaq2.pod
==============================================================================
--- perlfaq/trunk/perlfaq2.pod (original)
+++ perlfaq/trunk/perlfaq2.pod Wed Oct 1 16:17:11 2008
@@ -36,7 +36,7 @@
ActiveState: Windows, Linux, Mac OS X, Solaris, AIX and HP-UX
http://www.activestate.com/
-
+
Sunfreeware.com: Solaris 2.5 to Solaris 10 (SPARC and x86)
http://www.sunfreeware.com/
@@ -54,15 +54,15 @@
first. Consult the Usenet FAQs for your operating system for
information on where to get such a binary version.
-You might look around the net for a pre-built binary of Perl (or a
+You might look around the net for a pre-built binary of Perl (or a
C compiler!) that meets your needs, though:
For Windows, Vanilla Perl ( http://vanillaperl.com/ ) and Strawberry Perl
-( http://strawberryperl.com/ ) come with a
+( http://strawberryperl.com/ ) come with a
bundled C compiler. ActivePerl is a pre-compiled version of Perl
ready-to-use.
-For Sun systems, SunFreeware.com provides binaries of most popular
+For Sun systems, SunFreeware.com provides binaries of most popular
applications, including compilers and Perl.
=head2 I copied the perl binary from one machine to another, but scripts don't work.
@@ -395,7 +395,7 @@
by Richard Foley with Andy Lester
ISBN 1-59059-454-1 [1st edition July 2005]
http://www.apress.com/book/view/1590594541
-
+
=back
=head2 Which magazines have Perl content?
Modified: perlfaq/trunk/perlfaq3.pod
==============================================================================
--- perlfaq/trunk/perlfaq3.pod (original)
+++ perlfaq/trunk/perlfaq3.pod Wed Oct 1 16:17:11 2008
@@ -51,7 +51,7 @@
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
+and development environment. It can be found at
http://pardus-larus.student.utwente.nl/~pardus/projects/zoidberg/
or your local CPAN mirror.
@@ -166,7 +166,7 @@
C<Devel::DProf>.
dprofpp
-
+
You can also do the profiling and reporting in one step with the C<-p>
switch to <dprofpp>:
@@ -547,7 +547,7 @@
=item Wx
-This is a Perl binding for the cross-platform wxWidgets toolkit
+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
Modified: perlfaq/trunk/perlfaq4.pod
==============================================================================
--- perlfaq/trunk/perlfaq4.pod (original)
+++ perlfaq/trunk/perlfaq4.pod Wed Oct 1 16:17:11 2008
@@ -51,14 +51,14 @@
(contributed by brian d foy)
You're probably trying to convert a string to a number, which Perl only
-converts as a decimal number. When Perl converts a string to a number, it
+converts as a decimal number. When Perl converts a string to a number, it
ignores leading spaces and zeroes, then assumes the rest of the digits
are in base 10:
my $string = '0644';
-
+
print $string + 0; # prints 644
-
+
print $string + 44; # prints 688, certainly not octal!
This problem usually involves one of the Perl built-ins that has the
@@ -67,9 +67,9 @@
its first argument is octal because that's what it does:
%prompt> chmod 644 file
-
+
If you want to use the same literal digits (644) in Perl, you have to tell
-Perl to treat them as octal numbers either by prefixing the digits with
+Perl to treat them as octal numbers either by prefixing the digits with
a C<0> or using C<oct>:
chmod( 0644, $file); # right, has leading zero
@@ -82,7 +82,7 @@
chmod( oct($ARGV[0]), $file ); # correct, treat string as octal
-You can always check the value you're using by printing it in octal
+You can always check the value you're using by printing it in octal
notation to ensure it matches what you think it should be. Print it
in octal and decimal format:
@@ -814,7 +814,7 @@
(contributed by brian d foy)
-Damian Conway's L<Text::Autoformat> handles all of the thinking
+Damian Conway's L<Text::Autoformat> handles all of the thinking
for you.
use Text::Autoformat;
@@ -825,13 +825,13 @@
for my $style (qw( sentence title highlight )) {
print autoformat($x, { case => $style }), "\n";
}
-
-How do you want to capitalize those words?
+
+How do you want to capitalize those words?
FRED AND BARNEY'S LODGE # all uppercase
Fred And Barney's Lodge # title case
Fred and Barney's Lodge # highlight case
-
+
It's not as easy a problem as it looks. How many words do you think
are in there? Wait for it... wait for it.... If you answered 5
you're right. Perl words are groups of C<\w+>, but that's not what
@@ -847,9 +847,9 @@
$string =~ s/([\w']+)/\u\L$1/g;
-Now, what if you don't want to capitalize that "and"? Just use
+Now, what if you don't want to capitalize that "and"? Just use
L<Text::Autoformat> and get on with the next problem. :)
-
+
=head2 How can I split a [character] delimited string except when inside [character]?
Several modules can handle this sort of parsing--C<Text::Balanced>,
@@ -1052,7 +1052,7 @@
The C</e> will also silently ignore violations of strict, replacing
undefined variable names with the empty string. Since I'm using the
-C</e> flag (twice even!), I have all of the same security problems I
+C</e> flag (twice even!), I have all of the same security problems I
have with C<eval> in its string form. If there's something odd in
C<$foo>, perhaps something like C<@{[ system "rm -rf /" ]}>, then
I could get myself in trouble.
@@ -1064,7 +1064,7 @@
signal that I missed something:
my $string = 'This has $foo and $bar';
-
+
my %Replacements = (
foo => 'Fred',
);
@@ -1306,14 +1306,14 @@
contained in an array or a hash:
use 5.010;
-
- if( $item ~~ @array )
- {
+
+ if( $item ~~ @array )
+ {
say "The array contains $item"
}
-
+
if( $item ~~ %hash )
- {
+ {
say "The hash contains $item"
}
@@ -1399,17 +1399,17 @@
with the least amount of work:
use 5.010;
-
+
if( @array1 ~~ @array2 )
{
say "The arrays are the same";
}
-
+
if( %hash1 ~~ %hash2 ) # doesn't check values!
{
say "The hash keys are the same";
}
-
+
The following code works for single-level arrays. It uses a
stringwise comparison, and does not distinguish defined versus
undefined empty strings. Modify if you have other needs.
@@ -1550,7 +1550,7 @@
my @array = qw( a b c );
my $i = 0;
-
+
while( 1 ) {
print $array[ $i++ % @array ], "\n";
last if $i > 20;
@@ -1569,13 +1569,13 @@
The C<Array::Iterator::Circular> creates an iterator object for
circular arrays:
-
+
use Array::Iterator::Circular;
-
+
my $color_iterator = Array::Iterator::Circular->new(
qw(red green blue orange)
);
-
+
foreach ( 1 .. 20 ) {
print $color_iterator->next, "\n";
}
@@ -1732,7 +1732,7 @@
you can enumerate all the permutations of C<0..9> like this:
use Algorithm::Loops qw(NextPermuteNum);
-
+
my @list= 0..9;
do { print "@list\n" } while NextPermuteNum @list;
@@ -1796,7 +1796,7 @@
@ints = (...); # array of bits, e.g. ( 1, 0, 0, 1, 1, 0 ... )
$vec = '';
- foreach( 0 .. $#ints ) {
+ foreach( 0 .. $#ints ) {
vec($vec,$_,1) = 1 if $ints[$_];
}
@@ -1979,7 +1979,7 @@
duplicates:
my %new_hash = %hash1; # make a copy; leave %hash1 alone
-
+
foreach my $key2 ( keys %hash2 )
{
if( exists $new_hash{$key2} )
@@ -1992,11 +1992,11 @@
else
{
$new_hash{$key2} = $hash2{$key2};
- }
+ }
}
If you don't want to create a new hash, you can still use this looping
-technique; just change the C<%new_hash> to C<%hash1>.
+technique; just change the C<%new_hash> to C<%hash1>.
foreach my $key2 ( keys %hash2 )
{
@@ -2010,11 +2010,11 @@
else
{
$hash1{$key2} = $hash2{$key2};
- }
+ }
}
If you don't care that one hash overwrites keys and values from the other, you
-could just use a hash slice to add one hash to another. In this case, values
+could just use a hash slice to add one hash to another. In this case, values
from C<%hash2> replace values from C<%hash1> when they have keys in common:
@hash1{ keys %hash2 } = values %hash2;
@@ -2277,7 +2277,7 @@
Are you using a really old version of Perl?
Normally, accessing a hash key's value for a nonexistent key will
-I<not> create the key.
+I<not> create the key.
my %hash = ();
my $value = $hash{ 'foo' };
@@ -2289,18 +2289,18 @@
my_sub( $hash{ 'foo' } );
print "This will print before 5.004\n" if exists $hash{ 'foo' };
-
+
sub my_sub {
# $_[0] = 'bar'; # create hash key in case you do this
1;
}
-
+
Since Perl 5.004, however, this situation is a special case and Perl
creates the hash key only when you make the assignment:
my_sub( $hash{ 'foo' } );
print "This will print, even after 5.004\n" if exists $hash{ 'foo' };
-
+
sub my_sub {
$_[0] = 'bar';
}
@@ -2310,7 +2310,7 @@
Perl 5.004 didn't make this a special case:
my_sub( @hash{ qw/foo/ } );
-
+
=head2 How can I make the Perl equivalent of a C structure/C++ class/hash or array of hashes or arrays?
Usually a hash ref, perhaps like this:
Modified: perlfaq/trunk/perlfaq5.pod
==============================================================================
--- perlfaq/trunk/perlfaq5.pod (original)
+++ perlfaq/trunk/perlfaq5.pod Wed Oct 1 16:17:11 2008
@@ -26,7 +26,7 @@
while( <> ) {
print ".";
print "\n" unless ++$count % 50;
-
+
#... expensive line processing operations
}
@@ -40,7 +40,7 @@
while( <> ) {
print ".";
print "\n" unless ++$count % 50;
-
+
#... expensive line processing operations
}
@@ -51,10 +51,10 @@
{
my $previous_default = select(STDOUT); # save previous default
- $|++; # autoflush STDOUT
+ $|++; # autoflush STDOUT
select(STDERR);
$|++; # autoflush STDERR, to be sure
- select($previous_default); # restore previous default
+ select($previous_default); # restore previous default
}
# now should alternate . and +
@@ -65,23 +65,23 @@
print STDERR "+";
print STDOUT "\n" unless ++$count % 25;
}
-
+
Besides the C<$|> special variable, you can use C<binmode> to give
your filehandle a C<:unix> layer, which is unbuffered:
binmode( STDOUT, ":unix" );
-
+
while( 1 ) {
sleep 1;
print ".";
print "\n" unless ++$count % 50;
}
-
+
For more information on output layers, see the entries for C<binmode>
and C<open> in L<perlfunc>, and the C<PerlIO> module documentation.
If you are using C<IO::Handle> or one of its subclasses, you can
-call the C<autoflush> method to change the settings of the
+call the C<autoflush> method to change the settings of the
filehandle:
use IO::Handle;
@@ -92,7 +92,7 @@
the buffer any time you want without auto-buffering
$io_fh->flush;
-
+
=head2 How do I change, delete, or insert a line in a file, or append to the beginning of a file?
X<file, editing>
@@ -173,7 +173,7 @@
{
print $out $_;
}
-
+
To skip lines, use the looping controls. The C<next> in this example
skips comment lines, and the C<last> stops all processing once it
encounters either C<__END__> or C<__DATA__>.
@@ -373,7 +373,7 @@
return ();
}
}
-
+
}
=head2 How can I manipulate fixed-record-length files?
@@ -561,8 +561,8 @@
(contributed by Peter J. Holzer, [email protected])
-Since Perl 5.8.0 a file handle referring to a string can be created by
-calling open with a reference to that string instead of the filename.
+Since Perl 5.8.0 a file handle referring to a string can be created by
+calling open with a reference to that string instead of the filename.
This file handle can then be used to read from or write to the string:
open(my $fh, '>', \$string) or die "Could not open string for writing";
@@ -574,7 +574,7 @@
With older versions of Perl, the C<IO::String> module provides similar
functionality.
-
+
=head2 How can I output my numbers with commas added?
X<number, commify>
@@ -1236,9 +1236,9 @@
C<close()> function from the C<POSIX> module:
use POSIX ();
-
+
POSIX::close( $fd );
-
+
This should rarely be necessary, as the Perl C<close()> function is to be
used for things that Perl opened itself, even if it was a dup of a
numeric descriptor as with C<MHCONTEXT> above. But if you really have
@@ -1323,7 +1323,7 @@
(contributed by brian d foy)
-If you are seeing spaces between the elements of your array when
+If you are seeing spaces between the elements of your array when
you print the array, you are probably interpolating the array in
double quotes:
Modified: perlfaq/trunk/perlfaq6.pod
==============================================================================
--- perlfaq/trunk/perlfaq6.pod (original)
+++ perlfaq/trunk/perlfaq6.pod Wed Oct 1 16:17:11 2008
@@ -166,7 +166,7 @@
containing multiple levels of balanced text, but sometimes it isn't
balanced text, as in an empty tag (C<< <br/> >>, for instance). Even then,
things can occur out-of-order. Just when you think you've got a
-pattern that matches your input, someone throws you a curveball.
+pattern that matches your input, someone throws you a curveball.
If you'd like to do it the hard way, scratching and clawing your way
toward a right answer but constantly being disappointed, beseiged by
@@ -194,7 +194,7 @@
X<$/, regexes in> X<$INPUT_RECORD_SEPARATOR, regexes in>
X<$RS, regexes in>
-$/ has to be a string. You can use these examples if you really need to
+$/ has to be a string. You can use these examples if you really need to
do this.
If you have File::Stream, this is easy.
@@ -397,7 +397,7 @@
prints the lines of input that match it:
my $pattern = shift @ARGV;
-
+
while( <> ) {
print if m/$pattern/;
}
@@ -408,7 +408,7 @@
time, then reuse that for subsequent iterations:
my $pattern = shift @ARGV;
-
+
while( <> ) {
print if m/$pattern/o; # useful for Perl < 5.6
}
@@ -428,7 +428,7 @@
later, you should only see C<re> report that for the first iteration.
use re 'debug';
-
+
$regex = 'Perl';
foreach ( qw(Perl Java Ruby Python) ) {
print STDERR "-" x 73, "\n";
@@ -539,30 +539,30 @@
the first capture buffer finds (and remembers) the balanced text, and
you need that same pattern within the first buffer to get past the
nested text. That's the recursive part. The C<(?1)> uses the pattern
-in the outer capture buffer as an independent part of the regex.
+in the outer capture buffer as an independent part of the regex.
Putting it all together, you have:
#!/usr/local/bin/perl5.10.0
-
+
my $string =<<"HERE";
I have some <brackets in <nested brackets> > and
<another group <nested once <nested twice> > >
and that's it.
HERE
-
+
my @groups = $string =~ m/
( # start of capture buffer 1
< # match an opening angle bracket
- (?:
+ (?:
[^<>]++ # one or more non angle brackets, non backtracking
- |
+ |
(?1) # found < or >, so recurse to capture buffer 1
- )*
+ )*
> # match a closing angle bracket
) # end of capture buffer 1
/xg;
-
+
$" = "\n\t";
print "Found:\n\t@groups\n";
@@ -579,34 +579,34 @@
to process. Keep doing that until you get no matches:
#!/usr/local/bin/perl5.10.0
-
+
my @queue =<<"HERE";
I have some <brackets in <nested brackets> > and
<another group <nested once <nested twice> > >
and that's it.
HERE
-
+
my $regex = qr/
( # start of bracket 1
< # match an opening angle bracket
- (?:
+ (?:
[^<>]++ # one or more non angle brackets, non backtracking
- |
+ |
(?1) # recurse to bracket 1
- )*
+ )*
> # match a closing angle bracket
) # end of bracket 1
/x;
-
+
$" = "\n\t";
-
+
while( @queue )
{
my $string = shift @queue;
-
+
my @groups = $string =~ m/$regex/g;
print "Found:\n\t@groups\n\n" if @groups;
-
+
unshift @queue, map { s/^<//; s/>$//; $_ } @groups;
}
@@ -616,13 +616,13 @@
Found:
<brackets in <nested brackets> >
<another group <nested once <nested twice> > >
-
+
Found:
<nested brackets>
-
+
Found:
<nested once <nested twice> >
-
+
Found:
<nested twice>
Modified: perlfaq/trunk/perlfaq7.pod
==============================================================================
--- perlfaq/trunk/perlfaq7.pod (original)
+++ perlfaq/trunk/perlfaq7.pod Wed Oct 1 16:17:11 2008
@@ -63,7 +63,7 @@
if ($whoops) { exit 1 }
@nums = (1, 2, 3);
-
+
if ($whoops) {
exit 1;
}
@@ -172,7 +172,7 @@
$person = {}; # new anonymous hash
$person->{AGE} = 24; # set field AGE to 24
$person->{NAME} = "Nat"; # set field NAME to "Nat"
-
+
If you're looking for something a bit more rigorous, try L<perltoot>.
=head2 How do I create a module?
@@ -223,7 +223,7 @@
current maintainer. The PAUSE admins will also try to reach the
maintainer.
-=item
+=item
Post a public message in a heavily trafficked site announcing your
intention to take over the module.
@@ -231,7 +231,7 @@
=item
Wait a bit. The PAUSE admins don't want to act too quickly in case
-the current maintainer is on holiday. If there's no response to
+the current maintainer is on holiday. If there's no response to
private communication or the public post, a PAUSE admin can transfer
it to you.
@@ -266,7 +266,7 @@
hard-to-explain meaning. Usually, closures are implemented in Perl as
anonymous subroutines with lasting references to lexical variables
outside their own scopes. These lexicals magically refer to the
-variables that were around when the subroutine was defined (deep
+variables that were around when the subroutine was defined (deep
binding).
Closures are most often used in programming languages where you can
@@ -301,7 +301,7 @@
my $addpiece = shift;
return sub { shift() + $addpiece };
}
-
+
$f1 = make_adder(20);
$f2 = make_adder(555);
@@ -520,11 +520,11 @@
my $count = 1;
sub counter { $count++ }
}
-
+
my $start = counter();
-
+
.... # code that calls counter();
-
+
my $end = counter();
In the previous example, you created a function-private variable
@@ -571,19 +571,19 @@
sub visible {
print "var has value $var\n";
}
-
+
sub dynamic {
local $var = 'local'; # new temporary value for the still-global
visible(); # variable called $var
}
-
+
sub lexical {
my $var = 'private'; # new private variable, $var
visible(); # (invisible outside of sub scope)
}
-
+
$var = 'global';
-
+
visible(); # prints global
dynamic(); # prints local
lexical(); # prints global
@@ -690,9 +690,9 @@
which prints what its arguments list:
sub bar { &foo }
-
+
sub foo { print "Args in foo are: @_\n" }
-
+
bar( qw( a b c ) );
When you call C<bar> with arguments, you see that C<foo> got the same C<@_>:
@@ -704,14 +704,14 @@
the example to put parentheses after the call to C<foo> changes the program:
sub bar { &foo() }
-
+
sub foo { print "Args in foo are: @_\n" }
-
+
bar( qw( a b c ) );
Now the output shows that C<foo> doesn't get the C<@_> from its caller.
- Args in foo are:
+ Args in foo are:
The main use of the C<@_> pass-through feature is to write subroutines
whose main job it is to call other subroutines for you. For further
@@ -722,14 +722,14 @@
In Perl 5.10, use the C<given-when> construct described in L<perlsyn>:
use 5.010;
-
+
given ( $string ) {
when( 'Fred' ) { say "I found Fred!" }
when( 'Barney' ) { say "I found Barney!" }
when( /Bamm-?Bamm/ ) { say "I found Bamm-Bamm!" }
default { say "I don't recognize the name!" }
};
-
+
If one wants to use pure Perl and to be compatible with Perl versions
prior to 5.10, the general answer is to use C<if-elsif-else>:
@@ -800,7 +800,7 @@
"done" => sub { die "See ya!" },
"mad" => \&angry,
);
-
+
print "How are you? ";
chomp($string = <STDIN>);
if ($commands{$string}) {
@@ -864,22 +864,22 @@
use Scalar::Util qw(blessed);
my $object_package = blessed( $object );
-
+
Most of the time, you shouldn't care what package an object is blessed
into, however, as long as it claims to inherit from that class:
my $is_right_class = eval { $object->isa( $package ) }; # true or false
-If you want to find the package calling your code, perhaps to give better
+If you want to find the package calling your code, perhaps to give better
diagnostics as C<Carp> does, use the C<caller> built-in:
sub foo {
my @args = ...;
my( $package, $filename, $line ) = caller;
-
+
print "I was called from package $package\n";
);
-
+
By default, your program starts in package C<main>, so you should
always be in some package unless someone uses the C<package> built-in
with no namespace. See the C<package> entry in L<perlfunc> for the
@@ -894,18 +894,18 @@
with <=end>.
# program is here
-
+
=begin comment
-
+
all of this stuff
-
+
here will be ignored
by everyone
-
+
=end comment
-
+
=cut
-
+
# program continues
The pod directives cannot go just anywhere. You must put a
Modified: perlfaq/trunk/perlfaq8.pod
==============================================================================
--- perlfaq/trunk/perlfaq8.pod (original)
+++ perlfaq/trunk/perlfaq8.pod Wed Oct 1 16:17:11 2008
@@ -180,12 +180,12 @@
that tells the terminal to clear the screen. Once you have that
sequence, output it when you want to clear the screen.
-You can use the C<Term::ANSIScreen> module to get the special
+You can use the C<Term::ANSIScreen> module to get the special
sequence. Import the C<cls> function (or the C<:screen> tag):
use Term::ANSIScreen qw(cls);
my $clear_screen = cls();
-
+
print $clear_screen;
The C<Term::Cap> module can also get the special sequence if you want
@@ -196,18 +196,18 @@
$terminal = Term::Cap->Tgetent( { OSPEED => 9600 } );
$clear_string = $terminal->Tputs('cl');
-
+
print $clear_screen;
-On Windows, you can use the C<Win32::Console> module. After creating
+On Windows, you can use the C<Win32::Console> module. After creating
an object for the output filehandle you want to affect, call the
C<Cls> method:
Win32::Console;
-
+
$OUT = Win32::Console->new(STD_OUTPUT_HANDLE);
my $clear_string = $OUT->Cls;
-
+
print $clear_screen;
If you have a command-line program that does the job, you can call
@@ -1227,7 +1227,7 @@
C<import> method for the loaded package. These two are the same:
use MODULE qw(import list);
-
+
BEGIN {
require MODULE;
MODULE->import(import list);
@@ -1237,14 +1237,14 @@
import list. Both of these still happen at compile-time:
use MODULE ();
-
+
BEGIN {
require MODULE;
}
Since C<use> will also call the C<import> method, the actual value
for C<MODULE> must be a bareword. That is, C<use> cannot load files
-by name, although C<require> can:
+by name, although C<require> can:
require "$ENV{HOME}/lib/Foo.pm"; # no @INC searching!
@@ -1268,7 +1268,7 @@
For C<Build.PL>-based distributions, use the --install_base option:
- perl Build.PL --install_base /mydir/perl
+ perl Build.PL --install_base /mydir/perl
You can configure CPAN.pm to automatically use this option too:
@@ -1285,7 +1285,7 @@
at compile time:
use lib $directory;
-
+
The trick in this task is to find the directory. Before your script does
anything else (such as a C<chdir>), you can get the current working
directory with the C<Cwd> module, which comes with Perl:
@@ -1294,25 +1294,25 @@
use Cwd;
our $directory = cwd;
}
-
+
use lib $directory;
-
+
You can do a similar thing with the value of C<$0>, which holds the
script name. That might hold a relative path, but C<rel2abs> can turn
-it into an absolute path. Once you have the
+it into an absolute path. Once you have the
- BEGIN {
+ BEGIN {
use File::Spec::Functions qw(rel2abs);
use File::Basename qw(dirname);
-
+
my $path = rel2abs( $0 );
our $directory = dirname( $path );
}
-
+
use lib $directory;
The C<FindBin> module, which comes with Perl, might work. It finds the
-directory of the currently running script and puts it in C<$Bin>, which
+directory of the currently running script and puts it in C<$Bin>, which
you can then use to construct the right library path:
use FindBin qw($Bin);
Modified: perlfaq/trunk/perlfaq9.pod
==============================================================================
--- perlfaq/trunk/perlfaq9.pod (original)
+++ perlfaq/trunk/perlfaq9.pod Wed Oct 1 16:17:11 2008
@@ -290,24 +290,24 @@
returns the escaped string:
my $original = "Colon : Hash # Percent %";
-
+
my $escaped = uri_escape( $original )
-
+
print "$string\n"; # 'Colon%20%3A%20Hash%20%23%20Percent%20%25%20'
To decode the string, use the C<uri_unescape> function:
my $unescaped = uri_unescape( $escaped );
-
+
print $unescaped; # back to original
-
+
If you wanted to do it yourself, you simply need to replace the
reserved characters with their encodings. A global substitution
is one way to do it:
# encode
$string =~ s/([^^A-Za-z0-9\-_.!~*'()])/ sprintf "%%%0x", ord $1 /eg;
-
+
#decode
$string =~ s/%([A-Fa-f\d]{2})/chr hex $1/eg;