Perl
Perl
Perl
Topics Covered
- History and Introduction
- Variables
- Control Structures
- Regular Expression
- File Handling
Introduction
- Practical Extraction and Reporting Language.
- invented by Larry Wall in 1980’s.
- treated as a generic programming language.
- Interpreted language.
- Available as a free Software
Relevance
- Data manipulation
- Glue language : easy to migrate from traditional UNIX tools,tying
together systems & interfaces.
- CGI
- Quick coding
- Portability
Perl program
- Statements:
• they end with a semi colon
• curly braces are required in complex statements
• need not put a wrapping character \ for long statements.
- Comments:
• Anything in a line of Perl code that follows a # sign is a comment.
To run a Perl program
-Type in the example program using a text editor, and save it
as .pl or .cgi
-make sure the file is executable by using the command
chmod u+x progname
-to run the program, just type any of the following at the
prompt.
perl progname
./progname
- You can always run the program with warnings using the
command
perl -w progname
#!/usr/bin/perl.
Numbers
Ternary operator
(expr) ? Statement1 : statement2 ;
Perl converts strings to numbers transparently whenever it's
needed as in :
$a = $b; # Assign $b to $a
$a += $b; # Add $b to $a
$a -= $b; # Subtract $b from $a
$a .= $b; # Append $b onto $a
- push(@food, "eggs");
#which pushes eggs onto the end of the array @food.
- To remove the last item from a list and return it use the pop
function:
$grub = pop(@food); # Now $grub = "eels"
# @food now has two elements.
- If an array doesn't exist, by the way, you'll create it when you
try to assign a value to one of its elements.
$winter_months[0] = "December";
# Visit each item in turn and call it $morsel and prints the lines
given with the print command.
- Testing
$a == $b # Is $a numerically equal to $b?
# Beware: Don't use the = operator.
$a != $b # Is $a numerically unequal to $b?
$a eq $b # Is $a string-equal to $b?
$a ne $b # Is $a string-unequal to $b?
- You can also use logical and, or and not:
[qjk] # Either q or j or k
[^qjk] # Neither q nor j nor k
[a-z] # Anything from a to z inclusive
[^a-z] # No lower case letters
[a-zA-Z] # Any letter
[a-z]+ # Any non-zero sequence of lower case letters
Regular Expression Operators
- Match a string
# Shorthand form uses // to quote the regular expression
$Text =~ /search words/;
$a = "Welcome to Java!\n";
substr($a, 11, 4) = "Perl"; # $a is now "Welcome to
Perl!\n";
- The join() function
It takes a list of strings and attaches them together with a
specified string between each element, which may be an
empty string:
@a = ("Hello.", "Welcome", "Perl!\n");
$a = join(' ', @a); # "Hello. Welcome Perl!\n";
$b = join(' and ', @a); # "Hello. and Welcome and Perl!\n";
$c = join('', @a); # "Hello.WelcomePerl!\n";
File handling
- Open File.
- Read an opened File.
- Write data into the file.
- Close file.
Open & Read File
- Perl creates a filehandle for you by using the open()
function, which takes two arguments: the filehandle you
want to create and the file you want to work with. as:
open (LOGFILE, "log.txt");
#This statement will only show you the words Logfile is open!
if the open() succeeds
Subroutine
- Perl allows the user to define their own functions, called
subroutines. They may be placed anywhere in your
program but it's probably best to put them all at the
beginning or all at the end. A subroutine has the form
sub mysubroutine
{ print "Not a very interesting routine\n";
print "This does the same thing every time\n"; }
-All of the following will work to call this subroutine. Notice that
a subroutine is called with an & character in front of the
name:
&mysubroutine; # Call the subroutine
&mysubroutine($_); # Call it with a parameter
&mysubroutine(1+2, $_); # Call it with two parameters
Returning values
- Result of a subroutine is always the last thing evaluated.
sub maximum
{
if ($_[0] > $_[1])
{ $_[0]; }
else
{ $_[1]; }
}
$biggest = &maximise(37, 24); # Now $biggest is 37
References
- http://en.wikipedia.org/
- http://www.perl.com/
Thank You