Sie sind auf Seite 1von 6

PERL

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 :

It’s free and easy to write scripts


Interpreted language – no compilation required
Portable – available on different platforms
Fast text processing and file handling capability
Too slow in performing calculations
Huge number of pre-written scripts available for most common tasks

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.

Arithmetic operations on scalars:

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.

Perl supports all of the standard mathematical operators you'd expect.

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

Concatenation and Repetition


The dot operator ‘ . ‘ is used to concatenate two strings.

my $name = "Bob" . " " . "Smith";

You can also add to the end of an existing string using the same operator

my $name = "Bob ";


$name = $name . "Smith";

The ‘ x ‘ is used as repetition operator. For example,

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.

$mixed = "cASe is ALL oVeR The PlaCE";


print lc($mixed); # All lower case, but $mixed unchanged
$mixed = uc($mixed);
print $mixed; # All upper case
Reverse
The reverse function reverses a scalar. As with uc/lc it doesn't change the scalar itself but
returns a reversed version of it for you to play with.

$string = "\n?siht daer uoy nac";


$reversed = reverse $string;
print $reversed;
Substr
The substr function allows you to extract (and optionally replace) a substring. Syntax is

substr ([string],[offset],[length],[replacement])

String is the scalar you want to extract the substring from


Offset is the position in the string you want to start from (counting from 0). If you want to
be clever you can use a negative offset to start counting back from the end of the string.
Length is the length of substring you want to extract

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,

$text = "This is my text";


chop ($line);
After chop is called, the value of $text becomes

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,

print " enter value for";


$var2=<STDIN>; # enter 99
Print “ enter another value”;
$var2=<STDIN> ; #enter 88;
chomp ($var1);
chomp ($var2);
print “difference of $var1 and $var2 :”;
$var3=$var1 - $var2;
Print “$var3”;

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";

$num = hex ($myhexstring);


This call to hex assigns the decimal equivalent of 1ff to $num, which means that the value
of $num is now 511. The value stored in $myhexstring is not changed.
The value passed to the string can contain either uppercase or lowercase letters (provided
the letters are between a and f, inclusive). This value can be the result of an expression, as
follows:
$num = hex ("f" x 2);
Here, the expression "f" x 2 is equivalent to ff, which is converted to 255 by hex.

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:

Operator Example Description


== $x == $ y equal (numeric comparison)
< $x < $y less than; true if x is less than y
> $x < $y greater than; true if x is greater than y
<= $x <= $y less than or equal to; true if x is less than or equal to y
>= $x >= $y Greater than or equal to; true if x is greater than or
equal to y
!= $x != $y not equal (numeric comparison
<=> $x < = > $y general comparison; value is -1 if $x is less than $y ,
0 if $x equals $y, and 1 if $x is greater than $y.

Operators for Strings:


Operator Example Description
lt $x lt $y less than; true if x is less than y
gt $x gt $y greater than; true if x is greater than y
le $x le $y less than or equal to; true if x is less than or equal
to y
ge $x ge $y greater than or equal to; true if x is greater than or
equal to y
eq $x eq $y equal (stringwise comparison); true if x is equal to
y
ne $x ne $y not equal (stringwise comparison); true if x is not
equal to y
cmp $x cmp $y general comparison; value is -1 if $x is less than
$y , 0 if $x equals $y, and 1 if $x is greater than
$y.

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].

Das könnte Ihnen auch gefallen