Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Blessing with unknown classnames

by kcott (Archbishop)
on Apr 01, 2021 at 17:35 UTC ( [id://11130681]=note: print w/replies, xml ) Need Help??


in reply to Blessing with unknown classnames

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

Replies are listed 'Best First'.
Re^2: Blessing with unknown classnames
by bliako (Monsignor) on Apr 01, 2021 at 18:19 UTC

    Hello Ken,

    Minor issue: finding the classname (e.g. ref($ret)) is part of deciding if sub returned an error, for example when sub normally returns a blessed object instead of unblessed ref (as in my example).

    apropos the edits: Yes, I have added another use-case 5 minutes after posting, sorry. It does not change anything in your post.

      "Minor issue: finding the classname (e.g. ref($ret)) is part of deciding if sub returned an error, for example when sub normally returns a blessed object instead of unblessed ref (as in my example)."

      Sorry, but I don't understand this. My code using blessed clearly distinguished "Error" from "Success". Your line:

      die "@$ret" if ref($ret) eq 'YouveGotError';

      could be rewritten as:

      die "@$ret" if $err eq 'YouveGotError';

      If I change 'blessed $ret' in my code, to 'ref $ret', the output becomes:

      Run: 1 Error "ARRAY" detected Code: 42 Errstr: 43 Run: 2 Error "YouveGotError" detected Code: 0 Errstr: error was ...

      which, I'm pretty sure, is not what you want.

      I suspect we may be talking at cross-purposes, or there's some other misunderstanding; however, I've looked back over our posts, and can't see what the problem might be.

      "apropos the edits: ..."

      No need to apologise. I just wanted to make it clear what I was responding to. There was no intended rebuke or other negativity.

      — Ken

        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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11130681]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2024-04-23 23:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found