file handling - a simple explanation?

G

Geoff Cox

Hello,

I am getting the following error message for the last 3 files being
processed only, and must be doing something wrong - not sure what
though. The code works.

print() on closed filehandle OUT2 at hp3.pl line 33, <IN> line 12.

The code is

open (IN, $name);
open OUT, (">>hp-$name");
open OUT2, (">>html/hp-index.htm");

while (defined(my $line = <IN>)) {

if ($line =~ /<title>(.*?)- (.*?)<\/title>/) {
print OUT2 ('<a href="hp-$htmlname.htm">$1 $2</a> <br>\n');


Thanks for any ideas.

Geoff
 
J

John Bokma

Geoff said:
Hello,

I am getting the following error message for the last 3 files being
processed only, and must be doing something wrong - not sure what
though. The code works.

print() on closed filehandle OUT2 at hp3.pl line 33, <IN> line 12.

The code is
bad

open (IN, $name);

or die "Can't open '$name': $!";
open OUT, (">>hp-$name");

or die ...
open OUT2, (">>html/hp-index.htm");

or die ...

Also, put use strict; and use warnings; on top of your script if you
haven't done so.
 
G

Gunnar Hjalmarsson

Geoff said:
Hello,

I am getting the following error message for the last 3 files being
processed only, and must be doing something wrong - not sure what
though. The code works.

print() on closed filehandle OUT2 at hp3.pl line 33, <IN> line 12.

The code is

open (IN, $name);
open OUT, (">>hp-$name");
open OUT2, (">>html/hp-index.htm");

Yes, you are doing something wrong. You are not checking for success
when opening files.

open IN, $name or die "Couldn't open $name: $!";
 
G

Geoff Cox


John,

Thanks - I added the or die etc and then found that I had one
directory which did not have the sub directory expected by the code!

I am now converted to having the or die etc,.

Cheers

Geoff
 
G

Geoff Cox

Yes, you are doing something wrong. You are not checking for success
when opening files.

open IN, $name or die "Couldn't open $name: $!";

Thanks Gunnar - I corrected the code and then realised it expected a
sub directory which was not there. I am now convinced not to be lazy
re the or die!

Cheers

Geoff
 

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,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top