line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
## ---------------------------------------------------------------------------- |
2
|
|
|
|
|
|
|
# Contextual::Call |
3
|
|
|
|
|
|
|
# ----------------------------------------------------------------------------- |
4
|
|
|
|
|
|
|
# Mastering programmed by YAMASHINA Hio |
5
|
|
|
|
|
|
|
# |
6
|
|
|
|
|
|
|
# Copyright 2007 YAMASHINA Hio |
7
|
|
|
|
|
|
|
# ----------------------------------------------------------------------------- |
8
|
|
|
|
|
|
|
# $Id$ |
9
|
|
|
|
|
|
|
# ----------------------------------------------------------------------------- |
10
|
|
|
|
|
|
|
package Contextual::Call; |
11
|
2
|
|
|
2
|
|
49694
|
use strict; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
82
|
|
12
|
2
|
|
|
2
|
|
11
|
use warnings; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
166
|
|
13
|
2
|
|
|
2
|
|
11
|
use base qw(Exporter); |
|
2
|
|
|
|
|
8
|
|
|
2
|
|
|
|
|
1035
|
|
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
our @EXPORT_OK = qw(ccall); |
16
|
|
|
|
|
|
|
our %EXPORT_TAGS = ( all => \@EXPORT_OK ); |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
our $VERSION = '0.01'; |
19
|
|
|
|
|
|
|
1; |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
# ----------------------------------------------------------------------------- |
22
|
|
|
|
|
|
|
# my $result = ccall \⊂ |
23
|
|
|
|
|
|
|
# |
24
|
|
|
|
|
|
|
sub ccall ($;$) |
25
|
|
|
|
|
|
|
{ |
26
|
6
|
|
|
6
|
1
|
5507
|
my $wantarray; |
27
|
|
|
|
|
|
|
my $sub; |
28
|
|
|
|
|
|
|
|
29
|
6
|
100
|
|
|
|
26
|
@_ or die "ccall: argument required"; |
30
|
5
|
100
|
|
|
|
21
|
if( UNIVERSAL::isa($_[0], 'CODE') ) |
31
|
|
|
|
|
|
|
{ |
32
|
3
|
|
|
|
|
20
|
$wantarray = (caller(1))[5]; |
33
|
3
|
|
|
|
|
7
|
$sub = shift; |
34
|
|
|
|
|
|
|
}else |
35
|
|
|
|
|
|
|
{ |
36
|
2
|
|
|
|
|
3
|
$wantarray = shift; |
37
|
2
|
|
|
|
|
2
|
$sub = shift; |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
|
40
|
5
|
|
|
|
|
29
|
Contextual::Call->new({ |
41
|
|
|
|
|
|
|
context => $wantarray, |
42
|
|
|
|
|
|
|
sub => $sub, |
43
|
|
|
|
|
|
|
}); |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
# ----------------------------------------------------------------------------- |
47
|
|
|
|
|
|
|
# $pkg->new({ context => wantarray, sub => \&sub }); |
48
|
|
|
|
|
|
|
# |
49
|
|
|
|
|
|
|
sub new |
50
|
|
|
|
|
|
|
{ |
51
|
5
|
|
|
5
|
1
|
8
|
my $pkg = shift; |
52
|
5
|
|
|
|
|
6
|
my $opts = shift; |
53
|
|
|
|
|
|
|
|
54
|
5
|
|
|
|
|
8
|
my $wantarray = $opts->{context}; |
55
|
5
|
|
|
|
|
7
|
my $sub = $opts->{sub}; |
56
|
5
|
|
|
|
|
24
|
my @result; |
57
|
|
|
|
|
|
|
|
58
|
5
|
100
|
|
|
|
15
|
if( $wantarray ) |
|
|
100
|
|
|
|
|
|
59
|
|
|
|
|
|
|
{ |
60
|
|
|
|
|
|
|
# list context. |
61
|
1
|
|
|
|
|
5
|
@result = $sub->(@_); |
62
|
|
|
|
|
|
|
}elsif( defined($wantarray) ) |
63
|
|
|
|
|
|
|
{ |
64
|
|
|
|
|
|
|
# scalar context. |
65
|
2
|
|
|
|
|
5
|
$result[0] = $sub->(@_); |
66
|
|
|
|
|
|
|
}else |
67
|
|
|
|
|
|
|
{ |
68
|
|
|
|
|
|
|
# void context. |
69
|
2
|
|
|
|
|
7
|
$sub->(@_); |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
|
72
|
5
|
|
|
|
|
29
|
my $this = bless {}, __PACKAGE__; |
73
|
5
|
|
|
|
|
19
|
$this->{context} = $wantarray; |
74
|
5
|
|
|
|
|
11
|
$this->{result} = \@result; |
75
|
|
|
|
|
|
|
|
76
|
5
|
|
|
|
|
16
|
$this; |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
# ----------------------------------------------------------------------------- |
80
|
|
|
|
|
|
|
# $cresult->result(); |
81
|
|
|
|
|
|
|
# |
82
|
|
|
|
|
|
|
sub result |
83
|
|
|
|
|
|
|
{ |
84
|
10
|
|
|
10
|
1
|
2937
|
my $this = shift; |
85
|
10
|
|
|
|
|
16
|
my $wantarray = $this->{context}; |
86
|
10
|
100
|
|
|
|
26
|
if( $wantarray ) |
|
|
100
|
|
|
|
|
|
87
|
|
|
|
|
|
|
{ |
88
|
|
|
|
|
|
|
# list context. |
89
|
2
|
|
|
|
|
3
|
@{$this->{result}}; |
|
2
|
|
|
|
|
29
|
|
90
|
|
|
|
|
|
|
}elsif( defined($wantarray) ) |
91
|
|
|
|
|
|
|
{ |
92
|
|
|
|
|
|
|
# scalar context. |
93
|
4
|
|
|
|
|
22
|
$this->{result}->[0]; |
94
|
|
|
|
|
|
|
}else |
95
|
|
|
|
|
|
|
{ |
96
|
|
|
|
|
|
|
# void context. |
97
|
4
|
|
|
|
|
19
|
return; |
98
|
|
|
|
|
|
|
} |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
# ----------------------------------------------------------------------------- |
102
|
|
|
|
|
|
|
# End of Module. |
103
|
|
|
|
|
|
|
# ----------------------------------------------------------------------------- |
104
|
|
|
|
|
|
|
# ----------------------------------------------------------------------------- |
105
|
|
|
|
|
|
|
# End of File. |
106
|
|
|
|
|
|
|
# ----------------------------------------------------------------------------- |
107
|
|
|
|
|
|
|
__END__ |