blob: fcf1c53c81b7729070e1d9337bebca792e5b2e57 (
plain)
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
|
=pod
=encoding utf-8
=head1 PURPOSE
Test inlined Int type check clobbering C<< $1 >>.
=head1 SEE ALSO
L<https://rt.cpan.org/Ticket/Display.html?id=125132>.
=head1 AUTHOR
Marc Ballarin <marc.ballarin@1und1.de>.
Some modifications by Toby Inkster <tobyink@cpan.org>.
=head1 COPYRIGHT AND LICENCE
This software is copyright (c) 2018-2023 by Marc Ballarin.
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
=cut
use strict;
use warnings;
use Test::More;
use Type::Params qw(compile);
use Types::Standard qw(Str Int);
{
my $check;
sub check_int_tt_compile {
$check ||= compile(Int);
my ($int) = $check->(@_);
is($int, 123, 'check_int_tt_compile');
}
}
{
my $check;
sub check_str_tt {
$check ||= compile(Str);
my ($int) = $check->(@_);
is($int, 123, 'check_str_tt');
}
}
{
sub check_int_manual {
my ($int) = @_;
die "no Int!" unless $int =~ /^\d+$/;
is($int, 123, 'check_int_manual');
}
}
{
sub check_int_tt_no_compile {
my ($int) = @_;
Int->assert_valid($int);
is($int, 123, 'check_int_tt_no_compile');
}
}
my $string = 'a123';
subtest 'using temporary variable' => sub {
if ($string =~ /a(\d+)/) {
my $matched = $1;
check_int_tt_compile($matched);
check_int_manual($matched);
check_str_tt($matched);
check_int_tt_no_compile($matched);
}
};
subtest 'using direct $1' => sub {
if ($string =~ /a(\d+)/) {
check_int_tt_compile($1);
check_int_manual($1);
check_str_tt($1);
check_int_tt_no_compile($1);
}
};
done_testing;
|