Acme we are not just 500 Pound Anvils

Well I finally did learned something interesting looking at an Acme modules today.

I spotted Acme::Curse and at first glance it seemed to be just another of those silly play things in this namespace.

However the POD entry intrigued me.

Did you ever want to droo the blessing of an object? Well, now you can: Acme::Curse unblesses reference by returning a shallow, non-blessed copy of the object.

Well I do not know hot to 'droo' something (sounds painful) but the idea of a non-blessed copy is something I have need of.

So I looked inside and I saw this


sub curse {
    my ($obj) = @_;
    my $type = reftype($obj);
    if ($type eq 'HASH') {
        return { %$obj };
    } 

...

Just what I was looking for. Some days ago I was working with a system where a variable was passed into a sub. The way it has evolved you could get an blessed Object, a true Hash, Hash ref or the odd time an Array.

So there was all sorts of logic using 'ref' and at the top of the sub to try and figure out what was being passed to it, convert it to a standard format and then pass back.

Well long story short as it is late, Acme::Curse gave a very good example of how one should do this sort of thing.

So really this mod got me to have a closer look at the functions in Scalar::Util. I have used it occasionally in the past, mostly for looks_like_number, but I think it is going to see allot more use by me for the next few fixes I will be doing in house.

Acme_anvil.gif

1 Comment

Leave a comment

About byterock

user-pic Long time Perl guy, a few CPAN mods allot of work on DBD::Oracle and a few YAPC presentations