in reply to Pattern not manipulating file output

Rather than print OUT; you should consider print OUT "$out\n";

That is, you have to specify what it is you want printed.

Replies are listed 'Best First'.
Re^2: Pattern not manipulating file output
by bluethundr (Pilgrim) on Feb 20, 2008 at 23:09 UTC
    Thanks to all you guys! I incorporated most of your suggestions, but haven't had the opportunity to incorporate all of them yet (I'm still at work).

    But here is the code as I've tweaked it that seems to work as the exercise intended:

    #!/usr/bin/perl use strict; use warnings; my $file = @ARGV[0]; if (!$file) { print "Hey! You didn't give me a file, sucka!\n"; } my $out = $file; $out =~ s/\.\w+?$/.out/; open IN, "<$file" or die "File did not open.\n"; open OUT, ">$out" or die "File not available for write.\n"; while (<IN>) { $_ =~ s/fred/\Ul\Earry/gi; }
    This code seems to work as intended. My only remaining confusion relates to these lines of code:
    my $out = $file; $out =~ s/\.\w+?$/.out/;
    now is the first line $out accepting the first index of @ARGV as a "file" or is it just accepting the name of the file.

    What I'm supposing is that $out just knows the name of the file as a scalar variable, or a string, and that the program is relying on the OUT filehandle to create a file with a name based on the scalar variable which has been manipulated by the pattern match. Any clarification on this point will be appreciated.