From 2be1999433aacb32b8ae2a9b3991070a72b47957 Mon Sep 17 00:00:00 2001 From: ben hengst Date: Tue, 26 Mar 2013 00:40:25 -0700 Subject: [PATCH] resolve the missing methods from #42 seems ->isa() already does what is_instance was intended to do thus I am dropping is_instance. --- Build.PL | 1 + lib/perl5i.pm | 7 +++++++ lib/perl5i/2/Meta.pm | 10 ++++++++++ lib/perl5i/Meta.pod | 19 +++++++++++++++++++ t/Meta/methods.t | 9 +++++++++ 5 files changed, 46 insertions(+) diff --git a/Build.PL b/Build.PL index a18aa03..27283c8 100644 --- a/Build.PL +++ b/Build.PL @@ -52,6 +52,7 @@ my $builder = MyBuild->new( 'utf8::all' => '0.002', 'Carp::Fix::1_25' => '1.000000', 'Hash::StoredIterator' => '0.001', + 'Scalar::Util' => '1.25', }, build_requires => { 'ExtUtils::CBuilder' => '0.26', diff --git a/lib/perl5i.pm b/lib/perl5i.pm index 163df5e..c031eac 100644 --- a/lib/perl5i.pm +++ b/lib/perl5i.pm @@ -113,6 +113,13 @@ the object which were previously over complicated. For example... # the reference type of the object my $reftype = $obj->mo->reftype; + # test to see if object is a reference + print 'is ref' if $obj->is_ref; + + # test to see if object is a blessed object + print 'is object' if $obj->is_object + print 'is blessed' if $obj->is_blessed + A meta object is used to avoid polluting the global method space. C was chosen to avoid clashing with Moose's meta object. diff --git a/lib/perl5i/2/Meta.pm b/lib/perl5i/2/Meta.pm index 5ee75dd..192a3e5 100644 --- a/lib/perl5i/2/Meta.pm +++ b/lib/perl5i/2/Meta.pm @@ -7,10 +7,12 @@ use 5.010_000; # Be very careful not to import anything. require Carp::Fix::1_25; require mro; +require Scalar::Util; require perl5i::2::Meta::Instance; require perl5i::2::Meta::Class; + sub UNIVERSAL::mo { # Be careful to pass through an alias, not a copy return perl5i::2::Meta::Instance->new($_[0]); @@ -141,4 +143,12 @@ sub super { $class->$method_not_found($method); } +sub is_ref { + my $self = shift; + ref $self ? 1 : 0; +} + +sub is_blessed { Scalar::Util::blessed shift; } +sub is_object { Scalar::Util::blessed shift; } + 1; diff --git a/lib/perl5i/Meta.pod b/lib/perl5i/Meta.pod index b612ca2..6815f66 100644 --- a/lib/perl5i/Meta.pod +++ b/lib/perl5i/Meta.pod @@ -264,6 +264,25 @@ Examples: my $uri = URI->new("http://www.perl.org"); $uri->mo->is_equal("http://www.perl.org") # True + +=head3 is_ref + + $object->mo->is_ref; + +Return boolean value based on if mo->reftype exists. + +=head3 is_object + + $object->mo->is_object + +Return boolean value based on if $object is a blessed reference. + +=head3 is_blessed + + $object->mo->is_blessed + +Return boolean value based on if $object is a blessed reference. + =head3 perl Same as L. For backwards compatibility. diff --git a/t/Meta/methods.t b/t/Meta/methods.t index 0b624b4..a543ebe 100644 --- a/t/Meta/methods.t +++ b/t/Meta/methods.t @@ -108,5 +108,14 @@ SKIP: { can_ok "Fcntl", @methods; } +note "meta scalar utils: issue #42"; { + ok []->mo->is_ref, q{is_ref}; + my $obj = bless {}, 'THING'; + ok $obj->mo->is_object, q{is_object}; + ok $obj->mo->is_blessed, q{is_blessed}; + ok $obj->isa('THING'), q{isa thing}; + ok $obj->isa('HASH'), q{isa HASH}; +} + done_testing;