The Perl and Raku Conference 2025: Greenville, South Carolina - June 27-29 Learn more

use strict;
package MyTest;
use Test::More 'no_plan';
sub foo :Methodical {
return [ @_ ];
}
sub _extra { () }
sub bar {
my $self = shift;
#use Data::Dump::Streamer; Dump(\&foo)->To(\*STDERR)->Out;
is_deeply(
foo(1),
[ $self->_extra, $self, 1 ],
"methodical as function",
);
is_deeply(
$self->foo(1),
[ $self->_extra, $self, 1 ],
"methodical as method",
);
}
BEGIN { our @ISA = qw(MyTest) }
BEGIN { our @ISA = qw(MyTest) }
# if any other functions that referenced foo() were also overridden, this would
# need to be explicitly :Methodical, but if they aren't, this works
sub foo {
return [ 'OVERRIDE', @_ ];
}
sub _extra { 'OVERRIDE' }
BEGIN { our @ISA = 'MyTest' }
use Sub::Methodical -inherit;
sub bar {
my $self = shift;
is_deeply(
foo(1),
[ $self->_extra, $self, 1 ],
"methodical as function (AUTOLOAD)",
);
eval { baz(); };
like $@, qr/Undefined subroutine &MyTest::OverrideBar::baz called/,
'AUTOLOAD does not interfere with normal missing function mechanism';
}
BEGIN { our @ISA = 'MyTest' }
use Sub::Methodical -auto, -inherit;
sub foo {
return [ 'MOREOVER', @_ ];
}
sub _extra { 'MOREOVER' }
sub bar {
my $self = shift;
is_deeply(
foo(1),
[ $self->_extra, $self, 1 ],
"methodical as function (AUTOLOAD)",
);
}
no Test::More;
package main;
MyTest->bar;
MyTest::Derived->bar;
MyTest::OverrideFoo->bar;
MyTest::OverrideBar->bar;
MyTest::OverrideAll->bar;