This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Add perldelta for Indented Here-docs
[perl5.git] / pod / perlrequick.pod
index 557cd49..d72bd2b 100644 (file)
@@ -19,7 +19,7 @@ contains that word:
     "Hello World" =~ /World/;  # matches
 
 In this statement, C<World> is a regex and the C<//> enclosing
-C</World/> tells perl to search a string for a match.  The operator
+C</World/> tells Perl to search a string for a match.  The operator
 C<=~> associates the string with the regex match and produces a true
 value if the regex matched, or false if the regex did not match.  In
 our case, C<World> matches the second word in C<"Hello World">, so the
@@ -58,7 +58,7 @@ statement to be true:
     "Hello World" =~ /o W/;    # matches, ' ' is an ordinary char
     "Hello World" =~ /World /; # doesn't match, no ' ' at end
 
-perl will always match at the earliest possible point in the string:
+Perl will always match at the earliest possible point in the string:
 
     "Hello World" =~ /o/;       # matches 'o' in 'Hello'
     "That hat is red" =~ /hat/; # matches 'hat' in 'That'
@@ -85,8 +85,9 @@ for a carriage return.  Arbitrary bytes are represented by octal
 escape sequences, e.g., C<\033>, or hexadecimal escape sequences,
 e.g., C<\x1B>:
 
-    "1000\t2000" =~ m(0\t2)      # matches
-    "cat"      =~ /\143\x61\x74/ # matches in ASCII, but a weird way to spell cat
+    "1000\t2000" =~ m(0\t2)  # matches
+    "cat" =~ /\143\x61\x74/  # matches in ASCII, but 
+                             # a weird way to spell cat
 
 Regexes are treated mostly as double-quoted strings, so variable
 substitution works:
@@ -162,8 +163,9 @@ character, or the match fails.  Then
     /[a^]at/;  # matches 'aat' or '^at'; here '^' is ordinary
 
 Perl has several abbreviations for common character classes. (These
-definitions are those that Perl uses in ASCII mode with the C</a> modifier.
-See L<perlrecharclass/Backslash sequences> for details.)
+definitions are those that Perl uses in ASCII-safe mode with the C</a> modifier.
+Otherwise they could match many more non-ASCII Unicode characters as
+well.  See L<perlrecharclass/Backslash sequences> for details.)
 
 =over 4
 
@@ -231,15 +233,20 @@ character and a non-word character C<\w\W> or C<\W\w>:
 In the last example, the end of the string is considered a word
 boundary.
 
+For natural language processing (so that, for example, apostrophes are
+included in words), use instead C<\b{wb}>
+
+    "don't" =~ / .+? \b{wb} /x;  # matches the whole string
+
 =head2 Matching this or that
 
 We can match different character strings with the B<alternation>
 metacharacter C<'|'>.  To match C<dog> or C<cat>, we form the regex
-C<dog|cat>.  As before, perl will try to match the regex at the
+C<dog|cat>.  As before, Perl will try to match the regex at the
 earliest possible point in the string.  At each character position,
-perl will first try to match the first alternative, C<dog>.  If
-C<dog> doesn't match, perl will then try the next alternative, C<cat>.
-If C<cat> doesn't match either, then the match fails and perl moves to
+Perl will first try to match the first alternative, C<dog>.  If
+C<dog> doesn't match, Perl will then try the next alternative, C<cat>.
+If C<cat> doesn't match either, then the match fails and Perl moves to
 the next position in the string.  Some examples:
 
     "cats and dogs" =~ /cat|dog|bird/;  # matches "cat"
@@ -352,7 +359,7 @@ Here are some examples:
     /(\w+)\s+\g1/;    # match doubled words of arbitrary length
     $year =~ /^\d{2,4}$/;  # make sure year is at least 2 but not more
                            # than 4 digits
-    $year =~ /^\d{4}$|^\d{2}$/;    # better match; throw out 3 digit dates
+    $year =~ /^\d{4}$|^\d{2}$/; # better match; throw out 3 digit dates
 
 These quantifiers will try to match as much of the string as possible,
 while still allowing the regex to match.  So we have
@@ -370,22 +377,7 @@ no string left to it, so it matches 0 times.
 =head2 More matching
 
 There are a few more things you might want to know about matching
-operators.  In the code
-
-    $pattern = 'Seuss';
-    while (<>) {
-        print if /$pattern/;
-    }
-
-perl has to re-evaluate C<$pattern> each time through the loop.  If
-C<$pattern> won't be changing, use the C<//o> modifier, to only
-perform variable substitutions once.  If you don't want any
-substitutions at all, use the special delimiter C<m''>:
-
-    @pattern = ('Seuss');
-    m/@pattern/; # matches 'Seuss'
-    m'@pattern'; # matches the literal string '@pattern'
-
+operators.
 The global modifier C<//g> allows the matching operator to match
 within a string as many times as possible.  In scalar context,
 successive matches against a string will have C<//g> jump from match
@@ -451,7 +443,8 @@ substitute was bound to with C<=~>):
     print "$x $y\n"; # prints "I like dogs. I like cats."
 
     $x = "Cats are great.";
-    print $x =~ s/Cats/Dogs/r =~ s/Dogs/Frogs/r =~ s/Frogs/Hedgehogs/r, "\n";
+    print $x =~ s/Cats/Dogs/r =~ s/Dogs/Frogs/r =~
+        s/Frogs/Hedgehogs/r, "\n";
     # prints "Hedgehogs are great."
 
     @foo = map { s/[a-z]/X/r } qw(a b c 1 2 3);
@@ -507,6 +500,14 @@ the matched substrings from the groupings as well:
 Since the first character of $x matched the regex, C<split> prepended
 an empty initial element to the list.
 
+=head2 C<use re 'strict'>
+
+New in v5.22, this applies stricter rules than otherwise when compiling
+regular expression patterns.  It can find things that, while legal, may
+not be what you intended.
+
+See L<'strict' in re|re/'strict' mode>.
+
 =head1 BUGS
 
 None.