Introduction to Perl and Perl Syntax

hollowtexicoΛογισμικό & κατασκευή λογ/κού

13 Δεκ 2013 (πριν από 3 χρόνια και 6 μήνες)

79 εμφανίσεις

1
Introduction to
Perl
and
Perl
Syntax
Learning
Perl
, Schwartz
2
Next Reading Assignment
"How
Perl
Saved the Human Genome
Project" -- by Lincoln Stein
http://www.
foo
.be/docs/tpj/issues/vol1_2/tpj0
102-0001.html
3
Outline

Perl
overview

Perl
-- first line of program convention

Comments

Scalar data

Strings --

single and double quotes

Warning flags

Scalar variable names

Interpolation
4
Perl

Perl
is an interpreted language

perl
programs require an interpreter

once the interpreter is installed on a system, all (most)
perl
programs
should run on it

Contrast to a compiled program (such as C or C++)

compiled to a

binary

, or

opaque binary


a binary is so close to machine language, it is extremely difficult to
reverse engineer (at a high-level) the algorithms implemented

only binaries need to be transferred

not portable across architectures (Windows program will not run on a
Mac, etc)

Performance

Generally -- compiled programs are faster than interpreted programs
(
Perl
, Java)

However, processor speeds and available memory sizes have
increased to the point that

in

many bioinformatics applications
"Perl
is
good enough."
5
Perl
-- first line of a program
(optional)
#!
/usr/bin/perl
OR
#!
/usr/local/bin/perl
OR
#!
/usr/bin/apps/perl


. etc.

typical locations, but may be anywhere

first line of a
perl
application

allows for automatic invocation

tells the UNIX operating system where to find the
Perl
interpreter

Ex)
my_program
.pl

without this line we could also explicitly specify interpreter
/usr/bin/perl
.
/my_program
.pl
OR
perl my_program
.pl

The

Windows version supports this too (according to the docs -- but it

s a
different mechanism.

I suspect windows simply interprets the .pl extension -
- but I was unable to find this in the docs ).
6
Perl
After the first line, the interpreter will ignore all lines that begin with: #
#!/
usr/local/bin/perl
print

Hello, world!\n

;
Newline
= \n = carriage return
Hello, world!server06%
#!/
usr/local/bin/perl
print #This is A
CoMment


completely ignored!!

Hello, world!\n

; # Notice how formatting
andCommentsCanMake
#
itMoreDifficult/EasierToRead
7
Perl
#!
usr/local/bin/perl
print

Hello, world!\n

;
% ./
test.pl
interpreter

usr/local/bin/perl

not found
./
test.pl
: Command not found.
%
perl

test.pl
-- this works
Note that most statements are followed by the pesky semicolon:
print

Hello, world!\n

;
8
Scalar Data
scalar

simplest, most basic kind of data in
perl
(as opposed to a list, or array that is composed
of multiple scalars)
Numbers
floating point: 1.25, 255.00, -3.124E-24
integers: 0 69 236123321 236_123_321
$i = 236_123_321;
print

$i\n

;
236123321
9
Strings

contains any combination of any characters
Single quoted string literals [" '] key (not the [~`]
key):
'fred'
'barney'
'' # null string
'Don\'t
isn\'t
end\'d \'till here'
'last character is backslash\\'
'no
newline
here\n'
10
Strings
Double-quoted literals
Allows for variable interpolation
"barney" #same as 'barney'
"new line here\n"
"last character is a
dble
quote\""
Note in
MSWord/Poweroint
you have to turn off
Tools->
AutocorrectOptions-
>
AutoFormatAsYouType-
>(uncheck "replace
straight quotes with smart quotes")
or you get this:

test

and

test

11
Strings

Special characters
\n
newline
\t
tab
\r
carriage return
many others
12
Strings

concatenation of strings (.) period
"Go" . 'hawks'. ' '."\n" #
Gohawks
- with
newline
String repetition (x) :
$i = "Hawks" x 3; # $i =
HawksHawksHawks
5 x 4;
# equals 5555
# the integer 5 is converted to a string
4 x 5.6
# equals 44444 (5.6) truncated to 5
13
Auto Conversion between
Numbers and Strings
Perl
is useful for rapid prototyping, so it converts
strings/numbers to numbers/strings depending
on operator context (this is not necessarily a
good thing -- so you have to be careful)
5 + 5 # equals 10
5 . 5 # equals '55'
"12" * "3" # equals 36
"12fred34" * '3' # equals 36
"fred"
* "3" # equals 0 (these are all silly cases)
14
C counter example
main()
{
char c =
'f'
;
int
i = 3;
int
j = 0;
j = c*i;
printf
("%
d\n"
,j);
}
15
Warnings
%
perl

w program.pl
OR
#!
/usr/local/bin/perl

w
Argument "12fred34" isn't numeric in multiplication (*) at .
/testl
.pl line
18.
Warnings are very, very good

but note that the program still
executes.
Checks for

unsafe

constructs/mistyping.
How do you specify
"-w"
in

Eclipse?
16
Scalar Variables
A variable contains/holds a scalar value
(number/string).
Cannot start with a number
Case sensitive
All scalar variables are demarcated by $
$i
$This_is_aVariable259-1
$A
$a
17
Choose good Variable Names
$i
$line_length
#underscore is convention
$lineLength
#works too
Assignment
$fred
= 17; #
$fred
variable has
interger
17
$word = "Word";
$word = 3 *
$fred
; # word == 51
$words =
$fred
. "Word". $word; # "17Word51"

$word =
"$fred
is prime"; ## does this work???
$word =
'$fred
is prime'; #how about this?
18
Interpolation
Scalar variable name in string is replaced with
current value.
$isu
= "a team that needs to improve";
$hawks = "will the hawks beat
$isu"
;
# $hawks == "will the hawks beat a team that
needs to improve"
$hawks = 'will the hawks beat' .
$isu
;
print 'I like $money'; # prints dollar-sign
19
Comparison
Comparison
numeric
string
equal
==
eq
not equal
!=
ne
less than
<
lt
greater than
>
gt
less than/
<=
le
equal to
greater than/
>=
ge
equal to
20
Control Structure
Want to make decisions

Friday