1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
|
#!/usr/bin/perl
use v5.18;
use warnings;
use Test2::V0;
use Object::Pad 0.800;
use constant HAVE_SUBNAME => $^V ge v5.22;
use if HAVE_SUBNAME, 'Sub::Util' => qw( subname );
class Point :lexical_new
{
sub create
{
shift;
my ( $x, $y ) = @_;
return new( __PACKAGE__, x => $x, y => $y );
}
sub get_constructor { return \&new; }
field $x :param :reader;
field $y :param :reader;
}
{
my $p = Point->create( 10, 20 );
ok( defined $p, 'Lexically constructed class Point can ->create' );
is( $p->x, 10, 'Lexically constructed class instances have methods' );
ok( !defined &Point::new, '&Point::new is not in the symbol table' );
ok( !defined &new, 'my sub &new did not leak into lexical scope' );
if( HAVE_SUBNAME ) {
is( subname( Point->get_constructor ), "Point::new", 'subname of lexical constructor' );
}
}
class PointOverridingNew :lexical_new
{
method new :common
{
my ( $x, $y ) = @_;
return &new( __PACKAGE__, x => $x, y => $y );
}
field $x :param :reader;
field $y :param :reader;
}
{
my $p = PointOverridingNew->new( 10, 20 );
ok( defined $p, 'Lexically constructed class Point can invoke overridden ->new' );
is( $p->x, 10, 'Lexically constructed class saw correct constructor args' );
}
done_testing;
|