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);
....