Perl - OS3.nl

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

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

84 εμφανίσεις

Perl

ESA
2011/2012

Adam
Belloum

a.s.z.belloum@uva.nl

Material
Prepared
by

Eelco

Schatborn



ESA:
Perl



Today:

1.
Perl
introducCon

2.
Basic
Perl:
types,
variables,
statements,
.
.
.

3.
Object
Oriented
Perl

4.
DocumentaCon

Perl
IntroducCon



“PracCcal
ExtracCon
and
Report
Language"?



Developed
by
Larry
Wall,
as
system

administrator
at
NASA,
in
the
late
1980s



Created
as
a
way
to

make
report
processing

easier.



It's
an

interpreted

language,
but
can
be

compiled

as
well



Is
now
being
used
for:



System

administra1on
automa1on



Glue

between
systems,
conversion



CGI
backend

for
websites



Much
more.
.
.

Simple
Perl

A
simple
program
to
start
with:

#!/
usr/bin/perl

print "
Hi there!
\
n
";
print "This is a very";
print "
\
t
simple
program.\
n
"
;

Things
to
know
about

perl



Perl
vocabulary
is
extremely
rich
and
its
grammar
is

very
flexible.




Perl
programmers
usually
say
"
There's
more
than

one
way
to
do
it
".




In
fact,
you
can
literally
write
a
Perl
script
your
way,

with
your
own
style.
Some
people
even
do
"poetry"

in
Perl



Because
of
this,
some
Perl
scripts

can
be

very

difficult

to
read
.
WriCng
them
though,
is
always
a

pleasure.

Clement
Lefebvre:
h]p://www.linuxforums.org/arCcles/learn‐perl‐in‐10‐easy‐lessons‐lesson‐1_120.html

Another
Perl
script

h]p://
www.perlmonks.org/?node_id
=1590

h]p://
www.perlmonks.org/?node_id
=1590

The
Perl
interpreter



Perl
is
an
interpreted
language.




This
means
that
the
code
you
will
write
using
the
Perl
language
will

need
a
program
called
the

Perl
interprete
r
in
order
to
be
run.




For
instance,
if
you
write
a
Perl
script
in
a
file
called

myScript.pl

(.
pl

is
commonly
used
as
an
extension
for
Perl
scripts),
there
are
two

ways
to
run
a

perl

script.




call
the
interpreter
to
run
it:





$
perl

myScript.pl



tell
the
Perl
script
where
to
find
the
interpreter
and
make

the
script

executable




#!/
usr/bin/perl

Clement
Lefebvre:
h]p://www.linuxforums.org/arCcles/learn‐perl‐in‐10‐easy‐lessons‐lesson‐1_120.html

Numbers



simple
numbers
as
per
usual:



decimal:
12,
‐17,
255
.
.
.



octal,

start

with

0
:
015,
‐023,
0777
.
.
.



hexadecimal,
start
with

0x
:
0xc,
‐0x11,
0XfF,
.
.
.
either
case

allowed
for
the

x

or
hex
digits

abcdef
.

FloaCng
point
numbers:



“one
and
a
quarter":
1.25



“7.25
Cmes
10
to
the
45th
power":
7.25e45.




“negaCve
12
Cmes
10
to
the
‐24th":
‐12e‐24.

String
manipulaCon



Escape
sequences
represent
special
characters:

\
n
,

\
t
,

.
.
.



Only
in
double
quoted
(‘



’)
strings



For

verba1m

strings
use
single
quotes
(‘



’)





Perl:


print

"
Hi
there!

\
n

\
t

It's
me.\
n
"
;




print

'
And
me

\
n

\
t

as
well!
'
;







output:
Hi
there!




 
 

It's
me.




And
me
\
n

\
t

as
well!

Variables



Three
different
types
of
variables:

scalars, arrays and hashes


Start
with
the
dollar

(‘
$
’)
,
at

(‘
@
’)

or

(‘
%
’)

depending
on
the
type
of
variable

$
a = 2;
$
message = "Hello World!";
@
colors =
(
"red"
,
"green"
,
"blue"
)
;
@
primaryNumbers
=
(
1
,
2
,
3
,
5
,
7
)
;
%
phoneNumbers
=
(Alicia =>
"090-64-773315"
,

Tom =>

"085-153-3214"
,

Jimmy =>
"085-285-4545");


Variable
names
are:




a

punctua1on

character,

a

leAer

or

underscore
,




and
one

or

more
alphanumeric

characters
or

underscores

.

#!/
usr/bin/perl

$
nbArguments
= $#ARGV + 1;
print "number of arguments: $nbArgumentsn";
exit(1) unless $
nbArguments
== 3;
$a = $ARGV[0];
$b = $ARGV[2];
$operation = $ARGV[1];
if ($operation
eq
"+") { $result = $a + $
b
;}
elsif
($operation
eq
"-") { $result = $a -
$
b
;}
elsif
($operation
eq
"/") { $result = $a /
$
b
;}
elsif
($operation
eq
"
x
") { $result = $a *
$
b
;}
print "$a $operation $
b
= $
resultn
"
;

Make
the
script
executable:

$
chmod

a+rx

calculator.pl

Run
the
script
:


$ ./
calculator.pl
5 + 6./calculator.pl 11 -
2./calculator.pl 4
x
3./calculator.pl 33
/
3

Your
second
Perl
script,
the

Calculator!

The
command
line
arguments
are
stored
in

array
@ARGV


‐
first
element
$ARGV[0]


‐

second
element
$ARGV[1]...
etc.


‐

the
index
of
the
last
element
in
the
array








can
be
obtained
by
$#ARGV.

Two
operators
for
comparing
scalar
variables


==

and

eq

Unless
is
used
as
a
control
statement


Scalar
variables



Start
with
the
dollar

(‘
$
’)

sign.



Can
be
a
number
or
a
string
(or
an
object
reference)



The
usual
operators
apply:

+, -, *, /, =, +=, -=, *=, /=, ++, --


Perl

automa1cally
converts

between
numbers
and

strings.

$
n
= 8 + "2"

$
n

holds

"10"


concatenaCon
of
strings
using
a
period
`
.

`

$
n
= 8
.
"2"

$
n

holds

"82"
Array
variables
(1)



Start
with
the
at
(`
@
')
sign.



Arrays
represent

ordered

lists
of
scalars.



DefiniCon
using
parenthesized,
comma
delimited

lists:

@numbers =
(
1
,
2
,
4
,
8
,
16
)
;
@strings = ( "one"
,
"two"
,
"3" );


The
index
of
an
array
starts
at

0
,
not

1



Processing
single
items
as
a
scalar
using

[

and

]
:

print
$
strings
[
1
]
; # prints "two"
$
strings
[
1
]
= "2"; # we changed @strings
Array
variables
(2)



Arrays
can
be

created

using
scalar
assignment:


$
names
[0]
= "
Eelco
";




creates @names to hold ("
Eelco
")


The
length
of
an
array:

$#
strings
,
returns
the
number

of
items
in
the
array
minus
one.




If
equal
to

‐1
,
the
array
does
not
exist
or
is
empty.


print
$#
strings
; ! prints 2


Assignment
to

$#
strings

changes

the

size

of
the

array.

$#strings = 0;


@strings
now holds ( "one" )
Hash
variables
(1)



Start
with
the
percentage
(
%
)
sign.



Made
up
of

keys

and

values
.



Each

key

has

exactly

one

corresponding
value
.



DefiniCon
using
parenthesized,
comma
delimited
lists
of

keys
and
values:

%
numbers =
(
"
one
"
=>
"one"
,
"
two
"
=>
2
)
;


Processing
a
single
hash
as
a
scalar
using

{

and

}
:


print
$
numbers
{
"two
"
}
; ! prints "2"

$
numbers
{
"one
"
}
= "1"; ! Changed %numbers
Hash
variables
(2)



Like
arrays,
hashes
can
be

created

using
scalar

assignment:


$
strings
{
"
1
"
}
= "one";




creates %strings to hold ( "
1
" =>
"one" )


The

keys

<hash>
funcCon
will
return
an
array
filled

with
all
the
keys
in
the
hash.


print
keys

%numbers
; ! prints ( "one",
"two" )
Flow
control



A
block
of
code
contain
a
list
of
statements.



Code
blocks
are
delimited
by

{

and

}



A

perl

program
is
also
a
block
of
code.



Flow
control
can
be
excreted
using
looping
or

condiConal
Statements.

Looping
Statements



For
loops

are
for
repeCCon



Using
a

list

and
an

index
variable

a
block
of
code
will

be
executed
for
each
value
in
the
list.

for $
i
(
1, 2, 3, 4
) { . . . }


Using
`..'
you
can
also

give
a
range
:

for $
i
( 1
..
3, 4 ) { . . . }


The
list
can
contain
any

scalars

or
other

lists
.

@range = ( 1 .. 3 );
for $
i
(
@range
, 4 ) { . . . }
CondiConal
Statements
(1)



CondiConal
statements:

if
( test ) { . . . }
elsif

{ . . . }
else
{ . . . }
unless
( test ) { . . . }
else
{ . . . }
while
( test ) { . . . }
until
( test ) { . . . }


The
usual
comparison
operators
for
tesCng:

<, >, ==, !=, <=, >=


But
comparison
of
strings
using
`
eq
'.

0xa
==
" 10 "

true
0xa
eq
" 10 "

false
CondiConal
Statements
(2)

if
($a == 5) {


print "It's five!\
n
";
}
elsif
($a == 6) {


print "It's six!\
n
";
}
else
{


print "It's something else.\
n
";
}
unless
($pie
eq
'apple') {


print "
Ew
, I don't like
$pie
flavored pie.
\
n
";
}
else
{


print "Apple! My favorite!\
n
";
}
CondiConal
Statements
(3)

$a = 0;
while
($a != 3) {

$a++;

print "Counting up to $a...\
n
";
}
until
($a == 0) {

$a--;

print "Counting down to $a...\
n
";
}
Play
Around
1



At
this
point
you
have
some
basic
knowledge
of
Perl

syntax
and
a
few
simple
toys
to
play
with.
Try
wriCng

some
simple
programs
with
them.




Here
are
two
suggesCons,
one
simple
and
the
other

a
li]le
more
complex:



A
word
frequency
counter.
How
o|en
does
each
word

show
up
in
an
array
of
words?
Print
out
a
report.
(Hint:
Use

a
hash
to
count
of
the
number
of
appearances
of
each

word.)



Given
a
month
and
the
day
of
the
week
that's
the
first
of

that
month,
print
a
calendar
for
the
month.

Idea
of
a
soluCon



Use
split
to
divide
you
string
into
words,
separator




“
“
.




For

$
words(split
(‘
‘,
$strings)
)






$
count{$words
}
+=1;

Perl
Regular
expressions
(1)



Very
mature,
can
be
highly
complex



Can
be
used
for
matching
(tesCng)
or
transliteraCon.



Simple
syntax
is

/
.
.
.
/



The
matching
operator
`
=~
'
is
used
for
tesCng

Perl:

if ("This is
perl
!" =~ /is/) {



print "Match!";



}
output: Match
!

Perl
Regular
expressions
(2)



^


and

$

match
the
beginning
and
end
of
a
string

respecCvely.



You
can
use
character
classes
(
[

.
.
.

]
).



You
can
quanCfy
matches
using

*
,

+

or

?

a|er
a

character
or
character
class.



.

matches
any
character.



Use
a
backslash
(
\
,)
to
escape
characters
like:

.
,

^
,

$
,

/
,

\
,

{
,

}
,

[
,

]
,
.
.
.
.

Perl
Regular
expressions
(3)

Metacharacters

in
regular
expressions:


\
d



Matches
a
single
digit
character


\
w



Matches
a
single
word
character
(a
le]er,
digit



 

or
underscore)


\
s



Matches
a
whitespace
character






.
.
.
.
.
.

Flags
that
can
follow
a
regular
expression:


i



Match
case
insensiCvely


g



Remember
the
current
posiCon
for
the
next



 
 
 

matching

Perl
Regular
expressions
(4)



Matching

subexpressions
:



You
can
match

subexpressions

by
encapsulaCng
them
with

`
('

and
`
)
’



Each

subexpression

is
stored
in
a
variable

$1

.
.
.

$
n
,
which

can

be
used
later
on.

Perl:

"This is it!" =~ /
\
s
(
..
)
/;



print
$1
;


QuesCon:
What
is
printed
by
the
above
lines?

Perl
Regular
expressions
(4)

Search
and
replace:



Using

s
/

regexp

/

replacement

/



Perl:

$a = "This is it, or is it?"



$a =~
s/
\s.
(
s
)
/
wa
$1/
;



print $a;


output: This
was
it, or is it?


Using
the

g


flag
the
replacement
will
be
made
for
all

matches,
instead
of
just
the
first
one.



Perl:

$a = "This is it, or is it?”



$a =~
s/
\s.(s
)
/
wa
$1/g
;



print $a;


output: This
was
it, or
was
it?
File
Handling



The
Perl
language
was
designed
to

make
file
handling
easy

and

efficient
,
so

you'll
probably
won't
have
any
problem
opening
files
and
reading
them.





Opening
and
closing
files




open
”
instrucCon

takes

two
arguments
:
the
name
of
a

filehandle

and
the

name
of
the

file
itself
.




filehandle

is
like
a
variable
which
represents
the
handling
of
the
file

within
the
script.



open
(CLIENTS,
"
clients.txt
");



By
default,
the
file
is
open
in

read‐mode
.
To
open
a
file
in

write‐mode

or

append‐mode

simply
add
a
“
>
”
or
“
>>
”

in
front
of
the
file
name:


open
(CLIENTS,
"
>
clients.txt
");

open
(CLIENTS,
"
>>
clients.txt
");

File
Handling:
open



The
“open”
instrucCon

returns




true

if
it
managed
to
open
the
file,

false

otherwise.




You
can
use
the
return
value
to
test
the
success
of
the
operaCon

open (CLIENTS, ">
clients.txt
")
or
print "Insufficient
privileges\
n
";


Remember
to
always

close

the
files
once
you're
finished
with

them.
If
you
don't
your
changes
might
be
lost.




to
close
a
file,
simply
use
the
“close”
instrucCon
on
the

filehandle
:



close (CLIENTS);
File
Handling:
write



Wri1ng
into
files



Once
the
file
is
open
in
write
mode
you
can
write

into
it
simply
by
wriCng
into
its

filehandle
.




The
“print”
instrucCon
writes
things
on
the
screen
by

default,
but
you
can
specify
a

filehandle

to
write
into.


open (CLIENTS, "
>>
clients.txt
") or die
"
Insufficientprivilegesn
";
print
CLIENTS
"
Mr
John
Doen
";
close (CLIENTS)
;

File
Handling:
read



Copying
the
content
of
the
file
into
an
array



Each
line

will
then
correspond
to

an
element

of
the
array.
Here
is

an
example:


open(CLIENTS
, "
clients.txt
");
@lines = <CLIENTS> ;
close(CLIENTS
);
print @lines;





Looping
through
the

filehandle





you
can
loop
through
the

filehandle

in
a
while
loop
by
wriCng

while(
$line

=

)




think
of
it
as
“while”
which
assign
lines
in
the
clients
file
to

$line







open (CLIENTS, "
clients.txt
");
while ($line = <CLIENTS>) {
print $line;
}
close (CLIENTS);
Your
third
Perl
script,
list
of

employees
!

#
open
the

employeesfile

open
(
EMPLOYEES,"employees.txt
");

#for
each
line

while
(<EMPLOYEES>)
{

#remove
the
carriage
return

$line=$_;

chomp
$line;

#
split

the
line
between
tabs

#and
get
the
different
elemen
ts

($name,
$department,
$salary)
=

split(“\
t
”,
$line);

#go
to
the

nex
t
line
unless
the
name
starts
with
“
Mr

“

Next
unless
$name
=~
/^
Mr

/;

#go
to
the
next
line
unless
the
salary
is
more
than
25000.

Next
unless
$salary
>
25000;

#go
to
the
next
line
unless
the
department
is
R&D.

Next
unless
$department

eq

“R&D”;

#since
all
employees
here
are
male,

#remove
the
parCcle
in
front
of
their
name

$name
=~

s/Mr
//;

Print
”$name”;

}

Close
(EMPLOYEES);

What
this
script
does
to

the
list
of
employees
?

What
can
you
learn
from

the
example
(new

things
we
did
not

discuss)

Play
Around
2



A
rule
of
good
wriCng
is
"avoid
the
passive
voice."

Instead
of

The
report
was
read
by
Carl
,
say

Carl
read

the
report
.




Write
a
program
that
reads
a
file
of
sentences
(one

per
line),
detects
and
eliminates
the
passive
voice,

and
prints
the
result.
(Don't
worry
about
irregular

verbs
or
capitalizaCon,
though.)

SubrouCnes



Subs
are
funcCons,
they
can
have
any

arguments



The

arguments

are
handed
down
using
the

special
array

@_



A
value
can
be
returned
using

return

Perl:
sub
multiply {



my (@ops) =
@_
;



return
$ops[0] * $ops[1];



}




multiply(2, 3);
output: 6
Packages
(1)



Packages
are
a
way
to

split
up

perl

code
into

several

pieces



A
package
can
be

included

into
a
piece
of
code
by

using
“
use”



Each
package
represents
a
new
namespace



Create
a
package
by
creaCng
a
new
file,
generally

ending
in
.
pm



The
code
for
the
class
you
are
creaCng
goes
in
there.



The

classname

o|en
is
the
same
as
the
filename,
this

is
easier
to
use,
but

not
obligatory
.

Packages
(2)



Create
a
new
file,
for
instance

MyPackage.pm



The
first
line
should
provide
the
name
of
the
class
in
the


package:


package
MyPackage
;
. . . Next comes the code for the package


The
file
should
end
with

1
;
to
indicate
to
the
loading
module

that
the
enCre
package
was
read
successfully.



Example:


package

MyPackage
;

print "WOW, it worked!\
n
";

1;
Packages
(3)



To

use

our
package
run
the
following

perl

code:


#!/
usr/bin/perl


use

MyPackage
;






The
output
of
the
program:


WOW, it worked!;
Packages
(4)



You
can
refer
to

variables

and

filehandles

in

other
packages

by

prefixing

the

iden1fier

with
the

package
name

and
a
double

colon:

$
Package::
Variable



You
can

create

subs
in
a
package
in
the
normal
way



They
can
be

called

from
the
program
using
the
package:

$
Package::
the_sub
("well
?")






Special
code
blocks
can
be
used
for

ini1aliza1on

and

destruc1on
:

INIT {}, CHECK {}, BEGIN {}, END {}






Use

perldoc

perlmod

to
get
more
informaCon

Packages
(4)



INIT

blocks
are
run
just
before
the
Perl

run1me
begins

execu1on
,
in
"first
in,
first
out"
(FIFO)
order.




CHECK

code
blocks
are

run
just
aWer
the
ini1al

Perl
compile

phase
ends
and

before
the
run
1me
begins
,
in
LIFO
order.



BEGIN

code
block
is
executed
as
soon
as
possible,
that
is,
the

moment
it
is
completely
defined,
even
before
the
rest
of
the

containing
file
(or
string)
is
parsed.




END

code
block
is
executed
as
late
as
possible,
that
is,
a|er

perl

has
finished
running
the
program
and
just
before
the
interpreter

is
being
exited,
even
if
it
is
exiCng

as
a
result
of
a
die()
funcCon

CPAN



Comprehensive
Perl
Archive
Network
,




is
an
archive
of
nearly
100,000

modules
of
so|ware

wri]en
in

Perl
,




as
well
as
documentaCon
for
it



www.cpan.org



main
purpose
is
to
help
programmers
locate
modules
and
programs

not
included
in
the
Perl
standard
dist



CPAN
has
no

revision
control
system
,
no
formal
bug
tracking
system



Several
search
engines
have
been
wri]en
to
help
Perl
programmers

sort
through
the
CPAN.
The
most
popular
and
official
is

search.cpan.org
,

others:



kobesearch.cpan.org

Randy
Kobes
'
search
engine
(linked
to
by

cpan.org
)




cpan.uwinnipeg.ca

cpantools.com

CPAN
suggest
as
you
type

h]p://
en.wikipedia.org
/wiki/CPAN

CPAN



CPAN

is

mirrored

worldwide
at
more
than
200
locaCons



CPAN

can
denote
either
the
archive
network,
or
the
Perl
program

that
acts
as
an
interface
and
as
an
automated
so|ware
installer



CPAN

Testers

are
a
group
of
volunteers,
who
will
download
and
test

distribuCons
as
they
are
uploaded
to
CPAN.



3rd
party
bug
tracking
system

that
CPAN
designated
as
the

suggested
official
method
of
reporCng
issues
with
modules



ConCnuous
development
on
modules
is
rare,
many
are
abandoned

by
their
authors,
or
go
years
between
new
versions
being
released




Releases
of
new
versions
of
the
module,
and
accept
patches
from

the
community
to
the
module
as
their
Cme
permits.





Authors

Upload
Server

(
PAUSE

h]p://
pause.perl.org
).
To
do
so,

they
must

request
a
PAUSE
account
.
RegistraCon
informaCon
can

be
found
at
the

PAUSE
faq

h]p://
en.wikipedia.org
/wiki/CPAN

CPAN



CPAN

is

mirrored

worldwide
at
more
than
200
locaCons



CPAN

can
denote
either
the
archive
network,
or
the
Perl
program

that
acts
as
an
interface
and
as
an
automated
so|ware
installer



CPAN

Testers

are
a
group
of
volunteers,
who
will
download
and
test

distribuCons
as
they
are
uploaded
to
CPAN.



3rd
party
bug
tracking
system

that
CPAN
designated
as
the

suggested
official
method
of
reporCng
issues
with
modules



ConCnuous
development
on
modules
is
rare,
many
are
abandoned

by
their
authors,
or
go
years
between
new
versions
being
released




Releases
of
new
versions
of
the
module,
and
accept
patches
from

the
community
to
the
module
as
their
Cme
permits.





Authors

Upload
Server

(
PAUSE

h]p://
pause.perl.org
).
To
do
so,

they
must

request
a
PAUSE
account
.
RegistraCon
informaCon
can

be
found
at
the

PAUSE
faq

h]p://
en.wikipedia.org
/wiki/CPAN

Manual



Download
the

tarball

file
from

www.cpan.org

to
your
computer



Decompress
and
unpack
the

tarball

file:

$
gzip
-dc
file.tar.gz
| tar
xvf



Go
into
the
folder
that
was
created
by
the
previous
command.
Build
the

module
execuCng
the
following
commands:

$
perl

Makefile.PL

$ make
$ make test


If
everything
went
well
so
far,
install
the
module:



$ make install


Note:
if
you
use
the
manual
method,
you
have
to
manually
resolve
this

issues.

h]p://
www.perlhowto.com/installing_cpan_modules

automaCc
(using
the
CPAN
module)



Execute
the
following
command:

$
perl
-MCPAN -
e
'install module’


where




'module'
is
the
name
of
the
module
you
want
to
install



Note:




automaCc
method
is
easier
but
requires
Internet;
another

advantage
of
using
the
CPAN
shell
is
that
it
tries
to
resolve

potenCal
module
dependency
problems
automaCcally


h]p://
www.perlhowto.com/installing_cpan_modules

Objects
(1)

“An
Object

is

simply

a

reference

that

to
know

which

class

it
belongs

to.”



No
special
syntax
for
constructors,
you
have
to
make

your
own




A
constructor
is
merely
a

subrou1ne

that
returns
a

reference

to
something
"blessed"
into
a
class,

generally
the

class
that
the
subrouCne
is
defined
in.
“



An
object
is

created

using
the

bless
()



The

bless
()
funcCon

creates

a

reference

to
an

object

h]p://perldoc.perl.org/perlobj.html#An‐Object‐is‐Simply‐a‐Reference

Objects
(2)



Using
an
anonymous
reference
(Here
is
a
typical
constructor)

package Critter;

sub new { bless
{}
}
Note:




{}




allocates
an

anonymous
,
empty
hash
and
returns
a
reference
to
it



bless

takes
that
reference
and
tells
the
object
it
references
that
it's
now
a

Cri]er,
and
returns
the
reference



Using
anonymous
objects
the
class
will
not
know
itself



using
a
known
reference:

package Critter;
sub new {

my
$self =
{}
;

bless
$self;

return $self;
}
h]p://perldoc.perl.org/perlobj.html#An‐Object‐is‐Simply‐a‐Reference

Classes
(1)

“A
class
is

simply

a

package

that
happens
to

provide

methods

to

deal
with

object
references
.”



A
class
provides
the
methods
for

dealing

with
its

reference



ConstrucCon
by

defining
a
constructor
func1on
,
generally

new
()

package Critter;
sub new {

my $self = {};

bless $self;

return $self;
}
h]p://
perldoc.perl.org/perlobj.html

Classes
(2)



A

method

is

simply

a

subrou1ne

that

expects

as
the

first

argument
an:




object
reference





a
package
name
(for
a

class
method)



A
constructor
can
call
methods

using
the
reference

sub new {

my
$self = {};

bless
$self;

$self->initialize();

return
$self;
}
h]p://
perldoc.perl.org/perlobj.html

Classes
(3)



Classes
can
be
inherited



For
that
use
the

two‐
argument
version
of
bless



Bless
an
object
into
a
class

Sub new {

my
$class = shift;

my
$self = {};

bless
$self, $class;

$self->initialize();

return
$self;
}
h]p://
perldoc.perl.org/perlobj.html

documentaCon



BOOKS!,
they
are
in
the
back
of
the
classroom.
.
.



Use
the
web,
there
are
a
lot
of
websites
on

perl



Check

www.perl.com

for
help.



Find
out
about

perldoc

Material
for
these
slides
was
taken
from

h]p://www.perl.com/pub/a/2000/10/begperl1.html


Assignment

Create
a
simple
program
that
can
keep
track
of
your
mp3's.

1.
first
program
the
following
using
only
packages,
no
OO




1.1.
store
the
names
and
locaCons
of
your
mp3's
in
a
large
array
or




hash,
and
allow
for
addiConal
parameters
to
be
stored
as
well,
like




preference
(1‐5
stars),
number
of
Cmes
played,
etc.




1.2.
create
a
simple
interface
for
searching/adding/deleCng/ediCng




your
database.
Make
sure
to
use
subs
for
these
funcCons.
Make
use
of
RE...

2.
recreate,
using
the
previous
code,
the
assignment
using
OO
[OPTIONAL].




2.1
create
a
main
program
that
can
run
mulCple
databases
at
the
same.




Each
database
must
be
a
separate
object
instance.
Provide
an
interface




that
allows
the
user
to
choose
and
switch
between
the
databases
at
will.

3.
(BONUS)
add
funcCons
for
(
re)storing

your
databases

Perl
Script
Exercise:

Netswitch



In
this
exercise,
we
want
to
be
able
to
switch
between

networks.
We
defined
network

configuraCon
files

in
a

directory
called
"networks".



For
instance,
here
is
the
content
of
./networks/home:


interface=eth2

type=
dhcp

proxy=none

And
here
is
the
content
of
./networks/work:


interface=eth1

type=
dhcp

proxy=www‐
proxy.work.com

proxy_port
=8080

Perl
Script
Exercise:
Netswitch



The
Perl
script
should:




takes
a
network
name
as
its
command
line
argument,



opens
the
file
with
the
same
name
from
./networks
and

sets
the
network

interface

with
the
data
taken
from
the

content
of
that
file