Why jump up and down screaming eval is super bad, when instead you could have allowed users to specify the context of eval, and made it safe to use?
Why jump up and down screaming eval is super bad, when instead you could have allowed users to specify the context of eval, and made it safe to use?
Suppose for a moment that the scope of eval was truly an environment:
And you don't supply the resulting environment with anything but the functions you want.
Not even the dot operator, let alone magic builtins.
Python's eval suddenly becomes much safer. Unfortunately such an eval is harder to implement in Python, because every object carries it's magic methods with it, rather than looking them up elsewhere.
To the second part of your question: as you have undoubtedly noticed, you've never heard of E nor Monte.
* Racket [0], sometimes used when you don't want to use Racket's way of designing DSLs, or if you want to interface to databases simpler.
* Scheme [1], used when designing DSLs, interacting with databases. You often see it go hand-in-hand with X-Expressions.
* Lua [2] can, but it is rarely used.
Note, the link for Lua actually lists a whole host of languages where this can be technically done, but not all are entirely safe.
I think it's uncommonality is not because of it's inconvenience or rarity, but rather the mantra of 'eval is evil' in the programming community, even when it doesn't apply.
[0] https://docs.racket-lang.org/guide/eval.html
[1] https://www.gnu.org/software/guile/manual/html_node/Environm...
[2] https://rosettacode.org/wiki/Runtime_evaluation/In_an_enviro...
I agree with the author that this is probably overkill, in terms of warning about eval, but I'm also not understanding how merely having a block with a block scoped environment can make eval safe.
Having blocks simply means half the work of implementing eval this way has already been done in the interpreter.
Eval wouldn't have access to the base language if handed an empty environment.
Maybe I can try explaining again.
Eval is unsafe as it reads input values in terms of the current state of a program.
It's environment is implicitly given, and includes the entirety of the Perl6 language, as well as anything user-created.
But, what if the programmer was allowed to supply that context?
Rather than a blacklist against certain bad things, this is a whitelist where you supply every single thing that eval is allowed to use.
Other languages already have this.
Scheme example that works:
(eval '(+ 1 2) (let (null-environment) '(+ . +)))
Scheme eval that fails: (eval '(+ 1 2) (null-environment))
Perl6's flexibility is less unsafe when you don't provide anything flexible.