Blob


1 #!/usr/bin/perl
3 # Write a program that makes a modified copy of a text file. In the copy,
4 # every string Fred (case-insensitive) should be replaced with Larry. (So,
5 # Manfred Mann should become ManLarry Mann.) The input file name should be
6 # given on the command line (don't ask the user!), and the output filename
7 # should be the corresponding file name ending with .out.
9 use warnings;
10 use strict;
11 use utf8;
13 if (scalar(@ARGV) != 1) {
14 die "Usage: $0 filename";
15 }
16 my $filepath = ($ARGV[0] =~ s/.[^.]*\z/.out/r);
17 open my $fh, ">", $filepath or die "Unable to write to '$filepath': $!";
19 while (<>) {
20 chomp;
21 s/Fred/Larry/ig;
22 print $fh "$_\n";
23 }