perlipc typo
[p5sagit/p5-mst-13.2.git] / pod / perlfaq6.pod
index 0a134c3..6b0f3bb 100644 (file)
@@ -1,6 +1,6 @@
 =head1 NAME
 
-perlfaq6 - Regular Expressions ($Revision: 1.20 $, $Date: 2003/01/03 20:05:28 $)
+perlfaq6 - Regular Expressions ($Revision: 1.27 $, $Date: 2004/11/03 22:52:16 $)
 
 =head1 DESCRIPTION
 
@@ -151,7 +151,19 @@ Up to Perl 5.8.0, $/ has to be a string.  This may change in 5.10,
 but don't get your hopes up. Until then, you can use these examples
 if you really need to do this.
 
-Use the four argument form of sysread to continually add to
+If you have File::Stream, this is easy.
+
+                        use File::Stream;
+             my $stream = File::Stream->new(
+                  $filehandle,
+                  separator => qr/\s*,\s*/,
+                  );
+
+                        print "$_\n" while <$stream>;
+
+If you don't have File::Stream, you have to do a little more work.
+
+You can use the four argument form of sysread to continually add to
 a buffer.  After you add to the buffer, you check if you have a
 complete line (using your regular expression).
 
@@ -354,7 +366,7 @@ created by Jeffrey Friedl and later modified by Fred Curtis.
 
     $/ = undef;
     $_ = <>;
-    s#/\*[^*]*\*+([^/*][^*]*\*+)*/|("(\\.|[^"\\])*"|'(\\.|[^'\\])*'|.[^/"'\\]*)#$2#gs
+    s#/\*[^*]*\*+([^/*][^*]*\*+)*/|("(\\.|[^"\\])*"|'(\\.|[^'\\])*'|.[^/"'\\]*)#defined $2 ? $2 : ""#gse;
     print;
 
 This could, of course, be more legibly written with the C</x> modifier, adding
@@ -395,11 +407,11 @@ whitespace and comments.  Here it is expanded, courtesy of Fred Curtis.
          .           ##  Anything other char
          [^/"'\\]*   ##  Chars which doesn't start a comment, string or escape
        )
-     }{$2}gxs;
+     }{defined $2 ? $2 : ""}gxse;
 
 A slight modification also removes C++ comments:
 
-    s#/\*[^*]*\*+([^/*][^*]*\*+)*/|//[^\n]*|("(\\.|[^"\\])*"|'(\\.|[^'\\])*'|.[^/"'\\]*)#$2#gs;
+    s#/\*[^*]*\*+([^/*][^*]*\*+)*/|//[^\n]*|("(\\.|[^"\\])*"|'(\\.|[^'\\])*'|.[^/"'\\]*)#defined $2 ? $2 : ""#gse;
 
 =head2 Can I use Perl regular expressions to match balanced text?
 
@@ -679,15 +691,18 @@ guaranteed is slowness.)  See the book "Mastering Regular Expressions"
 hope to know on these matters (a full citation appears in
 L<perlfaq2>).
 
-=head2 What's wrong with using grep or map in a void context?
+=head2 What's wrong with using grep in a void context?
 
-The problem is that both grep and map build a return list,
-regardless of the context.  This means you're making Perl go
-to the trouble of building a list that you then just throw away.
-If the list is large, you waste both time and space.  If your
-intent is to iterate over the list then use a for loop for this
+The problem is that grep builds a return list, regardless of the context.
+This means you're making Perl go to the trouble of building a list that
+you then just throw away. If the list is large, you waste both time and space.
+If your intent is to iterate over the list, then use a for loop for this
 purpose.
 
+In perls older than 5.8.1, map suffers from this problem as well.
+But since 5.8.1, this has been fixed, and map is context aware - in void
+context, no lists are constructed.
+
 =head2 How can I match strings with multibyte characters?
 
 Starting from Perl 5.6 Perl has had some level of multibyte character
@@ -742,17 +757,17 @@ Or like this:
    }
 
 Here's another, slightly less painful, way to do it from Benjamin
-Goldberg:
+Goldberg, who uses a zero-width negative look-behind assertion.
 
-       $martian =~ m/
-          (?!<[A-Z])
-          (?:[A-Z][A-Z])*?
-          GX
-       /x;
+       print "found GX!\n" if  $martian =~ m/
+                  (?<![A-Z])
+                  (?:[A-Z][A-Z])*?
+                  GX
+               /x;
 
 This succeeds if the "martian" character GX is in the string, and fails
-otherwise.  If you don't like using (?!<), you can replace (?!<[A-Z])
-with (?:^|[^A-Z]).
+otherwise.  If you don't like using (?<!), a zero-width negative
+look-behind assertion, you can replace (?<![A-Z]) with (?:^|[^A-Z]).
 
 It does have the drawback of putting the wrong thing in $-[0] and $+[0],
 but this usually can be worked around.