autobox::Bless - Guess which package a hash or hashref probably should be and blessed it
package purple; sub new { my $package = shift; bless { one => 1, two => 2, }, $package; } sub three { my $self = shift; $self->{one} + $self->{two}; } # package main; use autobox::Bless; my $purple = purple->new; # optionally comment this out my %foo = ( one => 5, two => 17 ); print %foo->three, "\n"; # 22! print %foo->four, "\n"; # not found, but %foo is now blessed into purple (yes, really)
Attempts to guess which package an unblessed hash or hashref should be blessed into and bless it into that package on the fly.
Guessing is done by the fields (hash keys) present in the unblessed hash versus the fields in instances of various objects in memory. To be considered a match, the thing must find an object with all of the fields as the unblessed hash.
If that heuristic fails, as it would in the SYNOPSIS example where the <purple-new>> line is commented out, then a less nice strategy is attempted: all loaded packages are exampled for one containing the method called.
<purple-
Why would anyone want this? You have a large legacy codebase that makes heavy use of hashes for collections of assortments of data and you want to shoehorn an OO-ish API onto it. Or perhaps you just want to play with an ultra lazy style of programming.
my Foo::Bar $foo
When used with autobox::Core or similar modules that add API methods to primitive values, method names might clash.
autobox, autobox::Core, perl5i, ...
http://twitter.com/scrottie/status/10706254646
Scott Walters, <scott@slowass.net>
Copyright (C) 2010 by Scott Walters
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.12.0 or, at your option, any later version of Perl 5 you may have available.
To install autobox::Bless, copy and paste the appropriate command in to your terminal.
cpanm
cpanm autobox::Bless
CPAN shell
perl -MCPAN -e shell install autobox::Bless
For more information on module installation, please visit the detailed CPAN module installation guide.