There is no good or bad, just fun and not fun

Previous Entry Share Next Entry
t0m->can('has')->beer == Acme::LOLCat->can't('has')
clone
bobtfish
So, CPAN, it has some awesome code. Some shitty code. Some plain silly code.

All the deliberately silly code (and programmers love daft in-jokes, so there is a lot of silly code) in perl which is on CPAN, lives in the Acme:: namespace.

Ergo Acme::LOLCat, and, if you're the web application type, U CAN B RERITIN UR OUTPUTZ.

Aaaaand, back in the real world...

J Random programmer, writing real code, says:

package MyCompany::PlusOneClass;
use Moose;
use MooseX::Types::Moose qw/Int/;

has id => ( isa => Int, is => 'ro', required => 1 );

sub id_plus_one {
    my ($self) = @_;
    return $self->id + 1;
}

And all is well in the world.

Except, try this shit:

use Test::More tests => 4;

my $class = 'MyCompany::PlusOneClass';
my $instance = $class->new( id => 42 );

my $id_meth = $instance->can('id');
my $id_p1_meth = $instance->can('id_plus_one');

is $instance->$id_meth(), 42;
is $instance->$id_p1_meth(), 43;

# All good so far, see
# http://www.shadowcat.co.uk/blog/matt-s-trout/madness-with-methods/
# if you don't grok the stuff with ->can

ok $class->can('has'); # We are NOT Acme::LOLCat, ergo this is NOT cool.

# How many attributes?
my $attr_count = scalar( $class->meta->get_attribute_list );

$instance->has('unused', is => 'ro'); # You didn't really mean that, did you?

# This is some fucked up shit, right here..
isnt scalar( $class->meta->get_attribute_list ), $attr_count;

So, yeah.. Leaving keywords about can ruin your life, quite badly. Go talk to the #reaction crew for actual real life war stories about how random crap in your namespace murders kittens.

The correct solution to this is to drink the rafl koolaid. Just say use namespace::autoclean, and your class cannot has. Super.

So, this comes up fairly often on irc. The good way to explain this all in brief:

ok Acme::LOLCat->can('has');
ok !YourClass->can('has');

Clear? Good. I'm glad. I like that explanation too.

LAST WEEKEND I FOUND A HUGE BUG.

OH NOES!

To try and bleach my brain somewhat more, so I could forget how all software sucks & etc (my hopes, dreams and illusions having been shattered by above bug), I went out to #london.pm beers tonight.

Whereupon, the #london.pm people proved that they are epic pedants in real life, as well as on the mailing list.

ok !Acme::LOLCat->can('has');

is not acceptable.

Thus: Acme::LOLCat->can't('has') is yours for the taking.

PATCHES WELCOME, HTH, KTHNX BAI.

?

Log in

No account? Create an account