1*5486feefSafresh1#!./perl 2*5486feefSafresh1 3*5486feefSafresh1BEGIN { 4*5486feefSafresh1 chdir 't' if -d 't'; 5*5486feefSafresh1 require './test.pl'; 6*5486feefSafresh1 set_up_inc('../lib'); 7*5486feefSafresh1 require Config; 8*5486feefSafresh1} 9*5486feefSafresh1 10*5486feefSafresh1use v5.36; 11*5486feefSafresh1use feature 'class'; 12*5486feefSafresh1no warnings 'experimental::class'; 13*5486feefSafresh1 14*5486feefSafresh1# reader accessors 15*5486feefSafresh1{ 16*5486feefSafresh1 class Testcase1 { 17*5486feefSafresh1 field $s :reader = "the scalar"; 18*5486feefSafresh1 19*5486feefSafresh1 field @a :reader = qw( the array ); 20*5486feefSafresh1 21*5486feefSafresh1 # Present-but-empty parens counts as default 22*5486feefSafresh1 field %h :reader() = qw( the hash ); 23*5486feefSafresh1 } 24*5486feefSafresh1 25*5486feefSafresh1 my $o = Testcase1->new; 26*5486feefSafresh1 is($o->s, "the scalar", '$o->s accessor'); 27*5486feefSafresh1 ok(eq_array([$o->a], [qw( the array )]), '$o->a accessor'); 28*5486feefSafresh1 ok(eq_hash({$o->h}, {qw( the hash )}), '$o->h accessor'); 29*5486feefSafresh1 30*5486feefSafresh1 is(scalar $o->a, 2, '$o->a accessor in scalar context'); 31*5486feefSafresh1 is(scalar $o->h, 1, '$o->h accessor in scalar context'); 32*5486feefSafresh1 33*5486feefSafresh1 # Read accessor does not permit arguments 34*5486feefSafresh1 ok(!eval { $o->s("value") }, 35*5486feefSafresh1 'Reader accessor fails with argument'); 36*5486feefSafresh1 like($@, qr/^Too many arguments for subroutine \'Testcase1::s\' \(got 1; expected 0\) at /, 37*5486feefSafresh1 'Failure from argument to accessor'); 38*5486feefSafresh1} 39*5486feefSafresh1 40*5486feefSafresh1# Alternative names 41*5486feefSafresh1{ 42*5486feefSafresh1 class Testcase2 { 43*5486feefSafresh1 field $f :reader(get_f) = "value"; 44*5486feefSafresh1 } 45*5486feefSafresh1 46*5486feefSafresh1 is(Testcase2->new->get_f, "value", 'accessor with altered name'); 47*5486feefSafresh1 48*5486feefSafresh1 ok(!eval { Testcase2->new->f }, 49*5486feefSafresh1 'Accessor with altered name does not also generate original name'); 50*5486feefSafresh1 like($@, qr/^Can't locate object method "f" via package "Testcase2" at /, 51*5486feefSafresh1 'Failure from lack of original name accessor'); 52*5486feefSafresh1} 53*5486feefSafresh1 54*5486feefSafresh1done_testing; 55