Can 'use strict' warn instead of error

前端 未结 5 1168
没有蜡笔的小新
没有蜡笔的小新 2021-01-19 04:31

When using use strict perl will generate a runtime error on unsafe constructs. Now I am wondering if it is possible to have it only print a warning instead of c

5条回答
  •  佛祖请我去吃肉
    2021-01-19 05:06

    The preferred method:

    use Carp;
    
    sub foo {
      croak "no args" unless @_;
    }
    
    eval foo();
    if( $@ ){
      print "caught die: $@";
    }
    

    If you can't change your die's to croak's:

    sub foo {
      die "no args" unless @_;
    }
    
    {
      my $prev_die = $SIG{__DIE__};
      $SIG{__DIE__} = sub { print "caught die: $_[0]"; };
      eval foo();
      $SIG{__DIE__} = $prev_die;
    }
    

    The second method will print out the errors on STDERR.

    See:

    perldoc -f eval

    perldoc perlvar and search for /\$\@/ and /__DIE__/

    perldoc Carp

提交回复
热议问题