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

class TestCase::Module::Scope::Guard {
use Scope::Guard;
use Fn;
static method basic : int () {
# Executing the callback at the end of the scope
{
my $string = (mutable string)copy "abc";
{
my $callback = [has string : mutable string = $string] method : void () {
my $string = $self->{string};
$string->[0] = 'A';
$string->[1] = 'B';
};
my $guard = Scope::Guard->new($callback);
unless ($callback == $guard->callback) {
return 0;
}
$string->[0] = 'P';
}
unless ($string eq "ABc") {
return 0;
}
}
# Executing the callback at the end of the scope
{
my $string = (mutable string)copy "abc";
{
my $callback = [has string : mutable string = $string] method : void () {
my $string = $self->{string};
$string->[0] = 'A';
$string->[1] = 'B';
};
# Temporary local variable is created if the instance is not assigned to a local variable.
Scope::Guard->new($callback);
$string->[0] = 'P';
}
unless ($string eq "ABc") {
return 0;
}
}
# Exceptions
{
my $callback : Callback;
eval { Scope::Guard->new($callback); } ;
unless (Fn->contains($@, "The callback \$callback must be defined")) {
return 0;
}
}
$@ = undef;
return 1;
}
}