Perl6: How could I make all warnings fatal?

删除回忆录丶 提交于 2019-12-05 02:48:12

问题


How could I make all warnings in Perl6 fatal, so that the script dies as soon as a warning appears on the screen.


CONTROL { when CX::Warn { note $_; exit 1 } } dies more often.

This script dies with CONTROL { when CX::Warn { note $_; exit 1 } } but not with use fatal:

#!/usr/bin/env perl6
use v6;

my @a = 1 .. 4;
@a[5] = 6;
my @b;

for @a -> $i {
    @b.push( ~$i );
}

say "=====\n" x 3;

回答1:


Warnings are control exceptions of type CX::Warn that are resumed by default. If you want to change that behaviour, you need to add a CONTROL block, in your case something like this:

CONTROL {
    when CX::Warn {
        note $_;
        exit 1;
    }
}

Ignoring all warning instead of making them fatal would look like this:

CONTROL {
    when CX::Warn { .resume }
}



回答2:


You can make all exceptions immediately fatal with 'use fatal'. For instance, this code will not throw an error until you attempt to read from $file, so it will reach the 'say' line. If you uncomment 'use fatal', it will die immediately at the 'open' statement, and not reach the 'say' line.

For more fine-grained control, see the try/CATCH system for exceptions.

# use fatal;
my $file = open 'nonexistent', :r;
say 'Reached here';
my @lines = $file.IO.lines;


来源:https://stackoverflow.com/questions/34747960/perl6-how-could-i-make-all-warnings-fatal

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!