shmf has asked for the wisdom of the Perl Monks concerning the following question:
I'm new to Perl language and Linux but I would like to ask for your help.
I'm working in Linux and the files I need to process are in the format "file.gz".
I don't know if my problem is due to Perl or Linux itself so I'm very lost at the moment.
The problem arises when I try to decompress and then open about 300 files with my Perl script. (I need to do that because I'm analyzing some logs and some of the data needs to be kept on memory during the process. Uncompressing all files at once is unthinkable because it would require to much disk space.)
First I tried it, doing in my script: "system zcat file.gz Z file" followed by "open FILEHANDLE MODE" with MODE "<".
As it didn't work as I expected, I tried then with "open FILEHANDLE MODE" with MODE "|", which, as you know, makes the filename to be interpreted as a Linux command which pipes output to my script.
The problem found was exactly the same.
I've created a temporary solution, but it is not acceptable because it needs me to do much manual work.
Here's a more detailed description of my problem and my script:
I created a script which should decompress, open and then delete nearly 400 files.
To do that I use the Perl function "open" with the option "|" after the filename.
In the beginning the script works fine, but after about 300 files processed I get an error on Open function:
"dealError: Could not open file /home/Logs/backup/file.1183204803".
and exits.
I changed my script so I could continuing processing, but the solution I found is not acceptable:
I created the function "dealError" and I call it if the "open" function in my "proc" function returns an error.
What my "dealError" function does is, simply, wait for some text to be entered in the standard input. If that text is "GO" then "dealError" tries to open the file which has failed before.
This allows me to, manually, by the linux shell, decompress the problematic file and then type "GO" in my script shell.
If I do that, my script opens the file and continues execution normally but only until it needs to process another file.
When it does need to zcat and open another file the problem will be exactly the same.
If I keep decompressing manually the files in Linux shell and typing "GO" the script will end to terminate it's work correctly, but it is unacceptable for me do all this manual work for about 100 files!! (I've tested it with less files and it worked.)
So the problem is not with the files, since I can decompress them manually. I thougth that maby the problem could be in "open" or "zcat"...
Can you help me?
Thank you in advance.
(PS: Sorry for my bad English.)
Here's, what I think to be, the relevant part of my script:
(Note: Function dealError is at the bottom since it is not very relevant. I submitted it anyway, in case you wish to look at it.)
Code: ( perl )
use Cwd; my $path=cwd(); sub proc{ my ($file) = @_; # (...) $pathC = $path . "/$file"; open INPUT, "zcat $pathC|" or dealError($cpath); while($line = <INPUT> ){ eval($line); # (…) data proccessing irrelevant to the problem } close INPUT; } sub init{ (...) #Initializing @lista ("system "ls"" plus a bunch of tests) for my $fileName (@lista){ proc($fileName); } } init; #------------------------------------------------------------------ sub dealError{ my ($path) = @_; open(STDIN, "-"); #Opens standard INPUT print "\n!!ATENTION:\n\tdealError: Could not open file $cpath\n"; print "Check the problem with the file and type GO to continue\n"; while($line = <STDIN>){ if ($line =~ m/GO/){ open INPUT, "$cpath" or dealError($path); close STDIN; last; } else { print "Check the problem with the file and type GO to cont +inue\n"; } } }
|
|---|