Object::Util
I've recently released a new module called Object::Util to CPAN. This is a set of handy methods for working with classes and objects in Perl. It is inspired by Safe::Isa, a module which takes advantage of the fact that coderefs may be called as methods on Perl objects:
my $method = sub { ... }; $object->$method(@args);
Object::Util gives you a set of coderef utility methods that are applicable to a wide range of classes and objects.
$class->$_new(%args)is the same as$class->new(%args)except that it automatically loads $class if it's not already loaded, and allows$classto be a coderef instead of a class name (in which case it just passes%argsto the coderef).It is intended for dependency injection, where you have been passed a class or coderef and are expected to use that to generate objects.
$object->$_clone()clones an object. It delegates to the object'sclone()method (if it has one), and otherwise tries a bunch of fallback techniques.$object->$_clone(%args)clones an object, but passes some extra arguments to the constructor.$class->$_with_traits(@traits)is inspired by MooseX::Traits, but should work equally well with Moose, Mouse, Moo, or Role::Tiny.$object->$_extend(\%methods)is inspired by Object::Extend and prototype-based programming.$_isa,$_can,$_does,$_DOES, and$_call_if_objectare stolen from Safe::Isa.$object->$_try($method => @args)will call the method, but catch and ignore any exceptions thrown.$object->$_tap($method => @args)is stolen from Object::Tap.$object->$_dump()dumps the guts of an object.
Although designed to work nicely with Moose, Mouse, Moo, and Class::Tiny, Object::Util doesn't require any of those. It's a reasonably light-weight module.
Leave a comment