Skip to content

Commit 5f185ca

Browse files
committed
Adds explanation of EVAL
With possible errors that can be found there. This would close #2099. If the new text is not satisfactory, please feel free to reopen specifying what's the part of the problem it does not solve.
1 parent 35a3804 commit 5f185ca

File tree

3 files changed

+46
-29
lines changed

3 files changed

+46
-29
lines changed

doc/Language/5to6-perlfunc.pod6

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -457,8 +457,10 @@ C<$filehandle.eof>. Returns C<True> if at end of file.
457457
458458
=item eval EXPR
459459
460-
Replaced by L<EVAL|/routine/EVAL>. Note that C<EVAL> does not do any
461-
L<exception handling|/language/exceptions>!
460+
The closest replacment is the L<EVAL|/routine/EVAL> function. However,
461+
this function has to be allowed explicitly using a pragma to work in the
462+
same way. Note that C<EVAL> does not do any L<exception
463+
handling|/language/exceptions>!
462464
463465
=head2 evalbytes
464466

doc/Type/Cool.pod6

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77
class Cool is Any { }
88
99
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.
1717
1818
Methods in C<Cool> coerce the invocant to a more
1919
specific type, and then call the same method on that type. For example both
@@ -1311,31 +1311,46 @@ method EVAL(*%_)
13111311
=for code
13121312
sub EVAL($code where Cool|Blob, :$lang = 'perl6')
13131313
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>.
13201320
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:
13241324
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
13271327
use Test;
13281328
13291329
# any of the above allows:
13301330
EVAL "say { 5 + 5 }"; # OUTPUT: «10␤»
13311331
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+
13321346
Symbols in the current lexical scope are visible to code in an C<EVAL>.
13331347
13341348
my $answer = 42;
13351349
EVAL 'say $answer;'; # OUTPUT: «42␤»
13361350
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.
13391354
13401355
=for code :skip-test
13411356
EVAL 'my $lives = 9'; say $lives; # error, $lives not declared

doc/Type/Signature.pod6

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -440,9 +440,9 @@ except the C<$var> is a definition for a routine.
440440
441441
=head3 X<Coercion Type|coercion type (signature)>
442442
443-
To accept one type but coerce it automatically to another, use the accepted
444-
type as an argument to the target type. If the accepted type is C<Any> it can
445-
be omitted.
443+
To accept one type but coerce it automatically to another, use the
444+
accepted type as an argument to the target type. If the accepted type is
445+
C<Any> it can be omitted.
446446
447447
sub f(Int(Str) $want-int, Str() $want-str) {
448448
say $want-int.^name ~ ' ' ~ $want-str.^name
@@ -456,10 +456,10 @@ be omitted.
456456
foo "2016-12-01";
457457
# OUTPUT: «Date␤2016-12-01␤»
458458
459-
The coercion is performed by calling the method with the name of the type
460-
to coerce to, if it exists (e.g. C<Foo(Bar)> coercer, would call method C<Foo>).
461-
The method is assumed to return the correct type—no additional checks on
462-
the result are currently performed.
459+
The coercion is performed by calling the method with the name of the
460+
type to coerce to, if it exists (e.g. C<Foo(Bar)> coercer, would call
461+
method C<Foo>). The method is assumed to return the correct type—no
462+
additional checks on the result are currently performed.
463463
464464
Coercion can also be performed on return types:
465465

0 commit comments

Comments
 (0)