Hello again monks!! It's been a while since I've delved into the world of Perl. I've so enjoyed your benevolence in the past and hope to again!

Well, I have to admit I am befuddled by my code. After a couple years away, I've decided to delve back into the llama and I'm farther along this time than last.

I'm up to chapter 9 where it asks the student in exercise 2 to :

"Write a program that makes a modified copy of a text file. In the copy, every string Fred (case insensitive) should be replaced with Larry. (So, "Manfred Mann" should become "ManLarry Mann".) The input filename should be given on the command line (don't ask the user), and the output file name should be the corresponding file name ending with .out.

Here's my answer so far...Some unnecessary code in there, but I'm still playing around with it.

#!/usr/bin/perl use strict; my $file = $ARGV[0]; if (!$file) { print "Hey! You didn't give me a file, sucka!\n"; } my $out = $file; open IN, "$file" or die "File did not open\n"; open OUT, "<$out" or die "File not available for write.\n"; $out =~ s/(\.\w+)?$/.out/; while (<IN>) { $out =~ s/fred/larry/gi; print OUT; }
But what I'm befuddled by is that the pattern match: $out =~ s/(\.\w+)?$/.out/; does not manipulate the output filename.

To my way of thinking, because the variable $out holds the file that's being fed to the program, the file name should be changed by the pattern match. Maybe the pattern match is only meant for the contents of the file, as opposed to the name of the file itself. I may not know how to manipluate the name of the file in the filesystem.

Not only that, but this code
while (<IN>) { $out =~ s/fred/larry/gi; print OUT; }
fails to perform the substitution.

Obviously there is some lameness in my thinking, I was hoping some sharp blade out there could set me straight on what I'm doing wrong.

You all da best!

In reply to Pattern not manipulating file output by bluethundr

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.