Blob


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