Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re^3: How to handle an external process from CGI? (updated)

by haukex (Chancellor)
on Jun 22, 2019 at 08:40 UTC ( #11101709=note: print w/replies, xml ) Need Help??


in reply to Re^2: How to handle an external process from CGI?
in thread How to handle an external process from CGI?

I think there's no <MYPIPE> if open fails

Yes and no: the first <MYPIPE> does return undef, so the while loop never runs, but you should also be getting a warning "readline() on closed filehandle", indicating that something is wrong. A failed open doesn't have to be fatal, there are plenty of ways to handle the control flow:

if ( open my $fh, ... ) { while (<$fh>) { ... } close $fh; } else { warn "open: $!" } # - or - sub foo { open my $fh, ... or do { warn "open: $!"; return }; while (<$fh>) { ... } close $fh; } # - or - FILE: for my $file (@files) { open my $fh, ... or do { warn "skipping $file: $!"; next FILE }; while (<$fh>) { ... } close $fh; } # - or - use Try::Tiny; try { open my $fh, ... or die $!; while (<$fh>) { ... } close $fh; } catch { warn "error: $_"; }; # same thing, but without the module: eval { open my $fh, ... or die $!; ... ;1 } or do { warn "error: $@"; };

The fourth and fifth examples are different in that they catch any fatal error inside the block, which may or may not be desired.

Update: Added the third example, from this thread.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://11101709]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (6)
As of 2019-07-19 15:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?