For Programmers: Free Programming Magazines  


Home > Archive > PERL Miscellaneous > March 2004 > Cannot capture message from external program.









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Cannot capture message from external program.
Hon Seng Phuah

2004-03-29, 9:34 pm

I have the following code:

$file_name = $_[0];
@output_result = `cvs commit -m\"\" $file_name`;
print "Here: @output_result\n";

or

$file_name = $_[0]
open(OUTPUT, "cvs commit =m\"\" $file_name |");
@output_result = <OUTPUT>;
close(OUTPUT);
print "Here: @output_result\n";

Both code displays:

cvs commit: Examining .
Here:
Use of uninitialized value at test.pl line #.

How do I capture the output error message, "cvs commit: Examining ."
and store them into @output_message.
Sherm Pendley

2004-03-30, 2:33 am

Hon Seng Phuah wrote:

> How do I capture the output error message, "cvs commit: Examining ."


Your code is capturing stdout, but the message you want to grab is
apparently being printed to stderr.

Have a look at the core module IPC::Open3.

sherm--

--
Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org
Christian Winter

2004-03-30, 2:33 am

"Hon Seng Phuah" schrieb:
[...]
> Both code displays:
>
> cvs commit: Examining .
> Here:
> Use of uninitialized value at test.pl line #.
>
> How do I capture the output error message, "cvs commit: Examining ."
> and store them into @output_message.


Seems like cvs writes its output to stderr instead
of stdout. "perldoc -q stderr" should give you some
possible solutions.

HTH
-Christian
Simon Taylor

2004-03-30, 2:33 am

Hon Seng Phuah wrote:
> I have the following code:
>
> $file_name = $_[0];
> @output_result = `cvs commit -m\"\" $file_name`;
> print "Here: @output_result\n";


[snip]

> Both code displays:
>
> cvs commit: Examining .
> Here:
> Use of uninitialized value at test.pl line #.
>
> How do I capture the output error message, "cvs commit: Examining ."
> and store them into @output_message.


You could try the following, (the '2>&1' is a shell construction that
ensures that anything printed to the standard error stream is
redirected to standard out):


#!/usr/bin/perl

use strict;
use warnings;

my $file_name = 'somefile';
my @output_result = `cvs commit -m\"\" $file_name 2>&1`;
print "Here: @output_result\n";


Hope this helps,

Simon Taylor
--
Unisolve Pty Ltd - Melbourne, Australia

Himanshu Garg

2004-03-30, 4:42 am

hsphuah@usa.com (Hon Seng Phuah) wrote in message news:<3898598f.0403291739.52acb6e9@posting.google.com>...
> I have the following code:
>
> $file_name = $_[0];
> @output_result = `cvs commit -m\"\" $file_name`;
> print "Here: @output_result\n";
>
> or
>
> $file_name = $_[0]
> open(OUTPUT, "cvs commit =m\"\" $file_name |");
> @output_result = <OUTPUT>;
> close(OUTPUT);
> print "Here: @output_result\n";
>
> Both code displays:
>
> cvs commit: Examining .
> Here:
> Use of uninitialized value at test.pl line #.
>
> How do I capture the output error message, "cvs commit: Examining ."
> and store them into @output_message.


You may want to try Expect.pm from CPAN.

Something like the following works on my system. This is derived from
the ftp example that comes with the distribution. :-

use Expect;

# Start the cvs process.
my $filename = "himanshu.txt";
my $cvs = Expect->spawn("cvs commit -m\"\" $filename") or die
"Couldn't spawn cvs, $OS_ERROR";

# Look for error message. On my box this looks like:
my @output = $cvs->expect(30, "-re", "cvs.*\n");
print "Here: $output[2]";
print "Hereafter: $output[4]";

++imanshu.
Tad McClellan

2004-03-30, 8:35 am

Simon Taylor <simon@unisolve.com.au> wrote:
> Hon Seng Phuah wrote:




[color=darkred]
> You could try the following, (the '2>&1' is a shell construction that
> ensures that anything printed to the standard error stream is
> redirected to standard out):



> Hope this helps,



Hope the OP already knew that, as it is covered in the documentation
for the function he is using.

I wouldn't want to think that he would ask thousands of people
around the world without looking a bit himself first...


--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
Simon Taylor

2004-03-30, 5:39 pm

Tad McClellan wrote:

> Hope the OP already knew that, as it is covered in the documentation
> for the function he is using.
>
> I wouldn't want to think that he would ask thousands of people
> around the world without looking a bit himself first...


Yes but first the OP would need to know that back ticks are
documented in the perlop page. That's quite a counter-intuitive leap
(in my experience) for people who are new to perl.

All the best.

Simon


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com