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