The Perl eval Function and Scalars
Using the Perl eval Function - Part 1
Introduction
This is part 1 of my series, Using the Perl eval Function. In this part of the series, we see how the right operand scalar of an assignment operator is used in an eval function. The eval function is a predefined Perl function (subroutine).
Note: If you cannot see the code or if you think anything is missing (broken link, image absent), just contact me at forchatrans@yahoo.com. That is, contact me for the slightest problem you have about what you are reading.
Use of the eval Function
The argument of the eval function is an expression or one or more Perl statements. If it is a statement, as argument, it ends with a semicolon. If the argument is an expression, the argument does not end with a semicolon. Hey, with the eval function, it is possible for the end user to actually write code that will interact with a program you have written. The eval function also allows one Perl program to execute and use the result of another Perl program. We shall see how all that is possible in this series. Of course you are the one to allow the user to input Perl code into your program. This has an advantage and a disadvantage. The advantage is that you are giving the user more allowance to do what you did not envisage your program would do. The disadvantage is that if the program is dealing with a sensitive issue like banking, then the user may use the allowance to steal money.
Prerequisite
You need to have knowledge in the following areas before reading this series:
Getting Started with ActivePerl
Perl Reference to a Scalar
OOP Basics in Perl
These titles are each the first part of a series. To arrive at any of these series, just type the title and my name, Chrys in the Search Box of this page and click Search.
ActivePerl is just Perl for the windows operating system and some other operating systems, which traditional Perl does not operate in. If you are using traditional Perl, then you need to type an extra line at the beginning of a program. This series uses ActivePerl.
Requirements
You need a personal web server and Perl interpreter in order to test the code samples of this series. I use, Abyss Web Server X1, and the Perl free interpreter from the company, ActiveState (search the web for ActivePerl). Of course you need a browser, text editor and a computer.
Extract from Perl Specification
I now give you an extract from the Perl specification. Read it and ask yourself if you understand what you have read. After reading it, if you do not understand what you have read, then this series is for you. This is the extract:
eval EXPR
eval BLOCK
In the first form, the return value of EXPR is parsed and executed as if it were a little Perl program. The value of the expression (which is itself determined within scalar context) is first parsed, and if there weren't any errors, executed in the lexical context of the current Perl program, so that any variable settings or subroutine and format definitions remain afterwards. Note that the value is parsed every time the eval executes. If EXPR is omitted, evaluates $_. This form is typically used to delay parsing and subsequent execution of the text of EXPR until run time.
In the second form, the code within the BLOCK is parsed only once--at the same time the code surrounding the eval itself was parsed--and executed within the context of the current Perl program. This form is typically used to trap exceptions more efficiently than the first, while also providing the benefit of checking the code within BLOCK at compile time.
Choice of above eval Function in this Series
There are two formats for the eval function above. The specification proposes the second format (eval BLOCK) as the better format and that is what we shall learn in this series. You usually will not need the first format (eval EXPR). The first format has another disadvantage in that it conflicts with the use of string in Perl (see later).
Let us now begin proper, the topic of this part of the series:
What is a Block?
A block here consists of an expression or one or more statements enclosed in curly braces. In this series all our arguments for the eval function shall be in a block.
Important Right Operands
In this part of the series, we look at scalar literals as arguments inside the eval function. We also look at the variables that hold scalars as arguments inside the eval function. We consider the cases where the literals and variables are right and left operands, respectively, of an assignment (=) statement.
String Literals
A string literal is zero or more characters enclosed in double (") or single (') quotation marks. A string must be delimited by quotation marks of the same type; that is, either both single quotation marks or both double quotation marks. The following are examples of string literals:
"blah"
"1234"
"one line \r\n another line with \$"
A special character like the newline character (\n) or the return character (\r) or \$ for $, in the string literal, should be escaped as in the last example above.
The following statement shows a string literal as a right operand of an ordinary statement:
my $str = "I am a string.";
A string literal inside the block argument of an eval() function is returned by the eval function. The following script illustrates this; you have "HTML" code in front and after the Perl code.
use strict;
print "Content-Type: text/html\n\n";
print "<html>";
print "<head><title>Code Sample</title></head>";
print "<body>";
my $str = eval{"seen"};
print $str;
print "</body>";
print "</html>";
Note: A literal inside the block of the eval function does not need to have the semicolon.
Integers
Integers can be expressed in decimal (base 10), hexadecimal (base 16), and octal (base. A decimal integer literal consists of a sequence of digits without a leading 0 (zero). A leading 0 (zero) on an integer literal indicates it is in octal; a leading 0x indicates hexadecimal (base 16). Hexadecimal integers can include digits (0-9) and the letters a-f and A-F. Octal integers can include only the digits 0-7.
Some examples of integer literals are: 42, 011, 0xFFF, and -345.
The following script works without any problem:
use strict;
print "Content-Type: text/html\n\n";
print "<html>";
print "<head><title>Code Sample</title></head>";
print "<body>";
my $var1 = eval{25};
print $var1 . "<br />";
my $var2 = eval{011};
print $var2 . "<br />";
my $var3 = eval{0xA};
print $var3 . "<br />";
print "</body>";
print "</html>";
Let us look at what is inside the Perl code. The first literal is the decimal number, 25, and it is returned as 25. The second literal is 011, an octal number. The octal number 11 is equivalent to 9 in decimal; 9 is returned by the eval function. The third literal is 0xA, a hexadecimal number. The hexadecimal number, A, is equivalent to 10 in decimal; 10 is returned by the eval function.
A literal integer as argument in the block of the eval function is returned by the eval function; however, non-decimal numbers are converted into decimals before being returned. Read and try the above code if you have not already done so.
An arithmetic expression whose numbers are integer literals can be used as argument in the eval() function. The following script works without any problem:
use strict;
print "Content-Type: text/html\n\n";
print "<html>";
print "<head><title>Code Sample</title></head>";
print "<body>";
my $var = eval{4 + 8};
print $var . "<br />";
print "</body>";
print "</html>";
The output is 12, which means that addition took place. You can use subtraction, multiplication, etc. in place of the addition.
Floating-Point Literals
For simplicity a floating-point number is a number with a decimal point. A floating-point literal can have the following parts:
- A decimal integer
- A decimal point (".")
- A fraction (another decimal integer)
- An exponent
The exponent part is an "E" followed by an integer, which can be signed (preceded by "+" or "-"). A floating-point literal must have at least one digit and either a decimal point and/or E, followed by a decimal number. Some examples of floating-point literals are 3.1415, -3.1E12, .1E12, and 2E-12.
Now 2000, written as a floating-point literal is 2E3. The output of the following code is 2000:
use strict;
print "Content-Type: text/html\n\n";
print "<html>";
print "<head><title>Code Sample</title></head>";
print "<body>";
my $var = eval{2E3};
print $var . "<br />";
print "</body>";
print "</html>";
The argument to the eval() function is 2E3. The eval() function converts the floating-point literal to decimal and returns the decimal value.
Arithmetic Expression
Here, we look at an arithmetic expression with different types of numbers. Consider the following:
4095 - 2000 - 30.25 = 2064.75
The hexadecimal number for 4095 is 0xFFF. A floating-point number for 2000 is 2E3. 30.25 is a number whose value you easily appreciate. So the above equation can be written as:
0xFFF - 2E3 - 30.25 = 2064.75
The left hand side of the above equation can be the argument of the eval() function.
Somewhere above I said that examples of floating-point literals are 3.1415, -3.1E12, .1e12, and 2E-12. A decimal number actually consists of a whole number part, a decimal point and a fractional part as integer. Of course, the fractional part can be zero. Now, 3.1415 is a decimal floating-point number, while 2E-12 is an exponential floating-point number.
If the eval() function takes the left hand side of the above equation as argument, it would evaluate the expression and return 2064.75. So the eval() function would take any arithmetic expression as argument and return the answer as a decimal floating-point number. Here, we are referring to an arithmetic expression with different types of numbers. Read and try the following code:
use strict;
print "Content-Type: text/html\n\n";
print "<html>";
print "<head><title>Code Sample</title></head>";
print "<body>";
my $var = eval{0xFFF - 2E3 - 30.25};
print $var . "<br />";
print "</body>";
print "</html>";
Remember: in this series we are treating the case where all the arguments for the eval function are in the block (curly braces).
Scalar Variables as Arguments
A scalar variable holds a scalar. If this variable is in the block of the eval function, the value of the variable will be returned. The following code illustrates this:
use strict;
print "Content-Type: text/html\n\n";
print "<html>";
print "<head><title>Code Sample</title></head>";
print "<body>";
my $var1 = "one";
print eval{$var1} . "<br />";
my $var2 = 46;
print eval{$var2} . "<br />";
my $var3 = 33.58;
print eval{$var3} . "<br />";
my $var4 = 7-5;
print eval{$var4} . "<br />";
print "</body>";
print "</html>";
The return value from the eval function goes to the display.
We conclude in this part of the series that a scalar literal or the value of a scalar variable, in the block of a Perl eval function, is returned by the eval function.
That is it for this part of the series. We stop here and continue in the next part. The next part and the rest of the series can be found at:
The Perl eval Function and Arrays
Chrys


Reply With Quote
