问题
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