Troubleshooting printing to file

E

EmmettPower

Hi,

I have a problem with what should be a simple issue. I am testing a
number of files in a directory for a pattern using a regular
expression. The successful matches are printed to the screen a file
(Output.txt). I've set out the code below. The pattern matches across
lines, hence the use of File::Slurp.

I'm using Perl on a Windows platform. The script works fine when I test
it on one computer but when I migrate it across to the computer holding
the files is fails to print the results to the Output file. It prints
to screen fine. Both computers are using the same version of Windows
and Perl.

It should be simple but I can't figure out what's wrong.

Any suggestions would be appreciated.

Regards

Emmett Power


<--------------------------CODE-------------------------------->

use File::Slurp;

my $file;
my $name;

my $FILELIST = "FileList.txt";
open(LIST, "$FILELIST");

open (OUT,'>Output.txt');

while(<LIST>)
{
chomp;
$name = $_;
$file = read_file($name);

while ($file =~ /test/gi){

print "$&,$_\n";
print OUT "$&,$_\n";

}

}

close(OUT);
close(LIST);
 
G

Gunnar Hjalmarsson

The script works fine when I test it on one computer but when I
migrate it across to the computer holding the files is fails to print
the results to the Output file. It prints to screen fine.

<snip>

I hope you have

use strict;
use warnings;

here.
use File::Slurp;

my $file;
my $name;

my $FILELIST = "FileList.txt";
open(LIST, "$FILELIST");

open (OUT,'>Output.txt');

So, if it fails to print to Output.txt, you should obviously let Perl
check for success (same with the file reading).

open(LIST, "$FILELIST") or die "Couldn't open $FILELIST: $!";
open (OUT,'>Output.txt') or die
"Couldn't open Output.txt for writing: $!";

See "perldoc perlopentut".
 
C

Chris Mattern

Hi,

I have a problem with what should be a simple issue. I am testing a
number of files in a directory for a pattern using a regular
expression. The successful matches are printed to the screen a file
(Output.txt). I've set out the code below. The pattern matches across
lines, hence the use of File::Slurp.

I'm using Perl on a Windows platform. The script works fine when I test
it on one computer but when I migrate it across to the computer holding
the files is fails to print the results to the Output file. It prints
to screen fine. Both computers are using the same version of Windows
and Perl.

It should be simple but I can't figure out what's wrong.

Any suggestions would be appreciated.

Regards

Emmett Power


<--------------------------CODE-------------------------------->

use File::Slurp;
Where's your

use strict;
use warnings;

?
my $file;
my $name;

my $FILELIST = "FileList.txt";
open(LIST, "$FILELIST");

open (OUT,'>Output.txt');

Most likely, opening Output.txt failed. Why? No way to know,
because you didn't check. Always, *always* check to make sure
open succeeds.

open(OUT, '>Output.txt')
or die("Opening Output.txt failed: $!, stopped");

You should also check your opening of the input file. Checking
the close calls is a bit paranoid, but paranoia can be a good
habit for a programmer to get into.
while(<LIST>)
{
chomp;
$name = $_;
$file = read_file($name);

while ($file =~ /test/gi){

print "$&,$_\n";
print OUT "$&,$_\n";

}

}

close(OUT);
close(LIST);

--
Christopher Mattern

"Which one you figure tracked us?"
"The ugly one, sir."
"...Could you be more specific?"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top