<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.expertiza.ncsu.edu/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Vgupta6</id>
	<title>Expertiza_Wiki - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.expertiza.ncsu.edu/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Vgupta6"/>
	<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Special:Contributions/Vgupta6"/>
	<updated>2026-06-28T07:25:37Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.41.0</generator>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012&amp;diff=68328</id>
		<title>CSC/ECE 517 Fall 2012</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012&amp;diff=68328"/>
		<updated>2012-10-26T22:16:21Z</updated>

		<summary type="html">&lt;p&gt;Vgupta6: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;*[[CSC/ECE 517 Fall 2012/ch1 n xx]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w1 rk]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w20 pp]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w5 su]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w6 pp]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w4 aj]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w7 am]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w8 aa]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w9 av]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w10 pk]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w11 ap]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1a 1w12 mv]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w14 gv]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w17 ir]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w18 as]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w22 an]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w21 aa]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w21 wi]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w31 sa]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1a 1w16 br]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1a 1w23 as]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w24 nr]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w15 rt]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w3 pl]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w32 cm]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w5 dp]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w37 ss]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w67 ks]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w27 ms]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w29 sa]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w33 op]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w19 sa]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w34 vd]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w35 sa]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1 1w30 rp]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w58 am]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w47 sk]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w69 mv]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w44 as]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w45 is]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w53 kc]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w40 ar]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w39 sn]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w54 go]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w56 ms]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w64 nn]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w66 as]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w40 as]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w42 js]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w46 sm]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w71 gs]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w63 dv]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w55 ms]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w57 mp]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w52 an]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch1b 1w38 nm]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w60 ac]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch1b 1w62 rb]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w29 st]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w30 an]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w17 pt]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w31 up]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w9 ms]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w19 is]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w26 aj]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w5 dp]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w16 dp]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w8 vp]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w18 as]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w3 jm]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w23 sr]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2a_2w11_aa]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w15 rr]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2012/ch2a 2w33 pv]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2a_2w20_aa]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2a_2w14_bb]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2a_2w21_ap]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2a_2w13_sm]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2a_2w4_sa]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2a_2w25_nr]]&lt;br /&gt;
*[[CSC/ECE_517_Fall_2012/ch2a_2w12_sv]]&lt;/div&gt;</summary>
		<author><name>Vgupta6</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w28_mv&amp;diff=65334</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w28 mv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w28_mv&amp;diff=65334"/>
		<updated>2012-09-20T03:30:39Z</updated>

		<summary type="html">&lt;p&gt;Vgupta6: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= eval =&lt;br /&gt;
Eval function in most of the languages will either be a valid string representing expression in the language or some languages may allow blocks of valid code. The input is executed by the interpreter or compiler in same manner as other code in the language.&lt;br /&gt;
&lt;br /&gt;
= Advantage of eval =&lt;br /&gt;
eval is useful for writing metaprogamming code in most of the languages. This is a great asset for statically typed languages where most of the operatioons are performed at the compile time. Consider the following case:&lt;br /&gt;
&lt;br /&gt;
Problem statement:&lt;br /&gt;
Write a script to accept directories and pattern as input and send the files matching the pattern to a different server using sftp.&lt;br /&gt;
&lt;br /&gt;
We can maintain a string to store all the filenames to be moved to a directory on destination server preceded with appropriate sftp commands and use eval method to execute those commands in batch mode.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= Disadvantages of eval =&lt;br /&gt;
=== Slow in Execution ===&lt;br /&gt;
The code executed by eval runs slower, thus , whenever possible alternative techniques should be used.&lt;br /&gt;
&lt;br /&gt;
=== Security Risks ===&lt;br /&gt;
[[File:Security-risk.png|thumb|Border &amp;quot;border&amp;quot;|Location|Alignment|Size 0.5|link=http://expertiza.csc.ncsu.edu/wiki/index.php/File:Security-risk.png|alt=Alt|eval as a dangerous method in PHP]]&lt;br /&gt;
&lt;br /&gt;
'''JavaScript eval()'''&lt;br /&gt;
&amp;lt;br&amp;gt;Because most JSON-formatted text is also syntactically legal JavaScript code, an easy way for a JavaScript program to parse JSON-formatted data is to use the built-in JavaScript eval() function, which was designed to evaluate JavaScript expressions. Rather than using a JSON-specific parser, the JavaScript interpreter itself is used to execute the JSON data to produce native JavaScript objects. However, there are some Unicode characters that are valid in JSON strings but invalid in JavaScript, so additional escaping would be needed before using a JavaScript interpreter.&lt;br /&gt;
Unless precautions are taken to validate the data first, the eval technique is subject to security vulnerabilities if the data and the entire JavaScript environment is not within the control of a single trusted source. For example, if the data is itself not trusted, it may be subject to malicious JavaScript code injection attacks. Also, such breaches of trust may create vulnerabilities for data theft, authentication forgery, and other potential misuse of data and resources. Regular expressions can be used to validate the data prior to invoking eval(). Consider the following contrived example:&lt;br /&gt;
[[File:Problem-eval.png|thumb|Border &amp;quot;border&amp;quot;|Location|Alignment|Size 0.5|link=http://expertiza.csc.ncsu.edu/wiki/index.php/File:Problem-eval.png|alt=Alt|JavaScript eval risk ]]&lt;br /&gt;
&lt;br /&gt;
''' &amp;lt;html&amp;gt; &amp;lt;br&amp;gt;&amp;lt;head&amp;gt;&amp;lt;br&amp;gt;&amp;lt;/head&amp;gt;&amp;lt;br&amp;gt;&amp;lt;body&amp;gt;&amp;lt;br&amp;gt;&amp;lt;script type=&amp;quot;text/javascript&amp;quot; lang=&amp;quot;javascript&amp;quot;&amp;gt; '''&lt;br /&gt;
'''alert(&amp;quot;some problem&amp;quot;);&amp;lt;br&amp;gt;var evalstring=&amp;quot;obj={ name:'username',address: '238 Milton , st. Mary , WT 87382'}; '''&lt;br /&gt;
'''alert('This is malicious code');&amp;quot;;&amp;lt;br&amp;gt;eval(evalstring);&amp;lt;br&amp;gt;&amp;lt;/script&amp;gt;&amp;lt;br&amp;gt;&amp;lt;/body&amp;gt;&amp;lt;br&amp;gt;&amp;lt;/head&amp;gt;&amp;lt;br&amp;gt; &amp;lt;/html&amp;gt; '''&lt;br /&gt;
&lt;br /&gt;
Let's assume that the client side script is expecting a JSON object from the server. However, along with the JSON object it can also send other valid javascript statement. If the above code is run in the browser, after the execution of eval there will be an alert message saying 'This is malicious code'. The alert statement represents a placeholder for any malacious javascript code. This can be avoided by using the safe Javascript library at JSON.org which contains the JSON.parse method.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= eval in Different Languages =&lt;br /&gt;
&lt;br /&gt;
== Perl ==&lt;br /&gt;
The general form of [[Perl (programming language)|Perl]] expects an expression or a block of code as an expression. The expression passed as an argument is parsed and executed as small Perl program. If there are no errors in the expression , which is parsed , then it is executed within the lexical context of the current Perl program. &lt;br /&gt;
&lt;br /&gt;
=== Error Handling with eval ===&lt;br /&gt;
Eval can be used to trap the errors. During execution of the routines , the program might die due to errors and interrupt the code. However , using eval , the code can still be running if the errors are handled inside the eval without being exited. We can evaluate the functionality of eval from the given code.&amp;lt;br&amp;gt;&lt;br /&gt;
''' print &amp;quot;enter number 1 \n&amp;quot;; &amp;lt;br&amp;gt;$number1 = &amp;lt;STDIN&amp;gt;; &amp;lt;br&amp;gt;chomp $number1; &amp;lt;br&amp;gt;print &amp;quot;enter number 2 \n&amp;quot;; &amp;lt;br&amp;gt;$number2 = &amp;lt;STDIN&amp;gt;; ''' '''&amp;lt;br&amp;gt;chomp $number2; &amp;lt;br&amp;gt;print &amp;quot;division output is &amp;quot;; &amp;lt;br&amp;gt;$x =eval {$result = $number1/$number2}; &amp;lt;br&amp;gt;print &amp;quot;$x\n$@&amp;quot;; &amp;lt;br&amp;gt;$sum =''' '''$number1 + $number2; &amp;lt;br&amp;gt;print &amp;quot;sum is $sum \n&amp;quot;; &amp;lt;br&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
The above given code prompts the user to give 2 inputs and then performs the division and addition of the numbers. If the second number entered by the user is 0 , then in that case code exits showing the error message and it does not calculate the addition. However , we can handle this exception using eval. Eval handles the exception, shows the appropriate error message and then return the control back to the program without exiting it. Hence , even if the second input is 0 , the addition is performed by the eval method.&lt;br /&gt;
&lt;br /&gt;
== Ruby ==&lt;br /&gt;
Most non-compiled languages have some kind of eval function, which will take a string and execute it as code. The eval method in Ruby accomplishes this, and is rather straightforward. The prototype of eval&lt;br /&gt;
eval(string [, binding [, filename [,lineno]]]) → obj.&lt;br /&gt;
Consider the following example code:&amp;lt;br&amp;gt;&lt;br /&gt;
''' #!/usr/bin/env ruby&amp;lt;br&amp;gt;code = &amp;quot;Time.now&amp;quot;&amp;lt;br&amp;gt;result = eval(code)&amp;lt;br&amp;gt;puts result'''&amp;lt;br&amp;gt;&lt;br /&gt;
The method call Time.now is stored in a string. It's not executed, compiled or even looked at (other than determining that it's a string literal) by the Ruby interpreter. It's just a string, anything could be in there. However, once you call the eval method, that all changes. The Ruby interpreter parses this string as if it were Ruby code and runs it. It will return whatever the code evaluates to.&lt;br /&gt;
&lt;br /&gt;
Simple enough, but why would anyone want to use this? There's a lot of debate whether eval should be used, and it's often considered bad form and hackish. There are also some pretty big security implications in using eval at all in your code, so it's not something that should be used lightly. There is one great use for eval though: developer consoles. Adding a way for programmers to issue Ruby commands to your program directly by using eval can be a powerful debugging tool and quite easy to implement.&lt;br /&gt;
&lt;br /&gt;
''' Using bindings in eval: '''&lt;br /&gt;
You can use eval to run code from a string, but things are not as simple as they seem. The eval method executes code, but where in the program is that code executed? The context in which code is executed is called a binding. Code is bound to a context of instance variable, local variables, and global variables. If you were to reference the variable x, it's up to the binding to decide which variable named x you're referring to. Examine the following code.&amp;lt;br&amp;gt;&lt;br /&gt;
'''#!/usr/bin/env ruby&amp;lt;br&amp;gt;def func&amp;lt;br&amp;gt;x = 1337&amp;lt;br&amp;gt;return binding&amp;lt;br&amp;gt;end&amp;lt;br&amp;gt;x = 10&amp;lt;br&amp;gt;binding = func()&amp;lt;br&amp;gt;puts''' &lt;br /&gt;
'''eval(&amp;quot;x&amp;quot;, binding)&amp;lt;br&amp;gt;puts eval(&amp;quot;x&amp;quot;)&amp;lt;br&amp;gt;bind1 = func(1337)&amp;lt;br&amp;gt;'''&lt;br /&gt;
'''bind2 = func(404)&amp;lt;br&amp;gt;puts eval(&amp;quot;number&amp;quot;, bind1)&amp;lt;br&amp;gt;puts eval(&amp;quot;number&amp;quot;, bind2)'''&lt;br /&gt;
&lt;br /&gt;
This program works just as you would expect. Two invocations of the func method, and two numbers printed out. Bindings are an interesting feature, but just like eval, their use is controversial. While they can be useful at times, there's often a better way to achieve the same result.&lt;br /&gt;
&lt;br /&gt;
== PHP ==&lt;br /&gt;
&lt;br /&gt;
In PHP, eval executes code in a string almost exactly as if it had been put in the file instead of the call to eval(). The only exception is that errors are reported as coming from a call to eval(), and return statements become the result of the function.&lt;br /&gt;
Unlike some languages, the argument to eval must be a string of one or more complete statements, not just expressions; however, one can get the &amp;quot;expression&amp;quot; form of eval by putting the expression in a return statement, which causes eval to return the result of that expression.&amp;lt;br&amp;gt; &lt;br /&gt;
'''eval prototype''' : mixed eval ( string $code )&lt;br /&gt;
The code in eval doesn't have to be wrapped in PHP tags ( &amp;lt;?php ?&amp;gt; ). If there is parse error in the argument to eval then it returns false. eval can return a value if the last statement in the argument passed to eval has a return statement then eval will return a value which can be stored in a string.&amp;lt;br&amp;gt;&lt;br /&gt;
'''&amp;lt;?php&amp;lt;br&amp;gt;$string = 'cup';&amp;lt;br&amp;gt;$name = 'coffee';&amp;lt;br&amp;gt;$str = 'This is a $string with my $name in it.';'''&lt;br /&gt;
'''&amp;lt;br&amp;gt;echo $str. &amp;quot;\n&amp;quot;;&amp;lt;br&amp;gt;eval(&amp;quot;\$str = \&amp;quot;$str\&amp;quot;;&amp;quot;);&amp;lt;br&amp;gt;echo $str. &amp;quot;\n&amp;quot;;&amp;lt;br&amp;gt;?&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
The above code will output&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;quot;This is a $string with my $name in it.&amp;lt;br&amp;gt;&lt;br /&gt;
This is a cup with my coffee in it.&amp;quot;&amp;lt;br&amp;gt;&lt;br /&gt;
Unlike perl if there is a fatal error in the code passed to eval the whole script exits.&lt;br /&gt;
&lt;br /&gt;
= References =&lt;br /&gt;
http://ruby.about.com/od/advancedruby/a/Eval-Running-Code-On-The-Fly.htm&amp;lt;br&amp;gt;&lt;br /&gt;
http://php.net/manual/en/function.eval.php&amp;lt;br&amp;gt;&lt;br /&gt;
http://www.ruby-doc.org/core-1.9.3/Kernel.html&amp;lt;br&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Eval#PHP&amp;lt;br&amp;gt;&lt;br /&gt;
http://img.com.com/i/tr/bldr/pub/evalcalc.html?eq=&amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Vgupta6</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w28_mv&amp;diff=65152</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w28 mv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w28_mv&amp;diff=65152"/>
		<updated>2012-09-15T04:06:38Z</updated>

		<summary type="html">&lt;p&gt;Vgupta6: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= eval =&lt;br /&gt;
Eval function in most of the languages will either be a valid string representing expression in the language or some languages may allow blocks of valid code. The input is executed by the interpreter or compiler in same manner as other code in the language.&lt;br /&gt;
&lt;br /&gt;
= Advantage of eval =&lt;br /&gt;
eval is useful for writing metaprogamming code in most of the languages. This is a great asset for statically typed languages where most of the operatioons are performed at the compile time. Consider the following case:&lt;br /&gt;
&lt;br /&gt;
Problem statement:&lt;br /&gt;
Write a script to accept directories and pattern as input and send the files matching the pattern to a different server using sftp.&lt;br /&gt;
&lt;br /&gt;
We can maintain a string to store all the filenames to be moved to a directory on destination server preceded with appropriate sftp commands and use eval method to execute those commands in batch mode.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= Disadvantages of eval =&lt;br /&gt;
=== Slow in Execution ===&lt;br /&gt;
The code executed by eval runs slower, thus , whenever possible alternative techniques should be used.&lt;br /&gt;
&lt;br /&gt;
=== Security Risks ===&lt;br /&gt;
[[File:Security-risk.png|thumb|Border &amp;quot;border&amp;quot;|Location|Alignment|Size 0.5|link=http://expertiza.csc.ncsu.edu/wiki/index.php/File:Security-risk.png|alt=Alt|eval as a dangerous method in PHP]]&lt;br /&gt;
&lt;br /&gt;
'''JavaScript eval()'''&lt;br /&gt;
&amp;lt;br&amp;gt;Because most JSON-formatted text is also syntactically legal JavaScript code, an easy way for a JavaScript program to parse JSON-formatted data is to use the built-in JavaScript eval() function, which was designed to evaluate JavaScript expressions. Rather than using a JSON-specific parser, the JavaScript interpreter itself is used to execute the JSON data to produce native JavaScript objects. However, there are some Unicode characters that are valid in JSON strings but invalid in JavaScript, so additional escaping would be needed before using a JavaScript interpreter.&lt;br /&gt;
Unless precautions are taken to validate the data first, the eval technique is subject to security vulnerabilities if the data and the entire JavaScript environment is not within the control of a single trusted source. For example, if the data is itself not trusted, it may be subject to malicious JavaScript code injection attacks. Also, such breaches of trust may create vulnerabilities for data theft, authentication forgery, and other potential misuse of data and resources. Regular expressions can be used to validate the data prior to invoking eval(). Consider the following contrived example:&lt;br /&gt;
[[File:Problem-eval.png|thumb|Border &amp;quot;border&amp;quot;|Location|Alignment|Size 0.5|link=http://expertiza.csc.ncsu.edu/wiki/index.php/File:Problem-eval.png|alt=Alt|JavaScript eval risk ]]&lt;br /&gt;
&lt;br /&gt;
'''&amp;lt;html&amp;gt;&amp;lt;br&amp;gt;&amp;lt;head&amp;gt;&amp;lt;br&amp;gt;&amp;lt;/head&amp;gt;&amp;lt;br&amp;gt;&amp;lt;body&amp;gt;&amp;lt;br&amp;gt;&amp;lt;script type=&amp;quot;text/javascript&amp;quot; lang=&amp;quot;javascript&amp;quot;&amp;gt;'''&lt;br /&gt;
'''alert(&amp;quot;some problem&amp;quot;);&amp;lt;br&amp;gt;var evalstring=&amp;quot;obj={ name:'username',address: '238 Milton , st. Mary , WT 87382'}; alert('This is''' '''malicious code');&amp;quot;;&amp;lt;br&amp;gt;eval(evalstring);&amp;lt;br&amp;gt;&amp;lt;/script&amp;gt;&amp;lt;br&amp;gt;&amp;lt;/body&amp;gt;&amp;lt;br&amp;gt;&amp;lt;/head&amp;gt;&amp;lt;br&amp;gt;&amp;lt;/html&amp;gt; &amp;lt;br&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
Let's assume that the client side script is expecting a JSON object from the server. However, along with the JSON object it can also send other valid javascript statement. If the above code is run in the browser, after the execution of eval there will be an alert message saying 'This is malicious code'. The alert statement represents a placeholder for any malacious javascript code. This can be avoided by using the safe Javascript library at JSON.org which contains the JSON.parse method.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= eval in Different Languages =&lt;br /&gt;
&lt;br /&gt;
== Perl ==&lt;br /&gt;
The general form of [[Perl (programming language)|Perl]] expects an expression or a block of code as an expression. The expression passed as an argument is parsed and executed as small Perl program. If there are no errors in the expression , which is parsed , then it is executed within the lexical context of the current Perl program. &lt;br /&gt;
&lt;br /&gt;
=== Error Handling with eval ===&lt;br /&gt;
Eval can be used to trap the errors. During execution of the routines , the program might die due to errors and interrupt the code. However , using eval , the code can still be running if the errors are handled inside the eval without being exited. We can evaluate the functionality of eval from the given code.&amp;lt;br&amp;gt;&lt;br /&gt;
''' print &amp;quot;enter number 1 \n&amp;quot;; &amp;lt;br&amp;gt;$number1 = &amp;lt;STDIN&amp;gt;; &amp;lt;br&amp;gt;chomp $number1; &amp;lt;br&amp;gt;print &amp;quot;enter number 2 \n&amp;quot;; &amp;lt;br&amp;gt;$number2 = &amp;lt;STDIN&amp;gt;; ''' '''&amp;lt;br&amp;gt;chomp $number2; &amp;lt;br&amp;gt;print &amp;quot;division output is &amp;quot;; &amp;lt;br&amp;gt;$x =eval {$result = $number1/$number2}; &amp;lt;br&amp;gt;print &amp;quot;$x\n$@&amp;quot;; &amp;lt;br&amp;gt;$sum =''' '''$number1 + $number2; &amp;lt;br&amp;gt;print &amp;quot;sum is $sum \n&amp;quot;; &amp;lt;br&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
The above given code prompts the user to give 2 inputs and then performs the division and addition of the numbers. If the second number entered by the user is 0 , then in that case code exits showing the error message and it does not calculate the addition. However , we can handle this exception using eval. Eval handles the exception, shows the appropriate error message and then return the control back to the program without exiting it. Hence , even if the second input is 0 , the addition is performed by the eval method.&lt;br /&gt;
&lt;br /&gt;
== Ruby ==&lt;br /&gt;
Most non-compiled languages have some kind of eval function, which will take a string and execute it as code. The eval method in Ruby accomplishes this, and is rather straightforward. The prototype of eval&lt;br /&gt;
eval(string [, binding [, filename [,lineno]]]) → obj.&lt;br /&gt;
Consider the following example code:&amp;lt;br&amp;gt;&lt;br /&gt;
''' #!/usr/bin/env ruby&amp;lt;br&amp;gt;code = &amp;quot;Time.now&amp;quot;&amp;lt;br&amp;gt;result = eval(code)&amp;lt;br&amp;gt;puts result'''&amp;lt;br&amp;gt;&lt;br /&gt;
The method call Time.now is stored in a string. It's not executed, compiled or even looked at (other than determining that it's a string literal) by the Ruby interpreter. It's just a string, anything could be in there. However, once you call the eval method, that all changes. The Ruby interpreter parses this string as if it were Ruby code and runs it. It will return whatever the code evaluates to.&lt;br /&gt;
&lt;br /&gt;
Simple enough, but why would anyone want to use this? There's a lot of debate whether eval should be used, and it's often considered bad form and hackish. There are also some pretty big security implications in using eval at all in your code, so it's not something that should be used lightly. There is one great use for eval though: developer consoles. Adding a way for programmers to issue Ruby commands to your program directly by using eval can be a powerful debugging tool and quite easy to implement.&lt;br /&gt;
&lt;br /&gt;
''' Using bindings in eval: '''&lt;br /&gt;
You can use eval to run code from a string, but things are not as simple as they seem. The eval method executes code, but where in the program is that code executed? The context in which code is executed is called a binding. Code is bound to a context of instance variable, local variables, and global variables. If you were to reference the variable x, it's up to the binding to decide which variable named x you're referring to. Examine the following code.&amp;lt;br&amp;gt;&lt;br /&gt;
'''#!/usr/bin/env ruby&amp;lt;br&amp;gt;def func&amp;lt;br&amp;gt;x = 1337&amp;lt;br&amp;gt;return binding&amp;lt;br&amp;gt;end&amp;lt;br&amp;gt;x = 10&amp;lt;br&amp;gt;binding = func()&amp;lt;br&amp;gt;puts''' &lt;br /&gt;
'''eval(&amp;quot;x&amp;quot;, binding)&amp;lt;br&amp;gt;puts eval(&amp;quot;x&amp;quot;)&amp;lt;br&amp;gt;bind1 = func(1337)&amp;lt;br&amp;gt;'''&lt;br /&gt;
'''bind2 = func(404)&amp;lt;br&amp;gt;puts eval(&amp;quot;number&amp;quot;, bind1)&amp;lt;br&amp;gt;puts eval(&amp;quot;number&amp;quot;, bind2)'''&lt;br /&gt;
&lt;br /&gt;
This program works just as you would expect. Two invocations of the func method, and two numbers printed out. Bindings are an interesting feature, but just like eval, their use is controversial. While they can be useful at times, there's often a better way to achieve the same result.&lt;br /&gt;
&lt;br /&gt;
== PHP ==&lt;br /&gt;
&lt;br /&gt;
In PHP, eval executes code in a string almost exactly as if it had been put in the file instead of the call to eval(). The only exception is that errors are reported as coming from a call to eval(), and return statements become the result of the function.&lt;br /&gt;
Unlike some languages, the argument to eval must be a string of one or more complete statements, not just expressions; however, one can get the &amp;quot;expression&amp;quot; form of eval by putting the expression in a return statement, which causes eval to return the result of that expression.&amp;lt;br&amp;gt; &lt;br /&gt;
'''eval prototype''' : mixed eval ( string $code )&lt;br /&gt;
The code in eval doesn't have to be wrapped in PHP tags ( &amp;lt;?php ?&amp;gt; ). If there is parse error in the argument to eval then it returns false. eval can return a value if the last statement in the argument passed to eval has a return statement then eval will return a value which can be stored in a string.&amp;lt;br&amp;gt;&lt;br /&gt;
'''&amp;lt;?php&amp;lt;br&amp;gt;$string = 'cup';&amp;lt;br&amp;gt;$name = 'coffee';&amp;lt;br&amp;gt;$str = 'This is a $string with my $name in it.';'''&lt;br /&gt;
'''&amp;lt;br&amp;gt;echo $str. &amp;quot;\n&amp;quot;;&amp;lt;br&amp;gt;eval(&amp;quot;\$str = \&amp;quot;$str\&amp;quot;;&amp;quot;);&amp;lt;br&amp;gt;echo $str. &amp;quot;\n&amp;quot;;&amp;lt;br&amp;gt;?&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
The above code will output&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;quot;This is a $string with my $name in it.&amp;lt;br&amp;gt;&lt;br /&gt;
This is a cup with my coffee in it.&amp;quot;&amp;lt;br&amp;gt;&lt;br /&gt;
Unlike perl if there is a fatal error in the code passed to eval the whole script exits.&lt;br /&gt;
&lt;br /&gt;
= References =&lt;br /&gt;
http://ruby.about.com/od/advancedruby/a/Eval-Running-Code-On-The-Fly.htm&amp;lt;br&amp;gt;&lt;br /&gt;
http://php.net/manual/en/function.eval.php&amp;lt;br&amp;gt;&lt;br /&gt;
http://www.ruby-doc.org/core-1.9.3/Kernel.html&amp;lt;br&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Eval#PHP&amp;lt;br&amp;gt;&lt;br /&gt;
http://img.com.com/i/tr/bldr/pub/evalcalc.html?eq=&amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Vgupta6</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w28_mv&amp;diff=65151</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w28 mv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w28_mv&amp;diff=65151"/>
		<updated>2012-09-15T04:05:51Z</updated>

		<summary type="html">&lt;p&gt;Vgupta6: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= eval =&lt;br /&gt;
Eval function in most of the languages will either be a valid string representing expression in the language or some languages may allow blocks of valid code. The input is executed by the interpreter or compiler in same manner as other code in the language.&lt;br /&gt;
&lt;br /&gt;
= Advantage of eval =&lt;br /&gt;
eval is useful for writing metaprogamming code in most of the languages. This is a great asset for statically typed languages where most of the operatioons are performed at the compile time. Consider the following case:&lt;br /&gt;
&lt;br /&gt;
Problem statement:&lt;br /&gt;
Write a script to accept directories and pattern as input and send the files matching the pattern to a different server using sftp.&lt;br /&gt;
&lt;br /&gt;
We can maintain a string to store all the filenames to be moved to a directory on destination server preceded with appropriate sftp commands and use eval method to execute those commands in batch mode.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= Disadvantages of eval =&lt;br /&gt;
=== Slow in Execution ===&lt;br /&gt;
The code executed by eval runs slower, thus , whenever possible alternative techniques should be used.&lt;br /&gt;
&lt;br /&gt;
=== Security Risks ===&lt;br /&gt;
[[File:Security-risk.png|thumb|Border &amp;quot;border&amp;quot;|Location|Alignment|Size 0.5|link=http://expertiza.csc.ncsu.edu/wiki/index.php/File:Security-risk.png|alt=Alt|eval as a dangerous method in PHP]]&lt;br /&gt;
&lt;br /&gt;
'''JavaScript eval()'''&lt;br /&gt;
&amp;lt;br&amp;gt;Because most JSON-formatted text is also syntactically legal JavaScript code, an easy way for a JavaScript program to parse JSON-formatted data is to use the built-in JavaScript eval() function, which was designed to evaluate JavaScript expressions. Rather than using a JSON-specific parser, the JavaScript interpreter itself is used to execute the JSON data to produce native JavaScript objects. However, there are some Unicode characters that are valid in JSON strings but invalid in JavaScript, so additional escaping would be needed before using a JavaScript interpreter.&lt;br /&gt;
Unless precautions are taken to validate the data first, the eval technique is subject to security vulnerabilities if the data and the entire JavaScript environment is not within the control of a single trusted source. For example, if the data is itself not trusted, it may be subject to malicious JavaScript code injection attacks. Also, such breaches of trust may create vulnerabilities for data theft, authentication forgery, and other potential misuse of data and resources. Regular expressions can be used to validate the data prior to invoking eval(). Consider the following contrived example:&lt;br /&gt;
[[File:Problem-eval.png|thumb|Border &amp;quot;border&amp;quot;|Location|Alignment|Size 0.5|link=http://expertiza.csc.ncsu.edu/wiki/index.php/File:Problem-eval.png|alt=Alt|JavaScript eval risk ]]&lt;br /&gt;
Problem-eval.png&lt;br /&gt;
'''&amp;lt;html&amp;gt;&amp;lt;br&amp;gt;&amp;lt;head&amp;gt;&amp;lt;br&amp;gt;&amp;lt;/head&amp;gt;&amp;lt;br&amp;gt;&amp;lt;body&amp;gt;&amp;lt;br&amp;gt;&amp;lt;script type=&amp;quot;text/javascript&amp;quot; lang=&amp;quot;javascript&amp;quot;&amp;gt;'''&lt;br /&gt;
'''alert(&amp;quot;some problem&amp;quot;);&amp;lt;br&amp;gt;var evalstring=&amp;quot;obj={ name:'username',address: '238 Milton , st. Mary , WT 87382'}; alert('This is''' '''malicious code');&amp;quot;;&amp;lt;br&amp;gt;eval(evalstring);&amp;lt;br&amp;gt;&amp;lt;/script&amp;gt;&amp;lt;br&amp;gt;&amp;lt;/body&amp;gt;&amp;lt;br&amp;gt;&amp;lt;/head&amp;gt;&amp;lt;br&amp;gt;&amp;lt;/html&amp;gt; &amp;lt;br&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
Let's assume that the client side script is expecting a JSON object from the server. However, along with the JSON object it can also send other valid javascript statement. If the above code is run in the browser, after the execution of eval there will be an alert message saying 'This is malicious code'. The alert statement represents a placeholder for any malacious javascript code. This can be avoided by using the safe Javascript library at JSON.org which contains the JSON.parse method.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= eval in Different Languages =&lt;br /&gt;
&lt;br /&gt;
== Perl ==&lt;br /&gt;
The general form of [[Perl (programming language)|Perl]] expects an expression or a block of code as an expression. The expression passed as an argument is parsed and executed as small Perl program. If there are no errors in the expression , which is parsed , then it is executed within the lexical context of the current Perl program. &lt;br /&gt;
&lt;br /&gt;
=== Error Handling with eval ===&lt;br /&gt;
Eval can be used to trap the errors. During execution of the routines , the program might die due to errors and interrupt the code. However , using eval , the code can still be running if the errors are handled inside the eval without being exited. We can evaluate the functionality of eval from the given code.&amp;lt;br&amp;gt;&lt;br /&gt;
''' print &amp;quot;enter number 1 \n&amp;quot;; &amp;lt;br&amp;gt;$number1 = &amp;lt;STDIN&amp;gt;; &amp;lt;br&amp;gt;chomp $number1; &amp;lt;br&amp;gt;print &amp;quot;enter number 2 \n&amp;quot;; &amp;lt;br&amp;gt;$number2 = &amp;lt;STDIN&amp;gt;; ''' '''&amp;lt;br&amp;gt;chomp $number2; &amp;lt;br&amp;gt;print &amp;quot;division output is &amp;quot;; &amp;lt;br&amp;gt;$x =eval {$result = $number1/$number2}; &amp;lt;br&amp;gt;print &amp;quot;$x\n$@&amp;quot;; &amp;lt;br&amp;gt;$sum =''' '''$number1 + $number2; &amp;lt;br&amp;gt;print &amp;quot;sum is $sum \n&amp;quot;; &amp;lt;br&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
The above given code prompts the user to give 2 inputs and then performs the division and addition of the numbers. If the second number entered by the user is 0 , then in that case code exits showing the error message and it does not calculate the addition. However , we can handle this exception using eval. Eval handles the exception, shows the appropriate error message and then return the control back to the program without exiting it. Hence , even if the second input is 0 , the addition is performed by the eval method.&lt;br /&gt;
&lt;br /&gt;
== Ruby ==&lt;br /&gt;
Most non-compiled languages have some kind of eval function, which will take a string and execute it as code. The eval method in Ruby accomplishes this, and is rather straightforward. The prototype of eval&lt;br /&gt;
eval(string [, binding [, filename [,lineno]]]) → obj.&lt;br /&gt;
Consider the following example code:&amp;lt;br&amp;gt;&lt;br /&gt;
''' #!/usr/bin/env ruby&amp;lt;br&amp;gt;code = &amp;quot;Time.now&amp;quot;&amp;lt;br&amp;gt;result = eval(code)&amp;lt;br&amp;gt;puts result'''&amp;lt;br&amp;gt;&lt;br /&gt;
The method call Time.now is stored in a string. It's not executed, compiled or even looked at (other than determining that it's a string literal) by the Ruby interpreter. It's just a string, anything could be in there. However, once you call the eval method, that all changes. The Ruby interpreter parses this string as if it were Ruby code and runs it. It will return whatever the code evaluates to.&lt;br /&gt;
&lt;br /&gt;
Simple enough, but why would anyone want to use this? There's a lot of debate whether eval should be used, and it's often considered bad form and hackish. There are also some pretty big security implications in using eval at all in your code, so it's not something that should be used lightly. There is one great use for eval though: developer consoles. Adding a way for programmers to issue Ruby commands to your program directly by using eval can be a powerful debugging tool and quite easy to implement.&lt;br /&gt;
&lt;br /&gt;
''' Using bindings in eval: '''&lt;br /&gt;
You can use eval to run code from a string, but things are not as simple as they seem. The eval method executes code, but where in the program is that code executed? The context in which code is executed is called a binding. Code is bound to a context of instance variable, local variables, and global variables. If you were to reference the variable x, it's up to the binding to decide which variable named x you're referring to. Examine the following code.&amp;lt;br&amp;gt;&lt;br /&gt;
'''#!/usr/bin/env ruby&amp;lt;br&amp;gt;def func&amp;lt;br&amp;gt;x = 1337&amp;lt;br&amp;gt;return binding&amp;lt;br&amp;gt;end&amp;lt;br&amp;gt;x = 10&amp;lt;br&amp;gt;binding = func()&amp;lt;br&amp;gt;puts''' &lt;br /&gt;
'''eval(&amp;quot;x&amp;quot;, binding)&amp;lt;br&amp;gt;puts eval(&amp;quot;x&amp;quot;)&amp;lt;br&amp;gt;bind1 = func(1337)&amp;lt;br&amp;gt;'''&lt;br /&gt;
'''bind2 = func(404)&amp;lt;br&amp;gt;puts eval(&amp;quot;number&amp;quot;, bind1)&amp;lt;br&amp;gt;puts eval(&amp;quot;number&amp;quot;, bind2)'''&lt;br /&gt;
&lt;br /&gt;
This program works just as you would expect. Two invocations of the func method, and two numbers printed out. Bindings are an interesting feature, but just like eval, their use is controversial. While they can be useful at times, there's often a better way to achieve the same result.&lt;br /&gt;
&lt;br /&gt;
== PHP ==&lt;br /&gt;
&lt;br /&gt;
In PHP, eval executes code in a string almost exactly as if it had been put in the file instead of the call to eval(). The only exception is that errors are reported as coming from a call to eval(), and return statements become the result of the function.&lt;br /&gt;
Unlike some languages, the argument to eval must be a string of one or more complete statements, not just expressions; however, one can get the &amp;quot;expression&amp;quot; form of eval by putting the expression in a return statement, which causes eval to return the result of that expression.&amp;lt;br&amp;gt; &lt;br /&gt;
'''eval prototype''' : mixed eval ( string $code )&lt;br /&gt;
The code in eval doesn't have to be wrapped in PHP tags ( &amp;lt;?php ?&amp;gt; ). If there is parse error in the argument to eval then it returns false. eval can return a value if the last statement in the argument passed to eval has a return statement then eval will return a value which can be stored in a string.&amp;lt;br&amp;gt;&lt;br /&gt;
'''&amp;lt;?php&amp;lt;br&amp;gt;$string = 'cup';&amp;lt;br&amp;gt;$name = 'coffee';&amp;lt;br&amp;gt;$str = 'This is a $string with my $name in it.';'''&lt;br /&gt;
'''&amp;lt;br&amp;gt;echo $str. &amp;quot;\n&amp;quot;;&amp;lt;br&amp;gt;eval(&amp;quot;\$str = \&amp;quot;$str\&amp;quot;;&amp;quot;);&amp;lt;br&amp;gt;echo $str. &amp;quot;\n&amp;quot;;&amp;lt;br&amp;gt;?&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
The above code will output&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;quot;This is a $string with my $name in it.&amp;lt;br&amp;gt;&lt;br /&gt;
This is a cup with my coffee in it.&amp;quot;&amp;lt;br&amp;gt;&lt;br /&gt;
Unlike perl if there is a fatal error in the code passed to eval the whole script exits.&lt;br /&gt;
&lt;br /&gt;
= References =&lt;br /&gt;
http://ruby.about.com/od/advancedruby/a/Eval-Running-Code-On-The-Fly.htm&amp;lt;br&amp;gt;&lt;br /&gt;
http://php.net/manual/en/function.eval.php&amp;lt;br&amp;gt;&lt;br /&gt;
http://www.ruby-doc.org/core-1.9.3/Kernel.html&amp;lt;br&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Eval#PHP&amp;lt;br&amp;gt;&lt;br /&gt;
http://img.com.com/i/tr/bldr/pub/evalcalc.html?eq=&amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Vgupta6</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w28_mv&amp;diff=65150</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w28 mv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w28_mv&amp;diff=65150"/>
		<updated>2012-09-15T04:02:31Z</updated>

		<summary type="html">&lt;p&gt;Vgupta6: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= eval =&lt;br /&gt;
Eval function in most of the languages will either be a valid string representing expression in the language or some languages may allow blocks of valid code. The input is executed by the interpreter or compiler in same manner as other code in the language.&lt;br /&gt;
&lt;br /&gt;
= Advantage of eval =&lt;br /&gt;
eval is useful for writing metaprogamming code in most of the languages. This is a great asset for statically typed languages where most of the operatioons are performed at the compile time. Consider the following case:&lt;br /&gt;
&lt;br /&gt;
Problem statement:&lt;br /&gt;
Write a script to accept directories and pattern as input and send the files matching the pattern to a different server using sftp.&lt;br /&gt;
&lt;br /&gt;
We can maintain a string to store all the filenames to be moved to a directory on destination server preceded with appropriate sftp commands and use eval method to execute those commands in batch mode.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= Disadvantages of eval =&lt;br /&gt;
=== Slow in Execution ===&lt;br /&gt;
The code executed by eval runs slower, thus , whenever possible alternative techniques should be used.&lt;br /&gt;
&lt;br /&gt;
=== Security Risks ===&lt;br /&gt;
[[File:Security-risk.png|thumb|Border &amp;quot;border&amp;quot;|Location|Alignment|Size 0.5|link=http://expertiza.csc.ncsu.edu/wiki/index.php/File:Security-risk.png|alt=Alt|Caption]]&lt;br /&gt;
&lt;br /&gt;
'''JavaScript eval()'''&lt;br /&gt;
&amp;lt;br&amp;gt;Because most JSON-formatted text is also syntactically legal JavaScript code, an easy way for a JavaScript program to parse JSON-formatted data is to use the built-in JavaScript eval() function, which was designed to evaluate JavaScript expressions. Rather than using a JSON-specific parser, the JavaScript interpreter itself is used to execute the JSON data to produce native JavaScript objects. However, there are some Unicode characters that are valid in JSON strings but invalid in JavaScript, so additional escaping would be needed before using a JavaScript interpreter.&lt;br /&gt;
Unless precautions are taken to validate the data first, the eval technique is subject to security vulnerabilities if the data and the entire JavaScript environment is not within the control of a single trusted source. For example, if the data is itself not trusted, it may be subject to malicious JavaScript code injection attacks. Also, such breaches of trust may create vulnerabilities for data theft, authentication forgery, and other potential misuse of data and resources. Regular expressions can be used to validate the data prior to invoking eval(). Consider the following contrived example:&lt;br /&gt;
[[File:Problem-eval.png|thumb|Border &amp;quot;border&amp;quot;|Location|Alignment|Size 0.5|link=http://expertiza.csc.ncsu.edu/wiki/index.php/File:Problem-eval.png|alt=Alt|Caption]]&lt;br /&gt;
Problem-eval.png&lt;br /&gt;
'''&amp;lt;html&amp;gt;&amp;lt;br&amp;gt;&amp;lt;head&amp;gt;&amp;lt;br&amp;gt;&amp;lt;/head&amp;gt;&amp;lt;br&amp;gt;&amp;lt;body&amp;gt;&amp;lt;br&amp;gt;&amp;lt;script type=&amp;quot;text/javascript&amp;quot; lang=&amp;quot;javascript&amp;quot;&amp;gt;'''&lt;br /&gt;
'''alert(&amp;quot;some problem&amp;quot;);&amp;lt;br&amp;gt;var evalstring=&amp;quot;obj={ name:'username',address: '238 Milton , st. Mary , WT 87382'}; alert('This is''' '''malicious code');&amp;quot;;&amp;lt;br&amp;gt;eval(evalstring);&amp;lt;br&amp;gt;&amp;lt;/script&amp;gt;&amp;lt;br&amp;gt;&amp;lt;/body&amp;gt;&amp;lt;br&amp;gt;&amp;lt;/head&amp;gt;&amp;lt;br&amp;gt;&amp;lt;/html&amp;gt; &amp;lt;br&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
Let's assume that the client side script is expecting a JSON object from the server. However, along with the JSON object it can also send other valid javascript statement. If the above code is run in the browser, after the execution of eval there will be an alert message saying 'This is malicious code'. The alert statement represents a placeholder for any malacious javascript code. This can be avoided by using the safe Javascript library at JSON.org which contains the JSON.parse method.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= eval in Different Languages =&lt;br /&gt;
&lt;br /&gt;
== Perl ==&lt;br /&gt;
The general form of [[Perl (programming language)|Perl]] expects an expression or a block of code as an expression. The expression passed as an argument is parsed and executed as small Perl program. If there are no errors in the expression , which is parsed , then it is executed within the lexical context of the current Perl program. &lt;br /&gt;
&lt;br /&gt;
=== Error Handling with eval ===&lt;br /&gt;
Eval can be used to trap the errors. During execution of the routines , the program might die due to errors and interrupt the code. However , using eval , the code can still be running if the errors are handled inside the eval without being exited. We can evaluate the functionality of eval from the given code.&amp;lt;br&amp;gt;&lt;br /&gt;
''' print &amp;quot;enter number 1 \n&amp;quot;; &amp;lt;br&amp;gt;$number1 = &amp;lt;STDIN&amp;gt;; &amp;lt;br&amp;gt;chomp $number1; &amp;lt;br&amp;gt;print &amp;quot;enter number 2 \n&amp;quot;; &amp;lt;br&amp;gt;$number2 = &amp;lt;STDIN&amp;gt;; ''' '''&amp;lt;br&amp;gt;chomp $number2; &amp;lt;br&amp;gt;print &amp;quot;division output is &amp;quot;; &amp;lt;br&amp;gt;$x =eval {$result = $number1/$number2}; &amp;lt;br&amp;gt;print &amp;quot;$x\n$@&amp;quot;; &amp;lt;br&amp;gt;$sum =''' '''$number1 + $number2; &amp;lt;br&amp;gt;print &amp;quot;sum is $sum \n&amp;quot;; &amp;lt;br&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
The above given code prompts the user to give 2 inputs and then performs the division and addition of the numbers. If the second number entered by the user is 0 , then in that case code exits showing the error message and it does not calculate the addition. However , we can handle this exception using eval. Eval handles the exception, shows the appropriate error message and then return the control back to the program without exiting it. Hence , even if the second input is 0 , the addition is performed by the eval method.&lt;br /&gt;
&lt;br /&gt;
== Ruby ==&lt;br /&gt;
Most non-compiled languages have some kind of eval function, which will take a string and execute it as code. The eval method in Ruby accomplishes this, and is rather straightforward. The prototype of eval&lt;br /&gt;
eval(string [, binding [, filename [,lineno]]]) → obj.&lt;br /&gt;
Consider the following example code:&amp;lt;br&amp;gt;&lt;br /&gt;
''' #!/usr/bin/env ruby&amp;lt;br&amp;gt;code = &amp;quot;Time.now&amp;quot;&amp;lt;br&amp;gt;result = eval(code)&amp;lt;br&amp;gt;puts result'''&amp;lt;br&amp;gt;&lt;br /&gt;
The method call Time.now is stored in a string. It's not executed, compiled or even looked at (other than determining that it's a string literal) by the Ruby interpreter. It's just a string, anything could be in there. However, once you call the eval method, that all changes. The Ruby interpreter parses this string as if it were Ruby code and runs it. It will return whatever the code evaluates to.&lt;br /&gt;
&lt;br /&gt;
Simple enough, but why would anyone want to use this? There's a lot of debate whether eval should be used, and it's often considered bad form and hackish. There are also some pretty big security implications in using eval at all in your code, so it's not something that should be used lightly. There is one great use for eval though: developer consoles. Adding a way for programmers to issue Ruby commands to your program directly by using eval can be a powerful debugging tool and quite easy to implement.&lt;br /&gt;
&lt;br /&gt;
''' Using bindings in eval: '''&lt;br /&gt;
You can use eval to run code from a string, but things are not as simple as they seem. The eval method executes code, but where in the program is that code executed? The context in which code is executed is called a binding. Code is bound to a context of instance variable, local variables, and global variables. If you were to reference the variable x, it's up to the binding to decide which variable named x you're referring to. Examine the following code.&amp;lt;br&amp;gt;&lt;br /&gt;
'''#!/usr/bin/env ruby&amp;lt;br&amp;gt;def func&amp;lt;br&amp;gt;x = 1337&amp;lt;br&amp;gt;return binding&amp;lt;br&amp;gt;end&amp;lt;br&amp;gt;x = 10&amp;lt;br&amp;gt;binding = func()&amp;lt;br&amp;gt;puts''' &lt;br /&gt;
'''eval(&amp;quot;x&amp;quot;, binding)&amp;lt;br&amp;gt;puts eval(&amp;quot;x&amp;quot;)&amp;lt;br&amp;gt;bind1 = func(1337)&amp;lt;br&amp;gt;'''&lt;br /&gt;
'''bind2 = func(404)&amp;lt;br&amp;gt;puts eval(&amp;quot;number&amp;quot;, bind1)&amp;lt;br&amp;gt;puts eval(&amp;quot;number&amp;quot;, bind2)'''&lt;br /&gt;
&lt;br /&gt;
This program works just as you would expect. Two invocations of the func method, and two numbers printed out. Bindings are an interesting feature, but just like eval, their use is controversial. While they can be useful at times, there's often a better way to achieve the same result.&lt;br /&gt;
&lt;br /&gt;
== PHP ==&lt;br /&gt;
&lt;br /&gt;
In PHP, eval executes code in a string almost exactly as if it had been put in the file instead of the call to eval(). The only exception is that errors are reported as coming from a call to eval(), and return statements become the result of the function.&lt;br /&gt;
Unlike some languages, the argument to eval must be a string of one or more complete statements, not just expressions; however, one can get the &amp;quot;expression&amp;quot; form of eval by putting the expression in a return statement, which causes eval to return the result of that expression.&amp;lt;br&amp;gt; &lt;br /&gt;
'''eval prototype''' : mixed eval ( string $code )&lt;br /&gt;
The code in eval doesn't have to be wrapped in PHP tags ( &amp;lt;?php ?&amp;gt; ). If there is parse error in the argument to eval then it returns false. eval can return a value if the last statement in the argument passed to eval has a return statement then eval will return a value which can be stored in a string.&amp;lt;br&amp;gt;&lt;br /&gt;
'''&amp;lt;?php&amp;lt;br&amp;gt;$string = 'cup';&amp;lt;br&amp;gt;$name = 'coffee';&amp;lt;br&amp;gt;$str = 'This is a $string with my $name in it.';'''&lt;br /&gt;
'''&amp;lt;br&amp;gt;echo $str. &amp;quot;\n&amp;quot;;&amp;lt;br&amp;gt;eval(&amp;quot;\$str = \&amp;quot;$str\&amp;quot;;&amp;quot;);&amp;lt;br&amp;gt;echo $str. &amp;quot;\n&amp;quot;;&amp;lt;br&amp;gt;?&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
The above code will output&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;quot;This is a $string with my $name in it.&amp;lt;br&amp;gt;&lt;br /&gt;
This is a cup with my coffee in it.&amp;quot;&amp;lt;br&amp;gt;&lt;br /&gt;
Unlike perl if there is a fatal error in the code passed to eval the whole script exits.&lt;br /&gt;
&lt;br /&gt;
= References =&lt;br /&gt;
http://ruby.about.com/od/advancedruby/a/Eval-Running-Code-On-The-Fly.htm&amp;lt;br&amp;gt;&lt;br /&gt;
http://php.net/manual/en/function.eval.php&amp;lt;br&amp;gt;&lt;br /&gt;
http://www.ruby-doc.org/core-1.9.3/Kernel.html&amp;lt;br&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Eval#PHP&amp;lt;br&amp;gt;&lt;br /&gt;
http://img.com.com/i/tr/bldr/pub/evalcalc.html?eq=&amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Vgupta6</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w28_mv&amp;diff=65149</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w28 mv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w28_mv&amp;diff=65149"/>
		<updated>2012-09-15T04:00:47Z</updated>

		<summary type="html">&lt;p&gt;Vgupta6: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= eval =&lt;br /&gt;
Eval function in most of the languages will either be a valid string representing expression in the language or some languages may allow blocks of valid code. The input is executed by the interpreter or compiler in same manner as other code in the language.&lt;br /&gt;
&lt;br /&gt;
= Advantage of eval =&lt;br /&gt;
eval is useful for writing metaprogamming code in most of the languages. This is a great asset for statically typed languages where most of the operatioons are performed at the compile time. Consider the following case:&lt;br /&gt;
&lt;br /&gt;
Problem statement:&lt;br /&gt;
Write a script to accept directories and pattern as input and send the files matching the pattern to a different server using sftp.&lt;br /&gt;
&lt;br /&gt;
We can maintain a string to store all the filenames to be moved to a directory on destination server preceded with appropriate sftp commands and use eval method to execute those commands in batch mode.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= Disadvantages of eval =&lt;br /&gt;
=== Slow in Execution ===&lt;br /&gt;
The code executed by eval runs slower, thus , whenever possible alternative techniques should be used.&lt;br /&gt;
&lt;br /&gt;
=== Security Risks ===&lt;br /&gt;
[[File:Security-risk.png|thumb|Border &amp;quot;border&amp;quot;|Location|Alignment|Size 0.5|link=http://expertiza.csc.ncsu.edu/wiki/index.php/File:Security-risk.png|alt=Alt|Caption]]&lt;br /&gt;
&lt;br /&gt;
'''JavaScript eval()'''&lt;br /&gt;
&amp;lt;br&amp;gt;Because most JSON-formatted text is also syntactically legal JavaScript code, an easy way for a JavaScript program to parse JSON-formatted data is to use the built-in JavaScript eval() function, which was designed to evaluate JavaScript expressions. Rather than using a JSON-specific parser, the JavaScript interpreter itself is used to execute the JSON data to produce native JavaScript objects. However, there are some Unicode characters that are valid in JSON strings but invalid in JavaScript, so additional escaping would be needed before using a JavaScript interpreter.&lt;br /&gt;
Unless precautions are taken to validate the data first, the eval technique is subject to security vulnerabilities if the data and the entire JavaScript environment is not within the control of a single trusted source. For example, if the data is itself not trusted, it may be subject to malicious JavaScript code injection attacks. Also, such breaches of trust may create vulnerabilities for data theft, authentication forgery, and other potential misuse of data and resources. Regular expressions can be used to validate the data prior to invoking eval(). Consider the following contrived example:&lt;br /&gt;
[[File:Problem-eval.png|thumb|Border &amp;quot;border&amp;quot;|Location|Alignment|Size 0.5|link=http://expertiza.csc.ncsu.edu/wiki/index.php/File:Security-risk.png|alt=Alt|Caption]]&lt;br /&gt;
Problem-eval.png&lt;br /&gt;
'''&amp;lt;html&amp;gt;&amp;lt;br&amp;gt;&amp;lt;head&amp;gt;&amp;lt;br&amp;gt;&amp;lt;/head&amp;gt;&amp;lt;br&amp;gt;&amp;lt;body&amp;gt;&amp;lt;br&amp;gt;&amp;lt;script type=&amp;quot;text/javascript&amp;quot; lang=&amp;quot;javascript&amp;quot;&amp;gt;'''&lt;br /&gt;
'''alert(&amp;quot;some problem&amp;quot;);&amp;lt;br&amp;gt;var evalstring=&amp;quot;obj={ name:'username',address: '238 Milton , st. Mary , WT 87382'}; alert('This is''' '''malicious code');&amp;quot;;&amp;lt;br&amp;gt;eval(evalstring);&amp;lt;br&amp;gt;&amp;lt;/script&amp;gt;&amp;lt;br&amp;gt;&amp;lt;/body&amp;gt;&amp;lt;br&amp;gt;&amp;lt;/head&amp;gt;&amp;lt;br&amp;gt;&amp;lt;/html&amp;gt; &amp;lt;br&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
Let's assume that the client side script is expecting a JSON object from the server. However, along with the JSON object it can also send other valid javascript statement. If the above code is run in the browser, after the execution of eval there will be an alert message saying 'This is malicious code'. The alert statement represents a placeholder for any malacious javascript code. This can be avoided by using the safe Javascript library at JSON.org which contains the JSON.parse method.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= eval in Different Languages =&lt;br /&gt;
&lt;br /&gt;
== Perl ==&lt;br /&gt;
The general form of [[Perl (programming language)|Perl]] expects an expression or a block of code as an expression. The expression passed as an argument is parsed and executed as small Perl program. If there are no errors in the expression , which is parsed , then it is executed within the lexical context of the current Perl program. &lt;br /&gt;
&lt;br /&gt;
=== Error Handling with eval ===&lt;br /&gt;
Eval can be used to trap the errors. During execution of the routines , the program might die due to errors and interrupt the code. However , using eval , the code can still be running if the errors are handled inside the eval without being exited. We can evaluate the functionality of eval from the given code.&amp;lt;br&amp;gt;&lt;br /&gt;
''' print &amp;quot;enter number 1 \n&amp;quot;; &amp;lt;br&amp;gt;$number1 = &amp;lt;STDIN&amp;gt;; &amp;lt;br&amp;gt;chomp $number1; &amp;lt;br&amp;gt;print &amp;quot;enter number 2 \n&amp;quot;; &amp;lt;br&amp;gt;$number2 = &amp;lt;STDIN&amp;gt;; ''' '''&amp;lt;br&amp;gt;chomp $number2; &amp;lt;br&amp;gt;print &amp;quot;division output is &amp;quot;; &amp;lt;br&amp;gt;$x =eval {$result = $number1/$number2}; &amp;lt;br&amp;gt;print &amp;quot;$x\n$@&amp;quot;; &amp;lt;br&amp;gt;$sum =''' '''$number1 + $number2; &amp;lt;br&amp;gt;print &amp;quot;sum is $sum \n&amp;quot;; &amp;lt;br&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
The above given code prompts the user to give 2 inputs and then performs the division and addition of the numbers. If the second number entered by the user is 0 , then in that case code exits showing the error message and it does not calculate the addition. However , we can handle this exception using eval. Eval handles the exception, shows the appropriate error message and then return the control back to the program without exiting it. Hence , even if the second input is 0 , the addition is performed by the eval method.&lt;br /&gt;
&lt;br /&gt;
== Ruby ==&lt;br /&gt;
Most non-compiled languages have some kind of eval function, which will take a string and execute it as code. The eval method in Ruby accomplishes this, and is rather straightforward. The prototype of eval&lt;br /&gt;
eval(string [, binding [, filename [,lineno]]]) → obj.&lt;br /&gt;
Consider the following example code:&amp;lt;br&amp;gt;&lt;br /&gt;
''' #!/usr/bin/env ruby&amp;lt;br&amp;gt;code = &amp;quot;Time.now&amp;quot;&amp;lt;br&amp;gt;result = eval(code)&amp;lt;br&amp;gt;puts result'''&amp;lt;br&amp;gt;&lt;br /&gt;
The method call Time.now is stored in a string. It's not executed, compiled or even looked at (other than determining that it's a string literal) by the Ruby interpreter. It's just a string, anything could be in there. However, once you call the eval method, that all changes. The Ruby interpreter parses this string as if it were Ruby code and runs it. It will return whatever the code evaluates to.&lt;br /&gt;
&lt;br /&gt;
Simple enough, but why would anyone want to use this? There's a lot of debate whether eval should be used, and it's often considered bad form and hackish. There are also some pretty big security implications in using eval at all in your code, so it's not something that should be used lightly. There is one great use for eval though: developer consoles. Adding a way for programmers to issue Ruby commands to your program directly by using eval can be a powerful debugging tool and quite easy to implement.&lt;br /&gt;
&lt;br /&gt;
''' Using bindings in eval: '''&lt;br /&gt;
You can use eval to run code from a string, but things are not as simple as they seem. The eval method executes code, but where in the program is that code executed? The context in which code is executed is called a binding. Code is bound to a context of instance variable, local variables, and global variables. If you were to reference the variable x, it's up to the binding to decide which variable named x you're referring to. Examine the following code.&amp;lt;br&amp;gt;&lt;br /&gt;
'''#!/usr/bin/env ruby&amp;lt;br&amp;gt;def func&amp;lt;br&amp;gt;x = 1337&amp;lt;br&amp;gt;return binding&amp;lt;br&amp;gt;end&amp;lt;br&amp;gt;x = 10&amp;lt;br&amp;gt;binding = func()&amp;lt;br&amp;gt;puts''' &lt;br /&gt;
'''eval(&amp;quot;x&amp;quot;, binding)&amp;lt;br&amp;gt;puts eval(&amp;quot;x&amp;quot;)&amp;lt;br&amp;gt;bind1 = func(1337)&amp;lt;br&amp;gt;'''&lt;br /&gt;
'''bind2 = func(404)&amp;lt;br&amp;gt;puts eval(&amp;quot;number&amp;quot;, bind1)&amp;lt;br&amp;gt;puts eval(&amp;quot;number&amp;quot;, bind2)'''&lt;br /&gt;
&lt;br /&gt;
This program works just as you would expect. Two invocations of the func method, and two numbers printed out. Bindings are an interesting feature, but just like eval, their use is controversial. While they can be useful at times, there's often a better way to achieve the same result.&lt;br /&gt;
&lt;br /&gt;
== PHP ==&lt;br /&gt;
&lt;br /&gt;
In PHP, eval executes code in a string almost exactly as if it had been put in the file instead of the call to eval(). The only exception is that errors are reported as coming from a call to eval(), and return statements become the result of the function.&lt;br /&gt;
Unlike some languages, the argument to eval must be a string of one or more complete statements, not just expressions; however, one can get the &amp;quot;expression&amp;quot; form of eval by putting the expression in a return statement, which causes eval to return the result of that expression.&amp;lt;br&amp;gt; &lt;br /&gt;
'''eval prototype''' : mixed eval ( string $code )&lt;br /&gt;
The code in eval doesn't have to be wrapped in PHP tags ( &amp;lt;?php ?&amp;gt; ). If there is parse error in the argument to eval then it returns false. eval can return a value if the last statement in the argument passed to eval has a return statement then eval will return a value which can be stored in a string.&amp;lt;br&amp;gt;&lt;br /&gt;
'''&amp;lt;?php&amp;lt;br&amp;gt;$string = 'cup';&amp;lt;br&amp;gt;$name = 'coffee';&amp;lt;br&amp;gt;$str = 'This is a $string with my $name in it.';'''&lt;br /&gt;
'''&amp;lt;br&amp;gt;echo $str. &amp;quot;\n&amp;quot;;&amp;lt;br&amp;gt;eval(&amp;quot;\$str = \&amp;quot;$str\&amp;quot;;&amp;quot;);&amp;lt;br&amp;gt;echo $str. &amp;quot;\n&amp;quot;;&amp;lt;br&amp;gt;?&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
The above code will output&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;quot;This is a $string with my $name in it.&amp;lt;br&amp;gt;&lt;br /&gt;
This is a cup with my coffee in it.&amp;quot;&amp;lt;br&amp;gt;&lt;br /&gt;
Unlike perl if there is a fatal error in the code passed to eval the whole script exits.&lt;br /&gt;
&lt;br /&gt;
= References =&lt;br /&gt;
http://ruby.about.com/od/advancedruby/a/Eval-Running-Code-On-The-Fly.htm&amp;lt;br&amp;gt;&lt;br /&gt;
http://php.net/manual/en/function.eval.php&amp;lt;br&amp;gt;&lt;br /&gt;
http://www.ruby-doc.org/core-1.9.3/Kernel.html&amp;lt;br&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Eval#PHP&amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Vgupta6</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w28_mv&amp;diff=65148</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w28 mv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w28_mv&amp;diff=65148"/>
		<updated>2012-09-15T03:59:28Z</updated>

		<summary type="html">&lt;p&gt;Vgupta6: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= eval =&lt;br /&gt;
Eval function in most of the languages will either be a valid string representing expression in the language or some languages may allow blocks of valid code. The input is executed by the interpreter or compiler in same manner as other code in the language.&lt;br /&gt;
&lt;br /&gt;
= Advantage of eval =&lt;br /&gt;
eval is useful for writing metaprogamming code in most of the languages. This is a great asset for statically typed languages where most of the operatioons are performed at the compile time. Consider the following case:&lt;br /&gt;
&lt;br /&gt;
Problem statement:&lt;br /&gt;
Write a script to accept directories and pattern as input and send the files matching the pattern to a different server using sftp.&lt;br /&gt;
&lt;br /&gt;
We can maintain a string to store all the filenames to be moved to a directory on destination server preceded with appropriate sftp commands and use eval method to execute those commands in batch mode.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= Disadvantages of eval =&lt;br /&gt;
=== Slow in Execution ===&lt;br /&gt;
The code executed by eval runs slower, thus , whenever possible alternative techniques should be used.&lt;br /&gt;
&lt;br /&gt;
=== Security Risks ===&lt;br /&gt;
[[File:Security-risk.png|thumb|Border &amp;quot;border&amp;quot;|Location|Alignment|Size 0.5|link=http://expertiza.csc.ncsu.edu/wiki/index.php/File:Security-risk.png|alt=Alt|Caption]]&lt;br /&gt;
&lt;br /&gt;
'''JavaScript eval()'''&lt;br /&gt;
&amp;lt;br&amp;gt;Because most JSON-formatted text is also syntactically legal JavaScript code, an easy way for a JavaScript program to parse JSON-formatted data is to use the built-in JavaScript eval() function, which was designed to evaluate JavaScript expressions. Rather than using a JSON-specific parser, the JavaScript interpreter itself is used to execute the JSON data to produce native JavaScript objects. However, there are some Unicode characters that are valid in JSON strings but invalid in JavaScript, so additional escaping would be needed before using a JavaScript interpreter.&lt;br /&gt;
Unless precautions are taken to validate the data first, the eval technique is subject to security vulnerabilities if the data and the entire JavaScript environment is not within the control of a single trusted source. For example, if the data is itself not trusted, it may be subject to malicious JavaScript code injection attacks. Also, such breaches of trust may create vulnerabilities for data theft, authentication forgery, and other potential misuse of data and resources. Regular expressions can be used to validate the data prior to invoking eval(). Consider the following contrived example:&lt;br /&gt;
[[File:Problem-eval.png|thumb|Border &amp;quot;border&amp;quot;|Location|Alignment|Size 0.5|link=http://expertiza.csc.ncsu.edu/wiki/index.php/File:Security-risk.png|alt=Alt|Caption]]&lt;br /&gt;
Problem-eval.png&lt;br /&gt;
'''&amp;lt;html&amp;gt;&amp;lt;br&amp;gt;&amp;lt;head&amp;gt;&amp;lt;br&amp;gt;&amp;lt;/head&amp;gt;&amp;lt;br&amp;gt;&amp;lt;body&amp;gt;&amp;lt;br&amp;gt;&amp;lt;script type=&amp;quot;text/javascript&amp;quot; lang=&amp;quot;javascript&amp;quot;&amp;gt;'''&lt;br /&gt;
'''alert(&amp;quot;some problem&amp;quot;);&amp;lt;br&amp;gt;var evalstring=&amp;quot;obj={ name:'username',address: '238 Milton , st. Mary , WT 87382'}; alert('This is''' '''malicious code');&amp;quot;;&amp;lt;br&amp;gt;eval(evalstring);&amp;lt;br&amp;gt;&amp;lt;/script&amp;gt;&amp;lt;br&amp;gt;&amp;lt;/body&amp;gt;&amp;lt;br&amp;gt;&amp;lt;/head&amp;gt;&amp;lt;br&amp;gt;&amp;lt;/html&amp;gt; &amp;lt;br&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
Let's assume that the client side script is expecting a JSON object from the server. However, along with the JSON object it can also send other valid javascript statement. If the above code is run in the browser, after the execution of eval there will be an alert message saying 'This is malicious code'. The alert statement represents a placeholder for any malacious javascript code. This can be avoided by using the safe Javascript library at JSON.org which contains the JSON.parse method.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= eval in Different Languages =&lt;br /&gt;
&lt;br /&gt;
== Perl ==&lt;br /&gt;
The general form of [[Perl (programming language)|Perl]] expects an expression or a block of code as an expression. The expression passed as an argument is parsed and executed as small Perl program. If there are no errors in the expression , which is parsed , then it is executed within the lexical context of the current Perl program. &lt;br /&gt;
&lt;br /&gt;
=== Error Handling with eval ===&lt;br /&gt;
Eval can be used to trap the errors. During execution of the routines , the program might die due to errors and interrupt the code. However , using eval , the code can still be running if the errors are handled inside the eval without being exited. We can evaluate the functionality of eval from the given code.&amp;lt;br&amp;gt;&lt;br /&gt;
''' print &amp;quot;enter number 1 \n&amp;quot;; &amp;lt;br&amp;gt;$number1 = &amp;lt;STDIN&amp;gt;; &amp;lt;br&amp;gt;chomp $number1; &amp;lt;br&amp;gt;print &amp;quot;enter number 2 \n&amp;quot;; &amp;lt;br&amp;gt;$number2 = &amp;lt;STDIN&amp;gt;; ''' '''&amp;lt;br&amp;gt;chomp $number2; &amp;lt;br&amp;gt;print &amp;quot;division output is &amp;quot;; &amp;lt;br&amp;gt;$x =eval {$result = $number1/$number2}; &amp;lt;br&amp;gt;print &amp;quot;$x\n$@&amp;quot;; &amp;lt;br&amp;gt;$sum =''' '''$number1 + $number2; &amp;lt;br&amp;gt;print &amp;quot;sum is $sum \n&amp;quot;; &amp;lt;br&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
The above given code prompts the user to give 2 inputs and then performs the division and addition of the numbers. If the second number entered by the user is 0 , then in that case code exits showing the error message and it does not calculate the addition. However , we can handle this exception using eval. Eval handles the exception, shows the appropriate error message and then return the control back to the program without exiting it. Hence , even if the second input is 0 , the addition is performed by the eval method.&lt;br /&gt;
&lt;br /&gt;
== Ruby ==&lt;br /&gt;
Most non-compiled languages have some kind of eval function, which will take a string and execute it as code. The eval method in Ruby accomplishes this, and is rather straightforward. The prototype of eval&lt;br /&gt;
eval(string [, binding [, filename [,lineno]]]) → obj.&lt;br /&gt;
Consider the following example code:&amp;lt;br&amp;gt;&lt;br /&gt;
''' #!/usr/bin/env ruby&amp;lt;br&amp;gt;code = &amp;quot;Time.now&amp;quot;&amp;lt;br&amp;gt;result = eval(code)&amp;lt;br&amp;gt;puts result'''&amp;lt;br&amp;gt;&lt;br /&gt;
The method call Time.now is stored in a string. It's not executed, compiled or even looked at (other than determining that it's a string literal) by the Ruby interpreter. It's just a string, anything could be in there. However, once you call the eval method, that all changes. The Ruby interpreter parses this string as if it were Ruby code and runs it. It will return whatever the code evaluates to.&lt;br /&gt;
&lt;br /&gt;
Simple enough, but why would anyone want to use this? There's a lot of debate whether eval should be used, and it's often considered bad form and hackish. There are also some pretty big security implications in using eval at all in your code, so it's not something that should be used lightly. There is one great use for eval though: developer consoles. Adding a way for programmers to issue Ruby commands to your program directly by using eval can be a powerful debugging tool and quite easy to implement.&lt;br /&gt;
&lt;br /&gt;
''' Using bindings in eval: '''&lt;br /&gt;
You can use eval to run code from a string, but things are not as simple as they seem. The eval method executes code, but where in the program is that code executed? The context in which code is executed is called a binding. Code is bound to a context of instance variable, local variables, and global variables. If you were to reference the variable x, it's up to the binding to decide which variable named x you're referring to. Examine the following code.&amp;lt;br&amp;gt;&lt;br /&gt;
'''#!/usr/bin/env ruby&amp;lt;br&amp;gt;def func&amp;lt;br&amp;gt;x = 1337&amp;lt;br&amp;gt;return binding&amp;lt;br&amp;gt;end&amp;lt;br&amp;gt;x = 10&amp;lt;br&amp;gt;binding = func()&amp;lt;br&amp;gt;puts''' &lt;br /&gt;
'''eval(&amp;quot;x&amp;quot;, binding)&amp;lt;br&amp;gt;puts eval(&amp;quot;x&amp;quot;)&amp;lt;br&amp;gt;bind1 = func(1337)&amp;lt;br&amp;gt;'''&lt;br /&gt;
'''bind2 = func(404)&amp;lt;br&amp;gt;puts eval(&amp;quot;number&amp;quot;, bind1)&amp;lt;br&amp;gt;puts eval(&amp;quot;number&amp;quot;, bind2)'''&lt;br /&gt;
&lt;br /&gt;
This program works just as you would expect. Two invocations of the func method, and two numbers printed out. Bindings are an interesting feature, but just like eval, their use is controversial. While they can be useful at times, there's often a better way to achieve the same result.&lt;br /&gt;
&lt;br /&gt;
== PHP ==&lt;br /&gt;
&lt;br /&gt;
In PHP, eval executes code in a string almost exactly as if it had been put in the file instead of the call to eval(). The only exception is that errors are reported as coming from a call to eval(), and return statements become the result of the function.&lt;br /&gt;
Unlike some languages, the argument to eval must be a string of one or more complete statements, not just expressions; however, one can get the &amp;quot;expression&amp;quot; form of eval by putting the expression in a return statement, which causes eval to return the result of that expression. &lt;br /&gt;
eval prototype : mixed eval ( string $code )&lt;br /&gt;
The code in eval doesn't have to be wrapped in PHP tags ( &amp;lt;?php ?&amp;gt; ). If there is parse error in the argument to eval then it returns false. eval can return a value if the last statement in the argument passed to eval has a return statement then eval will return a value which can be stored in a string.&amp;lt;br&amp;gt;&lt;br /&gt;
'''&amp;lt;?php&amp;lt;br&amp;gt;$string = 'cup';&amp;lt;br&amp;gt;$name = 'coffee';&amp;lt;br&amp;gt;$str = 'This is a $string with my $name in it.';'''&lt;br /&gt;
'''&amp;lt;br&amp;gt;echo $str. &amp;quot;\n&amp;quot;;&amp;lt;br&amp;gt;eval(&amp;quot;\$str = \&amp;quot;$str\&amp;quot;;&amp;quot;);&amp;lt;br&amp;gt;echo $str. &amp;quot;\n&amp;quot;;&amp;lt;br&amp;gt;?&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
The above code will output&lt;br /&gt;
&amp;quot;This is a $string with my $name in it.&lt;br /&gt;
This is a cup with my coffee in it.&amp;quot;&lt;br /&gt;
Unlike perl if there is a fatal error in the code passed to eval the whole script exits.&lt;br /&gt;
&lt;br /&gt;
= References =&lt;br /&gt;
http://ruby.about.com/od/advancedruby/a/Eval-Running-Code-On-The-Fly.htm&amp;lt;br&amp;gt;&lt;br /&gt;
http://php.net/manual/en/function.eval.php&amp;lt;br&amp;gt;&lt;br /&gt;
http://www.ruby-doc.org/core-1.9.3/Kernel.html&amp;lt;br&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Eval#PHP&amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Vgupta6</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w28_mv&amp;diff=65147</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w28 mv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w28_mv&amp;diff=65147"/>
		<updated>2012-09-15T03:58:58Z</updated>

		<summary type="html">&lt;p&gt;Vgupta6: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= eval =&lt;br /&gt;
Eval function in most of the languages will either be a valid string representing expression in the language or some languages may allow blocks of valid code. The input is executed by the interpreter or compiler in same manner as other code in the language.&lt;br /&gt;
&lt;br /&gt;
= Advantage of eval =&lt;br /&gt;
eval is useful for writing metaprogamming code in most of the languages. This is a great asset for statically typed languages where most of the operatioons are performed at the compile time. Consider the following case:&lt;br /&gt;
&lt;br /&gt;
Problem statement:&lt;br /&gt;
Write a script to accept directories and pattern as input and send the files matching the pattern to a different server using sftp.&lt;br /&gt;
&lt;br /&gt;
We can maintain a string to store all the filenames to be moved to a directory on destination server preceded with appropriate sftp commands and use eval method to execute those commands in batch mode.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= Disadvantages of eval =&lt;br /&gt;
=== Slow in Execution ===&lt;br /&gt;
The code executed by eval runs slower, thus , whenever possible alternative techniques should be used.&lt;br /&gt;
&lt;br /&gt;
=== Security Risks ===&lt;br /&gt;
[[File:Security-risk.png|thumb|Border &amp;quot;border&amp;quot;|Location|Alignment|Size 0.5|link=http://expertiza.csc.ncsu.edu/wiki/index.php/File:Security-risk.png|alt=Alt|Caption]]&lt;br /&gt;
&lt;br /&gt;
'''JavaScript eval()'''&lt;br /&gt;
&amp;lt;br&amp;gt;Because most JSON-formatted text is also syntactically legal JavaScript code, an easy way for a JavaScript program to parse JSON-formatted data is to use the built-in JavaScript eval() function, which was designed to evaluate JavaScript expressions. Rather than using a JSON-specific parser, the JavaScript interpreter itself is used to execute the JSON data to produce native JavaScript objects. However, there are some Unicode characters that are valid in JSON strings but invalid in JavaScript, so additional escaping would be needed before using a JavaScript interpreter.&lt;br /&gt;
Unless precautions are taken to validate the data first, the eval technique is subject to security vulnerabilities if the data and the entire JavaScript environment is not within the control of a single trusted source. For example, if the data is itself not trusted, it may be subject to malicious JavaScript code injection attacks. Also, such breaches of trust may create vulnerabilities for data theft, authentication forgery, and other potential misuse of data and resources. Regular expressions can be used to validate the data prior to invoking eval(). Consider the following contrived example:&lt;br /&gt;
[[File:Problem-eval.png|thumb|Border &amp;quot;border&amp;quot;|Location|Alignment|Size 0.5|link=http://expertiza.csc.ncsu.edu/wiki/index.php/File:Security-risk.png|alt=Alt|Caption]]&lt;br /&gt;
Problem-eval.png&lt;br /&gt;
'''&amp;lt;html&amp;gt;&amp;lt;br&amp;gt;&amp;lt;head&amp;gt;&amp;lt;br&amp;gt;&amp;lt;/head&amp;gt;&amp;lt;br&amp;gt;&amp;lt;body&amp;gt;&amp;lt;br&amp;gt;&amp;lt;script type=&amp;quot;text/javascript&amp;quot; lang=&amp;quot;javascript&amp;quot;&amp;gt;'''&lt;br /&gt;
'''alert(&amp;quot;some problem&amp;quot;);&amp;lt;br&amp;gt;var evalstring=&amp;quot;obj={ name:'username',address: '238 Milton , st. Mary , WT 87382'}; alert('This is''' '''malicious code');&amp;quot;;&amp;lt;br&amp;gt;eval(evalstring);&amp;lt;br&amp;gt;&amp;lt;/script&amp;gt;&amp;lt;br&amp;gt;&amp;lt;/body&amp;gt;&amp;lt;br&amp;gt;&amp;lt;/head&amp;gt;&amp;lt;br&amp;gt;&amp;lt;/html&amp;gt; &amp;lt;br&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
Let's assume that the client side script is expecting a JSON object from the server. However, along with the JSON object it can also send other valid javascript statement. If the above code is run in the browser, after the execution of eval there will be an alert message saying 'This is malicious code'. The alert statement represents a placeholder for any malacious javascript code. This can be avoided by using the safe Javascript library at JSON.org which contains the JSON.parse method.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= eval in Different Languages =&lt;br /&gt;
&lt;br /&gt;
== Perl ==&lt;br /&gt;
The general form of [[Perl (programming language)|Perl]] expects an expression or a block of code as an expression. The expression passed as an argument is parsed and executed as small Perl program. If there are no errors in the expression , which is parsed , then it is executed within the lexical context of the current Perl program. &lt;br /&gt;
&lt;br /&gt;
=== Error Handling with eval ===&lt;br /&gt;
Eval can be used to trap the errors. During execution of the routines , the program might die due to errors and interrupt the code. However , using eval , the code can still be running if the errors are handled inside the eval without being exited. We can evaluate the functionality of eval from the given code.&amp;lt;br&amp;gt;&lt;br /&gt;
''' print &amp;quot;enter number 1 \n&amp;quot;; &amp;lt;br&amp;gt;$number1 = &amp;lt;STDIN&amp;gt;; &amp;lt;br&amp;gt;chomp $number1; &amp;lt;br&amp;gt;print &amp;quot;enter number 2 \n&amp;quot;; &amp;lt;br&amp;gt;$number2 = &amp;lt;STDIN&amp;gt;; ''' '''&amp;lt;br&amp;gt;chomp $number2; &amp;lt;br&amp;gt;print &amp;quot;division output is &amp;quot;; &amp;lt;br&amp;gt;$x =eval {$result = $number1/$number2}; &amp;lt;br&amp;gt;print &amp;quot;$x\n$@&amp;quot;; &amp;lt;br&amp;gt;$sum =''' '''$number1 + $number2; &amp;lt;br&amp;gt;print &amp;quot;sum is $sum \n&amp;quot;; &amp;lt;br&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
The above given code prompts the user to give 2 inputs and then performs the division and addition of the numbers. If the second number entered by the user is 0 , then in that case code exits showing the error message and it does not calculate the addition. However , we can handle this exception using eval. Eval handles the exception, shows the appropriate error message and then return the control back to the program without exiting it. Hence , even if the second input is 0 , the addition is performed by the eval method.&lt;br /&gt;
&lt;br /&gt;
== Ruby ==&lt;br /&gt;
Most non-compiled languages have some kind of eval function, which will take a string and execute it as code. The eval method in Ruby accomplishes this, and is rather straightforward. The prototype of eval&lt;br /&gt;
eval(string [, binding [, filename [,lineno]]]) → obj.&lt;br /&gt;
Consider the following example code:&amp;lt;br&amp;gt;&lt;br /&gt;
''' #!/usr/bin/env ruby&amp;lt;br&amp;gt;code = &amp;quot;Time.now&amp;quot;&amp;lt;br&amp;gt;result = eval(code)&amp;lt;br&amp;gt;puts result'''&lt;br /&gt;
The method call Time.now is stored in a string. It's not executed, compiled or even looked at (other than determining that it's a string literal) by the Ruby interpreter. It's just a string, anything could be in there. However, once you call the eval method, that all changes. The Ruby interpreter parses this string as if it were Ruby code and runs it. It will return whatever the code evaluates to.&lt;br /&gt;
&lt;br /&gt;
Simple enough, but why would anyone want to use this? There's a lot of debate whether eval should be used, and it's often considered bad form and hackish. There are also some pretty big security implications in using eval at all in your code, so it's not something that should be used lightly. There is one great use for eval though: developer consoles. Adding a way for programmers to issue Ruby commands to your program directly by using eval can be a powerful debugging tool and quite easy to implement.&lt;br /&gt;
&lt;br /&gt;
''' Using bindings in eval: '''&lt;br /&gt;
You can use eval to run code from a string, but things are not as simple as they seem. The eval method executes code, but where in the program is that code executed? The context in which code is executed is called a binding. Code is bound to a context of instance variable, local variables, and global variables. If you were to reference the variable x, it's up to the binding to decide which variable named x you're referring to. Examine the following code.&amp;lt;br&amp;gt;&lt;br /&gt;
'''#!/usr/bin/env ruby&amp;lt;br&amp;gt;def func&amp;lt;br&amp;gt;x = 1337&amp;lt;br&amp;gt;return binding&amp;lt;br&amp;gt;end&amp;lt;br&amp;gt;x = 10&amp;lt;br&amp;gt;binding = func()&amp;lt;br&amp;gt;puts''' &lt;br /&gt;
'''eval(&amp;quot;x&amp;quot;, binding)&amp;lt;br&amp;gt;puts eval(&amp;quot;x&amp;quot;)&amp;lt;br&amp;gt;bind1 = func(1337)&amp;lt;br&amp;gt;'''&lt;br /&gt;
'''bind2 = func(404)&amp;lt;br&amp;gt;puts eval(&amp;quot;number&amp;quot;, bind1)&amp;lt;br&amp;gt;puts eval(&amp;quot;number&amp;quot;, bind2)'''&lt;br /&gt;
&lt;br /&gt;
This program works just as you would expect. Two invocations of the func method, and two numbers printed out. Bindings are an interesting feature, but just like eval, their use is controversial. While they can be useful at times, there's often a better way to achieve the same result.&lt;br /&gt;
&lt;br /&gt;
== PHP ==&lt;br /&gt;
&lt;br /&gt;
In PHP, eval executes code in a string almost exactly as if it had been put in the file instead of the call to eval(). The only exception is that errors are reported as coming from a call to eval(), and return statements become the result of the function.&lt;br /&gt;
Unlike some languages, the argument to eval must be a string of one or more complete statements, not just expressions; however, one can get the &amp;quot;expression&amp;quot; form of eval by putting the expression in a return statement, which causes eval to return the result of that expression. &lt;br /&gt;
eval prototype : mixed eval ( string $code )&lt;br /&gt;
The code in eval doesn't have to be wrapped in PHP tags ( &amp;lt;?php ?&amp;gt; ). If there is parse error in the argument to eval then it returns false. eval can return a value if the last statement in the argument passed to eval has a return statement then eval will return a value which can be stored in a string.&amp;lt;br&amp;gt;&lt;br /&gt;
'''&amp;lt;?php&amp;lt;br&amp;gt;$string = 'cup';&amp;lt;br&amp;gt;$name = 'coffee';&amp;lt;br&amp;gt;$str = 'This is a $string with my $name in it.';'''&lt;br /&gt;
'''&amp;lt;br&amp;gt;echo $str. &amp;quot;\n&amp;quot;;&amp;lt;br&amp;gt;eval(&amp;quot;\$str = \&amp;quot;$str\&amp;quot;;&amp;quot;);&amp;lt;br&amp;gt;echo $str. &amp;quot;\n&amp;quot;;&amp;lt;br&amp;gt;?&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
The above code will output&lt;br /&gt;
&amp;quot;This is a $string with my $name in it.&lt;br /&gt;
This is a cup with my coffee in it.&amp;quot;&lt;br /&gt;
Unlike perl if there is a fatal error in the code passed to eval the whole script exits.&lt;br /&gt;
&lt;br /&gt;
= References =&lt;br /&gt;
http://ruby.about.com/od/advancedruby/a/Eval-Running-Code-On-The-Fly.htm&amp;lt;br&amp;gt;&lt;br /&gt;
http://php.net/manual/en/function.eval.php&amp;lt;br&amp;gt;&lt;br /&gt;
http://www.ruby-doc.org/core-1.9.3/Kernel.html&amp;lt;br&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Eval#PHP&amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Vgupta6</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w28_mv&amp;diff=65146</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w28 mv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w28_mv&amp;diff=65146"/>
		<updated>2012-09-15T03:57:59Z</updated>

		<summary type="html">&lt;p&gt;Vgupta6: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= eval =&lt;br /&gt;
Eval function in most of the languages will either be a valid string representing expression in the language or some languages may allow blocks of valid code. The input is executed by the interpreter or compiler in same manner as other code in the language.&lt;br /&gt;
&lt;br /&gt;
= Advantage of eval =&lt;br /&gt;
eval is useful for writing metaprogamming code in most of the languages. This is a great asset for statically typed languages where most of the operatioons are performed at the compile time. Consider the following case:&lt;br /&gt;
&lt;br /&gt;
Problem statement:&lt;br /&gt;
Write a script to accept directories and pattern as input and send the files matching the pattern to a different server using sftp.&lt;br /&gt;
&lt;br /&gt;
We can maintain a string to store all the filenames to be moved to a directory on destination server preceded with appropriate sftp commands and use eval method to execute those commands in batch mode.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= Disadvantages of eval =&lt;br /&gt;
=== Slow in Execution ===&lt;br /&gt;
The code executed by eval runs slower, thus , whenever possible alternative techniques should be used.&lt;br /&gt;
&lt;br /&gt;
=== Security Risks ===&lt;br /&gt;
[[File:Security-risk.png|thumb|Border &amp;quot;border&amp;quot;|Location|Alignment|Size 0.5|link=http://expertiza.csc.ncsu.edu/wiki/index.php/File:Security-risk.png|alt=Alt|Caption]]&lt;br /&gt;
&lt;br /&gt;
'''JavaScript eval()'''&lt;br /&gt;
&amp;lt;br&amp;gt;Because most JSON-formatted text is also syntactically legal JavaScript code, an easy way for a JavaScript program to parse JSON-formatted data is to use the built-in JavaScript eval() function, which was designed to evaluate JavaScript expressions. Rather than using a JSON-specific parser, the JavaScript interpreter itself is used to execute the JSON data to produce native JavaScript objects. However, there are some Unicode characters that are valid in JSON strings but invalid in JavaScript, so additional escaping would be needed before using a JavaScript interpreter.&lt;br /&gt;
Unless precautions are taken to validate the data first, the eval technique is subject to security vulnerabilities if the data and the entire JavaScript environment is not within the control of a single trusted source. For example, if the data is itself not trusted, it may be subject to malicious JavaScript code injection attacks. Also, such breaches of trust may create vulnerabilities for data theft, authentication forgery, and other potential misuse of data and resources. Regular expressions can be used to validate the data prior to invoking eval(). Consider the following contrived example:&lt;br /&gt;
[[File:Problem-eval.png|thumb|Border &amp;quot;border&amp;quot;|Location|Alignment|Size 0.5|link=http://expertiza.csc.ncsu.edu/wiki/index.php/File:Security-risk.png|alt=Alt|Caption]]&lt;br /&gt;
Problem-eval.png&lt;br /&gt;
'''&amp;lt;html&amp;gt;&amp;lt;br&amp;gt;&amp;lt;head&amp;gt;&amp;lt;br&amp;gt;&amp;lt;/head&amp;gt;&amp;lt;br&amp;gt;&amp;lt;body&amp;gt;&amp;lt;br&amp;gt;&amp;lt;script type=&amp;quot;text/javascript&amp;quot; lang=&amp;quot;javascript&amp;quot;&amp;gt;'''&lt;br /&gt;
'''alert(&amp;quot;some problem&amp;quot;);&amp;lt;br&amp;gt;var evalstring=&amp;quot;obj={ name:'username',address: '238 Milton , st. Mary , WT 87382'}; alert('This is''' '''malicious code');&amp;quot;;&amp;lt;br&amp;gt;eval(evalstring);&amp;lt;br&amp;gt;&amp;lt;/script&amp;gt;&amp;lt;br&amp;gt;&amp;lt;/body&amp;gt;&amp;lt;br&amp;gt;&amp;lt;/head&amp;gt;&amp;lt;br&amp;gt;&amp;lt;/html&amp;gt; &amp;lt;br&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
Let's assume that the client side script is expecting a JSON object from the server. However, along with the JSON object it can also send other valid javascript statement. If the above code is run in the browser, after the execution of eval there will be an alert message saying 'This is malicious code'. The alert statement represents a placeholder for any malacious javascript code. This can be avoided by using the safe Javascript library at JSON.org which contains the JSON.parse method.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= eval in Different Languages =&lt;br /&gt;
&lt;br /&gt;
== Perl ==&lt;br /&gt;
The general form of [[Perl (programming language)|Perl]] expects an expression or a block of code as an expression. The expression passed as an argument is parsed and executed as small Perl program. If there are no errors in the expression , which is parsed , then it is executed within the lexical context of the current Perl program. &lt;br /&gt;
&lt;br /&gt;
=== Error Handling with eval ===&lt;br /&gt;
Eval can be used to trap the errors. During execution of the routines , the program might die due to errors and interrupt the code. However , using eval , the code can still be running if the errors are handled inside the eval without being exited. We can evaluate the functionality of eval from the given code.&amp;lt;br&amp;gt;&lt;br /&gt;
''' print &amp;quot;enter number 1 \n&amp;quot;; &amp;lt;br&amp;gt;$number1 = &amp;lt;STDIN&amp;gt;; &amp;lt;br&amp;gt;chomp $number1; &amp;lt;br&amp;gt;print &amp;quot;enter number 2 \n&amp;quot;; &amp;lt;br&amp;gt;$number2 = &amp;lt;STDIN&amp;gt;; ''' '''&amp;lt;br&amp;gt;chomp $number2; &amp;lt;br&amp;gt;print &amp;quot;division output is &amp;quot;; &amp;lt;br&amp;gt;$x =eval {$result = $number1/$number2}; &amp;lt;br&amp;gt;print &amp;quot;$x\n$@&amp;quot;; &amp;lt;br&amp;gt;$sum =''' '''$number1 + $number2; &amp;lt;br&amp;gt;print &amp;quot;sum is $sum \n&amp;quot;; &amp;lt;br&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
The above given code prompts the user to give 2 inputs and then performs the division and addition of the numbers. If the second number entered by the user is 0 , then in that case code exits showing the error message and it does not calculate the addition. However , we can handle this exception using eval. Eval handles the exception, shows the appropriate error message and then return the controller back to the program without exiting it. Hence , even if the second input is 0 , the addition is performed by the eval method.&lt;br /&gt;
&lt;br /&gt;
== Ruby ==&lt;br /&gt;
Most non-compiled languages have some kind of eval function, which will take a string and execute it as code. The eval method in Ruby accomplishes this, and is rather straightforward. The prototype of eval&lt;br /&gt;
eval(string [, binding [, filename [,lineno]]]) → obj.&lt;br /&gt;
Consider the following example code:&amp;lt;br&amp;gt;&lt;br /&gt;
''' #!/usr/bin/env ruby&amp;lt;br&amp;gt;code = &amp;quot;Time.now&amp;quot;&amp;lt;br&amp;gt;result = eval(code)&amp;lt;br&amp;gt;puts result'''&lt;br /&gt;
The method call Time.now is stored in a string. It's not executed, compiled or even looked at (other than determining that it's a string literal) by the Ruby interpreter. It's just a string, anything could be in there. However, once you call the eval method, that all changes. The Ruby interpreter parses this string as if it were Ruby code and runs it. It will return whatever the code evaluates to.&lt;br /&gt;
&lt;br /&gt;
Simple enough, but why would anyone want to use this? There's a lot of debate whether eval should be used, and it's often considered bad form and hackish. There are also some pretty big security implications in using eval at all in your code, so it's not something that should be used lightly. There is one great use for eval though: developer consoles. Adding a way for programmers to issue Ruby commands to your program directly by using eval can be a powerful debugging tool and quite easy to implement.&lt;br /&gt;
&lt;br /&gt;
''' Using bindings in eval: '''&lt;br /&gt;
You can use eval to run code from a string, but things are not as simple as they seem. The eval method executes code, but where in the program is that code executed? The context in which code is executed is called a binding. Code is bound to a context of instance variable, local variables, and global variables. If you were to reference the variable x, it's up to the binding to decide which variable named x you're referring to. Examine the following code.&amp;lt;br&amp;gt;&lt;br /&gt;
'''#!/usr/bin/env ruby&amp;lt;br&amp;gt;def func&amp;lt;br&amp;gt;x = 1337&amp;lt;br&amp;gt;return binding&amp;lt;br&amp;gt;end&amp;lt;br&amp;gt;x = 10&amp;lt;br&amp;gt;binding = func()&amp;lt;br&amp;gt;puts''' &lt;br /&gt;
'''eval(&amp;quot;x&amp;quot;, binding)&amp;lt;br&amp;gt;puts eval(&amp;quot;x&amp;quot;)&amp;lt;br&amp;gt;bind1 = func(1337)&amp;lt;br&amp;gt;'''&lt;br /&gt;
'''bind2 = func(404)&amp;lt;br&amp;gt;puts eval(&amp;quot;number&amp;quot;, bind1)&amp;lt;br&amp;gt;puts eval(&amp;quot;number&amp;quot;, bind2)'''&lt;br /&gt;
&lt;br /&gt;
This program works just as you would expect. Two invocations of the func method, and two numbers printed out. Bindings are an interesting feature, but just like eval, their use is controversial. While they can be useful at times, there's often a better way to achieve the same result.&lt;br /&gt;
&lt;br /&gt;
== PHP ==&lt;br /&gt;
&lt;br /&gt;
In PHP, eval executes code in a string almost exactly as if it had been put in the file instead of the call to eval(). The only exception is that errors are reported as coming from a call to eval(), and return statements become the result of the function.&lt;br /&gt;
Unlike some languages, the argument to eval must be a string of one or more complete statements, not just expressions; however, one can get the &amp;quot;expression&amp;quot; form of eval by putting the expression in a return statement, which causes eval to return the result of that expression. &lt;br /&gt;
eval prototype : mixed eval ( string $code )&lt;br /&gt;
The code in eval doesn't have to be wrapped in PHP tags ( &amp;lt;?php ?&amp;gt; ). If there is parse error in the argument to eval then it returns false. eval can return a value if the last statement in the argument passed to eval has a return statement then eval will return a value which can be stored in a string.&amp;lt;br&amp;gt;&lt;br /&gt;
'''&amp;lt;?php&amp;lt;br&amp;gt;$string = 'cup';&amp;lt;br&amp;gt;$name = 'coffee';&amp;lt;br&amp;gt;$str = 'This is a $string with my $name in it.';'''&lt;br /&gt;
'''&amp;lt;br&amp;gt;echo $str. &amp;quot;\n&amp;quot;;&amp;lt;br&amp;gt;eval(&amp;quot;\$str = \&amp;quot;$str\&amp;quot;;&amp;quot;);&amp;lt;br&amp;gt;echo $str. &amp;quot;\n&amp;quot;;&amp;lt;br&amp;gt;?&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
The above code will output&lt;br /&gt;
&amp;quot;This is a $string with my $name in it.&lt;br /&gt;
This is a cup with my coffee in it.&amp;quot;&lt;br /&gt;
Unlike perl if there is a fatal error in the code passed to eval the whole script exits.&lt;br /&gt;
&lt;br /&gt;
= References =&lt;br /&gt;
http://ruby.about.com/od/advancedruby/a/Eval-Running-Code-On-The-Fly.htm&amp;lt;br&amp;gt;&lt;br /&gt;
http://php.net/manual/en/function.eval.php&amp;lt;br&amp;gt;&lt;br /&gt;
http://www.ruby-doc.org/core-1.9.3/Kernel.html&amp;lt;br&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Eval#PHP&amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Vgupta6</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w28_mv&amp;diff=65145</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w28 mv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w28_mv&amp;diff=65145"/>
		<updated>2012-09-15T03:56:24Z</updated>

		<summary type="html">&lt;p&gt;Vgupta6: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= eval =&lt;br /&gt;
Eval function in most of the languages will either be a valid string representing expression in the language or some languages may allow blocks of valid code. The input is executed by the interpreter or compiler in same manner as other code in the language.&lt;br /&gt;
&lt;br /&gt;
= Advantage of eval =&lt;br /&gt;
eval is useful for writing metaprogamming code in most of the languages. This is a great asset for statically typed languages where most of the operatioons are performed at the compile time. Consider the following case:&lt;br /&gt;
&lt;br /&gt;
Problem statement:&lt;br /&gt;
Write a script to accept directories and pattern as input and send the files matching the pattern to a different server using sftp.&lt;br /&gt;
&lt;br /&gt;
We can maintain a string to store all the filenames to be moved to a directory on destination server preceded with appropriate sftp commands and use eval method to execute those commands in batch mode.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= Disadvantages of eval =&lt;br /&gt;
=== Slow in Execution ===&lt;br /&gt;
The code executed by eval runs slower, thus , whenever possible alternative techniques should be used.&lt;br /&gt;
&lt;br /&gt;
=== Security Risks ===&lt;br /&gt;
[[File:Security-risk.png|thumb|Border &amp;quot;border&amp;quot;|Location|Alignment|Size 0.5|link=http://expertiza.csc.ncsu.edu/wiki/index.php/File:Security-risk.png|alt=Alt|Caption]]&lt;br /&gt;
&lt;br /&gt;
'''JavaScript eval()'''&lt;br /&gt;
&amp;lt;br&amp;gt;Because most JSON-formatted text is also syntactically legal JavaScript code, an easy way for a JavaScript program to parse JSON-formatted data is to use the built-in JavaScript eval() function, which was designed to evaluate JavaScript expressions. Rather than using a JSON-specific parser, the JavaScript interpreter itself is used to execute the JSON data to produce native JavaScript objects. However, there are some Unicode characters that are valid in JSON strings but invalid in JavaScript, so additional escaping would be needed before using a JavaScript interpreter.&lt;br /&gt;
Unless precautions are taken to validate the data first, the eval technique is subject to security vulnerabilities if the data and the entire JavaScript environment is not within the control of a single trusted source. For example, if the data is itself not trusted, it may be subject to malicious JavaScript code injection attacks. Also, such breaches of trust may create vulnerabilities for data theft, authentication forgery, and other potential misuse of data and resources. Regular expressions can be used to validate the data prior to invoking eval(). Consider the following contrived example:&lt;br /&gt;
[[File:Problem-eval.png|thumb|Border &amp;quot;border&amp;quot;|Location|Alignment|Size 0.5|link=http://expertiza.csc.ncsu.edu/wiki/index.php/File:Security-risk.png|alt=Alt|Caption]]&lt;br /&gt;
Problem-eval.png&lt;br /&gt;
'''&amp;lt;html&amp;gt;&amp;lt;br&amp;gt;&amp;lt;head&amp;gt;&amp;lt;br&amp;gt;&amp;lt;/head&amp;gt;&amp;lt;br&amp;gt;&amp;lt;body&amp;gt;&amp;lt;br&amp;gt;&amp;lt;script type=&amp;quot;text/javascript&amp;quot; lang=&amp;quot;javascript&amp;quot;&amp;gt;'''&lt;br /&gt;
'''alert(&amp;quot;some problem&amp;quot;);&amp;lt;br&amp;gt;var evalstring=&amp;quot;obj={ name:'username',address: '238 Milton , st. Mary , WT 87382'}; alert('This is''' '''malicious code');&amp;quot;;&amp;lt;br&amp;gt;eval(evalstring);&amp;lt;br&amp;gt;&amp;lt;/script&amp;gt;&amp;lt;br&amp;gt;&amp;lt;/body&amp;gt;&amp;lt;br&amp;gt;&amp;lt;/head&amp;gt;&amp;lt;br&amp;gt;&amp;lt;/html&amp;gt; &amp;lt;br&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
Let's assume that the client side script is expecting a JSON object from the server. However, along with the JSON object it can also send other valid javascript statement. If the above code is run in the browser, after the execution of eval there will be an alert message saying 'This is malicious code'. The alert statement represents a placeholder for any malacious javascript code. This can be avoided by using the safe Javascript library at JSON.org which contains the JSON.parse method.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= eval in Different Languages =&lt;br /&gt;
&lt;br /&gt;
== Perl ==&lt;br /&gt;
The general form of [[Perl (programming language)|Perl]] expects an expression or a block of code as an expression. The expr passed as an argument is parsed and executed as small Perl program. If there are no errors in the expr , which is parsed , then it is executed within the lexical content of the current Perl program. &lt;br /&gt;
&lt;br /&gt;
=== Error Handling with eval ===&lt;br /&gt;
Eval can be used to trap the errors. During execution of the routines , the program might die due to errors and interrupts in the code. However , using eval , the code can still be running if the errors are handled inside the eval without being exited. We can evaluate the functionality of eval from the given code.&lt;br /&gt;
''' print &amp;quot;entern number 1 \n&amp;quot;; &amp;lt;br&amp;gt;$number1 = &amp;lt;STDIN&amp;gt;; &amp;lt;br&amp;gt;chomp $number1; &amp;lt;br&amp;gt;print &amp;quot;enter number 2 \n&amp;quot;; &amp;lt;br&amp;gt;$number2 = &amp;lt;STDIN&amp;gt;; ''' '''&amp;lt;br&amp;gt;chomp $number2; &amp;lt;br&amp;gt;print &amp;quot;division output is &amp;quot;; &amp;lt;br&amp;gt;$x =eval {$result = $number1/$number2}; &amp;lt;br&amp;gt;print &amp;quot;$x\n$@&amp;quot;; &amp;lt;br&amp;gt;$sum =''' '''$number1 + $number2; &amp;lt;br&amp;gt;print &amp;quot;sum is $sum \n&amp;quot;; &amp;lt;br&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
The above given code prompts the user to give 2 inputs and then performs the division and addition of the numbers. If the second number entered by the user is 0 , then in that case code exits showing the error message and it does not calculate the addition. However , we can handle this exception using eval. Eval handles the exception, shows the appropriate error message and then return the controller back to the program without exiting it. Hence , even if the second input is 0 , the addition is performed by the eval method.&lt;br /&gt;
&lt;br /&gt;
== Ruby ==&lt;br /&gt;
Most non-compiled languages have some kind of eval function, which will take a string and execute it as code. The eval method in Ruby accomplishes this, and is rather straightforward. The prototype of eval&lt;br /&gt;
eval(string [, binding [, filename [,lineno]]]) → obj.&lt;br /&gt;
Consider the following example code:&amp;lt;br&amp;gt;&lt;br /&gt;
''' #!/usr/bin/env ruby&amp;lt;br&amp;gt;code = &amp;quot;Time.now&amp;quot;&amp;lt;br&amp;gt;result = eval(code)&amp;lt;br&amp;gt;puts result'''&lt;br /&gt;
The method call Time.now is stored in a string. It's not executed, compiled or even looked at (other than determining that it's a string literal) by the Ruby interpreter. It's just a string, anything could be in there. However, once you call the eval method, that all changes. The Ruby interpreter parses this string as if it were Ruby code and runs it. It will return whatever the code evaluates to.&lt;br /&gt;
&lt;br /&gt;
Simple enough, but why would anyone want to use this? There's a lot of debate whether eval should be used, and it's often considered bad form and hackish. There are also some pretty big security implications in using eval at all in your code, so it's not something that should be used lightly. There is one great use for eval though: developer consoles. Adding a way for programmers to issue Ruby commands to your program directly by using eval can be a powerful debugging tool and quite easy to implement.&lt;br /&gt;
&lt;br /&gt;
''' Using bindings in eval: '''&lt;br /&gt;
You can use eval to run code from a string, but things are not as simple as they seem. The eval method executes code, but where in the program is that code executed? The context in which code is executed is called a binding. Code is bound to a context of instance variable, local variables, and global variables. If you were to reference the variable x, it's up to the binding to decide which variable named x you're referring to. Examine the following code.&amp;lt;br&amp;gt;&lt;br /&gt;
'''#!/usr/bin/env ruby&amp;lt;br&amp;gt;def func&amp;lt;br&amp;gt;x = 1337&amp;lt;br&amp;gt;return binding&amp;lt;br&amp;gt;end&amp;lt;br&amp;gt;x = 10&amp;lt;br&amp;gt;binding = func()&amp;lt;br&amp;gt;puts''' &lt;br /&gt;
'''eval(&amp;quot;x&amp;quot;, binding)&amp;lt;br&amp;gt;puts eval(&amp;quot;x&amp;quot;)&amp;lt;br&amp;gt;bind1 = func(1337)&amp;lt;br&amp;gt;'''&lt;br /&gt;
'''bind2 = func(404)&amp;lt;br&amp;gt;puts eval(&amp;quot;number&amp;quot;, bind1)&amp;lt;br&amp;gt;puts eval(&amp;quot;number&amp;quot;, bind2)'''&lt;br /&gt;
&lt;br /&gt;
This program works just as you would expect. Two invocations of the func method, and two numbers printed out. Bindings are an interesting feature, but just like eval, their use is controversial. While they can be useful at times, there's often a better way to achieve the same result.&lt;br /&gt;
&lt;br /&gt;
== PHP ==&lt;br /&gt;
&lt;br /&gt;
In PHP, eval executes code in a string almost exactly as if it had been put in the file instead of the call to eval(). The only exception is that errors are reported as coming from a call to eval(), and return statements become the result of the function.&lt;br /&gt;
Unlike some languages, the argument to eval must be a string of one or more complete statements, not just expressions; however, one can get the &amp;quot;expression&amp;quot; form of eval by putting the expression in a return statement, which causes eval to return the result of that expression. &lt;br /&gt;
eval prototype : mixed eval ( string $code )&lt;br /&gt;
The code in eval doesn't have to be wrapped in PHP tags ( &amp;lt;?php ?&amp;gt; ). If there is parse error in the argument to eval then it returns false. eval can return a value if the last statement in the argument passed to eval has a return statement then eval will return a value which can be stored in a string.&amp;lt;br&amp;gt;&lt;br /&gt;
'''&amp;lt;?php&amp;lt;br&amp;gt;$string = 'cup';&amp;lt;br&amp;gt;$name = 'coffee';&amp;lt;br&amp;gt;$str = 'This is a $string with my $name in it.';'''&lt;br /&gt;
'''&amp;lt;br&amp;gt;echo $str. &amp;quot;\n&amp;quot;;&amp;lt;br&amp;gt;eval(&amp;quot;\$str = \&amp;quot;$str\&amp;quot;;&amp;quot;);&amp;lt;br&amp;gt;echo $str. &amp;quot;\n&amp;quot;;&amp;lt;br&amp;gt;?&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
The above code will output&lt;br /&gt;
&amp;quot;This is a $string with my $name in it.&lt;br /&gt;
This is a cup with my coffee in it.&amp;quot;&lt;br /&gt;
Unlike perl if there is a fatal error in the code passed to eval the whole script exits.&lt;br /&gt;
&lt;br /&gt;
= References =&lt;br /&gt;
http://ruby.about.com/od/advancedruby/a/Eval-Running-Code-On-The-Fly.htm&amp;lt;br&amp;gt;&lt;br /&gt;
http://php.net/manual/en/function.eval.php&amp;lt;br&amp;gt;&lt;br /&gt;
http://www.ruby-doc.org/core-1.9.3/Kernel.html&amp;lt;br&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Eval#PHP&amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Vgupta6</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w28_mv&amp;diff=65144</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w28 mv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w28_mv&amp;diff=65144"/>
		<updated>2012-09-15T03:55:52Z</updated>

		<summary type="html">&lt;p&gt;Vgupta6: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= eval =&lt;br /&gt;
Eval function in most of the languages will either be a valid string representing expression in the language or some languages may allow blocks of valid code. The input is executed by the interpreter or compiler in same manner as other code in the language.&lt;br /&gt;
&lt;br /&gt;
= Advantage of eval =&lt;br /&gt;
eval is useful for writing metaprogamming code in most of the languages. This is a great asset for statically typed languages where most of the operatioons are performed at the compile time. Consider the following case:&lt;br /&gt;
&lt;br /&gt;
Problem statement:&lt;br /&gt;
Write a script to accept directories and pattern as input and send the files matching the pattern to a different server using sftp.&lt;br /&gt;
&lt;br /&gt;
We can maintain a string to store all the filenames to be moved to a directory on destination server preceded with appropriate sftp commands and use eval method to execute those commands in batch mode.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= Disadvantages of eval =&lt;br /&gt;
=== Slow in Execution ===&lt;br /&gt;
The code executed by eval runs slower, thus , whenever possible alternative techniques should be used.&lt;br /&gt;
&lt;br /&gt;
=== Security Risks ===&lt;br /&gt;
[[File:Security-risk.png|thumb|Border &amp;quot;border&amp;quot;|Location|Alignment|Size 0.5|link=http://expertiza.csc.ncsu.edu/wiki/index.php/File:Security-risk.png|alt=Alt|Caption]]&lt;br /&gt;
&lt;br /&gt;
'''JavaScript eval()'''&lt;br /&gt;
&amp;lt;br&amp;gt;Because most JSON-formatted text is also syntactically legal JavaScript code, an easy way for a JavaScript program to parse JSON-formatted data is to use the built-in JavaScript eval() function, which was designed to evaluate JavaScript expressions. Rather than using a JSON-specific parser, the JavaScript interpreter itself is used to execute the JSON data to produce native JavaScript objects. However, there are some Unicode characters that are valid in JSON strings but invalid in JavaScript, so additional escaping would be needed before using a JavaScript interpreter.&lt;br /&gt;
Unless precautions are taken to validate the data first, the eval technique is subject to security vulnerabilities if the data and the entire JavaScript environment is not within the control of a single trusted source. For example, if the data is itself not trusted, it may be subject to malicious JavaScript code injection attacks. Also, such breaches of trust may create vulnerabilities for data theft, authentication forgery, and other potential misuse of data and resources. Regular expressions can be used to validate the data prior to invoking eval(). Consider the following contrived example:&lt;br /&gt;
[[File:Problem-eval.png|thumb|Border &amp;quot;border&amp;quot;|Location|Alignment|Size 0.5|link=http://expertiza.csc.ncsu.edu/wiki/index.php/File:Security-risk.png|alt=Alt|Caption]]&lt;br /&gt;
Problem-eval.png&lt;br /&gt;
'''&amp;lt;html&amp;gt;&amp;lt;br&amp;gt;&amp;lt;head&amp;gt;&amp;lt;br&amp;gt;&amp;lt;/head&amp;gt;&amp;lt;br&amp;gt;&amp;lt;body&amp;gt;&amp;lt;br&amp;gt;&amp;lt;script type=&amp;quot;text/javascript&amp;quot; lang=&amp;quot;javascript&amp;quot;&amp;gt;'''&lt;br /&gt;
'''alert(&amp;quot;some problem&amp;quot;);&amp;lt;br&amp;gt;var evalstring=&amp;quot;obj={ name:'username',address: '238 Milton , st. Mary , WT 87382'}; alert('This is''' '''malicious code');&amp;quot;;&amp;lt;br&amp;gt;eval(evalstring);&amp;lt;br&amp;gt;&amp;lt;/script&amp;gt;&amp;lt;br&amp;gt;&amp;lt;/body&amp;gt;&amp;lt;br&amp;gt;&amp;lt;/head&amp;gt;&amp;lt;br&amp;gt;&amp;lt;/html&amp;gt; &amp;lt;br&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
Let's assume that the client side script is expecting a JSON object from the server. However, along with the JSON object it can also send other valid javascript statement. If the above code is run in the browser, after the execution of eval there will be an alert message saying 'This is malicious code'. The alert statement represents a placeholder for any potential javascript code. This can be avoided by using the safe Javascript library at JSON.org which contains the JSON.parse method.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= eval in Different Languages =&lt;br /&gt;
&lt;br /&gt;
== Perl ==&lt;br /&gt;
The general form of [[Perl (programming language)|Perl]] expects an expression or a block of code as an expression. The expr passed as an argument is parsed and executed as small Perl program. If there are no errors in the expr , which is parsed , then it is executed within the lexical content of the current Perl program. &lt;br /&gt;
&lt;br /&gt;
=== Error Handling with eval ===&lt;br /&gt;
Eval can be used to trap the errors. During execution of the routines , the program might die due to errors and interrupts in the code. However , using eval , the code can still be running if the errors are handled inside the eval without being exited. We can evaluate the functionality of eval from the given code.&lt;br /&gt;
''' print &amp;quot;entern number 1 \n&amp;quot;; &amp;lt;br&amp;gt;$number1 = &amp;lt;STDIN&amp;gt;; &amp;lt;br&amp;gt;chomp $number1; &amp;lt;br&amp;gt;print &amp;quot;enter number 2 \n&amp;quot;; &amp;lt;br&amp;gt;$number2 = &amp;lt;STDIN&amp;gt;; ''' '''&amp;lt;br&amp;gt;chomp $number2; &amp;lt;br&amp;gt;print &amp;quot;division output is &amp;quot;; &amp;lt;br&amp;gt;$x =eval {$result = $number1/$number2}; &amp;lt;br&amp;gt;print &amp;quot;$x\n$@&amp;quot;; &amp;lt;br&amp;gt;$sum =''' '''$number1 + $number2; &amp;lt;br&amp;gt;print &amp;quot;sum is $sum \n&amp;quot;; &amp;lt;br&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
The above given code prompts the user to give 2 inputs and then performs the division and addition of the numbers. If the second number entered by the user is 0 , then in that case code exits showing the error message and it does not calculate the addition. However , we can handle this exception using eval. Eval handles the exception, shows the appropriate error message and then return the controller back to the program without exiting it. Hence , even if the second input is 0 , the addition is performed by the eval method.&lt;br /&gt;
&lt;br /&gt;
== Ruby ==&lt;br /&gt;
Most non-compiled languages have some kind of eval function, which will take a string and execute it as code. The eval method in Ruby accomplishes this, and is rather straightforward. The prototype of eval&lt;br /&gt;
eval(string [, binding [, filename [,lineno]]]) → obj.&lt;br /&gt;
Consider the following example code:&amp;lt;br&amp;gt;&lt;br /&gt;
''' #!/usr/bin/env ruby&amp;lt;br&amp;gt;code = &amp;quot;Time.now&amp;quot;&amp;lt;br&amp;gt;result = eval(code)&amp;lt;br&amp;gt;puts result'''&lt;br /&gt;
The method call Time.now is stored in a string. It's not executed, compiled or even looked at (other than determining that it's a string literal) by the Ruby interpreter. It's just a string, anything could be in there. However, once you call the eval method, that all changes. The Ruby interpreter parses this string as if it were Ruby code and runs it. It will return whatever the code evaluates to.&lt;br /&gt;
&lt;br /&gt;
Simple enough, but why would anyone want to use this? There's a lot of debate whether eval should be used, and it's often considered bad form and hackish. There are also some pretty big security implications in using eval at all in your code, so it's not something that should be used lightly. There is one great use for eval though: developer consoles. Adding a way for programmers to issue Ruby commands to your program directly by using eval can be a powerful debugging tool and quite easy to implement.&lt;br /&gt;
&lt;br /&gt;
''' Using bindings in eval: '''&lt;br /&gt;
You can use eval to run code from a string, but things are not as simple as they seem. The eval method executes code, but where in the program is that code executed? The context in which code is executed is called a binding. Code is bound to a context of instance variable, local variables, and global variables. If you were to reference the variable x, it's up to the binding to decide which variable named x you're referring to. Examine the following code.&amp;lt;br&amp;gt;&lt;br /&gt;
'''#!/usr/bin/env ruby&amp;lt;br&amp;gt;def func&amp;lt;br&amp;gt;x = 1337&amp;lt;br&amp;gt;return binding&amp;lt;br&amp;gt;end&amp;lt;br&amp;gt;x = 10&amp;lt;br&amp;gt;binding = func()&amp;lt;br&amp;gt;puts''' &lt;br /&gt;
'''eval(&amp;quot;x&amp;quot;, binding)&amp;lt;br&amp;gt;puts eval(&amp;quot;x&amp;quot;)&amp;lt;br&amp;gt;bind1 = func(1337)&amp;lt;br&amp;gt;'''&lt;br /&gt;
'''bind2 = func(404)&amp;lt;br&amp;gt;puts eval(&amp;quot;number&amp;quot;, bind1)&amp;lt;br&amp;gt;puts eval(&amp;quot;number&amp;quot;, bind2)'''&lt;br /&gt;
&lt;br /&gt;
This program works just as you would expect. Two invocations of the func method, and two numbers printed out. Bindings are an interesting feature, but just like eval, their use is controversial. While they can be useful at times, there's often a better way to achieve the same result.&lt;br /&gt;
&lt;br /&gt;
== PHP ==&lt;br /&gt;
&lt;br /&gt;
In PHP, eval executes code in a string almost exactly as if it had been put in the file instead of the call to eval(). The only exception is that errors are reported as coming from a call to eval(), and return statements become the result of the function.&lt;br /&gt;
Unlike some languages, the argument to eval must be a string of one or more complete statements, not just expressions; however, one can get the &amp;quot;expression&amp;quot; form of eval by putting the expression in a return statement, which causes eval to return the result of that expression. &lt;br /&gt;
eval prototype : mixed eval ( string $code )&lt;br /&gt;
The code in eval doesn't have to be wrapped in PHP tags ( &amp;lt;?php ?&amp;gt; ). If there is parse error in the argument to eval then it returns false. eval can return a value if the last statement in the argument passed to eval has a return statement then eval will return a value which can be stored in a string.&amp;lt;br&amp;gt;&lt;br /&gt;
'''&amp;lt;?php&amp;lt;br&amp;gt;$string = 'cup';&amp;lt;br&amp;gt;$name = 'coffee';&amp;lt;br&amp;gt;$str = 'This is a $string with my $name in it.';'''&lt;br /&gt;
'''&amp;lt;br&amp;gt;echo $str. &amp;quot;\n&amp;quot;;&amp;lt;br&amp;gt;eval(&amp;quot;\$str = \&amp;quot;$str\&amp;quot;;&amp;quot;);&amp;lt;br&amp;gt;echo $str. &amp;quot;\n&amp;quot;;&amp;lt;br&amp;gt;?&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
The above code will output&lt;br /&gt;
&amp;quot;This is a $string with my $name in it.&lt;br /&gt;
This is a cup with my coffee in it.&amp;quot;&lt;br /&gt;
Unlike perl if there is a fatal error in the code passed to eval the whole script exits.&lt;br /&gt;
&lt;br /&gt;
= References =&lt;br /&gt;
http://ruby.about.com/od/advancedruby/a/Eval-Running-Code-On-The-Fly.htm&amp;lt;br&amp;gt;&lt;br /&gt;
http://php.net/manual/en/function.eval.php&amp;lt;br&amp;gt;&lt;br /&gt;
http://www.ruby-doc.org/core-1.9.3/Kernel.html&amp;lt;br&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Eval#PHP&amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Vgupta6</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w28_mv&amp;diff=65143</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w28 mv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w28_mv&amp;diff=65143"/>
		<updated>2012-09-15T03:55:03Z</updated>

		<summary type="html">&lt;p&gt;Vgupta6: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= eval =&lt;br /&gt;
Eval function in most of the languages will either be a valid string representing expression in the language or some languages may allow blocks of valid code. The input is executed by the interpreter or compiler in same manner as other code in the language.&lt;br /&gt;
&lt;br /&gt;
= Advantage of eval =&lt;br /&gt;
eval is useful for writing metaprogamming code in most of the languages. This is a great asset for statically typed languages where most of the operatioons are performed at the compile time. Consider the following case:&lt;br /&gt;
&lt;br /&gt;
Problem statement:&lt;br /&gt;
Write a script to accept directories and pattern as input and send the files matching the pattern to a different server using sftp.&lt;br /&gt;
&lt;br /&gt;
We can maintain a string to store all the filenames to be moved to a directory on destination server preceded with appropriate sftp commands and use eval method to execute those commands in batchmode.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= Disadvantages of eval =&lt;br /&gt;
=== Slow in Execution ===&lt;br /&gt;
The code executed by eval runs slower, thus , whenever possible different technique should be used.&lt;br /&gt;
&lt;br /&gt;
=== Security Risks ===&lt;br /&gt;
[[File:Security-risk.png|thumb|Border &amp;quot;border&amp;quot;|Location|Alignment|Size 0.5|link=http://expertiza.csc.ncsu.edu/wiki/index.php/File:Security-risk.png|alt=Alt|Caption]]&lt;br /&gt;
&lt;br /&gt;
'''JavaScript eval()'''&lt;br /&gt;
&amp;lt;br&amp;gt;Because most JSON-formatted text is also syntactically legal JavaScript code, an easy way for a JavaScript program to parse JSON-formatted data is to use the built-in JavaScript eval() function, which was designed to evaluate JavaScript expressions. Rather than using a JSON-specific parser, the JavaScript interpreter itself is used to execute the JSON data to produce native JavaScript objects. However, there are some Unicode characters that are valid in JSON strings but invalid in JavaScript, so additional escaping would be needed before using a JavaScript interpreter.[16]&lt;br /&gt;
Unless precautions are taken to validate the data first, the eval technique is subject to security vulnerabilities if the data and the entire JavaScript environment is not within the control of a single trusted source. For example, if the data is itself not trusted, it may be subject to malicious JavaScript code injection attacks. Also, such breaches of trust may create vulnerabilities for data theft, authentication forgery, and other potential misuse of data and resources. Regular expressions can be used to validate the data prior to invoking eval(). Consider the following contrived example:&lt;br /&gt;
[[File:Problem-eval.png|thumb|Border &amp;quot;border&amp;quot;|Location|Alignment|Size 0.5|link=http://expertiza.csc.ncsu.edu/wiki/index.php/File:Security-risk.png|alt=Alt|Caption]]&lt;br /&gt;
Problem-eval.png&lt;br /&gt;
'''&amp;lt;html&amp;gt;&amp;lt;br&amp;gt;&amp;lt;head&amp;gt;&amp;lt;br&amp;gt;&amp;lt;/head&amp;gt;&amp;lt;br&amp;gt;&amp;lt;body&amp;gt;&amp;lt;br&amp;gt;&amp;lt;script type=&amp;quot;text/javascript&amp;quot; lang=&amp;quot;javascript&amp;quot;&amp;gt;'''&lt;br /&gt;
'''alert(&amp;quot;some problem&amp;quot;);&amp;lt;br&amp;gt;var evalstring=&amp;quot;obj={ name:'username',address: '238 Milton , st. Mary , WT 87382'}; alert('This is''' '''malicious code');&amp;quot;;&amp;lt;br&amp;gt;eval(evalstring);&amp;lt;br&amp;gt;&amp;lt;/script&amp;gt;&amp;lt;br&amp;gt;&amp;lt;/body&amp;gt;&amp;lt;br&amp;gt;&amp;lt;/head&amp;gt;&amp;lt;br&amp;gt;&amp;lt;/html&amp;gt; &amp;lt;br&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
Let's assume that the client side script is expecting a JSON object from the server. However, along with the JSON object it can also send other valid javascript statement. If the above code is run in the browser, after the execution of eval there will be an alert message saying 'This is malicious code'. The alert statement represents a placeholder for any potential javascript code. This can be avoided by using the safe Javascript library at JSON.org which contains the JSON.parse method.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= eval in Different Languages =&lt;br /&gt;
&lt;br /&gt;
== Perl ==&lt;br /&gt;
The general form of [[Perl (programming language)|Perl]] expects an expression or a block of code as an expression. The expr passed as an argument is parsed and executed as small Perl program. If there are no errors in the expr , which is parsed , then it is executed within the lexical content of the current Perl program. &lt;br /&gt;
&lt;br /&gt;
=== Error Handling with eval ===&lt;br /&gt;
Eval can be used to trap the errors. During execution of the routines , the program might die due to errors and interrupts in the code. However , using eval , the code can still be running if the errors are handled inside the eval without being exited. We can evaluate the functionality of eval from the given code.&lt;br /&gt;
''' print &amp;quot;entern number 1 \n&amp;quot;; &amp;lt;br&amp;gt;$number1 = &amp;lt;STDIN&amp;gt;; &amp;lt;br&amp;gt;chomp $number1; &amp;lt;br&amp;gt;print &amp;quot;enter number 2 \n&amp;quot;; &amp;lt;br&amp;gt;$number2 = &amp;lt;STDIN&amp;gt;; ''' '''&amp;lt;br&amp;gt;chomp $number2; &amp;lt;br&amp;gt;print &amp;quot;division output is &amp;quot;; &amp;lt;br&amp;gt;$x =eval {$result = $number1/$number2}; &amp;lt;br&amp;gt;print &amp;quot;$x\n$@&amp;quot;; &amp;lt;br&amp;gt;$sum =''' '''$number1 + $number2; &amp;lt;br&amp;gt;print &amp;quot;sum is $sum \n&amp;quot;; &amp;lt;br&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
The above given code prompts the user to give 2 inputs and then performs the division and addition of the numbers. If the second number entered by the user is 0 , then in that case code exits showing the error message and it does not calculate the addition. However , we can handle this exception using eval. Eval handles the exception, shows the appropriate error message and then return the controller back to the program without exiting it. Hence , even if the second input is 0 , the addition is performed by the eval method.&lt;br /&gt;
&lt;br /&gt;
== Ruby ==&lt;br /&gt;
Most non-compiled languages have some kind of eval function, which will take a string and execute it as code. The eval method in Ruby accomplishes this, and is rather straightforward. The prototype of eval&lt;br /&gt;
eval(string [, binding [, filename [,lineno]]]) → obj.&lt;br /&gt;
Consider the following example code:&amp;lt;br&amp;gt;&lt;br /&gt;
''' #!/usr/bin/env ruby&amp;lt;br&amp;gt;code = &amp;quot;Time.now&amp;quot;&amp;lt;br&amp;gt;result = eval(code)&amp;lt;br&amp;gt;puts result'''&lt;br /&gt;
The method call Time.now is stored in a string. It's not executed, compiled or even looked at (other than determining that it's a string literal) by the Ruby interpreter. It's just a string, anything could be in there. However, once you call the eval method, that all changes. The Ruby interpreter parses this string as if it were Ruby code and runs it. It will return whatever the code evaluates to.&lt;br /&gt;
&lt;br /&gt;
Simple enough, but why would anyone want to use this? There's a lot of debate whether eval should be used, and it's often considered bad form and hackish. There are also some pretty big security implications in using eval at all in your code, so it's not something that should be used lightly. There is one great use for eval though: developer consoles. Adding a way for programmers to issue Ruby commands to your program directly by using eval can be a powerful debugging tool and quite easy to implement.&lt;br /&gt;
&lt;br /&gt;
''' Using bindings in eval: '''&lt;br /&gt;
You can use eval to run code from a string, but things are not as simple as they seem. The eval method executes code, but where in the program is that code executed? The context in which code is executed is called a binding. Code is bound to a context of instance variable, local variables, and global variables. If you were to reference the variable x, it's up to the binding to decide which variable named x you're referring to. Examine the following code.&amp;lt;br&amp;gt;&lt;br /&gt;
'''#!/usr/bin/env ruby&amp;lt;br&amp;gt;def func&amp;lt;br&amp;gt;x = 1337&amp;lt;br&amp;gt;return binding&amp;lt;br&amp;gt;end&amp;lt;br&amp;gt;x = 10&amp;lt;br&amp;gt;binding = func()&amp;lt;br&amp;gt;puts''' &lt;br /&gt;
'''eval(&amp;quot;x&amp;quot;, binding)&amp;lt;br&amp;gt;puts eval(&amp;quot;x&amp;quot;)&amp;lt;br&amp;gt;bind1 = func(1337)&amp;lt;br&amp;gt;'''&lt;br /&gt;
'''bind2 = func(404)&amp;lt;br&amp;gt;puts eval(&amp;quot;number&amp;quot;, bind1)&amp;lt;br&amp;gt;puts eval(&amp;quot;number&amp;quot;, bind2)'''&lt;br /&gt;
&lt;br /&gt;
This program works just as you would expect. Two invocations of the func method, and two numbers printed out. Bindings are an interesting feature, but just like eval, their use is controversial. While they can be useful at times, there's often a better way to achieve the same result.&lt;br /&gt;
&lt;br /&gt;
== PHP ==&lt;br /&gt;
&lt;br /&gt;
In PHP, eval executes code in a string almost exactly as if it had been put in the file instead of the call to eval(). The only exception is that errors are reported as coming from a call to eval(), and return statements become the result of the function.&lt;br /&gt;
Unlike some languages, the argument to eval must be a string of one or more complete statements, not just expressions; however, one can get the &amp;quot;expression&amp;quot; form of eval by putting the expression in a return statement, which causes eval to return the result of that expression. &lt;br /&gt;
eval prototype : mixed eval ( string $code )&lt;br /&gt;
The code in eval doesn't have to be wrapped in PHP tags ( &amp;lt;?php ?&amp;gt; ). If there is parse error in the argument to eval then it returns false. eval can return a value if the last statement in the argument passed to eval has a return statement then eval will return a value which can be stored in a string.&amp;lt;br&amp;gt;&lt;br /&gt;
'''&amp;lt;?php&amp;lt;br&amp;gt;$string = 'cup';&amp;lt;br&amp;gt;$name = 'coffee';&amp;lt;br&amp;gt;$str = 'This is a $string with my $name in it.';'''&lt;br /&gt;
'''&amp;lt;br&amp;gt;echo $str. &amp;quot;\n&amp;quot;;&amp;lt;br&amp;gt;eval(&amp;quot;\$str = \&amp;quot;$str\&amp;quot;;&amp;quot;);&amp;lt;br&amp;gt;echo $str. &amp;quot;\n&amp;quot;;&amp;lt;br&amp;gt;?&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
The above code will output&lt;br /&gt;
&amp;quot;This is a $string with my $name in it.&lt;br /&gt;
This is a cup with my coffee in it.&amp;quot;&lt;br /&gt;
Unlike perl if there is a fatal error in the code passed to eval the whole script exits.&lt;br /&gt;
&lt;br /&gt;
= References =&lt;br /&gt;
http://ruby.about.com/od/advancedruby/a/Eval-Running-Code-On-The-Fly.htm&amp;lt;br&amp;gt;&lt;br /&gt;
http://php.net/manual/en/function.eval.php&amp;lt;br&amp;gt;&lt;br /&gt;
http://www.ruby-doc.org/core-1.9.3/Kernel.html&amp;lt;br&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Eval#PHP&amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Vgupta6</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=File:Problem-eval.png&amp;diff=65142</id>
		<title>File:Problem-eval.png</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=File:Problem-eval.png&amp;diff=65142"/>
		<updated>2012-09-15T03:51:27Z</updated>

		<summary type="html">&lt;p&gt;Vgupta6: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Vgupta6</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w28_mv&amp;diff=65141</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w28 mv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w28_mv&amp;diff=65141"/>
		<updated>2012-09-15T03:48:38Z</updated>

		<summary type="html">&lt;p&gt;Vgupta6: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= eval =&lt;br /&gt;
Eval function in most of the languages will either be a valid string representing expression in the language or some languages may allow blocks of valid code. The input is executed by the interpreter or compiler in same manner as other code in the language.&lt;br /&gt;
&lt;br /&gt;
= Advantage of eval =&lt;br /&gt;
eval is useful for writing metaprogamming code in most of the languages. This is a great asset for statically typed languages where most of the operatioons are performed at the compile time. Consider the following case:&lt;br /&gt;
&lt;br /&gt;
Problem statement:&lt;br /&gt;
Write a script to accept directories and pattern as input and send the files matching the pattern to a different server using sftp.&lt;br /&gt;
&lt;br /&gt;
We can maintain a string to store all the filenames to be moved to a directory on destination server preceded with appropriate sftp commands and use eval method to execute those commands in batchmode.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= Disadvantages of eval =&lt;br /&gt;
=== Slow in Execution ===&lt;br /&gt;
The code executed by eval runs slower, thus , whenever possible different technique should be used.&lt;br /&gt;
&lt;br /&gt;
=== Security Risks ===&lt;br /&gt;
[[File:Security-risk.png|thumb|Border &amp;quot;border&amp;quot;|Location|Alignment|Size 0.5|link=http://expertiza.csc.ncsu.edu/wiki/index.php/File:Security-risk.png|alt=Alt|Caption]]&lt;br /&gt;
&lt;br /&gt;
'''JavaScript eval()'''&lt;br /&gt;
&amp;lt;br&amp;gt;Because most JSON-formatted text is also syntactically legal JavaScript code, an easy way for a JavaScript program to parse JSON-formatted data is to use the built-in JavaScript eval() function, which was designed to evaluate JavaScript expressions. Rather than using a JSON-specific parser, the JavaScript interpreter itself is used to execute the JSON data to produce native JavaScript objects. However, there are some Unicode characters that are valid in JSON strings but invalid in JavaScript, so additional escaping would be needed before using a JavaScript interpreter.[16]&lt;br /&gt;
Unless precautions are taken to validate the data first, the eval technique is subject to security vulnerabilities if the data and the entire JavaScript environment is not within the control of a single trusted source. For example, if the data is itself not trusted, it may be subject to malicious JavaScript code injection attacks. Also, such breaches of trust may create vulnerabilities for data theft, authentication forgery, and other potential misuse of data and resources. Regular expressions can be used to validate the data prior to invoking eval(). Consider the following contrived example:&lt;br /&gt;
&lt;br /&gt;
'''&amp;lt;html&amp;gt;&amp;lt;br&amp;gt;&amp;lt;head&amp;gt;&amp;lt;br&amp;gt;&amp;lt;/head&amp;gt;&amp;lt;br&amp;gt;&amp;lt;body&amp;gt;&amp;lt;br&amp;gt;&amp;lt;script type=&amp;quot;text/javascript&amp;quot; lang=&amp;quot;javascript&amp;quot;&amp;gt;'''&lt;br /&gt;
'''alert(&amp;quot;some problem&amp;quot;);&amp;lt;br&amp;gt;var evalstring=&amp;quot;obj={ name:'username',address: '238 Milton , st. Mary , WT 87382'}; alert('This is''' '''malicious code');&amp;quot;;&amp;lt;br&amp;gt;eval(evalstring);&amp;lt;br&amp;gt;&amp;lt;/script&amp;gt;&amp;lt;br&amp;gt;&amp;lt;/body&amp;gt;&amp;lt;br&amp;gt;&amp;lt;/head&amp;gt;&amp;lt;br&amp;gt;&amp;lt;/html&amp;gt; &amp;lt;br&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
Let's assume that the client side script is expecting a JSON object from the server. However, along with the JSON object it can also send other valid javascript statement. If the above code is run in the browser, after the execution of eval there will be an alert message saying 'This is malicious code'. The alert statement represents a placeholder for any potential javascript code. This can be avoided by using the safe Javascript library at JSON.org which contains the JSON.parse method.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= eval in Different Languages =&lt;br /&gt;
&lt;br /&gt;
== Perl ==&lt;br /&gt;
The general form of [[Perl (programming language)|Perl]] expects an expression or a block of code as an expression. The expr passed as an argument is parsed and executed as small Perl program. If there are no errors in the expr , which is parsed , then it is executed within the lexical content of the current Perl program. &lt;br /&gt;
&lt;br /&gt;
=== Error Handling with eval ===&lt;br /&gt;
Eval can be used to trap the errors. During execution of the routines , the program might die due to errors and interrupts in the code. However , using eval , the code can still be running if the errors are handled inside the eval without being exited. We can evaluate the functionality of eval from the given code.&lt;br /&gt;
''' print &amp;quot;entern number 1 \n&amp;quot;; &amp;lt;br&amp;gt;$number1 = &amp;lt;STDIN&amp;gt;; &amp;lt;br&amp;gt;chomp $number1; &amp;lt;br&amp;gt;print &amp;quot;enter number 2 \n&amp;quot;; &amp;lt;br&amp;gt;$number2 = &amp;lt;STDIN&amp;gt;; ''' '''&amp;lt;br&amp;gt;chomp $number2; &amp;lt;br&amp;gt;print &amp;quot;division output is &amp;quot;; &amp;lt;br&amp;gt;$x =eval {$result = $number1/$number2}; &amp;lt;br&amp;gt;print &amp;quot;$x\n$@&amp;quot;; &amp;lt;br&amp;gt;$sum =''' '''$number1 + $number2; &amp;lt;br&amp;gt;print &amp;quot;sum is $sum \n&amp;quot;; &amp;lt;br&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
The above given code prompts the user to give 2 inputs and then performs the division and addition of the numbers. If the second number entered by the user is 0 , then in that case code exits showing the error message and it does not calculate the addition. However , we can handle this exception using eval. Eval handles the exception, shows the appropriate error message and then return the controller back to the program without exiting it. Hence , even if the second input is 0 , the addition is performed by the eval method.&lt;br /&gt;
&lt;br /&gt;
== Ruby ==&lt;br /&gt;
Most non-compiled languages have some kind of eval function, which will take a string and execute it as code. The eval method in Ruby accomplishes this, and is rather straightforward. The prototype of eval&lt;br /&gt;
eval(string [, binding [, filename [,lineno]]]) → obj.&lt;br /&gt;
Consider the following example code:&amp;lt;br&amp;gt;&lt;br /&gt;
''' #!/usr/bin/env ruby&amp;lt;br&amp;gt;code = &amp;quot;Time.now&amp;quot;&amp;lt;br&amp;gt;result = eval(code)&amp;lt;br&amp;gt;puts result'''&lt;br /&gt;
The method call Time.now is stored in a string. It's not executed, compiled or even looked at (other than determining that it's a string literal) by the Ruby interpreter. It's just a string, anything could be in there. However, once you call the eval method, that all changes. The Ruby interpreter parses this string as if it were Ruby code and runs it. It will return whatever the code evaluates to.&lt;br /&gt;
&lt;br /&gt;
Simple enough, but why would anyone want to use this? There's a lot of debate whether eval should be used, and it's often considered bad form and hackish. There are also some pretty big security implications in using eval at all in your code, so it's not something that should be used lightly. There is one great use for eval though: developer consoles. Adding a way for programmers to issue Ruby commands to your program directly by using eval can be a powerful debugging tool and quite easy to implement.&lt;br /&gt;
&lt;br /&gt;
''' Using bindings in eval: '''&lt;br /&gt;
You can use eval to run code from a string, but things are not as simple as they seem. The eval method executes code, but where in the program is that code executed? The context in which code is executed is called a binding. Code is bound to a context of instance variable, local variables, and global variables. If you were to reference the variable x, it's up to the binding to decide which variable named x you're referring to. Examine the following code.&amp;lt;br&amp;gt;&lt;br /&gt;
'''#!/usr/bin/env ruby&amp;lt;br&amp;gt;def func&amp;lt;br&amp;gt;x = 1337&amp;lt;br&amp;gt;return binding&amp;lt;br&amp;gt;end&amp;lt;br&amp;gt;x = 10&amp;lt;br&amp;gt;binding = func()&amp;lt;br&amp;gt;puts''' &lt;br /&gt;
'''eval(&amp;quot;x&amp;quot;, binding)&amp;lt;br&amp;gt;puts eval(&amp;quot;x&amp;quot;)&amp;lt;br&amp;gt;bind1 = func(1337)&amp;lt;br&amp;gt;'''&lt;br /&gt;
'''bind2 = func(404)&amp;lt;br&amp;gt;puts eval(&amp;quot;number&amp;quot;, bind1)&amp;lt;br&amp;gt;puts eval(&amp;quot;number&amp;quot;, bind2)'''&lt;br /&gt;
&lt;br /&gt;
This program works just as you would expect. Two invocations of the func method, and two numbers printed out. Bindings are an interesting feature, but just like eval, their use is controversial. While they can be useful at times, there's often a better way to achieve the same result.&lt;br /&gt;
&lt;br /&gt;
== PHP ==&lt;br /&gt;
&lt;br /&gt;
In PHP, eval executes code in a string almost exactly as if it had been put in the file instead of the call to eval(). The only exception is that errors are reported as coming from a call to eval(), and return statements become the result of the function.&lt;br /&gt;
Unlike some languages, the argument to eval must be a string of one or more complete statements, not just expressions; however, one can get the &amp;quot;expression&amp;quot; form of eval by putting the expression in a return statement, which causes eval to return the result of that expression. &lt;br /&gt;
eval prototype : mixed eval ( string $code )&lt;br /&gt;
The code in eval doesn't have to be wrapped in PHP tags ( &amp;lt;?php ?&amp;gt; ). If there is parse error in the argument to eval then it returns false. eval can return a value if the last statement in the argument passed to eval has a return statement then eval will return a value which can be stored in a string.&amp;lt;br&amp;gt;&lt;br /&gt;
'''&amp;lt;?php&amp;lt;br&amp;gt;$string = 'cup';&amp;lt;br&amp;gt;$name = 'coffee';&amp;lt;br&amp;gt;$str = 'This is a $string with my $name in it.';'''&lt;br /&gt;
'''&amp;lt;br&amp;gt;echo $str. &amp;quot;\n&amp;quot;;&amp;lt;br&amp;gt;eval(&amp;quot;\$str = \&amp;quot;$str\&amp;quot;;&amp;quot;);&amp;lt;br&amp;gt;echo $str. &amp;quot;\n&amp;quot;;&amp;lt;br&amp;gt;?&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
The above code will output&lt;br /&gt;
&amp;quot;This is a $string with my $name in it.&lt;br /&gt;
This is a cup with my coffee in it.&amp;quot;&lt;br /&gt;
Unlike perl if there is a fatal error in the code passed to eval the whole script exits.&lt;br /&gt;
&lt;br /&gt;
= References =&lt;br /&gt;
http://ruby.about.com/od/advancedruby/a/Eval-Running-Code-On-The-Fly.htm&amp;lt;br&amp;gt;&lt;br /&gt;
http://php.net/manual/en/function.eval.php&amp;lt;br&amp;gt;&lt;br /&gt;
http://www.ruby-doc.org/core-1.9.3/Kernel.html&amp;lt;br&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Eval#PHP&amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Vgupta6</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=File:Security-risk.png&amp;diff=65140</id>
		<title>File:Security-risk.png</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=File:Security-risk.png&amp;diff=65140"/>
		<updated>2012-09-15T03:40:45Z</updated>

		<summary type="html">&lt;p&gt;Vgupta6: If the argument to eval is malicious code then it can have undesirable effects.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;If the argument to eval is malicious code then it can have undesirable effects.&lt;/div&gt;</summary>
		<author><name>Vgupta6</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w28_mv&amp;diff=65139</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w28 mv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w28_mv&amp;diff=65139"/>
		<updated>2012-09-15T03:29:29Z</updated>

		<summary type="html">&lt;p&gt;Vgupta6: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= eval =&lt;br /&gt;
Eval function in most of the languages will either be a valid string representing expression in the language or some languages may allow blocks of valid code. The input is executed by the interpreter or compiler in same manner as other code in the language.&lt;br /&gt;
&lt;br /&gt;
= Advantage of eval =&lt;br /&gt;
eval is useful for writing metaprogamming code in most of the languages. This is a great asset for statically typed languages where most of the operatioons are performed at the compile time. Consider the following case:&lt;br /&gt;
&lt;br /&gt;
Problem statement:&lt;br /&gt;
Write a script to accept directories and pattern as input and send the files matching the pattern to a different server using sftp.&lt;br /&gt;
&lt;br /&gt;
We can maintain a string to store all the filenames to be moved to a directory on destination server preceded with appropriate sftp commands and use eval method to execute those commands in batchmode.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= Disadvantages of eval =&lt;br /&gt;
=== Slow in Execution ===&lt;br /&gt;
The code executed by eval runs slower, thus , whenever possible different technique should be used.&lt;br /&gt;
&lt;br /&gt;
=== Security Risks ===&lt;br /&gt;
'''JavaScript eval()'''&lt;br /&gt;
&amp;lt;br&amp;gt;Because most JSON-formatted text is also syntactically legal JavaScript code, an easy way for a JavaScript program to parse JSON-formatted data is to use the built-in JavaScript eval() function, which was designed to evaluate JavaScript expressions. Rather than using a JSON-specific parser, the JavaScript interpreter itself is used to execute the JSON data to produce native JavaScript objects. However, there are some Unicode characters that are valid in JSON strings but invalid in JavaScript, so additional escaping would be needed before using a JavaScript interpreter.[16]&lt;br /&gt;
Unless precautions are taken to validate the data first, the eval technique is subject to security vulnerabilities if the data and the entire JavaScript environment is not within the control of a single trusted source. For example, if the data is itself not trusted, it may be subject to malicious JavaScript code injection attacks. Also, such breaches of trust may create vulnerabilities for data theft, authentication forgery, and other potential misuse of data and resources. Regular expressions can be used to validate the data prior to invoking eval(). Consider the following contrived example:&lt;br /&gt;
&lt;br /&gt;
'''&amp;lt;html&amp;gt;&amp;lt;br&amp;gt;&amp;lt;head&amp;gt;&amp;lt;br&amp;gt;&amp;lt;/head&amp;gt;&amp;lt;br&amp;gt;&amp;lt;body&amp;gt;&amp;lt;br&amp;gt;&amp;lt;script type=&amp;quot;text/javascript&amp;quot; lang=&amp;quot;javascript&amp;quot;&amp;gt;'''&lt;br /&gt;
'''alert(&amp;quot;some problem&amp;quot;);&amp;lt;br&amp;gt;var evalstring=&amp;quot;obj={ name:'username',address: '238 Milton , st. Mary , WT 87382'}; alert('This is''' '''malicious code');&amp;quot;;&amp;lt;br&amp;gt;eval(evalstring);&amp;lt;br&amp;gt;&amp;lt;/script&amp;gt;&amp;lt;br&amp;gt;&amp;lt;/body&amp;gt;&amp;lt;br&amp;gt;&amp;lt;/head&amp;gt;&amp;lt;br&amp;gt;&amp;lt;/html&amp;gt; &amp;lt;br&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
Let's assume that the client side script is expecting a JSON object from the server. However, along with the JSON object it can also send other valid javascript statement. If the above code is run in the browser, after the execution of eval there will be an alert message saying 'This is malicious code'. The alert statement represents a placeholder for any potential javascript code. This can be avoided by using the safe Javascript library at JSON.org which contains the JSON.parse method.&lt;br /&gt;
&lt;br /&gt;
= eval in Different Languages =&lt;br /&gt;
&lt;br /&gt;
== Perl ==&lt;br /&gt;
The general form of [[Perl (programming language)|Perl]] expects an expression or a block of code as an expression. The expr passed as an argument is parsed and executed as small Perl program. If there are no errors in the expr , which is parsed , then it is executed within the lexical content of the current Perl program. &lt;br /&gt;
&lt;br /&gt;
=== Error Handling with eval ===&lt;br /&gt;
Eval can be used to trap the errors. During execution of the routines , the program might die due to errors and interrupts in the code. However , using eval , the code can still be running if the errors are handled inside the eval without being exited. We can evaluate the functionality of eval from the given code.&lt;br /&gt;
''' print &amp;quot;entern number 1 \n&amp;quot;; &amp;lt;br&amp;gt;$number1 = &amp;lt;STDIN&amp;gt;; &amp;lt;br&amp;gt;chomp $number1; &amp;lt;br&amp;gt;print &amp;quot;enter number 2 \n&amp;quot;; &amp;lt;br&amp;gt;$number2 = &amp;lt;STDIN&amp;gt;; ''' '''&amp;lt;br&amp;gt;chomp $number2; &amp;lt;br&amp;gt;print &amp;quot;division output is &amp;quot;; &amp;lt;br&amp;gt;$x =eval {$result = $number1/$number2}; &amp;lt;br&amp;gt;print &amp;quot;$x\n$@&amp;quot;; &amp;lt;br&amp;gt;$sum =''' '''$number1 + $number2; &amp;lt;br&amp;gt;print &amp;quot;sum is $sum \n&amp;quot;; &amp;lt;br&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
The above given code prompts the user to give 2 inputs and then performs the division and addition of the numbers. If the second number entered by the user is 0 , then in that case code exits showing the error message and it does not calculate the addition. However , we can handle this exception using eval. Eval handles the exception, shows the appropriate error message and then return the controller back to the program without exiting it. Hence , even if the second input is 0 , the addition is performed by the eval method.&lt;br /&gt;
&lt;br /&gt;
== Ruby ==&lt;br /&gt;
Most non-compiled languages have some kind of eval function, which will take a string and execute it as code. The eval method in Ruby accomplishes this, and is rather straightforward. The prototype of eval&lt;br /&gt;
eval(string [, binding [, filename [,lineno]]]) → obj.&lt;br /&gt;
Consider the following example code:&amp;lt;br&amp;gt;&lt;br /&gt;
''' #!/usr/bin/env ruby&amp;lt;br&amp;gt;code = &amp;quot;Time.now&amp;quot;&amp;lt;br&amp;gt;result = eval(code)&amp;lt;br&amp;gt;puts result'''&lt;br /&gt;
The method call Time.now is stored in a string. It's not executed, compiled or even looked at (other than determining that it's a string literal) by the Ruby interpreter. It's just a string, anything could be in there. However, once you call the eval method, that all changes. The Ruby interpreter parses this string as if it were Ruby code and runs it. It will return whatever the code evaluates to.&lt;br /&gt;
&lt;br /&gt;
Simple enough, but why would anyone want to use this? There's a lot of debate whether eval should be used, and it's often considered bad form and hackish. There are also some pretty big security implications in using eval at all in your code, so it's not something that should be used lightly. There is one great use for eval though: developer consoles. Adding a way for programmers to issue Ruby commands to your program directly by using eval can be a powerful debugging tool and quite easy to implement.&lt;br /&gt;
&lt;br /&gt;
''' Using bindings in eval: '''&lt;br /&gt;
You can use eval to run code from a string, but things are not as simple as they seem. The eval method executes code, but where in the program is that code executed? The context in which code is executed is called a binding. Code is bound to a context of instance variable, local variables, and global variables. If you were to reference the variable x, it's up to the binding to decide which variable named x you're referring to. Examine the following code.&amp;lt;br&amp;gt;&lt;br /&gt;
'''#!/usr/bin/env ruby&amp;lt;br&amp;gt;def func&amp;lt;br&amp;gt;x = 1337&amp;lt;br&amp;gt;return binding&amp;lt;br&amp;gt;end&amp;lt;br&amp;gt;x = 10&amp;lt;br&amp;gt;binding = func()&amp;lt;br&amp;gt;puts''' &lt;br /&gt;
'''eval(&amp;quot;x&amp;quot;, binding)&amp;lt;br&amp;gt;puts eval(&amp;quot;x&amp;quot;)&amp;lt;br&amp;gt;bind1 = func(1337)&amp;lt;br&amp;gt;'''&lt;br /&gt;
'''bind2 = func(404)&amp;lt;br&amp;gt;puts eval(&amp;quot;number&amp;quot;, bind1)&amp;lt;br&amp;gt;puts eval(&amp;quot;number&amp;quot;, bind2)'''&lt;br /&gt;
&lt;br /&gt;
This program works just as you would expect. Two invocations of the func method, and two numbers printed out. Bindings are an interesting feature, but just like eval, their use is controversial. While they can be useful at times, there's often a better way to achieve the same result.&lt;br /&gt;
&lt;br /&gt;
== PHP ==&lt;br /&gt;
&lt;br /&gt;
In PHP, eval executes code in a string almost exactly as if it had been put in the file instead of the call to eval(). The only exception is that errors are reported as coming from a call to eval(), and return statements become the result of the function.&lt;br /&gt;
Unlike some languages, the argument to eval must be a string of one or more complete statements, not just expressions; however, one can get the &amp;quot;expression&amp;quot; form of eval by putting the expression in a return statement, which causes eval to return the result of that expression. &lt;br /&gt;
eval prototype : mixed eval ( string $code )&lt;br /&gt;
The code in eval doesn't have to be wrapped in PHP tags ( &amp;lt;?php ?&amp;gt; ). If there is parse error in the argument to eval then it returns false. eval can return a value if the last statement in the argument passed to eval has a return statement then eval will return a value which can be stored in a string.&amp;lt;br&amp;gt;&lt;br /&gt;
'''&amp;lt;?php&amp;lt;br&amp;gt;$string = 'cup';&amp;lt;br&amp;gt;$name = 'coffee';&amp;lt;br&amp;gt;$str = 'This is a $string with my $name in it.';'''&lt;br /&gt;
'''&amp;lt;br&amp;gt;echo $str. &amp;quot;\n&amp;quot;;&amp;lt;br&amp;gt;eval(&amp;quot;\$str = \&amp;quot;$str\&amp;quot;;&amp;quot;);&amp;lt;br&amp;gt;echo $str. &amp;quot;\n&amp;quot;;&amp;lt;br&amp;gt;?&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
The above code will output&lt;br /&gt;
&amp;quot;This is a $string with my $name in it.&lt;br /&gt;
This is a cup with my coffee in it.&amp;quot;&lt;br /&gt;
Unlike perl if there is a fatal error in the code passed to eval the whole script exits.&lt;br /&gt;
&lt;br /&gt;
= References =&lt;br /&gt;
http://ruby.about.com/od/advancedruby/a/Eval-Running-Code-On-The-Fly.htm&amp;lt;br&amp;gt;&lt;br /&gt;
http://php.net/manual/en/function.eval.php&amp;lt;br&amp;gt;&lt;br /&gt;
http://www.ruby-doc.org/core-1.9.3/Kernel.html&amp;lt;br&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Eval#PHP&amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Vgupta6</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w28_mv&amp;diff=65135</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w28 mv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w28_mv&amp;diff=65135"/>
		<updated>2012-09-15T03:25:23Z</updated>

		<summary type="html">&lt;p&gt;Vgupta6: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= eval =&lt;br /&gt;
Eval function in most of the languages will either be a valid string representing expression in the language or some languages may allow blocks of valid code. The input is executed by the interpreter or compiler in same manner as other code in the language.&lt;br /&gt;
&lt;br /&gt;
= Advantage of eval =&lt;br /&gt;
eval is useful for writing metaprogamming code in most of the languages. This is a great asset for statically typed languages where most of the operatioons are performed at the compile time. Consider the following case:&lt;br /&gt;
&lt;br /&gt;
Problem statement:&lt;br /&gt;
Write a script to accept directories and pattern as input and send the files matching the pattern to a different server using sftp.&lt;br /&gt;
&lt;br /&gt;
We can maintain a string to store all the filenames to be moved to a directory on destination server preceded with appropriate sftp commands and use eval method to execute those commands in batchmode.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= Disadvantages of eval =&lt;br /&gt;
=== Slow in Execution ===&lt;br /&gt;
The code executed by eval runs slower, thus , whenever possible different technique should be used.&lt;br /&gt;
&lt;br /&gt;
=== Security Risks ===&lt;br /&gt;
'''JavaScript eval()'''&lt;br /&gt;
&amp;lt;br&amp;gt;Because most JSON-formatted text is also syntactically legal JavaScript code, an easy way for a JavaScript program to parse JSON-formatted data is to use the built-in JavaScript eval() function, which was designed to evaluate JavaScript expressions. Rather than using a JSON-specific parser, the JavaScript interpreter itself is used to execute the JSON data to produce native JavaScript objects. However, there are some Unicode characters that are valid in JSON strings but invalid in JavaScript, so additional escaping would be needed before using a JavaScript interpreter.[16]&lt;br /&gt;
Unless precautions are taken to validate the data first, the eval technique is subject to security vulnerabilities if the data and the entire JavaScript environment is not within the control of a single trusted source. For example, if the data is itself not trusted, it may be subject to malicious JavaScript code injection attacks. Also, such breaches of trust may create vulnerabilities for data theft, authentication forgery, and other potential misuse of data and resources. Regular expressions can be used to validate the data prior to invoking eval(). Consider the following contrived example:&lt;br /&gt;
&lt;br /&gt;
'''&amp;lt;html&amp;gt;&amp;lt;br&amp;gt;&amp;lt;head&amp;gt;&amp;lt;br&amp;gt;&amp;lt;/head&amp;gt;&amp;lt;br&amp;gt;&amp;lt;body&amp;gt;&amp;lt;br&amp;gt;&amp;lt;script type=&amp;quot;text/javascript&amp;quot; lang=&amp;quot;javascript&amp;quot;&amp;gt;'''&lt;br /&gt;
'''alert(&amp;quot;some problem&amp;quot;);&amp;lt;br&amp;gt;var evalstring=&amp;quot;obj={ name:'username',address: '238 Milton , st. Mary , WT 87382'}; alert('This is''' '''malicious code');&amp;quot;;&amp;lt;br&amp;gt;eval(evalstring);&amp;lt;br&amp;gt;&amp;lt;/script&amp;gt;&amp;lt;br&amp;gt;&amp;lt;/body&amp;gt;&amp;lt;br&amp;gt;&amp;lt;/head&amp;gt;&amp;lt;br&amp;gt;&amp;lt;/html&amp;gt; &amp;lt;br&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
Let's assume that the client side script is expecting a JSON object from the server. However, along with the JSON object it can also send other valid javascript statement. If the above code is run in the browser, after the execution of eval there will be an alert message saying 'This is malicious code'. The alert statement represents a placeholder for any potential javascript code. This can be avoided by using the safe Javascript library at JSON.org which contains the JSON.parse method.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= Perl =&lt;br /&gt;
The general form of [[Perl (programming language)|Perl]] expects an expression or a block of code as an expression. The expr passed as an argument is parsed and executed as small Perl program. If there are no errors in the expr , which is parsed , then it is executed within the lexical content of the current Perl program. &lt;br /&gt;
&lt;br /&gt;
=== Error Handling with eval ===&lt;br /&gt;
Eval can be used to trap the errors. During execution of the routines , the program might die due to errors and interrupts in the code. However , using eval , the code can still be running if the errors are handled inside the eval without being exited. We can evaluate the functionality of eval from the given code.&lt;br /&gt;
''' print &amp;quot;entern number 1 \n&amp;quot;; &amp;lt;br&amp;gt;$number1 = &amp;lt;STDIN&amp;gt;; &amp;lt;br&amp;gt;chomp $number1; &amp;lt;br&amp;gt;print &amp;quot;enter number 2 \n&amp;quot;; &amp;lt;br&amp;gt;$number2 = &amp;lt;STDIN&amp;gt;; ''' '''&amp;lt;br&amp;gt;chomp $number2; &amp;lt;br&amp;gt;print &amp;quot;division output is &amp;quot;; &amp;lt;br&amp;gt;$x =eval {$result = $number1/$number2}; &amp;lt;br&amp;gt;print &amp;quot;$x\n$@&amp;quot;; &amp;lt;br&amp;gt;$sum =''' '''$number1 + $number2; &amp;lt;br&amp;gt;print &amp;quot;sum is $sum \n&amp;quot;; &amp;lt;br&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
The above given code prompts the user to give 2 inputs and then performs the division and addition of the numbers. If the second number entered by the user is 0 , then in that case code exits showing the error message and it does not calculate the addition. However , we can handle this exception using eval. Eval handles the exception, shows the appropriate error message and then return the controller back to the program without exiting it. Hence , even if the second input is 0 , the addition is performed by the eval method.&lt;br /&gt;
&lt;br /&gt;
= Ruby =&lt;br /&gt;
Most non-compiled languages have some kind of eval function, which will take a string and execute it as code. The eval method in Ruby accomplishes this, and is rather straightforward. The prototype of eval&lt;br /&gt;
eval(string [, binding [, filename [,lineno]]]) → obj.&lt;br /&gt;
Consider the following example code:&amp;lt;br&amp;gt;&lt;br /&gt;
''' #!/usr/bin/env ruby&amp;lt;br&amp;gt;code = &amp;quot;Time.now&amp;quot;&amp;lt;br&amp;gt;result = eval(code)&amp;lt;br&amp;gt;puts result'''&lt;br /&gt;
The method call Time.now is stored in a string. It's not executed, compiled or even looked at (other than determining that it's a string literal) by the Ruby interpreter. It's just a string, anything could be in there. However, once you call the eval method, that all changes. The Ruby interpreter parses this string as if it were Ruby code and runs it. It will return whatever the code evaluates to.&lt;br /&gt;
&lt;br /&gt;
Simple enough, but why would anyone want to use this? There's a lot of debate whether eval should be used, and it's often considered bad form and hackish. There are also some pretty big security implications in using eval at all in your code, so it's not something that should be used lightly. There is one great use for eval though: developer consoles. Adding a way for programmers to issue Ruby commands to your program directly by using eval can be a powerful debugging tool and quite easy to implement.&lt;br /&gt;
&lt;br /&gt;
''' Using bindings in eval: '''&lt;br /&gt;
You can use eval to run code from a string, but things are not as simple as they seem. The eval method executes code, but where in the program is that code executed? The context in which code is executed is called a binding. Code is bound to a context of instance variable, local variables, and global variables. If you were to reference the variable x, it's up to the binding to decide which variable named x you're referring to. Examine the following code.&amp;lt;br&amp;gt;&lt;br /&gt;
'''#!/usr/bin/env ruby&amp;lt;br&amp;gt;def func&amp;lt;br&amp;gt;x = 1337&amp;lt;br&amp;gt;return binding&amp;lt;br&amp;gt;end&amp;lt;br&amp;gt;x = 10&amp;lt;br&amp;gt;binding = func()&amp;lt;br&amp;gt;puts''' &lt;br /&gt;
'''eval(&amp;quot;x&amp;quot;, binding)&amp;lt;br&amp;gt;puts eval(&amp;quot;x&amp;quot;)&amp;lt;br&amp;gt;bind1 = func(1337)&amp;lt;br&amp;gt;'''&lt;br /&gt;
'''bind2 = func(404)&amp;lt;br&amp;gt;puts eval(&amp;quot;number&amp;quot;, bind1)&amp;lt;br&amp;gt;puts eval(&amp;quot;number&amp;quot;, bind2)'''&lt;br /&gt;
&lt;br /&gt;
This program works just as you would expect. Two invocations of the func method, and two numbers printed out. Bindings are an interesting feature, but just like eval, their use is controversial. While they can be useful at times, there's often a better way to achieve the same result.&lt;br /&gt;
&lt;br /&gt;
= PHP =&lt;br /&gt;
&lt;br /&gt;
In PHP, eval executes code in a string almost exactly as if it had been put in the file instead of the call to eval(). The only exception is that errors are reported as coming from a call to eval(), and return statements become the result of the function.&lt;br /&gt;
Unlike some languages, the argument to eval must be a string of one or more complete statements, not just expressions; however, one can get the &amp;quot;expression&amp;quot; form of eval by putting the expression in a return statement, which causes eval to return the result of that expression. &lt;br /&gt;
eval prototype : mixed eval ( string $code )&lt;br /&gt;
The code in eval doesn't have to be wrapped in PHP tags ( &amp;lt;?php ?&amp;gt; ). If there is parse error in the argument to eval then it returns false. eval can return a value if the last statement in the argument passed to eval has a return statement then eval will return a value which can be stored in a string.&amp;lt;br&amp;gt;&lt;br /&gt;
'''&amp;lt;?php&amp;lt;br&amp;gt;$string = 'cup';&amp;lt;br&amp;gt;$name = 'coffee';&amp;lt;br&amp;gt;$str = 'This is a $string with my $name in it.';'''&lt;br /&gt;
'''&amp;lt;br&amp;gt;echo $str. &amp;quot;\n&amp;quot;;&amp;lt;br&amp;gt;eval(&amp;quot;\$str = \&amp;quot;$str\&amp;quot;;&amp;quot;);&amp;lt;br&amp;gt;echo $str. &amp;quot;\n&amp;quot;;&amp;lt;br&amp;gt;?&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
The above code will output&lt;br /&gt;
&amp;quot;This is a $string with my $name in it.&lt;br /&gt;
This is a cup with my coffee in it.&amp;quot;&lt;br /&gt;
Unlike perl if there is a fatal error in the code passed to eval the whole script exits.&lt;/div&gt;</summary>
		<author><name>Vgupta6</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w28_mv&amp;diff=65128</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w28 mv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w28_mv&amp;diff=65128"/>
		<updated>2012-09-15T03:12:30Z</updated>

		<summary type="html">&lt;p&gt;Vgupta6: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= eval =&lt;br /&gt;
Eval function in most of the languages will either be a valid string representing expression in the language or some languages may allow blocks of valid code. The input is executed by the interpreter or compiler in same manner as other code in the language.&lt;br /&gt;
&lt;br /&gt;
= Advantage of eval =&lt;br /&gt;
eval is useful for writing metaprogamming code in most of the languages. This is a great asset for statically typed languages where most of the operatioons are performed at the compile time. Consider the following case:&lt;br /&gt;
&lt;br /&gt;
Problem statement:&lt;br /&gt;
Write a script to accept directories and pattern as input and send the files matching the pattern to a different server using sftp.&lt;br /&gt;
&lt;br /&gt;
We can maintain a string to store all the filenames to be moved to a directory on destination server preceded with appropriate sftp commands and use eval method to execute those commands in batchmode.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= Disadvantages of eval =&lt;br /&gt;
=== Slow in Execution ===&lt;br /&gt;
The code executed by eval runs slower, thus , whenever possible different technique should be used.&lt;br /&gt;
&lt;br /&gt;
=== Security Risks ===&lt;br /&gt;
'''JavaScript eval()'''&lt;br /&gt;
&amp;lt;br&amp;gt;Because most JSON-formatted text is also syntactically legal JavaScript code, an easy way for a JavaScript program to parse JSON-formatted data is to use the built-in JavaScript eval() function, which was designed to evaluate JavaScript expressions. Rather than using a JSON-specific parser, the JavaScript interpreter itself is used to execute the JSON data to produce native JavaScript objects. However, there are some Unicode characters that are valid in JSON strings but invalid in JavaScript, so additional escaping would be needed before using a JavaScript interpreter.[16]&lt;br /&gt;
Unless precautions are taken to validate the data first, the eval technique is subject to security vulnerabilities if the data and the entire JavaScript environment is not within the control of a single trusted source. For example, if the data is itself not trusted, it may be subject to malicious JavaScript code injection attacks. Also, such breaches of trust may create vulnerabilities for data theft, authentication forgery, and other potential misuse of data and resources. Regular expressions can be used to validate the data prior to invoking eval(). Consider the following contrived example:&lt;br /&gt;
&lt;br /&gt;
'''&amp;lt;html&amp;gt;&amp;lt;br&amp;gt;&amp;lt;head&amp;gt;&amp;lt;br&amp;gt;&amp;lt;/head&amp;gt;&amp;lt;br&amp;gt;&amp;lt;body&amp;gt;&amp;lt;br&amp;gt;&amp;lt;script type=&amp;quot;text/javascript&amp;quot; lang=&amp;quot;javascript&amp;quot;&amp;gt;'''&lt;br /&gt;
'''alert(&amp;quot;some problem&amp;quot;);&amp;lt;br&amp;gt;var evalstring=&amp;quot;obj={ name:'username',address: '238 Milton , st. Mary , WT 87382'}; alert('This is''' '''malicious code');&amp;quot;;&amp;lt;br&amp;gt;eval(evalstring);&amp;lt;br&amp;gt;&amp;lt;/script&amp;gt;&amp;lt;br&amp;gt;&amp;lt;/body&amp;gt;&amp;lt;br&amp;gt;&amp;lt;/head&amp;gt;&amp;lt;br&amp;gt;&amp;lt;/html&amp;gt; &amp;lt;br&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
Let's assume that the client side script is expecting a JSON object from the server. However, along with the JSON object it can also send other valid javascript statement. If the above code is run in the browser, after the execution of eval there will be an alert message saying 'This is malicious code'. The alert statement represents a placeholder for any potential javascript code. This can be avoided by using the safe Javascript library at JSON.org which contains the JSON.parse method.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= Perl =&lt;br /&gt;
The general form of [[Perl (programming language)|Perl]] expects an expression or a block of code as an expression. The expr passed as an argument is parsed and executed as small Perl program. If there are no errors in the expr , which is parsed , then it is executed within the lexical content of the current Perl program. &lt;br /&gt;
&lt;br /&gt;
=== Error Handling with eval ===&lt;br /&gt;
Eval can be used to trap the errors. During execution of the routines , the program might die due to errors and interrupts in the code. However , using eval , the code can still be running if the errors are handled inside the eval without being exited. We can evaluate the functionality of eval from the given code.&lt;br /&gt;
''' print &amp;quot;entern number 1 \n&amp;quot;; &amp;lt;br&amp;gt;$number1 = &amp;lt;STDIN&amp;gt;; &amp;lt;br&amp;gt;chomp $number1; &amp;lt;br&amp;gt;print &amp;quot;enter number 2 \n&amp;quot;; &amp;lt;br&amp;gt;$number2 = &amp;lt;STDIN&amp;gt;; ''' '''&amp;lt;br&amp;gt;chomp $number2; &amp;lt;br&amp;gt;print &amp;quot;division output is &amp;quot;; &amp;lt;br&amp;gt;$x =eval {$result = $number1/$number2}; &amp;lt;br&amp;gt;print &amp;quot;$x\n$@&amp;quot;; &amp;lt;br&amp;gt;$sum =''' '''$number1 + $number2; &amp;lt;br&amp;gt;print &amp;quot;sum is $sum \n&amp;quot;; &amp;lt;br&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
The above given code prompts the user to give 2 inputs and then performs the division and addition of the numbers. If the second number entered by the user is 0 , then in that case code exits showing the error message and it does not calculate the addition. However , we can handle this exception using eval. Eval handles the exception, shows the appropriate error message and then return the controller back to the program without exiting it. Hence , even if the second input is 0 , the addition is performed by the eval method.&lt;br /&gt;
&lt;br /&gt;
= Ruby =&lt;br /&gt;
Most non-compiled languages have some kind of eval function, which will take a string and execute it as code. The eval method in Ruby accomplishes this, and is rather straightforward. The prototype of eval&lt;br /&gt;
eval(string [, binding [, filename [,lineno]]]) → obj.&lt;br /&gt;
Consider the following example code:&amp;lt;br&amp;gt;&lt;br /&gt;
''' #!/usr/bin/env ruby&amp;lt;br&amp;gt;code = &amp;quot;Time.now&amp;quot;&amp;lt;br&amp;gt;result = eval(code)&amp;lt;br&amp;gt;puts result'''&lt;br /&gt;
The method call Time.now is stored in a string. It's not executed, compiled or even looked at (other than determining that it's a string literal) by the Ruby interpreter. It's just a string, anything could be in there. However, once you call the eval method, that all changes. The Ruby interpreter parses this string as if it were Ruby code and runs it. It will return whatever the code evaluates to.&lt;br /&gt;
&lt;br /&gt;
Simple enough, but why would anyone want to use this? There's a lot of debate whether eval should be used, and it's often considered bad form and hackish. There are also some pretty big security implications in using eval at all in your code, so it's not something that should be used lightly. There is one great use for eval though: developer consoles. Adding a way for programmers to issue Ruby commands to your program directly by using eval can be a powerful debugging tool and quite easy to implement.&lt;br /&gt;
&lt;br /&gt;
''' Using bindings in eval: '''&lt;br /&gt;
You can use eval to run code from a string, but things are not as simple as they seem. The eval method executes code, but where in the program is that code executed? The context in which code is executed is called a binding. Code is bound to a context of instance variable, local variables, and global variables. If you were to reference the variable x, it's up to the binding to decide which variable named x you're referring to. Examine the following code.&amp;lt;br&amp;gt;&lt;br /&gt;
'''#!/usr/bin/env ruby&amp;lt;br&amp;gt;def func&amp;lt;br&amp;gt;x = 1337&amp;lt;br&amp;gt;return binding&amp;lt;br&amp;gt;end&amp;lt;br&amp;gt;x = 10&amp;lt;br&amp;gt;binding = func()&amp;lt;br&amp;gt;puts''' &lt;br /&gt;
'''eval(&amp;quot;x&amp;quot;, binding)&amp;lt;br&amp;gt;puts eval(&amp;quot;x&amp;quot;)&amp;lt;br&amp;gt;bind1 = func(1337)&amp;lt;br&amp;gt;'''&lt;br /&gt;
'''bind2 = func(404)&amp;lt;br&amp;gt;puts eval(&amp;quot;number&amp;quot;, bind1)&amp;lt;br&amp;gt;puts eval(&amp;quot;number&amp;quot;, bind2)'''&lt;br /&gt;
&lt;br /&gt;
This program works just as you would expect. Two invocations of the func method, and two numbers printed out. Bindings are an interesting feature, but just like eval, their use is controversial. While they can be useful at times, there's often a better way to achieve the same result.&lt;/div&gt;</summary>
		<author><name>Vgupta6</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w28_mv&amp;diff=65121</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w28 mv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w28_mv&amp;diff=65121"/>
		<updated>2012-09-15T02:53:11Z</updated>

		<summary type="html">&lt;p&gt;Vgupta6: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= eval =&lt;br /&gt;
Eval function in most of the languages will either be a valid string representing expression in the language or some languages may allow blocks of valid code. The input is executed by the interpreter or compiler in same manner as other code in the language.&lt;br /&gt;
&lt;br /&gt;
= Advantage of eval =&lt;br /&gt;
eval is useful for writing metaprogamming code in most of the languages. This is a great asset for statically typed languages where most of the operatioons are performed at the compile time. Consider the following case:&lt;br /&gt;
&lt;br /&gt;
Problem statement:&lt;br /&gt;
Write a script to accept directories and pattern as input and send the files matching the pattern to a different server using sftp.&lt;br /&gt;
&lt;br /&gt;
We can maintain a string to store all the filenames to be moved to a directory on destination server preceded with appropriate sftp commands and use eval method to execute those commands in batchmode.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= Disadvantages of eval =&lt;br /&gt;
Slow in Execution :&lt;br /&gt;
The code executed by eval runs slower, thus , whenever possible different technique should be used.&lt;br /&gt;
&lt;br /&gt;
= Perl =&lt;br /&gt;
The general form of [[Perl (programming language)|Perl]] expects an expression or a block of code as an expression. The expr passed as an argument is parsed and executed as small Perl program. If there are no errors in the expr , which is parsed , then it is executed within the lexical content of the current Perl program. &lt;br /&gt;
&lt;br /&gt;
=== Error Handling with eval ===&lt;br /&gt;
Eval can be used to trap the errors. During execution of the routines , the program might die due to errors and interrupts in the code. However , using eval , the code can still be running if the errors are handled inside the eval without being exited. We can evaluate the functionality of eval from the given code.&lt;br /&gt;
===== print &amp;quot;entern number 1 \n&amp;quot;; &amp;lt;br&amp;gt;$number1 = &amp;lt;STDIN&amp;gt;; &amp;lt;br&amp;gt;chomp $number1; &amp;lt;br&amp;gt;print &amp;quot;enter number 2 \n&amp;quot;; &amp;lt;br&amp;gt;$number2 = &amp;lt;STDIN&amp;gt;; &amp;lt;br&amp;gt;chomp $number2; &amp;lt;br&amp;gt;print &amp;quot;division output is &amp;quot;; &amp;lt;br&amp;gt;$x =eval {$result = $number1/$number2}; &amp;lt;br&amp;gt;print &amp;quot;$x\n$@&amp;quot;; &amp;lt;br&amp;gt;$sum = $number1 + $number2; &amp;lt;br&amp;gt;print &amp;quot;sum is $sum \n&amp;quot;; &amp;lt;br&amp;gt;=====&lt;br /&gt;
&lt;br /&gt;
The above given code prompts the user to give 2 inputs and then performs the division and addition of the numbers. If the second number entered by the user is 0 , then in that case code exits showing the error message and it does not calculate the addition. However , we can handle this exception using eval. Eval handles the exception, shows the appropriate error message and then return the controller back to the program without exiting it. Hence , even if the second input is 0 , the addition is performed by the eval method.&lt;br /&gt;
= Security risks =&lt;br /&gt;
'''JavaScript eval()'''&lt;br /&gt;
&amp;lt;br&amp;gt;Because most JSON-formatted text is also syntactically legal JavaScript code, an easy way for a JavaScript program to parse JSON-formatted data is to use the built-in JavaScript eval() function, which was designed to evaluate JavaScript expressions. Rather than using a JSON-specific parser, the JavaScript interpreter itself is used to execute the JSON data to produce native JavaScript objects. However, there are some Unicode characters that are valid in JSON strings but invalid in JavaScript, so additional escaping would be needed before using a JavaScript interpreter.[16]&lt;br /&gt;
Unless precautions are taken to validate the data first, the eval technique is subject to security vulnerabilities if the data and the entire JavaScript environment is not within the control of a single trusted source. For example, if the data is itself not trusted, it may be subject to malicious JavaScript code injection attacks. Also, such breaches of trust may create vulnerabilities for data theft, authentication forgery, and other potential misuse of data and resources. Regular expressions can be used to validate the data prior to invoking eval(). Consider the following contrived example:&lt;br /&gt;
&lt;br /&gt;
'''&amp;lt;html&amp;gt;&amp;lt;br&amp;gt;&amp;lt;head&amp;gt;&amp;lt;br&amp;gt;&amp;lt;/head&amp;gt;&amp;lt;br&amp;gt;&amp;lt;body&amp;gt;&amp;lt;br&amp;gt;&amp;lt;script type=&amp;quot;text/javascript&amp;quot; lang=&amp;quot;javascript&amp;quot;&amp;gt;'''&lt;br /&gt;
'''alert(&amp;quot;some problem&amp;quot;);&amp;lt;br&amp;gt;var evalstring=&amp;quot;obj={ name:'username',address: '238 Milton , st. Mary , WT 87382'}; alert('This is''' '''malicious code');&amp;quot;;&amp;lt;br&amp;gt;eval(evalstring);&amp;lt;br&amp;gt;&amp;lt;/script&amp;gt;&amp;lt;br&amp;gt;&amp;lt;/body&amp;gt;&amp;lt;br&amp;gt;&amp;lt;/head&amp;gt;&amp;lt;br&amp;gt;&amp;lt;/html&amp;gt; &amp;lt;br&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
Let's assume that the client side script is expecting a JSON object from the server. However, along with the JSON object it can also send other valid javascript statement. If the above code is run in the browser, after the execution of eval there will be an alert message saying 'This is malicious code'. The alert statement represents a placeholder for any potential javascript code. This can be avoided by using the safe Javascript library at JSON.org which contains the JSON.parse method.&lt;/div&gt;</summary>
		<author><name>Vgupta6</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w28_mv&amp;diff=65102</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w28 mv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w28_mv&amp;diff=65102"/>
		<updated>2012-09-15T02:15:35Z</updated>

		<summary type="html">&lt;p&gt;Vgupta6: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= eval =&lt;br /&gt;
&lt;br /&gt;
= Perl =&lt;br /&gt;
The general form of [[Perl (programming language)|Perl]] expects an expression or a block of code as an expression. The expr passed as an argument is parsed and executed as small Perl program. If there are no errors in the expr , which is parsed , then it is executed within the lexical content of the current Perl program. &lt;br /&gt;
&lt;br /&gt;
''' Error Handling with eval ''' &lt;br /&gt;
&amp;lt;br&amp;gt;Eval can be used to trap the errors. During execution of the routines , the program might die due to errors and interrupts in the code. However , using eval , the code can still be running if the errors are handled inside the eval without being exited. We can evaluate the functionality of eval from the given code.&lt;br /&gt;
===== print &amp;quot;entern number 1 \n&amp;quot;; &amp;lt;br&amp;gt;$number1 = &amp;lt;STDIN&amp;gt;; &amp;lt;br&amp;gt;chomp $number1; &amp;lt;br&amp;gt;print &amp;quot;enter number 2 \n&amp;quot;; &amp;lt;br&amp;gt;$number2 = &amp;lt;STDIN&amp;gt;; &amp;lt;br&amp;gt;chomp $number2; &amp;lt;br&amp;gt;print &amp;quot;division output is &amp;quot;; &amp;lt;br&amp;gt;$x =eval {$result = $number1/$number2}; &amp;lt;br&amp;gt;print &amp;quot;$x\n$@&amp;quot;; &amp;lt;br&amp;gt;$sum = $number1 + $number2; &amp;lt;br&amp;gt;print &amp;quot;sum is $sum \n&amp;quot;; &amp;lt;br&amp;gt;=====&lt;br /&gt;
&lt;br /&gt;
The above given code prompts the user to give 2 inputs and then performs the division and addition of the numbers. If the second number entered by the user is 0 , then in that case code exits showing the error message and it does not calculate the addition. However , we can handle this exception using eval. Eval handles the exception, shows the appropriate error message and then return the controller back to the program without exiting it. Hence , even if the second input is 0 , the addition is performed by the eval method.&lt;br /&gt;
= Security risks =&lt;br /&gt;
Special care '''must''' be taken when using &amp;lt;code&amp;gt;eval&amp;lt;/code&amp;gt; with data from an untrusted source. For instance, assuming that the &amp;lt;code&amp;gt;get_data()&amp;lt;/code&amp;gt; function gets data from the Internet, this [[Python (programming language)|Python]] code is insecure:&lt;br /&gt;
&lt;br /&gt;
session['authenticated'] = False &lt;br /&gt;
&amp;lt;br&amp;gt;data = get_data()&lt;br /&gt;
&amp;lt;br&amp;gt;foo = eval(data) &lt;br /&gt;
&lt;br /&gt;
An attacker could supply the program with the string &amp;lt;code&amp;gt;&amp;quot;session.update(authenticated=True)&amp;quot;&amp;lt;/code&amp;gt; as data, which would update the &amp;lt;code&amp;gt;session&amp;lt;/code&amp;gt; dictionary to set an authenticated key to be True. To remedy this, all data which will be used with &amp;lt;code&amp;gt;eval&amp;lt;/code&amp;gt; must be escaped, or it must be run without access to potentially harmful functions.&lt;/div&gt;</summary>
		<author><name>Vgupta6</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w28_mv&amp;diff=64942</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w28 mv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w28_mv&amp;diff=64942"/>
		<updated>2012-09-15T00:28:55Z</updated>

		<summary type="html">&lt;p&gt;Vgupta6: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Security risks =&lt;br /&gt;
Special care '''must''' be taken when using &amp;lt;code&amp;gt;eval&amp;lt;/code&amp;gt; with data from an untrusted source. For instance, assuming that the &amp;lt;code&amp;gt;get_data()&amp;lt;/code&amp;gt; function gets data from the Internet, this [[Python (programming language)|Python]] code is insecure:&lt;br /&gt;
http://en.wikipedia.org/wiki/Python_(programming_language)&lt;br /&gt;
session['authenticated'] = False&lt;br /&gt;
data = get_data()&lt;br /&gt;
foo = eval(data)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
An attacker could supply the program with the string &amp;lt;code&amp;gt;&amp;quot;session.update(authenticated=True)&amp;quot;&amp;lt;/code&amp;gt; as data, which would update the &amp;lt;code&amp;gt;session&amp;lt;/code&amp;gt; dictionary to set an authenticated key to be True. To remedy this, all data which will be used with &amp;lt;code&amp;gt;eval&amp;lt;/code&amp;gt; must be escaped, or it must be run without access to potentially harmful functions.&lt;/div&gt;</summary>
		<author><name>Vgupta6</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w28_mv&amp;diff=64919</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w28 mv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w28_mv&amp;diff=64919"/>
		<updated>2012-09-15T00:22:32Z</updated>

		<summary type="html">&lt;p&gt;Vgupta6: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Security risks =&lt;br /&gt;
Special care '''must''' be taken when using &amp;lt;code&amp;gt;eval&amp;lt;/code&amp;gt; with data from an untrusted source. For instance, assuming that the &amp;lt;code&amp;gt;get_data()&amp;lt;/code&amp;gt; function gets data from the Internet, this [[Python (programming language)|Python]] code is insecure:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
session['authenticated'] = False&lt;br /&gt;
data = get_data()&lt;br /&gt;
foo = eval(data)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
An attacker could supply the program with the string &amp;lt;code&amp;gt;&amp;quot;session.update(authenticated=True)&amp;quot;&amp;lt;/code&amp;gt; as data, which would update the &amp;lt;code&amp;gt;session&amp;lt;/code&amp;gt; dictionary to set an authenticated key to be True. To remedy this, all data which will be used with &amp;lt;code&amp;gt;eval&amp;lt;/code&amp;gt; must be escaped, or it must be run without access to potentially harmful functions.&lt;/div&gt;</summary>
		<author><name>Vgupta6</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w28_mv&amp;diff=64916</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w28 mv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w28_mv&amp;diff=64916"/>
		<updated>2012-09-15T00:22:03Z</updated>

		<summary type="html">&lt;p&gt;Vgupta6: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt; == Security risks ==&lt;br /&gt;
Special care '''must''' be taken when using &amp;lt;code&amp;gt;eval&amp;lt;/code&amp;gt; with data from an untrusted source. For instance, assuming that the &amp;lt;code&amp;gt;get_data()&amp;lt;/code&amp;gt; function gets data from the Internet, this [[Python (programming language)|Python]] code is insecure:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
session['authenticated'] = False&lt;br /&gt;
data = get_data()&lt;br /&gt;
foo = eval(data)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
An attacker could supply the program with the string &amp;lt;code&amp;gt;&amp;quot;session.update(authenticated=True)&amp;quot;&amp;lt;/code&amp;gt; as data, which would update the &amp;lt;code&amp;gt;session&amp;lt;/code&amp;gt; dictionary to set an authenticated key to be True. To remedy this, all data which will be used with &amp;lt;code&amp;gt;eval&amp;lt;/code&amp;gt; must be escaped, or it must be run without access to potentially harmful functions.&lt;/div&gt;</summary>
		<author><name>Vgupta6</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w28_mv&amp;diff=64899</id>
		<title>CSC/ECE 517 Fall 2012/ch1 1w28 mv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2012/ch1_1w28_mv&amp;diff=64899"/>
		<updated>2012-09-15T00:12:37Z</updated>

		<summary type="html">&lt;p&gt;Vgupta6: Created page with &amp;quot;Editing CSC/ECE 517 Fall 2012/ch1 1w28 mv * w28 mv&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Editing CSC/ECE 517 Fall 2012/ch1 1w28 mv&lt;br /&gt;
*&lt;br /&gt;
w28&lt;br /&gt;
mv&lt;/div&gt;</summary>
		<author><name>Vgupta6</name></author>
	</entry>
</feed>