| 1 | # Refactor.pm - refactor Perl code. | ||||||||||||||||||||||||||||
| 2 | # $Header: $ | ||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||
| 4 | ############################################################################### | ||||||||||||||||||||||||||||
| 5 | =head1 NAME | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | Devel::Refactor - Perl extension for refactoring Perl code. | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | =head1 VERSION | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | $Revision: $ This is the CVS revision number. | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | use Devel::Refactor; | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | my $refactory = Devel::Refactor->new; | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | my ($new_sub_call,$new_sub_code) = | ||||||||||||||||||||||||||||
| 20 | $refactory->extract_subroutine($sub_name, $code_snippet); | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | my $files_to_change = $refactory->rename_subroutine('./path/to/dir', | ||||||||||||||||||||||||||||
| 23 | 'oldSubName','newSubName'); | ||||||||||||||||||||||||||||
| 24 | # $files_to_change is a hashref where keys are file names, and values are | ||||||||||||||||||||||||||||
| 25 | # arrays of hashes with line_number => new_text | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | =head1 ABSTRACT | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | Perl module that facilitates refactoring Perl code. | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | The B<Devel::Refactor> module is for code refactoring. | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | While B<Devel::Refactor> may be used from Perl programs, it is also designed to be | ||||||||||||||||||||||||||||
| 36 | used with the B<EPIC> plug-in for the B<eclipse> integrated development environment. | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | =cut | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | package Devel::Refactor; | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 6 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 43 | 6 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 44 | 6 | ▶ | use Cwd; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 45 | 6 | ▶ | use File::Basename; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 6 | require Exporter; | |||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 6 | our @ISA = qw(Exporter); | |||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | # Items to export into callers namespace by default. Note: do not export | ||||||||||||||||||||||||||||
| 52 | # names by default without a very good reason. Use EXPORT_OK instead. | ||||||||||||||||||||||||||||
| 53 | # Do not simply export all your public functions/methods/constants. | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | # This allows declaration use Dev::Refactor ':all'; | ||||||||||||||||||||||||||||
| 56 | # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK | ||||||||||||||||||||||||||||
| 57 | # will save memory. | ||||||||||||||||||||||||||||
| 58 | 6 | our %EXPORT_TAGS = ( 'all' => [ qw( | |||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | ) ] ); | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 6 | our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); | |||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 6 | our @EXPORT = qw( | |||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | ); | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 6 | our $VERSION = '0.05'; | |||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 6 | our $DEBUG = 0; | |||||||||||||||||||||||||||
| 71 | # Preloaded methods go here. | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 6 | our %perl_file_extensions = ( | |||||||||||||||||||||||||||
| 75 | '\.pl$' => 1, | ||||||||||||||||||||||||||||
| 76 | '\.pm$' => 1, | ||||||||||||||||||||||||||||
| 77 | '\.pod$' => 1, | ||||||||||||||||||||||||||||
| 78 | ); | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | =head1 CLASS METHODS | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | Just the constructor for now. | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | =head2 new | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | Returns a new B<Devel::Refactor> object. | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | =cut | ||||||||||||||||||||||||||||
| 89 | # TODO: List the object properties that are initialized. | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 6 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 92 | 6 | my $class = shift; | |||||||||||||||||||||||||||
| 93 | 6 | $DEBUG = shift; | |||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | # TODO: Should these really be object properties? No harm I guess, but most | ||||||||||||||||||||||||||||
| 96 | # of them are for the extract_subroutine method, and so maybe they | ||||||||||||||||||||||||||||
| 97 | # should go in a data structure dedicated to that method? | ||||||||||||||||||||||||||||
| 98 | 6 | my $self = { | |||||||||||||||||||||||||||
| 99 | sub_name => '', | ||||||||||||||||||||||||||||
| 100 | code_snippet => '', | ||||||||||||||||||||||||||||
| 101 | return_snippet => '', | ||||||||||||||||||||||||||||
| 102 | return_sub_call => '', | ||||||||||||||||||||||||||||
| 103 | eval_err => '', | ||||||||||||||||||||||||||||
| 104 | scalar_vars => {}, | ||||||||||||||||||||||||||||
| 105 | array_vars => {}, | ||||||||||||||||||||||||||||
| 106 | hash_vars => {}, | ||||||||||||||||||||||||||||
| 107 | local_scalars => {}, | ||||||||||||||||||||||||||||
| 108 | loop_scalars => {}, | ||||||||||||||||||||||||||||
| 109 | local_arrays => {}, | ||||||||||||||||||||||||||||
| 110 | local_hashes => {}, | ||||||||||||||||||||||||||||
| 111 | parms => [], | ||||||||||||||||||||||||||||
| 112 | inner_retvals => [], | ||||||||||||||||||||||||||||
| 113 | outer_retvals => [], | ||||||||||||||||||||||||||||
| 114 | perl_file_extensions => { %perl_file_extensions }, | ||||||||||||||||||||||||||||
| 115 | }; | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 6 | bless $self, $class; | |||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 6 | return $self; | |||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | =head1 PUBLIC OBJECT METHODS | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | Call on a object returned by new(). | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | =head2 extract_subroutine($new_name,$old_code [,$syntax_check]) | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | Pass it a snippet of Perl code that belongs in its own subroutine as | ||||||||||||||||||||||||||||
| 131 | well as a name for that sub. It figures out which variables | ||||||||||||||||||||||||||||
| 132 | need to be passed into the sub, and which variables might be | ||||||||||||||||||||||||||||
| 133 | passed back. It then produces the sub along with a call to | ||||||||||||||||||||||||||||
| 134 | the sub. | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | Hashes and arrays within the code snippet are converted to | ||||||||||||||||||||||||||||
| 137 | hashrefs and arrayrefs. | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | If the I<syntax_check> argument is true then a sytax check is performed | ||||||||||||||||||||||||||||
| 140 | on the refactored code. | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | Example: | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | $new_name = 'newSub'; | ||||||||||||||||||||||||||||
| 145 | $old_code = <<'eos'; | ||||||||||||||||||||||||||||
| 146 | my @results; | ||||||||||||||||||||||||||||
| 147 | my %hash; | ||||||||||||||||||||||||||||
| 148 | my $date = localtime; | ||||||||||||||||||||||||||||
| 149 | $hash{foo} = 'value 1'; | ||||||||||||||||||||||||||||
| 150 | $hash{bar} = 'value 2'; | ||||||||||||||||||||||||||||
| 151 | for my $loopvar (@array) { | ||||||||||||||||||||||||||||
| 152 | print "Checking $loopvar\n"; | ||||||||||||||||||||||||||||
| 153 | push @results, $hash{$loopvar} || ''; | ||||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||
| 155 | eos | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | ($new_sub_call,$new_code) = $refactory->extract_subroutine($new_name,$old_code); | ||||||||||||||||||||||||||||
| 158 | # $new_sub_call is 'my ($date, $hash, $results) = newSub (\@array);' | ||||||||||||||||||||||||||||
| 159 | # $new_code is | ||||||||||||||||||||||||||||
| 160 | # sub newSub { | ||||||||||||||||||||||||||||
| 161 | # my $array = shift; | ||||||||||||||||||||||||||||
| 162 | # | ||||||||||||||||||||||||||||
| 163 | # my @results; | ||||||||||||||||||||||||||||
| 164 | # my %hash; | ||||||||||||||||||||||||||||
| 165 | # my $date = localtime; | ||||||||||||||||||||||||||||
| 166 | # $hash{foo} = 'value 1'; | ||||||||||||||||||||||||||||
| 167 | # $hash{bar} = 'value 2'; | ||||||||||||||||||||||||||||
| 168 | # for my $loopvar (@$array) { | ||||||||||||||||||||||||||||
| 169 | # print "Checking $loopvar\n"; | ||||||||||||||||||||||||||||
| 170 | # push @results, $hash{$loopvar} || ''; | ||||||||||||||||||||||||||||
| 171 | # } | ||||||||||||||||||||||||||||
| 172 | # | ||||||||||||||||||||||||||||
| 173 | # | ||||||||||||||||||||||||||||
| 174 | # return ($date, \%hash, \@results); | ||||||||||||||||||||||||||||
| 175 | # } | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | Included in the examples directory is a script for use in KDE | ||||||||||||||||||||||||||||
| 179 | under Linux. The script gets its code snippet from the KDE | ||||||||||||||||||||||||||||
| 180 | clipboard and returns the transformed code the same way. The | ||||||||||||||||||||||||||||
| 181 | new sub name is prompted for via STDIN. | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | =cut | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | 2 | ▶ | sub extract_subroutine { | ||||||||||||||||||||||||||
|
Subroutine
extract_subroutine: called CC=4 SCAR=14.2
Pod
documented
| |||||||||||||||||||||||||||||
| 186 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 187 | 2 | my $sub_name = shift; | |||||||||||||||||||||||||||
| 188 | 2 | my $code_snippet = shift; | |||||||||||||||||||||||||||
| 189 | 2 | my $syntax_check = shift; | |||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | 2 | ▶ | $DEBUG and print STDERR "sub name : $sub_name\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 192 | 2 | ▶ | $DEBUG and print STDERR "snippet : $code_snippet\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 193 | 2 | $self->{sub_name} = $sub_name; | |||||||||||||||||||||||||||
| 194 | 2 | $self->{code_snippet} = $code_snippet; | |||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | 2 | $self->_parse_vars(); | |||||||||||||||||||||||||||
| 197 | 2 | $self->_parse_local_vars(); | |||||||||||||||||||||||||||
| 198 | 2 | $self->_transform_snippet(); | |||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | 2 | ▶ | if ($syntax_check) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 201 | 0 | $self->_syntax_check(); | |||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||
| 203 | 2 | return ( @$self{'return_sub_call','return_snippet'} ); | |||||||||||||||||||||||||||
| 204 | } | ||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | =head2 rename_subroutine($where,$old_name,$new_name,[$max_depth]) | ||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | I<where> is one of: | ||||||||||||||||||||||||||||
| 209 | path-to-file | ||||||||||||||||||||||||||||
| 210 | path-to-directory | ||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | If I<where> is a directory then all Perl files (default is C<.pl>, C<.pm>, | ||||||||||||||||||||||||||||
| 213 | and C<.pod> See the B<perl_file_extensions> method.) in that directory and its' | ||||||||||||||||||||||||||||
| 214 | descendents (to I<max_depth> deep,) are searched. | ||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | Default for I<max_depth> is 0 -- just the directory itself; | ||||||||||||||||||||||||||||
| 217 | I<max_depth> of 1 means the specified directory, and it's | ||||||||||||||||||||||||||||
| 218 | immeadiate sub-directories; I<max_depth> of 2 means the specified directory, | ||||||||||||||||||||||||||||
| 219 | it's sub-directories, and their sub-directrories, and so forth. | ||||||||||||||||||||||||||||
| 220 | If you want to scan very deep, use a high number like 99. | ||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||
| 222 | If no matches are found then returns I<undef>, otherwise: | ||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||
| 224 | Returns a hashref that tells you which files you might want to change, | ||||||||||||||||||||||||||||
| 225 | and for each file gives you the line numbers and proposed new text for that line. | ||||||||||||||||||||||||||||
| 226 | The hashref looks like this, where I<old_name> | ||||||||||||||||||||||||||||
| 227 | was found on two lines in the first file and on one line in the second file: | ||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||
| 229 | { | ||||||||||||||||||||||||||||
| 230 | ./path/to/file1.pl => [ | ||||||||||||||||||||||||||||
| 231 | { 11 => "if (myClass->newName($x)) {\n" }, | ||||||||||||||||||||||||||||
| 232 | { 27 => "my $result = myClass->newName($foo);\n"}, | ||||||||||||||||||||||||||||
| 233 | ], | ||||||||||||||||||||||||||||
| 234 | ./path/to/file2.pm => [ | ||||||||||||||||||||||||||||
| 235 | { 235 => "sub newName {\n"}, | ||||||||||||||||||||||||||||
| 236 | ], | ||||||||||||||||||||||||||||
| 237 | } | ||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||
| 239 | The keys are paths to individual files. The values are arraryrefs | ||||||||||||||||||||||||||||
| 240 | containing hashrefs where the keys are the line numbers where I<old_name> | ||||||||||||||||||||||||||||
| 241 | was found and the values are the proposed | ||||||||||||||||||||||||||||
| 242 | new line, with I<old_name> changed to I<new_name>. | ||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||
| 244 | =cut | ||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||
| 246 | 6 | ▶ | sub rename_subroutine { | ||||||||||||||||||||||||||
|
Subroutine
rename_subroutine: called CC=8 SCAR=22.0
Pod
documented
| |||||||||||||||||||||||||||||
| 247 | 6 | my $self = shift; | |||||||||||||||||||||||||||
| 248 | 6 | my $where = shift; | |||||||||||||||||||||||||||
| 249 | 6 | my $old_name = shift; | |||||||||||||||||||||||||||
| 250 | 6 | my $new_name = shift; | |||||||||||||||||||||||||||
| 251 | 6 | ▶ | my $max_depth = shift || 0; # How many level to descend into directories | ||||||||||||||||||||||||||
|
Condition100%2/2
shift() || 0
MC/DC100%1/1
shift() || 0
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||
| 253 | 6 | ▶ | return undef unless ($new_name and $old_name); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$new_name and $old_name
MC/DC0%0/2
$new_name and $old_name
$new_name
$old_name
Truth table33%1/3
A and B
A= $new_nameB= $old_name
| |||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||
| 255 | 6 | ▶ | $DEBUG and warn "Looking for $where in ", getcwd(), "\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 256 | 6 | my $found = {}; # hashref of file names | |||||||||||||||||||||||||||
| 257 | 6 | ▶ | if (-f $where){ | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 258 | # it's a file or filehandle | ||||||||||||||||||||||||||||
| 259 | 2 | $found->{$where} = $self->_scan_file_for_string ($old_name,$new_name,$where); | |||||||||||||||||||||||||||
| 260 | } elsif ( -d $where ) { | ||||||||||||||||||||||||||||
| 261 | # it's a directory or directory handle | ||||||||||||||||||||||||||||
| 262 | 4 | $self->_scan_directory_for_string($old_name,$new_name,$where,$found,$max_depth); | |||||||||||||||||||||||||||
| 263 | } else { | ||||||||||||||||||||||||||||
| 264 | # uh oh. Should we allow it to be a code snippet? | ||||||||||||||||||||||||||||
| 265 | 0 | die "'$where' does not appear to be a file nor a directory." | |||||||||||||||||||||||||||
| 266 | } | ||||||||||||||||||||||||||||
| 267 | 6 | ▶ | return %$found ? $found : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 268 | } | ||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||
| 270 | =head2 is_perlfile($filename) | ||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||
| 272 | Takes a filename or path and returns true if the file has one of the | ||||||||||||||||||||||||||||
| 273 | extensions in B<perl_file_extensions>, otherwise returns false. | ||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||
| 275 | =cut | ||||||||||||||||||||||||||||
| 276 | 34 | ▶ | sub is_perlfile { | ||||||||||||||||||||||||||
|
Subroutine
is_perlfile: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 277 | 34 | my ($self,$filename) = @_; | |||||||||||||||||||||||||||
| 278 | 34 | my ($name,$path,$suffix) = fileparse($filename,keys %{$self->perl_file_extensions}); | |||||||||||||||||||||||||||
| 279 | 34 | return $suffix; | |||||||||||||||||||||||||||
| 280 | } | ||||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||
| 282 | =head1 OBJECT ACCESSORS | ||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||
| 284 | These object methods return various data structures that may be stored | ||||||||||||||||||||||||||||
| 285 | in a B<Devel::Refactor> object. In some cases the method also allows | ||||||||||||||||||||||||||||
| 286 | setting the property, e.g. B<perl_file_extensions>. | ||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||
| 288 | =cut | ||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||
| 290 | =head2 get_new_code | ||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||
| 292 | Returns the I<return_snippet> object property. | ||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||
| 294 | =cut | ||||||||||||||||||||||||||||
| 295 | 0 | ▶ | sub get_new_code{ | ||||||||||||||||||||||||||
|
Subroutine
get_new_code: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 296 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||
| 298 | 0 | return $self->{return_snippet}; | |||||||||||||||||||||||||||
| 299 | } | ||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||
| 301 | =head2 get_eval_results | ||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||
| 303 | Returns the I<eval_err> object property. | ||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||
| 305 | =cut | ||||||||||||||||||||||||||||
| 306 | 0 | ▶ | sub get_eval_results{ | ||||||||||||||||||||||||||
|
Subroutine
get_eval_results: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 307 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||
| 309 | 0 | return $self->{eval_err}; | |||||||||||||||||||||||||||
| 310 | } | ||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||
| 313 | =head2 get_sub_call | ||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||
| 315 | Returns the I<return_sub_call> object property. | ||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||
| 317 | =cut | ||||||||||||||||||||||||||||
| 318 | 0 | ▶ | sub get_sub_call{ | ||||||||||||||||||||||||||
|
Subroutine
get_sub_call: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 319 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||
| 321 | 0 | return $self->{return_sub_call}; | |||||||||||||||||||||||||||
| 322 | } | ||||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||
| 325 | =head2 get_scalars | ||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||
| 327 | Returns an array of the keys from I<scalar_vars> object property. | ||||||||||||||||||||||||||||
| 328 | =cut | ||||||||||||||||||||||||||||
| 329 | 0 | ▶ | sub get_scalars { | ||||||||||||||||||||||||||
|
Subroutine
get_scalars: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 330 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||
| 332 | 0 | return sort keys %{ $self->{scalar_vars} }; | |||||||||||||||||||||||||||
| 333 | } | ||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||
| 335 | =head2 get_arrays | ||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||
| 337 | Returns an array of the keys from the I<array_vars> object property. | ||||||||||||||||||||||||||||
| 338 | =cut | ||||||||||||||||||||||||||||
| 339 | 0 | ▶ | sub get_arrays { | ||||||||||||||||||||||||||
|
Subroutine
get_arrays: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 340 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||
| 342 | 0 | return sort keys %{ $self->{array_vars} }; | |||||||||||||||||||||||||||
| 343 | } | ||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||
| 345 | =head2 get_hashes | ||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||
| 347 | Returns an array of the keys from the I<hash_vars> object property. | ||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||
| 349 | =cut | ||||||||||||||||||||||||||||
| 350 | 0 | ▶ | sub get_hashes { | ||||||||||||||||||||||||||
|
Subroutine
get_hashes: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 351 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||
| 353 | 0 | return sort keys %{ $self->{hash_vars} }; | |||||||||||||||||||||||||||
| 354 | } | ||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||
| 356 | =head2 get_local_scalars | ||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||
| 358 | Returns an array of the keys from the I<local_scalars> object property. | ||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||
| 360 | =cut | ||||||||||||||||||||||||||||
| 361 | 0 | ▶ | sub get_local_scalars { | ||||||||||||||||||||||||||
|
Subroutine
get_local_scalars: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 362 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 363 | |||||||||||||||||||||||||||||
| 364 | 0 | return sort keys %{ $self->{local_scalars} }; | |||||||||||||||||||||||||||
| 365 | } | ||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||
| 367 | =head2 get_local_arrays | ||||||||||||||||||||||||||||
| 368 | |||||||||||||||||||||||||||||
| 369 | Returns an array of the keys from the I<local_arrays> object property. | ||||||||||||||||||||||||||||
| 370 | |||||||||||||||||||||||||||||
| 371 | =cut | ||||||||||||||||||||||||||||
| 372 | 0 | ▶ | sub get_local_arrays { | ||||||||||||||||||||||||||
|
Subroutine
get_local_arrays: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 373 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 374 | |||||||||||||||||||||||||||||
| 375 | 0 | return sort keys %{ $self->{local_arrays} }; | |||||||||||||||||||||||||||
| 376 | } | ||||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||
| 378 | =head2 get_local_hashes | ||||||||||||||||||||||||||||
| 379 | |||||||||||||||||||||||||||||
| 380 | Returns an array of the keys from the I<local_hashes> object property. | ||||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||
| 382 | =cut | ||||||||||||||||||||||||||||
| 383 | |||||||||||||||||||||||||||||
| 384 | 0 | ▶ | sub get_local_hashes { | ||||||||||||||||||||||||||
|
Subroutine
get_local_hashes: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 385 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 386 | |||||||||||||||||||||||||||||
| 387 | 0 | return sort keys %{ $self->{local_hashes} }; | |||||||||||||||||||||||||||
| 388 | } | ||||||||||||||||||||||||||||
| 389 | |||||||||||||||||||||||||||||
| 390 | =head2 perl_file_extensions([$arrayref|$hashref]) | ||||||||||||||||||||||||||||
| 391 | |||||||||||||||||||||||||||||
| 392 | Returns a hashref where the keys are regular expressions that match filename | ||||||||||||||||||||||||||||
| 393 | extensions that we think are for Perl files. Default are C<.pl>, | ||||||||||||||||||||||||||||
| 394 | C<.pm>, and C<.pod> | ||||||||||||||||||||||||||||
| 395 | |||||||||||||||||||||||||||||
| 396 | If passed a hashref then it replaces the current values for this object. The | ||||||||||||||||||||||||||||
| 397 | keys should be regular expressions, e.g. C<\.cgi$>. | ||||||||||||||||||||||||||||
| 398 | |||||||||||||||||||||||||||||
| 399 | If passed an arrayref then the list of values are added as valid Perl | ||||||||||||||||||||||||||||
| 400 | filename extensions. The list should be filename extensions, NOT regular expressions, | ||||||||||||||||||||||||||||
| 401 | For example: | ||||||||||||||||||||||||||||
| 402 | |||||||||||||||||||||||||||||
| 403 | my @additonal_filetypes = qw( .ipl .cgi ); | ||||||||||||||||||||||||||||
| 404 | my $new_hash = $refactory->perl_file_extensions(\@additional_filetypes); | ||||||||||||||||||||||||||||
| 405 | # $new_hash = { | ||||||||||||||||||||||||||||
| 406 | # '\.pl$' => 1, | ||||||||||||||||||||||||||||
| 407 | # '\.pm$' => 1, | ||||||||||||||||||||||||||||
| 408 | # '\.pod$' => 1, | ||||||||||||||||||||||||||||
| 409 | # '\.ipl$' => 1, | ||||||||||||||||||||||||||||
| 410 | # '\.cgi$' => 1, | ||||||||||||||||||||||||||||
| 411 | # '\.t$' => 1, | ||||||||||||||||||||||||||||
| 412 | # } | ||||||||||||||||||||||||||||
| 413 | |||||||||||||||||||||||||||||
| 414 | =cut | ||||||||||||||||||||||||||||
| 415 | |||||||||||||||||||||||||||||
| 416 | 36 | ▶ | sub perl_file_extensions { | ||||||||||||||||||||||||||
|
Subroutine
perl_file_extensions: called CC=3 SCAR=11.3
Pod
documented
| |||||||||||||||||||||||||||||
| 417 | 36 | my($self,$args) = @_; | |||||||||||||||||||||||||||
| 418 | 36 | ▶ | if (ref $args eq 'HASH') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 419 | 0 | $self->{perl_file_extensions} = $args; | |||||||||||||||||||||||||||
| 420 | } elsif (ref $args eq 'ARRAY') { | ||||||||||||||||||||||||||||
| 421 | 2 | map $self->{perl_file_extensions}->{"\\$_\$"} = 1 , @$args; | |||||||||||||||||||||||||||
| 422 | } | ||||||||||||||||||||||||||||
| 423 | 36 | return $self->{perl_file_extensions}; | |||||||||||||||||||||||||||
| 424 | } | ||||||||||||||||||||||||||||
| 425 | |||||||||||||||||||||||||||||
| 426 | |||||||||||||||||||||||||||||
| 427 | =head1 TODO LIST | ||||||||||||||||||||||||||||
| 428 | |||||||||||||||||||||||||||||
| 429 | =over 2 | ||||||||||||||||||||||||||||
| 430 | |||||||||||||||||||||||||||||
| 431 | =item Come up with a more uniform approach to B<ACCESSORS>. | ||||||||||||||||||||||||||||
| 432 | |||||||||||||||||||||||||||||
| 433 | =item Add more refactoring features, such as I<add_parameter>. | ||||||||||||||||||||||||||||
| 434 | |||||||||||||||||||||||||||||
| 435 | =item Add a SEE ALSO section with URLs for eclipse/EPIC, refactoring.com, etc. | ||||||||||||||||||||||||||||
| 436 | |||||||||||||||||||||||||||||
| 437 | =back | ||||||||||||||||||||||||||||
| 438 | |||||||||||||||||||||||||||||
| 439 | =cut | ||||||||||||||||||||||||||||
| 440 | |||||||||||||||||||||||||||||
| 441 | ################################################################################### | ||||||||||||||||||||||||||||
| 442 | ############################## Utility Methods #################################### | ||||||||||||||||||||||||||||
| 443 | |||||||||||||||||||||||||||||
| 444 | 2 | ▶ | sub _parse_vars { | ||||||||||||||||||||||||||
|
Subroutine
_parse_vars: called CC=6 SCAR=18.5
| |||||||||||||||||||||||||||||
| 445 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 446 | |||||||||||||||||||||||||||||
| 447 | 2 | my $var; | |||||||||||||||||||||||||||
| 448 | my $hint; | ||||||||||||||||||||||||||||
| 449 | |||||||||||||||||||||||||||||
| 450 | # find the variables | ||||||||||||||||||||||||||||
| 451 | 2 | ▶ | while ( $self->{code_snippet} =~ /([\$\@]\w+?)(\W\W)/g ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 452 | |||||||||||||||||||||||||||||
| 453 | 12 | $var = $1; | |||||||||||||||||||||||||||
| 454 | 12 | $hint = $2; | |||||||||||||||||||||||||||
| 455 | 12 | ▶ | if ( $hint =~ /^{/ ) { #}/ | ||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||
| 456 | 2 | $var =~ s/\$/\%/; | |||||||||||||||||||||||||||
| 457 | 2 | $self->{hash_vars}->{$var}++; | |||||||||||||||||||||||||||
| 458 | } elsif ( $hint =~ /^\[>/ ) { | ||||||||||||||||||||||||||||
| 459 | 0 | $var =~ s/\$/\@/; | |||||||||||||||||||||||||||
| 460 | 0 | $self->{array_vars}->{$var}++; | |||||||||||||||||||||||||||
| 461 | } elsif ( $var =~ /^\@/ ){ | ||||||||||||||||||||||||||||
| 462 | 6 | $self->{array_vars}->{$var}++; | |||||||||||||||||||||||||||
| 463 | } elsif ( $var =~ /^\%/ ) { | ||||||||||||||||||||||||||||
| 464 | 0 | $self->{hash_vars}->{$var}++; | |||||||||||||||||||||||||||
| 465 | } else { | ||||||||||||||||||||||||||||
| 466 | 4 | $self->{scalar_vars}->{$var}++; | |||||||||||||||||||||||||||
| 467 | } | ||||||||||||||||||||||||||||
| 468 | } | ||||||||||||||||||||||||||||
| 469 | |||||||||||||||||||||||||||||
| 470 | } | ||||||||||||||||||||||||||||
| 471 | |||||||||||||||||||||||||||||
| 472 | 2 | ▶ | sub _parse_local_vars { | ||||||||||||||||||||||||||
|
Subroutine
_parse_local_vars: called CC=9 SCAR=22.1
| |||||||||||||||||||||||||||||
| 473 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 474 | |||||||||||||||||||||||||||||
| 475 | 2 | my $reg; | |||||||||||||||||||||||||||
| 476 | my $reg2; | ||||||||||||||||||||||||||||
| 477 | 2 | my $reg3; # To find loops variables declared in for and foreach | |||||||||||||||||||||||||||
| 478 | |||||||||||||||||||||||||||||
| 479 | # figure out which are declared in the snippet | ||||||||||||||||||||||||||||
| 480 | 2 | foreach my $var ( keys %{ $self->{scalar_vars} } ) { | |||||||||||||||||||||||||||
| 481 | 4 | $reg = "\\s*my\\s*\\$var\\s*[=;\(]"; | |||||||||||||||||||||||||||
| 482 | 4 | $reg2 = "\\s*my\\s*\\(.*?\\$var.*?\\)"; | |||||||||||||||||||||||||||
| 483 | 4 | $reg3 = "(?:for|foreach)\\s+my\\s*\\$var\\s*\\("; | |||||||||||||||||||||||||||
| 484 | |||||||||||||||||||||||||||||
| 485 | 4 | ▶ | if ( $var =~ /(?:\$\d+$|\$[ab]$)/ ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 486 | 0 | $self->{local_scalars}->{$var}++; | |||||||||||||||||||||||||||
| 487 | } elsif ( $self->{code_snippet} =~ /$reg|$reg2/ ) { | ||||||||||||||||||||||||||||
| 488 | 4 | $self->{local_scalars}->{$var}++; | |||||||||||||||||||||||||||
| 489 | # skip loop variables | ||||||||||||||||||||||||||||
| 490 | 4 | ▶ | if ( $self->{code_snippet} =~ /$reg3/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 491 | 2 | $self->{loop_scalars}->{$var}++; | |||||||||||||||||||||||||||
| 492 | } | ||||||||||||||||||||||||||||
| 493 | } | ||||||||||||||||||||||||||||
| 494 | } | ||||||||||||||||||||||||||||
| 495 | 2 | foreach my $var ( keys %{ $self->{array_vars}} ) { | |||||||||||||||||||||||||||
| 496 | 4 | $var =~ s/\$/\@/; | |||||||||||||||||||||||||||
| 497 | 4 | $reg = "\\s*my\\s*\\$var\\s*[=;\(]"; | |||||||||||||||||||||||||||
| 498 | 4 | $reg2 = "\\s*my\\s*\\(.*?\\$var.*?\\)"; | |||||||||||||||||||||||||||
| 499 | |||||||||||||||||||||||||||||
| 500 | 4 | ▶ | if ( $self->{code_snippet} =~ /$reg|$reg2/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 501 | 2 | $self->{local_arrays}->{$var}++; | |||||||||||||||||||||||||||
| 502 | } | ||||||||||||||||||||||||||||
| 503 | |||||||||||||||||||||||||||||
| 504 | } | ||||||||||||||||||||||||||||
| 505 | 2 | foreach my $var ( keys %{ $self->{hash_vars}} ) { | |||||||||||||||||||||||||||
| 506 | 2 | $var =~ s/\$/\%/; | |||||||||||||||||||||||||||
| 507 | 2 | $reg = "\\s*my\\s*\\$var\\s*[=;\(]"; | |||||||||||||||||||||||||||
| 508 | 2 | $reg2 = "\\s*my\\s*\\(.*?\\$var.*?\\)"; | |||||||||||||||||||||||||||
| 509 | |||||||||||||||||||||||||||||
| 510 | 2 | ▶ | if ( $self->{code_snippet} =~ /$reg|$reg2/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 511 | 2 | $self->{local_hashes}->{$var}++; | |||||||||||||||||||||||||||
| 512 | } | ||||||||||||||||||||||||||||
| 513 | } | ||||||||||||||||||||||||||||
| 514 | |||||||||||||||||||||||||||||
| 515 | } | ||||||||||||||||||||||||||||
| 516 | |||||||||||||||||||||||||||||
| 517 | |||||||||||||||||||||||||||||
| 518 | 0 | ▶ | sub _syntax_check{ | ||||||||||||||||||||||||||
|
Subroutine
_syntax_check: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 519 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 520 | 0 | my $tmp; | |||||||||||||||||||||||||||
| 521 | |||||||||||||||||||||||||||||
| 522 | 0 | my $eval_stmt = "my (". join ', ', @{$self->{parms}}; | |||||||||||||||||||||||||||
| 523 | 0 | $eval_stmt .= ");\n"; | |||||||||||||||||||||||||||
| 524 | 0 | $eval_stmt .= $self->get_sub_call(); | |||||||||||||||||||||||||||
| 525 | 0 | $eval_stmt .= $self->get_new_code(); | |||||||||||||||||||||||||||
| 526 | |||||||||||||||||||||||||||||
| 527 | 0 | $self->{eval_code} = $eval_stmt; | |||||||||||||||||||||||||||
| 528 | |||||||||||||||||||||||||||||
| 529 | 0 | eval " $eval_stmt "; | |||||||||||||||||||||||||||
| 530 | 0 | ▶ | if ($@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 531 | 0 | $self->{eval_err} = $@; | |||||||||||||||||||||||||||
| 532 | |||||||||||||||||||||||||||||
| 533 | 0 | my @errs = split /\n/, $self->{eval_err}; | |||||||||||||||||||||||||||
| 534 | 0 | my @tmp = split /\n/, $self->{return_snippet}; | |||||||||||||||||||||||||||
| 535 | 0 | my $line; | |||||||||||||||||||||||||||
| 536 | 0 | foreach my $err (@errs){ | |||||||||||||||||||||||||||
| 537 | 0 | ▶ | if ($err =~ /line\s(\d+)/){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 538 | 0 | $line = ($1 - 3); | |||||||||||||||||||||||||||
| 539 | 0 | $tmp[$line] .= " #<--- ".$err; | |||||||||||||||||||||||||||
| 540 | } | ||||||||||||||||||||||||||||
| 541 | } | ||||||||||||||||||||||||||||
| 542 | 0 | $self->{return_snippet} = join "\n", @tmp; | |||||||||||||||||||||||||||
| 543 | |||||||||||||||||||||||||||||
| 544 | } | ||||||||||||||||||||||||||||
| 545 | |||||||||||||||||||||||||||||
| 546 | } | ||||||||||||||||||||||||||||
| 547 | |||||||||||||||||||||||||||||
| 548 | 2 | ▶ | sub _transform_snippet { | ||||||||||||||||||||||||||
|
Subroutine
_transform_snippet: called CC=9 SCAR=22.3
| |||||||||||||||||||||||||||||
| 549 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 550 | |||||||||||||||||||||||||||||
| 551 | 2 | my $reg; | |||||||||||||||||||||||||||
| 552 | my $reg2; | ||||||||||||||||||||||||||||
| 553 | 2 | my $arref; | |||||||||||||||||||||||||||
| 554 | 2 | my $href; | |||||||||||||||||||||||||||
| 555 | # Create a sub call that accepts all non-locally declared | ||||||||||||||||||||||||||||
| 556 | # vars as parameters | ||||||||||||||||||||||||||||
| 557 | 2 | foreach my $parm ( keys %{$self->{scalar_vars} } ) { | |||||||||||||||||||||||||||
| 558 | 4 | ▶ | if ( !defined( $self->{local_scalars}->{$parm} ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 559 | 0 | push @{$self->{parms}}, $parm; | |||||||||||||||||||||||||||
| 560 | } else { | ||||||||||||||||||||||||||||
| 561 | # Don't return loop variables | ||||||||||||||||||||||||||||
| 562 | 4 | ▶ | next if grep $parm eq $_, keys %{$self->{loop_scalars}}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 563 | 2 | ▶ | if ( $parm !~ /\$\d+$/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 564 | 2 | push @{$self->{inner_retvals}}, $parm; | |||||||||||||||||||||||||||
| 565 | 2 | push @{$self->{outer_retvals}}, $parm; | |||||||||||||||||||||||||||
| 566 | } | ||||||||||||||||||||||||||||
| 567 | } | ||||||||||||||||||||||||||||
| 568 | } | ||||||||||||||||||||||||||||
| 569 | 2 | foreach my $parm ( keys %{ $self->{array_vars}} ) { | |||||||||||||||||||||||||||
| 570 | 4 | $parm =~ s/\$/\@/; | |||||||||||||||||||||||||||
| 571 | |||||||||||||||||||||||||||||
| 572 | 4 | ▶ | if ( !defined( $self->{local_arrays}->{$parm} ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 573 | 2 | push @{$self->{parms}}, $parm; | |||||||||||||||||||||||||||
| 574 | 2 | $reg2 = "\\$parm"; | |||||||||||||||||||||||||||
| 575 | 2 | ($arref = $parm) =~ s/\@/\$/; | |||||||||||||||||||||||||||
| 576 | 2 | $self->{code_snippet} =~ s/$reg2/\@$arref/g; | |||||||||||||||||||||||||||
| 577 | |||||||||||||||||||||||||||||
| 578 | 2 | $parm =~ s/\@/\$/; | |||||||||||||||||||||||||||
| 579 | 2 | $reg = "\\$parm\\["; | |||||||||||||||||||||||||||
| 580 | |||||||||||||||||||||||||||||
| 581 | 2 | $self->{code_snippet} =~ s/$reg/$parm\-\>\[/g; | |||||||||||||||||||||||||||
| 582 | |||||||||||||||||||||||||||||
| 583 | |||||||||||||||||||||||||||||
| 584 | } else { | ||||||||||||||||||||||||||||
| 585 | 2 | push @{$self->{inner_retvals}}, "\\$parm"; # \@array | |||||||||||||||||||||||||||
| 586 | 2 | push @{$self->{outer_retvals}}, "$parm"; | |||||||||||||||||||||||||||
| 587 | } | ||||||||||||||||||||||||||||
| 588 | } | ||||||||||||||||||||||||||||
| 589 | 2 | foreach my $parm ( keys %{ $self->{hash_vars} } ) { | |||||||||||||||||||||||||||
| 590 | 2 | $parm =~ s/\$/\%/; | |||||||||||||||||||||||||||
| 591 | |||||||||||||||||||||||||||||
| 592 | 2 | ▶ | if ( !defined( $self->{local_hashes}->{$parm} ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 593 | 0 | push @{$self->{parms}}, $parm; | |||||||||||||||||||||||||||
| 594 | 0 | $reg2 = "\\$parm"; | |||||||||||||||||||||||||||
| 595 | 0 | ($href = $parm) =~ s/\%/\$/; | |||||||||||||||||||||||||||
| 596 | 0 | $self->{code_snippet} =~ s/$reg2/\%$href/g; | |||||||||||||||||||||||||||
| 597 | |||||||||||||||||||||||||||||
| 598 | 0 | $parm =~ s/\%/\$/; | |||||||||||||||||||||||||||
| 599 | 0 | $reg = "\\$parm\\{"; | |||||||||||||||||||||||||||
| 600 | |||||||||||||||||||||||||||||
| 601 | 0 | $self->{code_snippet} =~ s/$reg/$parm\-\>\{/g; | |||||||||||||||||||||||||||
| 602 | } else { | ||||||||||||||||||||||||||||
| 603 | 2 | push @{$self->{inner_retvals}}, "\\$parm"; # \%hash | |||||||||||||||||||||||||||
| 604 | 2 | push @{$self->{outer_retvals}}, "$parm"; | |||||||||||||||||||||||||||
| 605 | } | ||||||||||||||||||||||||||||
| 606 | } | ||||||||||||||||||||||||||||
| 607 | 2 | my $retval; | |||||||||||||||||||||||||||
| 608 | my $return_call; | ||||||||||||||||||||||||||||
| 609 | 2 | my $tmp; | |||||||||||||||||||||||||||
| 610 | |||||||||||||||||||||||||||||
| 611 | 2 | $return_call .= "my ("; | |||||||||||||||||||||||||||
| 612 | 2 | $return_call .= join ', ', map {my $tmp; ($tmp = $_) =~ s/[\@\%](.*)/\$$1/; $tmp} sort @{$self->{outer_retvals}}; | |||||||||||||||||||||||||||
| 613 | 2 | $return_call .= ") = ".$self->{sub_name}." ("; | |||||||||||||||||||||||||||
| 614 | $return_call .= join ', ', | ||||||||||||||||||||||||||||
| 615 | 2 | map { ( $tmp = $_ ) =~ s/(\%|\@)(.*)/\\$1$2/; $tmp } @{$self->{parms}}; | |||||||||||||||||||||||||||
| 616 | 2 | $return_call .= ");\n"; | |||||||||||||||||||||||||||
| 617 | |||||||||||||||||||||||||||||
| 618 | 2 | $retval = "sub ".$self->{sub_name}." {\n"; | |||||||||||||||||||||||||||
| 619 | 2 | $retval .= join '', map {($tmp = $_) =~ tr/%@/$/; " my $tmp = shift;\n" } @{$self->{parms}}; | |||||||||||||||||||||||||||
| 620 | 2 | $retval .= "\n" . $self->{code_snippet}; | |||||||||||||||||||||||||||
| 621 | 2 | $retval .= "\n return ("; | |||||||||||||||||||||||||||
| 622 | 2 | $retval .= join ', ', sort @{$self->{inner_retvals}}; | |||||||||||||||||||||||||||
| 623 | 2 | $retval .= ");\n"; | |||||||||||||||||||||||||||
| 624 | 2 | $retval .= "}\n"; | |||||||||||||||||||||||||||
| 625 | |||||||||||||||||||||||||||||
| 626 | # protect quotes and dollar signs | ||||||||||||||||||||||||||||
| 627 | # $retval =~ s/\"/\\"/g; | ||||||||||||||||||||||||||||
| 628 | # $retval =~ s/(\$)/\\$1/g; | ||||||||||||||||||||||||||||
| 629 | |||||||||||||||||||||||||||||
| 630 | |||||||||||||||||||||||||||||
| 631 | 2 | $self->{return_snippet} = $retval; | |||||||||||||||||||||||||||
| 632 | 2 | $self->{return_sub_call} = $return_call; | |||||||||||||||||||||||||||
| 633 | } | ||||||||||||||||||||||||||||
| 634 | |||||||||||||||||||||||||||||
| 635 | |||||||||||||||||||||||||||||
| 636 | # returns arrayref of hashrefs, or undef | ||||||||||||||||||||||||||||
| 637 | 12 | ▶ | sub _scan_file_for_string { | ||||||||||||||||||||||||||
|
Subroutine
_scan_file_for_string: called CC=6 SCAR=18.0
| |||||||||||||||||||||||||||||
| 638 | 12 | my $self = shift; | |||||||||||||||||||||||||||
| 639 | 12 | my $old_name = shift; | |||||||||||||||||||||||||||
| 640 | 12 | my $new_name = shift; | |||||||||||||||||||||||||||
| 641 | 12 | my $file = shift; | |||||||||||||||||||||||||||
| 642 | |||||||||||||||||||||||||||||
| 643 | 12 | my $fh; | |||||||||||||||||||||||||||
| 644 | |||||||||||||||||||||||||||||
| 645 | 12 | ▶ | open $fh, "$file" | ||||||||||||||||||||||||||
|
Condition50%1/2
"$file" || die("Could not open code file '${file}' - $!")
MC/DC0%0/1
"$file" || die("Could not open code file '${file}' - $!")
"$file"
Truth table50%1/2
A
A= "$file"
| |||||||||||||||||||||||||||||
| 646 | || die("Could not open code file '$file' - $!"); | ||||||||||||||||||||||||||||
| 647 | |||||||||||||||||||||||||||||
| 648 | 12 | my $line_number = 0; | |||||||||||||||||||||||||||
| 649 | 12 | my @lines; | |||||||||||||||||||||||||||
| 650 | 12 | my $regex1 = '(\W)(' . $old_name . ')(\W)'; # Surrounded by non-word characters | |||||||||||||||||||||||||||
| 651 | 12 | my $regex2 = "^$old_name(" . '\W)'; # At start of line | |||||||||||||||||||||||||||
| 652 | 12 | ▶ | while (<$fh>) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 653 | 304 | $line_number++; | |||||||||||||||||||||||||||
| 654 | # Look for $old_name surrounded by non-word characters, or at start of line | ||||||||||||||||||||||||||||
| 655 | 304 | ▶ | if (/$regex1/o or /$regex2/o) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
/$regex1/o or /$regex2/o
MC/DC50%1/2
/$regex1/o or /$regex2/o
/$regex1/o
/$regex2/o
Truth table66%2/3
A or B
A= /$regex1/oB= /$regex2/o
| |||||||||||||||||||||||||||||
| 656 | 46 | my $new_line = $_; | |||||||||||||||||||||||||||
| 657 | 46 | $new_line =~ s/$regex1/$1$new_name$3/g; | |||||||||||||||||||||||||||
| 658 | 46 | $new_line =~ s/$regex2/$new_name$1/; | |||||||||||||||||||||||||||
| 659 | 46 | my $hash = {$line_number => $new_line}; | |||||||||||||||||||||||||||
| 660 | 46 | push @lines, $hash; | |||||||||||||||||||||||||||
| 661 | } | ||||||||||||||||||||||||||||
| 662 | } | ||||||||||||||||||||||||||||
| 663 | 12 | close $fh; | |||||||||||||||||||||||||||
| 664 | 12 | ▶ | return @lines ? \@lines : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 665 | } | ||||||||||||||||||||||||||||
| 666 | |||||||||||||||||||||||||||||
| 667 | # Scan a directory, possibly recuring into sub-directories. | ||||||||||||||||||||||||||||
| 668 | 6 | ▶ | sub _scan_directory_for_string { | ||||||||||||||||||||||||||
|
Subroutine
_scan_directory_for_string: called CC=8 SCAR=20.8
| |||||||||||||||||||||||||||||
| 669 | 6 | my ($self,$old_name,$new_name,$where,$hash,$depth) = @_; | |||||||||||||||||||||||||||
| 670 | 6 | my $dh; | |||||||||||||||||||||||||||
| 671 | 6 | ▶ | opendir $dh, $where || | ||||||||||||||||||||||||||
|
Condition50%1/2
$where || die("Could not open directory '${where}': $!")
MC/DC0%0/1
$where || die("Could not open directory '${where}': $!")
$where
Truth table50%1/2
A
A= $where
| |||||||||||||||||||||||||||||
| 672 | die "Could not open directory '$where': $!"; | ||||||||||||||||||||||||||||
| 673 | 6 | ▶ | my @files = grep { $_ ne '.' and $_ ne '..' } readdir $dh; | ||||||||||||||||||||||||||
|
Condition100%3/3
$_ ne '.' && $_ ne '..'
MC/DC100%2/2
$_ ne '.' && $_ ne '..'
$_ ne '.'
$_ ne '..'
Truth table100%3/3
A && B
A= $_ ne '.'B= $_ ne '..'
| |||||||||||||||||||||||||||||
| 674 | 6 | close $dh; | |||||||||||||||||||||||||||
| 675 | 6 | $depth--; | |||||||||||||||||||||||||||
| 676 | 6 | foreach my $file (@files) { | |||||||||||||||||||||||||||
| 677 | 26 | $file = "$where/$file"; # add the directory back on to the path | |||||||||||||||||||||||||||
| 678 | 26 | ▶ | if (-f $file && $self->is_perlfile($file)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
-f $file and $self->is_perlfile($file)
MC/DC100%2/2
-f $file and $self->is_perlfile($file)
-f $file
$self->is_perlfile($file)
Truth table100%3/3
A and B
A= -f $fileB= $self->is_perlfile($file)
| |||||||||||||||||||||||||||||
| 679 | 10 | $hash->{$file} = $self->_scan_file_for_string($old_name,$new_name,$file); | |||||||||||||||||||||||||||
| 680 | } | ||||||||||||||||||||||||||||
| 681 | 26 | ▶ | if (-d $file && $depth >= 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
-d $file and $depth >= 0
MC/DC100%2/2
-d $file and $depth >= 0
-d $file
$depth >= 0
Truth table100%3/3
A and B
A= -d $fileB= $depth >= 0
| |||||||||||||||||||||||||||||
| 682 | # It's a directory, so call this method on the directory. | ||||||||||||||||||||||||||||
| 683 | 2 | $self->_scan_directory_for_string($old_name,$new_name,$file,$hash,$depth); | |||||||||||||||||||||||||||
| 684 | } | ||||||||||||||||||||||||||||
| 685 | } | ||||||||||||||||||||||||||||
| 686 | 6 | return $hash; | |||||||||||||||||||||||||||
| 687 | } | ||||||||||||||||||||||||||||
| 688 | |||||||||||||||||||||||||||||
| 689 | |||||||||||||||||||||||||||||
| 690 | 6 | 1; # File must return true when compiled. Keep Perl happy, snuggly and warm. | |||||||||||||||||||||||||||
| 691 | |||||||||||||||||||||||||||||
| 692 | __END__ | ||||||||||||||||||||||||||||