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:
20210217101536.72cc8e90@shy.leonerd.org.uk
On Tue, 16 Feb 2021 17:07:39 -0800
David Christensen <dpchrist@holgerdanske.com> wrote:

> On 2021-02-16 14:19, Paul "LeoNerd" Evans wrote:
> > On Tue, 16 Feb 2021 14:05:16 -0800
> > David Christensen wrote:  
> >> On 2021-02-16 13:17, Dan Book wrote:  
> >>> On Tue, Feb 16, 2021 at 4:14 PM David Christensen wrote:  
> >>>> On 2021-02-16 04:09, Paul "LeoNerd" Evans wrote:  
> >>>>> There is an odd asymmetry with isa when testing if an object is
> >>>>> derived from its reference type:  
> 
> >    $ perl5.32.1
> >    use feature 'say';
> >    use experimental 'isa';
> > 
> >    my $aref = [ 1, 2, 3 ];
> >    say "1.TRUE" if $aref isa ARRAY;
> > 
> >    my $aobj = bless [ 5, 6, 7 ], "MyArray::Class";
> >    say "2.TRUE" if $aobj isa MyArray::Class;
> >    say "3.TRUE" if $aobj isa ARRAY;
> >    ^D
> >    2.TRUE
> >    3.TRUE
> > 
> > The one numbered 3 is surprising.  
> 
> I agree that binary 'isa' in the case numbered 3 above does not
> appear to match this documentation:
> 
>      https://perldoc.perl.org/5.32.0/perlop#Class-Instance-Operator
> 
> because $aobj is not an object instance of the class 'ARRAY' or a 
> subclass derived from class 'ARRAY'.
> 
> 
> However, binary 'isa' does appear to be consistent with
> UNIVERSAL::isa() invoked as a function,

Indeed; as I wrote in the documentation:


 as shown in case 14 line 45,
> below. (But, UNIVERSAL::isa() invoked as an object method is broken
> -- case 11 line 41, case 12 line 42, case 15 line 46, and case 16
> line 47.)
> 
> 2021-02-16 16:57:03 dpchrist@samba 
> /var/local/samba/dpchrist/sandbox/perl/p5p
> $ freebsd-version ; uname -a ; perl -v | head -n 2
> 12.1-RELEASE-p13
> FreeBSD samba.tracy.holgerdanske.com 12.1-RELEASE-p13 FreeBSD 
> 12.1-RELEASE-p13 GENERIC  amd64
> 
> This is perl 5, version 32, subversion 1 (v5.32.1) built for 
> amd64-freebsd-thread-multi
> 
> 2021-02-16 17:04:26 dpchrist@samba 
> /var/local/samba/dpchrist/sandbox/perl/p5p
> $ cat paul-leonerd-evans-20210216-1419-dpc.pl
> # $Id: paul-leonerd-evans-20210216-1419-dpc.pl,v 1.7 2021/02/17
> 00:56:49 dpchrist Exp $
> 
> use strict;
> use warnings;
> use Scalar::Util	qw( reftype );
> use Test::More;
> 
> my $aref = [ 1, 2, 3 ];
> 
> my $aobj = bless [ 5, 6, 7 ], "MyArray::Class";
> 
> SKIP: {
>      skip 'Requires Perl version 5.32.0 or newer', 3
> 	unless 5.032000 < $];
> 
>      eval q{
>      	use experimental 'isa';
>      	#use feature 'isa';
> 	# isa is experimental at (eval 8) line 5.
> 	# isa is experimental at (eval 8) line 7.
> 	# isa is experimental at (eval 8) line 11.
> 
> 	is(($aref isa ARRAY), '', 'line ' .  __LINE__);
> 
> 	is(($aobj isa MyArray::Class), 1, 'line ' . __LINE__);
> 
> 	is(($aobj isa ARRAY), 1, 'line ' . __LINE__);
>      };
>      note $@ if $@;
> }
> 
> is ref($aref), 'ARRAY', 'line ' . __LINE__;
> is reftype($aref), 'ARRAY', 'line ' . __LINE__;
> is UNIVERSAL::isa($aref, 'ARRAY'), 1, 'line ' . __LINE__;
> is eval q{ $aref->isa('ARRAY') }, undef, 'line ' . __LINE__;
> note sprintf "line %i: %s", __LINE__, $@;
> isnt $@, '', 'line ' . __LINE__;
> 
> is ref($aobj), 'MyArray::Class', 'line ' . __LINE__;
> is UNIVERSAL::isa($aobj, 'MyArray::Class'), 1, 'line ' . __LINE__;
> is eval q{ $aobj->isa('MyArray::Class') }, 1, 'line ' . __LINE__;
> is $@, '', 'line ' . __LINE__;
> 
> is reftype($aobj), 'ARRAY', 'line ' . __LINE__;
> is UNIVERSAL::isa($aobj, 'ARRAY'), 1, 'line ' . __LINE__;
> is eval q{ $aobj->isa('ARRAY') }, 1, 'line ' . __LINE__;
> is $@, '', 'line ' . __LINE__;
> 
> done_testing;
> 
> 2021-02-16 17:04:28 dpchrist@samba 
> /var/local/samba/dpchrist/sandbox/perl/p5p
> $ perl paul-leonerd-evans-20210216-1419-dpc.pl
> ok 1 - line 8
> ok 2 - line 10
> ok 3 - line 12
> ok 4 - line 32
> ok 5 - line 33
> ok 6 - line 34
> ok 7 - line 35
> # line 36: Can't call method "isa" on unblessed reference at (eval
> 15) line 1.
> ok 8 - line 37
> ok 9 - line 39
> ok 10 - line 40
> not ok 11 - line 41
> #   Failed test 'line 41'
> #   at paul-leonerd-evans-20210216-1419-dpc.pl line 41.
> #          got: undef
> #     expected: '1'
> not ok 12 - line 42
> #   Failed test 'line 42'
> #   at paul-leonerd-evans-20210216-1419-dpc.pl line 42.
> #          got: 'Can't locate object method "isa" via package 
> "MyArray::Class" at (eval 19) line 1.
> # '
> #     expected: ''
> ok 13 - line 44
> ok 14 - line 45
> not ok 15 - line 46
> #   Failed test 'line 46'
> #   at paul-leonerd-evans-20210216-1419-dpc.pl line 46.
> #          got: undef
> #     expected: '1'
> not ok 16 - line 47
> #   Failed test 'line 47'
> #   at paul-leonerd-evans-20210216-1419-dpc.pl line 47.
> #          got: 'Can't locate object method "isa" via package 
> "MyArray::Class" at (eval 23) line 1.
> # '
> #     expected: ''
> 1..16
> # Looks like you failed 4 tests of 16.
> 
> 
> Here is a run on Perl 5.24.1 for comparison:
> 
> 2021-02-16 16:53:53 dpchrist@tinkywinky
> ~/samba/dpchrist/sandbox/perl/p5p $ cat /etc/debian_version; uname
> -a; perl -v | head -n 2 9.13
> Linux tinkywinky 4.9.0-13-amd64 #1 SMP Debian 4.9.228-1 (2020-07-05) 
> x86_64 GNU/Linux
> 
> This is perl 5, version 24, subversion 1 (v5.24.1) built for 
> x86_64-linux-gnu-thread-multi
> 
> 2021-02-16 17:06:02 dpchrist@tinkywinky
> ~/samba/dpchrist/sandbox/perl/p5p $ perl
> paul-leonerd-evans-20210216-1419-dpc.pl ok 1 # skip Requires Perl
> version 5.32.0 or newer ok 2 # skip Requires Perl version 5.32.0 or
> newer ok 3 # skip Requires Perl version 5.32.0 or newer
> ok 4 - line 32
> ok 5 - line 33
> ok 6 - line 34
> ok 7 - line 35
> # line 36: Can't call method "isa" on unblessed reference at (eval 9) 
> line 1.
> ok 8 - line 37
> ok 9 - line 39
> ok 10 - line 40
> ok 11 - line 41
> ok 12 - line 42
> ok 13 - line 44
> ok 14 - line 45
> ok 15 - line 46
> ok 16 - line 47
> 1..16
> 
> 
> David



-- 
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