From: Yuval Kogman Date: Mon, 21 Jan 2008 17:33:22 +0000 (+0000) Subject: FileHandle also accepts IO::Handle objects X-Git-Tag: 0_35~11 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=128c601edf534a1423a87b68cf3a37a18391e7b8;p=gitmo%2FMoose.git FileHandle also accepts IO::Handle objects --- diff --git a/lib/Moose/Util/TypeConstraints.pm b/lib/Moose/Util/TypeConstraints.pm index 3f4d12d..b771149 100644 --- a/lib/Moose/Util/TypeConstraints.pm +++ b/lib/Moose/Util/TypeConstraints.pm @@ -443,7 +443,7 @@ subtype 'GlobRef' => as 'Ref' => where { ref($_) eq 'GLOB' } => optimize_as # but a GLOB ref is not always a filehandle subtype 'FileHandle' => as 'GlobRef' - => where { Scalar::Util::openhandle($_) } + => where { Scalar::Util::openhandle($_) || ( blessed($_) && $_->isa("IO::Handle") ) } => optimize_as \&Moose::Util::TypeConstraints::OptimizedConstraints::FileHandle; # NOTE: diff --git a/lib/Moose/Util/TypeConstraints/OptimizedConstraints.pm b/lib/Moose/Util/TypeConstraints/OptimizedConstraints.pm index d45d30b..5a6607b 100644 --- a/lib/Moose/Util/TypeConstraints/OptimizedConstraints.pm +++ b/lib/Moose/Util/TypeConstraints/OptimizedConstraints.pm @@ -28,7 +28,7 @@ sub Int { defined($_[0]) && !ref($_[0]) && $_[0] =~ /^-?[0-9]+$/ } sub GlobRef { ref($_[0]) eq 'GLOB' } } -sub FileHandle { ref($_[0]) eq 'GLOB' && Scalar::Util::openhandle($_[0]) } +sub FileHandle { ref($_[0]) eq 'GLOB' && Scalar::Util::openhandle($_[0]) or blessed($_[0]) && $_[0]->isa("IO::Handle") } sub Object { blessed($_[0]) && blessed($_[0]) ne 'Regexp' } diff --git a/t/040_type_constraints/003_util_std_type_constraints.t b/t/040_type_constraints/003_util_std_type_constraints.t index 2961044..83dbaef 100644 --- a/t/040_type_constraints/003_util_std_type_constraints.t +++ b/t/040_type_constraints/003_util_std_type_constraints.t @@ -3,7 +3,7 @@ use strict; use warnings; -use Test::More tests => 271; +use Test::More tests => 273; use Test::Exception; use Scalar::Util (); @@ -20,6 +20,8 @@ my $GLOB_REF = \*GLOB_REF; my $fh; open($fh, '<', $0) || die "Could not open $0 for the test"; +my $fh_obj = bless {}, "IO::Handle"; # not really + Moose::Util::TypeConstraints->export_type_constraints_as_functions(); ok(defined Any(0), '... Any accepts anything'); @@ -247,6 +249,7 @@ ok(!defined GlobRef(sub {}), '... GlobRef rejects anything which is no ok(!defined GlobRef($SCALAR_REF), '... GlobRef rejects anything which is not a GlobRef'); ok(defined GlobRef($GLOB_REF), '... GlobRef accepts anything which is a GlobRef'); ok(defined GlobRef($fh), '... GlobRef accepts anything which is a GlobRef'); +ok(!defined GlobRef($fh_obj), '... GlobRef rejects anything which is not a GlobRef'); ok(!defined GlobRef(qr/../), '... GlobRef rejects anything which is not a GlobRef'); ok(!defined GlobRef(bless {}, 'Foo'), '... GlobRef rejects anything which is not a GlobRef'); ok(!defined GlobRef(undef), '... GlobRef rejects anything which is not a GlobRef'); @@ -261,6 +264,7 @@ ok(!defined FileHandle(sub {}), '... FileHandle rejects anything which ok(!defined FileHandle($SCALAR_REF), '... FileHandle rejects anything which is not a FileHandle'); ok(!defined FileHandle($GLOB_REF), '... FileHandle rejects anything which is not a FileHandle'); ok(defined FileHandle($fh), '... FileHandle accepts anything which is a FileHandle'); +ok(defined FileHandle($fh_obj), '... FileHandle accepts anything which is a FileHandle'); ok(!defined FileHandle(qr/../), '... FileHandle rejects anything which is not a FileHandle'); ok(!defined FileHandle(bless {}, 'Foo'), '... FileHandle rejects anything which is not a FileHandle'); ok(!defined FileHandle(undef), '... FileHandle rejects anything which is not a FileHandle');