|
7 | 7 | class Cool is Any { }
|
8 | 8 |
|
9 | 9 | C<Cool>, also known as the B<C>onvenient B<OO> B<L>oop, is a base class
|
10 |
| -employed by a number of built-in classes that can be meaningfully coerced |
11 |
| -to a string and a number. For example, an L<Array> can be used in mathematical |
12 |
| -operations, where its numerical representation is the number of elements it |
13 |
| -contains. At the same time, it can be concatenated to a string, where its |
14 |
| -stringy representation is all of its elements L<joined|/routine/join> by |
15 |
| -a space. Because L<Array> is L<Cool>, the appropriate coercion happens |
16 |
| -automatically. |
| 10 | +employed by a number of built-in classes that can be meaningfully |
| 11 | +coerced to a string and a number. For example, an L<Array> can be used |
| 12 | +in mathematical operations, where its numerical representation is the |
| 13 | +number of elements it contains. At the same time, it can be concatenated |
| 14 | +to a string, where its stringy representation is all of its elements |
| 15 | +L<joined|/routine/join> by a space. Because L<Array> is L<Cool>, the |
| 16 | +appropriate coercion happens automatically. |
17 | 17 |
|
18 | 18 | Methods in C<Cool> coerce the invocant to a more
|
19 | 19 | specific type, and then call the same method on that type. For example both
|
@@ -1311,31 +1311,46 @@ method EVAL(*%_)
|
1311 | 1311 | =for code
|
1312 | 1312 | sub EVAL($code where Cool|Blob, :$lang = 'perl6')
|
1313 | 1313 |
|
1314 |
| -Method form calls subroutine form with invocant as C<$code>, passing along |
1315 |
| -named args, if any. Subroutine form coerces L<Cool> C<$code> to L<Str>. |
1316 |
| -If C<$code> is a L<Blob>, it'll be processed using the same encoding as |
1317 |
| -the C<$lang> compiler would: for C<perl6>, uses the encoding specified |
1318 |
| -via C<--encoding> command line argument, or C<utf-8> if none were given; |
1319 |
| -for C<Perl5>, processes using same rules as C<perl>. |
| 1314 | +The method form calls subroutine form with invocant as C<$code>, passing |
| 1315 | +along named args, if any. Subroutine form coerces L<Cool> C<$code> to |
| 1316 | +L<Str>. If C<$code> is a L<Blob>, it'll be processed using the same |
| 1317 | +encoding as the C<$lang> compiler would: for C<perl6>, uses the encoding |
| 1318 | +specified via C<--encoding> command line argument, or C<utf-8> if none |
| 1319 | +were given; for C<Perl5>, processes using same rules as C<perl>. |
1320 | 1320 |
|
1321 |
| -This works as-is with a literal string parameter. More complex input, such as |
1322 |
| -a variable or string with embedded code, is illegal by default. This can be |
1323 |
| -overridden in any of several ways: |
| 1321 | +This works as-is with a literal string parameter. More complex input, |
| 1322 | +such as a variable or string with embedded code, is illegal by default. |
| 1323 | +This can be overridden in any of several ways: |
1324 | 1324 |
|
1325 |
| - use MONKEY-SEE-NO-EVAL; |
1326 |
| - use MONKEY; # shortcut that turns on all MONKEY pragmas |
| 1325 | + use MONKEY-SEE-NO-EVAL; # Or... |
| 1326 | + use MONKEY; # shortcut that turns on all MONKEY pragmas |
1327 | 1327 | use Test;
|
1328 | 1328 |
|
1329 | 1329 | # any of the above allows:
|
1330 | 1330 | EVAL "say { 5 + 5 }"; # OUTPUT: «10»
|
1331 | 1331 |
|
| 1332 | +In case the C<MONKEY-SEE-NO-EVAL> pragma is not activated, the compiler |
| 1333 | +will complain with a C<EVAL is a very dangerous function!!!> exception. |
| 1334 | +And it is essentially right, since that will run arbitrary code with the |
| 1335 | +same permissions as the program. You should take care of cleaning the |
| 1336 | +code that is going to pass through EVAL if you activate the |
| 1337 | +C<MONKEY-SEE-NO-EVAL> pragma. |
| 1338 | +
|
| 1339 | +Please note that you can interpolate to create routine names using |
| 1340 | +quotation, as can be seen in |
| 1341 | +L<this example|/language/quoting#index-entry-%26_(interpolation)> |
| 1342 | +or L<other ways to interpolate to create identifier names|/language/syntax#Identifiers>. |
| 1343 | +This only works, however, for already declared functions and other |
| 1344 | +objects and is thus safer to use. |
| 1345 | +
|
1332 | 1346 | Symbols in the current lexical scope are visible to code in an C<EVAL>.
|
1333 | 1347 |
|
1334 | 1348 | my $answer = 42;
|
1335 | 1349 | EVAL 'say $answer;'; # OUTPUT: «42»
|
1336 | 1350 |
|
1337 |
| -However, since the set of symbols in a lexical scope is immutable after compile |
1338 |
| -time, an EVAL can never introduce symbols into the surrounding scope. |
| 1351 | +However, since the set of symbols in a lexical scope is immutable after |
| 1352 | +compile time, an EVAL can never introduce symbols into the surrounding |
| 1353 | +scope. |
1339 | 1354 |
|
1340 | 1355 | =for code :skip-test
|
1341 | 1356 | EVAL 'my $lives = 9'; say $lives; # error, $lives not declared
|
|
0 commit comments