1 / 22

Perl

Perl. Day 1. Programming. Computers know how to execute a sequence of instructions Instructions must be very precise. Think of a recipe Preheat oven to 400F Place 1 cup sugar and 1 cup butter into a bowl Blend until creamy with a high speed mixer

luana
Download Presentation

Perl

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Perl Day 1

  2. Programming • Computers know how to execute a sequence of instructions • Instructions must be very precise. • Think of a recipe • Preheat oven to 400F • Place 1 cup sugar and 1 cup butter into a bowl • Blend until creamy with a high speed mixer • Add in 1 medium sized egg, 2 cups flour, and 1 teaspoon baking soda, ½ spoon salt • Cut into circular shaped cookies 2” in diamater, place on a greased cookie sheet • Bake in preheated oven for 6.5 minutes.

  3. Programming Language • Computers ultimately speak 1’s and 0’s, but we aren’t good at entering long strings of them. • Thus people came up with higher level languages to send instructions to the CPU • This is called Machine or Assembler language • That language is hard to understand, so later people developed higher level languages that are closer to English, which “compile” into assembler language • C, C++ are good examples of this • These languages are compiled for a specific processor (Intel/AMD, vs PowerPC vs. Spark). Thus a program compiled on an Intel processor won’t work on a PowerPC (Mac). • To get around this, people developed interpreted languages which have “interpreters” that are compiled for each platform, that run a even higher level language. • The cost to this is speed • The advantage is portability of code, code written once will execute on Mac, Windows, Unix etc.

  4. Where to get perl • Most Unix machines have it built in. • This includes scooby and scooby2 if you have accounts there. • For windows and mac: • http://www.activestate.com/activeperl/ • You’ll use notepad, vi or some other TEXT editor to write your scripts. • There are also many IDEs you can use, but I’ll leave that up to you to research.

  5. She bangs, she bangs… • All perl programs start with a line called a shabang. #!C:/perl/bin/perl.exe • If you are on Unix: #!/usr/bin/perl • Every line should end in a ; • If you wish to put a comment in your script, simply start the comment with a #: #This is a comment

  6. Having the script talk to you • print print(“hello world\n”); • \n • All lines that are printed should end with a \n inside the quotes. This is to tell perl that you want a “new line” (same as hitting enter). • If you forget to put the \n, sometimes it won’t print at all.

  7. What is a variable • Think of a variable as a bucket into which you can place something. • You can have an unlimited number of buckets. To create a bucket you simply use it. • Buckets have names which must be unique. • They are case sensitive so “a” is different from “A” • Buckets should be named so you can tell what’s in them. This will make your life easier and is mandatory as far as I’m concerned. • In Perl variable names are always named $[something], @[something], or %[something] • $ signifies a Scalar • @ signifies an Array • % signifies a Hash • e.g $a, $b, $enda, $this_is_long, $ThisIsLong

  8. Double Quotes – Double the fun • Anytime in perl you use “ “ whatever is inside the quotes will be interpreted. • Anytime you use ‘ ‘ whatever is inside the quotes will be used literly. print(“$x \n”); • This will output the value of $x print(‘$x \n’); • This will output $x \n

  9. Scalars • A scalar is a variable which holds exactly 1 value. • It usually contains: • a number, • a letter (character), • a set of characters (string) • e.g. $NumStudentsInClass=12; $ClassName=“Learning Perl”;

  10. Buckets of fun • Now that you have a scaler, you can do things with it like print it out: print(“There are $NumStudentsInClass people in this class\n”);

  11. Math • If a scalar contains a number, you can do math: $NumA=7; $NumB=10; $Num=$NumA+$NumB; Print(“$NumA + $NumB = $Num\n”); • Operators include: +, -, / (divide) and * (multiply)

  12. Text • If your scalar has characters or a string in it, you add additional text with the . operator: $Word1=‘Hello’; $Word2=‘World’; $Words=$Word1.’ ‘.$Word2; *or* $Words=“$Word1 $Word2”; • You can add additional text to an existing scalar: $a = ‘Hello’; $a .= ‘ World’; print(“$a\n”); #This prints “hello world”

  13. Conditionals • Sometimes you only want to do things if a scalar contains a certain value: $Num=105; if($Num==105) { print(“Big Number!\n”); }

  14. Tests • If’s can have the following tests: == (checks if numbers are equal) < (checks if numbers are less than) > (checks if numbers are greater than) <= (checks if numbers are less than or equal) >= (checks if numbers are greater than or equal) != (checks if numbers are not equal) eq (checks if characters/strings are identical) ne (checks if characters/strings are not identical)

  15. Maybe we need 2 tests • We can combine tests with and/or • AND: && • OR: || $Num=100; if(($Num>10) && ($Num<100)) { print(“Our numbers is between 10 and 100\n”); } $Name=“Enda”; $HeightInFeet=6; if(($Name eq “Enda”) || ($Height>5)) { print(“It’s either Enda, or a tall person\n”); }

  16. What if we want to do one thing if something is true, and something else otherwise? • Each if can have unlimited number of elsif, and up to 1 else: $Month=3; if($Month==1) { print(“Jan\n”); } elsif($Month==2) { print(“Feb\n”); } elsif($Month==3) { print(“Mar\n”); } … elsif($Month==12) { print(“Dec\n”); } else { print(“Error: Month should be between 1 and 12\n”); }

  17. Additional thing you can check • Does this variable exist? if(defined($a)) • What if you want something to happen if something is NOT true if(! (defined($a)))

  18. More than one thing in a variable • Crazy talk? • No, one way to do this is called an Array. @a=(1,2,3); • There is no restriction on what the values in the array can be, one could be a number, the next a string, the next a number etc. • When you refer to the entire array you use @ instead of $ • Arrays are indexed from 0, so the first element in the array is said to be at “index” 0. The second element is said to be at “index” 1. • When you refer to an individual scalar in the array, you use $ again • e.g. @b=@a; print(“$a[0]\n”);

  19. How many things are in the array? • You can find out by assigning the Array to a scalar • Yes, that doesn’t make sense, it’s a weird perl thing: @Months=(‘jan’,’feb’,’mar’,’apr’,’may’,’jun’,’jul’,’aug’,’sep’,’oct’,’nov’,’dec’); $NumMonths=@Months; print(“There are $NumMonths months\n”);

  20. Command Line • Let’s assume you wrote a Perl Script in a file called doit.pl • You typically execute it like this: • C:\perl\bin\perl doit.pl • What if you want to pass “arguments” into your script: • C:\perl\bin\perl doit.pl 1 2 Enda • Those arguments are automatically stored in an Array called @ARGV • Assuming the above command line: • @ARGV=(1,2,’Enda’); • $ARGV[0] now contains 1 • $ARGV[1] now contains 2 • $ARGV[2] now contains ‘Enda’

  21. Doh! • Often you want to ensure that you got at least 1 argument to your script. • If not there may be no reason to continue. • For example, imagine a script that takes one argument, which should be a number less than 5. It prints out the numbers before that number. • Sometime there is just no point in continuing, you should just die: • die “I asked you to send me a number, you gave me $ARGV[0]…I can do nothing with that…giving up”;

  22. count.pl #!C:/Perl/bin/perl.exe $NumArguments=@ARGV; If($NumArguments<1) { die “Usage: count.pl NUMBER”; } If($ARGV[0]==5) { print(“1 2 3 4 5\n”); } elsif($ARGV[0]==4) { print(“1 2 3 4\n”); } elsif($ARGV[0]==3) { print(“1 2 3\n”); } elsif($ARGV[0]==2) { print(“1 2\n”); } elsif($ARGV[0]==1) { print(“1\n”); } else { die “Error, you must send me a number between 1 and 5 as the first argument”; }

More Related