[Raleigh-talk] Messing around with symbol tables

Paul Bennett paul.w.bennett at gmail.com
Wed Dec 2 09:05:54 PST 2009


On Tue, 01 Dec 2009 17:04:40 -0500, Michael Peters <mpeters at plusthree.com>  
wrote:

> On 12/01/2009 04:53 PM, Paul Bennett wrote:
>
>> our @EXPORT = qw( antiobject );
>
> Why export this method?

For my own convenience. Maybe it should go in @EXPORT_OK, but I tend to  
feel that if you've got a package Foo that exists solely to provide foo(),  
it ought to export foo() by default. I'm not inflexible on this, though --  
it's more of a gut thing (I suppose largely inspired by IO::All, among  
others).

>> confess('Object cannot cmp()') unless UNIVERSAL::can($orig, 'cmp');
>> confess('Object cannot clone()') unless UNIVERSAL::can($orig, 'clone');
>
> You should be calling can() as $orig->can() instead of UNIVERSAL::can().

I disagree. What if "$orig"::can() is overriden with something utterly  
unlike capability testing? It would be outside what I consider safe, sane,  
and consensual, but not implausible.

>> my $new = $orig . '::anticlass';
>
> The intent of this isn't perfectly clear. You're creating a new class  
> (or blessing the object into a new class) so I'd probably do something  
> like:
>
>    my $new_class = ref $orig . '::anticlass';

I agree. I could have used a better variable name, and probably will. I  
could and should also check for /::anticlass$/ and get rid of it instead  
of building a potentially infinite chain of the things. On a side note,  
blessed($object) returns the blessed class name as a string (or undef if  
not blessed), so ref(blessed($object)) would be wrong.

> But a different (and perhaps cleaner) strategy would be too look at  
> something like Moose and Roles. So you clone an object and then apply  
> the "ReverseCmp" role to it -  
> http://search.cpan.org/~flora/Moose-0.93/lib/Moose/Manual/Roles.pod#APPLYING_ROLES
>
> I don't know if this approach would work though with objects that aren't  
> Moose based and it seems you can't really modify the code of the objects  
> that you're using. But maybe you can get some ideas from their code.

In my experience, Moose plays well with others right up until you want to  
do something too deeply Moosey. I'll see if I can give it a whirl, though.  
It could definitely add clarity.



Thanks for your help,



--
Paul


More information about the Raleigh-talk mailing list