Blame


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