Skip to content

Commit 8ab319f

Browse files
committed
Extend coverage of EVAL.
1 parent 9454fb3 commit 8ab319f

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

doc/Type/Cool.pod

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1270,6 +1270,34 @@ C<MONKEY-SEE-NO-EVAL> pragma:
12701270
use MONKEY-SEE-NO-EVAL;
12711271
EVAL "say { 5 + 5 }";
12721272
1273+
Symbols in the current lexical scope are visible to code in an C<EVAL>.
1274+
1275+
my $answer = 42;
1276+
EVAL 'say $answer;'; # says 42
1277+
1278+
However, since the set of symbols in a lexical scope is immutable after compile
1279+
time, an EVAL can never introduced symbols into the surrounding scope.
1280+
1281+
EVAL 'my $lives = 9'; say $lives; # error, $lives not declared
1282+
1283+
Furthermore, the C<EVAL> is evaluated in the current package:
1284+
1285+
module M {
1286+
EVAL 'our $answer = 42'
1287+
}
1288+
say $M::answer; # says 42
1289+
1290+
And also the current language, meaning any added syntax is available:
1291+
1292+
sub infix:<mean>(*@a) is assoc<list> {
1293+
@a.sum / @a.elems
1294+
}
1295+
EVAL 'say 2 mean 6 mean 4'; # says 4
1296+
1297+
An C<EVAL> statement evaluates to the result of the last statement:
1298+
1299+
say EVAL 'say 1; 2 mean 6 mean 4'; # says 1, then says 4
1300+
12731301
C<EVAL> is also a gateway for executing code in other languages:
12741302
12751303
EVAL "use v5.20; say 'Hello from perl5!'", :lang<Perl5>;

0 commit comments

Comments
 (0)