| 1 | # Paranoid::Input -- Paranoid Input functions | ||||||||||||||||||||||||||||||||||||
| 2 | # | ||||||||||||||||||||||||||||||||||||
| 3 | # $Id: lib/Paranoid/Input.pm, 2.10 2022/03/08 00:01:04 acorliss Exp $ | ||||||||||||||||||||||||||||||||||||
| 4 | # | ||||||||||||||||||||||||||||||||||||
| 5 | # This software is free software. Similar to Perl, you can redistribute it | ||||||||||||||||||||||||||||||||||||
| 6 | # and/or modify it under the terms of either: | ||||||||||||||||||||||||||||||||||||
| 7 | # | ||||||||||||||||||||||||||||||||||||
| 8 | # a) the GNU General Public License | ||||||||||||||||||||||||||||||||||||
| 9 | # <https://www.gnu.org/licenses/gpl-1.0.html> as published by the | ||||||||||||||||||||||||||||||||||||
| 10 | # Free Software Foundation <http://www.fsf.org/>; either version 1 | ||||||||||||||||||||||||||||||||||||
| 11 | # <https://www.gnu.org/licenses/gpl-1.0.html>, or any later version | ||||||||||||||||||||||||||||||||||||
| 12 | # <https://www.gnu.org/licenses/license-list.html#GNUGPL>, or | ||||||||||||||||||||||||||||||||||||
| 13 | # b) the Artistic License 2.0 | ||||||||||||||||||||||||||||||||||||
| 14 | # <https://opensource.org/licenses/Artistic-2.0>, | ||||||||||||||||||||||||||||||||||||
| 15 | # | ||||||||||||||||||||||||||||||||||||
| 16 | # subject to the following additional term: No trademark rights to | ||||||||||||||||||||||||||||||||||||
| 17 | # "Paranoid" have been or are conveyed under any of the above licenses. | ||||||||||||||||||||||||||||||||||||
| 18 | # However, "Paranoid" may be used fairly to describe this unmodified | ||||||||||||||||||||||||||||||||||||
| 19 | # software, in good faith, but not as a trademark. | ||||||||||||||||||||||||||||||||||||
| 20 | # | ||||||||||||||||||||||||||||||||||||
| 21 | # (c) 2005 - 2020, Arthur Corliss (corliss@digitalmages.com) | ||||||||||||||||||||||||||||||||||||
| 22 | # (tm) 2008 - 2020, Paranoid Inc. (www.paranoid.com) | ||||||||||||||||||||||||||||||||||||
| 23 | # | ||||||||||||||||||||||||||||||||||||
| 24 | ##################################################################### | ||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||
| 26 | ##################################################################### | ||||||||||||||||||||||||||||||||||||
| 27 | # | ||||||||||||||||||||||||||||||||||||
| 28 | # Environment definitions | ||||||||||||||||||||||||||||||||||||
| 29 | # | ||||||||||||||||||||||||||||||||||||
| 30 | ##################################################################### | ||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||
| 32 | package Paranoid::Input; | ||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||
| 34 | 64 | ▶ | use 5.008; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||
| 36 | 64 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 37 | 64 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 38 | 64 | ▶ | use vars qw($VERSION @EXPORT @EXPORT_OK %EXPORT_TAGS); | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 39 | 64 | ▶ | use base qw(Exporter); | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 40 | 64 | ▶ | use Paranoid; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 41 | 64 | ▶ | use Paranoid::Debug qw(:all); | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 42 | 64 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||
| 44 | 64 | ($VERSION) = ( q$Revision: 2.10 $ =~ /(\d+(?:\.\d+)+)/sm ); | |||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||
| 46 | 64 | @EXPORT = qw(detaint stringMatch pchomp); | |||||||||||||||||||||||||||||||||||
| 47 | 64 | @EXPORT_OK = ( @EXPORT, qw(NEWLINE_REGEX) ); | |||||||||||||||||||||||||||||||||||
| 48 | 64 | %EXPORT_TAGS = ( all => [@EXPORT_OK], ); | |||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||
| 50 | 64 | ▶ | use constant NEWLINE_REGEX => qr#(?:\15\12|\15|\12)#so; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||
| 52 | ##################################################################### | ||||||||||||||||||||||||||||||||||||
| 53 | # | ||||||||||||||||||||||||||||||||||||
| 54 | # Module code follows | ||||||||||||||||||||||||||||||||||||
| 55 | # | ||||||||||||||||||||||||||||||||||||
| 56 | ##################################################################### | ||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||
| 58 | 51 | ▶ | sub pchomp (;\[$@%]) { | ||||||||||||||||||||||||||||||||||
|
Subroutine
pchomp: called CC=14 SCAR=26.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||
| 60 | # Purpose: Platform neutral chomping | ||||||||||||||||||||||||||||||||||||
| 61 | # Returns: same as chomp | ||||||||||||||||||||||||||||||||||||
| 62 | # Usage: $n = pchomp($string); | ||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||
| 64 | 51 | my ($ref) = @_; | |||||||||||||||||||||||||||||||||||
| 65 | 51 | my $rv = 0; | |||||||||||||||||||||||||||||||||||
| 66 | 51 | my $nl = NEWLINE_REGEX; | |||||||||||||||||||||||||||||||||||
| 67 | 51 | my $e; | |||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||
| 69 | # If no args were passed work on $_ | ||||||||||||||||||||||||||||||||||||
| 70 | 51 | ▶ | $ref = \$_ unless @_; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||
| 72 | # slurp-mode bypass | ||||||||||||||||||||||||||||||||||||
| 73 | 51 | ▶ | return $rv unless defined $/; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||
| 75 | 50 | ▶ | if ( ref $ref eq 'SCALAR' and defined $$ref ) { | ||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
Condition66%2/3
ref $ref eq "SCALAR" and defined $$ref
MC/DC50%1/2
ref $ref eq "SCALAR" and defined $$ref
ref $ref eq "SCALAR"
defined $$ref
Truth table66%2/3
A and B
A= ref $ref eq "SCALAR"B= defined $$ref
| |||||||||||||||||||||||||||||||||||||
| 76 | 8 | ▶ | if ( $/ =~ /^$nl$/so ) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 77 | 5 | $e = length $$ref; | |||||||||||||||||||||||||||||||||||
| 78 | 5 | $$ref =~ s/$nl$//so; | |||||||||||||||||||||||||||||||||||
| 79 | 5 | $rv = $e - length $$ref; | |||||||||||||||||||||||||||||||||||
| 80 | } else { | ||||||||||||||||||||||||||||||||||||
| 81 | 3 | $rv = chomp $$ref; | |||||||||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||||||||||
| 83 | } elsif ( ref $ref eq 'ARRAY' ) { | ||||||||||||||||||||||||||||||||||||
| 84 | 41 | ▶ | if ( $/ =~ /^$nl$/so ) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 85 | 41 | foreach (@$ref) { | |||||||||||||||||||||||||||||||||||
| 86 | 1676 | ▶ | next unless defined; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 87 | 1676 | $e = length $_; | |||||||||||||||||||||||||||||||||||
| 88 | 1676 | $_ =~ s/$nl$//so; | |||||||||||||||||||||||||||||||||||
| 89 | 1676 | $rv += $e - length $_; | |||||||||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||||||||||
| 91 | } else { | ||||||||||||||||||||||||||||||||||||
| 92 | 0 | $rv = chomp @$ref; | |||||||||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||||||||||
| 94 | } elsif ( ref $ref eq 'HASH' ) { | ||||||||||||||||||||||||||||||||||||
| 95 | 1 | ▶ | if ( $/ =~ /^$nl$/so ) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 96 | 1 | foreach ( keys %$ref ) { | |||||||||||||||||||||||||||||||||||
| 97 | 4 | ▶ | next unless defined $$ref{$_}; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 98 | 4 | $e = length $$ref{$_}; | |||||||||||||||||||||||||||||||||||
| 99 | 4 | $$ref{$_} =~ s/$nl$//so; | |||||||||||||||||||||||||||||||||||
| 100 | 4 | $rv += $e - length $$ref{$_}; | |||||||||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||||||||||
| 102 | } else { | ||||||||||||||||||||||||||||||||||||
| 103 | 0 | $rv = chomp %$ref; | |||||||||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||
| 107 | 50 | return $rv; | |||||||||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||
| 110 | 64 | our %regexes = ( | |||||||||||||||||||||||||||||||||||
| 111 | alphabetic => qr/[a-z]+/si, | ||||||||||||||||||||||||||||||||||||
| 112 | alphanumeric => qr/[a-z0-9]+/si, | ||||||||||||||||||||||||||||||||||||
| 113 | alphawhite => qr/[a-z\s]+/si, | ||||||||||||||||||||||||||||||||||||
| 114 | alnumwhite => qr/[a-z0-9\s]+/si, | ||||||||||||||||||||||||||||||||||||
| 115 | email => qr/[a-z][\w\.\-]*\@(?:[a-z0-9][a-z0-9\-]*\.)*[a-z0-9]+/si, | ||||||||||||||||||||||||||||||||||||
| 116 | filename => qr#[/ \w\-\.:,@\+]+\[?#s, | ||||||||||||||||||||||||||||||||||||
| 117 | fileglob => qr#[/ \w\-\.:,@\+\*\?\{\}\[\]]+\[?#s, | ||||||||||||||||||||||||||||||||||||
| 118 | hostname => qr#(?:[a-z0-9][a-z0-9\-]*)(?:\.[a-z0-9][a-z0-9\-]*)*\.?#s, | ||||||||||||||||||||||||||||||||||||
| 119 | ipv4addr => | ||||||||||||||||||||||||||||||||||||
| 120 | qr/(?:(?:\d\d?|1\d\d|2[0-4][0-9]|25[0-5])\.){3}(?:\d\d?|1\d\d|2[0-4][0-9]|25[0-5])/s, | ||||||||||||||||||||||||||||||||||||
| 121 | ipv4netaddr => | ||||||||||||||||||||||||||||||||||||
| 122 | qr#(?:(?:\d\d?|1\d\d|2[0-4][0-9]|25[0-5])\.){3}(?:\d\d?|1\d\d|2[0-4][0-9]|25[0-5])/(?:(?:\d|[12]\d|3[0-2])|(?:(?:\d\d?|1\d\d|2[0-4][0-9]|25[0-5])\.){3}(?:\d\d?|1\d\d|2[0-4][0-9]|25[0-5]))#s, | ||||||||||||||||||||||||||||||||||||
| 123 | ipv6addr => qr/ | ||||||||||||||||||||||||||||||||||||
| 124 | :(?::[abcdef\d]{1,4}){1,7} | | ||||||||||||||||||||||||||||||||||||
| 125 | [abcdef\d]{1,4}(?:::?[abcdef\d]{1,4}){1,7} | | ||||||||||||||||||||||||||||||||||||
| 126 | (?:[abcdef\d]{1,4}:){1,7}: | ||||||||||||||||||||||||||||||||||||
| 127 | /six, | ||||||||||||||||||||||||||||||||||||
| 128 | ipv6netaddr => qr#(?::(?::[abcdef\d]{1,4}){1,7}| | ||||||||||||||||||||||||||||||||||||
| 129 | [abcdef\d]{1,4}(?:::?[abcdef\d]{1,4}){1,7} | | ||||||||||||||||||||||||||||||||||||
| 130 | (?:[abcdef\d]{1,4}:){1,7}:)/(?:\d\d?|1(?:[01]\d|2[0-8]))#six, | ||||||||||||||||||||||||||||||||||||
| 131 | login => qr/[a-z][\w\.\-]*/si, | ||||||||||||||||||||||||||||||||||||
| 132 | nometa => qr/[^\%\`\$\!\@]+/s, | ||||||||||||||||||||||||||||||||||||
| 133 | number => qr/[+\-]?[0-9]+(?:\.[0-9]+)?/s, | ||||||||||||||||||||||||||||||||||||
| 134 | 'int' => qr/[-+]?\d+/s, | ||||||||||||||||||||||||||||||||||||
| 135 | uint => qr/\d+/s, | ||||||||||||||||||||||||||||||||||||
| 136 | float => qr/[-+]?\d+(?:\.\d+)/s, | ||||||||||||||||||||||||||||||||||||
| 137 | ufloat => qr/\d+(?:\.\d+)/s, | ||||||||||||||||||||||||||||||||||||
| 138 | bin => qr/[01]+/s, | ||||||||||||||||||||||||||||||||||||
| 139 | octal => qr/[0-7]+/s, | ||||||||||||||||||||||||||||||||||||
| 140 | 'hex' => qr/[a-z0-9]+/si, | ||||||||||||||||||||||||||||||||||||
| 141 | ); | ||||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||||
| 143 | 938 | ▶ | sub detaint (\[$@%]$;\[$@%]) { | ||||||||||||||||||||||||||||||||||
|
Subroutine
detaint: called CC=26 SCAR=32.7
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||
| 145 | # Purpose: Detaints and validates input in one call | ||||||||||||||||||||||||||||||||||||
| 146 | # Returns: True (1) if detainting was successful, | ||||||||||||||||||||||||||||||||||||
| 147 | # False (0) if there are any errors | ||||||||||||||||||||||||||||||||||||
| 148 | # Usage: $rv = detaint($input, $dataType, $detainted); | ||||||||||||||||||||||||||||||||||||
| 149 | # Usage: $rv = detaint(@input, $dataType, @detainted); | ||||||||||||||||||||||||||||||||||||
| 150 | # Usage: $rv = detaint(%input, $dataType, %detainted); | ||||||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||||||
| 152 | 938 | my $iref = shift; | |||||||||||||||||||||||||||||||||||
| 153 | 938 | my $type = shift; | |||||||||||||||||||||||||||||||||||
| 154 | 938 | my $oref = shift; | |||||||||||||||||||||||||||||||||||
| 155 | 938 | my $po = defined $oref; | |||||||||||||||||||||||||||||||||||
| 156 | 938 | my $rv = 0; | |||||||||||||||||||||||||||||||||||
| 157 | 938 | my ( $regex, $tmp ); | |||||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||||||
| 159 | 938 | subPreamble( PDLEVEL1, '\[$@%]$;\[$@%]', $iref, $type, $oref ); | |||||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||||||
| 161 | # Make sure input and output data types match | ||||||||||||||||||||||||||||||||||||
| 162 | 938 | ▶ | croak "$iref and $oref aren't compatible data types" | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not defined $oref or ref $iref eq ref $oref
MC/DC0%0/2
not defined $oref or ref $iref eq ref $oref
not defined $oref
ref $iref eq ref $oref
Truth table66%2/3
not(A) or B
A= not defined $orefB= ref $iref eq ref $oref
| |||||||||||||||||||||||||||||||||||||
| 163 | unless !defined $oref | ||||||||||||||||||||||||||||||||||||
| 164 | or ref $iref eq ref $oref; | ||||||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||||||
| 166 | # Warn on unknown regexes | ||||||||||||||||||||||||||||||||||||
| 167 | 938 | ▶ | if ( ref $type eq 'Regexp' ) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 168 | 1 | $regex = $type; | |||||||||||||||||||||||||||||||||||
| 169 | 1 | $type = 'custom'; | |||||||||||||||||||||||||||||||||||
| 170 | } else { | ||||||||||||||||||||||||||||||||||||
| 171 | 937 | ▶ | if ( defined $type and exists $regexes{$type} ) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $type and exists $Paranoid::Input::regexes{$type}
MC/DC50%1/2
defined $type and exists $Paranoid::Input::regexes{$type}
defined $type
exists $Paranoid::Input::regexes{$type}
Truth table66%2/3
A and B
A= defined $typeB= exists $Paranoid::Input::regexes{$type}
| |||||||||||||||||||||||||||||||||||||
| 172 | 936 | $regex = $regexes{$type}; | |||||||||||||||||||||||||||||||||||
| 173 | } else { | ||||||||||||||||||||||||||||||||||||
| 174 | 1 | pdebug( 'unknown regex type requested: %s', PDLEVEL1, $type ); | |||||||||||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||||||
| 178 | # Create a reference structure under $oref if none was passed | ||||||||||||||||||||||||||||||||||||
| 179 | 938 | ▶ | unless ( defined $oref ) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 180 | 583 | ▶ | $oref = | ||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||
| 181 | ref $iref eq 'ARRAY' ? [] | ||||||||||||||||||||||||||||||||||||
| 182 | : ref $iref eq 'HASH' ? {} | ||||||||||||||||||||||||||||||||||||
| 183 | : \$tmp; | ||||||||||||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||||||
| 186 | # Make sure $oref is empty | ||||||||||||||||||||||||||||||||||||
| 187 | 938 | ▶ | if ( ref $oref eq 'SCALAR' ) { | ||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||
| 188 | 930 | $$oref = undef; | |||||||||||||||||||||||||||||||||||
| 189 | } elsif ( ref $oref eq 'ARRAY' ) { | ||||||||||||||||||||||||||||||||||||
| 190 | 4 | @$oref = (); | |||||||||||||||||||||||||||||||||||
| 191 | } else { | ||||||||||||||||||||||||||||||||||||
| 192 | 4 | %$oref = (); | |||||||||||||||||||||||||||||||||||
| 193 | } | ||||||||||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||||||||||
| 195 | # Start working | ||||||||||||||||||||||||||||||||||||
| 196 | 938 | ▶ | if ( defined $regex ) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 197 | 937 | ▶ | if ( ref $iref eq 'SCALAR' ) { | ||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||
| 198 | 929 | pdebug( 'evaluating (%s)', PDLEVEL2, $$iref ); | |||||||||||||||||||||||||||||||||||
| 199 | 929 | ▶ | ($$oref) = ( $$iref =~ /^($regex)$/s ) | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 200 | if defined $$iref; | ||||||||||||||||||||||||||||||||||||
| 201 | 929 | $rv = defined $$oref; | |||||||||||||||||||||||||||||||||||
| 202 | } elsif ( ref $iref eq 'ARRAY' ) { | ||||||||||||||||||||||||||||||||||||
| 203 | 4 | ▶ | if ( scalar @$iref ) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 204 | 4 | $rv = 1; | |||||||||||||||||||||||||||||||||||
| 205 | 4 | foreach (@$iref) { | |||||||||||||||||||||||||||||||||||
| 206 | 11 | pdebug( 'evaluating (%s)', PDLEVEL2, $_ ); | |||||||||||||||||||||||||||||||||||
| 207 | 11 | ▶ | ( $$oref[ $#{$oref} + 1 ] ) = | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 208 | defined $_ ? m/^($regex)$/s : (undef); | ||||||||||||||||||||||||||||||||||||
| 209 | 11 | ▶ | $rv = 0 unless defined $$oref[-1]; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 210 | 11 | pdebug( 'got (%s)', PDLEVEL2, $$oref[-1] ); | |||||||||||||||||||||||||||||||||||
| 211 | } | ||||||||||||||||||||||||||||||||||||
| 212 | } | ||||||||||||||||||||||||||||||||||||
| 213 | 4 | $rv = !scalar grep { !defined } @$oref; | |||||||||||||||||||||||||||||||||||
| 214 | } else { | ||||||||||||||||||||||||||||||||||||
| 215 | 4 | ▶ | if ( scalar keys %$iref ) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 216 | 4 | $rv = 1; | |||||||||||||||||||||||||||||||||||
| 217 | 4 | foreach ( keys %$iref ) { | |||||||||||||||||||||||||||||||||||
| 218 | 11 | pdebug( 'evaluating (%s)', PDLEVEL2, $$iref{$_} ); | |||||||||||||||||||||||||||||||||||
| 219 | ( $$oref{$_} ) = | ||||||||||||||||||||||||||||||||||||
| 220 | defined $$iref{$_} | ||||||||||||||||||||||||||||||||||||
| 221 | 11 | ▶ | ? ( $$iref{$_} =~ m/^($regex)$/s ) | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 222 | : undef; | ||||||||||||||||||||||||||||||||||||
| 223 | 11 | ▶ | $rv = 0 unless defined $$oref{$_}; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 224 | } | ||||||||||||||||||||||||||||||||||||
| 225 | } | ||||||||||||||||||||||||||||||||||||
| 226 | } | ||||||||||||||||||||||||||||||||||||
| 227 | } | ||||||||||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||||||||||
| 229 | # Copy everything back to $iref if needed | ||||||||||||||||||||||||||||||||||||
| 230 | 938 | ▶ | unless ($po) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 231 | 583 | ▶ | if ( ref $iref eq 'SCALAR' ) { | ||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||
| 232 | 579 | $$iref = $$oref; | |||||||||||||||||||||||||||||||||||
| 233 | } elsif ( ref $iref eq 'ARRAY' ) { | ||||||||||||||||||||||||||||||||||||
| 234 | 2 | @$iref = @$oref; | |||||||||||||||||||||||||||||||||||
| 235 | } else { | ||||||||||||||||||||||||||||||||||||
| 236 | 2 | %$iref = %$oref; | |||||||||||||||||||||||||||||||||||
| 237 | } | ||||||||||||||||||||||||||||||||||||
| 238 | } | ||||||||||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||||||||||
| 240 | 938 | subPostamble( PDLEVEL1, '$', $rv ); | |||||||||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||||||||||
| 242 | 938 | return $rv; | |||||||||||||||||||||||||||||||||||
| 243 | } | ||||||||||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||||||||||
| 245 | 2 | ▶ | sub stringMatch ($@) { | ||||||||||||||||||||||||||||||||||
|
Subroutine
stringMatch: called CC=9 SCAR=22.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||||||||||
| 247 | # Purpose: Looks for occurrences of strings and/or regexes in the passed | ||||||||||||||||||||||||||||||||||||
| 248 | # input | ||||||||||||||||||||||||||||||||||||
| 249 | # Returns: True (1) any of the strings/regexes match, | ||||||||||||||||||||||||||||||||||||
| 250 | # False (0), otherwise | ||||||||||||||||||||||||||||||||||||
| 251 | # Usage: $rv = stringMatch($input, @words); | ||||||||||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||||||||||
| 253 | 2 | my $input = shift; | |||||||||||||||||||||||||||||||||||
| 254 | 2 | my @match = splice @_; | |||||||||||||||||||||||||||||||||||
| 255 | 2 | my $rv = 0; | |||||||||||||||||||||||||||||||||||
| 256 | 2 | my @regex; | |||||||||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||||||||||
| 258 | 2 | subPreamble( PDLEVEL1, '$@', $input, @match ); | |||||||||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||||||||||
| 260 | 2 | ▶ | if ( defined $input and @match ) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $input and @match
MC/DC0%0/2
defined $input and @match
defined $input
@match
Truth table33%1/3
A and B
A= defined $inputB= @match
| |||||||||||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||||||||||
| 262 | # Populate @regex w/regexes | ||||||||||||||||||||||||||||||||||||
| 263 | 2 | ▶ | @regex = grep { defined $_ && ref $_ eq 'Regexp' } @match; | ||||||||||||||||||||||||||||||||||
|
Condition33%1/3
defined $_ && ref $_ eq 'Regexp'
MC/DC0%0/2
defined $_ && ref $_ eq 'Regexp'
defined $_
ref $_ eq 'Regexp'
Truth table33%1/3
A && B
A= defined $_B= ref $_ eq 'Regexp'
| |||||||||||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||||||||||
| 265 | # Convert remaining strings to regexes | ||||||||||||||||||||||||||||||||||||
| 266 | 2 | ▶ | foreach ( grep { defined $_ && ref $_ ne 'Regexp' } @match ) { | ||||||||||||||||||||||||||||||||||
|
Condition33%1/3
defined $_ && ref $_ ne 'Regexp'
MC/DC0%0/2
defined $_ && ref $_ ne 'Regexp'
defined $_
ref $_ ne 'Regexp'
Truth table33%1/3
A && B
A= defined $_B= ref $_ ne 'Regexp'
| |||||||||||||||||||||||||||||||||||||
| 267 | 6 | ▶ | push @regex, m#^/(.+)/$#s ? qr#$1#si : qr#\Q$_\E#si; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 268 | } | ||||||||||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||||||||||
| 270 | # Start comparisons | ||||||||||||||||||||||||||||||||||||
| 271 | 2 | study $input; | |||||||||||||||||||||||||||||||||||
| 272 | 2 | foreach my $r (@regex) { | |||||||||||||||||||||||||||||||||||
| 273 | 3 | ▶ | if ( $input =~ /$r/si ) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 274 | 1 | $rv = 1; | |||||||||||||||||||||||||||||||||||
| 275 | 1 | last; | |||||||||||||||||||||||||||||||||||
| 276 | } | ||||||||||||||||||||||||||||||||||||
| 277 | } | ||||||||||||||||||||||||||||||||||||
| 278 | } | ||||||||||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||||||||||
| 280 | 2 | subPostamble( PDLEVEL1, '$', $rv ); | |||||||||||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||||||||||
| 282 | 2 | return $rv; | |||||||||||||||||||||||||||||||||||
| 283 | } | ||||||||||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||||||||||
| 285 | 64 | 1; | |||||||||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||||||||||
| 287 | __END__ | ||||||||||||||||||||||||||||||||||||