Rules / Types, classes & roles / basic

CallFrame Reference

Captures the current frame state

What it is #

Routines #

Signatures are reproduced from the documentation, including their declared return types. A routine listed here is part of the type's published interface; whether Raku++ implements it is a separate question, answered by the examples below.

method code #

method code()

Return the callable code for the current block. When called on the object returned by callframe(0), this will be the same value found in &?BLOCK. The $frame variable will hold the Code for the block inside the loop in this case.

method file #

method file()

This is a shortcut for looking up the file annotation. Therefore, the following code prints True.

method line #

method line()

This is a shortcut for looking up the line annotation. For example, the following two calls are identical.

method annotations #

method annotations()

Returns a Map containing the invocants annotations, i.e. line and file. An easier way to get hold of the annotation information is to use one of the convenience methods instead.

method my #

method my()

Return a Hash that names all the variables and their values associated with the lexical scope of the frame.

sub callframe #

sub callframe(Int:D $level = 0)

Returns a CallFrame object for the given level. If no level is given, the default level is 0. Positive levels move up the frame stack and negative levels move down (into the call to callframe and deeper). Returns Mu if there is no call information for the given level. Negative levels may result in an exception.

Examples, run three ways #

Every example below comes from the official documentation, together with the output that documentation asserts. Each was then executed by Rakudo and by Raku++ when this page was built. Where the three agree, one result is shown; where they do not, all three are — because which of them is wrong is exactly the information worth having.

8 no-output · 1 not-runnable · 1 rakupp-differs

class CallFrame {}

Not executed: the documentation states no expected output for this example.

my $frame = callframe;
say "The above line of code ran at {$frame.file}:{$frame.line}.";

Not executed: the documentation states no expected output for this example.

sub calling-frame() {
    for 1..* -> $level {
        given callframe($level) -> $frame {
            when $frame ~~ CallFrame {
                    next unless $frame.code ~~ Routine;
                    say $frame.code.package;
                    last;
            }
            default {
                    say "no calling routine or method found";
                    last;
            }
        }
    }
}

Not executed: the documentation states no expected output for this example.

calling-frame;

Not executed: the documentation states no expected output for this example.

my $frame;
for ^3 { FIRST $frame = callframe; say $_ * 3 };
say $frame.code()

Not executed: the documentation states no expected output for this example.

my $frame = callframe(0);
say $frame.file eq $frame.annotations<file>;

Not executed: the documentation states no expected output for this example.

say callframe(1).line;
say callframe(1).annotations<line>;

Not executed: the documentation states no expected output for this example.

say callframe.annotations.^name;                   # OUTPUT: «Map␤»
say callframe.annotations<file> eq callframe.file; # OUTPUT: «True␤»
Documentation
Map
True
Rakudo
Map
True
Raku++

Raku++ disagrees with both the documentation and Rakudo — a defect.

sub some-value {
    my $the-answer = 42;
    callframe(0);
}

Not executed: the documentation states no expected output for this example.

my $frame = some-value();
say $frame.my<$the-answer>; # OUTPUT: «42␤»

Neither engine can run this in isolation — the example depends on context from the surrounding text.