Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

STDOUT qustion

by Noame (Beadle)
on Jun 14, 2009 at 07:10 UTC ( [id://771355]=perlquestion: print w/replies, xml ) Need Help??

Noame has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I’ve perl program P1.pl which calling to P2.pl.
Once P2.pl is starting to run I’m printing a message from P1.pl but the output from P2.pl which printings to the screen disappears.
Just in the end of P2.pl all the STDOUT buffer is printing to the screen
Following is my code from P1.pl:
$Cmd = "$HOME/bin/P2.pl"; die print "\n\n-E- Files: '$Cmd does not exist.\n" if (-e $Cmd +); print "\n-I- Executing:\n\t$Cmd\n"; $status = system ($Cmd); if ($status) { my $errMsg = "-E- Failed to execute:\n\t$Cmd "; print "\n\n$errMsg\n"; killBuild ("$errMsg"); exit 1; } else { print "\n-I- Script:\n\t'$Cmd\n Completed successfully.\n"; }

Please advice,
Thanks

Replies are listed 'Best First'.
Re: STDOUT qustion
by CountZero (Bishop) on Jun 14, 2009 at 07:44 UTC
    Perhaps you are Suffering from Buffering?

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: STDOUT qustion
by linuxer (Curate) on Jun 14, 2009 at 11:44 UTC

    What's that die print "..." statement? die already prints the string, no need for a dedicated print.

    Your script dies, when P2.pl exists although the message says the contrary.

    This code worked fine for me:

    #!/usr/bin/perl # # p2.pl # use strict; use warnings; # disable buffering of STDOUT; see the already mentioned "Suffering fr +om Buffering" $| = 1; my $string = "Hello World!"; for ( split //, $string ) { print $_; sleep 1; } print "\n"; __END__
    #! /usr/bin/perl # # p1.pl # use strict; use warnings; my $cmd = '/path/to/p2.pl'; die "(E) '$cmd' does not exist!\n" if !-e $cmd; print "(I) Executing: $cmd\n"; if ( system( $cmd ) == 0 ) { print "(I) '$cmd' completed successfully.\n"; } else { my $err_msg = "(E) failed to execute: $cmd\n"; warn $err_msg; killBuild( $err_msg ); exit 1; } __END__

    My result:

    (I) Executing: /tmp/p2.pl Hello World! (I) '/tmp/p2.pl' completed successfully.
Re: STDOUT qustion
by baxy77bax (Deacon) on Jun 14, 2009 at 08:37 UTC
    well if the stdout is something you can,t live without. one way to overcome this problem is piping the stdout from p2 to p1 and then printing it directly to screen like:
    open (STDIN,"perl p2.pl|") || die "$!"; while(<STDIN>){ print $_; }
    or if it is something small that p2 produces you can capture it with
    my $capture = qx($Cmd); print $capture . "\n";
    and print it

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://771355]
Approved by ikegami
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (7)
As of 2024-04-18 11:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found