Beruflich Dokumente
Kultur Dokumente
Introduction
Perl is a scripting language developed by Larry Wall in 1987. Perl borrows heavily from
other languages such as C and awk and is especially useful for processing text, generating
reports, and handling Common Gateway Interface (CGI) requests submitted via Web
browsers.
Features of Perl :
The programs written in Perl are called Perl scripts. These scripts may have a .pl extension.
It is a common practice to include the path of the Perl installation at the beginning of the
Perl script within a comment. The ‘#’ represents the beginning of a comment. Every
statement in Perl must end with a semicolon.
Here's a simple Perl script:
#!/usr/bin/perl
print "Hello Perl. \n";
Data Storage
The Perl data structure which is used to hold a single item of data, such as a piece of text,
or a number, is called a scalar. A variable which can store a piece of scalar data is called a
scalar variable. There are three built-in data types in Perl, they are Scalar ($), Arrays (@)
and Hashes (%).
Scalar variables in Perl have names which start with a dollar sign, and then have a name
which consists of letters, numbers and the underscore character. By convention they are
usually put in all lowercase. Examples of typical variable names would be;
$val= 5;
$user_name = ‘Perl’;
$password= “Srikanth”
$cmd= `ls`;
Perl doesn't require you to declare a type for your variables. Perl figures out by context
whether the value should be treated as a number or a character string and will even perform
character-to-numeric value conversions when necessary.
Note that there are several distinct ways to use quotation marks in a Perl script. Let's look
at the differences among single quotation marks, double quotation marks, and back ticks.
Single Quotes: Data contained in single quotes is interpreted literally. That means, for
example:
$user_name = ‘Srikanth’;
$var = ‘Good Morning $user_name’;
Print var ; produces Good Morning $user_name as output
Double Quotes:
Data contained in double quotes are said to be "interpolated". There are two kinds of
substitution which happen in double quotes, variable interpolation and the expansion of
special characters.
$user_name = ‘Srikanth’;
$var = “Hi,\nGood Morning\t\t$user_name”;
Print var ; produces
Hi,
Good Morning Srikanth as output
Back tick :
Data contained in back-tick are treated as system commands. For example
$ var =`ls`;
Print “the directory listing is as follows: \n $var”; yields the directory listing.
Perl does not have a separate data type for integers and floating point numbers All of these
data types are just scalars as far as Perl is concerned. In fact, behind the scenes, Perl does
treat these data types differently and automatically converts them depending upon the
usage of a variable.
Operation Example
Addition $x = $y + $z
Subtraction $x = $y - $z
Multiplication $x = $y * $z
Division $x = $y / $z
Exponentiation $x = $y ** $z
SquareRoot $x = sqrt($y)
Modulus $x = $y%$z
Increment ++$x or $x++ or $x+=1
Decrement --$x or $x-— or $x-=1
You can also add to the end of an existing string using the same operator
print "0"x10; Prints 10 zeros. This will also work with multi-character strings:
print "deadly sin"x7; yields => deadly sin deadly sin deadly sin deadly sin deadly sin
deadly sin deadly sin
Functions:
Functions such as print take one or more arguments passed as a list. A function may return
no value, a single value, or a list. You can enclose the argument list in parentheses.
Print
Print takes either a single scalar or a list (multiple scalars separated by commas) and by
default prints them to stdout.
Length
The length function returns the length of the scalar
$length = length("abcde");
print $length; # prints 5
Uc / lc
The functions uc and lc can be used to convert a string into upper or lower case. They do
not alter the original string, but instead return the adjusted string, which can be assigned to
a new variable, or back to the original one.
substr ([string],[offset],[length],[replacement])
Replacement is an (optional) scalar you want to substitute for the substring you specified.
Unlike reverse and uc/lc, this replacement does affect the original string.
$original = "hippopotamus";
print substr($original,0,5); # hippo
print substr($original,-3,3); # mus
substr($original,5,4,"jelly");
print $original; # hippojellymus
Chop
The chop function deletes the character at the right end of the line of text. For example,
This is my tex
Chomp
Unlike the chop function chomp does not deletes the character at the right end of the text
but looks for new line char and deletes the “\n” char from the string. This is useful in
accepting numeric input from the keyboard. For example,
After chomp is called, the type of $var1, $var2 is converted into integers
Join
The join function takes the elements of a list and converts them into a single character
string.
The syntax for the join function is : join (joinstr, list);
joinstr is the character string that is to be used to glue the elements of list together.
For example: @list = ("Here", "is", "a", "list");
$newstr = join ("::", @list);
After join is called, the value stored in $newstr becomes the following string:
Here::is::a::list
The join string, :: in this case, appears between each pair of joined elements. The most
common join string is a single blank space; however, you can use any value as the join
string, including the value resulting from an expression.
hex
The hex function assumes that a character string is a number written in hexadecimal format,
and it converts it into a decimal number.
The syntax for the hex function is : decnum = hex (hexnum);
hexnum is the hexadecimal character string, and decnum is the resulting decimal number.
The following is an example: $myhexstring = "1ff";
Relational Operators:
Perl provides two sets of comparison operators. The first set is for comparing numbers and the
second set is to compare strings.
Operators for Numbers:
Arrays:
Arrays used to store data elements in an orderly fashion to retrieve them based on their
position. Unlike many other languages arrays in Perl are dynamic, which means that
means elements can be added to them or remove from them at our will. We don't need to
say in advance how big they're going to be. Arrays in Perl are denoted by a leading @
symbol for example @array.
In order to identify the elements they are stored in the array with their corresponding index
included in square brackets at the end of the variable name. The $ is used to represent the
index or position of the element in the list. The first element in an array has index 0. So for
example the fifth element in @array would be $array[4].