Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

Getting stderr when using '-|'




I want to use exec($prog, @args) to execute a system command without
worrying about shell escapes, but I also want to capture both stdout
and stderr from this system command.  If I only wanted stdout, I
could do something like this

die "Can't fork: $!" unless defined(my $pid = open(my $read_child, '-|'));

if ($pid == 0) {
exec($prog, @argv) or die "Can't exec $prog: $!\n";
exit 0; # superfluous
}

my $output = do { local $/ = undef;, <$read_child> };
close $read_child;
if ($?) {
die "Child failed: $?"
}

In the code above, $output captures the stdout from the command
run through exec.  But how does one capture stderr as well?  Not
surprisingly adding the string '2>&1' to exec's arguments fails
because exec just passes this string along as one more argument to
$prog.

Any help would be much appreciated.

bill



Report this thread to moderator Post Follow-up to this message
Old Post
bill
12-30-04 08:57 AM


Re: Getting stderr when using '-|'
In <cr0539$so4$1@reader1.panix.com> bill <please_post@nomail.edu> writes:

>I want to use exec($prog, @args) to execute a system command without
>worrying about shell escapes, but I also want to capture both stdout
>and stderr from this system command.  If I only wanted stdout, I
>could do something like this

>die "Can't fork: $!" unless defined(my $pid = open(my $read_child, '-|'));

>if ($pid == 0) {

open (STDERR, '>&STDOUT') or die "Can't dup STDOUT\n";

>  exec($prog, @argv) or die "Can't exec $prog: $!\n";
>  exit 0; # superfluous
>}

>my $output = do { local $/ = undef;, <$read_child> };
>close $read_child;
>if ($?) {
>  die "Child failed: $?"
>}



HTH,

Karl

--
Sent from a spam-bucket account; I check it once in a blue moon.  If
you still want to e-mail me, cut out the extension from my address,
and make the obvious substitutions on what's left.

Report this thread to moderator Post Follow-up to this message
Old Post
KKramsch
12-30-04 08:57 AM


Re: Getting stderr when using '-|'
"bill" <please_post@nomail.edu> wrote in message
news:cr0539$so4$1@reader1.panix.com...
>
> I want to use exec($prog, @args) to execute a system command without
> worrying about shell escapes, but I also want to capture both stdout
> and stderr from this system command.


Have you read the appropriate FAQ on this topic?
perldoc -q stderr


Paul Lalli


Report this thread to moderator Post Follow-up to this message
Old Post
Paul Lalli
12-30-04 01:57 PM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

PERL Miscellaneous archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 08:41 AM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.