P e r l d o c B r o w s e r
●
5 . 4 1 . 1
L a t e s t
5 . 4 0 . 0
5 . 3 8 . 2
5 . 3 8 . 1
5 . 3 8 . 0
5 . 3 6 . 3
5 . 3 6 . 2
5 . 3 6 . 1
5 . 3 6 . 0
5 . 3 4 . 3
5 . 3 4 . 2
5 . 3 4 . 1
5 . 3 4 . 0
5 . 3 2 . 1
5 . 3 2 . 0
5 . 3 0 . 3
5 . 3 0 . 2
5 . 3 0 . 1
5 . 3 0 . 0
5 . 2 8 . 3
5 . 2 8 . 2
5 . 2 8 . 1
5 . 2 8 . 0
5 . 2 6 . 3
5 . 2 6 . 2
5 . 2 6 . 1
5 . 2 6 . 0
5 . 2 4 . 4
5 . 2 4 . 3
5 . 2 4 . 2
5 . 2 4 . 1
5 . 2 4 . 0
5 . 2 2 . 4
5 . 2 2 . 3
5 . 2 2 . 2
5 . 2 2 . 1
5 . 2 2 . 0
5 . 2 0 . 3
5 . 2 0 . 2
5 . 2 0 . 1
5 . 2 0 . 0
5 . 1 8 . 4
5 . 1 8 . 3
5 . 1 8 . 2
5 . 1 8 . 1
5 . 1 8 . 0
5 . 1 6 . 3
5 . 1 6 . 2
5 . 1 6 . 1
5 . 1 6 . 0
5 . 1 4 . 4
5 . 1 4 . 3
5 . 1 4 . 2
5 . 1 4 . 1
5 . 1 4 . 0
5 . 1 2 . 5
5 . 1 2 . 4
5 . 1 2 . 3
5 . 1 2 . 2
5 . 1 2 . 1
5 . 1 2 . 0
5 . 1 0 . 1
5 . 1 0 . 0
5 . 8 . 9
5 . 8 . 8
5 . 8 . 7
5 . 8 . 6
5 . 8 . 5
5 . 8 . 4
5 . 8 . 3
5 . 8 . 2
5 . 8 . 1
5 . 8 . 0
5 . 6 . 2
5 . 6 . 1
5 . 6 . 0
5 . 0 0 5 _ 0 4
5 . 0 0 5 _ 0 3
5 . 0 0 5 _ 0 2
5 . 0 0 5 _ 0 1
5 . 0 0 5
●
D e v
b l e a d
5 . 4 1 . 2
5 . 4 1 . 1
5 . 4 0 . 0 - R C 2
5 . 4 0 . 0 - R C 1
5 . 3 9 . 1 0
5 . 3 9 . 9
5 . 3 9 . 8
5 . 3 9 . 6
5 . 3 9 . 5
5 . 3 9 . 4
5 . 3 9 . 3
5 . 3 9 . 2
5 . 3 9 . 1
5 . 3 7 . 1 1
5 . 3 7 . 1 0
5 . 3 7 . 9
5 . 3 7 . 8
5 . 3 7 . 7
5 . 3 7 . 6
5 . 3 7 . 5
5 . 3 7 . 4
5 . 3 7 . 3
5 . 3 7 . 2
5 . 3 7 . 1
5 . 3 7 . 0
●
D o c u m e n t a t i o n
P e r l
I n t r o
T u t o r i a l s
F A Q s
R e f e r e n c e
O p e r a t o r s
F u n c t i o n s
V a r i a b l e s
M o d u l e s
U t i l i t i e s
C o m m u n i t y
H i s t o r y
E x p a n d
p e r l i n t r o
( s o u r c e ,
C P A N )
Y o u a r e v i e w i n g t h e v e r s i o n o f t h i s d o c u m e n t a t i o n f r o m P e r l 5 . 4 1 . 1 .
T h i s i s a d e v e l o p m e n t v e r s i o n o f P e r l .
●
N A M E
●
D E S C R I P T I O N
●
W h a t i s P e r l ?
●
R u n n i n g P e r l p r o g r a m s
●
S a f e t y n e t
●
B a s i c s y n t a x o v e r v i e w
●
P e r l v a r i a b l e t y p e s
●
V a r i a b l e s c o p i n g
●
C o n d i t i o n a l a n d l o o p i n g c o n s t r u c t s
●
B u i l t i n o p e r a t o r s a n d f u n c t i o n s
●
F i l e s a n d I / O
●
R e g u l a r e x p r e s s i o n s
●
W r i t i n g s u b r o u t i n e s
●
O O P e r l
●
U s i n g P e r l m o d u l e s
●
A U T H O R
# N A M E
p e r l i n t r o - a b r i e f i n t r o d u c t i o n a n d o v e r v i e w o f P e r l
# D E S C R I P T I O N
T h i s d o c u m e n t i s i n t e n d e d t o g i v e y o u a q u i c k o v e r v i e w o f t h e P e r l p r o g r a m m i n g l a n g u a g e , a l o n g w i t h p o i n t e r s t o f u r t h e r d o c u m e n t a t i o n . I t i s i n t e n d e d a s a " b o o t s t r a p " g u i d e f o r t h o s e w h o a r e n e w t o t h e l a n g u a g e , a n d p r o v i d e s j u s t e n o u g h i n f o r m a t i o n f o r y o u t o b e a b l e t o r e a d o t h e r p e o p l e s ' P e r l a n d u n d e r s t a n d r o u g h l y w h a t i t ' s d o i n g , o r w r i t e y o u r o w n s i m p l e s c r i p t s .
T h i s i n t r o d u c t o r y d o c u m e n t d o e s n o t a i m t o b e c o m p l e t e . I t d o e s n o t e v e n a i m t o b e e n t i r e l y a c c u r a t e . I n s o m e c a s e s p e r f e c t i o n h a s b e e n s a c r i f i c e d i n t h e g o a l o f g e t t i n g t h e g e n e r a l i d e a a c r o s s . Y o u a r e s t r o n g l y a d v i s e d t o f o l l o w t h i s i n t r o d u c t i o n w i t h m o r e i n f o r m a t i o n f r o m t h e f u l l P e r l m a n u a l , t h e t a b l e o f c o n t e n t s t o w h i c h c a n b e f o u n d i n p e r l t o c .
T h r o u g h o u t t h i s d o c u m e n t y o u ' l l s e e r e f e r e n c e s t o o t h e r p a r t s o f t h e P e r l d o c u m e n t a t i o n . Y o u c a n r e a d t h a t d o c u m e n t a t i o n u s i n g t h e p e r l d o c
c o m m a n d o r w h a t e v e r m e t h o d y o u ' r e u s i n g t o r e a d t h i s d o c u m e n t .
T h r o u g h o u t P e r l ' s d o c u m e n t a t i o n , y o u ' l l f i n d n u m e r o u s e x a m p l e s i n t e n d e d t o h e l p e x p l a i n t h e d i s c u s s e d f e a t u r e s . P l e a s e k e e p i n m i n d t h a t m a n y o f t h e m a r e c o d e f r a g m e n t s r a t h e r t h a n c o m p l e t e p r o g r a m s .
T h e s e e x a m p l e s o f t e n r e f l e c t t h e s t y l e a n d p r e f e r e n c e o f t h e a u t h o r o f t h a t p i e c e o f t h e d o c u m e n t a t i o n , a n d m a y b e b r i e f e r t h a n a c o r r e s p o n d i n g l i n e o f c o d e i n a r e a l p r o g r a m . E x c e p t w h e r e o t h e r w i s e n o t e d , y o u s h o u l d a s s u m e t h a t u s e s t r i c t
a n d u s e w a r n i n g s
s t a t e m e n t s a p p e a r e a r l i e r i n t h e " p r o g r a m " , a n d t h a t a n y v a r i a b l e s u s e d h a v e a l r e a d y b e e n d e c l a r e d , e v e n i f t h o s e d e c l a r a t i o n s h a v e b e e n o m i t t e d t o m a k e t h e e x a m p l e e a s i e r t o r e a d .
D o n o t e t h a t t h e e x a m p l e s h a v e b e e n w r i t t e n b y m a n y d i f f e r e n t a u t h o r s o v e r a p e r i o d o f s e v e r a l d e c a d e s . S t y l e s a n d t e c h n i q u e s w i l l t h e r e f o r e d i f f e r , a l t h o u g h s o m e e f f o r t h a s b e e n m a d e t o n o t v a r y s t y l e s t o o w i d e l y i n t h e s a m e s e c t i o n s . D o n o t c o n s i d e r o n e s t y l e t o b e b e t t e r t h a n o t h e r s - " T h e r e ' s M o r e T h a n O n e W a y T o D o I t " i s o n e o f P e r l ' s m o t t o s . A f t e r a l l , i n y o u r j o u r n e y a s a p r o g r a m m e r , y o u a r e l i k e l y t o e n c o u n t e r d i f f e r e n t s t y l e s .
# W h a t i s P e r l ?
P e r l i s a g e n e r a l - p u r p o s e p r o g r a m m i n g l a n g u a g e o r i g i n a l l y d e v e l o p e d f o r t e x t m a n i p u l a t i o n a n d n o w u s e d f o r a w i d e r a n g e o f t a s k s i n c l u d i n g s y s t e m a d m i n i s t r a t i o n , w e b d e v e l o p m e n t , n e t w o r k p r o g r a m m i n g , G U I d e v e l o p m e n t , a n d m o r e .
T h e l a n g u a g e i s i n t e n d e d t o b e p r a c t i c a l ( e a s y t o u s e , e f f i c i e n t , c o m p l e t e ) r a t h e r t h a n b e a u t i f u l ( t i n y , e l e g a n t , m i n i m a l ) . I t s m a j o r f e a t u r e s a r e t h a t i t ' s e a s y t o u s e , s u p p o r t s b o t h p r o c e d u r a l a n d o b j e c t - o r i e n t e d ( OO ) p r o g r a m m i n g , h a s p o w e r f u l b u i l t - i n s u p p o r t f o r t e x t p r o c e s s i n g , a n d h a s o n e o f t h e w o r l d ' s m o s t i m p r e s s i v e c o l l e c t i o n s o f t h i r d - p a r t y m o d u l e s .
D i f f e r e n t d e f i n i t i o n s o f P e r l a r e g i v e n i n p e r l , p e r l f a q 1 a n d n o d o u b t o t h e r p l a c e s . F r o m t h i s w e c a n d e t e r m i n e t h a t P e r l i s d i f f e r e n t t h i n g s t o d i f f e r e n t p e o p l e , b u t t h a t l o t s o f p e o p l e t h i n k i t ' s a t l e a s t w o r t h w r i t i n g a b o u t .
# R u n n i n g P e r l p r o g r a m s
T o r u n a P e r l p r o g r a m f r o m t h e U n i x c o m m a n d l i n e :
perl progname.pl
A l t e r n a t i v e l y , p u t t h i s a s t h e f i r s t l i n e o f y o u r s c r i p t :
#!/usr/bin/env perl
. . . a n d r u n t h e s c r i p t a s / p a t h / t o / s c r i p t . p l . O f c o u r s e , i t ' l l n e e d t o b e e x e c u t a b l e f i r s t , s o c
h m o d 7 5 5 s c r i p t . p l
( u n d e r U n i x ) .
( T h i s s t a r t l i n e a s s u m e s y o u h a v e t h e e n v p r o g r a m . Y o u c a n a l s o p u t d i r e c t l y t h e p a t h t o y o u r p e r l e x e c u t a b l e , l i k e i n # ! / u s r / b i
n / p e r l
) .
F o r m o r e i n f o r m a t i o n , i n c l u d i n g i n s t r u c t i o n s f o r o t h e r p l a t f o r m s s u c h a s W i n d o w s , r e a d p e r l r u n .
# S a f e t y n e t
P e r l b y d e f a u l t i s v e r y f o r g i v i n g . I n o r d e r t o m a k e i t m o r e r o b u s t i t i s r e c o m m e n d e d t o s t a r t e v e r y p r o g r a m w i t h t h e f o l l o w i n g l i n e s :
#!/usr/bin/perl
use strict;
use warnings;
T h e t w o a d d i t i o n a l l i n e s r e q u e s t f r o m p e r l t o c a t c h v a r i o u s c o m m o n p r o b l e m s i n y o u r c o d e . T h e y c h e c k d i f f e r e n t t h i n g s s o y o u n e e d b o t h . A p o t e n t i a l p r o b l e m c a u g h t b y u s e s t r i c t ;
w i l l c a u s e y o u r c o d e t o s t o p i m m e d i a t e l y w h e n i t i s e n c o u n t e r e d , w h i l e u s e w a r
n i n g s ;
w i l l m e r e l y g i v e a w a r n i n g ( l i k e t h e c o m m a n d - l i n e s w i t c h - w ) a n d l e t y o u r c o d e r u n . T o r e a d m o r e a b o u t t h e m , c h e c k t h e i r r e s p e c t i v e m a n u a l p a g e s a t s t r i c t a n d w a r n i n g s .
A u s e v 5 . 3 5
( o r h i g h e r ) d e c l a r a t i o n w i l l e n a b l e b o t h s t r i c t
a n d w
a r n i n g s
:
#!/usr/bin/perl
use v5.35;
I n a d d i t i o n t o e n a b l i n g t h e s t r i
c t
a n d w a r n i n g s
p r a g m a t a , t h i s d e c l a r a t i o n w i l l a l s o a c t i v a t e a " f e a t u r e b u n d l e " ; a c o l l e c t i o n o f n a m e d f e a t u r e s t h a t e n a b l e m a n y o f t h e m o r e r e c e n t a d d i t i o n s a n d c h a n g e s t o t h e l a n g u a g e , a s w e l l a s o c c a s i o n a l l y r e m o v i n g o l d e r f e a t u r e s f o u n d t o h a v e b e e n m i s t a k e s i n d e s i g n a n d d i s c o u r a g e d .
# B a s i c s y n t a x o v e r v i e w
A P e r l s c r i p t o r p r o g r a m c o n s i s t s o f o n e o r m o r e s t a t e m e n t s . T h e s e s t a t e m e n t s a r e s i m p l y w r i t t e n i n t h e s c r i p t i n a s t r a i g h t f o r w a r d f a s h i o n . T h e r e i s n o n e e d t o h a v e a m a i n ( )
f u n c t i o n o r a n y t h i n g o f t h a t k i n d .
P e r l s t a t e m e n t s e n d i n a s e m i - c o l o n :
print "Hello, world";
C o m m e n t s s t a r t w i t h a h a s h s y m b o l a n d r u n t o t h e e n d o f t h e l i n e
# This is a comment
W h i t e s p a c e i s i r r e l e v a n t :
print
"Hello, world"
;
. . . e x c e p t i n s i d e q u o t e d s t r i n g s :
# this would print with a linebreak in the middle
print "Hello
world";
D o u b l e q u o t e s o r s i n g l e q u o t e s m a y b e u s e d a r o u n d l i t e r a l s t r i n g s :
print "Hello, world";
print 'Hello, world';
H o w e v e r , o n l y d o u b l e q u o t e s " i n t e r p o l a t e " v a r i a b l e s a n d s p e c i a l c h a r a c t e r s s u c h a s n e w l i n e s ( \ n
) :
print "Hello, $name\n"; # works fine
print 'Hello, $name\n'; # prints $name\n literally
N u m b e r s d o n ' t n e e d q u o t e s a r o u n d t h e m :
print 42;
Y o u c a n u s e p a r e n t h e s e s f o r f u n c t i o n s ' a r g u m e n t s o r o m i t t h e m a c c o r d i n g t o y o u r p e r s o n a l t a s t e . T h e y a r e o n l y r e q u i r e d o c c a s i o n a l l y t o c l a r i f y i s s u e s o f p r e c e d e n c e .
print("Hello, world\n");
print "Hello, world\n";
M o r e d e t a i l e d i n f o r m a t i o n a b o u t P e r l s y n t a x c a n b e f o u n d i n p e r l s y n .
# P e r l v a r i a b l e t y p e s
P e r l h a s t h r e e m a i n v a r i a b l e t y p e s : s c a l a r s , a r r a y s , a n d h a s h e s .
# S c a l a r s
A s c a l a r r e p r e s e n t s a s i n g l e v a l u e :
my $animal = "camel";
my $answer = 42;
S c a l a r v a l u e s c a n b e s t r i n g s , i n t e g e r s o r f l o a t i n g p o i n t n u m b e r s , a n d P e r l w i l l a u t o m a t i c a l l y c o n v e r t b e t w e e n t h e m a s r e q u i r e d . Y o u h a v e t o d e c l a r e t h e m u s i n g t h e my
k e y w o r d t h e f i r s t t i m e y o u u s e t h e m . ( T h i s i s o n e o f t h e r e q u i r e m e n t s o f u s e s t r i c t ;
. )
S c a l a r v a l u e s c a n b e u s e d i n v a r i o u s w a y s :
print $animal;
print "The animal is $animal\n";
print "The square of $answer is ", $answer * $answer, "\n";
P e r l d e f i n e s a n u m b e r o f s p e c i a l s c a l a r s w i t h s h o r t n a m e s , o f t e n s i n g l e p u n c t u a t i o n m a r k s o r d i g i t s . T h e s e v a r i a b l e s a r e u s e d f o r a l l k i n d s o f p u r p o s e s , a n d a r e d o c u m e n t e d i n p e r l v a r . T h e o n l y o n e y o u n e e d t o k n o w a b o u t f o r n o w i s $ _
w h i c h i s t h e " d e f a u l t v a r i a b l e " . I t ' s u s e d a s t h e d e f a u l t a r g u m e n t t o a n u m b e r o f f u n c t i o n s i n P e r l , a n d i t ' s s e t i m p l i c i t l y b y c e r t a i n l o o p i n g c o n s t r u c t s .
print; # prints contents of $_ by default
# A r r a y s
A n a r r a y r e p r e s e n t s a l i s t o f v a l u e s :
my @animals = ("camel", "llama", "owl");
my @numbers = (23, 42, 69);
my @mixed = ("camel", 42, 1.23);
A r r a y s a r e z e r o - i n d e x e d . H e r e ' s h o w y o u g e t a t e l e m e n t s i n a n a r r a y :
print $animals[0]; # prints "camel"
print $animals[1 ]; # prints "llama"
T h e s p e c i a l v a r i a b l e $ # a r r a y
t e l l s y o u t h e i n d e x o f t h e l a s t e l e m e n t o f a n a r r a y :
print $mixed[$#mixed]; # last element, prints 1.23
Y o u m i g h t b e t e m p t e d t o u s e $ # a
r r a y + 1
t o t e l l y o u h o w m a n y i t e m s t h e r e a r e i n a n a r r a y . D o n ' t b o t h e r . A s i t h a p p e n s , u s i n g @ a r r a y
w h e r e P e r l e x p e c t s t o f i n d a s c a l a r v a l u e ( " i n s c a l a r c o n t e x t " ) w i l l g i v e y o u t h e n u m b e r o f e l e m e n t s i n t h e a r r a y :
if (@animals <5 ) { ... }
T h e e l e m e n t s w e ' r e g e t t i n g f r o m t h e a r r a y s t a r t w i t h a $
b e c a u s e w e ' r e g e t t i n g j u s t a s i n g l e v a l u e o u t o f t h e a r r a y ; y o u a s k f o r a s c a l a r , y o u g e t a s c a l a r .
T o g e t m u l t i p l e v a l u e s f r o m a n a r r a y :
@animals[0,1]; # gives ("camel", "llama");
@animals[0..2]; # gives ("camel", "llama", "owl");
@animals[1..$#animals]; # gives all except the first element
T h i s i s c a l l e d a n " a r r a y s l i c e " .
Y o u c a n d o v a r i o u s u s e f u l t h i n g s t o l i s t s :
my @sorted = sort @animals;
my @backwards = reverse @numbers;
T h e r e a r e a c o u p l e o f s p e c i a l a r r a y s t o o , s u c h a s @ A R G V
( t h e c o m m a n d l i n e a r g u m e n t s t o y o u r s c r i p t ) a n d @ _
( t h e a r g u m e n t s p a s s e d t o a s u b r o u t i n e ) . T h e s e a r e d o c u m e n t e d i n p e r l v a r .
# H a s h e s
A h a s h r e p r e s e n t s a s e t o f k e y / v a l u e p a i r s :
my %fruit_color = ("apple", "red", "banana", "yellow");
Y o u c a n u s e w h i t e s p a c e a n d t h e = >
o p e r a t o r t o l a y t h e m o u t m o r e n i c e l y :
my %fruit_color = (
apple => "red",
banana => "yellow",
);
T o g e t a t h a s h e l e m e n t s :
$fruit_color{"apple"}; # gives "red"
Y o u c a n g e t a t l i s t s o f k e y s a n d v a l u e s w i t h k e y s ( )
a n d v a l u e s
( )
.
my @fruits = keys %fruit_color;
my @colors = values %fruit_color;
H a s h e s h a v e n o p a r t i c u l a r i n t e r n a l o r d e r , t h o u g h y o u c a n s o r t t h e k e y s a n d l o o p t h r o u g h t h e m .
J u s t l i k e s p e c i a l s c a l a r s a n d a r r a y s , t h e r e a r e a l s o s p e c i a l h a s h e s . T h e m o s t w e l l k n o w n o f t h e s e i s % E N V
w h i c h c o n t a i n s e n v i r o n m e n t v a r i a b l e s . R e a d a l l a b o u t i t ( a n d o t h e r s p e c i a l v a r i a b l e s ) i n p e r l v a r .
S c a l a r s , a r r a y s a n d h a s h e s a r e d o c u m e n t e d m o r e f u l l y i n p e r l d a t a .
M o r e c o m p l e x d a t a t y p e s c a n b e c o n s t r u c t e d u s i n g r e f e r e n c e s , w h i c h a l l o w y o u t o b u i l d l i s t s a n d h a s h e s w i t h i n l i s t s a n d h a s h e s .
A r e f e r e n c e i s a s c a l a r v a l u e a n d c a n r e f e r t o a n y o t h e r P e r l d a t a t y p e . S o b y s t o r i n g a r e f e r e n c e a s t h e v a l u e o f a n a r r a y o r h a s h e l e m e n t , y o u c a n e a s i l y c r e a t e l i s t s a n d h a s h e s w i t h i n l i s t s a n d h a s h e s . T h e f o l l o w i n g e x a m p l e s h o w s a 2 l e v e l h a s h o f h a s h s t r u c t u r e u s i n g a n o n y m o u s h a s h r e f e r e n c e s .
my $variables = {
scalar => {
description => "single item",
sigil => '$',
},
array => {
description => "ordered list of items",
sigil => '@',
},
hash => {
description => "key/value pairs",
sigil => '%',
},
};
print "Scalars begin with a $variables->{'scalar'}->{'sigil'}\n";
E x h a u s t i v e i n f o r m a t i o n o n t h e t o p i c o f r e f e r e n c e s c a n b e f o u n d i n p e r l r e f t u t , p e r l l o l , p e r l r e f a n d p e r l d s c .
# V a r i a b l e s c o p i n g
T h r o u g h o u t t h e p r e v i o u s s e c t i o n a l l t h e e x a m p l e s h a v e u s e d t h e s y n t a x :
my $var = "value";
T h e my
i s a c t u a l l y n o t r e q u i r e d ; y o u c o u l d j u s t u s e :
$var = "value";
H o w e v e r , t h e a b o v e u s a g e w i l l c r e a t e g l o b a l v a r i a b l e s t h r o u g h o u t y o u r p r o g r a m , w h i c h i s b a d p r o g r a m m i n g p r a c t i c e . my
c r e a t e s l e x i c a l l y s c o p e d v a r i a b l e s i n s t e a d . T h e v a r i a b l e s a r e s c o p e d t o t h e b l o c k ( i . e . a b u n c h o f s t a t e m e n t s s u r r o u n d e d b y c u r l y - b r a c e s ) i n w h i c h t h e y a r e d e f i n e d .
my $x = "foo";
my $some_condition = 1;
if ($some_condition) {
my $y = "bar";
print $x; # prints "foo"
print $y; # prints "bar"
}
print $x; # prints "foo"
print $y; # prints nothing; $y has fallen out of scope
U s i n g my
i n c o m b i n a t i o n w i t h a u s e
s t r i c t ;
a t t h e t o p o f y o u r P e r l s c r i p t s m e a n s t h a t t h e i n t e r p r e t e r w i l l p i c k u p c e r t a i n c o m m o n p r o g r a m m i n g e r r o r s . F o r i n s t a n c e , i n t h e e x a m p l e a b o v e , t h e f i n a l p r i n t $ y
w o u l d c a u s e a c o m p i l e - t i m e e r r o r a n d p r e v e n t y o u f r o m r u n n i n g t h e p r o g r a m . U s i n g s t r i c
t
i s h i g h l y r e c o m m e n d e d .
# C o n d i t i o n a l a n d l o o p i n g c o n s t r u c t s
P e r l h a s m o s t o f t h e u s u a l c o n d i t i o n a l a n d l o o p i n g c o n s t r u c t s .
T h e c o n d i t i o n s c a n b e a n y P e r l e x p r e s s i o n . S e e t h e l i s t o f o p e r a t o r s i n t h e n e x t s e c t i o n f o r i n f o r m a t i o n o n c o m p a r i s o n a n d b o o l e a n l o g i c o p e r a t o r s , w h i c h a r e c o m m o n l y u s e d i n c o n d i t i o n a l s t a t e m e n t s .
# if
if ( condition ) {
...
} elsif ( other condition ) {
...
} else {
...
}
T h e r e ' s a l s o a n e g a t e d v e r s i o n o f i t :
unless ( condition ) {
...
}
T h i s i s p r o v i d e d a s a m o r e r e a d a b l e v e r s i o n o f i f ( ! c o n d i t i o n )
.
N o t e t h a t t h e b r a c e s a r e r e q u i r e d i n P e r l , e v e n i f y o u ' v e o n l y g o t o n e l i n e i n t h e b l o c k . H o w e v e r , t h e r e i s a c l e v e r w a y o f m a k i n g y o u r o n e - l i n e c o n d i t i o n a l b l o c k s m o r e E n g l i s h l i k e :
# the traditional way
if ($zippy) {
print "Yow!";
}
# the Perlish post-condition way
print "Yow!" if $zippy;
print "We have no bananas" unless $bananas;
# w h i l e
while ( condition ) {
...
}
T h e r e ' s a l s o a n e g a t e d v e r s i o n , f o r t h e s a m e r e a s o n w e h a v e u n
l e s s
:
until ( condition ) {
...
}
Y o u c a n a l s o u s e w h i l e
i n a p o s t - c o n d i t i o n :
print "LA LA LA\n" while 1; # loops forever
# f o r
E x a c t l y l i k e C :
for ($i = 0; $i <= $max; $i++) {
...
}
T h e C s t y l e f o r l o o p i s r a r e l y n e e d e d i n P e r l s i n c e P e r l p r o v i d e s t h e m o r e f r i e n d l y l i s t s c a n n i n g f o r e a c h
l o o p .
# f o r e a c h
foreach (@array) {
print "This element is $_\n";
}
print $list[$_] foreach 0 .. $max;
# you don't have to use the default $_ either...
foreach my $key (keys %hash) {
print "The value of $key is $hash{$key}\n";
}
T h e f o r e a c h
k e y w o r d i s a c t u a l l y a s y n o n y m f o r t h e f o r
k e y w o r d . S e e " F o r e a c h L o o p s " i n p e r l s y n
.
F o r m o r e d e t a i l o n l o o p i n g c o n s t r u c t s ( a n d s o m e t h a t w e r e n ' t m e n t i o n e d i n t h i s o v e r v i e w ) s e e p e r l s y n .
# B u i l t i n o p e r a t o r s a n d f u n c t i o n s
P e r l c o m e s w i t h a w i d e s e l e c t i o n o f b u i l t i n f u n c t i o n s . S o m e o f t h e o n e s w e ' v e a l r e a d y s e e n i n c l u d e p r i n t
, s o r t
a n d r e v e r s e
. A l i s t o f t h e m i s g i v e n a t t h e s t a r t o f p e r l f u n c a n d y o u c a n e a s i l y r e a d a b o u t a n y g i v e n f u n c t i o n b y u s i n g p e r l d o c - f f u n c t i o n n a m e
.
P e r l o p e r a t o r s a r e d o c u m e n t e d i n f u l l i n p e r l o p , b u t h e r e a r e a f e w o f t h e m o s t c o m m o n o n e s :
# A r i t h m e t i c
+ addition
- subtraction
* multiplication
/ division
# N u m e r i c c o m p a r i s o n
== equality
!= inequality
< less than
> greater than
<= less than or equal
>= greater than or equal
# S t r i n g c o m p a r i s o n
eq equality
ne inequality
lt less than
gt greater than
le less than or equal
ge greater than or equal
( W h y d o w e h a v e s e p a r a t e n u m e r i c a n d s t r i n g c o m p a r i s o n s ? B e c a u s e w e d o n ' t h a v e s p e c i a l v a r i a b l e t y p e s , a n d P e r l n e e d s t o k n o w w h e t h e r t o s o r t n u m e r i c a l l y ( w h e r e 9 9 i s l e s s t h a n 1 0 0 ) o r a l p h a b e t i c a l l y ( w h e r e 1 0 0 c o m e s b e f o r e 9 9 ) .
# B o o l e a n l o g i c
&& and
|| or
! not
( a n d
, or
a n d n o t
a r e n ' t j u s t i n t h e a b o v e t a b l e a s d e s c r i p t i o n s o f t h e o p e r a t o r s . T h e y ' r e a l s o s u p p o r t e d a s o p e r a t o r s i n t h e i r o w n r i g h t . T h e y ' r e m o r e r e a d a b l e t h a n t h e C - s t y l e o p e r a t o r s , b u t h a v e d i f f e r e n t p r e c e d e n c e t o & &
a n d f r i e n d s . C h e c k p e r l o p f o r m o r e d e t a i l . )
# M i s c e l l a n e o u s
= assignment
. string concatenation
x string multiplication (repeats strings)
.. range operator (creates a list of numbers or strings)
M a n y o p e r a t o r s c a n b e c o m b i n e d w i t h a =
a s f o l l o w s :
$x += 1; # same as $x = $x + 1
$x -= 1; # same as $x = $x - 1
$x .= "\n"; # same as $x = $x . "\n";
# F i l e s a n d I / O
Y o u c a n o p e n a f i l e f o r i n p u t o r o u t p u t u s i n g t h e o p e n ( )
f u n c t i o n . I t ' s d o c u m e n t e d i n e x t r a v a g a n t d e t a i l i n p e r l f u n c a n d p e r l o p e n t u t , b u t i n s h o r t :
open(my $in, "<", "input.txt") or die "Can't open input.txt: $!";
open(my $out, ">", "output.txt") or die "Can't open output.txt: $!";
open(my $log, ">>", "my.log") or die "Can't open my.log: $!";
Y o u c a n r e a d f r o m a n o p e n f i l e h a n d l e u s i n g t h e < >
o p e r a t o r . I n s c a l a r c o n t e x t i t r e a d s a s i n g l e l i n e f r o m t h e f i l e h a n d l e , a n d i n l i s t c o n t e x t i t r e a d s t h e w h o l e f i l e i n , a s s i g n i n g e a c h l i n e t o a n e l e m e n t o f t h e l i s t :
my $line = <$in>;
my @lines = <$in>;
R e a d i n g i n t h e w h o l e f i l e a t o n e t i m e i s c a l l e d s l u r p i n g . I t c a n b e u s e f u l b u t i t m a y b e a m e m o r y h o g . M o s t t e x t f i l e p r o c e s s i n g c a n b e d o n e a l i n e a t a t i m e w i t h P e r l ' s l o o p i n g c o n s t r u c t s .
T h e < >
o p e r a t o r i s m o s t o f t e n s e e n i n a w h i l e
l o o p :
while (<$in>) { # assigns each line in turn to $_
print "Just read in this line: $_";
}
W e ' v e a l r e a d y s e e n h o w t o p r i n t t o s t a n d a r d o u t p u t u s i n g p r i n t ( )
. H o w e v e r , p r i n t ( )
c a n a l s o t a k e a n o p t i o n a l f i r s t a r g u m e n t s p e c i f y i n g w h i c h f i l e h a n d l e t o p r i n t t o :
print STDERR "This is your final warning.\n";
print $out $record;
print $log $logmessage;
W h e n y o u ' r e d o n e w i t h y o u r f i l e h a n d l e s , y o u s h o u l d c l o s e ( )
t h e m ( t h o u g h t o b e h o n e s t , P e r l w i l l c l e a n u p a f t e r y o u i f y o u f o r g e t ) :
close $in or die "$in: $!";
# R e g u l a r e x p r e s s i o n s
P e r l ' s r e g u l a r e x p r e s s i o n s u p p o r t i s b o t h b r o a d a n d d e e p , a n d i s t h e s u b j e c t o f l e n g t h y d o c u m e n t a t i o n i n p e r l r e q u i c k , p e r l r e t u t , a n d e l s e w h e r e . H o w e v e r , i n s h o r t :
# S i m p l e m a t c h i n g
if (/foo/) { ... } # true if $_ contains "foo"
if ($x =~ /foo/) { ... } # true if $x contains "foo"
T h e / /
m a t c h i n g o p e r a t o r i s d o c u m e n t e d i n p e r l o p . I t o p e r a t e s o n $ _
b y d e f a u l t , o r c a n b e b o u n d t o a n o t h e r v a r i a b l e u s i n g t h e = ~
b i n d i n g o p e r a t o r ( a l s o d o c u m e n t e d i n p e r l o p ) .
# S i m p l e s u b s t i t u t i o n
s/foo/bar/; # replaces foo with bar in $_
$x =~ s/foo/bar/; # replaces foo with bar in $x
$x =~ s/foo/bar/g; # replaces ALL INSTANCES of foo with bar
# in $x
T h e s / / /
s u b s t i t u t i o n o p e r a t o r i s d o c u m e n t e d i n p e r l o p .
# M o r e c o m p l e x r e g u l a r e x p r e s s i o n s
Y o u d o n ' t j u s t h a v e t o m a t c h o n f i x e d s t r i n g s . I n f a c t , y o u c a n m a t c h o n j u s t a b o u t a n y t h i n g y o u c o u l d d r e a m o f b y u s i n g m o r e c o m p l e x r e g u l a r e x p r e s s i o n s . T h e s e a r e d o c u m e n t e d a t g r e a t l e n g t h i n p e r l r e , b u t f o r t h e m e a n t i m e , h e r e ' s a q u i c k c h e a t s h e e t :
. a single character
\s a whitespace character (space, tab, newline,
...)
\S non-whitespace character
\d a digit (0-9)
\D a non-digit
\w a word character (a-z, A-Z, 0-9, _)
\W a non-word character
[aeiou] matches a single character in the given set
[^aeiou] matches a single character outside the given
set
(foo|bar|baz) matches any of the alternatives specified
^ start of string
$ end of string
Q u a n t i f i e r s c a n b e u s e d t o s p e c i f y h o w m a n y o f t h e p r e v i o u s t h i n g y o u w a n t t o m a t c h o n , w h e r e " t h i n g " m e a n s e i t h e r a l i t e r a l c h a r a c t e r , o n e o f t h e m e t a c h a r a c t e r s l i s t e d a b o v e , o r a g r o u p o f c h a r a c t e r s o r m e t a c h a r a c t e r s i n p a r e n t h e s e s .
* zero or more of the previous thing
+ one or more of the previous thing
? zero or one of the previous thing
{3} matches exactly 3 of the previous thing
{3,6} matches between 3 and 6 of the previous thing
{3,} matches 3 or more of the previous thing
S o m e b r i e f e x a m p l e s :
/^\d+/ string starts with one or more digits
/^$/ nothing in the string (start and end are
adjacent)
/(\d\s){3}/ three digits, each followed by a whitespace
character (eg "3 4 5 ")
/(a.)+/ matches a string in which every odd-numbered
letter is a (eg "abacadaf")
# This loop reads from STDIN, and prints non-blank lines:
while (<>) {
next if /^$/;
print;
}
# P a r e n t h e s e s f o r c a p t u r i n g
A s w e l l a s g r o u p i n g , p a r e n t h e s e s s e r v e a s e c o n d p u r p o s e . T h e y c a n b e u s e d t o c a p t u r e t h e r e s u l t s o f p a r t s o f t h e r e g e x p m a t c h f o r l a t e r u s e . T h e r e s u l t s e n d u p i n $ 1
, $ 2
a n d s o o n .
# a cheap and nasty way to break an email address up into parts
if ($email =~ /([^@]+)@(.+)/) {
print "Username is $1\n";
print "Hostname is $2\n";
}
# O t h e r r e g e x p f e a t u r e s
P e r l r e g e x p s a l s o s u p p o r t b a c k r e f e r e n c e s , l o o k a h e a d s , a n d a l l k i n d s o f o t h e r c o m p l e x d e t a i l s . R e a d a l l a b o u t t h e m i n p e r l r e q u i c k , p e r l r e t u t , a n d p e r l r e .
# W r i t i n g s u b r o u t i n e s
W r i t i n g s u b r o u t i n e s i s e a s y :
sub logger {
my $logmessage = shift;
open my $logfile, ">>", "my.log" or die "Could not open my.log: $!";
print $logfile $logmessage;
}
N o w w e c a n u s e t h e s u b r o u t i n e j u s t a s a n y o t h e r b u i l t - i n f u n c t i o n :
logger("We have a logger subroutine!");
W h a t ' s t h a t s h i f t
? W e l l , t h e a r g u m e n t s t o a s u b r o u t i n e a r e a v a i l a b l e t o u s a s a s p e c i a l a r r a y c a l l e d @ _
( s e e p e r l v a r f o r m o r e o n t h a t ) . T h e d e f a u l t a r g u m e n t t o t h e s h i f t
f u n c t i o n j u s t h a p p e n s t o b e @ _
. S o m y $ l o g m e s s a g e = s h
i f t ;
s h i f t s t h e f i r s t i t e m o f f t h e l i s t o f a r g u m e n t s a n d a s s i g n s i t t o $ l o g m e s s a g e
.
W e c a n m a n i p u l a t e @ _
i n o t h e r w a y s t o o :
my ($logmessage, $priority) = @_; # common
my $logmessage = $_[0]; # uncommon, and ugly
S u b r o u t i n e s c a n a l s o r e t u r n v a l u e s :
sub square {
my $num = shift;
my $result = $num * $num;
return $result;
}
T h e n u s e i t l i k e :
$sq = square(8 );
F o r m o r e i n f o r m a t i o n o n w r i t i n g s u b r o u t i n e s , s e e p e r l s u b .
# O O P e r l
O O P e r l i s r e l a t i v e l y s i m p l e a n d i s i m p l e m e n t e d u s i n g r e f e r e n c e s w h i c h k n o w w h a t s o r t o f o b j e c t t h e y a r e b a s e d o n P e r l ' s c o n c e p t o f p a c k a g e s . H o w e v e r , O O P e r l i s l a r g e l y b e y o n d t h e s c o p e o f t h i s d o c u m e n t . R e a d p e r l o o t u t a n d p e r l o b j .
A s a b e g i n n i n g P e r l p r o g r a m m e r , y o u r m o s t c o m m o n u s e o f O O P e r l w i l l b e i n u s i n g t h i r d - p a r t y m o d u l e s , w h i c h a r e d o c u m e n t e d b e l o w .
# U s i n g P e r l m o d u l e s
P e r l m o d u l e s p r o v i d e a r a n g e o f f e a t u r e s t o h e l p y o u a v o i d r e i n v e n t i n g t h e w h e e l , a n d c a n b e d o w n l o a d e d f r o m C P A N . A n u m b e r o f p o p u l a r m o d u l e s a r e i n c l u d e d w i t h t h e P e r l d i s t r i b u t i o n i t s e l f .
C a t e g o r i e s o f m o d u l e s r a n g e f r o m t e x t m a n i p u l a t i o n t o n e t w o r k p r o t o c o l s t o d a t a b a s e i n t e g r a t i o n t o g r a p h i c s . A c a t e g o r i z e d l i s t o f m o d u l e s i s a l s o a v a i l a b l e f r o m C P A N .
T o l e a r n h o w t o i n s t a l l m o d u l e s y o u d o w n l o a d f r o m C P A N , r e a d p e r l m o d i n s t a l l .
T o l e a r n h o w t o u s e a p a r t i c u l a r m o d u l e , u s e p e r l d o c M o d u l e : : N a m
e
. T y p i c a l l y y o u w i l l w a n t t o u s
e M o d u l e : : N a m e
, w h i c h w i l l t h e n g i v e y o u a c c e s s t o e x p o r t e d f u n c t i o n s o r a n O O i n t e r f a c e t o t h e m o d u l e .
p e r l f a q c o n t a i n s q u e s t i o n s a n d a n s w e r s r e l a t e d t o m a n y c o m m o n t a s k s , a n d o f t e n p r o v i d e s s u g g e s t i o n s f o r g o o d C P A N m o d u l e s t o u s e .
p e r l m o d d e s c r i b e s P e r l m o d u l e s i n g e n e r a l . p e r l m o d l i b l i s t s t h e m o d u l e s w h i c h c a m e w i t h y o u r P e r l i n s t a l l a t i o n .
I f y o u f e e l t h e u r g e t o w r i t e P e r l m o d u l e s , p e r l n e w m o d w i l l g i v e y o u g o o d a d v i c e .
# A U T H O R
K i r r i l y " S k u d " R o b e r t < s k u d @ c p a n . o r g >
P e r l d o c B r o w s e r i s m a i n t a i n e d b y D a n B o o k ( D B O O K ) . P l e a s e c o n t a c t h i m v i a t h e G i t H u b i s s u e t r a c k e r or e m a i l r e g a r d i n g a n y i s s u e s w i t h t h e s i t e i t s e l f , s e a r c h , o r r e n d e r i n g o f d o c u m e n t a t i o n .
T h e P e r l d o c u m e n t a t i o n i s m a i n t a i n e d b y t h e P e r l 5 P o r t e r s i n t h e d e v e l o p m e n t o f P e r l . P l e a s e c o n t a c t t h e m v i a t h e P e r l i s s u e t r a c k e r , t h e m a i l i n g l i s t , o r I R C t o r e p o r t a n y i s s u e s w i t h t h e c o n t e n t s o r f o r m a t o f t h e d o c u m e n t a t i o n .