Introduction to Perl

greenbeansneedlesSoftware and s/w Development

Dec 13, 2013 (3 years and 10 months ago)

173 views

Introduction to Perl





2

What is Perl?


Practical Extraction and Report Language


实用摘录与报告语言



Pathlogical Eclectic Rubbish Lister


反常,折中的垃圾陈列器

3

What is Perl?


Perl is a Portable Scripting Language


No compiling is needed.


Runs on Windows, UNIX and LINUX


Fast and easy text processing capability


Fast and easy file handling capability


Written by Larry Wall


“Perl is the language for getting your job done.”

4

How to Access Perl


Off the school network


Located on the 100.10 machines at:
/usr/local/bin/perl


To install at home


www.perl.com

Has rpm's for Linux


www.activestate.com

Has binaries for Windows


Latest Version is 5.8


To check if Perl is working and the version number


% perl
-
v

5

The Basic Hello World Program


Program:


#!/usr/local/bin/perl
-
w

print “Hello World!
\
n”;



Save this as “hello.pl”


Give it executable permissions


chmod ug+x hello.pl


Run it as follows:


./hello.pl

6


Hello World” Observations



.pl” extension is optional but is commonly
used


The first line “#!/usr/local/bin/perl” tells UNIX
where to find Perl



-
w” switches on warning : not required but a
really good idea

7

How to use Perl




Syntax




A Web Server Example

8


要记住,过早的优化往往是错误的根源。如
果您在

Perl
中写了一个原型,并用其他语
言来重写是没有问题的。原型意味着能够方
便地开发。

9


Perl
解释器



速度和

Benchmark



Perl
的容错能力



10

Perl
的缺憾



速度

算法,内置函数



训练



面向对象



线程以及统一字符编码


11

Perl
的优势



某些方面的优势,例如:



正则表达式



隐含的函数声明



不严格的语法



象日用文档似的程序结构


通用的灵活,大大减少开发的时间


几乎没有任何

C


C++
能做而

Perl
不能
的事情

12

Syntax


Literals


Types of Variables


Operators


String Functions


Loop


13

Numerical Literals


Numerical Literals


6






Integer


12.6





Floating Point


1e10





Scientific Notation


6.4E
-
33




Scientific Notation


4_348_348


Underscores instead of







commas for long numbers

14

String Literals


String Literals


“There is more than on way to do it!”


'Just don't create a file called
-
rf.'


“Beauty?
\
nWhat's that?
\
n”


“”


“Real programmers can write assembly in
any language.”




Quotes from Larry Wall

15

Types of Variables


Types of variables:


Scalar variables : $a, $b, $c


Array variables : @array


Hash variables : %hash


File handles : STDIN, SRC, DEST



Variables do not need to be declared


Variable type (int, char, ...) is decided at run
time


$a = 5; # now an integer


$a = “perl”; # now a string


16

Operators on Scalar Variables


Numeric and Logic Operators


Typical : +,
-
, *, /, %, ++,
--
, +=,
-
=, *=, /=, ||,
&&, ! ect …


Not typical: ** for exponentiation



String Operators


Concatenation: “.”
-

similar to strcat

$first_name = “Larry”;

$last_name = “Wall”;

$full_name = $first_name
.

“ “
.

$last_name;

17

Equality Operators for Strings


Equality/ Inequality : eq and ne


$language = “Perl”;

if ($language == “Perl”) ...

# Wrong!

if ($language

eq
“Perl”) ...

#Correct



Use eq / ne rather than == / != for strings

18

Relational Operators for
Strings


Greater than


Numeric : >




String : gt


Greater than or equal to


Numeric : >=



String : ge


Less than


Numeric : <




String : lt


Less than or equal to


Numeric : <=



String : le

19

String Functions


Convert to upper case


$name = uc($name);


Convert only the first char to upper case


$name = ucfirst($name);


Convert to lower case


$name = lc($name);


Convert only the first char to lower case


$name = lcfirst($name);



20

If ... else ... statements


Similar to C/C++
-

except the scope braces
are REQUIRED!!


if ( $os eq “Linux” ) {


print “Sweet!
\
n”;

}

elsif ( $os eq “Windows” ) {


print “Time to move to Linux, buddy!
\
n”;

}

else {


print “Hmm...!
\
n”;

}

21

Unless ... else Statements


Unless Statements are the opposite of if ...
else statements.


unless ($os eq “Linux”) {


print “Time to move to Linux, buddy!
\
n”;

}

else {


print “Sweet!
\
n”;

}



And again remember the braces are required!

22

While Loop


While loop: Similar to C/C++ but again the
braces are
required!!



Example :

$i = 0;

while ( $i <= 1000 ) {


print “$i
\
n”;


$i++;

}

23

Until Loop


The until function evaluates an expression
repeatedly until a specific condition is met.



Example:


$i = 0;


until ($i == 1000) {


print “$i
\
n”;


$i++;

}

24

For Loops


Like C/C++


Example :


for ( $i = 0; $i <= 1000; $i++ ) {


print “$i
\
n”;


}



Another way to create a for loop


Example


for $i(0..1000) {


print “$i
\
n”;


}

25

Minimal Perl Web Server(1)

#!/usr/bin/perl


use Socket;

use Carp;

use FileHandle;


# (1) use port 8080 by default, unless overridden on command line

$port = (@ARGV ? $ARGV[0] : 8080);


# (2) create local TCP socket and set it to listen for connections

$proto = getprotobyname('tcp');

socket(S, PF_INET, SOCK_STREAM, $proto) || die;

setsockopt(S, SOL_SOCKET, SO_REUSEADDR, pack("l", 1)) || die;

bind(S, sockaddr_in($port, INADDR_ANY)) || die;

listen(S, SOMAXCONN) || die;


26

Minimal Perl Web Server(1)

# (3) print a startup message

printf("


<<<Type
-
O
-
Serve Accepting on Port %d>>>
\
n
\
n",$port);


while (1){




# (4) wait for a connection C




$cport_caddr = accept(C, S);




($cport,$caddr) = sockaddr_in($cport_caddr);




C
-
>autoflush(1);




# (5) print who the connection is from




$cname = gethostbyaddr($caddr,AF_INET);




printf("


<<<Request From '%s'>>>
\
n",$cname);



27

Minimal Perl Web Server(1)


# (6) read request msg until blank line, and print on screen




while ($line = <C>)


{





print $line;





if ($line =~ /^
\
r/) { last; }




}



# (7) prompt for response message, and input response lines,




#


sending response lines to client, until solitary "."




printf("


<<<Type Response Followed by '.'>>>
\
n");




while ($line = <STDIN>)




{





$line =~ s/
\
r//;





$line =~ s/
\
n//;





if ($line =~ /^
\
./) { last; }





print C $line . "
\
r
\
n";




}




close(C);

}


28

Resources For Perl


Books:


Learning Perl


By Schwartz


Published by O'Reilly


Programming Perl


By Larry Wall,Tom Christiansen and Jon Orwant


Published by O'Reilly


Web Site


http://safari.oreilly.com


Contains both Learning Perl and Programming
Perl in ebook form

29

Web Sources for Perl


Web


www.perl.com


www.perldoc.com


www.perl.org


www.perlmonks.org

Thank you