luckysing has asked for the wisdom of the Perl Monks concerning the following question:

What I want to do is that I have a tar file which in turn has many .tgz files inside it .i want to extract the big tar file and then for each .tgz file i want to run 4 Perl scripts.after i run these scripts i get 2 text files which are of interest to me .So if i iterate for 20 .tgz files i should get 40 .txt files in all but the problem with this code is iam getting text files for only the first run?that is i am getting only two text files from the entire process

#!/usr/bin/perl -w use strict; use Archive::Tar; my (@files,$i,$name,$ext,@filelist,@textfiles); my $tar = Archive::Tar->new(); $tar->read('some.tar'); $tar->extract(); my $tar1 = Archive::Tar->new(); @files=<*.tgz>; for $i(0..$#files) { $tar1->read("$files[$i]"); $tar1->extract(); #untar .tgz files @filelist=<*.txt>; system("perl script1.pl "); #run 4 scripts on the files and get +the data #from files system("perl script2.pl "); system("perl script3.pl "); system("perl script4.pl "); @textfiles=("file1.txt","file2.txt"); unlink @textfiles; #remove unwanted textfiles from the director +y unlink @filelist; #remove the extracted files }

Replies are listed 'Best First'.
Re: call multiple perl scripts from within a perl scripts ?
by BrowserUk (Patriarch) on Aug 03, 2010 at 23:57 UTC
    the problem with this code is iam getting text files for only the first run?that is i am getting only two text files from the entire process

    Is is possible that the second run overwrites the files from the first run. And the third run overwrites the files from the second. And so on?

      Thanks for the suggestion that was exactly what I was doing the text files were getting overwritten since two of the Perl scripts were not able to get the file names properly.
Re: call multiple perl scripts from within a perl scripts ?
by cdarke (Prior) on Aug 04, 2010 at 06:14 UTC
    You don't appear to be passing a file-name to the scripts, therefore you are probably using fixed filenames, unless you are generating them in some way in the scripts. Therefore it is most likely that you are overwriting the files on each loop iteration.

    A few other observations:
    The loop is probably bettern written as:
    for my $file (<*.tgz>)) { $tar1->read($file); ... }
    I don't see any error checking, anywhere. What if you cannot open the tar file? What if @filelist does not have the right number of files in it? What if any of the perl scripts fail? Some of these problems could cause your symptoms, possibily silently (depending on your scripts).
      Thanks for the suggestion that was exactly what I was doing wrong the text files were getting overwritten since two of the Perl scripts were not able to get the file names properly.
Re: call multiple perl scripts from within a perl scripts ?
by ahmad (Hermit) on Aug 04, 2010 at 01:25 UTC

    As I can see in your script you're emptying the array and reset it for the current extracted files only @filelist=<*.txt>; .. And in the final step of each run you are making sure those files are removed unlink @filelist;

    I would suggest that you re-write this script in a proper way, with some good comments.

    And you don't really need to run 4 separate files ... you can write 4 subroutines to process whatever you want.

    Tell us what you want to accomplish and you might get better help OR at least some good suggestions.

      I think writing 4 sub routines is a great idea rather than 4 scripts which work on the same data set.I will work on that. Thanks for the hint!!