| 1 | package Filter::Heredoc::Rule; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 26 | ▶ | use 5.010; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 26 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 26 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 26 | our $VERSION = '0.05'; | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | =head1 NAME | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | Filter::Heredoc::Rule - Load or unload rules for heredoc processing | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | =head1 VERSION | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | Version 0.05 | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | =cut | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 26 | ▶ | use base qw(Exporter); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | 26 | ▶ | use feature 'state'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | # Private subroutine used in author tests. _hd* is used by Filter::Heredoc | ||||||||||||||||||||||||||||
| 23 | 26 | our @EXPORT_OK = qw ( | |||||||||||||||||||||||||||
| 24 | hd_syntax | ||||||||||||||||||||||||||||
| 25 | _hd_is_rules_ok_line | ||||||||||||||||||||||||||||
| 26 | _is_lonely_redirect | ||||||||||||||||||||||||||||
| 27 | ); | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | ### Export_ok subroutines starts here ### | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | ### INTERFACE SUBROUTINE ### | ||||||||||||||||||||||||||||
| 32 | # Usage : hd_syntax ( $rulename ) or hd_syntax() | ||||||||||||||||||||||||||||
| 33 | # Purpose : Accessor subroutine to get/set the helper rules to use. | ||||||||||||||||||||||||||||
| 34 | # If 'none' is used in $rulename, all existing | ||||||||||||||||||||||||||||
| 35 | # rule is set to the $EMPTY_STR in the hash. | ||||||||||||||||||||||||||||
| 36 | # Limitation : Can not apply multiple rules during one run. | ||||||||||||||||||||||||||||
| 37 | # Returns : Hash of available rule(s). | ||||||||||||||||||||||||||||
| 38 | # Throws : No | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 86 | ▶ | sub hd_syntax { | ||||||||||||||||||||||||||
|
Subroutine
hd_syntax: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||
| 41 | 86 | my $language = shift; | |||||||||||||||||||||||||||
| 42 | 86 | my $EMPTY_STR = q{}; | |||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | # Default to no rules | ||||||||||||||||||||||||||||
| 45 | 86 | state $pod = $EMPTY_STR; | |||||||||||||||||||||||||||
| 46 | 86 | my %syntax = ( pod => $pod, ); | |||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | # Sets a new language (rule) | ||||||||||||||||||||||||||||
| 49 | 86 | ▶ | if ( defined $language ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | 26 | my $POD = q{pod}; | |||||||||||||||||||||||||||
| 51 | 26 | my $NONE = q{none}; | |||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 26 | chomp $language; | |||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | # Reset all rules with 'none' keyword, ignore case | ||||||||||||||||||||||||||||
| 56 | 26 | $language = lc($language); | |||||||||||||||||||||||||||
| 57 | 26 | ▶ | if ( $language eq $NONE ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 58 | 9 | $syntax{pod} = $EMPTY_STR; | |||||||||||||||||||||||||||
| 59 | 9 | $pod = $EMPTY_STR; # update persistent variable | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | # Set one of the defined rules | ||||||||||||||||||||||||||||
| 63 | elsif ( exists( $syntax{$language} ) ) { | ||||||||||||||||||||||||||||
| 64 | 13 | ▶ | if ( $language eq $POD ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | 13 | $syntax{pod} = $POD; | |||||||||||||||||||||||||||
| 66 | 13 | $pod = $POD; # update persistent variable | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | } # end language rule defined | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | # The existing rule (possible changed) | ||||||||||||||||||||||||||||
| 73 | 86 | return %syntax; | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | ### INTERNAL (Filter::Heredoc only) INTERFACE SUBROUTINE ### | ||||||||||||||||||||||||||||
| 78 | # Usage : _hd_is_rules_ok_line ( $line ) | ||||||||||||||||||||||||||||
| 79 | # Purpose : Test if $line should be trusted compared to any set | ||||||||||||||||||||||||||||
| 80 | # rules, i.e. should not initiate an ingress/egress change. | ||||||||||||||||||||||||||||
| 81 | # Returns : Returns "False" if line is "false positive" - i.e. not ok. | ||||||||||||||||||||||||||||
| 82 | # Throws : No | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 56 | ▶ | sub _hd_is_rules_ok_line { | ||||||||||||||||||||||||||
|
Subroutine
_hd_is_rules_ok_line: called CC=5 SCAR=16.3
| |||||||||||||||||||||||||||||
| 85 | 56 | my $line = shift; | |||||||||||||||||||||||||||
| 86 | 56 | my $EMPTY_STR = q{}; | |||||||||||||||||||||||||||
| 87 | 56 | my $NONE = q{none}; | |||||||||||||||||||||||||||
| 88 | 56 | my $POD = q{pod}; | |||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 56 | my %syntax = hd_syntax(); | |||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | # Line is to be trusted (to 'none rules') | ||||||||||||||||||||||||||||
| 93 | 56 | ▶ | if ( $syntax{pod} eq $EMPTY_STR ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 94 | 50 | return 1; | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | # Apply pod rules | ||||||||||||||||||||||||||||
| 98 | elsif ( $syntax{pod} eq $POD ) { | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | # 'False line', '<<' and '>>' on line | ||||||||||||||||||||||||||||
| 101 | 6 | ▶ | if ( _is_redirector_pair($line) ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 102 | 2 | return $EMPTY_STR; | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | # 'False line', empty '<<' line | ||||||||||||||||||||||||||||
| 106 | elsif ( _is_lonely_redirect($line) ) { | ||||||||||||||||||||||||||||
| 107 | 4 | return $EMPTY_STR; | |||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 0 | return 1; # Default - line is ok if reaching down here | |||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | ### The Module private subroutines starts here ### | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | ### INTERNAL UTILITY ### | ||||||||||||||||||||||||||||
| 120 | # Usage : _is_lonely_redirect( $line ) | ||||||||||||||||||||||||||||
| 121 | # Purpose : Bugfix DBNX#1: Prevent a false ingress change | ||||||||||||||||||||||||||||
| 122 | # when line is 'cat <<' or 'cat <<-' | ||||||||||||||||||||||||||||
| 123 | # Returns : True (1) if redirector is lonely, otherwise False. | ||||||||||||||||||||||||||||
| 124 | # Throws : No | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 4 | ▶ | sub _is_lonely_redirect { | ||||||||||||||||||||||||||
|
Subroutine
_is_lonely_redirect: called CC=4 SCAR=18.6
| |||||||||||||||||||||||||||||
| 127 | 4 | my $EMPTY_STR = q{}; | |||||||||||||||||||||||||||
| 128 | 4 | my $line; | |||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 4 | ▶ | if ( !defined( $line = shift ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 131 | 0 | return $EMPTY_STR; | |||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | # lonely '<<' with no characters on line after it | ||||||||||||||||||||||||||||
| 135 | 4 | ▶ | if ( $line =~ m/(<<)$/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 136 | 4 | return 1; | |||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | # lonely '<<-' with no characters on line after it | ||||||||||||||||||||||||||||
| 140 | 0 | ▶ | if ( $line =~ m/(-)$/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 141 | 0 | return 1; | |||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 0 | return $EMPTY_STR; # It's not a lonely redirect (return false) | |||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | ### INTERNAL UTILITY ### | ||||||||||||||||||||||||||||
| 148 | # Usage : _is_redirector_pair( $line ) | ||||||||||||||||||||||||||||
| 149 | # Purpose : Bugfix DBNX#16: Prevent a false ingress change | ||||||||||||||||||||||||||||
| 150 | # when line is '<<' and '>>' typical POD | ||||||||||||||||||||||||||||
| 151 | # Returns : True (1) if pair is found, otherwise False. | ||||||||||||||||||||||||||||
| 152 | # Throws : No | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | 6 | ▶ | sub _is_redirector_pair { | ||||||||||||||||||||||||||
|
Subroutine
_is_redirector_pair: called CC=4 SCAR=14.2
| |||||||||||||||||||||||||||||
| 155 | 6 | my $EMPTY_STR = q{}; | |||||||||||||||||||||||||||
| 156 | 6 | my $line; | |||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 6 | ▶ | if ( !defined( $line = shift ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 159 | 0 | return $EMPTY_STR; | |||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | # POD use matching << and >> but not any here document syntax | ||||||||||||||||||||||||||||
| 163 | 6 | ▶ | if ( ( $line =~ m/<</ ) && ( $line =~ m/>>/ ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$line =~ /<</ and $line =~ />>/
MC/DC50%1/2
$line =~ /<</ and $line =~ />>/
$line =~ /<</
$line =~ />>/
Truth table66%2/3
A and B
A= $line =~ /<</B= $line =~ />>/
| |||||||||||||||||||||||||||||
| 164 | 2 | return 1; | |||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | 4 | return $EMPTY_STR; # It's not a POD pair (return false) | |||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | use 5.010; | ||||||||||||||||||||||||||||
| 173 | use Filter::Heredoc::Rule qw( hd_syntax ); | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | # load the 'pod' rule (i.e. activate it) | ||||||||||||||||||||||||||||
| 176 | my %rule = hd_syntax( q{pod} ); | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | # print capability and status | ||||||||||||||||||||||||||||
| 179 | foreach ( keys %rule ) { | ||||||||||||||||||||||||||||
| 180 | print "'$_' '$rule{$_}'\n"; | ||||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | # unload all with keyword 'none'; | ||||||||||||||||||||||||||||
| 184 | hd_syntax( 'none' ); | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | Support here document parsing with rules to prevent "false positives". | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | =head1 SUBROUTINES | ||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | I<Filter::Heredoc::Rule> exports following subroutine only on request. | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | hd_syntax # load/unload a script syntax rule | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | =head2 B<hd_syntax> | ||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | %rule = hd_syntax( $rulename ); | ||||||||||||||||||||||||||||
| 199 | %rule = hd_syntax(); | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | Load or unload the syntax rule to use when parsing a here document. | ||||||||||||||||||||||||||||
| 202 | This subroutine use a key/value hash to hold capability and status. | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | %rule = ( | ||||||||||||||||||||||||||||
| 205 | pod => 'pod', | ||||||||||||||||||||||||||||
| 206 | ) | ||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | The rule capability is given by the hash key. To load a rule, use the | ||||||||||||||||||||||||||||
| 209 | key as the value in the argument. A rule is deactivated if the value is | ||||||||||||||||||||||||||||
| 210 | equal to an $EMPTY_STR (q{}). Supplying the special word 'none' (not | ||||||||||||||||||||||||||||
| 211 | case sensitive) deactivates all rules. Only one rule (i.e. 'pod') exists | ||||||||||||||||||||||||||||
| 212 | in this release. Returns the hash with key/values. | ||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | =head1 ERRORS | ||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | No messages are printed from any subroutine. | ||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | =head1 BUGS AND LIMITATIONS | ||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | This version only supports one rule. | ||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||
| 222 | Please report any bugs or feature requests to | ||||||||||||||||||||||||||||
| 223 | L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Filter-Heredoc> or at | ||||||||||||||||||||||||||||
| 224 | C<< <bug-filter-heredoc at rt.cpan.org> >>. | ||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | L<Filter::Heredoc>, L<filter-heredoc> | ||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||
| 230 | =head1 LICENSE AND COPYRIGHT | ||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | Copyright 2011-18, Bertil Kronlund | ||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||
| 234 | This program is free software; you can redistribute it and/or modify it | ||||||||||||||||||||||||||||
| 235 | under the terms of either: the GNU General Public License as published | ||||||||||||||||||||||||||||
| 236 | by the Free Software Foundation; or the Artistic License. | ||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||
| 238 | See http://dev.perl.org/licenses/ for more information. | ||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||
| 240 | =cut | ||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||
| 242 | 26 | 1; # End of Filter::Heredoc::Rule | |||||||||||||||||||||||||||