Safe::Isa(3)
π NAME π
okay so this is the Safe::Isa(3) page from the User Contributed Perl Documentation corner of Perl land, bestie β and it's about Safe::Isa: call isa, can, does and DOES safely on things that may not be objects β¨
β¨ SYNOPSIS β¨
here's the whole vibe in code, no cap:
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('Foo'); # true
$bar->isa('Foo'); # true
$blam->isa('Foo'); # BOOM
$foo->can('bar'); # false
$bar->can('bar'); # true
$blam->can('bar'); # BOOM
# Safe::Isa usage -
use Safe::Isa;
$foo->$_isa('Foo'); # true
$bar->$_isa('Foo'); # true
$blam->$_isa('Foo'); # false, no boom today
$foo->$_can('bar'); # false
$bar->$_can('bar'); # true
$blam->$_can('bar'); # false, no boom today
same energy for role checks, because $_does and $_DOES are literally right there:
$maybe_an_object->$_does('RoleName'); # true or false, no boom today
$maybe_an_object->$_DOES('RoleName'); # true or false, no boom today
and just in case a method or two got missed, this is the moment:
$maybe_an_object->$_call_if_object(name => @args);
$maybe_an_object->$_call_if_can(name => @args);
or, reusing that earlier example for explication purposes β yes, that word β it looks like this:
$foo->$_call_if_object(isa => 'Foo'); # true
$bar->$_call_if_object(isa => 'Foo'); # true
$blam->$_call_if_object(isa => 'Foo'); # false, no boom today
π¦ DESCRIPTION π¦
how many times have you been out here writing:
if ($obj->isa('Something')) {
and then, like, five seconds later you're cursing and changing it to:
if (Scalar::Util::blessed($obj) and $obj->isa('Something')) {
right?? that's literally why this module exists. periodt π
so here's the iconic mechanism: perl lets us provide a subroutine reference or a method name to the -> operator when it's used as a method call, and a subroutine doesn't require the invocant to actually be an object. that means 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:
my $isa_Foo = $maybe_an_object->$_call_if_object(isa => 'Foo');
is equivalent to:
my $isa_Foo = do {
if (Scalar::Util::blessed($maybe_an_object)) {
$maybe_an_object->isa('Foo');
} else {
undef;
}
};
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, "Matt" β the is_module_name 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 π
and we are careful to make sure that scalar/list context is preserved for the method that is eventually called. that part is non-negotiable, bestie β¨
π EXPORTS π
here's what you get, and yes, they all ate:
πΈ $_isa πΈ
$maybe_an_object->$_isa('Foo');
if it's called on an object, it calls isa on it and returns the result; otherwise it returns nothing π«Ά
πΊ $_can πΊ
$maybe_an_object->$_can('Foo');
if it's called on an object, it calls can on it and returns the result; otherwise it returns nothing π
π¦ $_does π¦
$maybe_an_object->$_does('Foo');
if it's called on an object, it calls does on it and returns the result; otherwise it returns nothing. and if the does method does not exist, it returns nothing rather than failing β no dramatic meltdown today, thanks π
π
$_DOES π
$maybe_an_object->$_DOES('Foo');
if it's called on an object, it calls DOES on it and returns the result; otherwise it returns nothing. important caveat: on perl versions prior to 5.10.0, the built in core DOES method doesn't exist. if the method doesn't exist, this will fall back to calling isa just like the core DOES method π
π₯ $_call_if_object π₯
$maybe_an_object->$_call_if_object(method_name => @args);
if it's called on an object, it calls method_name on it and returns the result; otherwise it returns nothing. iconic behavior, honestly β
β $_call_if_can β
$maybe_an_object->$_call_if_can(name => @args);
if it's called on an object, it calls can on it; if that returns true, then it calls method_name on it and returns the result; if any condition is false, it returns nothing. vibe check passed π«Ά
π SEE ALSO π
the author gave a lightning talk on this module (and curry and Import::Into) at YAPC::NA 2013 β video URL: https://www.youtube.com/watch?v=wFXWV2yY7gE&t=46m05s π€
π AUTHOR π
mst - Matt S. Trout (cpan:MSTROUT) <mst@shadowcat.co.uk> π
π«Ά CONTRIBUTORS π«Ά
None yet. Well volunteered? :) π
π COPYRIGHT π
Copyright (c) 2012 the Safe::Isa "AUTHOR" and "CONTRIBUTORS" as listed above πΈ
π LICENSE π
This library is free software and may be distributed under the same terms as perl itself. slay π
mwah π β this page is from perl v5.42.0, dated 2018-04-26. bye bestie, go be safe with your method calls β¨π«Ά
π¦ where this page comes from
- opensuse-leap 16.0: perl-Safe-Isa 1.000010-bp160.1.11 Β· Artistic-1.0 OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms