X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FMoose%2FAutobox.pm;h=7899bb78901f7cda473ddc22adff7fb43b14e080;hb=244bd352801e30561098541bd2a7313234374810;hp=5af7ff58c68b317af3af0deaba9c0502adc75e61;hpb=252ab1a26175a028754adbe4589cd87bc5240ce6;p=gitmo%2FMoose-Autobox.git diff --git a/lib/Moose/Autobox.pm b/lib/Moose/Autobox.pm index 5af7ff5..7899bb7 100644 --- a/lib/Moose/Autobox.pm +++ b/lib/Moose/Autobox.pm @@ -4,32 +4,63 @@ package Moose::Autobox; use strict; use warnings; -use Moose qw(confess); +use Carp qw(confess); use Scalar::Util (); -our $VERSION = '0.01'; - -sub import { - eval q| -package SCALAR; -use Moose; -with 'Moose::Autobox::Scalar'; - -package ARRAY; -use Moose; -with 'Moose::Autobox::Array'; - -package HASH; -use Moose; -with 'Moose::Autobox::Hash'; - -package CODE; -use Moose; -with 'Moose::Autobox::Code'; - |; - confess 'Could not create autobox packages because - ' . $@ if $@; -} +our $VERSION = '0.03'; + +use base 'autobox'; +sub import { + (shift)->SUPER::import( + DEFAULT => 'Moose::Autobox::', + UNDEF => 'Moose::Autobox::Undef', + ); +} + +sub mixin_additional_role { + my ($class, $type, $role) = @_; + ($type =~ /SCALAR|ARRAY|HASH|CODE/) + || confess "Can only add additional roles to SCALAR, ARRAY, HASH or CODE"; + ('Moose::Autobox::' . $type)->meta->_apply_all_roles($role); +} + +{ + + package Moose::Autobox::SCALAR; + # NOTE: + # this doesnt make sense, but + # I need to prevent Moose from + # assiging to @ISA + use base 'UNIVERSAL'; + use Moose; + with 'Moose::Autobox::Scalar'; + + *does = \&Moose::Object::does; + + package Moose::Autobox::ARRAY; + use base 'UNIVERSAL'; + use Moose; + with 'Moose::Autobox::Array'; + + *does = \&Moose::Object::does; + + package Moose::Autobox::HASH; + use base 'UNIVERSAL'; + use Moose; + with 'Moose::Autobox::Hash'; + + *does = \&Moose::Object::does; + + package Moose::Autobox::CODE; + use base 'UNIVERSAL'; + use Moose; + with 'Moose::Autobox::Code'; + + *does = \&Moose::Object::does; + +} + 1; __END__ @@ -38,31 +69,79 @@ __END__ =head1 NAME -Moose::Autobox - autoboxed for her pleasure +Moose::Autobox - Autoboxed for her pleasure =head1 SYNOPOSIS + use Moose::Autobox; + use autobox; + + print 'Print squares from 1 to 10 : '; + print [ 1 .. 10 ]->map(sub { $_ * $_ })->join(', '); + +=head1 CAVEAT + +First, a warning. + +This module is very very very very very very very experimental. It +makes use of a very experimental module (L) and uses some +shiney new technology (L) to accomplish it's goals. + +Use this at your own risk. If it breaks the lamp in the living room +and your mother yells at you, don't come complaining to me. + +Also, as this is so experimental, it's API should not be considered +to be stable. It could very well change in radical ways. + =head1 DESCRIPTION - Any - Item - Bool - Undef - Defined - Value - Num - Int - Str - Ref - ScalarRef - ArrayRef - HashRef - CodeRef - RegexpRef - Object - Role - +Moose::Autobox provides an implementation of SCALAR, ARRAY, HASH +& CODE for use with L. It does this using a hierarchy of +roles in a manner similar to what Perl 6 I do. This module, +like L and L, was inspired by my work on the +Perl 6 Object Space, and the 'core types' implemented there. + +=head2 A quick word about autobox + +The L module provides the ability for calling 'methods' +on normal Perl values like Scalars, Arrays, Hashes and Code +references. This gives the illusion that Perl's types are first-class +objects. However, this is only an illusion, albeit a very nice one. +I created this module because L itself does not actually +provide an implementation for the Perl types but instead only provides +the 'hooks' for others to add implementation too. + +=head2 Is this for real? or just play? + +My intent is to try and make this module as production worthy as +possible. This may or may not be possible, depending on how well +L works out. At this point, I have high hopes for things +but only time (and more tests and code) will tell. +=head1 METHODS + +=over 4 + +=item B + +This will mixin an additonal C<$role> into a certain C<$type>. The +types can be SCALAR, ARRAY, HASH or CODE. + +This can be used to add additional methods to the types, see the +F directory for some examples. + +=back + +=head1 TODO + +=over 4 + +=item More docs + +=item More tests + +=back + =head1 BUGS All complex software has bugs lurking in it, and this module is no @@ -82,7 +161,4 @@ L This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. - =cut - -