You might want to look at IPC::Open2

You can open a system command with an INPUT pipe and OUTPUT pipe.

Here is how I used it.

open2(*READ, *ZIPIT, "/apps/bin/zip $FORM{'zipfile'} -@ 2>&1");
READ is the input pipe
ZIPIT is the output pipe

Perl Gurus, Yeah I should have used the ZIP module.. I am too lazy to change
it now.


> -----Original Message-----
> From: Tirthankar C. Patnaik [mailto:[EMAIL PROTECTED]]
> Sent: Friday, April 12, 2002 1:04 PM
> To: [EMAIL PROTECTED]
> Subject: Input | Program | Output : help 
> 
> 
> 
> 
> 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]
> 

----------------------------------------------------------------------------
--------------------
The views and opinions expressed in this email message are the sender's
own, and do not necessarily represent the views and opinions of Summit
Systems Inc.


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

Reply via email to