Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9175847
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:58:59+00:00 2026-06-17T16:58:59+00:00

I’m working my way through Learning Perl , Chapter 9, Processing Text with Regular

  • 0

I’m working my way through Learning Perl, Chapter 9, “Processing Text with Regular Expressions.”

Here’s two of the end-of-chapter exercises:

  1. Write a program to add a copyright line to all of your exercise answers so far, placing a line like ## Copyright (c) 20XX by Yours Truly in the file immediately after the ‘shebang’ line. Presume that the program will be invoked with the filenames to edit already on the command line.

  2. Modify the previous program so that it doesn’t edit the files that already contain the copyright line. As a hint on that, you might need to know that the name of the file being read by the diamond operator is in $ARGV.

This was my attempted solution:

#!/usr/bin/env perl

use 5.014;
use warnings;

my $shebang     = '(#!/usr/bin/env perl|#!/usr/bin/perl)'; 
my $copyright   = '# Copyright (c) 20XX Yours Truly'; 

$^I = ".bak";

while (<>) {
    unless (/$copyright/mi) {
        s/($shebang)/$1\n$copyright/mig;
    }
    print;
}

Run on the command line with perl ch9.pl sample_perl_script.pl.

My goals were:

  • Keep the original shebang intact, regardless of path.
  • Loop through <> just once.
  • Check to see if the copyright notice existed.
  • If it didn’t, add it (hence the attempt with unless { ... }).

This works for the first part of the problem (adding a copyright line) but not the second (check to make sure the copyright doesn’t already exist).

My questions are: Why? And why is the unless totally ignored when I run the program?

I peeked at the appendix, and the book’s proposed solution was to create a hash to track filenames from $ARGV, and pass over the files twice. First to eliminate files that already had the copyright notice, then to perform the search/replace. Like so:

my %do_these;
foreach (@ARGV) {
    $do_these{$_} = 1;
}

while (<>) { 
    if (/\A## Copyright/) {
        delete $do_these{$ARGV};
    }
}

@ARGV = sort keys %do_these; 
$^I = ".bak";
while (<>) {
    if (/\A#!/) {
        $_ .= "## Copyright (c) 20XX by Yours Truly\n";
    }
    print;
}

This works, of course, but it seems like twice the work. I’m trying to see if there’s a way to do this within a single while (<>) { ... } loop, with my approach, and come away with a better understanding of how the diamond operator works.

If my approach is totally off-base, please explain why and don’t spare my feelings. I’m more interested in a full understanding than my ego.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T16:59:00+00:00Added an answer on June 17, 2026 at 4:59 pm

    Your unless does not work because the copyright is not on the same line as the shebang. The diamond operator reads a line up until the first value of $/, which by default is newline. Your program will perform the substitution on all the lines that do not contain the copyright.

    Since this is perl, there are many ways to fix it. The most straightforward way is perhaps to unset $/ and slurp the file (read it all into one line). That way you can check right away if there is a copyright notice on the second line of the file. E.g.:

    local $/;                                     # slurp the file
    while (<>) {
        s/^.*\n\K(?!\Q$copyright\E)/$copyright/;  # negative lookahead assertion
        print;
    }
    

    You can also check line number 2 in your files directly, without slurping the file:

    while (<>) {
        if ($. == 2) {
             unless (/\Q$copyright/) {
                   print "$copyright\n";
             }
        }
        print;
        close ARGV if eof;                # this will reset the line counter $.
    }
    

    Note that Nick ODell is correct that your copyright string contains meta characters (namely parentheses) which must be escaped. I used \Q ... \E escape sequences above.

    Note also that you do not need to be very specific in checking for the shebang, that is more likely to trip you up on slightly varied lines.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
In my XML file chapters tag has more chapter tag.i need to display chapters
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I've tracked down a weird MySQL problem to the two different ways I was
I have a text area in my form which accepts all possible characters from
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have a reasonable size flat file database of text documents mostly saved in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.