Subject: | use declared types instead of globals (with backcompat) |
Patch attached.
--
Rafael
Subject: | mxtio.patch |
diff -ruN MooseX-Types-IO-0.02-jqvzlJ/lib/MooseX/Types/IO/All.pm /home/rkitover/src/moose/mxtypes-io/lib/MooseX/Types/IO/All.pm
--- MooseX-Types-IO-0.02-jqvzlJ/lib/MooseX/Types/IO/All.pm 2008-10-19 06:11:00.000000000 -0700
+++ /home/rkitover/src/moose/mxtypes-io/lib/MooseX/Types/IO/All.pm 2009-05-18 13:16:09.000000000 -0700
@@ -12,10 +12,10 @@
use namespace::clean;
use MooseX::Types -declare => [qw( IO_All )];
-class_type 'IO::All';
+my $global = class_type 'IO::All';
subtype IO_All, as 'IO::All';
-coerce 'IO::All',
+coerce IO_All,
from Str,
via {
io $_;
@@ -27,6 +27,8 @@
return $s;
};
+$global->coercion(IO_All->coercion);
+
1;
__END__
@@ -39,10 +41,10 @@
package Foo;
use Moose;
- use MooseX::Types::IO::All;
+ use MooseX::Types::IO::All 'IO_All';
has io => (
- isa => "IO::All",
+ isa => IO_All,
is => "rw",
coerce => 1,
);
diff -ruN MooseX-Types-IO-0.02-jqvzlJ/lib/MooseX/Types/IO_Global.pm /home/rkitover/src/moose/mxtypes-io/lib/MooseX/Types/IO_Global.pm
--- MooseX-Types-IO-0.02-jqvzlJ/lib/MooseX/Types/IO_Global.pm 1969-12-31 16:00:00.000000000 -0800
+++ /home/rkitover/src/moose/mxtypes-io/lib/MooseX/Types/IO_Global.pm 2009-05-18 13:14:30.000000000 -0700
@@ -0,0 +1,12 @@
+package MooseX::Types::IO_Global;
+
+use strict;
+use warnings;
+use Moose::Util::TypeConstraints;
+use MooseX::Types::IO 'IO';
+
+my $global = subtype 'IO' => as IO;
+
+$global->coercion(IO->coercion);
+
+1;
diff -ruN MooseX-Types-IO-0.02-jqvzlJ/lib/MooseX/Types/IO.pm /home/rkitover/src/moose/mxtypes-io/lib/MooseX/Types/IO.pm
--- MooseX-Types-IO-0.02-jqvzlJ/lib/MooseX/Types/IO.pm 2008-10-19 06:11:00.000000000 -0700
+++ /home/rkitover/src/moose/mxtypes-io/lib/MooseX/Types/IO.pm 2009-05-18 13:00:08.000000000 -0700
@@ -9,13 +9,13 @@
use IO qw/File Handle/;
use IO::String;
-use MooseX::Types::Moose qw/Str ScalarRef FileHandle ArrayRef/;
+use MooseX::Types::Moose qw/Str ScalarRef FileHandle ArrayRef Object/;
use namespace::clean;
use MooseX::Types -declare => [qw( IO )];
-subtype 'IO', as 'Object';
+subtype IO, as Object;
-coerce 'IO',
+coerce IO,
from Str,
via {
my $fh = new IO::File; $fh->open($_); return $fh;
@@ -24,11 +24,13 @@
via {
IO::String->new($$_);
},
- from 'ArrayRef[FileHandle|Str]',
+ from ArrayRef[FileHandle|Str],
via {
IO::Handle->new_from_fd( @$_ );
};
+require MooseX::Types::IO_Global;
+
1;
__END__
@@ -41,10 +43,10 @@
package Foo;
use Moose;
- use MooseX::Types::IO;
+ use MooseX::Types::IO 'IO';
has io => (
- isa => "IO",
+ isa => IO,
is => "rw",
coerce => 1,
);
diff -ruN MooseX-Types-IO-0.02-jqvzlJ/t/01-io.t /home/rkitover/src/moose/mxtypes-io/t/01-io.t
--- MooseX-Types-IO-0.02-jqvzlJ/t/01-io.t 2008-10-19 06:11:00.000000000 -0700
+++ /home/rkitover/src/moose/mxtypes-io/t/01-io.t 2009-05-18 13:12:53.000000000 -0700
@@ -1,35 +1,45 @@
#!perl -T
-use Test::More tests => 11;
+use Test::More tests => 22;
use Test::Exception;
-use MooseX::Types::IO;
+use MooseX::Types::IO 'IO';
use FindBin qw/$Bin/;
use Moose::Util::TypeConstraints;
+isa_ok( find_type_constraint(IO), "Moose::Meta::TypeConstraint" );
isa_ok( find_type_constraint('IO'), "Moose::Meta::TypeConstraint" );
{
{
package Foo;
use Moose;
+ use MooseX::Types::IO 'IO';
has io => (
- isa => "IO",
+ isa => IO,
is => "rw",
coerce => 1,
);
- }
- my $str = "test for IO::String\n line 2";
- my $coerced = Foo->new( io => \$str )->io;
+ # global type
+ has io2 => (
+ isa => 'IO',
+ is => "rw",
+ coerce => 1,
+ );
+ }
- isa_ok( $coerced, "IO::String", "coerced IO::String" );
- ok( $coerced->can('print'), "can print" );
- is(do { local $/; <$coerced> }, $str, 'get string');
-
- my $filename = "$Bin/00-load.t";
- my $str2 = <<'FC';
+ for my $accessor (qw/io io2/) {
+ my $str = "test for IO::String\n line 2";
+ my $coerced = Foo->new( $accessor => \$str )->$accessor;
+
+ isa_ok( $coerced, "IO::String", "coerced IO::String" );
+ ok( $coerced->can('print'), "can print" );
+ is(do { local $/; <$coerced> }, $str, 'get string');
+
+ my $filename = "$Bin/00-load.t";
+ my $str2 = <<'FC';
#!perl -T
use Test::More tests => 1;
@@ -40,18 +50,19 @@
diag( "Testing MooseX::Types::IO $MooseX::Types::IO::VERSION, Perl $], $^X" );
FC
- my $coerced2 = Foo->new( io => $filename )->io;
- isa_ok( $coerced2, "IO::File", "coerced IO::File" );
- ok( $coerced2->can('print'), "can print" );
- is(do { local $/; <$coerced2> }, $str2, 'get string');
-
- open(my $fh, '<', $filename);
- my $coerced3 = Foo->new( io => [ $fh, '<' ] )->io;
- isa_ok( $coerced3, "IO::Handle", "coerced IO::Handle" );
- ok( $coerced3->can('print'), "can print" );
- is(do { local $/; <$coerced3> }, $str2, 'get string');
-
- throws_ok { Foo->new( io => [\$str2] ) } qr/IO/, "constraint";
+ my $coerced2 = Foo->new( $accessor => $filename )->$accessor;
+ isa_ok( $coerced2, "IO::File", "coerced IO::File" );
+ ok( $coerced2->can('print'), "can print" );
+ is(do { local $/; <$coerced2> }, $str2, 'get string');
+
+ open(my $fh, '<', $filename);
+ my $coerced3 = Foo->new( $accessor => [ $fh, '<' ] )->$accessor;
+ isa_ok( $coerced3, "IO::Handle", "coerced IO::Handle" );
+ ok( $coerced3->can('print'), "can print" );
+ is(do { local $/; <$coerced3> }, $str2, 'get string');
+
+ throws_ok { Foo->new( $accessor => [\$str2] ) } qr/IO/, "constraint";
+ }
}
diff -ruN MooseX-Types-IO-0.02-jqvzlJ/t/02-io-all.t /home/rkitover/src/moose/mxtypes-io/t/02-io-all.t
--- MooseX-Types-IO-0.02-jqvzlJ/t/02-io-all.t 2008-10-19 06:11:00.000000000 -0700
+++ /home/rkitover/src/moose/mxtypes-io/t/02-io-all.t 2009-05-18 13:18:05.000000000 -0700
@@ -1,35 +1,45 @@
#!perl -T
-use Test::More tests => 8;
+use Test::More tests => 16;
use Test::Exception;
-use MooseX::Types::IO::All;
+use MooseX::Types::IO::All 'IO_All';
use FindBin qw/$Bin/;
use Moose::Util::TypeConstraints;
+isa_ok( find_type_constraint(IO_All), "Moose::Meta::TypeConstraint" );
isa_ok( find_type_constraint('IO::All'), "Moose::Meta::TypeConstraint" );
{
{
package Foo;
use Moose;
+ use MooseX::Types::IO::All 'IO_All';
has io => (
- isa => "IO::All",
+ isa => IO_All,
is => "rw",
coerce => 1,
);
- }
- my $str = "test for IO::All\n line 2";
- my $coerced = Foo->new( io => \$str )->io;
+ # global
+ has io2 => (
+ isa => 'IO::All',
+ is => "rw",
+ coerce => 1,
+ );
+ }
- isa_ok( $coerced, "IO::All", "coerced IO::All" );
- ok( $coerced->can('print'), "can print" );
- is( ${ $coerced->string_ref }, $str, 'get string');
-
- my $filename = "$Bin/00-load.t";
- my $str2 = <<'FC';
+ for my $accessor (qw/io io2/) {
+ my $str = "test for IO::All\n line 2";
+ my $coerced = Foo->new( io2 => \$str )->io2;
+
+ isa_ok( $coerced, "IO::All", "coerced IO::All" );
+ ok( $coerced->can('print'), "can print" );
+ is( ${ $coerced->string_ref }, $str, 'get string');
+
+ my $filename = "$Bin/00-load.t";
+ my $str2 = <<'FC';
#!perl -T
use Test::More tests => 1;
@@ -40,12 +50,13 @@
diag( "Testing MooseX::Types::IO $MooseX::Types::IO::VERSION, Perl $], $^X" );
FC
- my $coerced2 = Foo->new( io => $filename )->io;
- isa_ok( $coerced2, "IO::All", "coerced IO::All" );
- ok( $coerced2->can('print'), "can print" );
- is( $coerced2->all, $str2, 'get string');
+ my $coerced2 = Foo->new( io2 => $filename )->io2;
+ isa_ok( $coerced2, "IO::All", "coerced IO::All" );
+ ok( $coerced2->can('print'), "can print" );
+ is( $coerced2->all, $str2, 'get string');
- throws_ok { Foo->new( io => [\$str2] ) } qr/IO\:\:All/, "constraint";
+ throws_ok { Foo->new( io2 => [\$str2] ) } qr/IO\:\:All/, "constraint";
+ }
}