X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=pod%2Fperlfaq6.pod;h=121f66c491dcb2166f3f0a0333fae711ee3b3ad6;hb=e5dd39fcc65538f6d292cb5228105f85fe9eff3e;hp=a032d49a703569b56cb684f65ce6b7dd1a97562e;hpb=a6dd486b7feb5918da837e5ad585c8ce954f9bbf;p=p5sagit%2Fp5-mst-13.2.git diff --git a/pod/perlfaq6.pod b/pod/perlfaq6.pod index a032d49..121f66c 100644 --- a/pod/perlfaq6.pod +++ b/pod/perlfaq6.pod @@ -115,7 +115,7 @@ Here's code that finds everything between START and END in a paragraph: undef $/; # read in whole file, not just one line or paragraph while ( <> ) { - while ( /START(.*?)END/sm ) { # /s makes . cross line boundaries + while ( /START(.*?)END/sgm ) { # /s makes . cross line boundaries print "$1\n"; } } @@ -186,7 +186,7 @@ properties of bitwise xor on ASCII strings. $old = 'test'; $new = 'success'; - s{(\Q$old\E} + s{(\Q$old\E)} { uc $new | (uc $1 ^ $1) . (uc(substr $1, -1) ^ substr $1, -1) x (length($new) - length $1) @@ -212,6 +212,21 @@ This prints: this is a SUcCESS case +As an alternative, to keep the case of the replacement word if it is +longer than the original, you can use this code, by Jeff Pinyan: + + sub preserve_case { + my ($from, $to) = @_; + my ($lf, $lt) = map length, @_; + + if ($lt < $lf) { $from = substr $from, 0, $lt } + else { $from .= substr $to, $lf } + + return uc $to | ($from ^ uc $from); + } + +This changes the sentence to "this is a SUcCess case." + Just to show that C programmers can write C in any programming language, if you prefer a more C-like solution, the following script makes the substitution have the same case, letter by letter, as the original. @@ -404,7 +419,7 @@ expression engine to find a match as quickly as possible and pass control on to whatever is next in line, like you would if you were playing hot potato. -=head2 How do I process each word on each line? +=head2 How do I process each word on each line? Use the split function: