Debian Codegraph @

tests/overloaded

raw
1 #!/usr/bin/perl
2
3 # Copyright 2003-2026, Paul Johnson ([email protected])
4
5 # This software is free. It is licensed under the same terms as Perl itself.
6
7 # The latest version of this software should be available from my homepage:
8 # https://pjcj.net
9
10 # Example stolen from overload documentation
11
12 # __COVER__ changes s/^17 +3 +3/17 2 2/
13 # __COVER__ changes s/^(str +)3( tests/overloaded:17)/${1}2$2/
14
15 package two_face;
16
17 sub new { my $p = shift; bless [@_], $p }
18 use overload '""' => \&str, '0+' => \&num, fallback => 1;
19 sub num {shift->[1]}
20 sub str {shift->[0]}
21
22 package main;
23
24 my $seven = new two_face ("vii", 7);
25 printf "seven=$seven, seven=%d, eight=%d\n", $seven, $seven+1;
26 print "seven contains `i'\n" if $seven =~ /i/;