File tree Expand file tree Collapse file tree 1 file changed +28
-0
lines changed Expand file tree Collapse file tree 1 file changed +28
-0
lines changed Original file line number Diff line number Diff line change @@ -1270,6 +1270,34 @@ C<MONKEY-SEE-NO-EVAL> pragma:
1270
1270
use MONKEY-SEE-NO-EVAL;
1271
1271
EVAL "say { 5 + 5 }";
1272
1272
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
+
1273
1301
C < EVAL > is also a gateway for executing code in other languages:
1274
1302
1275
1303
EVAL "use v5.20; say 'Hello from perl5!'", :lang<Perl5>;
You can’t perform that action at this time.
0 commit comments