Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

writing to output using filehandles

6 views
Skip to first unread message

mark baumeister

unread,
Apr 11, 2011, 8:47:53 PM4/11/11
to begi...@perl.org
Hi I am having trouble with my search and replace code in the program
below.
I can sucessfully copy the input file to the output file but
my search and replace is not working. Any hints on what I am doing
wrong?
Thanks,
M


#!/usr/bin/perl -w
use strict;

my $input;
my $output;
my $search;
my $replace;

print "enter an input file name:\n";
$input = <STDIN>;
chomp($input);

print "enter an output file name:\n";
$output = <STDIN>;
chomp($output);


print "enter a search pattern:\n";
$search = <STDIN>;
chomp($search);

print "enter a replacement string:\n";
$replace = <STDIN>;
chomp($replace);

open INFILE, "<$input" or die "Can’t open $input ($!)";
open OUTFILE, "<$output" or die "Can’t open $output ($!)";

while (<INFILE>) {
s/$search/$replace/g;
print OUTFILE $_;
}

Sunita Rani Pradhan

unread,
Apr 12, 2011, 4:28:10 AM4/12/11
to mark baumeister, begi...@perl.org
Hi Mark

You have opened OUTFILE (OUTFILE, "<$output" ) in read mode . To
write to this file you need to open in write mode (">$output").

Sunita


#!/usr/bin/perl -w
use strict;


--
To unsubscribe, e-mail: beginners-...@perl.org
For additional commands, e-mail: beginne...@perl.org
http://learn.perl.org/


Rob Dixon

unread,
Apr 12, 2011, 6:48:37 PM4/12/11
to begi...@perl.org, Mark Baumeister
On 12/04/2011 01:47, mark baumeister wrote:
>
> Hi I am having trouble with my search and replace code in the
> program below.
>
> I can sucessfully copy the input file to the output file but
> my search and replace is not working. Any hints on what I am doing
> wrong?

Hello Mark.

You must tell us what is happening to indicate that your search and
replace isn't working. In the mean time here are some comments on your
code.

> #!/usr/bin/perl -w
> use strict;

You should 'use warnings' here instead of -w or -W on the command line.

> my $input;
> my $output;
> my $search;
> my $replace;

Once you have used 'use strict' it is best to declare variables as
closely as possible to their use. Only FORTRAN or very old C required
declaration at the start of a block.

> print "enter an input file name:\n";
> $input =<STDIN>;
> chomp($input);
>
> print "enter an output file name:\n";
> $output =<STDIN>;
> chomp($output);

OK, but omit the declaration at the top and

chomp (my $input = <STDIN>);

etc.

> print "enter a search pattern:\n";
> $search =<STDIN>;
> chomp($search);
>
> print "enter a replacement string:\n";
> $replace =<STDIN>;
> chomp($replace);
>
> open INFILE, "<$input" or die "Can’t open $input ($!)";
> open OUTFILE, "<$output" or die "Can’t open $output ($!)";

Common wisdom says that you should write

open my $infile, '<', $input or die "Can't open $input ($!)";

> while (<INFILE>) {
> s/$search/$replace/g;
> print OUTFILE $_;
> }

If you have taken my advice and used a lexical file handle then this is

while (<$infile>) {
s/$search/$replace/g;
print $outfile;
}


My best guess is that there are characters in $search that mean
something special in the context of a regular expression. You really
should have explained your problem properly, but to fix that you should
write instead

while (<$infile>) {
s/\Q$search/$replace/g;
print $outfile;
}

If this doesn't fix your problem then please ask again, but please also
try to rephrase your question properly.

HTH,

Rob

0 new messages