Upload
berenice-mitchell
View
216
Download
1
Tags:
Embed Size (px)
Citation preview
Brief Introduction
• Perl was developed by Larry Walls, because the available scripting languages didn’t have the ability to manipulate text that he required.
• Because his area of study was in languages, he developed Perl to be context-based just like natural human languages.
• It is so versatile and flexible that it is called the “Swiss army knife” of scripting languages.
Brief Introduction
• It is part of the LAMP solution for web serversL – Linux (free operating system)A – Apache (free webserver)M – MySQL (free database server)P – Perl, Python, PHP (scripting
languages)
Brief Introduction
• Perl is not an acronym. It was actually named after the “Pearl of great value” from a parable in the Bible.
• Perl can be called a backronym, since the expansions came after it got its name.
“Practical Extraction and Report Language”“Pathologically Eclectic Rubbish Lister”
Perl Installation
• Linux - Perl comes with UNIX/Linux• Windows- Download ActivePerl from the
following website:www.activestate.com
• Modules can be downloaded from CPAN (Comprehensive Perl Archive Network)
www.cpan.org
Programming Tips
• Add code a few lines at a time.• Test frequently.• Test ideas in a smaller script before adding to
your larger script.• Should you have problems with your script,
review that code that you added, or go back to previously saved version.
• Indent blocks of code• Add comments to your code.
Hello World
#!/usr/bin/perl# Hello World for Linux/UNIX# hello_world.pl
Print “Hello World\n”;
• Linux Example
Variable Types
• Scalar$NAME=“Sam”;$NUMBER=1;$NUMBER[1]=2;
• Array@NUMBERS=(30,35,40);@ROCKS=(“granite”,”limestone”,”quartz”);@ANIMALS=(qw<monkey
dogcat>);
Input/Output
• File Input
open (INFILE, “students.txt”);$STUDENT1=<INFILE>;chomp($STUDENT1);$STUDENT2=<INFILE>)chomp($STUDENT2);print “$STUDENT1\n”;print “$STUDENT2\n”;close INFILE;
Input/Output
• File Input
open (INFILE, “students.txt”);@STUDENTS=<INFILE>;chomp(@STUDENTS);print “$STUDENT[0]\n”;print “$STUDENT[1]\n”;close INFILE;
Input/Output
• File Output – Creates a fresh file
open(OUTFILE, “> students.txt”)print OUTFILE “Mickey Mouse\n”;close OUTFILE;
Input/Output
• File Output – Adds lines to a file
open(OUTFILE, “>> students.txt”)print OUTFILE “Donald Duck\n”;close OUTFILE;
Arithmetic Operators• Addition
$ANSWER=$NUMBER1+$NUMBER2;$ANSWER=+$NUMBER;
• Subtraction$ANSWER=$NUMBER1-$NUMBER2;$ANSWER=-$NUMBER;
• Multiplication$ANSWER=$NUMBER1*$NUMBER2;$ANSWER=*$NUMBER;
• Division$ANSWER=$NUMBER1/$NUMBER2;$ANSWER=/$NUMBER;
• Increment$ANSWER++;
• Decrement$ANSWER--;
String Operations
• Adding strings together$STRING=“Jack and Jill”.“ went up the hill.”;$FULLNAME=$FIRSTNAME.“ ”.$LASTNAME;
• Split($FIRSTNAME,$LASTNAME)=split(/ /,$FULLNAME);
• Join$FULLNAME=join(/”, “/,$LASTNAME,$FIRSTNAME);
• Substring$STRING1=substr($STRING2,$initial_position,$length);
• Index$location=substr($FULLNAME, “,”);
Numeric Logic Operators
• Equal $NUMBER1 == $NUMBER2
• Greater than$NUMBER1 > $NUMBER2
• Greater than or equal to$NUMBER1 >= $NUMBER2
• Less than$NUMBER1 < $NUMBER2
• Less than or equal to$NUMBER1 <= $NUMBER2
String Logic Operators
• Equal to$STRING1 eq $STRING2
• Greater than$STRING1 gt $STRING2
• Less than$STRING1 lt $STRING2
• Contains$STRING1 =~ $STRING2
Flow Control• If Statement
if ($NUMBER1 > $NUMBER2){print “It’s true.\n”;
}• If-else Statement
if ($NUMBER1 > $NUMBER2){print “$NUMBER1 is greater than $NUMBER2.\n”;
} else {print “$NUMBER1 is less than or equal to $NUMBER2.\n”;
}• If-elsif Statement
if ($NUMBER1 > $NUMBER2){print “$NUMBER1 is greater than $NUMBER2.\n”;
}elsif ($NUMBER1 < $NUMBER2){print “$NUMBER1 is less than $NUMBER2.\n”;
}
Flow Control
• Foreach Loop
foreach $rock(@rocks){print “$rock\n”;
}
foreach $i (1..10){print “$i \n”;
}
Using a Loop for File Input
open (INFILE, “students.txt”);while ($LINE=<INFILE>){
chomp($LINE);print “$LINE\n”;
}close INFILE;
Flow Control• Subroutines
&sub1;sub2();
sub sub1{print “This is sub1.\n”;}
sub sub2{print “This is sub2.\n”;}