File: test45-overload.p

package info (click to toggle)
libdevel-nytprof-perl 6.12%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 2,552 kB
  • sloc: perl: 5,616; javascript: 4,033; ansic: 107; makefile: 23
file content (29 lines) | stat: -rw-r--r-- 596 bytes parent folder | download | duplicates (7)
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
# test to see that 

# example from the overload docs (with slight changes)
{
package two_face; # Scalars with separate string and numeric values.

use overload
    '""' => \&str,            # ref to named sub
    '0+' => sub {shift->[0]}, # ref to anon sub
    '&{}' => "code",          # name of method
    fallback => 1;

sub new {
    my $p = shift;
    bless [@_], $p
}
sub str {
    shift->[0]
}
sub code {
    sub { 1 }
}

}

my $seven = new two_face ("vii", 7);
printf "seven=$seven, seven=%d, eight=%d\n", $seven, $seven+1;
print "seven contains ā€˜i’\n" if $seven =~ /i/;
$seven->();