From: Yuval Kogman Date: Tue, 10 Jun 2008 14:04:06 +0000 (+0000) Subject: use teh Squirrel X-Git-Tag: 0.04~36 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=692e1bcd8e516531416d8f6976f144080828b59e;p=gitmo%2FMouse.git use teh Squirrel --- diff --git a/lib/Squirrel.pm b/lib/Squirrel.pm new file mode 100644 index 0000000..6e2b856 --- /dev/null +++ b/lib/Squirrel.pm @@ -0,0 +1,72 @@ +#!/usr/bin/perl + +package Squirrel; + +use strict; +use warnings; + +sub _choose_backend { + if ( $INC{"Moose.pm"} ) { + return { + import => \&Moose::import, + unimport => \&Moose::unimport, + } + } else { + require Mouse; + return { + import => \&Mouse::import, + unimport => \&Mouse::unimport, + } + } +} + +my %pkgs; + +sub _handlers { + my $class = shift; + + my $caller = caller(1); + + $pkgs{$caller} ||= $class->_choose_backend; +} + +sub import { + goto $_[0]->_handlers->{import}; +} + +sub unimport { + goto $_[0]->_handlers->{unimport}; +} + +__PACKAGE__ + +__END__ + +=pod + +=head1 NAME + +Squirrel - Use L, unless L is already loaded. + +=head1 SYNOPSIS + + use Squirrel; + + has goggles => ( + is => "rw", + ); + +=head1 DESCRIPTION + +L and L are TEH BEST FRENDS, but if L isn't there +L will hang out with L as too. + +When your own code doesn't actually care whether or not you use L or +L you can use either, and let your users decide for you. + +This lets you run with minimal dependencies and have a faster startup, but if +L is already in use you get all the benefits of using that. + +=cut + + diff --git a/t/squirrel.t b/t/squirrel.t new file mode 100644 index 0000000..aa09a9d --- /dev/null +++ b/t/squirrel.t @@ -0,0 +1,50 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +use Test::More; + +{ + package Foo; + use Squirrel; + + has foo => ( + isa => "Int", + is => "rw", + ); +} + +# note that 'Foo' is defined before this, to prevent Moose being loaded from +# affecting its definition + +BEGIN { + plan skip_all => "Moose required for this test" unless eval { require Moose }; + plan 'no_plan'; +} + +{ + package Bar; + use Squirrel; + + has foo => ( + isa => "Int", + is => "rw", + ); +} + +my $foo = Foo->new( foo => 3 ); + +isa_ok( $foo, "Foo" ); + +isa_ok( $foo, "Mouse::Object" ); + +is( $foo->foo, 3, "accessor" ); + + +my $bar = Bar->new( foo => 3 ); + +isa_ok( $bar, "Bar" ); +isa_ok( $bar, "Moose::Object" ); + +is( $bar->foo, 3, "accessor" );