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;
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 }
}
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;