Skip to content

Commit 10db477

Browse files
committed
Reflow, eliminates level
And some general cleanup as an initiation for #2719
1 parent e42f328 commit 10db477

File tree

1 file changed

+44
-27
lines changed

1 file changed

+44
-27
lines changed

doc/Type/CallFrame.pod6

Lines changed: 44 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,42 @@
22
33
=TITLE class CallFrame
44
5-
=SUBTITLE Capturing current frame state
5+
=SUBTITLE Captures the current frame state
66
77
class CallFrame {}
88
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.
1011
1112
my $frame = callframe;
1213
say "The above line of code ran at {$frame.file}:{$frame.line}.";
1314
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
18+
L«C<$?LINE>|/language/variables#Compile-time_variables».
19+
20+
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.
2441
2542
sub calling-frame() {
2643
for 1..* -> $level {
@@ -40,10 +57,15 @@ Here's a short example that will find the calling routine and prints the package
4057
4158
calling-frame;
4259
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.
4464
4565
=head1 Methods
4666
67+
B<Note> From version 6.d, C<.perl> can be called on C<CallFrame>.
68+
4769
=head2 method code
4870
4971
method code(CallFrame:D:)
@@ -54,22 +76,18 @@ Return the callable code for the current block. When called on the object return
5476
5577
method file(CallFrame:D:)
5678
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>.
5881
5982
my $frame = callframe(0);
6083
say $frame.file eq $frame.annotations<file>;
6184
62-
=head2 method 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-
6885
=head2 method line
6986
7087
method line(CallFrame:D:)
7188
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.
7391
7492
say callframe(1).line;
7593
say callframe(1).annotations<line>;
@@ -78,9 +96,9 @@ This is a shortcut for looking up the C<line> annotation. For example, the follo
7896
7997
method annotations(--> Map:D)
8098
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.
84102
85103
say callframe.annotations.^name; # OUTPUT: «Map␤»
86104
@@ -90,7 +108,8 @@ of the convenience methods instead.
90108
91109
method my(CallFrame:D:)
92110
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.
94113
95114
sub some-value {
96115
my $the-answer = 42;
@@ -100,8 +119,6 @@ Return a L<Hash|/type/Hash> that names all the variables and their values associ
100119
my $frame = some-value();
101120
say $frame.my<$the-answer>; # OUTPUT: «42␤»
102121
103-
B<Note> From version 6.d, C<.perl> can be called on C<CallFrame>.
104-
105122
=head1 Routines
106123
107124
=head2 sub callframe

0 commit comments

Comments
 (0)