Folks, 
    Consider this code snippet: 

Here $ParseFile is a plain-text file, which could be gzipped, bzipped, or
not compressed at all. I'd like my program to determine this, open the
file, and cat it to another program called discretise. 

In the code below, 

my(@PARSELIST) = @ARGV;         #  This would be the .par files.
my($ParseFile);
for $ParseFile (@PARSELIST){
    chomp $ParseFile;
    my($date,$name,$suffix) = &FileTest($ParseFile);  # uses File::Basename 
    if($suffix eq ".bz2"){
        system("bunzip2 -c $ParseFile | discretise $delta $BOD $EOD > ${OUTFILE}");
    }elsif($suffix eq ".gz"){
        system("gunzip -c $ParseFile | discretise $delta $BOD $EOD > ${OUTFILE}");
    }elsif($suffix eq ".par"){
        system("cat $ParseFile | discretise $delta $BOD $EOD > ${OUTFILE}");
    }


As you can see, I've got three system commands to run on $ParseFile,
depending on it's extn (.par/.gz/.bz2).  

I don't know how I should ask this, but can I 

    1. open $ParseFile and assign a filehandle to it  (IN), say.
    2. open $Outfile and assign a filehandle to it (OUT), 
    3. determine the extension of the input file,

And this is the important part:     
    4. Depending on the extension, cat, gunzip -c, or bunzip -c the input
        file to the output file, using the _filehandle_, without a system
        command. 

    Is this possible in Perl, if so how to do it? And is it necessary? 

For instance, I could have,     

    if($suffix eq ".bz2"){
        open(IN, "bunzip2 -c $ParseFile|")
            ||die "Could not open $ParseFile: $!";
    }elsif($suffix eq ".gz"){
        open(IN, "gunzip -c $ParseFile|")
            ||die "Could not open $ParseFile: $!";
    }elsif($suffix eq ".par"){
        open(IN, "<$ParseFile")
            || die "Could not open $ParseFile: $!";
    }

And then I open an output file: 

    
    open(OUT, "| discretise delta BOD EOD > $OutFile") || die "Sorry :$!"; 

Problem: How do I channel the input from $ParseFile to the OUT filehandle? 

Please explain in --verbose mode! I do not know these things. :)

-tir


TIA, 






-- 
 Tirthankar, IGIDR. 
 +91-22-8400919 x275 (r), x593(o), x542(CFL). 
 http://www.igidr.ac.in/~tir

  ACKNOWLEDGE, v.t.  To confess.  Acknowledgement of one another's
  faults is the highest duty imposed by our love of truth.


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to