# 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, "::Utility::execute Can't open $filename", 1) and die "aw hell"; } elsif ( $^O =~ /solaris/i ) { $filename = OSpath("$workdir/execute.sh"); 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; }