How to make perl6 die on undefined values?

  • A+
Category:Languages

I am attempting to work with a hash in Perl6, but when I put some fake values into it (intentionally) like

say %key<fake_key>; 

I get

(Any)

but I want the program to die in such occurrences, as Perl5 does, because this implies that important data is missing.

For example,

#!/usr/bin/env perl  use strict; use warnings 'FATAL' => 'all'; use autodie qw(:all);  my %hash; print "$hash{y}/n"; 

as of 5.26.1 produces

Use of uninitialized value $hash{"y"} in concatenation (.) or string at undefined.pl line 8. Command exited with non-zero status 255 

How can I get the equivalent of use warnings 'FATAL' => 'all' and use autodie qw(:all) in Perl6?

 


You can create a role to do this. A simple version would be :

role KeyRequired {      method AT-KEY( /key ) {          die "Key {key} not found" unless self.EXISTS-KEY(key);          nextsame;      }  }; 

Then you create your hash with : my %key does KeyRequired; and it will die if you request a non existent key.

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: