FAQ
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

Search Discussions

  • Rob Dixon at May 11, 2011 at 1:06 pm

    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
  • Irfan Sayed at May 11, 2011 at 1:14 pm
    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 <rob.dixon@gmx.com>
    To: Perl Beginners <beginners@perl.org>
    Cc: Irfan Sayed <irfan_sayed2002@yahoo.com>
    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
  • Rob Dixon at May 11, 2011 at 2:39 pm

    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
  • Rob Dixon at May 11, 2011 at 2:46 pm

    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

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupbeginners @
categoriesperl
postedMay 11, '11 at 12:12p
activeMay 11, '11 at 2:46p
posts5
users2
websiteperl.org

2 users in discussion

Rob Dixon: 3 posts Irfan Sayed: 2 posts

People

Translate

site design / logo © 2021 Grokbase