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

In reply to Re^5: Blessing with unknown classnames by haukex
in thread Blessing with unknown classnames by bliako

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.