Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re^2: Blessing with unknown classnames

by bliako (Monsignor)
on Apr 01, 2021 at 18:19 UTC ( [id://11130683]=note: print w/replies, xml ) Need Help??


in reply to Re: Blessing with unknown classnames
in thread Blessing with unknown classnames

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.

Replies are listed 'Best First'.
Re^3: Blessing with unknown classnames
by kcott (Archbishop) on Apr 01, 2021 at 18:55 UTC
    "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.

        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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (2)
As of 2024-04-19 21:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found