Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Introduction to Perl William G. Dishman CUR/516 November 5, 2014."— Presentation transcript:

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

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

3 Brief Introduction It is part of the LAMP solution for web servers L– Linux (free operating system) A – Apache (free webserver) M– MySQL (free database server) P– Perl, Python, PHP (scripting languages)

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

5 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

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

7 Programming Tips View script created for work.

8 Hello World # Hello World for Windows # hello_world.pl print “Hello World\n”; Windows Example

9 Hello World #!/usr/bin/perl # Hello World for Linux/UNIX # hello_world.pl Print “Hello World\n”; Linux Example

10 Variable Types Scalar $NAME=“Sam”; $NUMBER=1; $NUMBER[1]=2; Array @NUMBERS=(30,35,40); @ROCKS=(“granite”,”limestone”,”quartz”); @ANIMALS=(qw<monkey dog cat>);

11 Input/Output Input $VARIABLE= ; @VARIABLE=`dir`; Output print “$VARIABLE\n”;

12 Input/Output File Input open (INFILE, “students.txt”); $STUDENT1= ; chomp($STUDENT1); $STUDENT2= ) chomp($STUDENT2); print “$STUDENT1\n”; print “$STUDENT2\n”; close INFILE;

13 Input/Output File Input open (INFILE, “students.txt”); @STUDENTS= ; chomp(@STUDENTS); print “$STUDENT[0]\n”; print “$STUDENT[1]\n”; close INFILE;

14 Input/Output File Output – Creates a fresh file open(OUTFILE, “> students.txt”) print OUTFILE “Mickey Mouse\n”; close OUTFILE;

15 Input/Output File Output – Adds lines to a file open(OUTFILE, “>> students.txt”) print OUTFILE “Donald Duck\n”; close OUTFILE;

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

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

18 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

19 String Logic Operators Equal to $STRING1 eq $STRING2 Greater than $STRING1 gt $STRING2 Less than $STRING1 lt $STRING2 Contains $STRING1 =~ $STRING2

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

21 Flow Control Foreach Loop foreach $rock(@rocks){ print “$rock\n”; } foreach $i (1..10){ print “$i \n”; }

22 Flow Control While Loop $NUMBER=1; While ($NUMBER < 5){ print “$NUMBER \n”; $NUMBER++; }

23 Flow Control Do-While Loop $NUMBER=1; do{ print “$NUMBER \n”; $NUMBER++; while ($NUMBER < 5)

24 Using a Loop for File Input open (INFILE, “students.txt”); while ($LINE= ){ chomp($LINE); print “$LINE\n”; } close INFILE;

25 Flow Control Subroutines &sub1; sub2(); sub sub1 { print “This is sub1.\n”; } sub sub2 { print “This is sub2.\n”; }

26 Reference Schwartz, 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


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

Similar presentations


Ads by Google