Blame


1 ffd9a51f 2023-08-04 jrmu #!/usr/bin/perl
2 ffd9a51f 2023-08-04 jrmu
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.
8 ffd9a51f 2023-08-04 jrmu
9 ffd9a51f 2023-08-04 jrmu use warnings;
10 ffd9a51f 2023-08-04 jrmu use strict;
11 ffd9a51f 2023-08-04 jrmu use utf8;
12 ffd9a51f 2023-08-04 jrmu
13 ffd9a51f 2023-08-04 jrmu if (scalar(@ARGV) != 1) {
14 ffd9a51f 2023-08-04 jrmu die "Usage: $0 filename";
15 ffd9a51f 2023-08-04 jrmu }
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': $!";
18 ffd9a51f 2023-08-04 jrmu
19 ffd9a51f 2023-08-04 jrmu while (<>) {
20 ffd9a51f 2023-08-04 jrmu chomp;
21 ffd9a51f 2023-08-04 jrmu s/Fred/Larry/ig;
22 ffd9a51f 2023-08-04 jrmu print $fh "$_\n";
23 ffd9a51f 2023-08-04 jrmu }