Steve Grazzini <[EMAIL PROTECTED]> writes:

> On Sun, Jun 29, 2003 at 01:37:39PM -0700, Harry Putnam wrote:
>> 
>> From perldoc it appears that something like this should work but it
>> doesn't either:
>> my ($wtr, $wdr, $err, @item, $pid);
>> 
>> $pid = open2($wtr, $rdr,  'perl -e ', 'use CPAN;',  'CPAN::Shell->i;');
>
> This is the equivalent of:
>
>   % perl -e 'use CPAN;' 'CPAN::Shell->i;'
>
> Which doesn't work either (each bit of code needs a '-e').
>
> Anyway, I missed the opening of this thread, but it seems 
> strange to open up a subprocess like this.  Can't you just
> use the CPAN::Shell methods right in the main script?

Thanks for your interest:
I've looked up an answer to this problem that I finally remembered
having asked about a year ago on the moderated perl newsgroup.
comp.lang.perl.moderated.

I got good answers there but had forgotten them so I now have a
handle on this.  (I'll include a snippet below)

The original script tried to redirect the output of cpan commands and
was not able to,  so the thread has been about how to do that.

>From OP:
   How do I go about making the output from 
   CPAN::Shell->i; go into a file handle
   Instead of STDOUT like it does in this formulation:
   
   $target = "somefile";
   if($opt_r){
     open(FH,">$target") or die "Cannot open $target: $!";  
       print FH CPAN::Shell->i;  
     } 
   } 

One way to do it [although it doesn't write into a file handle as
stipulated in the OP.  It does capture the data and allow
manipulation]:

(from a response on comp.lang.perl.moderated)
[Not sure if the author would want his name included so left it
unattributed]

[...]
Since CPAN::Shell->i writes to stdout, you need to fork and have
another process do the work. Something like this:

   #!/usr/local/bin/perl
   use CPAN;
   
   my $pid = open(CPAN_I, "-|");
   die "Can't fork: $!" if not defined $pid;
   
   if ($pid == 0) # child
   {
       CPAN::Shell->i;
       exit 0;
   }
   
   my @cpan_lines = <CPAN_I>;
   close CPAN_I or warn "child exited: $?";

## I've added a dopey for loop [ed -HP]
for (@cpan_lines){
   print "WE got you... haha => $_";
}


[...]


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

Reply via email to