perl day 1. programming computers know how to execute a sequence of instructions computers know how...

22
Perl Perl Day 1 Day 1

Upload: amberlynn-jones

Post on 04-Jan-2016

218 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Perl Day 1. Programming Computers know how to execute a sequence of instructions Computers know how to execute a sequence of instructions –Instructions

PerlPerl

Day 1Day 1

Page 2: Perl Day 1. Programming Computers know how to execute a sequence of instructions Computers know how to execute a sequence of instructions –Instructions

ProgrammingProgramming

Computers know how to execute a Computers know how to execute a sequence of instructionssequence of instructions– Instructions must be very precise.Instructions must be very precise.– Think of a recipe Think of a recipe

Preheat oven to 400FPreheat oven to 400F Place 1 cup sugar and 1 cup butter into a bowlPlace 1 cup sugar and 1 cup butter into a bowl Blend until creamy with a high speed mixerBlend until creamy with a high speed mixer Add in 1 medium sized egg, 2 cups flour, and 1 Add in 1 medium sized egg, 2 cups flour, and 1

teaspoon baking soda, ½ spoon saltteaspoon baking soda, ½ spoon salt Cut into circular shaped cookies 2” in diamater, place Cut into circular shaped cookies 2” in diamater, place

on a greased cookie sheeton a greased cookie sheet Bake in preheated oven for 6.5 minutes.Bake in preheated oven for 6.5 minutes.

Page 3: Perl Day 1. Programming Computers know how to execute a sequence of instructions Computers know how to execute a sequence of instructions –Instructions

Programming LanguageProgramming Language Computers ultimately speak 1’s and 0’s, but we aren’t good Computers ultimately speak 1’s and 0’s, but we aren’t good

at entering long strings of them.at entering long strings of them.– Thus people came up with higher level languages to send Thus people came up with higher level languages to send

instructions to the CPUinstructions to the CPU This is called Machine or Assembler languageThis is called Machine or Assembler language

– That language is hard to understand, so later people That language is hard to understand, so later people developed higher level languages that are closer to English, developed higher level languages that are closer to English, which “compile” into assembler languagewhich “compile” into assembler language

C, C++ are good examples of thisC, C++ are good examples of this These languages are compiled for a specific processor (Intel/AMD, These languages are compiled for a specific processor (Intel/AMD,

vs PowerPC vs. Spark). Thus a program compiled on an Intel vs PowerPC vs. Spark). Thus a program compiled on an Intel processor won’t work on a PowerPC (Mac).processor won’t work on a PowerPC (Mac).

– To get around this, people developed interpreted languages To get around this, people developed interpreted languages which have “interpreters” that are compiled for each platform, which have “interpreters” that are compiled for each platform, that run a even higher level language.that run a even higher level language.

The cost to this is speedThe cost to this is speed The advantage is portability of code, code written once will execute The advantage is portability of code, code written once will execute

on Mac, Windows, Unix etc.on Mac, Windows, Unix etc.

Page 4: Perl Day 1. Programming Computers know how to execute a sequence of instructions Computers know how to execute a sequence of instructions –Instructions

Where to get perlWhere to get perl

Most Unix machines have it built in.Most Unix machines have it built in.– This includes scooby and scooby2 if you This includes scooby and scooby2 if you

have accounts there.have accounts there. For windows and mac:For windows and mac:

– http://www.activestate.com/activeperl/http://www.activestate.com/activeperl/ You’ll use notepad, vi or some other You’ll use notepad, vi or some other

TEXT editor to write your scripts.TEXT editor to write your scripts.– There are also many IDEs you can use, There are also many IDEs you can use,

but I’ll leave that up to you to research.but I’ll leave that up to you to research.

Page 5: Perl Day 1. Programming Computers know how to execute a sequence of instructions Computers know how to execute a sequence of instructions –Instructions

She bangs, she bangs…She bangs, she bangs…

All perl programs start with a line called a All perl programs start with a line called a shabang.shabang.

#!C:/perl/bin/perl.exe#!C:/perl/bin/perl.exe If you are on Unix:If you are on Unix:

#!/usr/bin/perl#!/usr/bin/perl Every line should end in a ;Every line should end in a ; If you wish to put a comment in your If you wish to put a comment in your

script, simply start the comment with a #:script, simply start the comment with a #:

#This is a comment#This is a comment

Page 6: Perl Day 1. Programming Computers know how to execute a sequence of instructions Computers know how to execute a sequence of instructions –Instructions

Having the script talk to youHaving the script talk to you

printprint

print(“hello world\n”);print(“hello world\n”); \n\n

– All lines that are printed should end with All lines that are printed should end with a \n inside the quotes. This is to tell perl a \n inside the quotes. This is to tell perl that you want a “new line” (same as that you want a “new line” (same as hitting enter).hitting enter). If you forget to put the \n, sometimes it If you forget to put the \n, sometimes it

won’t print at all.won’t print at all.

Page 7: Perl Day 1. Programming Computers know how to execute a sequence of instructions Computers know how to execute a sequence of instructions –Instructions

What is a variableWhat is a variable Think of a variable as a bucket into which you can Think of a variable as a bucket into which you can

place something.place something.– You can have an unlimited number of buckets. To You can have an unlimited number of buckets. To

create a bucket you simply use it.create a bucket you simply use it.– Buckets have names which must be unique.Buckets have names which must be unique.

They are case sensitive so “a” is different from “A”They are case sensitive so “a” is different from “A”– Buckets should be named so you can tell what’s in them. Buckets should be named so you can tell what’s in them.

This will make your life easier and is mandatory as far This will make your life easier and is mandatory as far as I’m concerned.as I’m concerned.

In Perl variable names are always named $In Perl variable names are always named $[something], @[something], or %[something][something], @[something], or %[something]– $ signifies a Scalar$ signifies a Scalar– @ signifies an Array@ signifies an Array– % signifies a Hash % signifies a Hash

e.g $a, $b, $enda, $this_is_long, $ThisIsLonge.g $a, $b, $enda, $this_is_long, $ThisIsLong

Page 8: Perl Day 1. Programming Computers know how to execute a sequence of instructions Computers know how to execute a sequence of instructions –Instructions

Double Quotes – Double the Double Quotes – Double the funfun

Anytime in perl you use “ “ whatever Anytime in perl you use “ “ whatever is inside the quotes will be is inside the quotes will be interpreted.interpreted.

Anytime you use ‘ ‘ whatever is Anytime you use ‘ ‘ whatever is inside the quotes will be used literly.inside the quotes will be used literly.print(“$x \n”);print(“$x \n”);

This will output the value of $xThis will output the value of $x

print(‘$x \n’);print(‘$x \n’); This will output $x \nThis will output $x \n

Page 9: Perl Day 1. Programming Computers know how to execute a sequence of instructions Computers know how to execute a sequence of instructions –Instructions

ScalarsScalars

A scalar is a variable which holds A scalar is a variable which holds exactly 1 value. exactly 1 value. – It usually contains:It usually contains:

a number, a number, a letter (character), a letter (character), a set of characters (string) a set of characters (string)

e.g.e.g.$NumStudentsInClass=12;$NumStudentsInClass=12;$ClassName=“Learning Perl”;$ClassName=“Learning Perl”;

Page 10: Perl Day 1. Programming Computers know how to execute a sequence of instructions Computers know how to execute a sequence of instructions –Instructions

Buckets of funBuckets of fun

Now that you have a scaler, you can Now that you have a scaler, you can do things with it like print it out:do things with it like print it out:

print(“There are $NumStudentsInClass people in this class\n”);print(“There are $NumStudentsInClass people in this class\n”);

Page 11: Perl Day 1. Programming Computers know how to execute a sequence of instructions Computers know how to execute a sequence of instructions –Instructions

MathMath

If a scalar contains a number, you If a scalar contains a number, you can do math:can do math:

$NumA=7;$NumA=7;

$NumB=10;$NumB=10;

$Num=$NumA+$NumB;$Num=$NumA+$NumB;

Print(“$NumA + $NumB = $Num\n”);Print(“$NumA + $NumB = $Num\n”); Operators include: +, -, / (divide) and Operators include: +, -, / (divide) and

* (multiply)* (multiply)

Page 12: Perl Day 1. Programming Computers know how to execute a sequence of instructions Computers know how to execute a sequence of instructions –Instructions

TextText If your scalar has characters or a string in it, you If your scalar has characters or a string in it, you

add additional text with the . operator:add additional text with the . operator:$Word1=‘Hello’;$Word1=‘Hello’;$Word2=‘World’;$Word2=‘World’;$Words=$Word1.’ ‘.$Word2;$Words=$Word1.’ ‘.$Word2;*or**or*$Words=“$Word1 $Word2”;$Words=“$Word1 $Word2”; You can add additional text to an existing scalar:You can add additional text to an existing scalar:$a = ‘Hello’;$a = ‘Hello’;$a .= ‘ World’;$a .= ‘ World’;print(“$a\n”); #This prints “hello world”print(“$a\n”); #This prints “hello world”

Page 13: Perl Day 1. Programming Computers know how to execute a sequence of instructions Computers know how to execute a sequence of instructions –Instructions

ConditionalsConditionals

Sometimes you only want to do Sometimes you only want to do things if a scalar contains a certain things if a scalar contains a certain value:value:

$Num=105;$Num=105;

if($Num==105)if($Num==105)

{{

print(“Big Number!\n”);print(“Big Number!\n”);

}}

Page 14: Perl Day 1. Programming Computers know how to execute a sequence of instructions Computers know how to execute a sequence of instructions –Instructions

TestsTests

If’s can have the following tests:If’s can have the following tests:== (checks if numbers are equal)== (checks if numbers are equal)

< (checks if numbers are less than)< (checks if numbers are less than)

> (checks if numbers are greater than)> (checks if numbers are greater than)

<= (checks if numbers are less than or equal)<= (checks if numbers are less than or equal)

>= (checks if numbers are greater than or equal)>= (checks if numbers are greater than or equal)

!= (checks if numbers are not equal)!= (checks if numbers are not equal)

eq (checks if characters/strings are identical)eq (checks if characters/strings are identical)

ne (checks if characters/strings are not identical)ne (checks if characters/strings are not identical)

Page 15: Perl Day 1. Programming Computers know how to execute a sequence of instructions Computers know how to execute a sequence of instructions –Instructions

Maybe we need 2 testsMaybe we need 2 tests We can combine tests with and/orWe can combine tests with and/or

– AND: &&AND: &&– OR: ||OR: ||

$Num=100;$Num=100;if(($Num>10) && ($Num<100))if(($Num>10) && ($Num<100)){{ print(“Our numbers is between 10 and 100\n”);print(“Our numbers is between 10 and 100\n”);}}

$Name=“Enda”;$Name=“Enda”;$HeightInFeet=6;$HeightInFeet=6;if(($Name eq “Enda”) || ($Height>5))if(($Name eq “Enda”) || ($Height>5)){{ print(“It’s either Enda, or a tall person\n”);print(“It’s either Enda, or a tall person\n”);}}

Page 16: Perl Day 1. Programming Computers know how to execute a sequence of instructions Computers know how to execute a sequence of instructions –Instructions

What if we want to do one thing if something What if we want to do one thing if something is true, and something else otherwise?is true, and something else otherwise?

Each if can have unlimited number of elsif, and up to 1 else:Each if can have unlimited number of elsif, and up to 1 else:

$Month=3;$Month=3;if($Month==1)if($Month==1){ print(“Jan\n”); }{ print(“Jan\n”); }elsif($Month==2)elsif($Month==2){ print(“Feb\n”); }{ print(“Feb\n”); }elsif($Month==3)elsif($Month==3){ print(“Mar\n”); }{ print(“Mar\n”); }……elsif($Month==12)elsif($Month==12){ print(“Dec\n”); }{ print(“Dec\n”); }elseelse{ print(“Error: Month should be between 1 and 12\n”); }{ print(“Error: Month should be between 1 and 12\n”); }

Page 17: Perl Day 1. Programming Computers know how to execute a sequence of instructions Computers know how to execute a sequence of instructions –Instructions

Additional thing you can Additional thing you can checkcheck

Does this variable exist?Does this variable exist?

if(defined($a))if(defined($a)) What if you want something to What if you want something to

happen if something is NOT truehappen if something is NOT true

if(! (defined($a)))if(! (defined($a)))

Page 18: Perl Day 1. Programming Computers know how to execute a sequence of instructions Computers know how to execute a sequence of instructions –Instructions

More than one thing in a More than one thing in a variablevariable

Crazy talk?Crazy talk?– No, one way to do this is called an Array.No, one way to do this is called an Array.@a=(1,2,3);@a=(1,2,3);– There is no restriction on what the values in the array There is no restriction on what the values in the array

can be, one could be a number, the next a string, the can be, one could be a number, the next a string, the next a number etc.next a number etc.

– When you refer to the entire array you use @ instead of When you refer to the entire array you use @ instead of $$

– Arrays are indexed from 0, so the first element in the Arrays are indexed from 0, so the first element in the array is said to be at “index” 0. The second element is array is said to be at “index” 0. The second element is said to be at “index” 1.said to be at “index” 1.

– When you refer to an individual scalar in the array, you When you refer to an individual scalar in the array, you use $ againuse $ again

– e.g.e.g.@b=@a;@b=@a;print(“$a[0]\n”);print(“$a[0]\n”);

Page 19: Perl Day 1. Programming Computers know how to execute a sequence of instructions Computers know how to execute a sequence of instructions –Instructions

How many things are in the How many things are in the array?array?

You can find out by assigning the You can find out by assigning the Array to a scalar Array to a scalar – Yes, that doesn’t make sense, it’s a Yes, that doesn’t make sense, it’s a

weird perl thing:weird perl thing:

@Months=(‘jan’,’feb’,’mar’,’apr’,’may’,’ju@Months=(‘jan’,’feb’,’mar’,’apr’,’may’,’jun’,’jul’,n’,’jul’,’aug’,’sep’,’oct’,’nov’,’dec’);’aug’,’sep’,’oct’,’nov’,’dec’);

$NumMonths=@Months;$NumMonths=@Months;

print(“There are $NumMonths months\n”);print(“There are $NumMonths months\n”);

Page 20: Perl Day 1. Programming Computers know how to execute a sequence of instructions Computers know how to execute a sequence of instructions –Instructions

Command LineCommand Line Let’s assume you wrote a Perl Script in a file Let’s assume you wrote a Perl Script in a file

called doit.plcalled doit.pl You typically execute it like this:You typically execute it like this:

– C:\perl\bin\perl doit.plC:\perl\bin\perl doit.pl What if you want to pass “arguments” into your What if you want to pass “arguments” into your

script:script:– C:\perl\bin\perl doit.pl 1 2 EndaC:\perl\bin\perl doit.pl 1 2 Enda– Those arguments are automatically stored in an Array Those arguments are automatically stored in an Array

called @ARGVcalled @ARGV Assuming the above command line:Assuming the above command line:

– @ARGV=(1,2,’Enda’);@ARGV=(1,2,’Enda’);– $ARGV[0] now contains 1$ARGV[0] now contains 1– $ARGV[1] now contains 2$ARGV[1] now contains 2– $ARGV[2] now contains ‘Enda’$ARGV[2] now contains ‘Enda’

Page 21: Perl Day 1. Programming Computers know how to execute a sequence of instructions Computers know how to execute a sequence of instructions –Instructions

Doh!Doh!

Often you want to ensure that you got at Often you want to ensure that you got at least 1 argument to your script.least 1 argument to your script.

– If not there may be no reason to continue.If not there may be no reason to continue.– For example, imagine a script that takes one For example, imagine a script that takes one

argument, which should be a number less argument, which should be a number less than 5. It prints out the numbers before that than 5. It prints out the numbers before that number.number.

Sometime there is just no point in Sometime there is just no point in continuing, you should just die:continuing, you should just die:

– die “I asked you to send me a number, you die “I asked you to send me a number, you gave me $ARGV[0]…I can do nothing with gave me $ARGV[0]…I can do nothing with that…giving up”;that…giving up”;

Page 22: Perl Day 1. Programming Computers know how to execute a sequence of instructions Computers know how to execute a sequence of instructions –Instructions

#!C:/Perl/bin/perl.exe#!C:/Perl/bin/perl.exe

$NumArguments=@ARGV;$NumArguments=@ARGV;If($NumArguments<1)If($NumArguments<1){ die “Usage: count.pl NUMBER”; }{ die “Usage: count.pl NUMBER”; }

If($ARGV[0]==5)If($ARGV[0]==5){ print(“1 2 3 4 5\n”); }{ print(“1 2 3 4 5\n”); }elsif($ARGV[0]==4)elsif($ARGV[0]==4){ print(“1 2 3 4\n”); }{ print(“1 2 3 4\n”); }elsif($ARGV[0]==3)elsif($ARGV[0]==3){ print(“1 2 3\n”); }{ print(“1 2 3\n”); }elsif($ARGV[0]==2)elsif($ARGV[0]==2){ print(“1 2\n”); }{ print(“1 2\n”); }elsif($ARGV[0]==1)elsif($ARGV[0]==1){ print(“1\n”); }{ print(“1\n”); }elseelse{ die “Error, you must send me a number between 1 and 5 as { die “Error, you must send me a number between 1 and 5 as

the first argument”; }the first argument”; }

count.pl