.\" Automatically generated by Pod::Man 4.09 (Pod::Simple 3.35) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .if !\nF .nr F 0 .if \nF>0 \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} .\} .\" ======================================================================== .\" .IX Title "Safe::Isa 3pm" .TH Safe::Isa 3pm "2018-05-06" "perl v5.26.2" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" Safe::Isa \- Call isa, can, does and DOES safely on things that may not be objects .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 2 \& use strict; \& use warnings; \& \& { package Foo; sub new { bless({}, $_[0]) } } \& { package Bar; our @ISA = qw(Foo); sub bar { 1 } } \& \& my $foo = Foo\->new; \& my $bar = Bar\->new; \& my $blam = [ 42 ]; \& \& # basic isa usage \- \& \& $foo\->isa(\*(AqFoo\*(Aq); # true \& $bar\->isa(\*(AqFoo\*(Aq); # true \& $blam\->isa(\*(AqFoo\*(Aq); # BOOM \& \& $foo\->can(\*(Aqbar\*(Aq); # false \& $bar\->can(\*(Aqbar\*(Aq); # true \& $blam\->can(\*(Aqbar\*(Aq); # BOOM \& \& # Safe::Isa usage \- \& \& use Safe::Isa; \& \& $foo\->$_isa(\*(AqFoo\*(Aq); # true \& $bar\->$_isa(\*(AqFoo\*(Aq); # true \& $blam\->$_isa(\*(AqFoo\*(Aq); # false, no boom today \& \& $foo\->$_can(\*(Aqbar\*(Aq); # false \& $bar\->$_can(\*(Aqbar\*(Aq); # true \& $blam\->$_can(\*(Aqbar\*(Aq); # false, no boom today .Ve .PP Similarly: .PP .Vb 2 \& $maybe_an_object\->$_does(\*(AqRoleName\*(Aq); # true or false, no boom today \& $maybe_an_object\->$_DOES(\*(AqRoleName\*(Aq); # true or false, no boom today .Ve .PP And just in case we missed a method or two: .PP .Vb 2 \& $maybe_an_object\->$_call_if_object(name => @args); \& $maybe_an_object\->$_call_if_can(name => @args); .Ve .PP Or to re-use a previous example for purposes of explication: .PP .Vb 3 \& $foo\->$_call_if_object(isa => \*(AqFoo\*(Aq); # true \& $bar\->$_call_if_object(isa => \*(AqFoo\*(Aq); # true \& $blam\->$_call_if_object(isa => \*(AqFoo\*(Aq); # false, no boom today .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" How many times have you found yourself writing: .PP .Vb 1 \& if ($obj\->isa(\*(AqSomething\*(Aq)) { .Ve .PP and then shortly afterwards cursing and changing it to: .PP .Vb 1 \& if (Scalar::Util::blessed($obj) and $obj\->isa(\*(AqSomething\*(Aq)) { .Ve .PP Right. That's why this module exists. .PP Since perl allows us to provide a subroutine reference or a method name to the \-> operator when used as a method call, and a subroutine doesn't require the invocant to actually be an object, we can create safe versions of isa, can and friends by using a subroutine reference that only tries to call the method if it's used on an object. So: .PP .Vb 1 \& my $isa_Foo = $maybe_an_object\->$_call_if_object(isa => \*(AqFoo\*(Aq); .Ve .PP is equivalent to .PP .Vb 7 \& my $isa_Foo = do { \& if (Scalar::Util::blessed($maybe_an_object)) { \& $maybe_an_object\->isa(\*(AqFoo\*(Aq); \& } else { \& undef; \& } \& }; .Ve .PP Note that we don't handle trying class names, because many things are valid class names that you might not want to treat as one (like say \*(L"Matt\*(R") \- the \&\f(CW\*(C`is_module_name\*(C'\fR function from Module::Runtime is a good way to check for something you might be able to call methods on if you want to do that. .PP We are careful to make sure that scalar/list context is preserved for the method that is eventually called. .SH "EXPORTS" .IX Header "EXPORTS" .ie n .SS "$_isa" .el .SS "\f(CW$_isa\fP" .IX Subsection "$_isa" .Vb 1 \& $maybe_an_object\->$_isa(\*(AqFoo\*(Aq); .Ve .PP If called on an object, calls \f(CW\*(C`isa\*(C'\fR on it and returns the result, otherwise returns nothing. .ie n .SS "$_can" .el .SS "\f(CW$_can\fP" .IX Subsection "$_can" .Vb 1 \& $maybe_an_object\->$_can(\*(AqFoo\*(Aq); .Ve .PP If called on an object, calls \f(CW\*(C`can\*(C'\fR on it and returns the result, otherwise returns nothing. .ie n .SS "$_does" .el .SS "\f(CW$_does\fP" .IX Subsection "$_does" .Vb 1 \& $maybe_an_object\->$_does(\*(AqFoo\*(Aq); .Ve .PP If called on an object, calls \f(CW\*(C`does\*(C'\fR on it and returns the result, otherwise returns nothing. If the \f(CW\*(C`does\*(C'\fR method does not exist, returns nothing rather than failing. .ie n .SS "$_DOES" .el .SS "\f(CW$_DOES\fP" .IX Subsection "$_DOES" .Vb 1 \& $maybe_an_object\->$_DOES(\*(AqFoo\*(Aq); .Ve .PP If called on an object, calls \f(CW\*(C`DOES\*(C'\fR on it and returns the result, otherwise returns nothing. On perl versions prior to 5.10.0, the built in core \f(CW\*(C`DOES\*(C'\fR method doesn't exist. If the method doesn't exist, this will fall back to calling \f(CW\*(C`isa\*(C'\fR just like the core \f(CW\*(C`DOES\*(C'\fR method. .ie n .SS "$_call_if_object" .el .SS "\f(CW$_call_if_object\fP" .IX Subsection "$_call_if_object" .Vb 1 \& $maybe_an_object\->$_call_if_object(method_name => @args); .Ve .PP If called on an object, calls \f(CW\*(C`method_name\*(C'\fR on it and returns the result, otherwise returns nothing. .ie n .SS "$_call_if_can" .el .SS "\f(CW$_call_if_can\fP" .IX Subsection "$_call_if_can" .Vb 1 \& $maybe_an_object\->$_call_if_can(name => @args); .Ve .PP If called on an object, calls \f(CW\*(C`can\*(C'\fR on it; if that returns true, then calls \f(CW\*(C`method_name\*(C'\fR on it and returns the result; if any condition is false returns nothing. .SH "SEE ALSO" .IX Header "SEE ALSO" I gave a lightning talk on this module (and curry and Import::Into) at \&\s-1YAPC::NA 2013\s0 . .SH "AUTHOR" .IX Header "AUTHOR" mst \- Matt S. Trout (cpan:MSTROUT) .SH "CONTRIBUTORS" .IX Header "CONTRIBUTORS" None yet. Well volunteered? :) .SH "COPYRIGHT" .IX Header "COPYRIGHT" Copyright (c) 2012 the Safe::Isa \*(L"\s-1AUTHOR\*(R"\s0 and \*(L"\s-1CONTRIBUTORS\*(R"\s0 as listed above. .SH "LICENSE" .IX Header "LICENSE" This library is free software and may be distributed under the same terms as perl itself.