Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

What is wrong with this script...going no where....

Status
Not open for further replies.

vptl

Technical User
Jan 22, 2004
120
0
0
US
Hello,
Can you please tell me where I am missing something...

The error stats that $file is not initialised while reading instruction for opening DATA.

Thanks.

#!/usr/bin/perl -w
use strict;
use diagnostics;
my $count=0;
open STREAM, "/bin/ls /var/tmp/*SED |" or die "Could not do ls" ;
while (<STREAM>) {
chomp;
my $file;
print "Your Input is $file\n";
open (DATA,"$file");
while(<DATA>) {
chomp;
print;
print (++$count % 3 ? " " : "\n");
close DATA ;
}
}
close STREAM;
 
Got it.!
while (my $file=<STREAM>).
 
Now the other prob...Sorry for asking ebvery bit...but I am going ..to find wwhat is wrong!


This is my code...


#!/usr/bin/perl -w
use strict;
use diagnostics;
my $count=0;
open OUTFILE, "> /var/tmp/BIG_FILE" or die "Cannot create Big file" ;
open STREAM, "/bin/ls /var/tmp/*SED | awk '{print \$9}'|" or die "Could not do ls" ;
while (my $file=<STREAM>) {
chomp $file;
open (DATA,$file);
while(<DATA>) {
chomp;
print OUTFILE;
print OUTFILE (++$count % 3 ? " " : "\n");
}
close DATA ;
}
close STREAM;
close OUTFILE;


And this is the error....
readline() on closed filehandle main::DATA at ./vml1 line 10 (#1)

(W closed) The filehandle you're reading from got itself closed sometime before now.
Check your logic flow.



I have to close DATA steam so that I can get it from next file....? Any help ...? pls....

Thanks.
 
Probably, this may be the problem. You are closing the DATA file handle outside the while loop but open again and again inside the loop. Try to move the close DATA; inside the while loop, or else use a local.

Code:
while (my $file=<STREAM>) {
        chomp $file;
        open (DATA,$file);
        while(<DATA>) {
        chomp;
        print OUTFILE;
        print  OUTFILE (++$count % 3 ? " " : "\n");
           close DATA ;
        }
       }

VaRaKal
 
Okay..now works....
Missed ls -l in my command.

it happens....with me a lot! :)

Thanks everyone for today's help especially My perl instructor Triojan. Thanks.
 
Yes you are correct I had to do that also.
Thanks Varakal.
 
You should ALWAYS <b>or die</b> opened files or you have no idea whether or not the files ever opened.

Code:
open (DATA,$file) or die "Error opening file $file for reading: $!";
 
The filehandle DATA can mean something special, I doubt it's reserved for its special meaning but whenever I see it I think it's being used that way. I'd use another name for the handle

The special meaning, just for the record, is this:

Code:
# this is a complete script

while($f = <DATA>){

[tab]print $f;

}

__END__
one
two
three
four
five
six
seven
eight
nine
ten
etc..

If you just read from the DATA filehandle without using it in an open() function it looks for the token __END__ in the script and reads any data that follows it.

Mike

You cannot really appreciate Dilbert unless you've read it in the
original Klingon.

Want great answers to your Tek-Tips questions? Have a look at faq219-2884

 
It looks for __END__ or __DATA__ (just tried it with both). I never knew about __END__. Thanks Mike...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top