my $WARNINGS_CATEGORY;
BEGIN {
$WARNINGS_CATEGORY = 'File::KDBX';
- warnings::register_categories($WARNINGS_CATEGORY) if warnings->can('register_categories');
+ if (warnings->can('register_categories')) {
+ warnings::register_categories($WARNINGS_CATEGORY);
+ }
+ else {
+ eval qq{package $WARNINGS_CATEGORY; use warnings::register; 1}; ## no critic ProhibitStringyEval
+ }
}
use overload '""' => 'to_string', cmp => '_cmp';
This can be convenient for error handling when you're not sure what the exception is but you want to treat it
as a B<File::KDBX::Error>. Example:
- eval { .... };
+ eval { ... };
if (my $error = error(@_)) {
if ($error->type eq 'key.missing') {
handle_missing_key($error);
=cut
sub error {
+ my $class = @_ && $_[0] eq __PACKAGE__ ? shift : undef;
my $self = (blessed($_[0]) && $_[0]->isa('File::KDBX::Error'))
? shift
- : (@_ && $_[0] eq __PACKAGE__)
- ? shift->new(@_)
+ : $class
+ ? $class->new(@_)
: __PACKAGE__->new(@_);
return $self;
}