Perl
Perl
Comments start with a hash symbol and run to the end of the
line:
# This is a comment
Whitespace is irrelevant:
print "Hello, world";
print $animal;
print "The animal is $animal\n";
print "The square of $answer is ", $answer * $answer, "\n";
$_ which is the "default variable". It's used as the default
argument to a number of functions in Perl, and it's set
implicitly by certain looping constructs.
print; # prints contents of $_ by default
• You can get at lists of keys and values with keys() and values() built-in
functions.
my @fruits = keys %fruit_colors;
my @colors = values %fruit_colors;
•
PERL Conditional Statements
print "Happy Birthday!\n" if ($date == $today);
In this instance, the message will only be printed if the expression evaluates
to a true value.
if ($date == $today) { print "Happy Birthday!\n"; }
for Loops
you can write a loop to iterate 100 times like this:
for ($i=0;$i<100;$i++) { ... }
You can place multiple variables into the expressions using the standard
list operator (the comma):
for ($i=0, $j=0;$i<100;$i++,$j++)
• The last keyword ends the loop entirely, skipping the remaining
statements in the code block, as well as dropping out of the loop. The
last keyword is therefore identical to the break keyword in C and
Shellscript. For example:
while ( )
{
last if ($found);
}
• Would exit the loop if the value of $found was true, whether the end of
the file had actually been reached or not. The continue block is not
executed.
• The redo keyword reexecutes the code block without reevaluating the
conditional statement for the loop. This skips the remainder of the code
block and also the continue block before the main code block is
reexecuted. For example, the following code would read the next line
from a file if the current line terminates with a backslash:
while(<DATA>)
{
if (s#\\$#)
{
$_ .= <DATA>; redo;
}
}
PERL Built-in Operators
• Arithmetic Operators
+ addition - subtraction * multiplication / division
• Numeric Comparison Operators
• == equality != inequality < less than > greater than <= less than or equal
>= greater than or equal
• String Comparison Operators
eq equality ne inequality lt less than gt greater than le less than or equal
ge greater than or equal
• Boolean Logic Operators
&& and || or ! not
• Miscellaneous Operators
= assignment
. string concatenation
x string multiplication
.. range operator (creates a list of numbers)
PERL Files & I/O
Following is the syntax to open file.txt in read-only mode. Here less than <
signe indicates that file has to be opend in read-only mode
open(DATA, "<file.txt");
Here DATA is the file handle which will be used to read the file. Here is the
example which will open a file and will print its content over the screen.
#!/usr/bin/perl
open(DATA, "<file.txt");
while(<DATA>)
{
print "$_"; }
• Open Function
Following is the syntax to open file.txt in writing mode. Here greater than >
signe indicates that file has to be opend in writing mode
open(DATA, ">file.txt");
• You can use O_CREAT to create a new file and O_WRONLY- to open file
in write only mode and O_RDONLY - to open file in read only mode.
• The PERMS argument specifies the file permissions for the file specified
if it has to be created. By default it takes 0x666
• Following is the table which gives possible values of MODE
Value Definition
O_RDWR Read and Write
O_RDONLY Read Only
O_WRONLY Write Only
O_CREAT Create the file
O_APPEND Append the file
O_TRUNC Truncate the file
O_EXCL Stops if file already exists
O_NONBLOCK Non-Blocking usability
• Close Function
To close a filehandle, and therefore disassociate the filehandle from the
corresponding file, you use the close function. This flushes the
filehandle's buffers and closes the system's file descriptor.
close FILEHANDLE
close
If no FILEHANDLE is specified, then it closes the currently selected
filehandle. It returns true only if it could successfully flush the buffers and
close the file.
• close(DATA) || die "Couldn't close file properly";
• Reading and Writing Filehandles
Once you have an open filehandle, you need to be able to read and write
information. There are a number of different ways of reading and writing
data into the file.
The <FILEHANDL> Operator
The main method of reading the information from an open filehandle is the
<FILEHANDLE> operator. In a scalar context it returns a single line from
the filehandle. For example:
#!/usr/bin/perl
print "What is your name?\n";
$name = <STDIN>;
print "Hello $name\n";
When you use the <FILEHANDLE> operator in a list context, it returns a list
of lines from the specified filehandle. For example, to import all the lines
from a file into an array:
#!/usr/bin/perl
open(DATA,"<import.txt") or die "Can't open data";
@lines = <DATA>;
close(DATA);
• getc Function
The getc function returns a single character
from the specified FILEHANDLE, or STDIN if
none is specified:
getc FILEHANDLE
getc
If there was an error, or the filehandle is at end
of file, then undef is returned instead.
• print Function
For all the different methods used for reading information from filehandles,
the main function for writing information back is the print function.
print FILEHANDLE LIST
print LIST
print
The print function prints the evaluated value of LIST to FILEHANDLE, or to
the current output filehandle (STDOUT by default). For example:
print "Hello World!\n";
• Renaming a file
Here is an example which shows how we can rename a file file1.txt to
file2.txt. Assuming file is available in /usr/test directory.
#!/usr/bin/perl
rename ("/usr/test/file1.txt", "/usr/test/file2.txt" );
This function rename takes two arguments and it just rename existing file
• Copying Files
Here is the example which opens an existing file file1.txt and read it line by
line and generate another copy file2.txt
#!/usr/bin/perl
# Open file to read
open(DATA1, "<file1.txt");
# Open new file to write
open(DATA2, ">file2.txt");
# Copy data from one file to another.
while(<DATA1>)
{
print DATA2 $_;
}
close( DATA1 );
close( DATA2 );
• Deleting an exiting file
Here is an example which shows how to delete a file file1.txt
using unlink function.
#!/usr/bin/perl unlink ("/usr/test/file1.txt");
Locating Your Position Within a File
You can use to tell function to know the current position of a
file and seek function to point a particular position inside the
file.
tell Function
The first requirement is to find your position within a file, which
you do using the tell function:
tell FILEHANDLE
tell
This returns the position of the file pointer, in bytes, within
FILEHANDLE if specified, or the current default selected
filehandle if none is specified.
• seek Function
The seek function positions the file pointer to the specified number of
bytes within a file:
seek FILEHANDLE, POSITION, WHENCE
The function uses the fseek system function, and you have the same ability
to position relative to three different points: the start, the end, and the
current position. You do this by specifying a value for WHENCE.
Zero sets the positioning relative to the start of the file. For example, the
line sets the file pointer to the 256th byte in the file.
seek DATA, 256, 0;
PERL Regular Expressions
• A regular expression is a string of characters that define the pattern or
patterns you are viewing. The syntax of regular expressions in Perl is
very similar to what you will find within other regular
expression.supporting programs, such as sed, grep, and awk.
• The basic method for applying a regular expression is to use the pattern
binding operators =~ and !~. The first operator is a test and assignment
operator.
The m// actually works in the same fashion as the q// operator series.you
can use any combination of naturally matching characters to act as
delimiters for the expression. For example, m{}, m(), and m>< are all
valid.
• You can omit the m from m// if the delimiters are forward slashes, but for
all other delimiters you must use the m prefix.
• Note that the entire match expression.that is the expression on the left of
=~ or !~ and the match operator, returns true (in a scalar context) if the
expression matches. Therefore the statement:
$true = ($foo =~ m/foo/);
Will set $true to 1 if $foo matches the regex, or 0 if the match fails.
• Another example:
#/user/bin/perl
$string = 'The cat sat on the mat';
$string =~ s/cat/dog/;
print "Final Result is $string\n";
This will produce following result
The dog sat on the mat
• Translation
Translation is similar, but not identical, to the principles of substitution, but
unlike substitution, translation (or transliteration) does not use regular
expressions for its search on replacement values. The translation
operators are:
tr/SEARCHLIST/REPLACEMENTLIST/cds
y/SEARCHLIST/REPLACEMENTLIST/cds
The translation replaces all occurrences of the characters in SEARCHLIST
with the corresponding characters in REPLACEMENTLIST. For example,
using the "The cat sat on the mat." string we have been using in this
chapter:
#/user/bin/perl
$string = 'The cat sat on the mat';
$string =~ tr/a/o/;
print "$string\n";
• As well as this direct method, matched groups are also available within
the special $x variables, where x is the number of the group within the
regular expression. We could therefore rewrite the preceding example as
follows:
$time =~ m/(\d+):(\d+):(\d+)/;
my ($hours, $minutes, $seconds) = ($1, $2, $3);
• When groups are used in substitution expressions, the $x syntax can be used in
the replacement text. Thus, we could reformat a date string using this:
#!/usr/bin/perl
$date = '03/26/1999';
$date =~ s#(\d+)/(\d+)/(\d+)#$3/$1/$2#;
print "$date";
This will produce following result
1999/03/26
• Using the \G Assertion
The \G assertion allows you to continue searching from the point where the
last match occurred.
For example, in the following code we have used \G so that we can
search to the correct position and then extract some information, without
having to create a more complex, single regular expression:
#!/usr/bin/perl
$string = "The time is: 12:31:02 on 4/12/00";
$string =~ /:\s+/g;
($time) = ($string =~ /\G(\d+:\d+:\d+)/);
$string =~ /.+\s+/g;
($date) = ($string =~ m{\G(\d+/\d+/\d+)});
print "Time: $time, Date: $date\n";
This will produce following result
Time: 12:31:02, Date: 4/12/00