.\" -*- mode: troff; coding: utf-8 -*- .\" Automatically generated by Pod::Man v6.0.2 (Pod::Simple 3.45) .\" .\" 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 .. .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>. .ie n \{\ . ds C` "" . ds C' "" 'br\} .el\{\ . 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 .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" .\" Required to disable full justification in groff 1.23.0. .if n .ds AD l .\" ======================================================================== .\" .IX Title "Safe::Isa 3" .TH Safe::Isa 3 2025-12-14 "perl v5.42.0" "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\*(Aqs 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\*(Aqt 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\*(Aqs 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\*(Aqt handle trying class names, because many things are valid class names that you might not want to treat as one (like say "Matt") \- 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\*(Aqt exist. If the method doesn\*(Aqt 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 YAPC::NA 2013 . .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 "AUTHOR" and "CONTRIBUTORS" as listed above. .SH LICENSE .IX Header "LICENSE" This library is free software and may be distributed under the same terms as perl itself.