command output
am 11.05.2011 14:12:10 von Irfan Sayed
--0-671053795-1305115930=:45313
Content-Type: text/plain; charset=us-ascii
Hi,
i am running system command using following line in perl script
($stdout,$stderr) = capture_exec("$workspace\\Dev_Tools\\Nant\\bin\\nant -buildfile:abc.build $target");
print "$stdout\n";
print "$stderr\n";
now the issue is , the contents of these variable gets printed on console after this command gets executed
i need to print the output of this command as it is executed
what does it mean that, whenever there is a new entry in these variable, it should print that immediately on console
please suggest
regards
irfan
--0-671053795-1305115930=:45313--
Re: command output
am 11.05.2011 15:06:16 von Rob Dixon
On 11/05/2011 13:12, Irfan Sayed wrote:
>
> i am running system command using following line in perl script
>
> ($stdout,$stderr) = capture_exec("$workspace\\Dev_Tools\\Nant\\bin\\nant -buildfile:abc.build $target");
> print "$stdout\n";
> print "$stderr\n";
>
>
> now the issue is , the contents of these variable gets printed on console after this command gets executed
> i need to print the output of this command as it is executed
> what does it mean that, whenever there is a new entry in these variable, it should print that immediately on console
Hi Irfan
You don't show the code for capture_exec, but your purpose is probably
best served by a pipe open on the command's output. To see the output in
real time you would also need to enable autoflush by setting the system
variable $| to true. Something like this:
use strict;
use warnings;
my $cmd = "$workspace\\Dev_Tools\\Nant\\bin\\nant -buildfile:abc.build
$target";
open my $ch, "$cmd |" or die $!;
local $| = 1;
while (my $line = <$ch>) {
print $line;
}
If you want the output to stderr to be merged with the standard output,
then you could change the open statement to look like this:
open my $ch, "$cmd 2>&1 |" or die $!;
HTH,
Rob
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
Re: command output
am 11.05.2011 15:14:52 von Irfan Sayed
--0-499813516-1305119692=:13362
Content-Type: text/plain; charset=us-ascii
thanks rob.
is there any another shorter way??
capture_exec is just external perl module from cpan. i am just using it as it is
regards,
irfan
________________________________
From: Rob Dixon
To: Perl Beginners
Cc: Irfan Sayed
Sent: Wednesday, May 11, 2011 6:37 PM
Subject: Re: command output
You chose to allow Rob Dixon (rob.dixon@gmx.com) even though this message failed authentication
Click to disallow
--0-499813516-1305119692=:13362--
Re: command output
am 11.05.2011 16:39:28 von Rob Dixon
On 11/05/2011 14:14, Irfan Sayed wrote:
> On 11/05/2011 14:06, Rob Dixon wrote:
>> On 11/05/2011 13:12, Irfan Sayed wrote:
>>>
>>> i am running system command using following line in perl script
>>>
>>> ($stdout,$stderr) =
>>> capture_exec("$workspace\\Dev_Tools\\Nant\\bin\\nant
>>> -buildfile:abc.build $target");
>>> print "$stdout\n";
>>> print "$stderr\n";
>>>
>>>
>>> now the issue is , the contents of these variable gets printed on
>>> console after this command gets executed
>>> i need to print the output of this command as it is executed
>>> what does it mean that, whenever there is a new entry in these
>>> variable, it should print that immediately on console
>>
>> You don't show the code for capture_exec, but your purpose is probably
>> best served by a pipe open on the command's output. To see the output in
>> real time you would also need to enable autoflush by setting the system
>> variable $| to true. Something like this:
>>
>>
>> use strict;
>> use warnings;
>>
>> my $cmd = "$workspace\\Dev_Tools\\Nant\\bin\\nant -buildfile:abc.build $target";
>> open my $ch, "$cmd |" or die $!;
>>
>> local $| = 1;
>>
>> while (my $line = <$ch>) {
>> print $line;
>> }
>>
>>
>> If you want the output to stderr to be merged with the standard output,
>> then you could change the open statement to look like this:
>>
>>
>> open my $ch, "$cmd 2>&1 |" or die $!;
>
> thanks rob.
> is there any another shorter way??
> capture_exec is just external perl module from cpan. i am just using it as it is
Ah, then I assume you are using IO::CaptureOutput? We cannot know the
functionality of all the CPAN modules, so please say if you are using a
module. Better still, post the entirety of your code.
I am surprised you think this solution is too long for you. A large
proportion of it is the command line that you posted, followed by an
open, enabling flushing, and a loop that must exist for any solution.
Maybe it would help if I rewrote it as a subroutine?
use strict;
use warnings;
display_exec("$workspace\\Dev_Tools\\Nant\\bin\\nant -buildfile:abc.build $target");
sub display_exec {
my $cmd = shift;
open my $ch, "$cmd |" or die $!;
local $| = 1;
print while <$ch>;
}
Please can you explain more about your problem, and what you want to do
with the output from the shell command?
Rob
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
Re: command output
am 11.05.2011 16:46:48 von Rob Dixon
On 11/05/2011 14:14, Irfan Sayed wrote:
> thanks rob.
> is there any another shorter way??
> capture_exec is just external perl module from cpan. i am just using it as it is
If all you want is to be able to see the output from the command, have
you considered
system("$workspace\\Dev_Tools\\Nant\\bin\\nant -buildfile:abc.build $target");
which forks a process with the same stdout and stderr? Or even
exec("$workspace\\Dev_Tools\\Nant\\bin\\nant -buildfile:abc.build $target");
if the command is the last thing for the Perl script to do?
Rob
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/