Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

•Re: Re: •Re: Re: Say no to ref $thing eq "Expected::Type"

by merlyn (Sage)
on Oct 29, 2002 at 19:34 UTC ( [id://208842]=note: print w/replies, xml ) Need Help??


in reply to Re: •Re: Re: Say no to ref $thing eq "Expected::Type"
in thread Putting file contents into a scalar

{I said this in the CB... maybe you didn't see it.}

If I want to create a wrapper class that delegates rather than inherits, my class @ISA-not {grin} your precious IO::File, even though I'm using one internally, and delegating everything.

So, please, don't check isa. It's not the right thing to do for maximum re-usability.

I'd say that I could probably override UNIVERSAL::isa to report that "yes, I am in fact a IO::File". However, the most common idiom these days for determining "isa" is not with a method call, because that fails on non-blessed references. {sigh}

-- Randal L. Schwartz, Perl hacker
Be sure to read my standard disclaimer if this is a reply.

  • Comment on •Re: Re: •Re: Re: Say no to ref $thing eq "Expected::Type"

Replies are listed 'Best First'.
Re: •Re: Re: •Re: Re: Say no to ref $thing eq "Expected::Type"
by rir (Vicar) on Oct 30, 2002 at 05:25 UTC
    In your IO::File wrapper you should override isa. Why not? It completes the wrap properly. This is a good argument for isa.

    Are they grinding you down today? Reusability is based first on usability. Ensuring that good code will interface with the most lousy code possible may be efficacious, but it is not maximizing reusability.1 Instead it's a way to prematurely embrittle the code.

    When possible I avoid eval for errors which will be immediately handled.2  So to check if a param can do something, if it even is a blessed thing I'd use these terms in some conditional:

    ref $it and $it=~/=/ and $it->can($meth) and $it->$meth();


    1 Having the largest user base doesn't mean the code is more reusable. The word "pandering" begs usage here. It's like that. Stop it, go 'way. Bad word!

    2 The "throw" analogy for exceptions is appropriate: raise an exception when you can not handle the problem locally.

      In your IO::File wrapper you should override isa. Why not? It completes the wrap properly. This is a good argument for isa.
      But you can't. Sorry for not spelling it out earlier, but there's nothing I can do in my wrapper class to make:
      UNIVERSAL::isa($object_of_my_class, "IO::File")
      return true. Yes, I could make:
      $object_of_my_class->isa("IO::File")
      return true, but nobody does that any more because it breaks if the item in question is not a blessed reference. Everyone uses the subroutine form now.

      So, we lose. We all lose. Some place, we've got to push it so it works. I'm saying to do that with either eval wrappers or capability testing with can, not isa.

      -- Randal L. Schwartz, Perl hacker
      Be sure to read my standard disclaimer if this is a reply.

        There's nothing I can do in my wrapper class to make: UNIVERSAL::isa($object_of_my_class, "IO::File") return true.
        Nothing? Nothing reliable, perhaps, but try
        *UNIVERSAL::old_isa = *UNIVERSAL::isa; *UNIVERSAL::isa = sub { my($self,$pkg) = @_; return 1 if ref($self) eq 'MyClass' and $pkg eq 'IO::Handle'; return UNIVERSAL::old_isa(@_); }; package MyClass; sub new { bless {} } my $object_of_myclass = new MyClass(); my $rc = UNIVERSAL::isa($object_of_myclass, 'IO::Handle'); print $rc ? "It worked\n" : "Oh nuts\n";
        And no, I'm not serious.

        But you can. You can override the function UNIVERSAL::isa with a wrapper that adds any special case logic that you want before calling the built-in.

        Of course when you start doing that...

        Just that many people use UNIVERSAL::can( $complete_unknown, $classname) is not a reason to stop providing proper interfaces to your objects. No one is going to use good interfaces if they don't exist.

        The two issues are completely unrelated. If you don't even know if you have an object, naturally you aren't prepared to use its interface.

        I hope the nobodies prevail.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (3)
As of 2024-03-30 06:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found