Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to programming in Perl. What is Perl ? Perl : Practical Extraction and Report Language by Larry Wall in 1987 Text-processing language Glue.

Similar presentations


Presentation on theme: "Introduction to programming in Perl. What is Perl ? Perl : Practical Extraction and Report Language by Larry Wall in 1987 Text-processing language Glue."— Presentation transcript:

1 Introduction to programming in Perl

2 What is Perl ? Perl : Practical Extraction and Report Language by Larry Wall in 1987 Text-processing language Glue language Very high level language perl is the language compiler/interpreter program

3 A first example #!/usr/bin/perl # shebang line # Pragmas use strict; # Restrict unsafe constructs use warnings; # Provide helpful diagnostics # Assign 15 to $number1 my $number1 = 15; # Assign 25 to $number2 my $number2 = 25; # Assign 11 to $number3 my $number3 = 11; $number1 = $number1 + $number2; # $number1 contains 40 $number1 = $number1 + $number3; # $number1 contains 51 print “My result is : $number1\n”; # Print the result on the terminal

4 Scalar Data Type $answer = 36; # an integer $pi = 3141659265 # a real number $avocados = 6.02e23; # scientific notation $language = “Perl”; # a string $sign1 = “$language is nice”; # string with interpolation $sign2 = ‘$language is nice’; # string without interpolation Scalar = singular variable $ --> S

5 Scalar Binary Operators $u = 17$v = 3$s = “Perl” NameExampleResult Addition$u + $v17 + 3 = 20 Subtraction$u -$v17 -3 = 14 Multiplication$u * $v17 * 3 = 51 Division$u / $v17 / 3 = 5.66666666667 Modulus$u % $v17 % 3 = 2 Exponentiation$u ** $v17 ** 3 = 4913 Concatenation$s. $s“Perl”. “Perl”= “PerlPerl” Repetition$s x n“Perl”x 3 = “PerlPerlPerl”

6 Context $u = “12”+ 5; $u = “12john”+5; $u = “john12”+ 5; use strict; $u = “john12”+ 5; Argument “john12”isn’t numeric in addition (+) at line 3 5 $u = “12”+ 5; 17

7 Array data type $data[0] = 35; $data[1] = 12.4; $data[2] = “bye\n”; $data[3] = 1.7e23; $data[4] = ‘Hi’; @data = (35, 12.4, “bye\n”, 1.7e23, ‘Hi’) Array = plural variable @ -->a 35 12.4 “bye\n”i 1.7e23 ‘Hi’ 1 2 3 4 5

8 Array operators @let = (“J”, “ P ”, “ S ”, “ D ”, “ C ”); pop$r=pop(@let)$r=“C” @let=(“ J ”, “P”, “ S ”, “ D ”) pushpush(@let,”G”)@let=(“J”,”P”,”S”,”D”,”C”,”G”) shift$r=shift(@let)$r=“J” @let=(“P”,”S”,”D”,”C”) unshiftunshift(@let,”G”)@let=(“G”,”j”,”P”,”S”,”D”,”C”) splice@a=splice(@let,1,2)@a=(“P”,”S”) @let=(“J”,”D”,”C”) join$r=join(‘:’,@let)$r=“J:P:S:D:C” scalar$r=scalar(@let)$r=5 reverse@a=reverse(@let)@a=(“C”,”D”,”S”,”P”,”J”)

9 Search for a name #!/usr/bin/perl use strict; use warnings; my @names = (“John”, “Peter”, “Simon”, “Dave”, “Chris”); my $offset = int(rand(scalar(@names))); # random index in [0,..., 4] if ($names[$offset] eq”Simon”) { # block start for the if statement print “Simon is found\n”; print “Success!\n”; } # block end for the if statement else { # block start for the else statement print “Simon is not found\n”; print “Failed!\n”; } # block end for the else statement

10 Comparison operators ComparisonNumericStringReturn Value Equal==eq1 if $a is equal to $b, otherwise “” Not equal!=ne1 if $a is not equal to $b, otherwise “” Less than<lt1 if $ais less than $b, otherwise “” Greater than>gt1 if $a is greater than $b, otherwise “” Less than or equal<=le1 if $a is not greater than $b, otherwise “” Greater than or equal>=ge1if $ais not less than $b, otherwise “” Comparison cmp0 if $a and $b are equal, 1 if is greater, -1 if $b is greater “”is the empty string Match=~Not match!~

11 What is true or false? ・ Any number is true except for 0. ・ Any string is true except for “”and “0”. ・ Anything else converted to a true value string or a true value number is true. ・ Anything that is not true is false.

12 Logical operators ExampleNameResult $a && $bAND$a if $a is false, $b otherwise $a || $bOR$a if $ ais true, $b otherwise ! $aNOTTrue if $a is not true, false otherwise $a and $bAND$a if $a is false, $b otherwise $a or $bOR$a if $a is true, $b otherwise not $aNOTTrue if $a is not true, false otherwise $a xor $bXORTrue if $a or $b is true, false if both are true $xyz = $x || $y || $z is not the same as $xyz = $x or $y or $y ! Use parentheses !

13 Conditional statements ・ Simple Statement if (Expression); ・ Compound if (Expression) Block if (Expression) Block else Block if (Expression) Block elsif (Expression) Block else Block

14 Loop statements ・ Simple Statement while (Expression); ・ Compound while (Expression) Block for (Initialization; Expression; Incrementing) Block

15 Loop statement foreach localvar (listexpr){ statement_block; } Until ( ) { }

16 Reading from a file #!/usr/bin/perl use strict; use warnings; prin t “Enter the filename: ”; my $filename = ; # Read Standard Input for a filename chomp($filename); # Remove the end of line character if (! (-e $filename)) { # Test whether the file exists print “File not found\n”; exit 1; } open(IN, $filename) || die “Could not open $filename\n”; my @names = ; # Store the content of the file in an array close(IN); print @names; Input file John Peter Simon Dave Chris

17 Input and output functions openOpen FILEHANDLE, EXPROpen a file to referred using FILEHANDLE closeClose FILEHANDLEClose the file associated with FILEHANDLE printPrint [FILEHANDLE] LISTPrint each element of LIST to FILEHANDLE open GENE, " gene list open SNP, "> /picb/home40/zhouyao/snp/snpout.txt"; #save file

18 Packages #!/usr/bin/perl BEGIN{push@INC,'/picb/home40/zhouyao /Library/perl/'}; open GENE, " gene list open SNP, "> /picb/home40/zhouyao/snp/snpout.txt"; #save file chomp(@gene = ); $url = "http://compbio.cs.queensu.ca/cgi- bin/compbio/search/main.cgi?id_type=snp_id &id_val=&disease_category=dl0&disease_n ame=0&search_mode=gene&gene_name=$ gene&chr=1&start_pos=&end_pos="; use LWP::Simple; #!/usr/bin/perl use strict; use warnings; use MyTools; my $filename1 = “data1.txt”; my $filename2 = “data2.txt”; my %data1 = MyTools::get_data($filename1); my %data2 = MyTools::get_data($filename2); $, = “”; # set the print separator print keys %data1, “\n”, values %data1, “\n”; print keys %data2, “\n”, values %data2, “\n”;

19 References Recommend book: “Beginner Learning Perl”, 4th Edition by Randal Schwartz, Tom Phoenix & Brian D Foy Perl 中文教程: www.sun126.com/perl5/ Perl bbs: www. Perlchina.org

20 Thank you!


Download ppt "Introduction to programming in Perl. What is Perl ? Perl : Practical Extraction and Report Language by Larry Wall in 1987 Text-processing language Glue."

Similar presentations


Ads by Google