Presentation is loading. Please wait.

Presentation is loading. Please wait.

Perl By Gabe and Ted. History Perl was created by Larry Wall while working at NASA’s Jet Propulsion Labs. Larry Wall also created patch which is in widespread.

Similar presentations


Presentation on theme: "Perl By Gabe and Ted. History Perl was created by Larry Wall while working at NASA’s Jet Propulsion Labs. Larry Wall also created patch which is in widespread."— Presentation transcript:

1 Perl By Gabe and Ted

2 History Perl was created by Larry Wall while working at NASA’s Jet Propulsion Labs. Larry Wall also created patch which is in widespread use. Perl pulled together the best features of several languages such as sed, awk, C, Pascal, Basic, UNIX, and English. Version 1 of Perl was distributed on December 18, 1987.

3 Background Perl is a lot like English and was designed to be easy for humans to write, rather than easy for computers. Perl is very portable. It is available for a huge variety of operating systems. Perl is a multi paradigm language because it is functional, procedural, and object oriented.

4 Comments and pods Comments are preceded with a #. Pods start with a =head1 heading Followed by a body And ending with =cut Ex. =Fun1 description This function is sweet. =cut

5 Printing Printing uses the print keyword. \n prints a new line. Print statements can be separated with commas. Like all statements they have to end with a ;. Ex. print 10; print “Hello world.\n”; print “Hello”, “\n”, “world.\n”;

6 Scalars Scalars are single values such as numbers or strings. Scalars use dollar signs to assign variables. A scalar is weakly typed, and can be changed on the fly. Integers –$tickets= 34; Floating point numbers –$price= 2.99; Strings –$name= “Bob”; –$name = 1337;

7 Arrays Arrays (Lists)- @gifts = ( “ipod”, “car”, “dog”); –print $gifts[0]=“ipod” @list = (5,4,3,2,1); –print $list[3]= 2 Associative Arrays (Hashes)- % Hometowns = (Nick => “Chicago”, Tom => “Detroit”, Ed => “Boston”, Sarah => “Pittsburgh”); –print $Hometowns{Ed}=“Boston”

8 Numbers Perl understands numbers in many different forms. We can express numbers as binary with a 0b in front. As hex with a 0x in front. And as a octal with a 0b in front. 255 = 0377 = 0b11111111 = 0xFF

9 Operators Addition, Subtraction, Multiplication, Division, Modulo, Exponent –print 10 + 14 = 24; –print 3 * 3 = 9; –print 2**5 = 32; –print 12 % 5 = 2; String operators $Word1=“snow”; $full_string=$Word1. “storm”; –print $full_string = snowstorm;

10 Input To get input use Must run chomp function after input. ex. $name = ; chomp $name; Chomp removes last character if is it a new line character. Chop removes last character regardless of what character it is.

11 Comparing Strings It is possible to compare strings. gt - ($a gt $b) = (“a” > “b”) lt - ($c lt $e) = (“c” < “e”) ge - ($d ge $d) = (“d” >= “d”) le - ($f le $g) = (“f ” <= “g”) eq - ($happy eq $sad) = (“happy” == “sad”) ne – ($r ne $p) = ( “r” != “p” )

12 Conditionals Conditionals are set up as in C++, always need curly braces. Else if is elsif. Can link equality statements with || and && like in C++. Can also use ! to take the opposite of something. $day = ; chomp $day; $mon = “Monday”; if ($day eq $mon){ print “Today is Monday”; } elsif ($day eq “Tuesday”){ print “Today is Tuesday”; } else { print “Today is $day”; }

13 Conditionals unless statement The opposite of an if statement. unless ($t) { Print “$t is not true”; } unless ($dest eq $home){ print “I’m not going home”; } if ($dest ne $home){ print “I’m not going home”; }

14 Loops While loops are also similar to C++. Unless loops are the opposite of while loops. While loop while ($num != 10){ print $num; $num++; } Until loop until ($num == 10){ Print $num; $num++; }

15 For loops Foreach loops are used to loop through lists and arrays. For loop for ($i=1;$i <= 10; $i++){ print 2 ** $i; } foreach loop foreach $number (3.. 15){ print “the number is: $number \n”; }

16 Do while, do until Test is done after the action. do {action} while/until (cond); $i = 1; do{ print “i = $i\n”; $i++; } while ($i <= 10);

17 Practice problems To use perl on cerebro, type “perl programname.pl” Make a program that will take in first name and last name separately and concatenates them together. Make a program that will take in name and birthday.


Download ppt "Perl By Gabe and Ted. History Perl was created by Larry Wall while working at NASA’s Jet Propulsion Labs. Larry Wall also created patch which is in widespread."

Similar presentations


Ads by Google