File::Find reference to subroutine
Example 12-5. examples/shell/change_files.pl #!/usr/bin/perl
use strict;
use warnings;
use File::Find qw(find);
my $dir = ".";
if (defined $ARGV[0]) {
$dir = $ARGV[0];
}
find( \&change_file, $dir);
sub change_file {
if (not -f $_) {
return;
}
if (substr($_, -3) ne ".pl") {
return;
}
print "$_\n";
my $data;
if (open my $fh, "<", $_) {
local $/ = undef;
$data = <$fh>;
} else {
warn "Could not open '$_' for reading\n";
return;
}
$data =~ s/Copyright Old/Coyright New/g;
# Let's not ruin our example files....
my $new_name = "$_.new";
if (open my $fh, ">", $new_name) {
print $fh $data;
} else {
warn "Could not open '$new_name' for writing\n";
}
return;
}
If you are interested in on-site trainings by the author, please
contact me directly.
|