Top Banner
Introduction to Perl William G. Dishman CUR/516 November 5, 2014
26

Introduction to Perl William G. Dishman CUR/516 November 5, 2014.

Dec 13, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Introduction to Perl William G. Dishman CUR/516 November 5, 2014.

Introduction to Perl

William G. DishmanCUR/516

November 5, 2014

Page 2: Introduction to Perl William G. Dishman CUR/516 November 5, 2014.

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.

Page 3: Introduction to Perl William G. Dishman CUR/516 November 5, 2014.

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)

Page 4: Introduction to Perl William G. Dishman CUR/516 November 5, 2014.

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”

Page 5: Introduction to Perl William G. Dishman CUR/516 November 5, 2014.

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

Page 6: Introduction to Perl William G. Dishman CUR/516 November 5, 2014.

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.

Page 7: Introduction to Perl William G. Dishman CUR/516 November 5, 2014.

Programming Tips

• View script created for work.

Page 8: Introduction to Perl William G. Dishman CUR/516 November 5, 2014.

Hello World

# Hello World for Windows# hello_world.pl

print “Hello World\n”;

• Windows Example

Page 9: Introduction to Perl William G. Dishman CUR/516 November 5, 2014.

Hello World

#!/usr/bin/perl# Hello World for Linux/UNIX# hello_world.pl

Print “Hello World\n”;

• Linux Example

Page 10: Introduction to Perl William G. Dishman CUR/516 November 5, 2014.

Variable Types

• Scalar$NAME=“Sam”;$NUMBER=1;$NUMBER[1]=2;

• Array@NUMBERS=(30,35,40);@ROCKS=(“granite”,”limestone”,”quartz”);@ANIMALS=(qw<monkey

dogcat>);

Page 11: Introduction to Perl William G. Dishman CUR/516 November 5, 2014.

Input/Output

• Input$VARIABLE=<STDIN>;@VARIABLE=`dir`;

• Outputprint “$VARIABLE\n”;

Page 12: Introduction to Perl William G. Dishman CUR/516 November 5, 2014.

Input/Output

• File Input

open (INFILE, “students.txt”);$STUDENT1=<INFILE>;chomp($STUDENT1);$STUDENT2=<INFILE>)chomp($STUDENT2);print “$STUDENT1\n”;print “$STUDENT2\n”;close INFILE;

Page 13: Introduction to Perl William G. Dishman CUR/516 November 5, 2014.

Input/Output

• File Input

open (INFILE, “students.txt”);@STUDENTS=<INFILE>;chomp(@STUDENTS);print “$STUDENT[0]\n”;print “$STUDENT[1]\n”;close INFILE;

Page 14: Introduction to Perl William G. Dishman CUR/516 November 5, 2014.

Input/Output

• File Output – Creates a fresh file

open(OUTFILE, “> students.txt”)print OUTFILE “Mickey Mouse\n”;close OUTFILE;

Page 15: Introduction to Perl William G. Dishman CUR/516 November 5, 2014.

Input/Output

• File Output – Adds lines to a file

open(OUTFILE, “>> students.txt”)print OUTFILE “Donald Duck\n”;close OUTFILE;

Page 16: Introduction to Perl William G. Dishman CUR/516 November 5, 2014.

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

Page 17: Introduction to Perl William G. Dishman CUR/516 November 5, 2014.

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, “,”);

Page 18: Introduction to Perl William G. Dishman CUR/516 November 5, 2014.

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

Page 19: Introduction to Perl William G. Dishman CUR/516 November 5, 2014.

String Logic Operators

• Equal to$STRING1 eq $STRING2

• Greater than$STRING1 gt $STRING2

• Less than$STRING1 lt $STRING2

• Contains$STRING1 =~ $STRING2

Page 20: Introduction to Perl William G. Dishman CUR/516 November 5, 2014.

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

}

Page 21: Introduction to Perl William G. Dishman CUR/516 November 5, 2014.

Flow Control

• Foreach Loop

foreach $rock(@rocks){print “$rock\n”;

}

foreach $i (1..10){print “$i \n”;

}

Page 22: Introduction to Perl William G. Dishman CUR/516 November 5, 2014.

Flow Control

• While Loop

$NUMBER=1;While ($NUMBER < 5){

print “$NUMBER \n”;$NUMBER++;

}

Page 23: Introduction to Perl William G. Dishman CUR/516 November 5, 2014.

Flow Control

• Do-While Loop

$NUMBER=1;do{

print “$NUMBER \n”;$NUMBER++;

while ($NUMBER < 5)

Page 24: Introduction to Perl William G. Dishman CUR/516 November 5, 2014.

Using a Loop for File Input

open (INFILE, “students.txt”);while ($LINE=<INFILE>){

chomp($LINE);print “$LINE\n”;

}close INFILE;

Page 25: Introduction to Perl William G. Dishman CUR/516 November 5, 2014.

Flow Control• Subroutines

&sub1;sub2();

sub sub1{print “This is sub1.\n”;}

sub sub2{print “This is sub2.\n”;}

Page 26: Introduction to Perl William G. Dishman CUR/516 November 5, 2014.

ReferenceSchwartz, R. L., Phoenix, T., & Foy, B. D. (2009). Learning Perl (5th ed.). Sebastopol, CA:

O'Reilly.YouTube. (2011, June 13). Larry Wall: Why Perl Is Like a Human Language. Retrieved

from https://www.youtube.com/watch?v=ju1IMxGSuNE