You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: doc/Type/CallFrame.pod6
+44-27Lines changed: 44 additions & 27 deletions
Original file line number
Diff line number
Diff line change
@@ -2,25 +2,42 @@
2
2
3
3
=TITLEclass CallFrame
4
4
5
-
=SUBTITLECapturing current frame state
5
+
=SUBTITLECaptures the current frame state
6
6
7
7
class CallFrame {}
8
8
9
-
There's no way to instantiate a C<CallFrame>. Instead, it is captured from the current state of a program using the L<callframe|/routine/callframe> subroutine.
9
+
A CallFrame will be usually captured from the
10
+
current state of a program using the L<callframe|/routine/callframe> subroutine.
10
11
11
12
my $frame = callframe;
12
13
say "The above line of code ran at {$frame.file}:{$frame.line}.";
13
14
14
-
With no arguments the callframe will give you frame information for the line calling C<callframe>. The file and line annotations will be identical to those in
15
-
L«C<$?FILE>|/language/variables#Compile-time_variables» and L«C<$?LINE>|/language/variables#Compile-time_variables».
16
-
17
-
You may, however, pass a number to C<callframe> to specify a different frame level. The current frame is level 0 (though level of the frame may be different, see L<level|/routine/level> for more information). A positive number will move upward through the levels of frame. A negative number will move downward into the C<callframe> method and class itself at the point at which they are running to construct this information for you.
18
-
19
-
The frames themselves do not necessarily match to method or subroutine calls. Perl constructs a frames for blocks and such as well, so if you need a callframe for a particular method call, do not assume it is a fixed number of levels up.
20
-
21
-
Each frame stores L<annotations|/routine/annotations>, including the L<file|/routine/file> and L<line|/routine/line> annotations, which have convenience methods for accessing those directly. You can also retrieve a reference to the code block of the currently executing frame using the L<code|/routine/code> method. The frame also captures all lexical variables stored with the frame, which are available by calling L<my|/routine/my> on the frame object.
22
-
23
-
Here's a short example that will find the calling routine and prints the package of the caller using the C<callframe> interface.
15
+
With no arguments the callframe will give you frame information for the line
16
+
calling C<callframe>. The file and line annotations will be identical to those
17
+
in L«C<$?FILE>|/language/variables#Compile-time_variables» and
You may, however, pass a number to C<callframe> to specify a different frame
21
+
level. The current frame is level 0 (though level of the frame may be different,
22
+
see L<level|/routine/level> for more information). A positive number will move
23
+
upward through the levels of frame. A negative number will move downward into
24
+
the C<callframe> method and class itself at the point at which they are running
25
+
to construct this information for you.
26
+
27
+
The frames themselves do not necessarily match only method or subroutine calls.
28
+
Perl constructs a frames for blocks and such as well, so if you need a callframe
29
+
for a particular method call, do not assume it is a fixed number of levels up.
30
+
31
+
Each frame stores L<annotations|/routine/annotations>, including the
32
+
L<file|/routine/file> and L<line|/routine/line> annotations, which have
33
+
convenience methods for accessing those directly. You can also retrieve a
34
+
reference to the code block of the currently executing frame using the
35
+
L<code|/routine/code> method. The frame also captures all lexical variables
36
+
stored with the frame, which are available by calling L<my|/routine/my> on the
37
+
frame object.
38
+
39
+
Here's a short example that will find the calling routine and prints the package
40
+
of the caller using the C<callframe> interface.
24
41
25
42
sub calling-frame() {
26
43
for 1..* -> $level {
@@ -40,10 +57,15 @@ Here's a short example that will find the calling routine and prints the package
40
57
41
58
calling-frame;
42
59
43
-
If you just need to trace caller information, L<Backtrace|/type/Backtrace> may provide a better means of getting information. L<CallFrame|/type/CallFrame> contains more information about a specific frame, but provides a tedious interface for enumerating a call stack.
60
+
If you just need to trace caller information, L<Backtrace|/type/Backtrace> may
61
+
provide a better means of getting information. L<CallFrame|/type/CallFrame>
62
+
contains more information about a specific frame, but provides a tedious
63
+
interface for enumerating a call stack.
44
64
45
65
=head1Methods
46
66
67
+
B<Note> From version 6.d, C<.perl> can be called on C<CallFrame>.
68
+
47
69
=head2method code
48
70
49
71
method code(CallFrame:D:)
@@ -54,22 +76,18 @@ Return the callable code for the current block. When called on the object return
54
76
55
77
method file(CallFrame:D:)
56
78
57
-
This is a shortcut for looking up the C<file> annotation. Therefore, the following code prints C<True>.
79
+
This is a shortcut for looking up the C<file> annotation. Therefore, the
80
+
following code prints C<True>.
58
81
59
82
my $frame = callframe(0);
60
83
say $frame.file eq $frame.annotations<file>;
61
84
62
-
=head2method level
63
-
64
-
method level(CallFrame:D:)
65
-
66
-
Return the absolute level of the frame in the stack. This will usually be higher than 0 when calling C<callframe(0)>. Using a negative number to retrieve a frame less than 0 will result in an exception.
67
-
68
85
=head2method line
69
86
70
87
method line(CallFrame:D:)
71
88
72
-
This is a shortcut for looking up the C<line> annotation. For example, the following two calls are identical.
89
+
This is a shortcut for looking up the C<line> annotation. For example, the
90
+
following two calls are identical.
73
91
74
92
say callframe(1).line;
75
93
say callframe(1).annotations<line>;
@@ -78,9 +96,9 @@ This is a shortcut for looking up the C<line> annotation. For example, the follo
78
96
79
97
method annotations(--> Map:D)
80
98
81
-
Returns a L<Map|/type/Map> containing the invocants annotations, i.e. C<line> and C<file>.
82
-
An easier way to get hold of the annotation information is to use one
83
-
of the convenience methods instead.
99
+
Returns a L<Map|/type/Map> containing the invocants annotations, i.e. C<line>
100
+
and C<file>. An easier way to get hold of the annotation information is to use
101
+
one of the convenience methods instead.
84
102
85
103
say callframe.annotations.^name; # OUTPUT: «Map»
86
104
@@ -90,7 +108,8 @@ of the convenience methods instead.
90
108
91
109
method my(CallFrame:D:)
92
110
93
-
Return a L<Hash|/type/Hash> that names all the variables and their values associated with the lexical scope of the frame.
111
+
Return a L<Hash|/type/Hash> that names all the variables and their values
112
+
associated with the lexical scope of the frame.
94
113
95
114
sub some-value {
96
115
my $the-answer = 42;
@@ -100,8 +119,6 @@ Return a L<Hash|/type/Hash> that names all the variables and their values associ
100
119
my $frame = some-value();
101
120
say $frame.my<$the-answer>; # OUTPUT: «42»
102
121
103
-
B<Note> From version 6.d, C<.perl> can be called on C<CallFrame>.
0 commit comments