bluethundr has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks,

I am, yet again, working my way through Llama. I am making an attempt at Chapter 9 exercise 4. The question is on page 134 (of the 4th Edition. I just got the 5th edition and I plan on making my way through that too!).

To summarize, the question asks you to add a copyright line to each exercise you've written so far by naming the files on the command line. This was my solution, but it doesn't seem to be panning out:

#!/usr/bin/perl -w use strict; $^I = ".bak"; while (<>) { if (/^#!/) { $_ .= "## Copyright (C) 2008 by C'est Mois"; } }


When I run the program on my files the files are not altered. Any idea where I went wrong? Many thanks to the all knowing monks!

Replies are listed 'Best First'.
Re: Regex not working
by linuxer (Curate) on Dec 27, 2008 at 15:43 UTC

    Testing your code, my testfiles were altered. I had a .bak file containing the old content and the "original" file was empty.

    You lack a print statement in you while loop to print the file contents.

    #!/usr/bin/perl -w use strict; $^I = ".bak"; while (<>) { if (/^#!/) { # BE AWARE OF THE NEWLINE $_ .= "## Copyright (C) 2008 by C'est Mois\n"; } # BEWARE THIS: prints $_ print; }
Re: Regex not working
by Corion (Patriarch) on Dec 27, 2008 at 15:45 UTC

    The question is, how are you running the program?

    Most likely, you're running the program from the command line without naming any files to be altered, or you're running it under Windows, with only giving a wildcard match to the script. In both cases, you can give or expand the script files to be matched by prepopulating the @ARGV array:

    ... use strict; use File::Glob qw(bsd_glob); @ARGV = glob '*.pl'; # to treat all .pl files ...
Re: Regex not working
by Anonymous Monk on Dec 27, 2008 at 15:48 UTC
      That's it guys! Adding the print statement and the newline is what did it! You guys rule! And I just know if I persist at learning this stuff, obvious things like this won't bite me in the ass anymore.

      Cheers!
      #!/usr/bin/perl -w use strict; $^I = ".bak"; while (<>) { if (/^#!/) { $_ .= "## Copyright (C) 2008 by C'est Mois\n"; } print; }
Re: Regex not working
by sasdrtx (Friar) on Dec 28, 2008 at 00:04 UTC

    Copyright by "That is Month"?? Or are you using an ancient version of Occitan or something?

    sas