Fact-checked by Grok 2 weeks ago

eval

Eval is a built-in function present in many dynamic programming languages that evaluates a string of code as if it were source code in the language, parsing it, executing it dynamically, and returning the result of the evaluation. This capability allows programs to treat code as data, facilitating metaprogramming techniques such as runtime code generation and interpretation. Originating in the Lisp programming language, where it served as both a formal definition of the language and a core interpreter component, eval has been adopted in languages including Python, JavaScript, Ruby, Perl, PHP, and Tcl to enable flexible, interactive execution of expressions. The concept of eval traces its roots to the development of in the late 1950s at MIT's Project, led by John McCarthy. In Lisp's foundational design, the eval function eval(e, a) was defined to compute the value of an expression e in an environment a, embodying the language's homoiconic nature where programs and data share the same representation. This recursive definition not only provided a mathematical specification for Lisp but also functioned as an efficient interpreter, enabling and symbolic computation essential for early research. McCarthy's 1960 paper formalized this recursive definition and discussed support for garbage collection in list-based structures. The formal nature of eval later enabled proving program properties in subsequent research. In modern usage, eval enables powerful features like dynamic expression evaluation—for instance, in , eval(source, globals=None, locals=None) parses a string or code object as a Python expression within specified namespaces, defaulting to the caller's environment if omitted. Similarly, JavaScript's eval(string) treats the input as a script, returning its completion value, though it has been available since the language's in 1995. However, eval's ability to execute arbitrary introduces significant risks, such as vulnerabilities when processing untrusted input, leading to recommendations against its use in favor of safer alternatives like ast.literal_eval in Python or indirect evaluation in JavaScript. Despite these dangers, studies show its persistent use in web applications for tasks like parsing or plugin systems, underscoring its utility in dynamic contexts.

Overview

Definition and Purpose

In programming languages, particularly those with dynamic typing and interpretation capabilities, eval (short for "evaluate") refers to a built-in or that parses and executes a of source code as if it were written directly in the program, within the current execution environment. This process typically involves compiling the input into an executable form—such as bytecode in interpreted languages—and running it, returning the result of the computation. For instance, in , eval(expression, globals=None, locals=None) evaluates a Python expression from a or code object, using specified namespaces to control variable . Similarly, in , eval(code) treats the input as a script, allowing execution of expressions, statements, or definitions. The primary purpose of eval is to enable dynamic code execution at runtime, facilitating flexibility in scenarios where code cannot be predetermined at or during static analysis. This is particularly valuable in , where programs generate and run other ; processing user-supplied scripts or configurations; or implementing embedded domain-specific languages. In Lisp dialects like Lisp, eval evaluates a form (such as a list representing ) in the current lexical or dynamic environment, supporting the language's homoiconic nature where and data share the same representation. For example, 's eval comes in string and block forms: the string variant parses and executes dynamically for delayed or conditional execution, while the block form traps exceptions efficiently during runtime. In , eval(code) executes a string as PHP , inheriting the caller's scope, which aids in scenarios like dynamic function generation from templates. Despite its utility, eval is widely regarded as hazardous due to its ability to execute arbitrary code, potentially exposing programs to injection attacks if untrusted input is evaluated. Official documentation across languages emphasizes avoiding eval with user-provided data, recommending safer alternatives like parsing to abstract syntax trees (e.g., Python's ast.literal_eval) or structured data processing. In JavaScript, direct use of eval is explicitly warned against for security and performance reasons, as it bypasses optimizations and can lead to vulnerabilities like cross-site scripting. Perl and PHP documentation similarly highlight the risks of error-prone parsing and scope inheritance, urging validation or avoidance in production code.

Historical Development

The concept of the eval function originated in the Lisp programming language, where it was introduced by John McCarthy in his seminal 1960 paper, "Recursive Functions of Symbolic Expressions and Their Computation by Machine, Part I." In this work, McCarthy defined eval as a recursive function that interprets symbolic expressions (S-expressions) within an association list environment, handling atomic values, quotes, conditionals via cond, list operations like car and cdr, and function applications through auxiliary functions such as evcon and evlis. This design enabled Lisp to be self-interpreting, with eval serving as the core of its interpreter implemented on the IBM 704 computer, facilitating symbolic computation for artificial intelligence applications like the proposed Advice Taker system from 1958. The function's structure, pairing evaluation with application via apply, laid the foundation for Lisp's homoiconicity, where code and data share the same representation, allowing dynamic manipulation of programs as data. As and its derivatives evolved through the 1960s and 1970s, eval remained a central primitive, influencing paradigms and techniques in languages like . For instance, 's eval extends Lisp's model by operating within a specified , supporting modular outside the global scope; it was first standardized in the R5RS report in 1998. This period saw eval adopted in early interactive environments, such as the read-eval-print loop (REPL), which became a hallmark of Lisp-family interpreters, enabling and . High-impact contributions, including Paul Graham's work on metacircular evaluators in "On Lisp" (1993), underscored eval's role in building interpreters and compilers within the language itself, emphasizing its power for while highlighting early concerns over performance and security in unbounded . The proliferation of interpreted scripting languages in the late 1980s and extended eval's influence to broader domains, particularly and system scripting, where dynamic code execution addressed needs for flexibility in and . In , released in 1987 by , the eval operator was included from version 1.0 for string evaluation, enabling runtime code execution for tasks like dynamic loading in text-processing scripts; the block form for exception trapping was added in Perl 5 (1994), which also introduced features like lexical scoping. , developed by starting in 1989, incorporated eval as a built-in from its early releases (around Python 1.0 in 1994), distinguishing it from exec for expression evaluation in interactive shells and safe computation of user inputs, though with warnings on security risks in its documentation. In the mid-1990s web boom, eval appeared in client- and server-side languages to support dynamic behaviors. JavaScript's eval, created by during Netscape's 10-day prototype sprint in May 1995, allowed runtime execution of string-based code for interactive web pages, standardized in 1.0 (1997) but later restricted in strict mode (ES5, 2009) to mitigate vulnerabilities. PHP, initially released as PHP/FI 1.0 in 1995 by , included eval from its inception to enable dynamic script generation in web applications, inheriting the current scope but inheriting early criticism for enabling . Ruby, launched in 1995 by , integrated eval as a from version 0.95, leveraging it for like dynamic method definition, aligning with Ruby's emphasis on programmer happiness and expressiveness. These adoptions reflected eval's enduring utility in dynamic languages, balanced against growing awareness of its risks, such as injection attacks, prompting safer alternatives like abstract syntax trees and sandboxes in modern implementations.

Implementation Approaches

In Interpreted Languages

In interpreted languages, the eval construct enables dynamic execution of code represented as a by leveraging the runtime's existing parser and evaluator. This approach is particularly suited to interpreted environments, where source code is processed on-the-fly, allowing the string to be fed directly into the interpreter's phase to generate an executable form, such as or an , before immediate evaluation in the current . Unlike compiled languages, which require integrating a full at , interpreted implementations benefit from the active interpreter , making dynamic code execution more efficient and integrated. However, this flexibility introduces significant risks, as eval can execute arbitrary , potentially leading to vulnerabilities if used with untrusted input. In , an interpreted language using a , the built-in eval() function takes a representing a single expression, compiles it into a object via the language's (which parses the input and generates ), and then executes it within specified global and local namespaces. If no namespaces are provided, it defaults to the caller's environment, inheriting variables and scope. The process strips leading and trailing whitespace from inputs and raises a SyntaxError for invalid syntax, emphasizing its role in safe, expression-level evaluation rather than full statements (for which exec() is used). Performance-wise, repeated calls incur parsing overhead, but the integration with the interpreter ensures seamless execution. Security documentation explicitly warns against using eval() with user-supplied data due to risks. JavaScript, executed by just-in-time (JIT) interpreters in browsers and runtimes like Node.js, implements eval() as a global function that parses the input string as JavaScript code and executes it immediately, returning the completion value. For direct calls (e.g., eval(code)), execution occurs in the caller's lexical environment, including access to local variables and strict mode inheritance; indirect calls (e.g., via a variable) shift to the global scope. The ECMAScript specification defines this behavior, relying on the engine's parser to handle statements and expressions dynamically. This runtime parsing disables certain optimizations, such as dead code elimination, resulting in slower performance compared to pre-parsed code—often 10-100 times slower in benchmarks for simple expressions. Due to its power, eval() is flagged as dangerous, enabling potential attacks like cross-site scripting if the string originates from untrusted sources. Perl's eval in string context (eval EXPR) treats the string as a block of Perl code, parsing and executing it at runtime within the current lexical scope while trapping errors to prevent program termination. The Perl interpreter compiles the string into an opcode tree each time eval is invoked, delaying parsing until execution and allowing dynamic features like runtime code generation. Upon success, it returns the value of the last evaluated expression (in the context of the eval call—scalar, list, or void); errors set the special variable $@ with the exception message and return undef or an empty list. This implementation supports exception handling and is commonly used for tasks like loading optional modules, but it forbids source filters in modern versions (Perl 5.16+) under the unicode_eval feature for safety. Like other evals, it poses security risks with tainted input, as it can execute system commands or alter program flow. In Ruby, the Kernel#eval method (available as a global function) evaluates a string of Ruby code in the current context or a provided Binding object, which captures the execution environment including locals and self. The Ruby interpreter parses the string into an abstract syntax tree and executes it, supporting full expressions and statements, with optional filename and line number parameters for improved error reporting in syntax exceptions. Implemented in the virtual machine's evaluation core (vm_eval.c), it integrates directly with Ruby's dynamic typing and method lookup, enabling metaprogramming techniques like defining methods at runtime. While powerful for reflective programming, eval bypasses static analysis, leading to performance penalties from dynamic parsing and potential security exposures if the string is not controlled. Lua, a lightweight interpreted language, lacks a direct eval but achieves equivalent functionality through the load function, which compiles a string chunk into an using Lua's . The resulting function can then be invoked to execute the code in a specified table, allowing control over globals and scope isolation. For instance, load("return 2 + 2")() evaluates the expression and returns 4, with the handling parsing into Lua for the . This two-step process (compile then run) provides flexibility for error checking before execution and supports binary or text modes, but it requires manual invocation, differing from single-call evals in other languages. As an , Lua's implementation prioritizes safety and performance, avoiding unrestricted global access by default. PHP's eval() function, in its interpreted execution model, evaluates a as PHP code within the current , inheriting variables from the calling and requiring the string to end with a for valid syntax. The parses the input dynamically, compiling it to opcodes on-the-fly before execution, similar to script loading. It returns the result of the evaluated or NULL on failure, with errors suppressed unless using output control. This runtime compilation enables dynamic scripting but is notoriously insecure for user input, as it can lead to remote execution; official guidance recommends alternatives like include for safer . Performance suffers from repeated parsing, often making it unsuitable for high-throughput applications.

In Compiled Languages

In compiled languages, where source code is typically translated to machine code or bytecode ahead of time, direct evaluation of arbitrary strings as executable code—similar to the eval function in interpreted languages—is not a standard built-in feature. This stems from the emphasis on static compilation for performance and type safety, which precludes simple runtime interpretation of source text. Instead, dynamic code evaluation is achieved through runtime compilation mechanisms, just-in-time (JIT) compilers, or integration with scripting engines. These methods enable flexibility for tasks like plugin systems or configuration-driven logic but often incur overhead from compilation steps and raise security concerns due to potential code injection vulnerabilities. In , dynamic code execution is supported via the javax.script package, introduced in Java 6 as part of the JSR 223 specification. This API provides the ScriptEngine interface, whose eval method compiles and executes scripts in various languages, such as using the engine (default until Java 15) or third-party engines like GraalVM's implementation. For instance, a ScriptEngineManager can instantiate an engine, and engine.eval("1 + 2") returns the result as an object, bridging Java's static nature with dynamic scripting. This facility is commonly used in applications requiring user-defined expressions or embedded DSLs, though it requires careful sandboxing to mitigate risks. For .NET-based languages like C#, runtime code evaluation leverages the Roslyn compiler platform, which exposes C# and VB.NET compilation as APIs since .NET 4.6. Developers can use Microsoft.CodeAnalysis.CSharp to parse a code string into a syntax tree, compile it into an in-memory assembly via CSharpCompilation, and invoke the resulting methods using reflection. An example workflow involves creating a SyntaxTree from source text, emitting it to a MetadataReference-loaded assembly, and executing via Assembly.Load and MethodInfo.Invoke. This approach powers tools like dynamic LINQ query builders and scripting hosts, offering full C# language support at runtime while maintaining type safety through compilation errors. Earlier mechanisms like CSharpCodeProvider from CodeDOM provided similar functionality but were deprecated in favor of Roslyn for better performance and diagnostics. In C++, lacking a standard compiler, dynamic evaluation typically involves external libraries for compilation, such as LLVM's ORC infrastructure. This framework allows embedding a pipeline to generate (IR) from C++-like source, optimize it, and link executable code at . For example, the LLVM C++ API can parse expressions via , compile to , and execute via a execution engine, enabling use cases in numerical computing or game engines. However, this requires significant setup and is not portable across platforms without LLVM dependencies, contrasting with simpler interpreted alternatives. Security-focused implementations often restrict capabilities to prevent . Languages like Go and further illustrate the challenges: Go has no built-in eval due to its focus on simplicity and static binaries, relying instead on external tools or libraries like github.com/apaxa-go/eval for limited expression parsing. , emphasizing , avoids runtime code generation in stable releases, with experimental support via the rustc crate for only; runtime JIT is possible through unsafe bindings to but discouraged for production. These designs prioritize compile-time guarantees over runtime dynamism.

Usage in Programming Languages

Lisp and Derivatives

In Lisp, the eval function serves as the core mechanism for dynamically evaluating Lisp expressions represented as data structures, enabling the language's homoiconic nature where code and data share the same representation. Originally defined by John McCarthy in 1960, EVAL[e, a] computes the value of a Lisp expression e given a list of variable assignments a, functioning recursively to handle various form types such as atoms (which evaluate to themselves or their bound values), quoted expressions (which return unevaluated), and compound forms like function applications via an auxiliary APPLY function. This design not only provides a formal semantics for Lisp but also implements a self-interpreter, allowing Lisp programs to manipulate and execute other Lisp programs as data, a foundational aspect of metaprogramming in the language. In Common Lisp, eval evaluates a form in the current dynamic environment while using the null lexical environment, effectively providing a user interface to the language's evaluator for dynamic code execution. Unlike the original Lisp's simpler model, Common Lisp's eval integrates with features like dynamic variables and special forms, but it discards lexical bindings from the caller's scope to prevent unintended variable capture, which can limit its utility in lexically scoped contexts without explicit environment management. This function is essential for tasks such as interpreting user-input expressions or implementing domain-specific languages, though its use is cautioned due to performance overhead and security risks in untrusted inputs. Scheme, a dialect of Lisp emphasizing lexical scoping and minimalism, defines eval in the Revised Report on the Algorithmic Language (R5RS) as a that evaluates an expression in a specified , returning its value. This contrasts with imperative Lisps by requiring an explicit to access lexical variables, as eval operates outside the caller's lexical by default; for instance, (eval 'x (interaction-environment)) resolves x in the top-level . The supports Scheme's functional purity and is used in applications like symbolic computation or extending interpreters, but its dependency underscores Scheme's strict separation of evaluation contexts to avoid dynamic scoping pitfalls. Modern Lisp derivatives like Clojure adapt eval to hosted environments, such as the Java Virtual Machine, where it evaluates a form data structure (not a string) in the current namespace, compiling it if necessary and returning the result. Clojure's eval leverages the host platform's just-in-time compilation for efficiency, supporting dynamic features like runtime macro expansion, but it resolves symbols in the global namespace rather than local bindings, often requiring workarounds like binding for dynamic variables. This makes it suitable for interactive development and plugin systems, as seen in tools like REPL-driven workflows.

JavaScript

In , the eval() function is a built-in method of the global object that evaluates a of JavaScript code as if it were script source, executing it in the current context and returning its completion value. Defined in the specification since its first edition in 1997, eval() parses the input as an program, treating it as global code unless invoked as a direct eval, in which case it uses the caller's lexical environment for variable resolution. If the argument is not a primitive, eval() returns it unchanged; otherwise, it throws a SyntaxError for invalid code or restricted constructs like super outside methods. The function supports both direct and indirect invocations, a distinction introduced to enhance and . A direct eval, called as eval(x), executes in the caller's , allowing access to local and potentially introducing new ones, which can lead to unintended side effects. In contrast, an indirect eval—invoked via a reference like window.eval(x) or a variable—operates in the global lexical , isolating it from the caller's and preventing local pollution. This behavior, formalized in editions from 5.1 onward, ensures that indirect evals do not affect the caller's , though they still pose risks if the string originates from untrusted sources. Historically, eval() has been integral to since its inception in 2.0 in 1996, enabling dynamic code execution for early web scripting needs like configuration loading or simple expression evaluation. A 2011 empirical study of over 10,000 websites found eval() prevalent in 82% of the top 100 sites and 50% of the top 10,000, often for tasks such as JSON deserialization (up to 37% of calls), handling, and dynamic property access. Despite its utility, usage has declined with modern alternatives, though it remains supported across all major engines like V8, , and JavaScriptCore since their initial releases. Security concerns dominate discussions of eval(), as it enables , facilitating (XSS) attacks if fed user-controlled input. The (CSP) directive script-src 'unsafe-eval' explicitly controls such executions to mitigate injection risks, a measure recommended since CSP's introduction in 2012. The same study revealed that 7-8% of eval() calls modify the global scope, often with strings sourced from the DOM, amplifying vulnerability to malicious payloads. In strict mode, introduced in 5 (2009), eval() cannot bind new variables named arguments or eval, further restricting its behavior to curb errors and exploits. Performance-wise, eval() incurs significant overhead by bypassing static parsing, forcing runtime compilation and variable lookups that disable engine optimizations like dead code elimination. Benchmarks from the era indicate it generates slower bytecode than equivalent static code, with execution times potentially 10-100 times higher for complex expressions due to dynamic scope resolution. As a result, best practices advise against its use in production, favoring safer alternatives like the Function constructor for isolated execution, JSON.parse() for data parsing, or template literals with tagged functions for dynamic strings. For simple cases, such as evaluating mathematical expressions, eval() might be used as follows:
javascript
const result = eval("2 + 3 * 4");  // Returns 14
However, this is discouraged; instead, libraries like math.js or custom parsers are preferred for security and maintainability. Despite deprecation calls, eval() persists in 2026 for , underscoring its role in legacy codebases while modern emphasizes safer, modular paradigms.

Python

In Python, eval() is a built-in that parses and evaluates a as a valid Python expression, returning the result of the computation. It accepts a or a pre-compiled code object as input and optionally specifies global and local namespaces to control the evaluation context. This allows dynamic execution of expressions, such as simple arithmetic or calls, within a controlled environment. The function's syntax is eval(expression, globals=None, locals=None), where expression is the input to evaluate, globals is a dictionary representing the global (defaulting to the caller's globals if omitted), and locals is a for the local (defaulting to the caller's locals). If provided, both must be dictionaries or , and the function strips leading and trailing whitespace from string inputs. For example, given x = 1, eval('x + 1') returns 2. The return value is the result of the expression, which can be any object, or raises exceptions like SyntaxError for invalid input or NameError for undefined names. Unlike exec(), which executes arbitrary Python statements (such as assignments or loops) and returns None, eval() is restricted to single expressions and produces a value. This distinction makes eval() suitable for computing results dynamically, while exec() is used for broader code execution. Both functions share similar namespace parameters but are generally discouraged due to performance overhead and risks. A primary concern with eval() is its potential for vulnerabilities, as it executes arbitrary that could compromise the if the input derives from untrusted sources, such as input. For instance, an input like __import__('os').system('rm -rf /') could delete files, highlighting the dangers of . Official documentation warns against its use with untrusted data, recommending restricted namespaces or avoidance altogether. As a safer alternative for evaluating literal values (e.g., numbers, strings, lists, or dictionaries) without executing arbitrary code, provides ast.literal_eval() from the ast . This function parses strings containing only literals and returns the corresponding object, raising a ValueError for non-literal content, thus preventing namespace access or side effects. For example, ast.literal_eval('{"a": 1}') safely returns {'a': 1}, but ast.literal_eval('__import__("os")') fails. It supports sets and bytes literals since Python 3.2 and empty sets via 'set()' since 3.9, though large inputs may still risk resource exhaustion.

Perl

In , the eval function enables the dynamic execution of code, either from a string or a block, primarily for trapping runtime errors and facilitating tasks. Introduced as a core feature in early versions of Perl, it draws inspiration from Lisp's eval mechanism but is tailored for Perl's dynamic nature, allowing code to be parsed and run within the current lexical scope while sharing the same package namespace. The function returns the value of the last evaluated expression or the explicit return value, operating in void, scalar, or list context based on the caller's . The string form, eval EXPR, compiles and executes the content of EXPR (typically a ) at each time it is invoked, making it suitable for evaluating user-supplied or dynamically generated code. For instance, to safely compute a division while catching division-by-zero errors:
my $x = 10;
my $y = 0;
eval '$answer = $x / $y';
if ($@) {
    warn "Error: $@";
} else {
    print $answer;
}
Here, if an error occurs during or execution, eval returns undef in scalar (or an empty list in list ), and the special variable $@ is set to the ; otherwise, $@ is an . This form recompiles the code on every call, which can impact performance for repeated use. In contrast, the block form, eval BLOCK, parses the code block at but executes it at , offering efficiency for static snippets and better integration with lexical variables. It is commonly used for , akin to a try-catch construct:
my $x = 10;
my $y = 0;
eval { $answer = $x / $y };
if ($@) {
    warn "Error: $@";
} else {
    print $answer;
}
Block eval does not support loop control statements like next or last from outer scopes and avoids the recompilation overhead of string eval. Both forms route warnings to STDERR unless suppressed, and prior to Perl 5.14, assigning to $@ within the eval could lead to bugs, necessitating temporary variables for reliable error checking in older versions. Eval is integral to Perl's exception-handling paradigm, often paired with die to raise errors, and features like the module for sandboxed execution to mitigate risks. However, string eval poses significant hazards by permitting arbitrary , especially with tainted input; Perl's taint mode flags such untrusted data to prevent unsafe operations. Best practices recommend preferring block eval for error trapping, avoiding string eval unless necessary (e.g., for dynamic loading via eval "require $module"), and using alternatives like do for file-based code execution to reduce vulnerabilities. For secure dynamic evaluation, the compartment restricts access to sensitive operations, though it is considered experimental and not foolproof against interpreter bugs.

PHP

In PHP, the eval() language construct evaluates a string as PHP code within the current scope, allowing dynamic execution of code generated at runtime. Unlike a true function, eval() is a core language feature that parses and executes the provided string directly, inheriting the variable scope of the calling context while defining any new functions or classes in the global namespace. The syntax requires a single string argument containing valid PHP code, which must end with a semicolon and omit opening (<?php) or closing (?>) tags; for instance, eval("echo 'Hello';"); outputs "Hello" without errors. Introduced as a foundational element in early PHP versions and consistently available from PHP 4 through PHP 8, eval() has been used for tasks requiring runtime code generation, such as dynamically constructing and executing variable assignments or simple expressions based on configuration data. A representative example involves merging user-defined variables into code strings, like $name = 'World'; $code = "echo 'Hello, $name!';"; eval($code);, which produces "Hello, World!". However, such usage is rare in modern due to performance overhead and risks, with the construct returning null by default or the value from a return statement if present; parse errors throw a ParseError exception in PHP 7 and later, previously returning false. Security vulnerabilities arise primarily from when untrusted input, such as from $_GET or $_POST, is passed to eval() without , enabling attackers to execute arbitrary commands and potentially achieve remote code execution (RCE). For example, a vulnerable like $var = $_GET['input']; eval($var); could be exploited with input phpinfo(); to leak information or system('rm -rf /'); to delete files, compromising confidentiality, integrity, and availability. The Foundation classifies this as a severe injection flaw, emphasizing that eval() amplifies risks in web applications by interpreting malicious strings as executable code. To mitigate these dangers, documentation strongly advises against using eval() with user-supplied data, recommending strict input validation—such as whitelisting allowed characters or formats—and safer alternatives like anonymous s (introduced in 5.3) or call_user_func() for dynamic invocation. Deprecated features like create_function(), which internally relied on eval() for creation, were removed in 8.0, further discouraging its use; similarly, the /e modifier in preg_replace(), which evaluated replacement strings as code, was eliminated in 7.0 to reduce injection vectors. In production environments, disabling eval() via configuration (e.g., through disable_functions in php.ini) or employing opcode caches like OPcache can limit exposure, though complete avoidance remains the .

Ruby

In Ruby, dynamic code evaluation is primarily achieved through the Kernel#eval method, which executes a string of Ruby code as if it were part of the program at the point of invocation. The method signature is eval(string [, binding [, filename [, lineno]]]), where the string parameter contains the Ruby expressions to evaluate, binding (optional) specifies the execution context via a Binding object, filename (optional) sets the name used in error messages (defaulting to "(eval)"), and lineno (optional) indicates the starting line number for errors (defaulting to 1). The return value is the result of the last evaluated expression, or nil if none. For example:
ruby
eval("1 + 2")  # => 3
This mechanism allows runtime computation of expressions, such as generating code from user input or data-driven logic, though it inherits the current scope's variables unless a different binding is provided. Ruby enhances this with context-specific evaluation methods for metaprogramming. The instance_eval method, defined on Object, evaluates a string or block within the receiver's context, making instance variables and private methods accessible and setting self to the receiver. Its signature is instance_eval(string=undef, filename=nil, line=nil, &block), supporting either a string or block but not both. This enables temporary modifications to an object's behavior, such as defining singleton methods. An illustrative example:
ruby
obj = Object.new
obj.instance_eval { @secret = "hidden"; def reveal; @secret; end }
obj.reveal  # => "hidden"
Such usage facilitates object introspection and customization without altering the class definition globally. For class-level modifications, the class_eval method (aliased as module_eval) on Module evaluates code in the context of a class or module, allowing dynamic addition of methods, constants, or aliases. The signature is module_eval(string=nil, filename=nil, lineno=1, &block), again accepting either a string or block. This supports Ruby's open classes by enabling runtime extensions, as shown:
ruby
class String
end
String.class_eval { def shout; upcase + "!"; end }
"hello".shout  # => "HELLO!"
class_eval is invoked on the class itself, ensuring new methods apply to all instances. These evaluation tools collectively empower Ruby's flexible, model, though they require careful handling to avoid unintended side effects.

Lua

In Lua, dynamic code evaluation is achieved through the load function rather than a dedicated eval primitive, allowing strings of Lua source code to be compiled into chunks at . The load function takes a chunk—typically a string containing Lua code—and compiles it into an , returning that function if successful or nil along with an error message if compilation fails. This design separates compilation from execution, enabling developers to inspect or modify the loaded code before running it, which contrasts with traditional eval functions that execute code immediately. The syntax for load is load(chunk, [chunkname], [mode], [env]), where chunk is the input string, chunkname provides a name for debugging (defaulting to "=string" for string inputs), mode specifies the format ("t" for text, "b" for binary, or "bt" to auto-detect; defaults to "bt"), and env sets the execution environment (defaults to the global environment). To evaluate the code, the returned function is invoked, often with arguments passed to it. For example, the following code evaluates a simple arithmetic expression stored in a string:
lua
local code = "return 2 + 3 * 4"
local func = load(code)
if func then
    print(func())  -- Outputs: 14
end
This approach supports both expressions and full statements, making it versatile for tasks like configuration loading or scripting in embedded systems. Historically, earlier versions (up to 5.1) provided loadstring as a simpler alias for loading strings, but it was deprecated starting in Lua 5.2 to consolidate functionality under load, which handles both strings and reader functions for more flexible input sources. Lua's load was intentionally designed as a "pure and total" operation—free of side effects during and guaranteed to terminate—unlike many eval implementations that blend , , and execution. This stems from Lua's origins as an embeddable extension language developed in 1993 at PUC-Rio, emphasizing simplicity and safety for integration into larger applications like games and databases. In practice, load is commonly used in environments such as , where the EVAL command leverages 's interpreter to execute server-side scripts atomically, compiling the provided Lua code string via an internal load-like mechanism before execution. Due to its ability to execute arbitrary code, load requires careful handling in untrusted contexts; Lua's C API variant lua_load explicitly warns that malformed bytecode can crash the interpreter, underscoring the need for input validation or sandboxing. For safer alternatives, libraries like literal restrict evaluation to literal expressions (e.g., numbers, booleans) without full code execution, mitigating risks in sensitive applications.

Other Languages

In the R programming language, the eval() function evaluates an R expression in a specified environment, returning the computed value, with the default being the parent frame. This allows dynamic execution of code represented as expressions or parsed from strings using parse(), enabling metaprogramming techniques such as computing on the language for data analysis tasks. For instance, it is commonly used to evaluate variable expressions within functions, like eval(substitute(mean(x)), data) to compute means conditionally. However, due to potential performance overhead and scoping issues, alternatives like non-standard evaluation in packages such as dplyr are often preferred for data manipulation. MATLAB's eval executes a containing a MATLAB expression and returns outputs in specified , facilitating dynamic code generation for tasks like naming in loops or integrating with scripts. It interprets the input as MATLAB code, supporting statements, expressions, and even M-file execution, but it is discouraged in modern code due to difficulties and reduced compared to operations or structures. For example, eval('A = 1:10') assigns a to A, though using cell arrays or structs is recommended instead for better maintainability. advises against its routine use, favoring alternatives like feval for evaluation or assignin for . The Tcl scripting language provides the eval command, which concatenates its arguments into a Tcl script and executes it, enabling dynamic command construction and at runtime. This is fundamental to Tcl's , where eval performs a new round of interpretation separate from the initial parsing, useful for building commands from lists or handling expansions in embedded scripts. An example is eval {puts $var}, which outputs the value of var after substitution. Tcl's design emphasizes through its string-based nature, but eval requires careful to avoid injection risks, and it is often paired with uplevel for scoped evaluation. Julia, a high-performance for technical computing, includes the Base.eval function to execute expressions at global scope within the current module, supporting through expression objects generated by Meta.parse. This allows runtime , such as defining functions dynamically: eval(:(f(x) = x^2)) creates a square . Julia's "world age" system tracks invalidations from eval to maintain optimization, preventing issues in multi-threaded or just-in-time compiled contexts. While powerful for domain-specific languages or symbolic , its use is cautioned against in performance-critical code due to recompilation costs, with macros preferred for compile-time generation.

Command-Line Interpreters

Unix Shells

In Unix shells compliant with the standard, such as the (sh) and its derivatives including , Korn shell (ksh), and (zsh), the eval builtin command enables the dynamic execution of strings as shell commands. It constructs a command by concatenating its arguments, separated by single spaces, and then parses and executes the resulting string using the shell's standard word expansions, redirections, and other syntactic processing, as if the user had typed it directly at the prompt. This functionality makes eval a fundamental tool for implementing techniques in shell scripting, where commands need to be generated and run at runtime. The syntax for eval is straightforward: eval [argument...]. When invoked without arguments or with only null arguments, it returns an of zero without performing any action. In all other cases, the matches that of the executed command; if a occurs during in an interactive , a non-zero status is returned instead. A primary application of eval is achieving indirect reference, which standard variable expansion alone cannot accomplish. Consider the following example in a POSIX-compliant :
foo=10
x=foo
eval "y=\$$x"
echo "$y"  # Outputs: 10
Here, eval receives the string y=$foo (after the shell expands $x to foo and the \$ becomes literal $), then executes it to set y to the value of $foo (10). This pattern is essential for tasks like iterating over dynamically named variables or evaluating expressions stored in configuration files. Note that in , indirect expansion can also use ${!x} for safer alternatives, but eval ensures POSIX compatibility. In scripting, eval facilitates the execution of commands assembled from multiple sources, such as loops or external data. For instance, to navigate to a directory specified in a variable:
cmd='cd /tmp'
eval "$cmd"
The shell parses $cmd after concatenation, applying expansions like tilde (~) or parameter substitution within the string. In Bash specifically, eval integrates seamlessly with advanced features like arrays—for example, eval "${array[@]}" can execute a sequence of commands stored in an array by treating its elements as arguments. However, while powerful for automation in tools like build scripts or configuration parsers, its use demands careful quoting to avoid unintended expansions or errors from untrusted input. Across Unix shells, eval maintains portability, ensuring consistent behavior in environments from traditional System V to modern distributions, though extensions in shells like may introduce subtle variations in expansion order.

PowerShell

In PowerShell, the primary mechanism for dynamic evaluation akin to eval in other languages is the Invoke-Expression cmdlet, often abbreviated as iex. This cmdlet evaluates a specified string as a PowerShell command or expression within the current scope and returns the results of its execution. Introduced with Windows PowerShell 1.0 in , it enables runtime execution of code generated dynamically, such as from variables or user input, making it useful for advanced scripting scenarios like or processing configuration strings. The syntax for Invoke-Expression is straightforward: Invoke-Expression [-Command] <String> [<CommonParameters>], where the -Command parameter accepts a mandatory string input representing the code to execute. It supports pipelining, allowing strings to be passed directly from other cmdlets. For instance, to run a simple command stored in a , one might use $command = "Get-Process"; Invoke-Expression $command, which lists running processes. Another example involves evaluating arithmetic or conditional expressions dynamically: Invoke-Expression "[2 + 2](/page/2_+_2_=_?)" yields 4, demonstrating its ability to parse and compute expressions . It can also execute external scripts by providing their as a string, such as Invoke-Expression "C:\scripts\example.ps1", though this is generally discouraged in favor of direct invocation. Despite its utility, Invoke-Expression poses significant security risks, particularly when handling untrusted input, as it can execute arbitrary malicious . For example, if user-supplied strings are passed without validation, attackers could inject commands to access sensitive data or escalate privileges, a common vector in -based exploits. explicitly advises against its use except as a last resort, recommending verification of inputs and avoidance in production scripts due to these vulnerabilities. The Script Analyzer includes a rule, AvoidUsingInvokeExpression, to flag its usage in reviews. Safer alternatives include the call operator &, which invokes commands or scripts without full string evaluation. For dynamic scenarios, techniques like splatting (using hashtables for parameters) or direct cmdlet calls with parameterized inputs provide robustness without the risks of . Best practices emphasize predefined commands, input sanitization, and leveraging PowerShell's execution policy to restrict unsigned scripts, ensuring secure automation in enterprise environments.

Security Considerations

Risks and Vulnerabilities

The primary risk associated with the eval function across programming languages is , where untrusted input—such as user-supplied strings—can be interpreted and run as code, potentially leading to attacks. This vulnerability arises because eval dynamically parses and executes strings in the current execution context, granting the injected code access to the program's full privileges, including operations, network access, and manipulation. For instance, attackers can exploit this to steal sensitive , escalate privileges, or install , making eval a common vector for remote code execution (RCE) vulnerabilities. In , the eval() executes arbitrary expressions and is explicitly warned against when handling input due to its potential for breaches, as it can invoke any with the caller's permissions. Similarly, in , eval() poses an enormous risk by allowing malicious actors to execute arbitrary scripts, often leading to (XSS) attacks if combined with dynamic content loading. 's eval() is described as very dangerous for the same reason, enabling execution of arbitrary that can compromise server resources if tainted with external data. Perl mitigates some risks through taint mode, which marks untrusted data and prevents its use in eval without explicit untainting, but failure to validate inputs can still result in insecure dependencies and code injection. Ruby's Kernel#eval carries potential security vulnerabilities when passed untrusted input, as it evaluates strings as Ruby code and is advised against in secure coding practices unless implementing interactive environments like REPLs. In Lua, the equivalent load() or deprecated loadstring() functions compile and execute strings as Lua chunks, risking command injection if unverified user input is processed, particularly in embedded systems. For command-line interpreters, Bash's eval builtin executes strings as shell commands, amplifying risks in scripts processing environment variables or arguments, potentially allowing command injection like $(rm -rf /) if inputs are not sanitized. PowerShell's Invoke-Expression is particularly hazardous for injection attacks, as it parses and runs arbitrary strings, and recommends avoiding it entirely in favor of safer parameter passing to prevent script injection. Across these contexts, the common vulnerability stems from insufficient input validation, underscoring the need for strict whitelisting or avoidance of dynamic evaluation.

Best Practices and Mitigations

To mitigate the risks associated with the eval , the primary recommendation is to avoid its use altogether when processing untrusted input, as it can execute arbitrary code and lead to injection vulnerabilities. Instead, developers should opt for safer alternatives that separate code from data, such as parameterized APIs or predefined functions, which prevent direct interpretation of user-supplied strings as executable code. For instance, in , when parsing data, JSON.parse() should be used rather than eval(), as the latter can execute embedded scripts if the input is malicious. Input validation forms a critical layer of defense when eval cannot be entirely avoided. All untrusted data must undergo strict allowlist-based validation on the server side, rejecting inputs that do not conform to expected formats, lengths, or character sets (e.g., canonicalization to thwart ). This includes centralized routines to classify data sources as trusted or untrusted and to block meta-characters or patterns that could form valid code snippets. Output encoding in the appropriate further reduces risks by ensuring user data is treated as literal text rather than executable elements. Additional mitigations involve restricting dynamic code generation and employing runtime protections. Developers should prohibit user-generated or modifiable code paths, such as dynamic includes or redirects based on input, and integrate static code analysis tools during development to detect eval usage. In environments where dynamic evaluation is unavoidable, sandboxing techniques—such as isolated virtual machines or restricted execution contexts—can limit the impact of potential exploits, though these are not substitutes for avoidance. Implementing a (WAF) with rules tuned for patterns provides an extra barrier by filtering malicious payloads before they reach the application.

Alternatives to Eval

Safe Evaluation Methods

Safe evaluation methods provide mechanisms to execute dynamically generated code or expressions while mitigating the security risks associated with unrestricted functions like eval, such as and access to sensitive resources. These approaches typically involve restricting the language subset, isolating execution environments, or inputs to prevent malicious operations. They are essential in scenarios involving untrusted input, like user-defined scripts in web applications or plugins, where full eval would expose systems to injection attacks. One common technique is literal evaluation, which safely parses and constructs only basic data structures without executing arbitrary code. In , the ast.literal_eval function evaluates strings containing Python literals—such as numbers, strings, lists, dictionaries, and sets—while rejecting operators, function calls, or attribute access that could lead to code execution. This method is explicitly designed as a secure alternative to eval, limiting output to immutable or basic container types and raising exceptions for invalid inputs. For instance, ast.literal_eval("[1, 2, {'key': 'value'}]") returns the corresponding , but attempts like ast.literal_eval("1 + 2") or code with imports fail with a ValueError. However, it remains vulnerable to resource exhaustion from deeply nested structures. Similar literal parsers exist in other languages, such as JavaScript's JSON.parse for subsets, though they are limited to structured data without computational logic. Restricted execution environments enforce a safe subset of the language by transforming or filtering code before evaluation. RestrictedPython, a originally developed for the , defines a guarded Python dialect that prohibits dangerous features like imports, attribute access beyond , and global modifications. It compiles code into a restricted form using custom guards and safe builtins, allowing controlled execution of untrusted scripts in a defined . This approach supports 3.9–3.13 as of 2024 and is used in systems to evaluate user formulas without full interpreter access. A (CVE-2025-22153) affecting versions prior to 8.0 allowed bypass via type confusion in try/except* clauses on 3.11–3.13.1; it was patched in RestrictedPython 8.0 by removing support for try/except*. In Lua, the load function combined with a restricted globals table achieves analogous isolation, permitting only approved functions and variables. These methods prioritize configurability but require careful management to avoid bypassing restrictions. Sandboxed virtual machines offer stronger by running in separate or processes, preventing direct to the host . In , the vm module creates V8 contexts with custom global objects, allowing scripts to execute in isolated scopes via vm.runInContext. For example, a context with variables like { console: limitedConsole } can evaluate user without exposing the full Node API, though it includes options for timeouts to bound execution time. This is suitable for server-side plugins but is not foolproof against all attacks, as asynchronous or prototype pollution may leak information. More robust sandboxing uses OS-level mechanisms, such as or filters, to containerize evaluation—evident in deprecated tools like Google's Native Client (NaCl), which compiled C/C++ to sandboxed but reached end-of-life in by mid-2025. Modern alternatives include WebAssembly-based sandboxing for browser environments. Seminal work in this area includes dynamic information flow tracking, which monitors and enforces security policies during execution to prevent data leaks in dynamic languages. For expression-specific evaluation, parser-based evaluators compile strings into abstract syntax trees (ASTs) and compute results without full code execution, ideal for mathematical or logical formulas. Libraries like in parse and symbolically evaluate expressions in a controlled domain, supporting operations like differentiation while disallowing side effects. In , third-party parsers such as math.js evaluate numeric expressions safely by building and traversing ASTs, avoiding eval entirely. These tools emphasize domain-specific safety, but they falter on general-purpose code. Overall, selecting a safe method depends on the required expressiveness, with hybrid approaches combining parsing and sandboxing for optimal security.

Language-Specific Alternatives

In programming languages, alternatives to the universal eval function often prioritize safety by restricting execution to specific expression types, scopes, or parsed structures, reducing risks like code injection while enabling dynamic behavior. Python
The ast module provides literal_eval, a function that safely evaluates strings containing only Python literals—such as numbers, strings, tuples, lists, dictionaries, sets, booleans, None, and Ellipsis—into corresponding Python objects without executing arbitrary code. This makes it suitable for untrusted input where full code execution is unnecessary, though it may still consume excessive resources on malformed data. For example, ast.literal_eval("{'x': 1, 'y': 2}") returns {'x': 1, 'y': 2}.
JavaScript
The Function constructor offers a controlled alternative to eval by creating a new function from a string in a local scope, allowing parameters to be passed explicitly and avoiding direct interference with the current execution context. It is faster and less prone to scope pollution than direct eval, but remains vulnerable to malicious input. For dynamic property access, bracket notation (obj[key]) is safer and avoids parsing errors common with eval. Data parsing should use JSON.parse instead, which limits evaluation to valid JSON structures like arrays and objects.
Java
Java's javax.script package includes the ScriptEngine interface for dynamic , typically via JavaScript engines like GraalVM's GraalJS in JDK 15 and later (Nashorn was removed after JDK 14). A ScriptEngineManager retrieves an engine (e.g., getEngineByName("graal.js")), which can then eval a string as a script, supporting binding with put and function invocation through Invocable. This approach isolates execution and integrates with Java objects, serving as a structured substitute for ad-hoc code . For instance, engine.eval("print('Hello')") outputs "Hello" without compiling full Java code.
C#
C# supports dynamic evaluation through expression trees in System.Linq.Expressions, which build and compile lambda expressions at runtime for safe, performant execution—ideal for dynamic LINQ queries without full compilation. The Expression class constructs trees from parameters, enabling methods like Where or Select to be composed from strings or runtime state. For broader code execution, the Roslyn compiler API (Microsoft.CodeAnalysis) allows compiling C# snippets into assemblies in memory, though it requires careful sandboxing for security. Microsoft guidance emphasizes expression trees for runtime query modification, such as filtering collections based on user input.
Ruby
Ruby's public_send method dynamically invokes existing methods by name as a safer option to eval for simple dynamic calls, restricting execution to defined public methods and avoiding arbitrary code. It replaces patterns like eval("#{method_name}(args)") with obj.public_send(method_name, args), enhancing and preventing injection via undefined actions. Variants like instance_eval and class_eval provide scoped evaluation but are still eval-like and discouraged for untrusted input.
PHP
PHP deprecated create_function in favor of anonymous functions for dynamic callback creation, offering a safer path than eval for generating code from strings in controlled contexts like array mapping. For expression parsing, libraries like math-parser handle mathematical or logical strings without full execution, though PHP lacks a built-in non-eval evaluator for general cases. Official recommendations avoid eval entirely, favoring structured alternatives like call_user_func for indirect invocation.

Theoretical Aspects

Eval-Apply Cycle

The eval-apply cycle forms the core mechanism of interpreters, embodying the recursive process by which symbolic expressions are evaluated and functions are applied. Introduced in John McCarthy's foundational paper on , the cycle relies on two mutually recursive functions: EVAL, which computes the value of an in a given environment (represented as an association list), and APPLY, which executes a function on evaluated arguments. EVAL handles different expression types—such as atoms (via association lookup), quoted forms (returning the form unchanged), conditional expressions (via an auxiliary evcon function), and function applications (by evaluating the operator and arguments separately, then invoking ). For instance, APPLY constructs a new expression by consing the function onto the quoted argument list and delegates back to EVAL with an empty association list, creating a recursive loop that resolves computations universally, akin to a Turing machine's step-by-step execution. This interplay reveals the theoretical elegance of Lisp's design, where the language's syntax and semantics are expressed using the language itself, enabling self-interpretation. McCarthy's pseudocode illustrates the cycle's recursion: EVAL[e; a] dispatches based on e's structure, and for applications, it calls APPLY[car[e]; evlis[cdr[e]; a]], where evlis evaluates the argument list. APPLY[f; args], in turn, is defined as EVAL[cons[f; appq[args]]; NIL], with appq quoting the arguments to prevent premature evaluation. This mutual recursion ensures that evaluation unwinds nested expressions step-by-step, treating code as data while preserving computational integrity. The cycle's universality stems from its ability to compute any recursive function definable over symbolic expressions, foundational to Lisp's homoiconicity and metaprogramming capabilities. Later formalized in the metacircular evaluator of Structure and Interpretation of Computer Programs (SICP) by Harold Abelson and Gerald Jay Sussman, the cycle is implemented in Scheme (a Lisp dialect) to demonstrate an interpreter written in the language it interprets. Here, eval classifies expressions in an environment model: self-evaluating atoms return themselves, variables are looked up, special forms like if or lambda are handled directly, and applications evaluate the operator to a procedure, evaluate operands to values, and pass them to apply. Apply then dispatches: primitive procedures execute immediately, while compound procedures (lambdas) extend the environment with parameters bound to arguments and recursively eval the body sequence. This metacircular structure, as pseudocode shows:
(define (eval exp env)
  (cond ((self-evaluating? exp) exp)
        ((application? exp)
         (apply (eval (operator exp) env)
                (list-of-values (operands exp) env)))
        ;; other clauses for variables, quotes, etc.
        (else (error "Unknown expression type"))))

(define (apply proc args)
  (cond ((primitive-procedure? proc)
         (apply-primitive-procedure proc args))
        ((compound-procedure? proc)
         (eval-sequence (procedure-body proc)
                        (extend-environment (procedure-parameters proc)
                                            args
                                            (procedure-environment proc))))
        ;; other clauses
        (else (error "Unknown procedure type"))))
exposes the essence of as a cycle toggling between expression dispatch and execution, bridging abstract syntax and semantics. Theoretically, the eval-apply cycle underscores Lisp's reflective nature, allowing interpreters to model their own behavior and facilitating extensions like macros or domain-specific languages. It contrasts with non-recursive evaluation models in other languages, highlighting recursion's role in handling lexical scoping and dynamic binding. While early Lisp implementations like Lisp 1.5 operationalized this cycle for machine computation, modern variants (e.g., in or ) refine it for efficiency, yet retain the conceptual loop for clarity in . This enduring framework influences language design beyond , informing interpreters in functional and scripting languages.

Categorical Perspectives

In , the operation, often denoted as eval, is formalized as the evaluation in the structure of a (CCC). A CCC is a category equipped with all finite products and, for any objects A and B, an B^A representing the hom-set \hom(A, B), together with a natural \hom(Z, B^A) \cong \hom(Z \times A, B). The evaluation is the \mathrm{ev}_{A,B}: B^A \times A \to B, which serves as the counit of the adjunction between the product functor (- \times A) and the (-)^A. This encodes the application of a generalized "" from A to B to an element of A, providing a categorical foundation for independent of specific computational models. The satisfies key universal properties that ensure the closed structure. Specifically, for any f: Z \times A \to B, there exists a unique "" \overline{f}: Z \to B^A such that \mathrm{ev}_{A,B} \circ (\overline{f} \times \mathrm{id}_A) = f, and conversely, \overline{\mathrm{ev}_{A,B} \circ (g \times \mathrm{id}_A)} = g for g: Z \to B^A. These properties, along with the internal c_{X,Y,Z}: Z^Y \times Y^X \to Z^X defined via \overline{\mathrm{ev}_{Y,Z} \circ (\mathrm{ev}_{X,Y} \times \mathrm{id}_Z)}, mirror the equational axioms of typed calculi, where corresponds to beta-reduction in application. Seminal work by Joachim Lambek established the functional completeness of CCCs, showing that adjoining indeterminates to a CCC generates all s via and application, directly linking the categorical structure to terms. This categorical perspective on eval underpins for functional programming languages, where CCCs model the simply-typed . In such models, types correspond to objects, terms to s, to the Curry operator, and application to the evaluation , ensuring that beta-eta equivalence is captured by equality of s. For instance, in the , B^A is the set of functions from A to B, and \mathrm{ev}_{A,B} is application, providing a concrete interpretation. Gérard Huet's equational framework further mechanizes this connection, translating terms into CCC arrows while preserving reduction semantics, which has influenced and proof assistants. Higher-order extensions, such as in locally Cartesian closed categories, generalize this to dependent types, but the core eval remains the evaluation counit.

References

  1. [1]
    Built-in Functions
    ### Summary of `eval()` from https://docs.python.org/3/library/functions.html#eval
  2. [2]
    eval() - JavaScript - MDN Web Docs - Mozilla
    Jul 8, 2025 · The eval() function evaluates JavaScript code represented as a string and returns its completion value. The source is parsed as a script.
  3. [3]
    [PDF] History of Lisp - John McCarthy
    Feb 12, 1979 · the LISP function eval that serves both as a formal definition of the language and as an interpreter, and garbage collection as a means of ...
  4. [4]
    [PDF] A Large-scale Study of the Use of Eval in JavaScript Applications
    The `eval` function in JavaScript transforms text into executable code, parsing a string as source code and immediately executing it.
  5. [5]
    eval - Perldoc Browser
    eval in all its forms is used to execute a little Perl program, trapping any errors encountered so they don't crash the calling program.
  6. [6]
    Eval (GNU Emacs Lisp Reference Manual)
    The `eval` function evaluates a form at runtime, returning the result. It's used when forms are not automatically evaluated, like after reading from text.
  7. [7]
    eval - Manual - PHP
    Evaluates the given code as PHP. The code being evaluated inherits the variable scope of the line on which the eval() call occurs.Missing: programming | Show results with:programming
  8. [8]
    [PDF] Recursive Functions of Symbolic Expressions and Their ...
    Recursive Functions of Symbolic Expressions and Their Computation by Machine, Part I. John McCarthy, Massachusetts Institute of Technology, Cambridge, Mass.
  9. [9]
    [PDF] JavaScript: the first 20 years - Department of Computer Science
    JavaScript was initially designed and implemented in May 1995 at Netscape by Brendan Eich, one of the authors of this paper. It was intended to be a simple, ...
  10. [10]
    ECMAScript® 2026 Language Specification
    ### Specification Details for `eval` in ECMAScript
  11. [11]
    Method: Kernel#eval – Documentation for core (3.4.3) - RubyDoc.info
    Evaluates the Ruby expression(s) in string. If binding is given, which must be a Binding object, the evaluation is performed in its context.
  12. [12]
    Lua 5.4 Reference Manual
    Summary of each segment:
  13. [13]
    Why can't compiled language have "eval" function? - Stack Overflow
    Mar 14, 2012 · Compiled languages can certainly have an eval function, even for C/C++. You'd just need some way for the runtime to run the compiler.dynamic - Writing `eval()` in C - Stack OverflowHow is eval implemented? - interpreter - Stack OverflowMore results from stackoverflow.com
  14. [14]
    javax.script (Java Platform SE 8 ) - Oracle Help Center
    Script execution uses eval methods of ScriptEngine and methods of the Invocable interface. Binding: This facility allows Java objects to be exposed to script ...
  15. [15]
    Compile code by using C# compiler - C# - Microsoft Learn
    May 7, 2022 · This article provides sample code that enables you to compile code from a text source. The application allows you to either just build the executable or build ...
  16. [16]
    eval package - github.com/apaxa-go/eval - Go Packages
    Dec 23, 2017 · Value used to store arguments passed to/returned from expression. It can stores: data, type, built-in function and package. GoLang valid ...For whose who wants to... · Documentation · Overview · Types
  17. [17]
    Evaluation - Clojure
    In all cases, evaluation is the same - a single object is considered by the compiler, evaluated, and its result returned. If an expression needs to be compiled, ...
  18. [18]
    ECMAScript Language Specification - ECMA-262 Edition 5.1
    ... eval function is a String, it is treated as an ECMAScript Program. The eval code for a particular invocation of eval is the global code portion of that Program.
  19. [19]
    Strict mode - JavaScript - MDN Web Docs
    Jul 8, 2025 · JavaScript's strict mode is a way to opt in to a restricted variant of JavaScript, thereby implicitly opting-out of "sloppy mode".<|control11|><|separator|>
  20. [20]
    Built-in Functions
    ### Description
  21. [21]
    ast — Abstract syntax trees
    ### Summary of `ast.literal_eval`
  22. [22]
    perlsec - Perl security - Perldoc Browser
    Such attacks involve constructing a set of keys which collide into the same bucket producing inefficient behavior.Taint mode · Laundering and Detecting... · Cleaning Up Your Path
  23. [23]
    Safe - Compile and execute code in restricted compartments
    Bugs in the perl interpreter that could be abused to bypass Safe restrictions are not treated as vulnerabilities. See perlsecpolicy for additional information.
  24. [24]
    Code Injection - OWASP Foundation
    If an attacker is able to inject PHP code into an application and have it executed, they are only limited by what PHP is capable of. Command injection consists ...
  25. [25]
    create_function - Manual - PHP
    Creates a function dynamically from the parameters passed, and returns a unique name for it. Caution. This function internally performs an eval() and as such ...
  26. [26]
    RFC: Remove preg_replace /e modifier - PHP Wiki
    Feb 4, 2012 · Security issues​​ As /e evaluates arbitrary PHP code it can easily be exploited if user input is not carefully validated or sanitized. For ...<|control11|><|separator|>
  27. [27]
  28. [28]
  29. [29]
  30. [30]
    A Look at the Design of Lua - Communications of the ACM
    Nov 1, 2018 · The function load simplifies the semantics of Lua in two ways: First, unlike eval , load is pure and total; it has no side effects and it always ...
  31. [31]
  32. [32]
    [PDF] The Evolution of Lua
    We report on the birth and evolution of Lua and discuss how it moved from a simple configuration language to a versatile, widely used language that supports ...
  33. [33]
    EVAL | Docs - Redis
    Invoke the execution of a server-side Lua script. The first argument is the script's source code. Scripts are written in Lua and executed by the embedded Lua ...
  34. [34]
  35. [35]
    mpeterv/literal: Safely evaluate literal Lua expressions - GitHub
    Jan 16, 2014 · literal is a library for safe evaluation of Lua literal expressions, written in pure Lua. It can evaluate literals like nil, true, false, decimal and ...
  36. [36]
    eval function - RDocumentation
    The `eval` function evaluates an R expression in a specified environment, returning the computed value. The default environment is the parent frame.
  37. [37]
    eval - Evaluate MATLAB expression - MathWorks
    The `eval` function evaluates a MATLAB expression, returning outputs in specified variables. It is less efficient and can be difficult to debug.Description · Input Arguments
  38. [38]
  39. [39]
    Invoke-Expression (Microsoft.PowerShell.Utility)
    Description. The Invoke-Expression cmdlet evaluates or runs a specified string as a command and returns the results of the expression or command.
  40. [40]
    PowerShell Invoke-Expression | Equivalent of Wake-on-Lan (WOL ...
    Jun 25, 2000 · Introduction to PowerShell's Invoke-Expression. Invoke-Expression is a brilliant PowerShell cmdlet, which mimics us typing instructions into ...
  41. [41]
    Avoid using Invoke-Expression - PowerShell - Microsoft Learn
    Nov 17, 2022 · The Invoke-Expression cmdlet should only be used as a last resort. In most scenarios, safer and more robust alternatives are available.Missing: best | Show results with:best
  42. [42]
  43. [43]
  44. [44]
    AJAX Security - OWASP Cheat Sheet Series
    Don't use eval() , new Function() or other code evaluation tools. eval() function is dangerous, never use it. Needing to use eval() usually indicates a problem ...<|control11|><|separator|>
  45. [45]
    security - Documentation for Ruby 2.3.0
    This document aims to discuss many of these pitfalls and provide more secure alternatives where applicable.
  46. [46]
    Lua 5.4 Reference Manual
    May 21, 2025 · Lua is a powerful, efficient, lightweight, embeddable scripting language. It supports procedural programming, object-oriented programming, functional ...
  47. [47]
  48. [48]
    HTML5 Security - OWASP Cheat Sheet Series
    Never evaluate passed messages as code (e.g. via eval() ) or insert it to a page DOM (e.g. via innerHTML ), as that would create a DOM-based XSS vulnerability.
  49. [49]
    Injection Prevention - OWASP Cheat Sheet Series
    This article is focused on providing clear, simple, actionable guidance for preventing the entire category of Injection flaws in your applications.SQL Injection Prevention · LDAP Injection Prevention
  50. [50]
    Secure Coding Practices Checklist - OWASP Foundation
    Database security · Use strongly typed parameterized queries · Utilize input validation and output encoding and be sure to address meta characters. · Ensure that ...
  51. [51]
    [PDF] CODE REVIEW GUIDE - OWASP Foundation
    code. If the response is JSON, never use the insecure eval() function; use the safe option JSON.parse() instead. • Endpoints exposed through the 'ws ...
  52. [52]
    ast — Abstract syntax trees — Python 3.14.0 documentation
    The ast module helps Python applications to process trees of the Python abstract syntax grammar.
  53. [53]
    RestrictedPython 8.2 documentation
    **Summary of RestrictedPython as a Safe Alternative to eval in Python:**
  54. [54]
    VM (executing JavaScript) | Node.js v25.2.0 Documentation
    ### Summary of `vm` Module for Safe, Sandboxed Code Execution
  55. [55]
    [PDF] Secure Program Execution via Dynamic Information Flow Tracking
    We present a simple architectural mechanism called dynamic information flow tracking that can significantly improve the security of computing systems with ...Missing: alternatives | Show results with:alternatives
  56. [56]
    2 The Java Scripting API
    Create a ScriptEngineManager object. Get a ScriptEngine object from the manager. Evaluate the script using the script engine's eval() method.
  57. [57]
    How to Build LINQ Queries based on run-time state - Microsoft Learn
    Apr 25, 2024 · In this article, you learn techniques to use System.Linq.IQueryable<T> interface and types that implement it to modify the shape of a query at run time.Use run-time state from within... · Call more LINQ methods
  58. [58]
    Ruby Style Guide
    This Ruby style guide recommends best practices so that real-world Ruby programmers can write code that can be maintained by other real-world Ruby programmers.
  59. [59]
    [PDF] Recursive Functions of Symbolic Expressions and Their ...
    We hope to describe some of the symbolic computations for which LISP has been used in another paper, and also to give elsewhere some applications of our ...
  60. [60]
    4.1 The Metacircular Evaluator
    Figure 4.1: The eval - apply cycle exposes the essence of a computer language. The implementation of the evaluator will depend upon procedures that define the ...
  61. [61]
    [PDF] Cartesian Closed Categories and Lambda-Calculus - Inria
    The purpose of these notes is to propose an equational framework for the formalization, and ultimately the mechanization, of categorical reasoning. This ...Missing: seminal | Show results with:seminal
  62. [62]
    Functional completeness of cartesian categories - ScienceDirect.com
    Functional completeness of cartesian categories. Author links open ... Lambek. Deductive systems and categories I. Math. Systems Theory, 2 (1968), pp ...