summaryrefslogtreecommitdiff
path: root/pod/perlfaq6.pod
diff options
context:
space:
mode:
authorJeff Pinyan <japhy@pobox.com>2001-05-29 14:03:27 -0400
committerJarkko Hietaniemi <jhi@iki.fi>2001-05-30 01:35:31 +0000
commit74b9445a971233515f866c1f132853d3cb88841a (patch)
tree92c815359809164ae024832cce6aa0ed2b6aa431 /pod/perlfaq6.pod
parent3e2ca581c1c499ffde24bad697337995b6562165 (diff)
downloadperl-74b9445a971233515f866c1f132853d3cb88841a.tar.gz
Re: [PATCH] perlfaq6.pod -- case-aware s///
Message-ID: <Pine.GSO.4.21.0105291802330.1961-100000@crusoe.crusoe.net> p4raw-id: //depot/perl@10303
Diffstat (limited to 'pod/perlfaq6.pod')
-rw-r--r--pod/perlfaq6.pod15
1 files changed, 15 insertions, 0 deletions
diff --git a/pod/perlfaq6.pod b/pod/perlfaq6.pod
index ed6c01b31b..45f096632c 100644
--- a/pod/perlfaq6.pod
+++ b/pod/perlfaq6.pod
@@ -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.