File: methext_bench.pl

package info (click to toggle)
libdata-util-perl 0.67-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 556 kB
  • sloc: perl: 2,958; ansic: 416; makefile: 8
file content (80 lines) | stat: -rw-r--r-- 1,211 bytes parent folder | download | duplicates (5)
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
#!perl -w

use strict;
use Benchmark qw(:all);

use FindBin qw($Bin);
use lib $Bin, "$Bin/../example/lib";
use Common;

{
	package Base;
	sub e{ $_[1] }

	sub f{ $_[1] }
	sub g{ $_[1] }
	sub h{ $_[1] }
	sub i{ $_[1] }
	sub j{ $_[1] }
}


sub around{
	my $next = shift;
	goto &{$next};
}

{
	package X;
	use parent -norequire => qw(Base);
	use Method::Modifiers;

	before f => sub{ };
	around g => \&main::around;
	after  h => sub{ };

	sub i{
		my $self = shift;
		$self->SUPER::i(@_);
	}
	Data::Util::install_subroutine(
		__PACKAGE__,
		j => Data::Util::modify_subroutine(__PACKAGE__->can('j')),
	);
}

signeture
	'Data::Util' => \&Data::Util::modify_subroutine,
;

print <<'END';
Calling extended methods:
	inher  - no extended, only inherited

	before - extended with :before modifier
	around - extended with :around modifier
	after  - extended with :after modifier
	super  - extended with SUPER:: pseudo class

END

cmpthese -1 => {
	inher => sub{
		X->e(42) == 42 or die;
	},
	before => sub{
		X->f(42) == 42 or die;
	},
	around => sub{
		X->g(42) == 42 or die;
	},
	after => sub{
		X->h(42) == 42 or die;
	},
	super => sub{
		X->i(42) == 42 or die;
	},
#	simple => sub{
#		X->j(42) == 42 or die;
#	},
};