Blame


1 ffd9a51f 2023-08-04 jrmu #!/usr/bin/perl
2 ffd9a51f 2023-08-04 jrmu
3 ffd9a51f 2023-08-04 jrmu # Extra credit exercise: write a program to add a copyright line to
4 ffd9a51f 2023-08-04 jrmu # all of your exercise answers so far, by placing a line like:
5 ffd9a51f 2023-08-04 jrmu #
6 ffd9a51f 2023-08-04 jrmu # ## Copyright (C) 2021 by Your Name <yourname@ircnow.org>
7 ffd9a51f 2023-08-04 jrmu # ##
8 ffd9a51f 2023-08-04 jrmu # ## Permission is granted to use, copy, modify, and/or distribute
9 ffd9a51f 2023-08-04 jrmu # ## this work for any purpose with or without fee. This work is
10 ffd9a51f 2023-08-04 jrmu # ## offered as-is, with absolutely no warranty whatsoever. The
11 ffd9a51f 2023-08-04 jrmu # ## author is not responsible for any damages that result from
12 ffd9a51f 2023-08-04 jrmu # ## using this work.
13 ffd9a51f 2023-08-04 jrmu #
14 ffd9a51f 2023-08-04 jrmu # in the file immediately after the "shebang" line. You should edit
15 ffd9a51f 2023-08-04 jrmu # the files "in place,", keeping a backup. Presume that the program
16 ffd9a51f 2023-08-04 jrmu # will be invokved with the filenames to edit already on the
17 ffd9a51f 2023-08-04 jrmu # command line.
18 ffd9a51f 2023-08-04 jrmu #
19 ffd9a51f 2023-08-04 jrmu # Extra extra credit exercise: modify the previous program so that
20 ffd9a51f 2023-08-04 jrmu # it doesn't edit the files that already contain the copyright
21 ffd9a51f 2023-08-04 jrmu # line. As a hint on that, you might need to know that the name of
22 ffd9a51f 2023-08-04 jrmu # the file being read by the diamond operator is in $ARGV.
23 ffd9a51f 2023-08-04 jrmu
24 ffd9a51f 2023-08-04 jrmu use warnings;
25 ffd9a51f 2023-08-04 jrmu use strict;
26 ffd9a51f 2023-08-04 jrmu use utf8;
27 ffd9a51f 2023-08-04 jrmu
28 ffd9a51f 2023-08-04 jrmu my %files;
29 ffd9a51f 2023-08-04 jrmu my $copyright = <<'END_COPYRIGHT';
30 ffd9a51f 2023-08-04 jrmu ## Copyright (C) 2023 by jrmu <jrmu@ircnow.org>
31 ffd9a51f 2023-08-04 jrmu ##
32 ffd9a51f 2023-08-04 jrmu ## Permission is granted to use, copy, modify, and/or distribute
33 ffd9a51f 2023-08-04 jrmu ## this work for any purpose with or without fee. This work is
34 ffd9a51f 2023-08-04 jrmu ## offered as-is, with absolutely no warranty whatsoever. The
35 ffd9a51f 2023-08-04 jrmu ## author is not responsible for any damages that result from
36 ffd9a51f 2023-08-04 jrmu ## using this work.
37 ffd9a51f 2023-08-04 jrmu END_COPYRIGHT
38 ffd9a51f 2023-08-04 jrmu
39 ffd9a51f 2023-08-04 jrmu foreach my $file (@ARGV) {
40 ffd9a51f 2023-08-04 jrmu $files{$file} = 1;
41 ffd9a51f 2023-08-04 jrmu }
42 ffd9a51f 2023-08-04 jrmu foreach my $file (@ARGV) {
43 ffd9a51f 2023-08-04 jrmu open my $fh, '<', $file or die "Unable to open $file: $!";
44 ffd9a51f 2023-08-04 jrmu my $lines = join '', <$fh>;
45 ffd9a51f 2023-08-04 jrmu if ($lines =~ m/\A#!.*(\n|\r)*\Q$copyright\E/mi) {
46 ffd9a51f 2023-08-04 jrmu delete $files{$file};
47 ffd9a51f 2023-08-04 jrmu next;
48 ffd9a51f 2023-08-04 jrmu }
49 ffd9a51f 2023-08-04 jrmu }
50 ffd9a51f 2023-08-04 jrmu
51 ffd9a51f 2023-08-04 jrmu @ARGV = sort keys %files;
52 ffd9a51f 2023-08-04 jrmu $^I = ".bak";
53 ffd9a51f 2023-08-04 jrmu
54 ffd9a51f 2023-08-04 jrmu while (<>) {
55 ffd9a51f 2023-08-04 jrmu s@\A(#!.*\n)@$1$copyright@m;
56 ffd9a51f 2023-08-04 jrmu print;
57 ffd9a51f 2023-08-04 jrmu }