Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

This sub is at the core of controlling / monitoring all external functionality my Perl scripts will be forced to utilize. It is imperative that I trap and log STDERR which is why I went with Open3. However, Niether of the Camels read clearly to me on bidirectional communication, and just looking at this code I get the feeling I'm setting myself up for headaches long term. It's intended to be handed off to others instead of having to support it myself.

In particular I'm concerned with my assumptions about gathering $stdout and $stderr, and definately have my ears open to all input

# execute() runs a program on any platform and traps # the STDOUT and STDERR. A Calling program must handle # the return value to determine if it worked or not. # if there is a $@ (trapped error) there are BIG problems # There is a danger in overruning the Perl buffers # (if they exceed 255 chars) with either the commandline writer # or the output buffer reader this function gets around this by # (when necessary) creating a batch file in a temporary directory # and then executing that file. sub execute { my $execute = $_[0]; my $logfile = $_[1]; my $writer = IO::Handle->new(); my $reader = IO::Handle->new(); my $debug = "false"; #$debug = "true"; if ( $debug =~ /true/i && $logfile ) { print "::Utility::execute::\$execute = $execute\n"; } elsif ( $debug =~ /true/i ) { print "::Utility::execute::\$logfile = $logfile\n"; } local (*FILE); if ( (length $execute) > 254 ) { my $workdir = OSworkdir(); OSmkdir($workdir); my $filename; if ( $^O =~ /win/i ) { $filename = "$workdir"."/execute.bat"; $filename = OSpath($filename); open ( FILE, ">$filename" ) or OSlogger( $logfile, "::Util +ity::execute Can't open $filename", 1) and die "aw hell"; } elsif ( $^O =~ /solaris/i ) { $filename = OSpath("$workdir/"); open ( FILE, 774, ">$filename" ) or OSlogger( $logfile, ": +:Utility::execute Can't open $filename", 1) and die "aw hell"; } print FILE $execute; close (FILE); OSlogger( $logfile, "Created executable $filename", 1); $execute = $filename; } if ( $logfile ) { OSlogger( $logfile, "Executing $execute", 1); } # Here's the actual execution. my $pid; eval { $pid = open3( $writer, $reader, $reader, $execute ); }; die "::Utility::execute died a horrible death!\n" if $@; close($writer); # Lets get STDOUT and STDERR ( note the first read always(?) seems + to get nothing) my $stdout = <$reader> ; $stdout = <$reader> ; my $return_val = $stdout; my @stderr; while (<$reader>) { push (@stderr, $_ ); } if ( $logfile ) { OSlogger( $logfile,"\$run RETURNED: $stdout\n", 1); } if ( @stderr ) { OSlogger( $logfile,"Error on $execute\n\n @stderr\n", 1); $return_val = @stderr; } # Return the output return $return_val; }

Edit Masem 2002-02-19 - Added READMORE tag after 1st para

In reply to Open3 and bad gut feeling by coreolyn

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others wandering the Monastery: (6)
    As of 2018-02-25 10:47 GMT
    Find Nodes?
      Voting Booth?
      When it is dark outside I am happiest to see ...

      Results (312 votes). Check out past polls.