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

i have tried for hours now, and i am very upset, cause this should be super simple. File::Fetch::WARN, as I have read, when set to 0 will stop things from being carp'd (which outputs to STDOUT, no?). but, my code emits an error message when $PARAMS{url} does not exist when i need it to just shutup and let me handle the error. so i do this in my code:
use strict; use warnings; use CGI::Carp qw(fatalsToBrowser); use File::Copy; use File::Path qw(remove_tree rmtree); use File::Fetch; $File::Fetch::WARN = 0; use Fcntl; use URI::Escape; use English; my $ff = 0; eval { $ff = File::Fetch->new(uri => $PARAMS{url}); }; if (not $ff) { if ($DEBUG_OP_ADD_COVER_PHOTO) { $output .= "failed to retrieve image: " . $ff->error . "<br>\n"; } else { print error_redir(redirrer($OP_QUERY, $OP_FILE_MANAGER, $FOLDER_QU +ERY, $FOLDER), "Failed to Retrieve Image: " . $ff->error); exit 1; } }
the eval was added in as a suggestion to suppress the warning, but that doesn't work. $File::Fetch::WARN = 0 has no affect!

the fetching process emits a completely different error message, and it's printed (i presume to STDOUT) before i get to print anything to STDOUT! which causes "internal server error", and i can't get the thing to stop printing that damn junk out, it 'breaks' things!

Note: whilst $DEBUG_OP_ADD_COVER_PHOTO = 1, the error message emitted is: could not retrieve file: Command failed:. Nowhere in my code have I asked for that to be printed out.

Edit: Added "use" statements which may affect your answer. the code in this post is only a very small part of a much larger program.

I was pondering why the ball was getting bigger, and then...it hit me!

Replies are listed 'Best First'.
Re: $File::Fetch::WARN = 0; not working?
by haukex (Archbishop) on Sep 12, 2019 at 19:37 UTC
    carp'd (which outputs to STDOUT, no?)

    No, by default warn and Carp messages go to STDERR.

    my code emits an error message

    What error message exactly? That would help in grepping where in File::Fetch it's being generated.

    the eval was added in as a suggestion to suppress the warning

    eval only catches fatal errors, it does not stop things from being printed, including warnings.

    First, I'd investigate where in the module the message is being generated and why the WARN flag isn't suppressing it. If the module doesn't provide a way to turn this message off, there may be some trickery necessary with a local $SIG{__WARN__} or other monkey patching. If the case were to be that I can't do anything about it, as a last resort one can use Capture::Tiny to catch any output from a block of code.

      okay, if $DEBUG_OP_ADD_COVER_PHOTO = 0, my server returns status: 500 and apache2 error log says: malformed header from script 'index.pl': Bad header: <br><br><br>

      the above error doesn't make sense, because nothing is being printed to STDOUT at this point

      if $DEBUG_OP_ADD_COVER_PHOTO = 1, I can then print the error given by File::Fetch: could not retrieve file: Command failed:

      this one just lacks information

      EDIT: Oh, i think i figured it out. omg, i feel so dumb. body of an if statement much further on is missing $output .= starthtml(); and thus, negates this whole issue. face palm with both hands, does walk of shame.
      I was pondering why the ball was getting bigger, and then...it hit me!