I mean this: if (defined $err) can become if (defined($err) && ($err eq 'YouveGotError')), so that it errors only on'YouveGotError' blessed refs and nothing else, because the sub may normally return other blessed things.
I'm not sure if this is a question, but just in case, note that I fully agree with kcott that Scalar::Util's blessed (a core module) is the "correct" answer to the question of how to tell if a reference is blessed or not. But if the sub also can return other blessed refs, and you want to differentiate them, then one possible solution is what you said in your root node: creating an "error" class is one way. But since Perl's OO system is so nice and simple, inheritance can be incredibly simple, as I show in the following example. Of course, there are existing solutions on CPAN, e.g. Throwable and Exception::Class.
use warnings;
use strict;
use Scalar::Util qw/blessed/;
@Error::ISA = qw//; # "base class" for errors
@BadInput::ISA = qw/Error/;
sub doit {
my $in = shift;
if ( $in =~ /foo/ ) {
return bless [123,456], 'RealReturnValue';
}
else {
return bless ['abc','def'], 'BadInput';
}
}
for my $val ("foobar", "quzbaz") {
my $ret = doit($val);
if ( my $err = blessed $ret and $ret->isa('Error') ) {
print "$val -> Error: $err\n";
}
else {
print "$val -> OK\n";
}
}
__END__
foobar -> OK
quzbaz -> Error: BadInput