| 1 | # vim: set ts=2 sts=2 sw=2 expandtab smarttab: | ||||||||||||||||||||||||||||
| 2 | # | ||||||||||||||||||||||||||||
| 3 | # This file is part of String-Substitution | ||||||||||||||||||||||||||||
| 4 | # | ||||||||||||||||||||||||||||
| 5 | # This software is copyright (c) 2010 by Randy Stauner. | ||||||||||||||||||||||||||||
| 6 | # | ||||||||||||||||||||||||||||
| 7 | # This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 8 | # the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||
| 9 | # | ||||||||||||||||||||||||||||
| 10 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | package String::Substitution; | ||||||||||||||||||||||||||||
| 14 | # git description: v1.001-5-gd591202 | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 3 | our $AUTHORITY = 'cpan:RWSTAUNER'; | |||||||||||||||||||||||||||
| 17 | # ABSTRACT: Simple runtime string substitution functions | ||||||||||||||||||||||||||||
| 18 | 3 | $String::Substitution::VERSION = '1.002'; | |||||||||||||||||||||||||||
| 19 | 3 | ▶ | use Sub::Exporter 0.982; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | { | ||||||||||||||||||||||||||||
| 21 | 3 | my $exports = { | |||||||||||||||||||||||||||
| 22 | exports => [qw(interpolate_match_vars last_match_vars)], | ||||||||||||||||||||||||||||
| 23 | groups => {} | ||||||||||||||||||||||||||||
| 24 | }; | ||||||||||||||||||||||||||||
| 25 | 3 | my @funcs = qw(sub gsub); | |||||||||||||||||||||||||||
| 26 | 3 | foreach my $suffix ( qw(copy modify context) ){ | |||||||||||||||||||||||||||
| 27 | 9 | push(@{ $exports->{exports} }, map { "${_}_${suffix}" } @funcs); | |||||||||||||||||||||||||||
| 28 | $exports->{groups}->{$suffix} = [ | ||||||||||||||||||||||||||||
| 29 | 9 | map { ("${_}_${suffix}" => { -as => $_ }) } @funcs | |||||||||||||||||||||||||||
| 30 | ]; | ||||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||
| 32 | 3 | Sub::Exporter::setup_exporter($exports); | |||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 174 | ▶ | sub gsub_copy { | ||||||||||||||||||||||||||
|
Subroutine
gsub_copy: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 37 | 174 | my ($string, $pattern, $replacement) = @_; | |||||||||||||||||||||||||||
| 38 | 174 | $string =~ s/$pattern/ | |||||||||||||||||||||||||||
| 39 | 166 | _replacement_sub($replacement)->(last_match_vars());/ge; | |||||||||||||||||||||||||||
| 40 | 174 | return $string; | |||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 174 | ▶ | sub gsub_modify { | ||||||||||||||||||||||||||
|
Subroutine
gsub_modify: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 45 | 174 | my ( undef, $pattern, $replacement ) = @_; | |||||||||||||||||||||||||||
| 46 | 174 | return $_[0] =~ s/$pattern/ | |||||||||||||||||||||||||||
| 47 | 166 | _replacement_sub($replacement)->(last_match_vars());/ge; | |||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 174 | ▶ | sub gsub_context { | ||||||||||||||||||||||||||
|
Subroutine
gsub_context: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 52 | return defined wantarray | ||||||||||||||||||||||||||||
| 53 | 174 | ▶ | ? gsub_copy(@_) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | : gsub_modify(@_); | ||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 528 | ▶ | sub interpolate_match_vars { | ||||||||||||||||||||||||||
|
Subroutine
interpolate_match_vars: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 59 | 528 | my ($replacement, @matched) = @_; | |||||||||||||||||||||||||||
| 60 | 528 | my $string = $replacement; | |||||||||||||||||||||||||||
| 61 | # Handling backslash-escapes and variable interpolations | ||||||||||||||||||||||||||||
| 62 | # in the same substitution (alternation) keeps track of the position | ||||||||||||||||||||||||||||
| 63 | # in the string so that we don't have to count backslashes. | ||||||||||||||||||||||||||||
| 64 | 528 | $string =~ | |||||||||||||||||||||||||||
| 65 | s/ | ||||||||||||||||||||||||||||
| 66 | (?: | ||||||||||||||||||||||||||||
| 67 | \\(.) # grab escaped characters (including $) | ||||||||||||||||||||||||||||
| 68 | | | ||||||||||||||||||||||||||||
| 69 | (?: | ||||||||||||||||||||||||||||
| 70 | \$\{([1-9]\d*)\} # match "${1}" (not unrelated '${0}') | ||||||||||||||||||||||||||||
| 71 | | | ||||||||||||||||||||||||||||
| 72 | \$ ([1-9]\d*) # match "$1" (not unrelated '$0') | ||||||||||||||||||||||||||||
| 73 | ) | ||||||||||||||||||||||||||||
| 74 | ) | ||||||||||||||||||||||||||||
| 75 | / | ||||||||||||||||||||||||||||
| 76 | 840 | ▶ | defined $1 | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$2 || $3
MC/DC0%0/2
$2 || $3
$2
$3
Truth table66%2/3
A || B
A= $2B= $3
| |||||||||||||||||||||||||||||
| 77 | ? $1 # if something was escaped drop the \\ | ||||||||||||||||||||||||||||
| 78 | : $matched[$2 || $3]; # else use braced or unbraced number | ||||||||||||||||||||||||||||
| 79 | # ($2 will never contain '0') | ||||||||||||||||||||||||||||
| 80 | /xge; | ||||||||||||||||||||||||||||
| 81 | 528 | return $string; | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 664 | ▶ | sub last_match_vars { | ||||||||||||||||||||||||||
|
Subroutine
last_match_vars: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 86 | 3 | ▶ | no strict 'refs'; ## no critic | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 87 | return ( | ||||||||||||||||||||||||||||
| 88 | # fake $& with a substr to avoid performance penalty (see perlvar) | ||||||||||||||||||||||||||||
| 89 | #(@_ ? substr($_[0], $-[0], $+[0] - $-[0]) : undef), | ||||||||||||||||||||||||||||
| 90 | undef, | ||||||||||||||||||||||||||||
| 91 | # $1, $2 .. | ||||||||||||||||||||||||||||
| 92 | 664 | ▶ | map { defined($$_) ? $$_ : '' } ( 1 .. $#- ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | ); | ||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | # Return a sub that will get matched vars array passed to it | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 664 | ▶ | sub _replacement_sub { | ||||||||||||||||||||||||||
|
Subroutine
_replacement_sub: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 99 | 664 | my ($rep) = @_; | |||||||||||||||||||||||||||
| 100 | # if $rep is not a sub, assume it's a string to be interpolated | ||||||||||||||||||||||||||||
| 101 | ref $rep | ||||||||||||||||||||||||||||
| 102 | ? $rep | ||||||||||||||||||||||||||||
| 103 | 664 | ▶ | : sub { interpolate_match_vars($rep, @_); }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 196 | ▶ | sub sub_copy { | ||||||||||||||||||||||||||
|
Subroutine
sub_copy: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 108 | 196 | my ($string, $pattern, $replacement) = @_; | |||||||||||||||||||||||||||
| 109 | 196 | $string =~ s/$pattern/ | |||||||||||||||||||||||||||
| 110 | 166 | _replacement_sub($replacement)->(last_match_vars());/e; | |||||||||||||||||||||||||||
| 111 | 196 | return $string; | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 196 | ▶ | sub sub_modify { | ||||||||||||||||||||||||||
|
Subroutine
sub_modify: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 116 | 196 | my ( undef, $pattern, $replacement ) = @_; | |||||||||||||||||||||||||||
| 117 | 196 | return $_[0] =~ s/$pattern/ | |||||||||||||||||||||||||||
| 118 | 166 | _replacement_sub($replacement)->(last_match_vars());/e; | |||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 196 | ▶ | sub sub_context { | ||||||||||||||||||||||||||
|
Subroutine
sub_context: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 123 | return defined wantarray | ||||||||||||||||||||||||||||
| 124 | 196 | ▶ | ? sub_copy(@_) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 125 | : sub_modify(@_); | ||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 3 | 1; | |||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | __END__ | ||||||||||||||||||||||||||||