G'day bliako,

There is no requirement for $class to be a real class, have length, or even be present. See bless for details.

The use of ref in your posted code is not the best choice: the doco explains issues. It would be much better to use the blessed function of the builtin module Scalar::Util.

Here's an example of usage. My perle alias captures all sorts of problems.

$ alias perle alias perle='perl -Mstrict -Mwarnings -Mautodie=:all -MCarp::Always -E +'
$ perle ' use Scalar::Util "blessed"; for (1, 2) { say "Run: $_"; my $ret = foo(); my $err = blessed $ret; if (defined $err) { say qq{Error "$err" detected}; say qq{Code: $ret->[0]}; say qq{Errstr: $ret->[1]}; } else { say qq{Success!}; say for @$ret; } } sub foo { state $error = 0; my $retval; if ($error) { $retval = bless [0, "error was ..."] => "YouveGotError"; } else { $retval = [42, 43]; } $error ^= 1; return $retval; } ' Run: 1 Success! 42 43 Run: 2 Error "YouveGotError" detected Code: 0 Errstr: error was ...

[Note: Between reading the OP and clicking "Comment on", there appears to have been two updates to the OP. There may be others. I'm replying to the post that ended with "Thanks LanX for looking this up on CB". I don't believe this changes the essence of my reply; just bear in mind what I'm replying to.]

— Ken


In reply to Re: Blessing with unknown classnames by kcott
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.