You're making this a lot harder than it needs to be.
#!/usr/bin/perl use 5.010; use strict; use File::Slurp qw(edit_file_lines); use Path::Class qw(file dir); use constant SKIP => 'SKIP'; use constant SIMULATION => 1; sub process_file { my ($file, %param) = @_; # skip files that need skipping if ($param{skip}->($file) eq SKIP) { say { $param{log} } "Skipping $file"; return 0; } if ($file->is_dir) { my $modified = 0; say { $param{log} } "Recursing into directory $file"; foreach ($file->children) { $modified += process_file($_, %param); } say { $param{log} } "Finished with directory $file - modified +$modified files inside"; return $modified; } say { $param{log} } "Processing file $file"; my $i = 0; my $modified = 0; edit_file_lines { $i++; foreach my $change (@{ $param{changes} }) { my ($match, $replace) = @$change; if (/$match/) { say { $param{log} } "Line $i matched $match - replacin +g with $replace"; $modified = 1; s/$match/$replace/g unless SIMULATION; } } } $file; say { $param{log} } "Done with file $file"; return $modified; } process_file( # starting point for crawl dir('somedir'), # list of changes to make changes => [ [ qr{ChangeThis}i => 'ToThis' ], [ qr{ChangeThat}i => 'ToThat' ], ], # sub which returns SKIP if a file (or directory) should be skippe +d skip => sub { my $file = shift; if ($file->is_dir and [$file->dir_list]->[-1] !~ /^[a-zA-Z0-9] +/) { return SKIP; } return; }, log => file('changling.log')->openw, );
In reply to Re: Break out of program to get correct logic (total count)
by tobyink
in thread Break out of program to get correct logic (total count)
by begood321
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |