Blob


1 #!/usr/bin/perl
2 ## Copyright (C) 2023 by jrmu <jrmu@ircnow.org>
3 ##
4 ## Permission is granted to use, copy, modify, and/or distribute
5 ## this work for any purpose with or without fee. This work is
6 ## offered as-is, with absolutely no warranty whatsoever. The
7 ## author is not responsible for any damages that result from
8 ## using this work.
10 # Write a program that makes a modified copy of a text file. In the copy,
11 # every string Fred (case-insensitive) should be replaced with Larry. (So,
12 # Manfred Mann should become ManLarry Mann.) The input file name should be
13 # given on the command line (don't ask the user!), and the output filename
14 # should be the corresponding file name ending with .out.
16 use warnings;
17 use strict;
18 use utf8;
20 if (scalar(@ARGV) != 1) {
21 die "Usage: $0 filename";
22 }
23 my $filepath = ($ARGV[0] =~ s/.[^.]*\z/.out/r);
24 open my $fh, ">", $filepath or die "Unable to write to '$filepath': $!";
26 while (<>) {
27 chomp;
28 s/Fred/Larry/ig;
29 print $fh "$_\n";
30 }