develooper Front page | perl.perl5.porters | Postings from February 2021

Re: Blessed objects are `isa` their reftype

Thread Previous | Thread Next
From:
Paul "LeoNerd" Evans
Date:
February 17, 2021 10:15
Subject:
Re: Blessed objects are `isa` their reftype
Message ID:
20210217101458.789ec6d0@shy.leonerd.org.uk
On Tue, 16 Feb 2021 22:17:19 -0800
L A Walsh <astara@tlinx.org> wrote:

> So why introducing another new keyword when it's already in
> the core library?

$ref->isa(...)  crashes if $ref isn't a blessed object.

  $ perl -E 'say "ISA" if []->isa("ARRAY")'
  Can't call method "isa" on unblessed reference at -e line 1.

The `isa` operator does not

  $ perl -E 'use experimental qw(isa); say "ISA" if [] isa ARRAY; say "OK"'
  OK


This makes it much safer:

  if(blessed $obj and $obj->isa($class))

can become simply

  if($obj isa $class)

-- 
Paul "LeoNerd" Evans

leonerd@leonerd.org.uk      |  https://metacpan.org/author/PEVANS
http://www.leonerd.org.uk/  |  https://www.tindie.com/stores/leonerd/

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About