Suppress "BEGIN failed - compilation canceled by"

I have a module that should do some validation in a BEGIN block. This prevents the user from viewing the useless message along the line (during the compilation phase visible in the second BEGIN here).

The problem is that if I die in BEGIN, then the message that I throw is buried BEGIN failed--compilation aborted at. However, I prefer dieto exit 1, since then it will be a trap. Should I just use exit 1or is there something I can do to suppress this extra message?

#!/usr/bin/env perl

use strict;
use warnings;

BEGIN {
  my $message = "Useful message, helping the user prevent Horrible Death";
  if ($ENV{AUTOMATED_TESTING}) {
    # prevent CPANtesters from filling my mailbox
    print $message;
    exit 0;
  } else {

    ## appends: BEGIN failed--compilation aborted at
    ## which obscures the useful message
    die $message;

    ## this mechanism means that the error is not trappable
    #print $message;
    #exit 1;

  }
}

BEGIN {
  die "Horrible Death with useless message.";
}
+5
source share
1 answer

die, , . , die BEGIN, , , .

, exit 1, :

# place this at the top of the BEGIN block before you try to die

local $SIG{__DIE__} = sub {warn @_; exit 1};
+10

All Articles