| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | ## ---------------------------------------------------------------------------- | 
| 2 |  |  |  |  |  |  | #  String::Gsub::Functions | 
| 3 |  |  |  |  |  |  | # ----------------------------------------------------------------------------- | 
| 4 |  |  |  |  |  |  | # Mastering programmed by YAMASHINA Hio | 
| 5 |  |  |  |  |  |  | # | 
| 6 |  |  |  |  |  |  | # Copyright 2006 YAMASHINA Hio | 
| 7 |  |  |  |  |  |  | # ----------------------------------------------------------------------------- | 
| 8 |  |  |  |  |  |  | # $Id$ | 
| 9 |  |  |  |  |  |  | # ----------------------------------------------------------------------------- | 
| 10 |  |  |  |  |  |  | package String::Gsub::Functions; | 
| 11 | 4 |  |  | 4 |  | 24704 | use warnings; | 
|  | 4 |  |  |  |  | 7 |  | 
|  | 4 |  |  |  |  | 96 |  | 
| 12 | 4 |  |  | 4 |  | 19 | use strict; | 
|  | 4 |  |  |  |  | 6 |  | 
|  | 4 |  |  |  |  | 99 |  | 
| 13 | 4 |  |  | 4 |  | 2285 | use String::Gsub::Matcher; | 
|  | 4 |  |  |  |  | 11 |  | 
|  | 4 |  |  |  |  | 110 |  | 
| 14 |  |  |  |  |  |  |  | 
| 15 | 4 |  |  | 4 |  | 26 | use base qw(Exporter); | 
|  | 4 |  |  |  |  | 25 |  | 
|  | 4 |  |  |  |  | 2225 |  | 
| 16 |  |  |  |  |  |  | our @EXPORT_OK = qw(gsub gsubx subs subsx); | 
| 17 |  |  |  |  |  |  |  | 
| 18 |  |  |  |  |  |  | 1; | 
| 19 |  |  |  |  |  |  |  | 
| 20 |  |  |  |  |  |  | # ----------------------------------------------------------------------------- | 
| 21 |  |  |  |  |  |  | # gsub($str, $regex, $replacement); | 
| 22 |  |  |  |  |  |  | #   $regex is qr// or "string". | 
| 23 |  |  |  |  |  |  | #   $replacement is sub{} or "string". | 
| 24 |  |  |  |  |  |  | #   returns new value. | 
| 25 |  |  |  |  |  |  | # | 
| 26 |  |  |  |  |  |  | sub gsub($$$) | 
| 27 |  |  |  |  |  |  | { | 
| 28 | 10 |  |  | 10 | 1 | 48 | splice(@_, 0, 1, $_[0]); # copy. | 
| 29 | 10 |  |  |  |  | 25 | &gsubx; | 
| 30 |  |  |  |  |  |  | } | 
| 31 |  |  |  |  |  |  |  | 
| 32 |  |  |  |  |  |  | # ----------------------------------------------------------------------------- | 
| 33 |  |  |  |  |  |  | # gsubx($str, $regex, $replacement); | 
| 34 |  |  |  |  |  |  | #   $regex is qr// or "string". | 
| 35 |  |  |  |  |  |  | #   $replacement is sub{} or "string". | 
| 36 |  |  |  |  |  |  | #   returns new value and $str is replaced with result too. | 
| 37 |  |  |  |  |  |  | sub gsubx($$$) | 
| 38 |  |  |  |  |  |  | { | 
| 39 |  |  |  |  |  |  | #my $str = $_[0]; | 
| 40 | 13 |  |  | 13 | 1 | 1774 | my $re  = $_[1]; | 
| 41 | 13 |  |  |  |  | 21 | my $sub = $_[2]; | 
| 42 |  |  |  |  |  |  |  | 
| 43 | 13 | 100 |  |  |  | 71 | ref($re)  or $re  = qr/\Q$re\E/; | 
| 44 | 13 | 100 |  | 12 |  | 33 | ref($sub) or $sub = do{ my$tmpl=$sub; sub{ shift->expand($tmpl); }; }; | 
|  | 6 |  |  |  |  | 9 |  | 
|  | 6 |  |  |  |  | 27 |  | 
|  | 12 |  |  |  |  | 36 |  | 
| 45 | 13 |  |  |  |  | 112 | $_[0] =~ s{$re} | 
| 46 |  |  |  |  |  |  | { | 
| 47 | 26 |  |  |  |  | 175 | my $match = String::Gsub::Matcher->new($_[0]); | 
| 48 | 26 |  |  |  |  | 60 | $sub->($match); | 
| 49 |  |  |  |  |  |  | }ge; | 
| 50 | 13 |  |  |  |  | 107 | $_[0]; | 
| 51 |  |  |  |  |  |  | } | 
| 52 |  |  |  |  |  |  |  | 
| 53 |  |  |  |  |  |  | # ----------------------------------------------------------------------------- | 
| 54 |  |  |  |  |  |  | # subs($str, $regex, $replacement); | 
| 55 |  |  |  |  |  |  | #   $regex is qr// or "string". | 
| 56 |  |  |  |  |  |  | #   $replacement is sub{} or "string". | 
| 57 |  |  |  |  |  |  | #   returns new value. | 
| 58 |  |  |  |  |  |  | # | 
| 59 |  |  |  |  |  |  | sub subs($$$) | 
| 60 |  |  |  |  |  |  | { | 
| 61 | 5 |  |  | 5 | 1 | 1655 | splice(@_, 0, 1, $_[0]); # copy. | 
| 62 | 5 |  |  |  |  | 13 | &subsx; | 
| 63 |  |  |  |  |  |  | } | 
| 64 |  |  |  |  |  |  |  | 
| 65 |  |  |  |  |  |  | # ----------------------------------------------------------------------------- | 
| 66 |  |  |  |  |  |  | # subsx($str, $regex, $replacement); | 
| 67 |  |  |  |  |  |  | #   $regex is qr// or "string". | 
| 68 |  |  |  |  |  |  | #   $replacement is sub{} or "string". | 
| 69 |  |  |  |  |  |  | #   returns new value and $str is replaced with result too. | 
| 70 |  |  |  |  |  |  | sub subsx($$$) | 
| 71 |  |  |  |  |  |  | { | 
| 72 |  |  |  |  |  |  | #my $str = $_[0]; | 
| 73 | 8 |  |  | 8 | 1 | 1444 | my $re  = $_[1]; | 
| 74 | 8 |  |  |  |  | 16 | my $sub = $_[2]; | 
| 75 |  |  |  |  |  |  |  | 
| 76 | 8 | 50 |  |  |  | 34 | ref($re)  or $re  = qr/\Q$re\E/; | 
| 77 | 8 | 100 |  | 2 |  | 23 | ref($sub) or $sub = do{ my$tmpl=$sub; sub{ shift->expand($tmpl); }; }; | 
|  | 2 |  |  |  |  | 5 |  | 
|  | 2 |  |  |  |  | 18 |  | 
|  | 2 |  |  |  |  | 8 |  | 
| 78 | 8 |  |  |  |  | 53 | $_[0] =~ s{$re} | 
| 79 |  |  |  |  |  |  | { | 
| 80 | 8 |  |  |  |  | 37 | my $match = String::Gsub::Matcher->new($_[0]); | 
| 81 | 8 |  |  |  |  | 91 | $sub->($match); | 
| 82 |  |  |  |  |  |  | }e; | 
| 83 | 8 |  |  |  |  | 72 | $_[0]; | 
| 84 |  |  |  |  |  |  | } | 
| 85 |  |  |  |  |  |  |  | 
| 86 |  |  |  |  |  |  | __END__ |