Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

resolve the missing methods from #42 #249

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Build.PL
Original file line number Diff line number Diff line change
Expand Up @@ -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',
'Hash::FieldHash' => '0.06',
},
build_requires => {
Expand Down
7 changes: 7 additions & 0 deletions lib/perl5i.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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<mo> was chosen to avoid clashing with Moose's meta object.

Expand Down
10 changes: 10 additions & 0 deletions lib/perl5i/2/Meta.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down Expand Up @@ -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;
19 changes: 19 additions & 0 deletions lib/perl5i/Meta.pod
Original file line number Diff line number Diff line change
Expand Up @@ -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<as_perl>. For backwards compatibility.
Expand Down
9 changes: 9 additions & 0 deletions t/Meta/methods.t
Original file line number Diff line number Diff line change
Expand Up @@ -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;