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

The code below is wrapped in an eval somewhere, but no matter how hard I try I can not get the file read portion of this to work. Why? I've tested every other portion...and it works.
use File::Temp qw/ tempfile /; my ( $fh, $filename ) = tempfile(); my ( $eh, $errorname ) = tempfile(); use Win32::Job; my $job = Win32::Job->new; my $exe = "remotely_executing_program_tool.exe"; my $args = " some_command_line_that_connects_and_runs_stuff_on_remote_machine"; my %opts = ( 'stdout' => $fh, 'stderr' => $eh ); #print $args; $job->spawn( $exe, $args, \%opts ); $job->run(120); $job->kill(); local $/ = undef; open F, '<', $filename; $text = <F>; close F; $connectOk = 0 if length($text) < 1; open F, '<', $errorname; $errStr = <F>; close F; $retval = $text; #print $retval;

Replies are listed 'Best First'.
Re: eval horrors
by tchrist (Pilgrim) on May 12, 2011 at 18:17 UTC
    Since you don’t seem to care to check for errors, you had better put this at the top of your program:
    use 5.10.1; use autodie;
    Alternately, check your opens:
    open(F, "<", $filename) || die "can't open $filename: $!";
    I also suggest you get used to using parentheses around your function calls to make them easier to read and parse, although that isn’t really the problem here.
Re: eval horrors
by Anonymous Monk on May 12, 2011 at 17:40 UTC
    open my $lexicalFileHandle, '<', 'threeArgOpen.txt' or warn "The reason for failure is very useful and is as follows: $!" and return "We canna do it cap'n!";

    If you check for errors during your file operations and handle them gracefully, then you probably don't need the eval at all. (I suspect it is just there to trap errors from that code.)