writing to output using filehandles

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 =3D <STDIN>;
chomp($input);

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


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

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

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

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


--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
mark baumeister [ Di, 12 April 2011 02:47 ] [ ID #2057988 ]

RE: writing to output using filehandles

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

-----Original Message-----
From: mark baumeister [mailto:mlfmp975 [at] gmail.com]
Sent: Tuesday, April 12, 2011 6:18 AM
To: beginners [at] perl.org
Subject: writing to output using filehandles

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 =3D <STDIN>;
chomp($input);

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


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

print "enter a replacement string:\n";
$replace =3D <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 $_;
}


--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/



--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Sunita Rani Pradhan [ Di, 12 April 2011 10:28 ] [ ID #2057990 ]

Re: writing to output using filehandles

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 =3D<STDIN>;
> chomp($input);
>
> print "enter an output file name:\n";
> $output =3D<STDIN>;
> chomp($output);

OK, but omit the declaration at the top and

chomp (my $input =3D <STDIN>);

etc.

> print "enter a search pattern:\n";
> $search =3D<STDIN>;
> chomp($search);
>
> print "enter a replacement string:\n";
> $replace =3D<STDIN>;
> chomp($replace);
>
> open INFILE, "<$input" or die "Can=92t open $input ($!)";
> open OUTFILE, "<$output" or die "Can=92t 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

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Rob Dixon [ Mi, 13 April 2011 00:48 ] [ ID #2058043 ]
Perl » gmane.comp.lang.perl.beginners » writing to output using filehandles

Vorheriges Thema: call method by name
Nächstes Thema: Regular Expressions Question