readline() on closed fielhandle?

G

Geoff Cox

Hello,

Cannot see what is wrong with following - I get following error
message "readline() on closed fielhandle at line ++" - help please!

Cheers

Geoff

use warnings;
use strict;

use File::Find;

open (OUT, ">>out");

my $dir = 'c:/atemp1/test';

find ( sub {

open (IN, "$_");

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

if ($line =~ /Mailto:(.*?)"/i) {
print OUT ("$1");
}

}

}, $dir);

close (OUT);
close (IN);
 
K

Kevin Michael Vail

Geoff Cox said:
Cannot see what is wrong with following - I get following error
message "readline() on closed fielhandle at line ++" - help please!

[]

open (IN, "$_");

You don't even know if the open succeeded. **Always** check to be sure
open succeeds:

open IN, $_ or die "can't open $_: $!\n";

(Oh, and you don't need the quotes around $_.)
print OUT ("$1");

Don't need the quotes around $1, either.
 
M

Michael Budash

Geoff Cox said:
Hello,

Cannot see what is wrong with following - I get following error
message "readline() on closed fielhandle at line ++" - help please!

Cheers

Geoff

use warnings;
use strict;

use File::Find;

open (OUT, ">>out");

my $dir = 'c:/atemp1/test';

find ( sub {

open (IN, "$_");

open (IN, "$_") or return;
++ while (defined(my $line=<IN>)) {
[snip]

hth-
 
G

Geoff Cox

Geoff Cox said:
Cannot see what is wrong with following - I get following error
message "readline() on closed fielhandle at line ++" - help please!

[]

open (IN, "$_");

You don't even know if the open succeeded. **Always** check to be sure
open succeeds:

open IN, $_ or die "can't open $_: $!\n";

Kevin,

This has got me confused ! If I use the check for opening the file I
get error message "failed in open(): Permission denied at line 8" and
nothing goes into the file called out ...

If I use line 7 instead, I get error message "readline() on closed
filehandle IN at line 9" - but the email addresses are put into the
file
called out ..?? What am I missing here?

Geoff


1 use warnings;
2 use strict;
3 use File::Find;
4 open (OUT, ">>out");
5 my $dir = 'c:/atemp1/test';
6 find ( sub {
7 open (IN, "$_");
8 # open(IN, $_) or die "Failed in open(): $!";
9 while (defined(my $line=<IN>)) {
10 if ($line =~ /Mailto:(.*?)"/i) {
11 print OUT ("$1 \n");
12 }
13 }
14 }, $dir);
15 close (OUT);
16 close (IN);
 
B

Bob Walton

Geoff said:
Cannot see what is wrong with following - I get following error
message "readline() on closed fielhandle at line ++" - help please!

[]

open (IN, "$_");
You don't even know if the open succeeded. **Always** check to be sure
open succeeds:

open IN, $_ or die "can't open $_: $!\n";

Kevin,

This has got me confused ! If I use the check for opening the file I
get error message "failed in open(): Permission denied at line 8" and
nothing goes into the file called out ...

If I use line 7 instead, I get error message "readline() on closed
filehandle IN at line 9" - but the email addresses are put into the
file
called out ..?? What am I missing here?


As the sub code reference argument to File::Find's find function, note
that your sub (and your open statement) is being executed many times,
once for each file (but your close statement isn't -- but that doesn't
matter, since open() will first close the filehandle if it is still
open). Apparently somewhere down that list of files you have a file
with permissions set so the user you are running this under cannot read
that file. At that point your open fails with your permission denied
error, and you choose to either die or ignore that fact, depending upon
whether you put the "or die" in. When you told it to die on error, it
did. Maybe you want to just "or return" on error and ignore that file.
You will probably note that only some of your email addresses are put
into the file called out, and that it either skips those files or
terminates when it gets to the first file with the bad permissions
anyway. I'm not sure if the readline() error is a warning or a fatal
error -- I'd guess it is merely a warning, and that consequently your
code is essentially skipping your bad-permission file(s) when you omit
the "or die" on the open.

Geoff


1 use warnings;
2 use strict;
3 use File::Find;
4 open (OUT, ">>out");
5 my $dir = 'c:/atemp1/test';
6 find ( sub {
7 open (IN, "$_");
8 # open(IN, $_) or die "Failed in open(): $!";
9 while (defined(my $line=<IN>)) {
10 if ($line =~ /Mailto:(.*?)"/i) {
11 print OUT ("$1 \n");
12 }
13 }
14 }, $dir);
15 close (OUT);
16 close (IN);
....
 
G

Geoff Cox

Geoff said:
Cannot see what is wrong with following - I get following error
message "readline() on closed fielhandle at line ++" - help please!

[]

open (IN, "$_");

You don't even know if the open succeeded. **Always** check to be sure
open succeeds:

open IN, $_ or die "can't open $_: $!\n";

Kevin,

This has got me confused ! If I use the check for opening the file I
get error message "failed in open(): Permission denied at line 8" and
nothing goes into the file called out ...

If I use line 7 instead, I get error message "readline() on closed
filehandle IN at line 9" - but the email addresses are put into the
file
called out ..?? What am I missing here?


As the sub code reference argument to File::Find's find function, note
that your sub (and your open statement) is being executed many times,
once for each file (but your close statement isn't -- but that doesn't
matter, since open() will first close the filehandle if it is still
open). Apparently somewhere down that list of files you have a file
with permissions set so the user you are running this under cannot read
that file. At that point your open fails with your permission denied

Bob,

this is odd as I am using Windows 98(SE) and have checked that all
files have only the A atribute .. ?
error, and you choose to either die or ignore that fact, depending upon
whether you put the "or die" in. When you told it to die on error, it
did. Maybe you want to just "or return" on error and ignore that file.

with "or return" the code runs without error message and the email
addresses are extracted ..

Cheers

Geoff
 
T

Tad McClellan

Geoff Cox said:
1 use warnings;
2 use strict;
3 use File::Find;
4 open (OUT, ">>out");
5 my $dir = 'c:/atemp1/test';
6 find ( sub {
7 open (IN, "$_");
8 # open(IN, $_) or die "Failed in open(): $!";
9 while (defined(my $line=<IN>)) {
10 if ($line =~ /Mailto:(.*?)"/i) {
11 print OUT ("$1 \n");
12 }
13 }
14 }, $dir);
15 close (OUT);
16 close (IN);


Please spend the time to format your code sensibly.

Many people will just move on to help someone else who has
done what they can to make it easier to give an answer.
 
J

Jay Tilton

: >Geoff Cox wrote:

: >>>open IN, $_ or die "can't open $_: $!\n";

: >> This has got me confused ! If I use the check for opening the file I
: >> get error message "failed in open(): Permission denied at line 8" and
: >> nothing goes into the file called out ...

: >Apparently somewhere down that list of files you have a file
: >with permissions set so the user you are running this under cannot read
: >that file.

: this is odd as I am using Windows 98(SE) and have checked that all
: files have only the A atribute .. ?

Win98 will also say "Permission denied" when your program tries to open
a directory as if it is a file.
 
G

Geoff Cox

: >Geoff Cox wrote:

: >>>open IN, $_ or die "can't open $_: $!\n";

: >> This has got me confused ! If I use the check for opening the file I
: >> get error message "failed in open(): Permission denied at line 8" and
: >> nothing goes into the file called out ...

: >Apparently somewhere down that list of files you have a file
: >with permissions set so the user you are running this under cannot read
: >that file.

: this is odd as I am using Windows 98(SE) and have checked that all
: files have only the A atribute .. ?

Win98 will also say "Permission denied" when your program tries to open
a directory as if it is a file.

Jay,

the email addresses are in files which are in a folder with no
sub-folders so it cannot be this, can it?

Geoff
 
G

Geoff Cox

Please spend the time to format your code sensibly.

Many people will just move on to help someone else who has
done what they can to make it easier to give an answer.

Tad,

sorry about that - would you recommend any program which automates the
formatting?

Cheers

Geoff
 
J

Jay Tilton

: On Sat, 13 Sep 2003 20:51:28 GMT, (e-mail address removed) (Jay Tilton)
: wrote:

: >Win98 will also say "Permission denied" when your program tries to open
: >a directory as if it is a file.
:
: the email addresses are in files which are in a folder with no
: sub-folders so it cannot be this, can it?

Instead of asking me, ask your program to give you that information.

find ( sub {
if( -d $_ ) {
warn "'$_' is a subdirectory.\n";
return;
}
# rest of subroutine goes here
}, $dir);
 
G

Geoff Cox

: On Sat, 13 Sep 2003 20:51:28 GMT, (e-mail address removed) (Jay Tilton)
: wrote:

: >Win98 will also say "Permission denied" when your program tries to open
: >a directory as if it is a file.
:
: the email addresses are in files which are in a folder with no
: sub-folders so it cannot be this, can it?

Instead of asking me, ask your program to give you that information.

find ( sub {
if( -d $_ ) {
warn "'$_' is a subdirectory.\n";
return;
}
# rest of subroutine goes here
}, $dir);

ok !

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
474,138
Messages
2,570,803
Members
47,349
Latest member
jojonoy597

Latest Threads

Top