Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (1)
As of 2024-04-25 01:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found