Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > capturing STDOUT from and piped "into" program

Reply
Thread Tools

capturing STDOUT from and piped "into" program

 
 
Tim
Guest
Posts: n/a
 
      05-01-2007
Hi

I've read a lot good info on this list about capturing STDOUT but I'm
still having a problems grasping it. I think I'm missing something. I
am trying to capture the output from the "$p4 $spectype -i" command
and place it in a $scalar. I know I can't redirect directly into a
scalar.

open(FULLSPEC,"|$p4 $spectype -i");
print FULLSPEC @fullspec;
close(FULLSPEC);

Can someone please show me an example on how I can directly place the
output of the above example into a scalar variable?


Up to now I've cludged it by placing the output into a file and then
reading it however it's ugly.

open(FULLSPEC,"|$p4 $spectype -i > $outfile");
print FULLSPEC @fullspec;
close(FULLSPEC);

open(OUTPUT,"<$outfile");
$specout = <OUTPUT>;
close(OUTPUT);
chomp($specout);
unlink($outfile);
return $specout;

 
Reply With Quote
 
 
 
 
Paul Lalli
Guest
Posts: n/a
 
      05-01-2007
On May 1, 4:33 pm, Tim <(E-Mail Removed)> wrote:
> Hi
>
> I've read a lot good info on this list about capturing STDOUT but I'm
> still having a problems grasping it. I think I'm missing something. I
> am trying to capture the output from the "$p4 $spectype -i" command
> and place it in a $scalar. I know I can't redirect directly into a
> scalar.
>
> open(FULLSPEC,"|$p4 $spectype -i");
> print FULLSPEC @fullspec;
> close(FULLSPEC);
>
> Can someone please show me an example on how I can directly place the
> output of the above example into a scalar variable?
>
> Up to now I've cludged it by placing the output into a file and then
> reading it however it's ugly.
>
> open(FULLSPEC,"|$p4 $spectype -i > $outfile");
> print FULLSPEC @fullspec;
> close(FULLSPEC);
>
> open(OUTPUT,"<$outfile");
> $specout = <OUTPUT>;
> close(OUTPUT);
> chomp($specout);
> unlink($outfile);
> return $specout;


perldoc -q pipe
Found in /software/perl-5.8.5-0/pkg/lib/5.8.5/pod/perlfaq8.pod
How can I open a pipe both to and from a command?

http://perldoc.perl.org/IPC/Open2.html

[untested]
use IPC::Open2;
my $pid = open2(my $read_fh, my $write_fh, "$p4 $spectype -i");
print $write_fh @fullspec;
my $specout = do { local $/; <$read_fh> };

Hope this helps,
Paul Lalli

 
Reply With Quote
 
 
 
 
xhoster@gmail.com
Guest
Posts: n/a
 
      05-01-2007
Tim <(E-Mail Removed)> wrote:
> Hi
>
> I've read a lot good info on this list about capturing STDOUT but I'm
> still having a problems grasping it. I think I'm missing something. I
> am trying to capture the output from the "$p4 $spectype -i" command
> and place it in a $scalar. I know I can't redirect directly into a
> scalar.
>
> open(FULLSPEC,"|$p4 $spectype -i");
> print FULLSPEC @fullspec;
> close(FULLSPEC);


One possibility, depending on what kinds of characters can exist in
@fullspec, would be:

my $output = `echo '@fullspec'|$p4 $spectype -i`;

For portability and also for safety (WRT weird characters in @fullspec) you
could use IPC::Open2, but you have to be careful to handle buffering to
avoid deadlock. Deadlock should not be a problem either if $p4 reads all
of the input before generating any output, or if join (" ", @fullspec) is
small. On many systems, small means less than 4096 bytes.

Perhaps a better answer would be IPC::Run:

my ($out,$err);
my $in=join " ", @fullspec;

IPC::Run::run([$p4, $spectype, '-i'],\$in,\$out,\$err) or die $!;

print $out;

Xho

--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
 
Reply With Quote
 
Tim
Guest
Posts: n/a
 
      05-02-2007
On May 1, 2:48 pm, (E-Mail Removed) wrote:
> Tim <(E-Mail Removed)> wrote:
> > Hi

>
> > I've read a lot good info on this list about capturing STDOUT but I'm
> > still having a problems grasping it. I think I'm missing something. I
> > am trying to capture the output from the "$p4 $spectype -i" command
> > and place it in a $scalar. I know I can't redirect directly into a
> > scalar.

>
> > open(FULLSPEC,"|$p4 $spectype -i");
> > print FULLSPEC @fullspec;
> > close(FULLSPEC);

>
> One possibility, depending on what kinds of characters can exist in
> @fullspec, would be:
>
> my $output = `echo '@fullspec'|$p4 $spectype -i`;
>
> For portability and also for safety (WRT weird characters in @fullspec) you
> could use IPC::Open2, but you have to be careful to handle buffering to
> avoid deadlock. Deadlock should not be a problem either if $p4 reads all
> of the input before generating any output, or if join (" ", @fullspec) is
> small. On many systems, small means less than 4096 bytes.
>
> Perhaps a better answer would be IPC::Run:
>
> my ($out,$err);
> my $in=join " ", @fullspec;
>
> IPC::Run::run([$p4, $spectype, '-i'],\$in,\$out,\$err) or die $!;
>
> print $out;
>
> Xho
>
> --
> --------------------http://NewsReader.Com/--------------------
> Usenet Newsgroup Service $9.95/Month 30GB



Hi Paul and Xho

I really appreciate your responses. I initially tried Paul's solution.
It did indeed work on my OSX and Linux box. I was singing praises to
Paul but then I tried it on Windows XP with Activestate...
unfortunately it locked up.
I then went to try your solution.. It appears that IPC::Run is only
available in a separate package. I forgot to mention one detail ....
I'm attempting to write a perl benchmark test that customers could use
in their own perl environment without having them add additional
packages.

I'm using v5.8.5 on Linux and 5.8.8 Activestate on Windows. Knowing
what I just mentioned, any other ideas? Once again, thank you guys
VERY much for the info.

Tim

 
Reply With Quote
 
xhoster@gmail.com
Guest
Posts: n/a
 
      05-02-2007
Tim <(E-Mail Removed)> wrote:
> On May 1, 2:48 pm, (E-Mail Removed) wrote:
> > Tim <(E-Mail Removed)> wrote:
> > > Hi

> >
> > > I've read a lot good info on this list about capturing STDOUT but I'm
> > > still having a problems grasping it. I think I'm missing something. I
> > > am trying to capture the output from the "$p4 $spectype -i" command
> > > and place it in a $scalar. I know I can't redirect directly into a
> > > scalar.

> >
> > > open(FULLSPEC,"|$p4 $spectype -i");
> > > print FULLSPEC @fullspec;
> > > close(FULLSPEC);

> >
> > One possibility, depending on what kinds of characters can exist in
> > @fullspec, would be:
> >
> > my $output = `echo '@fullspec'|$p4 $spectype -i`;
> >
> > For portability and also for safety (WRT weird characters in @fullspec)
> > you could use IPC::Open2, but you have to be careful to handle
> > buffering to avoid deadlock. Deadlock should not be a problem either
> > if $p4 reads all of the input before generating any output, or if join
> > (" ", @fullspec) is small. On many systems, small means less than 4096
> > bytes.
> >
> > Perhaps a better answer would be IPC::Run:
> >
> > my ($out,$err);
> > my $in=join " ", @fullspec;
> >
> > IPC::Run::run([$p4, $spectype, '-i'],\$in,\$out,\$err) or die $!;
> >
> > print $out;
> >
> > Xho
> >
> > --
> > --------------------http://NewsReader.Com/--------------------
> > Usenet Newsgroup Service $9.95/Month 30GB

>
> Hi Paul and Xho
>
> I really appreciate your responses. I initially tried Paul's solution.
> It did indeed work on my OSX and Linux box. I was singing praises to
> Paul but then I tried it on Windows XP with Activestate...
> unfortunately it locked up.


How big was "@fullspec"? If it was big, then it maybe a buffering issue
that can solved with select. Even if it were not big, it still might be
one, but is likely.

> I then went to try your solution.. It appears that IPC::Run is only
> available in a separate package.


IPC::Run seems to be pure perl, and the source code is available, and thus
the source of it can be incorporated anywhere. I would assume its terms of
use would allow that, but I didn't check the license file--you should if
you want to use it that way. In any event, the ideas behind the package
are surely available to you.

Xho

--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
 
Reply With Quote
 
Tim
Guest
Posts: n/a
 
      05-02-2007
On May 1, 2:35 pm, Paul Lalli <(E-Mail Removed)> wrote:
> On May 1, 4:33 pm, Tim <(E-Mail Removed)> wrote:
>
>
>
> > Hi

>
> > I've read a lot good info on this list about capturing STDOUT but I'm
> > still having a problems grasping it. I think I'm missing something. I
> > am trying to capture the output from the "$p4 $spectype -i" command
> > and place it in a $scalar. I know I can't redirect directly into a
> > scalar.

>
> > open(FULLSPEC,"|$p4 $spectype -i");
> > print FULLSPEC @fullspec;
> > close(FULLSPEC);

>
> > Can someone please show me an example on how I can directly place the
> > output of the above example into a scalar variable?

>
> > Up to now I've cludged it by placing the output into a file and then
> > reading it however it's ugly.

>
> > open(FULLSPEC,"|$p4 $spectype -i > $outfile");
> > print FULLSPEC @fullspec;
> > close(FULLSPEC);

>
> > open(OUTPUT,"<$outfile");
> > $specout = <OUTPUT>;
> > close(OUTPUT);
> > chomp($specout);
> > unlink($outfile);
> > return $specout;

>
> perldoc -q pipe
> Found in /software/perl-5.8.5-0/pkg/lib/5.8.5/pod/perlfaq8.pod
> How can I open a pipe both to and from a command?
>
> http://perldoc.perl.org/IPC/Open2.html
>
> [untested]
> use IPC::Open2;
> my $pid = open2(my $read_fh, my $write_fh, "$p4 $spectype -i");
> print $write_fh @fullspec;
> my $specout = do { local $/; <$read_fh> };
>
> Hope this helps,
> Paul Lalli



Hi Paul

I'm not sure whether you saw my respone. Thaniks for your input! - Tim

#####
Hi Paul and Xho

I really appreciate your responses. I initially tried Paul's solution.
It did indeed work on my OSX and Linux box. I was singing praises to
Paul but then I tried it on Windows XP with Activestate...
unfortunately it locked up.
I then went to try your solution.. It appears that IPC::Run is only
available in a separate package. I forgot to mention one detail ....
I'm attempting to write a perl benchmark test that customers could use
in their own perl environment without having them add additional
packages.

I'm using v5.8.5 on Linux and 5.8.8 Activestate on Windows. Knowing
what I just mentioned, any other ideas? Once again, thank you guys
VERY much for the info.

Tim

 
Reply With Quote
 
Tim
Guest
Posts: n/a
 
      05-02-2007

> How big was "@fullspec"? If it was big, then it maybe a buffering issue
> that can solved with select. Even if it were not big, it still might be
> one, but is likely.
>
> > I then went to try your solution.. It appears that IPC::Run is only
> > available in a separate package.

>
> IPC::Run seems to be pure perl, and the source code is available, and thus
> the source of it can be incorporated anywhere. I would assume its terms of
> use would allow that, but I didn't check the license file--you should if
> you want to use it that way. In any event, the ideas behind the package
> are surely available to you.
>
> Xho
>


Hi Xho

The input to @fullspec can vary. It can be VERY large (it is a source
control changelist for a branch). It can be 10,000 lines depending on
the dataset. The output is only one line.. the change number. Here is
a input file.

$ ls -l out
-rw-r--r-- 1 me me 133246 May 2 11:48 out

$ wc out
1454 4406 133246 out


dataset sample:

//depot/r45.0.0/0Jam/MAIN/src/RELNOTES # branch
//depot/r45.0.0/0Jam/MAIN/src/command.c # branch
//depot/r45.0.0/0Jam/MAIN/src/command.h # branch
//depot/r45.0.0/0Jam/MAIN/src/compile.c # branch
//depot/r45.0.0/0Jam/MAIN/src/compile.h # branch
//depot/r45.0.0/0Jam/MAIN/src/execcmd.h # branch

I'll look into IPC:Run a bit closer - Thx! Tim

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
capturing stdout and stderr? Mike Perl Misc 0 08-09-2009 09:33 PM
re-launch piped external program rihad Perl Misc 14 09-25-2007 08:54 AM
Capturing stderr and stdout of a subprocess as a single stream Fuzzyman Python 3 01-07-2007 08:44 PM
capturing stdout from lynx.. sergio@village-buzz.com Python 2 03-13-2006 03:07 PM
Capturing stdout incrementally Moosebumps Python 5 04-07-2004 03:38 PM



Advertisments