[svn:perlfaq] r6309 - perlfaq/trunk
[email protected] Wed, 17 May 2006 22:44:46 -0700 (PDT)
| Newsgroups | perl.cvs.perlfaq |
|---|---|
| Message-ID | <[email protected]> |
Author: comdog
Date: Wed May 17 22:44:45 2006
New Revision: 6309
Modified:
perlfaq/trunk/perlfaq3.pod
perlfaq/trunk/perlfaq4.pod
perlfaq/trunk/perlfaq6.pod
perlfaq/trunk/perlfaq7.pod
perlfaq/trunk/perlfaq8.pod
perlfaq/trunk/perlfaq9.pod
Log:
Normalize em dashes according to the Chicago Manual of Style. Use two
hyphens and no spaces around them.
Modified: perlfaq/trunk/perlfaq3.pod
==============================================================================
--- perlfaq/trunk/perlfaq3.pod (original)
+++ perlfaq/trunk/perlfaq3.pod Wed May 17 22:44:45 2006
@@ -210,8 +210,8 @@
Put that in your F<.exrc> file (replacing the caret characters
with control characters) and away you go. In insert mode, ^T is
-for indenting, ^D is for undenting, and ^O is for blockdenting--
-as it were. A more complete example, with comments, can be found at
+for indenting, ^D is for undenting, and ^O is for blockdenting--as
+it were. A more complete example, with comments, can be found at
http://www.cpan.org/authors/id/TOMC/scripts/toms.exrc.gz
The a2ps http://www-inf.enst.fr/%7Edemaille/a2ps/black+white.ps.gz does
Modified: perlfaq/trunk/perlfaq4.pod
==============================================================================
--- perlfaq/trunk/perlfaq4.pod (original)
+++ perlfaq/trunk/perlfaq4.pod Wed May 17 22:44:45 2006
@@ -342,7 +342,7 @@
BEGIN { srand() if $] < 5.004 }
5.004 and later automatically call C<srand> at the beginning. Don't
-call C<srand> more than once---you make your numbers less random,
+call C<srand> more than once--you make your numbers less random,
rather than more.
Computers are good at being predictable and bad at being random
@@ -829,7 +829,7 @@
=head2 How can I split a [character] delimited string except when inside [character]?
-Several modules can handle this sort of parsing---C<Text::Balanced>,
+Several modules can handle this sort of parsing--C<Text::Balanced>,
C<Text::CSV>, C<Text::CSV_XS>, and C<Text::ParseWords>, among others.
Take the example case of trying to split a string that is
@@ -1015,11 +1015,12 @@
=head2 What's wrong with always quoting "$vars"?
-The problem is that those double-quotes force stringification--
-coercing numbers and references into strings--even when you
-don't want them to be strings. Think of it this way: double-quote
-expansion is used to produce new strings. If you already
-have a string, why do you need more?
+The problem is that those double-quotes force
+stringification--coercing numbers and references into
+strings--even when you don't want them to be strings. Think
+of it this way: double-quote expansion is used to produce
+new strings. If you already have a string, why do you need
+more?
If you get used to writing odd things like these:
Modified: perlfaq/trunk/perlfaq6.pod
==============================================================================
--- perlfaq/trunk/perlfaq6.pod (original)
+++ perlfaq/trunk/perlfaq6.pod Wed May 17 22:44:45 2006
@@ -255,36 +255,36 @@
If the substitution has more characters than the string being substituted,
the case of the last character is used for the rest of the substitution.
- # Original by Nathan Torkington, massaged by Jeffrey Friedl
- #
- sub preserve_case($$)
- {
- my ($old, $new) = @_;
- my ($state) = 0; # 0 = no change; 1 = lc; 2 = uc
- my ($i, $oldlen, $newlen, $c) = (0, length($old), length($new));
- my ($len) = $oldlen < $newlen ? $oldlen : $newlen;
-
- for ($i = 0; $i < $len; $i++) {
- if ($c = substr($old, $i, 1), $c =~ /[\W\d_]/) {
- $state = 0;
- } elsif (lc $c eq $c) {
- substr($new, $i, 1) = lc(substr($new, $i, 1));
- $state = 1;
- } else {
- substr($new, $i, 1) = uc(substr($new, $i, 1));
- $state = 2;
- }
- }
- # finish up with any remaining new (for when new is longer than old)
- if ($newlen > $oldlen) {
- if ($state == 1) {
- substr($new, $oldlen) = lc(substr($new, $oldlen));
- } elsif ($state == 2) {
- substr($new, $oldlen) = uc(substr($new, $oldlen));
- }
- }
- return $new;
- }
+ # Original by Nathan Torkington, massaged by Jeffrey Friedl
+ #
+ sub preserve_case($$)
+ {
+ my ($old, $new) = @_;
+ my ($state) = 0; # 0 = no change; 1 = lc; 2 = uc
+ my ($i, $oldlen, $newlen, $c) = (0, length($old), length($new));
+ my ($len) = $oldlen < $newlen ? $oldlen : $newlen;
+
+ for ($i = 0; $i < $len; $i++) {
+ if ($c = substr($old, $i, 1), $c =~ /[\W\d_]/) {
+ $state = 0;
+ } elsif (lc $c eq $c) {
+ substr($new, $i, 1) = lc(substr($new, $i, 1));
+ $state = 1;
+ } else {
+ substr($new, $i, 1) = uc(substr($new, $i, 1));
+ $state = 2;
+ }
+ }
+ # finish up with any remaining new (for when new is longer than old)
+ if ($newlen > $oldlen) {
+ if ($state == 1) {
+ substr($new, $oldlen) = lc(substr($new, $oldlen));
+ } elsif ($state == 2) {
+ substr($new, $oldlen) = uc(substr($new, $oldlen));
+ }
+ }
+ return $new;
+ }
=head2 How can I make C<\w> match national character sets?
X<\w>
@@ -548,7 +548,7 @@
loop since it has no way to know what $pattern will be.
@patterns = qw( foo bar baz );
-
+
LINE: while( <DATA> )
{
foreach $pattern ( @patterns )
@@ -750,15 +750,15 @@
such as in a tokenizer. Jeffrey Friedl offers this example
which works in 5.004 or later.
- while (<>) {
- chomp;
- PARSER: {
- m/ \G( \d+\b )/gcx && do { print "number: $1\n"; redo; };
- m/ \G( \w+ )/gcx && do { print "word: $1\n"; redo; };
- m/ \G( \s+ )/gcx && do { print "space: $1\n"; redo; };
- m/ \G( [^\w\d]+ )/gcx && do { print "other: $1\n"; redo; };
- }
- }
+ while (<>) {
+ chomp;
+ PARSER: {
+ m/ \G( \d+\b )/gcx && do { print "number: $1\n"; redo; };
+ m/ \G( \w+ )/gcx && do { print "word: $1\n"; redo; };
+ m/ \G( \s+ )/gcx && do { print "space: $1\n"; redo; };
+ m/ \G( [^\w\d]+ )/gcx && do { print "other: $1\n"; redo; };
+ }
+ }
For each line, the PARSER loop first tries to match a series
of digits followed by a word boundary. This match has to
@@ -833,7 +833,7 @@
Here are a few ways, all painful, to deal with it:
# Make sure adjacent "martian" bytes are no longer adjacent.
- $martian =~ s/([A-Z][A-Z])/ $1 /g;
+ $martian =~ s/([A-Z][A-Z])/ $1 /g;
print "found GX!\n" if $martian =~ /GX/;
@@ -877,7 +877,7 @@
We don't have to hard-code patterns into the match operator (or
anything else that works with regular expressions). We can put the
pattern in a variable for later use.
-
+
The match operator is a double quote context, so you can interpolate
your variable just like a double quoted string. In this case, you
read the regular expression as user input and store it in C<$regex>.
@@ -885,24 +885,24 @@
match operator.
chomp( my $regex = <STDIN> );
-
+
if( $string =~ m/$regex/ ) { ... }
-Any regular expression special characters in C<$regex> are still
+Any regular expression special characters in C<$regex> are still
special, and the pattern still has to be valid or Perl will complain.
For instance, in this pattern there is an unpaired parenthesis.
my $regex = "Unmatched ( paren";
-
+
"Two parens to bind them all" =~ m/$regex/;
-
+
When Perl compiles the regular expression, it treats the parenthesis
as the start of a memory match. When it doesn't find the closing
parenthesis, it complains:
- Unmatched ( in regex; marked by <-- HERE in m/Unmatched ( <-- HERE paren/ at script line 3.
+ Unmatched ( in regex; marked by <-- HERE in m/Unmatched ( <-- HERE paren/ at script line 3.
-You can get around this in several ways depending on our situation.
+You can get around this in several ways depending on our situation.
First, if you don't want any of the characters in the string to be
special, you can escape them with C<quotemeta> before you use the string.
@@ -921,25 +921,25 @@
if( $string =~ m/\Q$regex\E/ ) { ... }
Alternately, you can use C<qr//>, the regular expression quote operator (see
-L<perlop> for more details). It quotes and perhaps compiles the pattern,
+L<perlop> for more details). It quotes and perhaps compiles the pattern,
and you can apply regular expression flags to the pattern.
chomp( my $input = <STDIN> );
-
+
my $regex = qr/$input/is;
- $string =~ m/$regex/ # same as m/$input/is;
+ $string =~ m/$regex/ # same as m/$input/is;
You might also want to trap any errors by wrapping an C<eval> block
around the whole thing.
chomp( my $input = <STDIN> );
-
- eval {
- if( $string =~ m/\Q$input\E/ ) { ... }
+
+ eval {
+ if( $string =~ m/\Q$input\E/ ) { ... }
};
warn $@ if $@;
-
+
Or...
my $regex = eval { qr/$input/is };
Modified: perlfaq/trunk/perlfaq7.pod
==============================================================================
--- perlfaq/trunk/perlfaq7.pod (original)
+++ perlfaq/trunk/perlfaq7.pod Wed May 17 22:44:45 2006
@@ -942,7 +942,7 @@
line in your perl script (the "shebang" line) does not contain the
right path to perl (or any other program capable of running scripts).
Sometimes this happens when you move the script from one machine to
-another and each machine has a different path to perl---/usr/bin/perl
+another and each machine has a different path to perl--/usr/bin/perl
versus /usr/local/bin/perl for instance. It may also indicate
that the source machine has CRLF line terminators and the
destination machine has LF only: the shell tries to find
Modified: perlfaq/trunk/perlfaq8.pod
==============================================================================
--- perlfaq/trunk/perlfaq8.pod (original)
+++ perlfaq/trunk/perlfaq8.pod Wed May 17 22:44:45 2006
@@ -556,7 +556,7 @@
L<perlfunc>).
Remember to check the modules that came with your distribution, and
-CPAN as well---someone may already have written a module to do it. On
+CPAN as well--someone may already have written a module to do it. On
Windows, try Win32::API. On Macs, try Mac::Carbon. If no module
has an interface to the C function, you can inline a bit of C in your
Perl source with Inline::C.
Modified: perlfaq/trunk/perlfaq9.pod
==============================================================================
--- perlfaq/trunk/perlfaq9.pod (original)
+++ perlfaq/trunk/perlfaq9.pod Wed May 17 22:44:45 2006
@@ -311,7 +311,7 @@
To enable authentication for your web server, you need to configure
your web server. The configuration is different for different sorts
-of web servers---apache does it differently from iPlanet which does
+of web servers--apache does it differently from iPlanet which does
it differently from IIS. Check your web server documentation for
the details for your particular server.
@@ -404,7 +404,7 @@
will not bounce). Modules like Mail::CheckUser and Mail::EXPN
try to interact with the domain name system or particular
mail servers to learn even more, but their methods do not
-work everywhere---especially for security conscious administrators.
+work everywhere--especially for security conscious administrators.
Many are tempted to try to eliminate many frequently-invalid
mail addresses with a simple regex, such as