perl_presentation_yao

Download Report

Transcript perl_presentation_yao

Introduction to programming in
Perl
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
A first example
#!/usr/bin/perl
# Pragmas
use strict;
use warnings;
# shebang line
# Restrict unsafe constructs
# 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 = $number1 + $number3;
print “My result is : $number1\n”;
# $number1 contains 40
# $number1 contains 51
# Print the result on the terminal
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
Scalar Binary Operators
$u = 17
$v = 3
$s = “Perl”
Name
Example
Result
Addition
$u + $v
17 + 3 = 20
Subtraction
$u -$v
17 -3 = 14
Multiplication
$u * $v
17 * 3 = 51
Division
$u / $v
17 / 3 = 5.66666666667
Modulus
$u % $v
17 % 3 = 2
Exponentiation
$u ** $v
17 ** 3 = 4913
Concatenation
$s . $s
“Perl”. “Perl”= “PerlPerl”
Repetition
$s x n
“Perl”x 3 = “PerlPerlPerl”
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
Array data type
1
35
2
12.4
3
“bye\n”i
4
1.7e23
5
‘Hi’
$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
Array operators
@let = (“J”, “ P ”, “ S ”, “ D ”, “ C ”);
pop
$r=pop(@let)
$r=“C” @let=(“ J ”, “P”, “ S ”, “ D ”)
push
push(@let,”G”)
@let=(“J”,”P”,”S”,”D”,”C”,”G”)
shift
$r=shift(@let)
$r=“J” @let=(“P”,”S”,”D”,”C”)
unshift
unshift(@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”)
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
Comparison operators
Comparison
Numeric
String
Return Value
Equal
==
eq
1 if $a is equal to $b, otherwise “”
Not equal
!=
ne
1 if $a is not equal to $b, otherwise “”
Less than
<
lt
1 if $ais less than $b, otherwise “”
Greater than
>
gt
1 if $a is greater than $b, otherwise “”
Less than or equal
<=
le
1 if $a is not greater than $b, otherwise
“”
Greater than or equal
>=
ge
1if $ais not less than $b, otherwise “”
Comparison
<=>
cmp
0 if $a and $b are equal, 1 if is greater,
-1 if $b is greater
Match
=~
Not match
“”is the empty string
!~
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.
Logical operators
Example
Name
Result
$a && $b
AND
$a if $a is false, $b otherwise
$a || $b
OR
$a if $ ais true, $b otherwise
! $a
NOT
True if $a is not true, false otherwise
$a and $b
AND
$a if $a is false, $b otherwise
$a or $b
OR
$a if $a is true, $b otherwise
not $a
NOT
True if $a is not true, false otherwise
$a xor $b
XOR
True 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 !
Conditional statements
・Simple
Statement if (Expression);
・Compound
if (Expression) Block
if (Expression) Block else Block
if (Expression) Block elsif (Expression) Block else
Block
Loop statements
・ Simple
Statement while (Expression);
・ Compound
while (Expression) Block
for (Initialization; Expression; Incrementing) Block
Loop statement
foreach localvar (listexpr){
statement_block;
}
Until (<expression>) {
<statement_block>
}
Reading from a file
#!/usr/bin/perl
use strict;
use warnings;
prin
t “Enter the filename: ”;
my $filename = <STDIN>; # 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 = <IN>;
# Store the content of the file in an array
close(IN);
print @names;
Input file
John
Peter
Simon
Dave
Chris
Input and output functions
open
Open FILEHANDLE, EXPR Open a file to referred
using FILEHANDLE
close
Close FILEHANDLE
Close the file associated
with FILEHANDLE
print
Print [FILEHANDLE] LIST
Print each element of
LIST to FILEHANDLE
open GENE, "< /picb/home40/zhouyao/snp/gene.txt";
open SNP, "> /picb/home40/zhouyao/snp/snpout.txt";
#open file->gene list
#save file
Packages
#!/usr/bin/perl
BEGIN{push@INC,'/picb/home40/zhouyao
/Library/perl/'};
open GENE, "<
/picb/home40/zhouyao/snp/gene.txt";
#open file->gene list
open SNP, ">
/picb/home40/zhouyao/snp/snpout.txt";
#save file
chomp(@gene = <GENE>);
$url = "http://compbio.cs.queensu.ca/cgibin/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”;
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
Thank you!