Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re^3: I don't understand UNIVERSAL::DOES()

by eric256 (Parson)
on Mar 09, 2007 at 23:12 UTC ( [id://604086]=note: print w/replies, xml ) Need Help??


in reply to Re^2: I don't understand UNIVERSAL::DOES()
in thread I don't understand UNIVERSAL::DOES()

Wouldn't it be better if the role names matched the ref? I.e. CODE,ARRAY,HASH,REGEX ? And could I see the patch? Just curiousity to see how something like that would be implemented, seems easy enough to implement in perl if you wanted and import it in.

Here is my stab. I went with a DOES sub returning a list of roles that an object does, makes it easy to make them inheritable...(i.e.

sub DOES { qw/log print/, shift->SUPER::DOES() }</code). Realy just fo +r my fun and amusment. ;) /me goes off to investigate the various ro +les modules</p> <code> use strict; use warnings; use Test::More qw/no_plan/; { package UNIVERSAL; sub does { return 1 if ref $_[0] eq $_[1]; #if ( eval qq/"@" . ref($_[0]) . "::DOES"/; if (UNIVERSAL::isa($_[0], 'UNIVERSAL') ) { if ($_[0]->can('DOES')) { return 1 if grep { $_[1] eq $_ } $_[0]->DOES(); } else { return 1 if $_[0]->isa($_[1]); } } return 0; } } sub say {print @_, "\n";} {package Foo}; is(UNIVERSAL::does(sub{},'CODE'), 1, 'Code Ref'); is(UNIVERSAL::does(qr//,'Regexp'), 1, 'Regex'); # return tr +ue is(UNIVERSAL::does([],'ARRAY') , 1, 'Array') ; # return + true is(UNIVERSAL::does([],'The::Funky::Chicken'),0 , 'Bad Class name') ;# +return false is(UNIVERSAL::does([],'UNIVERSAL'),0, 'Array isn\'t Univeral') ; + # return false @Bar::ISA=qw(Foo); is(UNIVERSAL::does('Bar', 'Foo'), 1, 'Bar does Foo'); # retur +n true {package Bang; @Bang::ISA=qw(Foo UNIVERSAL); sub DOES { qw(test) }; } is(Bang->does('Foo'), 0, 'Bang doesn\'t Foo'); + # return false { package A; sub DOES { qw/this or that/ }; } { package B; @B::ISA = qw/A/; sub DOES { qw/other/ }; } #B->test(); is(A->does('this') , 1, 'A does this'); # true is(B->does('that') , 0, 'B doesn\'t do that'); # false is(B->does('other'), 1, 'B does other'); # true

___________
Eric Hodges

Replies are listed 'Best First'.
Re^4: I don't understand UNIVERSAL::DOES()
by demerphq (Chancellor) on Mar 10, 2007 at 19:48 UTC

    This is what i have so far. Warning XS/Internals aware code. And no error checking on the rolename.

    UPDATE: An improved version of this code has been posted to p5p for further review, and maybe application in time for Perl 5.10.

    ---
    $world=~s/war/peace/g

Re^4: I don't understand UNIVERSAL::DOES()
by demerphq (Chancellor) on Mar 10, 2007 at 10:45 UTC

    Wouldn't it be better if the role names matched the ref? I.e. CODE,ARRAY,HASH,REGEX ?

    No. For reasons i explained in my reply to chromatic. And 'REGEX' isnt a type. You will never see it returned from ref() or reftype(). Also, your code doesnt solve the "can i deref this item in a particular way" problem, nor the "does this reference contain regexp magic" problem.

    As for code, yeah, once i get it done ill post it. Currently its a work in progress.

    ---
    $world=~s/war/peace/g

      I wasn't thinking of those as types (and it wasn't meant as a final product, just an example of the way i was thinking about roles and does), I was thinking of them as roles since we are talking about does, and does talks about roles. If something reports that it does a role then its that somethings responsibility to insure it does. SO if i create a class that claims it does 'ARRAY' then it is my class's responsibility to insure that it can do everything that role entails. I see now reading your reply to chromatic that you aren't realy talking about roles at all which is where the disconnect is.

      If we assume that DOES is for roles, why not overload ->can to do what you want?. You do a role, but you can a method. So if you wanted to ask if you can dereference it as an array you ask if it <cdoe>->can($obj, '@{}')</code> to me that seems to follow your overloading style better as it doesn't imply that '@()' is a role, but actually that it is a method, which it is if you've overloaded it.


      ___________
      Eric Hodges

        I wasn't thinking of those as types (and it wasn't meant as a final product, just an example of the way i was thinking about roles and does), I was thinking of them as roles since we are talking about does, and does talks about roles.

        A given mode of dereferencing an object is merely an interface to that object. And as its an interface its a role. "Can i treat reference $x as a reference to an array?" could equally be thought of "can $x play the role of an array?".

        ---
        $world=~s/war/peace/g

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2024-03-29 08:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found