Questions tagged [eval]

eval is a function that allows a programmer to execute arbitrary code written in the same language, from a string variable within a code.

Filter by
Sorted by
Tagged with
564votes
25answers
273kviews

Why is using the JavaScript eval function a bad idea?

The eval function is a powerful and easy way to dynamically generate code, so what are the caveats?
user avatar
  • 6,486
522votes
3answers
251kviews

What's the difference between eval, exec, and compile?

I've been looking at dynamic evaluation of Python code, and come across the eval() and compile() functions, and the exec statement. Can someone please explain the difference between eval and exec, ...
user avatar
329votes
10answers
485kviews

What does Python's eval() do?

In the book that I am reading on Python, it keeps using the code eval(input('blah')) I read the documentation, and I understand it, but I still do not see how it changes the input() function. What ...
user avatar
  • 9,689
325votes
7answers
220kviews

Evaluate expression given as a string

I'm curious to know if R can use its eval() function to perform calculations provided by e.g. a string. This is a common case: eval("5+5") However, instead of 10 I get: [1] "5+5" Any solution?
user avatar
288votes
26answers
102kviews

When is JavaScript's eval() not evil?

I'm writing some JavaScript code to parse user-entered functions (for spreadsheet-like functionality). Having parsed the formula I could convert it into JavaScript and run eval() on it to yield the ...
user avatar
234votes
7answers
275kviews

Using python's eval() vs. ast.literal_eval()

I have a situation with some code where eval() came up as a possible solution. Now I have never had to use eval() before but, I have come across plenty of information about the potential danger it ...
user avatar
  • 6,649
198votes
19answers
90kviews

Convert a string to a template string

Is it possible to create a template string as a usual string, let a = "b:${b}"; and then convert it into a template string, let b = 10; console.log(a.template()); // b:10 without eval, new ...
user avatar
  • 2,523
190votes
10answers
425kviews

eval command in Bash and its typical uses

After reading the bash man pages and with respect to this post. I am still having trouble understanding what exactly the eval command does and which would be its typical uses. For example if we do: ...
user avatar
  • 6,850
164votes
8answers
58kviews

Why is using 'eval' a bad practice?

I am using the following class to easily store data of my songs. class Song: """The class to store the details of each song""" attsToStore=('Name', 'Artist', 'Album', 'Genre', 'Location') ...
user avatar
  • 6,236
162votes
5answers
142kviews

instantiate a class from a variable in PHP?

I know this question sounds rather vague so I will make it more clear with an example: $var = 'bar'; $bar = new {$var}Class('var for __construct()'); //$bar = new barClass('var for __construct()'); ...
user avatar
  • 31.4k
162votes
13answers
202kviews

Is there an eval() function in Java?

I have a string like the following: String str = "4*5"; Now I have to get the result of 20 by using the string. I know in some other languages the eval() function will do this. How can I do this in ...
user avatar
  • 5,178
144votes
12answers
16kviews

Why exactly is eval evil?

I know that Lisp and Scheme programmers usually say that eval should be avoided unless strictly necessary. I’ve seen the same recommendation for several programming languages, but I’ve not yet seen a ...
user avatar
  • 9,403
143votes
22answers
184kviews

Executing <script> elements inserted with .innerHTML

I've got a script that inserts some content into an element using innerHTML. The content could for example be: <script type="text/javascript">alert('test');</script> <strong>test&...
user avatar
  • 5,503
137votes
10answers
236kviews

How to modify a global variable within a function in bash?

I'm working with this: GNU bash, version 4.1.2(1)-release (x86_64-redhat-linux-gnu) I have a script like below: #!/bin/bash e=2 function test1() { e=4 echo "hello" } test1 echo "$e" Which ...
user avatar
  • 6,840
137votes
1answer
5kviews

Why {} + {} is NaN only on the client side? Why not in Node.js?

While [] + [] is an empty string, [] + {} is "[object Object]", and {} + [] is 0. Why is {} + {} NaN? > {} + {} NaN My question isn't why ({} + {}).toString() is "[object Object][object Object]"...
user avatar
130votes
3answers
80kviews

Why should eval be avoided in Bash, and what should I use instead?

Time and time again, I see Bash answers on Stack Overflow using eval and the answers get bashed, pun intended, for the use of such an "evil" construct. Why is eval so evil? If eval can't be used ...
user avatar
  • 17.4k
112votes
5answers
289kviews

How to print / echo environment variables?

How do I print the environment variable just being set? NAME=sam echo "$NAME" # empty You can see here using eval it works. Is this the way? NAME=sam eval 'echo $NAME' # => sam
user avatar
  • 47.7k
104votes
6answers
52kviews

Are eval() and new Function() the same thing?

Are these two functions doing the same thing behind the scenes? (in single statement functions) var evaluate = function(string) { return eval('(' + string + ')'); } var func = function(string) { ...
user avatar
  • 32.2k
97votes
4answers
21kviews

(1, eval)('this') vs eval('this') in JavaScript?

I start to read JavaScript Patterns, some codes confused me. var global = (function () { return this || (1, eval)('this'); }()); Here are my questions: Q1: (1, eval) === eval? Why and how ...
user avatar
  • 1,527
88votes
20answers
46kviews

When is eval evil in php?

In all the years I have been developing in PHP, I have always heard that using eval() is evil. Considering the following code, wouldn't it make sense to use the second (and more elegant) option? If ...
user avatar
72votes
2answers
28kviews

Dynamically evaluate an expression from a formula in Pandas

I would like to perform arithmetic on one or more dataframes columns using pd.eval. Specifically, I would like to port the following code that evaluates a formula: x = 5 df2['D'] = df1['A'] + (df1['B']...
user avatar
  • 321k
70votes
4answers
14kviews

What specifically are the dangers of eval(parse(...))?

There are several questions on how to avoid using eval(parse(...)) r-evalparse-is-often-suboptimal avoiding-the-infamous-evalparse-construct Which sparks the questions: Why Specifically should eval(...
user avatar
65votes
11answers
106kviews

Accessing object property as string and setting its value

I have an instance of the Account class. Each account object has an owner, reference, etc. One way I can access an accounts properties is through accessors like account.Reference; but I would like ...
user avatar
  • 11.8k
61votes
13answers
90kviews

Calculate string value in javascript, not using eval

Is there a way to calculate a formula stored in a string in JavaScript without using eval? Normally I would do something like var apa = "12/5*9+9.4*2"; alert(eval(apa)); So, does anyone know about ...
user avatar
  • 23.7k
60votes
2answers
147kviews

Understanding ASP.NET Eval() and Bind()

Can anyone show me some absolutely minimal ASP.NET code to understand Eval() and Bind()? It is best if you provide me with two separate code-snippets or may be web-links.
user avatar
  • 16.8k
60votes
9answers
110kviews

How can I evaluate a C# expression dynamically?

I would like to do the equivalent of: object result = Eval("1 + 3"); string now = Eval("System.DateTime.Now().ToString()") as string Following Biri s link, I got this snippet (modified to remove ...
user avatar
  • 64.6k
57votes
1answer
7kviews

Indirect eval call in strict mode

I understand about how eval() works in non-strict contexts, however the case of using eval() in strict mode has completely befuddled me. When eval() is called directly in the global scope, variables ...
user avatar
55votes
4answers
39kviews

Python: make eval safe [duplicate]

I want an easy way to do a "calculator API" in Python. Right now I don't care much about the exact set of features the calculator is going to support. I want it to receive a string, say "1+1" and ...
user avatar
  • 245k
51votes
15answers
43kviews

What's the main benefit of using eval() in JavaScript?

I know this may be a newbie question, but I'm curious as to the main benefit of eval() - where would it be used best? I appreciate any info.
user avatar
  • 11.1k
49votes
5answers
18kviews

Safely sandbox and execute user submitted JavaScript?

I would like to have the ability to let users submit arbitrary JavaScript code, which is then sent to a Node.JS server and safely executed before the output is sent back to multiple clients (as JSON). ...
user avatar
46votes
7answers
53kviews

eval javascript, check for syntax error

I wanted to know if it is possible to find through javascript if a call to eval() has a syntax error or undefined variable, etc... so lets say I use eval for some arbitrary javascript is there a way ...
user avatar
  • 22.5k
44votes
7answers
36kviews

Indirect variable assignment in bash

Seems that the recommended way of doing indirect variable setting in bash is to use eval: var=x; val=foo eval $var=$val echo $x # --> foo The problem is the usual one with eval: var=x; val=1$'\...
user avatar
  • 28.5k
43votes
8answers
133kviews

Escaping single quotes in JavaScript string for JavaScript evaluation

I have a project, in which some JavaScript var is evaluated. Because the string needs to be escaped (single quotes only), I have written the exact same code in a test function. I have the following ...
user avatar
43votes
1answer
2kviews

What are the caveats of using source versus parse & eval?

Short version Can I replace source(filename, local = TRUE, encoding = 'UTF-8') with eval(parse(filename, encoding = 'UTF-8')) without any risk of breakage, to make UTF-8 source files work on ...
user avatar
41votes
6answers
11kviews

Python eval: is it still dangerous if I disable builtins and attribute access?

We all know that eval is dangerous, even if you hide dangerous functions, because you can use Python's introspection features to dig down into things and re-extract them. For example, even if you ...
user avatar
  • 79.2k
40votes
2answers
17kviews

How to rescue an eval in Ruby?

I'm trying to figure out how to rescue syntax errors that come up when eval()ing code in Ruby 1.8.6. I would expect the following Ruby code: #!/usr/bin/ruby good_str = "(1+1)" bad_str = "(1+1" # ...
user avatar
39votes
5answers
30kviews

<%# Eval("State") %> or <%# DataBinder.Eval(Container.DataItem, "state")%>

What is the difference between having <%# Eval("State") %> in your aspx page, versus having <%# DataBinder.Eval(Container.DataItem, "State") %> in your aspx page?
user avatar
  • 44.1k
37votes
1answer
36kviews

How to evaluate formula passed as string in PHP?

Just trying to figure out the proper and safer way to execute mathematical operation passed as string. In my scenario it is values fetched from image EXIF data. After little research I found two way ...
user avatar
  • 5,888
36votes
4answers
93kviews

Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control

I am getting the following error Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control. but all I am trying to do is inside a ASP.NET ...
user avatar
  • 363
35votes
7answers
13kviews

Why does JavaScript's eval need parentheses to eval JSON data?

I've learned (the hard way) that I need to add parentheses around JSON data, like this: stuff = eval('(' + data_from_the_wire + ')'); // where data_from_the_wire was, for example {"text": "hello"} (...
user avatar
  • 3,292
35votes
9answers
18kviews

Restricting eval() to a narrow scope

I have a javascript file that reads another file which may contain javascript fragments that need to be eval()-ed. The script fragments are supposed to conform to a strict subset of javascript that ...
user avatar
  • 39.6k
35votes
4answers
58kviews

Best Technique for Multiple Eval Fields in Gridview ItemTemplate?

What is the best way to use multiple EVAL fields in a GridView ItemTemplate? Looking to have some control over formatting for appearance as well as setting up hyperlinks/javascript etc.
user avatar
  • 5,420
35votes
6answers
2kviews

Why does eval() exist?

Many programmers say it is a bad practice to use the eval() function: When is JavaScript's eval() not evil? I'd like to take a moment to address the premise of your question - that eval() is "...
user avatar
  • 2,481
33votes
3answers
30kviews

eval to import a module

I can't import a module using the eval() function. So, I have a function where if I do import vfs_tests as v it works. However, the same import using eval() like eval('import vfs_tests as v') throws ...
user avatar
  • 4,769
33votes
4answers
75kviews

Is it possible to execute a string in MySQL?

I have to convert a MSSQL stored proc that passes a varchar that is a query: INSERT INTO Results EXEC (@Expresion); This isn't working. I'm pretty sure that EXEC and EXECUTE aren't MySQL commands, ...
user avatar
  • 33.7k
32votes
3answers
6kviews

How do you evaluate a string as a clojure expression?

How would I get something similar to the following?: (evaluate-text "(+ 1 2)") ; resolves to 3
user avatar
  • 3,463
32votes
6answers
28kviews

PHP eval and capturing errors (as much as possible)

Disclaimer; I'm fully aware of the pitfalls and "evils" of eval, including but not limited to: performance issues, security, portability etc. The problem Reading the PHP manual on eval... eval() ...
user avatar
  • 26.7k
32votes
6answers
101kviews

Alternative to eval() javascript [duplicate]

I work mainly with javascript, Jquery, knockout, etc The thing that attracted eval() to me is var a = 5; var b = 10; eval("a+b"); //Gives me output 15 Note: I work in cases where the value of a and ...
user avatar
  • 9,860
31votes
3answers
34kviews

What is the best way to handle exceptions in Perl?

I've noticed that Exception.pm and Error.pm don't seem to be extensively used in the Perl community. Is that due to the large footprint of eval for exception handling? Also Perl programs appear to ...
user avatar
  • 44.9k
31votes
4answers
30kviews

I can't seem to use the Bash "-c" option with arguments after the "-c" option string

The man page for Bash says, regarding the -c option: -c string If the -c option is present, then commands are read from string. If there are arguments after the string, they are assigned to ...
user avatar
  • 2,046

15 30 50 per page
1
2 3 4 5
93