You are looking at a very old, but free version of the course. If you are interesed the most recent version, check it out on the Perl Maven site.

12.9. Change Copyright text in every source file in a directory hierarchy

  • 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.