Lecture #2 Perl Syntaxes Introduction a Perl Script Consists of a Sequence
Total Page:16
File Type:pdf, Size:1020Kb
Lecture #2 Perl Syntaxes Introduction A Perl script consists of a sequence of declarations and statements which run from the top to the bottom; therefore, the scripting requires basic understanding of syntaxes. Literally, a declaration specifies the name and data type of a variable or script element; statements are the instructions programmers write to tell Perl interpreter what to do. The following is a declaration of variable x as scalar type of data. In programming, any data types that hold a single data item called scalar (or base) data types. Programming languages like C++ or Java has scalar type like char, int, short long, float, and double. Interestingly, Perl does not have a variety of scalar data types. Perl only distinguish string and numbers. $x = 7; # variable The following is a sample statement. Perl statements end in a semi-colon (;): print "Hello, world!"; Statements frequently contain expressions. An expression is something which evaluates to a value. The following is a statement with an expression because (3<5) will be evaluated to true. The ouput is 1 which means true. print (3<5); Expressions are often part of a loop (repetition structure) or a decisive statement (such as the if statement or the conditional operator) because they typically return a Boolean result: true or false. The following uses the conditional operator (? :) to evaluate an expression ($s > 60). #!"X:\xampp\perl\bin\perl.exe" print "Enter your score: "; $s = <>; $grade = ($s > 60)? "passed" : "Not passed"; print "$grade\n"; A Perl script is made of a combination of statements. Some statements are declaration; others are expressions. Many statements contains both declaration and expressions. The following is an example of complicated Perl statements. #!"X:\xampp\perl\bin\perl.exe" X must be the correct drive name print "Content-Type: text/html\n\n"; print "<html><head>", # comma as separator "<title>Test Page</title></head>", #comma as separator "<body>"; # semicolon [end of line] $x = 7; # declare a scalr variable $y = 5; # variable if ($y <= $x) # test expression { $msg = "CIS245!"; # variable } print "$msg</body></html>"; 38 All the lines of code you have just seen are examples of Perl statements. Basically, a statement is one task for the Perl interpreter to perform. A statement can contain construct, variables, expression, or any combination of them. The semicolon (;) indicates the end of each statement, while comma (,) is a separator between sections of a statement. The print construct, as discussed in the previous lecture, supports the comma (,) sign to break a long statement into lines. #!"X:\xampp\perl\bin\perl.exe" print "Content-type: text/html\n\n", "<!Doctype html>", "<html><body>", "<h1>Hello, World!</h1>", "</body></html>"; A Perl program can be thought of as a collection of statements performed one at a time. When the Perl interpreter sees a statement, it breaks the statement down into smaller units of information. In this example, the smaller units of information are $x, =, 7, and ;. Each of these smaller units of information is called a token. Perl statements can be grouped into blocks. A Perl block is enclosed by a pair of curly brackets. In the above code, the if statement is an example of blocks. The following is another example that uses the “sub” keyword to create a subroutine which has its own code block. A later lecture will discuss to how create subroutine in details. #!"X:\xampp\perl\bin\perl.exe" print "Content-Type: text/html\n\n"; print "<!Doctype html>"; print "<html><body>"; sub Hello { print "Hello, World!"; } Hello(); #call the subroutine print "</body></html>"; Perl is a case sensitive language. File names, variables, and arrays are all case sensitive. If you capitalize a variable name when you define it, you must capitalize it to call it. $X and $x are two different variables. Perl Data Perl has three built-in data types: scalars, arrays, and hashes. A scalar is either a single string Types and or a number in most of the cases. Yet, it can also be a reference to something. A sting literal is Variables a combination of characters enclosed by either single quotes or double-quotes. These quotes are not a part of the string they just mark the beginning and end of the string for the Perl interpreter. The following are some examples. '239' "45.5" 'four' "Jennifer Lopez" "Penn State University" Both ‘239’ and “45.5” are not numbers. ‘239’ is not a unit of anything. It is a combination of character: 2, 3, and 9. “45.5” does not mean a value of forty-five and a half. It is a combination 39 of characters: 4, 5, ., and 5. The blank space between Jennifer and Lopez in the string “Jennifer Lopez” as well as those in “Penn State University” is also a character called “blank” character. There are actually two varieties of null strings (sometimes referred to as “empty” strings), a defined one and an undefined one. The defined version is just a string of length zero, such as "" . #!"X:\xampp\perl\bin\perl.exe" $x = ""; print $x; The undefined version is the use of keyword “undef” which indicates “no real value” for the variable. #!"X:\xampp\perl\bin\perl.exe" $x = undef; print $x; It is necessary to understand the difference between a null character and a blank character. A null character does not take any bits, while a blank character does. A blank character can be considered as an invisible character. The following is a sample code that use the length() method to return the length of characters in a string. The comma sign (,) is the separator of the print function, and “\n” inserts a newline. #!"X:\xampp\perl\bin\perl.exe" print length(""), "\n<br>"; print length(" "), "\n<br>"; print length(''), "\n<br>"; print length(' '), "\n<br>"; To test the above code, name the script as “test.pl” and save it under the “X:\xampp\perl\bin\” directory, where “X” is the drive name that host XAMPP. Open the Microsoft Command Prompt, change to the “X:\xampp\perl\bin\” directory, and then issue perl test.pl. X:\cd xampp\perl\bin X:\xampp\perl\bin>pel test.pl 0 1 0 1 Numeric literals (numbers) could be floating point or integer formats, as shown in the following table. Format Example integer 627 floating point 345.29 Scientific .23E-10 hex 0xff octal 0377 binary 0b011011 The following is a Web-ready script that illustrates how these variations works. Interestingly, most Web browser ignores blank space created by “\n”. To break a line in a browser, you need to use the <br> HTML tag. #!"X:\xampp\perl\bin\perl.exe" print "Content-Type: text/html\n\n"; 40 print "<!Doctype html>"; print "<html><body>"; print 627, "\n<br>"; print 345.29, "\n<br>"; print .23E-10, "\n<br>"; print 0xff, "\n<br>"; print 0377, "\n<br>"; print 0b011011, "\n<br>"; print "</body></html>"; It is necessary to distinguish string and numbers. Strings may contain any symbol, letter, or number. Must be enclosed by quotes. E.g. “23”, “3.1412”, and “hell”. Numbers may contain exponents, integers, or decimal values. E.g.: 23, 3.1412. A Perl variable is a temporary name given by the programmers to mark an area of the physical memory to temporarily store a value. Throughout the duration of the Perl script. In Perl, variables are declared to hold a scalar which could be a single value, such as a string, a number, or a reference. A Perl variable names begin with a dollar sign ($). They can be any combination of letters, numbers, or underscores. Names that start with a digit may only contain more digits. Names that do not start with a letter, digit, or underscore are limited to one character besides the $ ($*, etc.). $x $age $firstName $Student_ID $bloodType Perl variables do not have to be explicitly declared to reserve memory space, which means you do not need to specify the data type like int, float, double, string, char, and so on. The following compare Perl variable declaration with that in C++. Perl C++ $x double x $age float age $firstName string firstName $Student_ID string Student_ID $bloodType char bloodType The above scalar variables (age, firstName, and Student_ID) do not have initial values (also known as default values), meaning they do not represent any value at the time they are declared. Perl uses assignment statements to give a variable some value, or to change the existing value of a variable. The following is the syntax for assigning a value to a variable, where variableName is the variable’s name, and value is the number or string you want to store in (or be represented by) the variable name. variableName = value; The following is a script that demonstrates how to declare variables and assign their initial values. Again, when declaring a string variable with initial value, the value must be enclosed by a pair of quotes. #!"X:\xampp\perl\bin\perl.exe" print "Content-Type: text/html\n\n"; print "<!Doctype html>"; 41 print "<html><body>"; $x = 3.12; $age = 41; $firstName = "Jennifer"; $Student_ID = "D004821256"; $bloodType = 'A'; $score = ""; print "</body></html>"; The syntax to retrieve the value of a variable is: $variableName The following is an Web-ready script that illustrates how to declare a variable, assign an initial value, and retrieve the value of a Perl variable. #!"X:\xampp\perl\bin\perl.exe" print "Content-Type: text/html\n\n"; print "<!Doctype html>"; print "<html><body>"; $x = 3.12; $age = 41; $firstName = "Jennifer"; $Student_ID = "D004821256"; $bloodType = 'A'; $score = ""; print $x, "\n<br>"; print $age, "\n<br>"; print $firstName, "\n<br>"; print $Student_ID, "\n<br>"; print $bloodType, "\n<br>"; print "</body></html>"; Remember, a scalar variable can only hold one value at a given time.