P e r l d o c B r o w s e r
●
5 . 0 0 5
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 o p
( 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 . 0 0 5 .
V i e w t h e l a t e s t v e r s i o n
●
N A M E
●
S Y N O P S I S
●
D E S C R I P T I O N
●
T e r m s a n d L i s t O p e r a t o r s ( L e f t w a r d )
●
T h e A r r o w O p e r a t o r
●
A u t o - i n c r e m e n t a n d A u t o - d e c r e m e n t
●
E x p o n e n t i a t i o n
●
S y m b o l i c U n a r y O p e r a t o r s
●
B i n d i n g O p e r a t o r s
●
M u l t i p l i c a t i v e O p e r a t o r s
●
A d d i t i v e O p e r a t o r s
●
S h i f t O p e r a t o r s
●
N a m e d U n a r y O p e r a t o r s
●
R e l a t i o n a l O p e r a t o r s
●
E q u a l i t y O p e r a t o r s
●
B i t w i s e A n d
●
B i t w i s e O r a n d E x c l u s i v e O r
●
C - s t y l e L o g i c a l A n d
●
C - s t y l e L o g i c a l O r
●
R a n g e O p e r a t o r s
●
C o n d i t i o n a l O p e r a t o r
●
A s s i g n m e n t O p e r a t o r s
●
C o m m a O p e r a t o r
●
L i s t O p e r a t o r s ( R i g h t w a r d )
●
L o g i c a l N o t
●
L o g i c a l A n d
●
L o g i c a l o r a n d E x c l u s i v e O r
●
C O p e r a t o r s M i s s i n g F r o m P e r l
●
Q u o t e a n d Q u o t e - l i k e O p e r a t o r s
●
R e g e x p Q u o t e - L i k e O p e r a t o r s
●
G o r y d e t a i l s o f p a r s i n g q u o t e d c o n s t r u c t s
●
I / O O p e r a t o r s
●
C o n s t a n t F o l d i n g
●
B i t w i s e S t r i n g O p e r a t o r s
●
I n t e g e r A r i t h m e t i c
●
F l o a t i n g - p o i n t A r i t h m e t i c
●
B i g g e r N u m b e r s
# N A M E
p e r l o p - P e r l o p e r a t o r s a n d p r e c e d e n c e
# S Y N O P S I S
P e r l o p e r a t o r s h a v e t h e f o l l o w i n g a s s o c i a t i v i t y a n d p r e c e d e n c e , l i s t e d f r o m h i g h e s t p r e c e d e n c e t o l o w e s t . N o t e t h a t a l l o p e r a t o r s b o r r o w e d f r o m C k e e p t h e s a m e p r e c e d e n c e r e l a t i o n s h i p w i t h e a c h o t h e r , e v e n w h e r e C ' s p r e c e d e n c e i s s l i g h t l y s c r e w y . ( T h i s m a k e s l e a r n i n g P e r l e a s i e r f o r C f o l k s . ) W i t h v e r y f e w e x c e p t i o n s , t h e s e a l l o p e r a t e o n s c a l a r v a l u e s o n l y , n o t a r r a y v a l u e s .
left terms and list operators (leftward)
left ->
nonassoc ++ --
right **
right ! ~ \ and unary + and -
left =~ !~
left * / % x
left + - .
left << >>
nonassoc named unary operators
nonassoc < > <= >= lt gt le ge
nonassoc == != <=> eq ne cmp
left &
left | ^
left &&
left ||
nonassoc .. ...
right ?:
right = += -= *= etc.
left , =>
nonassoc list operators (rightward)
right not
left and
left or xor
I n t h e f o l l o w i n g s e c t i o n s , t h e s e o p e r a t o r s a r e c o v e r e d i n p r e c e d e n c e o r d e r .
M a n y o p e r a t o r s c a n b e o v e r l o a d e d f o r o b j e c t s . S e e o v e r l o a d .
# D E S C R I P T I O N
# T e r m s a n d L i s t O p e r a t o r s ( L e f t w a r d )
A T E R M h a s t h e h i g h e s t p r e c e d e n c e i n P e r l . T h e y i n c l u d e s v a r i a b l e s , q u o t e a n d q u o t e - l i k e o p e r a t o r s , a n y e x p r e s s i o n i n p a r e n t h e s e s , a n d a n y f u n c t i o n w h o s e a r g u m e n t s a r e p a r e n t h e s i z e d . A c t u a l l y , t h e r e a r e n ' t r e a l l y f u n c t i o n s i n t h i s s e n s e , j u s t l i s t o p e r a t o r s a n d u n a r y o p e r a t o r s b e h a v i n g a s f u n c t i o n s b e c a u s e y o u p u t p a r e n t h e s e s a r o u n d t h e a r g u m e n t s . T h e s e a r e a l l d o c u m e n t e d i n p e r l f u n c .
I f a n y l i s t o p e r a t o r ( p r i n t ( ) , e t c . ) o r a n y u n a r y o p e r a t o r ( c h d i r ( ) , e t c . ) i s f o l l o w e d b y a l e f t p a r e n t h e s i s a s t h e n e x t t o k e n , t h e o p e r a t o r a n d a r g u m e n t s w i t h i n p a r e n t h e s e s a r e t a k e n t o b e o f h i g h e s t p r e c e d e n c e , j u s t l i k e a n o r m a l f u n c t i o n c a l l .
I n t h e a b s e n c e o f p a r e n t h e s e s , t h e p r e c e d e n c e o f l i s t o p e r a t o r s s u c h a s p r i n t
, s o r t
, o r c h m o d
i s e i t h e r v e r y h i g h o r v e r y l o w d e p e n d i n g o n w h e t h e r y o u a r e l o o k i n g a t t h e l e f t s i d e o r t h e r i g h t s i d e o f t h e o p e r a t o r . F o r e x a m p l e , i n
@ary = (1, 3, sort 4, 2);
print @ary; # prints 1324
t h e c o m m a s o n t h e r i g h t o f t h e s o r t a r e e v a l u a t e d b e f o r e t h e s o r t , b u t t h e c o m m a s o n t h e l e f t a r e e v a l u a t e d a f t e r . I n o t h e r w o r d s , l i s t o p e r a t o r s t e n d t o g o b b l e u p a l l t h e a r g u m e n t s t h a t f o l l o w t h e m , a n d t h e n a c t l i k e a s i m p l e T E R M w i t h r e g a r d t o t h e p r e c e d i n g e x p r e s s i o n . N o t e t h a t y o u h a v e t o b e c a r e f u l w i t h p a r e n t h e s e s :
# These evaluate exit before doing the print:
print($foo, exit); # Obviously not what you want.
print $foo, exit; # Nor is this.
# These do the print before evaluating exit:
(print $foo), exit; # This is what you want.
print($foo), exit; # Or this.
print ($foo), exit; # Or even this.
A l s o n o t e t h a t
print ($foo & 255) + 1, "\n";
p r o b a b l y d o e s n ' t d o w h a t y o u e x p e c t a t f i r s t g l a n c e . S e e " N a m e d U n a r y O p e r a t o r s " f o r m o r e d i s c u s s i o n o f t h i s .
A l s o p a r s e d a s t e r m s a r e t h e d o
{ }
a n d e v a l { }
c o n s t r u c t s , a s w e l l a s s u b r o u t i n e a n d m e t h o d c a l l s , a n d t h e a n o n y m o u s c o n s t r u c t o r s [ ]
a n d { }
.
S e e a l s o " Q u o t e a n d Q u o t e - l i k e O p e r a t o r s " t o w a r d t h e e n d o f t h i s s e c t i o n , a s w e l l a s " O O p e r a t o r s " " i n " I .
# T h e A r r o w O p e r a t o r
J u s t a s i n C a n d C + + , " - >
" i s a n i n f i x d e r e f e r e n c e o p e r a t o r . I f t h e r i g h t s i d e i s e i t h e r a [ . . . ]
or { . . . }
s u b s c r i p t , t h e n t h e l e f t s i d e m u s t b e e i t h e r a h a r d o r s y m b o l i c r e f e r e n c e t o a n a r r a y o r h a s h ( o r a l o c a t i o n c a p a b l e o f h o l d i n g a h a r d r e f e r e n c e , i f i t ' s a n l v a l u e ( a s s i g n a b l e ) ) . S e e p e r l r e f .
O t h e r w i s e , t h e r i g h t s i d e i s a m e t h o d n a m e o r a s i m p l e s c a l a r v a r i a b l e c o n t a i n i n g t h e m e t h o d n a m e , a n d t h e l e f t s i d e m u s t e i t h e r b e a n o b j e c t ( a b l e s s e d r e f e r e n c e ) o r a c l a s s n a m e ( t h a t i s , a p a c k a g e n a m e ) . S e e p e r l o b j .
# A u t o - i n c r e m e n t a n d A u t o - d e c r e m e n t
" + + " a n d " - - " w o r k a s i n C . T h a t i s , i f p l a c e d b e f o r e a v a r i a b l e , t h e y i n c r e m e n t o r d e c r e m e n t t h e v a r i a b l e b e f o r e r e t u r n i n g t h e v a l u e , a n d i f p l a c e d a f t e r , i n c r e m e n t o r d e c r e m e n t t h e v a r i a b l e a f t e r r e t u r n i n g t h e v a l u e .
T h e a u t o - i n c r e m e n t o p e r a t o r h a s a l i t t l e e x t r a b u i l t i n m a g i c t o i t . I f y o u i n c r e m e n t a v a r i a b l e t h a t i s n u m e r i c , o r t h a t h a s e v e r b e e n u s e d i n a n u m e r i c c o n t e x t , y o u g e t a n o r m a l i n c r e m e n t . I f , h o w e v e r , t h e v a r i a b l e h a s b e e n u s e d i n o n l y s t r i n g c o n t e x t s s i n c e i t w a s s e t , a n d h a s a v a l u e t h a t i s n o t t h e e m p t y s t r i n g a n d m a t c h e s t h e p a t t e r n / ^ [ a - z A - Z ] * [
0 - 9 ] * $ /
, t h e i n c r e m e n t i s d o n e a s a s t r i n g , p r e s e r v i n g e a c h c h a r a c t e r w i t h i n i t s r a n g e , w i t h c a r r y :
print ++($foo = '99'); # prints '100'
print ++($foo = 'a0'); # prints 'a1'
print ++($foo = 'Az'); # prints 'Ba'
print ++($foo = 'zz'); # prints 'aaa'
T h e a u t o - d e c r e m e n t o p e r a t o r i s n o t m a g i c a l .
# E x p o n e n t i a t i o n
B i n a r y " * * " i s t h e e x p o n e n t i a t i o n o p e r a t o r . N o t e t h a t i t b i n d s e v e n m o r e t i g h t l y t h a n u n a r y m i n u s , s o - 2 * * 4 i s - ( 2 * * 4 ) , n o t ( - 2 ) * * 4 . ( T h i s i s i m p l e m e n t e d u s i n g C ' s p o w ( 3 ) f u n c t i o n , w h i c h a c t u a l l y w o r k s o n d o u b l e s i n t e r n a l l y . )
# S y m b o l i c U n a r y O p e r a t o r s
U n a r y " ! " p e r f o r m s l o g i c a l n e g a t i o n , i . e . , " n o t " . S e e a l s o n o t
f o r a l o w e r p r e c e d e n c e v e r s i o n o f t h i s .
U n a r y " - " p e r f o r m s a r i t h m e t i c n e g a t i o n i f t h e o p e r a n d i s n u m e r i c . I f t h e o p e r a n d i s a n i d e n t i f i e r , a s t r i n g c o n s i s t i n g o f a m i n u s s i g n c o n c a t e n a t e d w i t h t h e i d e n t i f i e r i s r e t u r n e d . O t h e r w i s e , i f t h e s t r i n g s t a r t s w i t h a p l u s o r m i n u s , a s t r i n g s t a r t i n g w i t h t h e o p p o s i t e s i g n i s r e t u r n e d . O n e e f f e c t o f t h e s e r u l e s i s t h a t - b a r e w o r d
i s e q u i v a l e n t t o " - b a
r e w o r d "
.
U n a r y " ~ " p e r f o r m s b i t w i s e n e g a t i o n , i . e . , 1 ' s c o m p l e m e n t . F o r e x a m p l e , 0 6 6 6 & ~ 0 2 7
i s 0 6 4 0 . ( S e e a l s o " I n t e g e r A r i t h m e t i c " a n d " B i t w i s e S t r i n g O p e r a t o r s " . )
U n a r y " + " h a s n o e f f e c t w h a t s o e v e r , e v e n o n s t r i n g s . I t i s u s e f u l s y n t a c t i c a l l y f o r s e p a r a t i n g a f u n c t i o n n a m e f r o m a p a r e n t h e s i z e d e x p r e s s i o n t h a t w o u l d o t h e r w i s e b e i n t e r p r e t e d a s t h e c o m p l e t e l i s t o f f u n c t i o n a r g u m e n t s . ( S e e e x a m p l e s a b o v e u n d e r T e r m s a n d L i s t O p e r a t o r s ( L e f t w a r d ) . )
U n a r y " \ " c r e a t e s a r e f e r e n c e t o w h a t e v e r f o l l o w s i t . S e e p e r l r e f . D o n o t c o n f u s e t h i s b e h a v i o r w i t h t h e b e h a v i o r o f b a c k s l a s h w i t h i n a s t r i n g , a l t h o u g h b o t h f o r m s d o c o n v e y t h e n o t i o n o f p r o t e c t i n g t h e n e x t t h i n g f r o m i n t e r p r e t a t i o n .
# B i n d i n g O p e r a t o r s
B i n a r y " = ~ " b i n d s a s c a l a r e x p r e s s i o n t o a p a t t e r n m a t c h . C e r t a i n o p e r a t i o n s s e a r c h o r m o d i f y t h e s t r i n g $ _ b y d e f a u l t . T h i s o p e r a t o r m a k e s t h a t k i n d o f o p e r a t i o n w o r k o n s o m e o t h e r s t r i n g . T h e r i g h t a r g u m e n t i s a s e a r c h p a t t e r n , s u b s t i t u t i o n , o r t r a n s l i t e r a t i o n . T h e l e f t a r g u m e n t i s w h a t i s s u p p o s e d t o b e s e a r c h e d , s u b s t i t u t e d , o r t r a n s l i t e r a t e d i n s t e a d o f t h e d e f a u l t $ _ . T h e r e t u r n v a l u e i n d i c a t e s t h e s u c c e s s o f t h e o p e r a t i o n . ( I f t h e r i g h t a r g u m e n t i s a n e x p r e s s i o n r a t h e r t h a n a s e a r c h p a t t e r n , s u b s t i t u t i o n , o r t r a n s l i t e r a t i o n , i t i s i n t e r p r e t e d a s a s e a r c h p a t t e r n a t r u n t i m e . T h i s c a n b e i s l e s s e f f i c i e n t t h a n a n e x p l i c i t s e a r c h , b e c a u s e t h e p a t t e r n m u s t b e c o m p i l e d e v e r y t i m e t h e e x p r e s s i o n i s e v a l u a t e d .
B i n a r y " ! ~ " i s j u s t l i k e " = ~ " e x c e p t t h e r e t u r n v a l u e i s n e g a t e d i n t h e l o g i c a l s e n s e .
# M u l t i p l i c a t i v e O p e r a t o r s
B i n a r y " * " m u l t i p l i e s t w o n u m b e r s .
B i n a r y " / " d i v i d e s t w o n u m b e r s .
B i n a r y " % " c o m p u t e s t h e m o d u l u s o f t w o n u m b e r s . G i v e n i n t e g e r o p e r a n d s $ a
a n d $ b
: I f $ b
i s p o s i t i v e , t h e n $ a % $ b
is $ a
m i n u s t h e l a r g e s t m u l t i p l e o f $ b
t h a t i s n o t g r e a t e r t h a n $ a
. I f $ b
i s n e g a t i v e , t h e n $ a % $ b
is $ a
m i n u s t h e s m a l l e s t m u l t i p l e o f $ b
t h a t i s n o t l e s s t h a n $ a
( i . e . t h e r e s u l t w i l l b e l e s s t h a n o r e q u a l t o z e r o ) . N o t e t h a n w h e n u s e i n t e g e
r
i s i n s c o p e , " % " g i v e y o u d i r e c t a c c e s s t o t h e m o d u l u s o p e r a t o r a s i m p l e m e n t e d b y y o u r C c o m p i l e r . T h i s o p e r a t o r i s n o t a s w e l l d e f i n e d f o r n e g a t i v e o p e r a n d s , b u t i t w i l l e x e c u t e f a s t e r .
B i n a r y " x " i s t h e r e p e t i t i o n 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 t u r n s a s t r i n g c o n s i s t i n g o f t h e l e f t o p e r a n d r e p e a t e d t h e n u m b e r o f t i m e s s p e c i f i e d b y t h e r i g h t o p e r a n d . I n l i s t c o n t e x t , i f t h e l e f t o p e r a n d i s a l i s t i n p a r e n t h e s e s , i t r e p e a t s t h e l i s t .
print '-' x 80; # print row of dashes
print "\t" x ($tab/8), ' ' x ($tab%8); # tab over
@ones = (1 ) x 80; # a list of 80 1's
@ones = (5 ) x @ones; # set all elements to 5
# A d d i t i v e O p e r a t o r s
B i n a r y " + " r e t u r n s t h e s u m o f t w o n u m b e r s .
B i n a r y " - " r e t u r n s t h e d i f f e r e n c e o f t w o n u m b e r s .
B i n a r y " . " c o n c a t e n a t e s t w o s t r i n g s .
# S h i f t O p e r a t o r s
B i n a r y " < < " r e t u r n s t h e v a l u e o f i t s l e f t a r g u m e n t s h i f t e d l e f t b y t h e n u m b e r o f b i t s s p e c i f i e d b y t h e r i g h t a r g u m e n t . A r g u m e n t s s h o u l d b e i n t e g e r s . ( S e e a l s o " I n t e g e r A r i t h m e t i c " . )
B i n a r y " > > " r e t u r n s t h e v a l u e o f i t s l e f t a r g u m e n t s h i f t e d r i g h t b y t h e n u m b e r o f b i t s s p e c i f i e d b y t h e r i g h t a r g u m e n t . A r g u m e n t s s h o u l d b e i n t e g e r s . ( S e e a l s o " I n t e g e r A r i t h m e t i c " . )
# N a m e d U n a r y O p e r a t o r s
T h e v a r i o u s n a m e d u n a r y o p e r a t o r s a r e t r e a t e d a s f u n c t i o n s w i t h o n e a r g u m e n t , w i t h o p t i o n a l p a r e n t h e s e s . T h e s e i n c l u d e t h e f i l e t e s t o p e r a t o r s , l i k e - f
, - M
, e t c . S e e p e r l f u n c .
I f a n y l i s t o p e r a t o r ( p r i n t ( ) , e t c . ) o r a n y u n a r y o p e r a t o r ( c h d i r ( ) , e t c . ) i s f o l l o w e d b y a l e f t p a r e n t h e s i s a s t h e n e x t t o k e n , t h e o p e r a t o r a n d a r g u m e n t s w i t h i n p a r e n t h e s e s a r e t a k e n t o b e o f h i g h e s t p r e c e d e n c e , j u s t l i k e a n o r m a l f u n c t i o n c a l l . E x a m p l e s :
chdir $foo || die; # (chdir $foo) || die
chdir($foo) || die; # (chdir $foo) || die
chdir ($foo) || die; # (chdir $foo) || die
chdir +($foo) || die; # (chdir $foo) || die
b u t , b e c a u s e * i s h i g h e r p r e c e d e n c e t h a n | | :
chdir $foo * 20; # chdir ($foo * 20)
chdir($foo) * 20; # (chdir $foo) * 20
chdir ($foo) * 20; # (chdir $foo) * 20
chdir +($foo) * 20; # chdir ($foo * 20)
rand 10 * 20; # rand (10 * 20)
rand(10 ) * 20; # (rand 10) * 20
rand (10 ) * 20; # (rand 10) * 20
rand +(10 ) * 20; # rand (10 * 20)
S e e a l s o " T e r m s a n d L i s t O p e r a t o r s ( L e f t w a r d ) " .
# R e l a t i o n a l O p e r a t o r s
B i n a r y " < " r e t u r n s t r u e i f t h e l e f t a r g u m e n t i s n u m e r i c a l l y l e s s t h a n t h e r i g h t a r g u m e n t .
B i n a r y " > " r e t u r n s t r u e i f t h e l e f t a r g u m e n t i s n u m e r i c a l l y g r e a t e r t h a n t h e r i g h t a r g u m e n t .
B i n a r y " < = " r e t u r n s t r u e i f t h e l e f t a r g u m e n t i s n u m e r i c a l l y l e s s t h a n o r e q u a l t o t h e r i g h t a r g u m e n t .
B i n a r y " > = " r e t u r n s t r u e i f t h e l e f t a r g u m e n t i s n u m e r i c a l l y g r e a t e r t h a n o r e q u a l t o t h e r i g h t a r g u m e n t .
B i n a r y " l t " r e t u r n s t r u e i f t h e l e f t a r g u m e n t i s s t r i n g w i s e l e s s t h a n t h e r i g h t a r g u m e n t .
B i n a r y " g t " r e t u r n s t r u e i f t h e l e f t a r g u m e n t i s s t r i n g w i s e g r e a t e r t h a n t h e r i g h t a r g u m e n t .
B i n a r y " l e " r e t u r n s t r u e i f t h e l e f t a r g u m e n t i s s t r i n g w i s e l e s s t h a n o r e q u a l t o t h e r i g h t a r g u m e n t .
B i n a r y " g e " r e t u r n s t r u e i f t h e l e f t a r g u m e n t i s s t r i n g w i s e g r e a t e r t h a n o r e q u a l t o t h e r i g h t a r g u m e n t .
# E q u a l i t y O p e r a t o r s
B i n a r y " = = " r e t u r n s t r u e i f t h e l e f t a r g u m e n t i s n u m e r i c a l l y e q u a l t o t h e r i g h t a r g u m e n t .
B i n a r y " ! = " r e t u r n s t r u e i f t h e l e f t a r g u m e n t i s n u m e r i c a l l y n o t e q u a l t o t h e r i g h t a r g u m e n t .
B i n a r y " < = > " r e t u r n s - 1 , 0 , o r 1 d e p e n d i n g o n w h e t h e r t h e l e f t a r g u m e n t i s n u m e r i c a l l y l e s s t h a n , e q u a l t o , o r g r e a t e r t h a n t h e r i g h t a r g u m e n t .
B i n a r y " e q " r e t u r n s t r u e i f t h e l e f t a r g u m e n t i s s t r i n g w i s e e q u a l t o t h e r i g h t a r g u m e n t .
B i n a r y " n e " r e t u r n s t r u e i f t h e l e f t a r g u m e n t i s s t r i n g w i s e n o t e q u a l t o t h e r i g h t a r g u m e n t .
B i n a r y " c m p " r e t u r n s - 1 , 0 , o r 1 d e p e n d i n g o n w h e t h e r t h e l e f t a r g u m e n t i s s t r i n g w i s e l e s s t h a n , e q u a l t o , o r g r e a t e r t h a n t h e r i g h t a r g u m e n t .
" l t " , " l e " , " g e " , " g t " a n d " c m p " u s e t h e c o l l a t i o n ( s o r t ) o r d e r s p e c i f i e d b y t h e c u r r e n t l o c a l e i f u s e l o c a l e
i s i n e f f e c t . S e e p e r l l o c a l e .
# B i t w i s e A n d
B i n a r y " & " r e t u r n s i t s o p e r a t o r s A N D e d t o g e t h e r b i t b y b i t . ( S e e a l s o " I n t e g e r A r i t h m e t i c " a n d " B i t w i s e S t r i n g O p e r a t o r s " . )
# B i t w i s e O r a n d E x c l u s i v e O r
B i n a r y " | " r e t u r n s i t s o p e r a t o r s O R e d t o g e t h e r b i t b y b i t . ( S e e a l s o " I n t e g e r A r i t h m e t i c " a n d " B i t w i s e S t r i n g O p e r a t o r s " . )
B i n a r y " ^ " r e t u r n s i t s o p e r a t o r s X O R e d t o g e t h e r b i t b y b i t . ( S e e a l s o " I n t e g e r A r i t h m e t i c " a n d " B i t w i s e S t r i n g O p e r a t o r s " . )
# C - s t y l e L o g i c a l A n d
B i n a r y " & & " p e r f o r m s a s h o r t - c i r c u i t l o g i c a l A N D o p e r a t i o n . T h a t i s , i f t h e l e f t o p e r a n d i s f a l s e , t h e r i g h t o p e r a n d i s n o t e v e n e v a l u a t e d . S c a l a r o r l i s t c o n t e x t p r o p a g a t e s d o w n t o t h e r i g h t o p e r a n d i f i t i s e v a l u a t e d .
# C - s t y l e L o g i c a l O r
B i n a r y " | | " p e r f o r m s a s h o r t - c i r c u i t l o g i c a l O R o p e r a t i o n . T h a t i s , i f t h e l e f t o p e r a n d i s t r u e , t h e r i g h t o p e r a n d i s n o t e v e n e v a l u a t e d . S c a l a r o r l i s t c o n t e x t p r o p a g a t e s d o w n t o t h e r i g h t o p e r a n d i f i t i s e v a l u a t e d .
T h e | |
a n d & &
o p e r a t o r s d i f f e r f r o m C ' s i n t h a t , r a t h e r t h a n r e t u r n i n g 0 o r 1 , t h e y r e t u r n t h e l a s t v a l u e e v a l u a t e d . T h u s , a r e a s o n a b l y p o r t a b l e w a y t o f i n d o u t t h e h o m e d i r e c t o r y ( a s s u m i n g i t ' s n o t " 0 " ) m i g h t b e :
$home = $ENV{'HOME'} || $ENV{'LOGDIR'} ||
(getpwuid($<))[7 ] || die "You're homeless!\n";
I n p a r t i c u l a r , t h i s m e a n s t h a t y o u s h o u l d n ' t u s e t h i s f o r s e l e c t i n g b e t w e e n t w o a g g r e g a t e s f o r a s s i g n m e n t :
@a = @b || @c; # this is wrong
@a = scalar(@b) || @c; # really meant this
@a = @b ? @b : @c; # this works fine, though
A s m o r e r e a d a b l e a l t e r n a t i v e s t o & &
a n d | |
w h e n u s e d f o r c o n t r o l f l o w , P e r l p r o v i d e s a n d
a n d or
o p e r a t o r s ( s e e b e l o w ) . T h e s h o r t - c i r c u i t b e h a v i o r i s i d e n t i c a l . T h e p r e c e d e n c e o f " a n d " a n d " o r " i s m u c h l o w e r , h o w e v e r , s o t h a t y o u c a n s a f e l y u s e t h e m a f t e r a l i s t o p e r a t o r w i t h o u t t h e n e e d f o r p a r e n t h e s e s :
unlink "alpha", "beta", "gamma"
or gripe(), next LINE;
W i t h t h e C - s t y l e o p e r a t o r s t h a t w o u l d h a v e b e e n w r i t t e n l i k e t h i s :
unlink("alpha", "beta", "gamma")
|| (gripe(), next LINE);
U s e " o r " f o r a s s i g n m e n t i s u n l i k e l y t o d o w h a t y o u w a n t ; s e e b e l o w .
# R a n g e O p e r a t o r s
B i n a r y " . . " i s t h e r a n g e o p e r a t o r , w h i c h i s r e a l l y t w o d i f f e r e n t o p e r a t o r s d e p e n d i n g o n t h e c o n t e x t . I n l i s t c o n t e x t , i t r e t u r n s a n a r r a y o f v a l u e s c o u n t i n g ( b y o n e s ) f r o m t h e l e f t v a l u e t o t h e r i g h t v a l u e . T h i s i s u s e f u l f o r w r i t i n g f o r e a c h ( 1 . . 1 0 )
l o o p s a n d f o r d o i n g s l i c e o p e r a t i o n s o n a r r a y s . I n t h e c u r r e n t i m p l e m e n t a t i o n , n o t e m p o r a r y a r r a y i s c r e a t e d w h e n t h e r a n g e o p e r a t o r i s u s e d a s t h e e x p r e s s i o n i n f o r e
a c h
l o o p s , b u t o l d e r v e r s i o n s o f P e r l m i g h t b u r n a l o t o f m e m o r y w h e n y o u w r i t e s o m e t h i n g l i k e t h i s :
for (1 .. 1_000_000) {
# code
}
I n s c a l a r c o n t e x t , " . . " r e t u r n s a b o o l e a n v a l u e . T h e o p e r a t o r i s b i s t a b l e , l i k e a f l i p - f l o p , a n d e m u l a t e s t h e l i n e - r a n g e ( c o m m a ) o p e r a t o r o f s e d , a w k , a n d v a r i o u s e d i t o r s . E a c h " . . " o p e r a t o r m a i n t a i n s i t s o w n b o o l e a n s t a t e . I t i s f a l s e a s l o n g a s i t s l e f t o p e r a n d i s f a l s e . O n c e t h e l e f t o p e r a n d i s t r u e , t h e r a n g e o p e r a t o r s t a y s t r u e u n t i l t h e r i g h t o p e r a n d i s t r u e , A F T E R w h i c h t h e r a n g e o p e r a t o r b e c o m e s f a l s e a g a i n . ( I t d o e s n ' t b e c o m e f a l s e t i l l t h e n e x t t i m e t h e r a n g e o p e r a t o r i s e v a l u a t e d . I t c a n t e s t t h e r i g h t o p e r a n d a n d b e c o m e f a l s e o n t h e s a m e e v a l u a t i o n i t b e c a m e t r u e ( a s i n a w k ) , b u t i t s t i l l r e t u r n s t r u e o n c e . I f y o u d o n ' t w a n t i t t o t e s t t h e r i g h t o p e r a n d t i l l t h e n e x t e v a l u a t i o n ( a s i n s e d ) , u s e t h r e e d o t s ( " . . . " ) i n s t e a d o f t w o . ) T h e r i g h t o p e r a n d i s n o t e v a l u a t e d w h i l e t h e o p e r a t o r i s i n t h e " f a l s e " s t a t e , a n d t h e l e f t o p e r a n d i s n o t e v a l u a t e d w h i l e t h e o p e r a t o r i s i n t h e " t r u e " s t a t e . T h e p r e c e d e n c e i s a l i t t l e l o w e r t h a n | | a n d & & . T h e v a l u e r e t u r n e d i s e i t h e r t h e e m p t y s t r i n g f o r f a l s e , o r a s e q u e n c e n u m b e r ( b e g i n n i n g w i t h 1 ) f o r t r u e . T h e s e q u e n c e n u m b e r i s r e s e t f o r e a c h r a n g e e n c o u n t e r e d . T h e f i n a l s e q u e n c e n u m b e r i n a r a n g e h a s t h e s t r i n g " E 0 " a p p e n d e d t o i t , w h i c h d o e s n ' t a f f e c t i t s n u m e r i c v a l u e , b u t g i v e s y o u s o m e t h i n g t o s e a r c h f o r i f y o u w a n t t o e x c l u d e t h e e n d p o i n t . Y o u c a n e x c l u d e t h e b e g i n n i n g p o i n t b y w a i t i n g f o r t h e s e q u e n c e n u m b e r t o b e g r e a t e r t h a n 1 . I f e i t h e r o p e r a n d o f s c a l a r " . . " i s a c o n s t a n t e x p r e s s i o n , t h a t o p e r a n d i s i m p l i c i t l y c o m p a r e d t o t h e $ .
v a r i a b l e , t h e c u r r e n t l i n e n u m b e r . E x a m p l e s :
A s a s c a l a r o p e r a t o r :
if (101 .. 200) { print; } # print 2nd hundred lines
next line if (1 .. /^$/); # skip header lines
s/^/> / if (/^$/ .. eof()); # quote body
# parse mail messages
while (<>) {
$in_header = 1 .. /^$/;
$in_body = /^$/ .. eof();
# do something based on those
} continue {
close ARGV if eof; # reset $. each file
}
A s a l i s t o p e r a t o r :
for (101 .. 200) { print; } # print $_ 100 times
@foo = @foo[0 .. $#foo]; # an expensive no-op
@foo = @foo[$#foo-4 .. $#foo]; # slice last 5 items
T h e r a n g e o p e r a t o r ( i n l i s t c o n t e x t ) m a k e s u s e o f t h e m a g i c a l a u t o - i n c r e m e n t a l g o r i t h m i f t h e o p e r a n d s a r e s t r i n g s . Y o u c a n s a y
@alphabet = ('A' .. 'Z');
t o g e t a l l t h e l e t t e r s o f t h e a l p h a b e t , o r
$hexdigit = (0 .. 9, 'a' .. 'f')[$num &15 ];
t o g e t a h e x a d e c i m a l d i g i t , o r
@z2 = ('01' .. '31'); print $z2[$mday];
t o g e t d a t e s w i t h l e a d i n g z e r o s . I f t h e f i n a l v a l u e s p e c i f i e d i s n o t i n t h e s e q u e n c e t h a t t h e m a g i c a l i n c r e m e n t w o u l d p r o d u c e , t h e s e q u e n c e g o e s u n t i l t h e n e x t v a l u e w o u l d b e l o n g e r t h a n t h e f i n a l v a l u e s p e c i f i e d .
# C o n d i t i o n a l O p e r a t o r
T e r n a r y " ? : " i s t h e c o n d i t i o n a l o p e r a t o r , j u s t a s i n C . I t w o r k s m u c h l i k e a n i f - t h e n - e l s e . I f t h e a r g u m e n t b e f o r e t h e ? i s t r u e , t h e a r g u m e n t b e f o r e t h e : i s r e t u r n e d , o t h e r w i s e t h e a r g u m e n t a f t e r t h e : i s r e t u r n e d . F o r e x a m p l e :
printf "I have %d dog%s.\n", $n,
($n == 1) ? '' : "s";
S c a l a r o r l i s t c o n t e x t p r o p a g a t e s d o w n w a r d i n t o t h e 2 n d o r 3 r d a r g u m e n t , w h i c h e v e r i s s e l e c t e d .
$a = $ok ? $b : $c; # get a scalar
@a = $ok ? @b : @c; # get an array
$a = $ok ? @b : @c; # oops, that's just a count!
T h e o p e r a t o r m a y b e a s s i g n e d t o i f b o t h t h e 2 n d a n d 3 r d a r g u m e n t s a r e l e g a l l v a l u e s ( m e a n i n g t h a t y o u c a n a s s i g n t o t h e m ) :
($a_or_b ? $a : $b) = $c;
T h i s i s n o t n e c e s s a r i l y g u a r a n t e e d t o c o n t r i b u t e t o t h e r e a d a b i l i t y o f y o u r p r o g r a m .
B e c a u s e t h i s o p e r a t o r p r o d u c e s a n a s s i g n a b l e r e s u l t , u s i n g a s s i g n m e n t s w i t h o u t p a r e n t h e s e s w i l l g e t y o u i n t r o u b l e . F o r e x a m p l e , t h i s :
$a % 2 ? $a += 10 : $a += 2
R e a l l y m e a n s t h i s :
(($a % 2) ? ($a += 10) : $a) += 2
R a t h e r t h a n t h i s :
($a % 2) ? ($a += 10) : ($a += 2)
# A s s i g n m e n t O p e r a t o r s
" = " i s t h e o r d i n a r y a s s i g n m e n t o p e r a t o r .
A s s i g n m e n t o p e r a t o r s w o r k a s i n C . T h a t i s ,
$a += 2;
i s e q u i v a l e n t t o
$a = $a + 2;
a l t h o u g h w i t h o u t d u p l i c a t i n g a n y s i d e e f f e c t s t h a t d e r e f e r e n c i n g t h e l v a l u e m i g h t t r i g g e r , s u c h a s f r o m t i e ( ) . O t h e r a s s i g n m e n t o p e r a t o r s w o r k s i m i l a r l y . T h e f o l l o w i n g a r e r e c o g n i z e d :
**= += *= &= <<= &&=
-= /= |= >>= ||=
.= %= ^=
x=
N o t e t h a t w h i l e t h e s e a r e g r o u p e d b y f a m i l y , t h e y a l l h a v e t h e p r e c e d e n c e o f a s s i g n m e n t .
U n l i k e i n C , t h e a s s i g n m e n t o p e r a t o r p r o d u c e s a v a l i d l v a l u e . M o d i f y i n g a n a s s i g n m e n t i s e q u i v a l e n t t o d o i n g t h e a s s i g n m e n t a n d t h e n m o d i f y i n g t h e v a r i a b l e t h a t w a s a s s i g n e d t o . T h i s i s u s e f u l f o r m o d i f y i n g a c o p y o f s o m e t h i n g , l i k e t h i s :
($tmp = $global) =~ tr [A-Z] [a-z];
L i k e w i s e ,
($a += 2) *= 3;
i s e q u i v a l e n t t o
$a += 2;
$a *= 3;
# C o m m a O p e r a t o r
B i n a r y " , " i s t h e c o m m a 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 e v a l u a t e s i t s l e f t a r g u m e n t , t h r o w s t h a t v a l u e a w a y , t h e n e v a l u a t e s i t s r i g h t a r g u m e n t a n d r e t u r n s t h a t v a l u e . T h i s i s j u s t l i k e C ' s c o m m a o p e r a t o r .
I n l i s t c o n t e x t , i t ' s j u s t t h e l i s t a r g u m e n t s e p a r a t o r , a n d i n s e r t s b o t h i t s a r g u m e n t s i n t o t h e l i s t .
T h e = > d i g r a p h i s m o s t l y j u s t a s y n o n y m f o r t h e c o m m a o p e r a t o r . I t ' s u s e f u l f o r d o c u m e n t i n g a r g u m e n t s t h a t c o m e i n p a i r s . A s o f r e l e a s e 5 . 0 0 1 , i t a l s o f o r c e s a n y w o r d t o t h e l e f t o f i t t o b e i n t e r p r e t e d a s a s t r i n g .
# L i s t O p e r a t o r s ( R i g h t w a r d )
O n t h e r i g h t s i d e o f a l i s t o p e r a t o r , i t h a s v e r y l o w p r e c e d e n c e , s u c h t h a t i t c o n t r o l s a l l c o m m a - s e p a r a t e d e x p r e s s i o n s f o u n d t h e r e . T h e o n l y o p e r a t o r s w i t h l o w e r p r e c e d e n c e a r e t h e l o g i c a l o p e r a t o r s " a n d " , " o r " , a n d " n o t " , w h i c h m a y b e u s e d t o e v a l u a t e c a l l s t o l i s t o p e r a t o r s w i t h o u t t h e n e e d f o r e x t r a p a r e n t h e s e s :
open HANDLE, "filename"
or die "Can't open: $!\n";
S e e a l s o d i s c u s s i o n o f l i s t o p e r a t o r s i n T e r m s a n d L i s t O p e r a t o r s ( L e f t w a r d ) .
# L o g i c a l N o t
U n a r y " n o t " r e t u r n s t h e l o g i c a l n e g a t i o n o f t h e e x p r e s s i o n t o i t s r i g h t . I t ' s t h e e q u i v a l e n t o f " ! " e x c e p t f o r t h e v e r y l o w p r e c e d e n c e .
# L o g i c a l A n d
B i n a r y " a n d " r e t u r n s t h e l o g i c a l c o n j u n c t i o n o f t h e t w o s u r r o u n d i n g e x p r e s s i o n s . I t ' s e q u i v a l e n t t o & & e x c e p t f o r t h e v e r y l o w p r e c e d e n c e . T h i s m e a n s t h a t i t s h o r t - c i r c u i t s : i . e . , t h e r i g h t e x p r e s s i o n i s e v a l u a t e d o n l y i f t h e l e f t e x p r e s s i o n i s t r u e .
# L o g i c a l o r a n d E x c l u s i v e O r
B i n a r y " o r " r e t u r n s t h e l o g i c a l d i s j u n c t i o n o f t h e t w o s u r r o u n d i n g e x p r e s s i o n s . I t ' s e q u i v a l e n t t o | | e x c e p t f o r t h e v e r y l o w p r e c e d e n c e . T h i s m a k e s i t u s e f u l f o r c o n t r o l f l o w
print FH $data or die "Can't write to FH: $!";
T h i s m e a n s t h a t i t s h o r t - c i r c u i t s : i . e . , t h e r i g h t e x p r e s s i o n i s e v a l u a t e d o n l y i f t h e l e f t e x p r e s s i o n i s f a l s e . D u e t o i t s p r e c e d e n c e , y o u s h o u l d p r o b a b l y a v o i d u s i n g t h i s f o r a s s i g n m e n t , o n l y f o r c o n t r o l f l o w .
$a = $b or $c; # bug: this is wrong
($a = $b) or $c; # really means this
$a = $b || $c; # better written this way
H o w e v e r , w h e n i t ' s a l i s t c o n t e x t a s s i g n m e n t a n d y o u ' r e t r y i n g t o u s e " | | " f o r c o n t r o l f l o w , y o u p r o b a b l y n e e d " o r " s o t h a t t h e a s s i g n m e n t t a k e s h i g h e r p r e c e d e n c e .
@info = stat($file) || die; # oops, scalar sense of stat!
@info = stat($file) or die; # better, now @info gets its due
T h e n a g a i n , y o u c o u l d a l w a y s u s e p a r e n t h e s e s .
B i n a r y " x o r " r e t u r n s t h e e x c l u s i v e - O R o f t h e t w o s u r r o u n d i n g e x p r e s s i o n s . I t c a n n o t s h o r t c i r c u i t , o f c o u r s e .
# C O p e r a t o r s M i s s i n g F r o m P e r l
H e r e i s w h a t C h a s t h a t P e r l d o e s n ' t :
# u n a r y &
A d d r e s s - o f o p e r a t o r . ( B u t s e e t h e " \ " o p e r a t o r f o r t a k i n g a r e f e r e n c e . )
# u n a r y *
D e r e f e r e n c e - a d d r e s s o p e r a t o r . ( P e r l ' s p r e f i x d e r e f e r e n c i n g o p e r a t o r s a r e t y p e d : $ , @ , % , a n d & . )
# ( T Y P E )
T y p e c a s t i n g o p e r a t o r .
# Q u o t e a n d Q u o t e - l i k e O p e r a t o r s
W h i l e w e u s u a l l y t h i n k o f q u o t e s a s l i t e r a l v a l u e s , i n P e r l t h e y f u n c t i o n a s o p e r a t o r s , p r o v i d i n g v a r i o u s k i n d s o f i n t e r p o l a t i n g a n d p a t t e r n m a t c h i n g c a p a b i l i t i e s . P e r l p r o v i d e s c u s t o m a r y q u o t e c h a r a c t e r s f o r t h e s e b e h a v i o r s , b u t a l s o p r o v i d e s a w a y f o r y o u t o c h o o s e y o u r q u o t e c h a r a c t e r f o r a n y o f t h e m . I n t h e f o l l o w i n g t a b l e , a { }
r e p r e s e n t s a n y p a i r o f d e l i m i t e r s y o u c h o o s e . N o n - b r a c k e t i n g d e l i m i t e r s u s e t h e s a m e c h a r a c t e r f o r e a n d a f t , b u t t h e 4 s o r t s o f b r a c k e t s ( r o u n d , a n g l e , s q u a r e , c u r l y ) w i l l a l l n e s t .
Customary Generic Meaning Interpolates
'' q{} Literal no
"" qq{} Literal yes
`` qx{} Command yes (unless '' is delimiter)
qw{} Word list no
// m{} Pattern match yes
qr{} Pattern yes
s{}{} Substitution yes
tr{}{} Transliteration no (but see below)
N o t e t h a t t h e r e c a n b e w h i t e s p a c e b e t w e e n t h e o p e r a t o r a n d t h e q u o t i n g c h a r a c t e r s , e x c e p t w h e n #
i s b e i n g u s e d a s t h e q u o t i n g c h a r a c t e r . q # f o o #
i s p a r s e d a s b e i n g t h e s t r i n g f o o
, w h i l e q # f o o #
i s t h e o p e r a t o r q
f o l l o w e d b y a c o m m e n t . I t s a r g u m e n t w i l l b e t a k e n f r o m t h e n e x t l i n e . T h i s a l l o w s y o u t o w r i t e :
s {foo} # Replace foo
{bar} # with bar.
F o r c o n s t r u c t s t h a t d o i n t e r p o l a t i o n , v a r i a b l e s b e g i n n i n g w i t h " $
" o r " @
" a r e i n t e r p o l a t e d , a s a r e t h e f o l l o w i n g s e q u e n c e s . W i t h i n a t r a n s l i t e r a t i o n , t h e f i r s t t e n o f t h e s e s e q u e n c e s m a y b e u s e d .
\t tab (HT, TAB)
\n newline (NL )
\r return (CR )
\f form feed (FF )
\b backspace (BS )
\a alarm (bell) (BEL)
\e escape (ESC)
\033 octal char
\x1b hex char
\c[ control char
\l lowercase next char
\u uppercase next char
\L lowercase till \E
\U uppercase till \E
\E end case modification
\Q quote non-word characters till \E
If u s e l o c a l e
i s i n e f f e c t , t h e c a s e m a p u s e d b y \ l
, \ L
, \ u
a n d \ U
i s t a k e n f r o m t h e c u r r e n t l o c a l e . S e e p e r l l o c a l e .
A l l s y s t e m s u s e t h e v i r t u a l " \ n "
t o r e p r e s e n t a l i n e t e r m i n a t o r , c a l l e d a " n e w l i n e " . T h e r e i s n o s u c h t h i n g a s a n u n v a r y i n g , p h y s i c a l n e w l i n e c h a r a c t e r . I t i s a n i l l u s i o n t h a t t h e o p e r a t i n g s y s t e m , d e v i c e d r i v e r s , C l i b r a r i e s , a n d P e r l a l l c o n s p i r e t o p r e s e r v e . N o t a l l s y s t e m s r e a d " \ r "
a s A S C I I C R a n d " \ n "
a s A S C I I L F . F o r e x a m p l e , o n a M a c , t h e s e a r e r e v e r s e d , a n d o n s y s t e m s w i t h o u t l i n e t e r m i n a t o r , p r i n t i n g " \ n
"
m a y e m i t n o a c t u a l d a t a . I n g e n e r a l , u s e " \ n "
w h e n y o u m e a n a " n e w l i n e " f o r y o u r s y s t e m , b u t u s e t h e l i t e r a l A S C I I w h e n y o u n e e d a n e x a c t c h a r a c t e r . F o r e x a m p l e , m o s t n e t w o r k i n g p r o t o c o l s e x p e c t a n d p r e f e r a C R + L F ( " \ 0 1 2 \ 0
1 5 "
or " \ c J \ c M "
) f o r l i n e t e r m i n a t o r s , a n d a l t h o u g h t h e y o f t e n a c c e p t j u s t " \ 0 1 2 "
, t h e y s e l d o m t o l e r a t e j u s t " \ 0 1 5 "
. I f y o u g e t i n t h e h a b i t o f u s i n g " \ n "
f o r n e t w o r k i n g , y o u m a y b e b u r n e d s o m e d a y .
Y o u c a n n o t i n c l u d e a l i t e r a l $
or @
w i t h i n a \ Q
s e q u e n c e . A n u n e s c a p e d $
or @
i n t e r p o l a t e s t h e c o r r e s p o n d i n g v a r i a b l e , w h i l e e s c a p i n g w i l l c a u s e t h e l i t e r a l s t r i n g \ $
t o b e i n s e r t e d . Y o u ' l l n e e d t o w r i t e s o m e t h i n g l i k e m / \ Q u s e r \ E \
@ \ Q h o s t /
.
P a t t e r n s a r e s u b j e c t t o a n a d d i t i o n a l l e v e l o f i n t e r p r e t a t i o n a s a r e g u l a r e x p r e s s i o n . T h i s i s d o n e a s a s e c o n d p a s s , a f t e r v a r i a b l e s a r e i n t e r p o l a t e d , s o t h a t r e g u l a r e x p r e s s i o n s m a y b e i n c o r p o r a t e d i n t o t h e p a t t e r n f r o m t h e v a r i a b l e s . I f t h i s i s n o t w h a t y o u w a n t , u s e \ Q
t o i n t e r p o l a t e a v a r i a b l e l i t e r a l l y .
A p a r t f r o m t h e a b o v e , t h e r e a r e n o m u l t i p l e l e v e l s o f i n t e r p o l a t i o n . I n p a r t i c u l a r , c o n t r a r y t o t h e e x p e c t a t i o n s o f s h e l l p r o g r a m m e r s , b a c k - q u o t e s d o N O T i n t e r p o l a t e w i t h i n d o u b l e q u o t e s , n o r d o s i n g l e q u o t e s i m p e d e e v a l u a t i o n o f v a r i a b l e s w h e n u s e d w i t h i n d o u b l e q u o t e s .
# R e g e x p Q u o t e - L i k e O p e r a t o r s
H e r e a r e t h e q u o t e - l i k e o p e r a t o r s t h a t a p p l y t o p a t t e r n m a t c h i n g a n d r e l a t e d a c t i v i t i e s .
M o s t o f t h i s s e c t i o n i s r e l a t e d t o u s e o f r e g u l a r e x p r e s s i o n s f r o m P e r l . S u c h a u s e m a y b e c o n s i d e r e d f r o m t w o p o i n t s o f v i e w : P e r l h a n d l e s a a s t r i n g a n d a " p a t t e r n " t o R E ( r e g u l a r e x p r e s s i o n ) e n g i n e t o m a t c h , R E e n g i n e f i n d s ( o r d o e s n o t f i n d ) t h e m a t c h , a n d P e r l u s e s t h e f i n d i n g s o f R E e n g i n e f o r i t s o p e r a t i o n , p o s s i b l y a s k i n g t h e e n g i n e f o r o t h e r m a t c h e s .
R E e n g i n e h a s n o i d e a w h a t P e r l i s g o i n g t o d o w i t h w h a t i t f i n d s , s i m i l a r l y , t h e r e s t o f P e r l h a s n o i d e a w h a t a p a r t i c u l a r r e g u l a r e x p r e s s i o n m e a n s t o R E e n g i n e . T h i s c r e a t e s a c l e a n s e p a r a t i o n , a n d i n t h i s s e c t i o n w e d i s c u s s m a t c h i n g f r o m P e r l p o i n t o f v i e w o n l y . T h e o t h e r p o i n t o f v i e w m a y b e f o u n d i n p e r l r e .
# ? P A T T E R N ?
T h i s i s j u s t l i k e t h e / p a t t e r n /
s e a r c h , e x c e p t t h a t i t m a t c h e s o n l y o n c e b e t w e e n c a l l s t o t h e r e s e t ( ) o p e r a t o r . T h i s i s a u s e f u l o p t i m i z a t i o n w h e n y o u w a n t t o s e e o n l y t h e f i r s t o c c u r r e n c e o f s o m e t h i n g i n e a c h f i l e o f a s e t o f f i l e s , f o r i n s t a n c e . O n l y ??
p a t t e r n s l o c a l t o t h e c u r r e n t p a c k a g e a r e r e s e t .
while (<>) {
if (?^$?) {
# blank line between header and body
}
} continue {
reset if eof; # clear ?? status for next file
}
T h i s u s a g e i s v a g u e l y d e p r e c a t e d , a n d m a y b e r e m o v e d i n s o m e f u t u r e v e r s i o n o f P e r l .
# m / P A T T E R N / c g i m o s x
# / P A T T E R N / c g i m o s x
S e a r c h e s a s t r i n g f o r a p a t t e r n m a t c h , a n d i n s c a l a r c o n t e x t r e t u r n s t r u e ( 1 ) o r f a l s e ( ' ' ) . I f n o s t r i n g i s s p e c i f i e d v i a t h e = ~
or ! ~
o p e r a t o r , t h e $ _ s t r i n g i s s e a r c h e d . ( T h e s t r i n g s p e c i f i e d w i t h = ~
n e e d n o t b e a n l v a l u e - - i t m a y b e t h e r e s u l t o f a n e x p r e s s i o n e v a l u a t i o n , b u t r e m e m b e r t h e = ~
b i n d s r a t h e r t i g h t l y . ) S e e a l s o p e r l r e . S e e p e r l l o c a l e f o r d i s c u s s i o n o f a d d i t i o n a l c o n s i d e r a t i o n s t h a t a p p l y w h e n u s e l o c a l e
i s i n e f f e c t .
O p t i o n s a r e :
c Do not reset search position on a failed match when /g is in effect.
g Match globally, i.e., find all occurrences.
i Do case-insensitive pattern matching.
m Treat string as multiple lines.
o Compile pattern only once.
s Treat string as single line.
x Use extended regular expressions.
I f " / " i s t h e d e l i m i t e r t h e n t h e i n i t i a l m
i s o p t i o n a l . W i t h t h e m
y o u c a n u s e a n y p a i r o f n o n - a l p h a n u m e r i c , n o n - w h i t e s p a c e c h a r a c t e r s a s d e l i m i t e r s ( i f s i n g l e q u o t e s a r e u s e d , n o i n t e r p r e t a t i o n i s d o n e o n t h e r e p l a c e m e n t s t r i n g . U n l i k e P e r l 4 , P e r l 5 t r e a t s b a c k t i c k s a s n o r m a l d e l i m i t e r s ; t h e r e p l a c e m e n t t e x t i s n o t e v a l u a t e d a s a c o m m a n d ) . T h i s i s p a r t i c u l a r l y u s e f u l f o r m a t c h i n g U n i x p a t h n a m e s t h a t c o n t a i n " / " , t o a v o i d L T S ( l e a n i n g t o o t h p i c k s y n d r o m e ) . I f " ? " i s t h e d e l i m i t e r , t h e n t h e m a t c h - o n l y - o n c e r u l e o f ? P A T T E R N ?
a p p l i e s .
P A T T E R N m a y c o n t a i n v a r i a b l e s , w h i c h w i l l b e i n t e r p o l a t e d ( a n d t h e p a t t e r n r e c o m p i l e d ) e v e r y t i m e t h e p a t t e r n s e a r c h i s e v a l u a t e d . ( N o t e t h a t $ )
a n d $ |
m i g h t n o t b e i n t e r p o l a t e d b e c a u s e t h e y l o o k l i k e e n d - o f - s t r i n g t e s t s . ) I f y o u w a n t s u c h a p a t t e r n t o b e c o m p i l e d o n l y o n c e , a d d a / o
a f t e r t h e t r a i l i n g d e l i m i t e r . T h i s a v o i d s e x p e n s i v e r u n - t i m e r e c o m p i l a t i o n s , a n d i s u s e f u l w h e n t h e v a l u e y o u a r e i n t e r p o l a t i n g w o n ' t c h a n g e o v e r t h e l i f e o f t h e s c r i p t . H o w e v e r , m e n t i o n i n g / o
c o n s t i t u t e s a p r o m i s e t h a t y o u w o n ' t c h a n g e t h e v a r i a b l e s i n t h e p a t t e r n . I f y o u c h a n g e t h e m , P e r l w o n ' t e v e n n o t i c e .
I f t h e P A T T E R N e v a l u a t e s t o t h e e m p t y s t r i n g , t h e l a s t s u c c e s s f u l l y m a t c h e d r e g u l a r e x p r e s s i o n i s u s e d i n s t e a d .
I f t h e / g
o p t i o n i s n o t u s e d , m
/ /
i n a l i s t c o n t e x t r e t u r n s a l i s t c o n s i s t i n g o f t h e s u b e x p r e s s i o n s m a t c h e d b y t h e p a r e n t h e s e s i n t h e p a t t e r n , i . e . , ( $ 1
, $
2
, $ 3
. . . ) . ( N o t e t h a t h e r e $ 1
e t c . a r e a l s o s e t , a n d t h a t t h i s d i f f e r s f r o m P e r l 4 ' s b e h a v i o r . ) W h e n t h e r e a r e n o p a r e n t h e s e s i n t h e p a t t e r n , t h e r e t u r n v a l u e i s t h e l i s t ( 1 )
f o r s u c c e s s . W i t h o r w i t h o u t p a r e n t h e s e s , a n e m p t y l i s t i s r e t u r n e d u p o n f a i l u r e .
E x a m p l e s :
open(TTY, '/dev/tty');
<TTY> =~ /^y/i && foo(); # do foo if desired
if (/Version: *([0-9.]*)/) { $version = $1; }
next if m#^/usr/spool/uucp#;
# poor man's grep
$arg = shift;
while (<>) {
print if /$arg/o; # compile only once
}
if (($F1, $F2, $Etc) = ($foo =~ /^(\S+)\s+(\S+)\s*(.*)/))
T h i s l a s t e x a m p l e s p l i t s $ f o o i n t o t h e f i r s t t w o w o r d s a n d t h e r e m a i n d e r o f t h e l i n e , a n d a s s i g n s t h o s e t h r e e f i e l d s t o $ F 1 , $ F 2 , a n d $ E t c . T h e c o n d i t i o n a l i s t r u e i f a n y v a r i a b l e s w e r e a s s i g n e d , i . e . , i f t h e p a t t e r n m a t c h e d .
T h e / g
m o d i f i e r s p e c i f i e s g l o b a l p a t t e r n m a t c h i n g - - t h a t i s , m a t c h i n g a s m a n y t i m e s a s p o s s i b l e w i t h i n t h e s t r i n g . H o w i t b e h a v e s d e p e n d s o n t h e c o n t e x t . I n l i s t c o n t e x t , i t r e t u r n s a l i s t o f a l l t h e s u b s t r i n g s m a t c h e d b y a l l t h e p a r e n t h e s e s i n t h e r e g u l a r e x p r e s s i o n . I f t h e r e a r e n o p a r e n t h e s e s , i t r e t u r n s a l i s t o f a l l t h e m a t c h e d s t r i n g s , a s i f t h e r e w e r e p a r e n t h e s e s a r o u n d t h e w h o l e p a t t e r n .
I n s c a l a r c o n t e x t , e a c h e x e c u t i o n o f m / / g
f i n d s t h e n e x t m a t c h , r e t u r n i n g T R U E i f i t m a t c h e s , a n d F A L S E i f t h e r e i s n o f u r t h e r m a t c h . T h e p o s i t i o n a f t e r t h e l a s t m a t c h c a n b e r e a d o r s e t u s i n g t h e p o s ( ) f u n c t i o n ; s e e " p o s " i n p e r l f u n c . A f a i l e d m a t c h n o r m a l l y r e s e t s t h e s e a r c h p o s i t i o n t o t h e b e g i n n i n g o f t h e s t r i n g , b u t y o u c a n a v o i d t h a t b y a d d i n g t h e / c
m o d i f i e r ( e . g . m / / g c
) . M o d i f y i n g t h e t a r g e t s t r i n g a l s o r e s e t s t h e s e a r c h p o s i t i o n .
Y o u c a n i n t e r m i x m / / g
m a t c h e s w i t h m / \ G . . . / g
, w h e r e \ G
i s a z e r o - w i d t h a s s e r t i o n t h a t m a t c h e s t h e e x a c t p o s i t i o n w h e r e t h e p r e v i o u s m / / g
, i f a n y , l e f t o f f . T h e \ G
a s s e r t i o n i s n o t s u p p o r t e d w i t h o u t t h e / g
m o d i f i e r ; c u r r e n t l y , w i t h o u t / g
, \ G
b e h a v e s j u s t l i k e \ A
, b u t t h a t ' s a c c i d e n t a l a n d m a y c h a n g e i n t h e f u t u r e .
E x a m p l e s :
# list context
($one,$five,$fifteen) = (`uptime` =~ /(\d+\.\d+)/g);
# scalar context
$/ = ""; $* = 1; # $* deprecated in modern perls
while (defined($paragraph = <>)) {
while ($paragraph =~ /[a-z]['")]*[.!?]+['")]*\s/g) {
$sentences++;
}
}
print "$sentences\n";
# using m//gc with \G
$_ = "ppooqppqq";
while ($i++ <2 ) {
print "1: '";
print $1 while /(o )/gc; print "', pos=", pos, "\n";
print "2: '";
print $1 if /\G(q )/gc; print "', pos=", pos, "\n";
print "3: '";
print $1 while /(p )/gc; print "', pos=", pos, "\n";
}
T h e l a s t e x a m p l e s h o u l d p r i n t :
1: 'oo', pos=4
2: 'q', pos=5
3: 'pp', pos=7
1: '', pos=7
2: 'q', pos=8
3: '', pos=8
A u s e f u l i d i o m f o r l e x
- l i k e s c a n n e r s i s / \ G . . . / g c
. Y o u c a n c o m b i n e s e v e r a l r e g e x p s l i k e t h i s t o p r o c e s s a s t r i n g p a r t - b y - p a r t , d o i n g d i f f e r e n t a c t i o n s d e p e n d i n g o n w h i c h r e g e x p m a t c h e d . E a c h r e g e x p t r i e s t o m a t c h w h e r e t h e p r e v i o u s o n e l e a v e s o f f .
$_ = <<'EOL';
$url = new URI::URL "http://www/"; die if $url eq "xXx";
EOL
LOOP:
{
print(" digits"), redo LOOP if /\G\d+\b[,.;]?\s*/gc;
print(" lowercase"), redo LOOP if /\G[a-z]+\b[,.;]?\s*/gc;
print(" UPPERCASE"), redo LOOP if /\G[A-Z]+\b[,.;]?\s*/gc;
print(" Capitalized"), redo LOOP if /\G[A-Z][a-z]+\b[,.;]?\s*/gc;
print(" MiXeD"), redo LOOP if /\G[A-Za-z]+\b[,.;]?\s*/gc;
print(" alphanumeric"), redo LOOP if /\G[A-Za-z0-9]+\b[,.;]?\s*/gc;
print(" line-noise"), redo LOOP if /\G[^A-Za-z0-9]+/gc;
print ". That's all!\n";
}
H e r e i s t h e o u t p u t ( s p l i t i n t o s e v e r a l l i n e s ) :
line-noise lowercase line-noise lowercase UPPERCASE line-noise
UPPERCASE line-noise lowercase line-noise lowercase line-noise
lowercase lowercase line-noise lowercase lowercase line-noise
MiXeD line-noise. That's all!
# q / S T R I N G /
# ' S T R I N G '
A s i n g l e - q u o t e d , l i t e r a l s t r i n g . A b a c k s l a s h r e p r e s e n t s a b a c k s l a s h u n l e s s f o l l o w e d b y t h e d e l i m i t e r o r a n o t h e r b a c k s l a s h , i n w h i c h c a s e t h e d e l i m i t e r o r b a c k s l a s h i s i n t e r p o l a t e d .
$foo = q!I said, "You said, 'She said it.'"!;
$bar = q('This is it.');
$baz = '\n'; # a two-character string
# q q / S T R I N G /
# " S T R I N G "
A d o u b l e - q u o t e d , i n t e r p o l a t e d s t r i n g .
$_ .= qq
(*** The previous line contains the naughty word "$1".\n)
if /(tcl|rexx|python)/; # :-)
$baz = "\n"; # a one-character string
# q r / S T R I N G / i m o s x
A s t r i n g w h i c h i s ( p o s s i b l y ) i n t e r p o l a t e d a n d t h e n c o m p i l e d a s a r e g u l a r e x p r e s s i o n . T h e r e s u l t m a y b e u s e d a s a p a t t e r n i n a m a t c h
$re = qr/$pattern/;
$string =~ /foo${re}bar/; # can be interpolated in other patterns
$string =~ $re; # or used standalone
O p t i o n s a r e :
i Do case-insensitive pattern matching.
m Treat string as multiple lines.
o Compile pattern only once.
s Treat string as single line.
x Use extended regular expressions.
T h e b e n e f i t f r o m t h i s i s t h a t t h e p a t t e r n i s p r e c o m p i l e d i n t o a n i n t e r n a l r e p r e s e n t a t i o n , a n d d o e s n o t n e e d t o b e r e c o m p i l e d e v e r y t i m e a m a t c h i s a t t e m p t e d . T h i s m a k e s i t v e r y e f f i c i e n t t o d o s o m e t h i n g l i k e :
foreach $pattern (@pattern_list) {
my $re = qr/$pattern/;
foreach $line (@lines) {
if($line =~ /$re/) {
do_something($line);
}
}
}
S e e p e r l r e f o r a d d i t i o n a l i n f o r m a t i o n o n v a l i d s y n t a x f o r S T R I N G , a n d f o r a d e t a i l e d l o o k a t t h e s e m a n t i c s o f r e g u l a r e x p r e s s i o n s .
# q x / S T R I N G /
# ` S T R I N G `
A s t r i n g w h i c h i s ( p o s s i b l y ) i n t e r p o l a t e d a n d t h e n e x e c u t e d a s a s y s t e m c o m m a n d w i t h / b i n / s h
o r i t s e q u i v a l e n t . S h e l l w i l d c a r d s , p i p e s , a n d r e d i r e c t i o n s w i l l b e h o n o r e d . T h e c o l l e c t e d s t a n d a r d o u t p u t o f t h e c o m m a n d i s r e t u r n e d ; s t a n d a r d e r r o r i s u n a f f e c t e d . I n s c a l a r c o n t e x t , i t c o m e s b a c k a s a s i n g l e ( p o t e n t i a l l y m u l t i - l i n e ) s t r i n g . I n l i s t c o n t e x t , r e t u r n s a l i s t o f l i n e s ( h o w e v e r y o u ' v e d e f i n e d l i n e s w i t h $ / o r $ I N P U T _ R E C O R D _ S E P A R A T O R ) .
B e c a u s e b a c k t i c k s d o n o t a f f e c t s t a n d a r d e r r o r , u s e s h e l l f i l e d e s c r i p t o r s y n t a x ( a s s u m i n g t h e s h e l l s u p p o r t s t h i s ) i f y o u c a r e t o a d d r e s s t h i s . T o c a p t u r e a c o m m a n d ' s S T D E R R a n d S T D O U T t o g e t h e r :
$output = `cmd 2>&1`;
T o c a p t u r e a c o m m a n d ' s S T D O U T b u t d i s c a r d i t s S T D E R R :
$output = `cmd 2>/dev/null`;
T o c a p t u r e a c o m m a n d ' s S T D E R R b u t d i s c a r d i t s S T D O U T ( o r d e r i n g i s i m p o r t a n t h e r e ) :
$output = `cmd 2>&1 1>/dev/null`;
T o e x c h a n g e a c o m m a n d ' s S T D O U T a n d S T D E R R i n o r d e r t o c a p t u r e t h e S T D E R R b u t l e a v e i t s S T D O U T t o c o m e o u t t h e o l d S T D E R R :
$output = `cmd 3>&1 1>&2 2>&3 3>&-`;
T o r e a d b o t h a c o m m a n d ' s S T D O U T a n d i t s S T D E R R s e p a r a t e l y , i t ' s e a s i e s t a n d s a f e s t t o r e d i r e c t t h e m s e p a r a t e l y t o f i l e s , a n d t h e n r e a d f r o m t h o s e f i l e s w h e n t h e p r o g r a m i s d o n e :
system("program args 1>/tmp/program.stdout 2>/tmp/program.stderr");
U s i n g s i n g l e - q u o t e a s a d e l i m i t e r p r o t e c t s t h e c o m m a n d f r o m P e r l ' s d o u b l e - q u o t e i n t e r p o l a t i o n , p a s s i n g i t o n t o t h e s h e l l i n s t e a d :
$perl_info = qx(ps $$); # that's Perl's $$
$shell_info = qx'ps $$'; # that's the new shell's $$
N o t e t h a t h o w t h e s t r i n g g e t s e v a l u a t e d i s e n t i r e l y s u b j e c t t o t h e c o m m a n d i n t e r p r e t e r o n y o u r s y s t e m . O n m o s t p l a t f o r m s , y o u w i l l h a v e t o p r o t e c t s h e l l m e t a c h a r a c t e r s i f y o u w a n t t h e m t r e a t e d l i t e r a l l y . T h i s i s i n p r a c t i c e d i f f i c u l t t o d o , a s i t ' s u n c l e a r h o w t o e s c a p e w h i c h c h a r a c t e r s . S e e p e r l s e c f o r a c l e a n a n d s a f e e x a m p l e o f a m a n u a l f o r k ( ) a n d e x e c ( ) t o e m u l a t e b a c k t i c k s s a f e l y .
O n s o m e p l a t f o r m s ( n o t a b l y D O S - l i k e o n e s ) , t h e s h e l l m a y n o t b e c a p a b l e o f d e a l i n g w i t h m u l t i l i n e c o m m a n d s , s o p u t t i n g n e w l i n e s i n t h e s t r i n g m a y n o t g e t y o u w h a t y o u w a n t . Y o u m a y b e a b l e t o e v a l u a t e m u l t i p l e c o m m a n d s i n a s i n g l e l i n e b y s e p a r a t i n g t h e m w i t h t h e c o m m a n d s e p a r a t o r c h a r a c t e r , i f y o u r s h e l l s u p p o r t s t h a t ( e . g . ;
o n m a n y U n i x s h e l l s ; &
o n t h e W i n d o w s N T c m
d
s h e l l ) .
B e w a r e t h a t s o m e c o m m a n d s h e l l s m a y p l a c e r e s t r i c t i o n s o n t h e l e n g t h o f t h e c o m m a n d l i n e . Y o u m u s t e n s u r e y o u r s t r i n g s d o n ' t e x c e e d t h i s l i m i t a f t e r a n y n e c e s s a r y i n t e r p o l a t i o n s . S e e t h e p l a t f o r m - s p e c i f i c r e l e a s e n o t e s f o r m o r e d e t a i l s a b o u t y o u r p a r t i c u l a r e n v i r o n m e n t .
U s i n g t h i s o p e r a t o r c a n l e a d t o p r o g r a m s t h a t a r e d i f f i c u l t t o p o r t , b e c a u s e t h e s h e l l c o m m a n d s c a l l e d v a r y b e t w e e n s y s t e m s , a n d m a y i n f a c t n o t b e p r e s e n t a t a l l . A s o n e e x a m p l e , t h e t y
p e
c o m m a n d u n d e r t h e P O S I X s h e l l i s v e r y d i f f e r e n t f r o m t h e t y
p e
c o m m a n d u n d e r D O S . T h a t d o e s n ' t m e a n y o u s h o u l d g o o u t o f y o u r w a y t o a v o i d b a c k t i c k s w h e n t h e y ' r e t h e r i g h t w a y t o g e t s o m e t h i n g d o n e . P e r l w a s m a d e t o b e a g l u e l a n g u a g e , a n d o n e o f t h e t h i n g s i t g l u e s t o g e t h e r i s c o m m a n d s . J u s t u n d e r s t a n d w h a t y o u ' r e g e t t i n g y o u r s e l f i n t o .
S e e " O O p e r a t o r s " " i n " I f o r m o r e d i s c u s s i o n .
# q w / S T R I N G /
R e t u r n s a l i s t o f t h e w o r d s e x t r a c t e d o u t o f S T R I N G , u s i n g e m b e d d e d w h i t e s p a c e a s t h e w o r d d e l i m i t e r s . I t i s e x a c t l y e q u i v a l e n t t o
split(' ', q/STRING/);
T h i s e q u i v a l e n c y m e a n s t h a t i f u s e d i n s c a l a r c o n t e x t , y o u ' l l g e t s p l i t ' s ( u n f o r t u n a t e ) s c a l a r c o n t e x t b e h a v i o r , c o m p l e t e w i t h m y s t e r i o u s w a r n i n g s .
S o m e f r e q u e n t l y s e e n e x a m p l e s :
use POSIX qw( setlocale localeconv )
@EXPORT = qw( foo bar baz );
A c o m m o n m i s t a k e i s t o t r y t o s e p a r a t e t h e w o r d s w i t h c o m m a o r t o p u t c o m m e n t s i n t o a m u l t i - l i n e qw
- s t r i n g . F o r t h i s r e a s o n t h e - w
s w i t c h p r o d u c e w a r n i n g s i f t h e S T R I N G c o n t a i n s t h e " , " o r t h e " # " c h a r a c t e r .
# s / P A T T E R N / R E P L A C E M E N T / e g i m o s x
S e a r c h e s a s t r i n g f o r a p a t t e r n , a n d i f f o u n d , r e p l a c e s t h a t p a t t e r n w i t h t h e r e p l a c e m e n t t e x t a n d r e t u r n s t h e n u m b e r o f s u b s t i t u t i o n s m a d e . O t h e r w i s e i t r e t u r n s f a l s e ( s p e c i f i c a l l y , t h e e m p t y s t r i n g ) .
I f n o s t r i n g i s s p e c i f i e d v i a t h e = ~
or ! ~
o p e r a t o r , t h e $ _
v a r i a b l e i s s e a r c h e d a n d m o d i f i e d . ( T h e s t r i n g s p e c i f i e d w i t h = ~
m u s t b e s c a l a r v a r i a b l e , a n a r r a y e l e m e n t , a h a s h e l e m e n t , o r a n a s s i g n m e n t t o o n e o f t h o s e , i . e . , a n l v a l u e . )
I f t h e d e l i m i t e r c h o s e n i s s i n g l e q u o t e , n o v a r i a b l e i n t e r p o l a t i o n i s d o n e o n e i t h e r t h e P A T T E R N o r t h e R E P L A C E M E N T . O t h e r w i s e , i f t h e P A T T E R N c o n t a i n s a $ t h a t l o o k s l i k e a v a r i a b l e r a t h e r t h a n a n e n d - o f - s t r i n g t e s t , t h e v a r i a b l e w i l l b e i n t e r p o l a t e d i n t o t h e p a t t e r n a t r u n - t i m e . I f y o u w a n t t h e p a t t e r n c o m p i l e d o n l y o n c e t h e f i r s t t i m e t h e v a r i a b l e i s i n t e r p o l a t e d , u s e t h e / o
o p t i o n . I f t h e p a t t e r n e v a l u a t e s t o t h e e m p t y s t r i n g , t h e l a s t s u c c e s s f u l l y e x e c u t e d r e g u l a r e x p r e s s i o n i s u s e d i n s t e a d . S e e p e r l r e f o r f u r t h e r e x p l a n a t i o n o n t h e s e . S e e p e r l l o c a l e f o r d i s c u s s i o n o f a d d i t i o n a l c o n s i d e r a t i o n s t h a t a p p l y w h e n u s e l o c a l e
i s i n e f f e c t .
O p t i o n s a r e :
e Evaluate the right side as an expression.
g Replace globally, i.e., all occurrences.
i Do case-insensitive pattern matching.
m Treat string as multiple lines.
o Compile pattern only once.
s Treat string as single line.
x Use extended regular expressions.
A n y n o n - a l p h a n u m e r i c , n o n - w h i t e s p a c e d e l i m i t e r m a y r e p l a c e t h e s l a s h e s . I f s i n g l e q u o t e s a r e u s e d , n o i n t e r p r e t a t i o n i s d o n e o n t h e r e p l a c e m e n t s t r i n g ( t h e / e
m o d i f i e r o v e r r i d e s t h i s , h o w e v e r ) . U n l i k e P e r l 4 , P e r l 5 t r e a t s b a c k t i c k s a s n o r m a l d e l i m i t e r s ; t h e r e p l a c e m e n t t e x t i s n o t e v a l u a t e d a s a c o m m a n d . I f t h e P A T T E R N i s d e l i m i t e d b y b r a c k e t i n g q u o t e s , t h e R E P L A C E M E N T h a s i t s o w n p a i r o f q u o t e s , w h i c h m a y o r m a y n o t b e b r a c k e t i n g q u o t e s , e . g . , s ( f o o ) ( b a r )
or s < f
o o > / b a r /
. A / e
w i l l c a u s e t h e r e p l a c e m e n t p o r t i o n t o b e i n t e r p r e t e d a s a f u l l - f l e d g e d P e r l e x p r e s s i o n a n d e v a l ( ) e d r i g h t t h e n a n d t h e r e . I t i s , h o w e v e r , s y n t a x c h e c k e d a t c o m p i l e - t i m e .
E x a m p l e s :
s/\bgreen\b/mauve/g; # don't change wintergreen
$path =~ s|/usr/bin|/usr/local/bin|;
s/Login: $foo/Login: $bar/; # run-time pattern
($foo = $bar) =~ s/this/that/; # copy first, then change
$count = ($paragraph =~ s/Mister\b/Mr./g); # get change-count
$_ = 'abc123xyz';
s/\d+/$&*2/e; # yields 'abc246xyz'
s/\d+/sprintf("%5d",$&)/e; # yields 'abc 246xyz'
s/\w/$& x 2/eg; # yields 'aabbcc 224466xxyyzz'
s/%(.)/$percent{$1}/g; # change percent escapes; no /e
s/%(.)/$percent{$1} || $&/ge; # expr now, so /e
s/^=(\w+)/&pod($1)/ge; # use function call
# expand variables in $_, but dynamics only, using
# symbolic dereferencing
s/\$(\w+)/${$1}/g;
# /e's can even nest; this will expand
# any embedded scalar variable (including lexicals) in $_
s/(\$\w+)/$1/eeg;
# Delete (most) C comments.
$program =~ s {
/\* # Match the opening delimiter.
.*? # Match a minimal number of characters.
\*/ # Match the closing delimiter.
} []gsx;
s/^\s*(.*?)\s*$/$1/; # trim white space in $_, expensively
for ($variable) { # trim white space in $variable, cheap
s/^\s+//;
s/\s+$//;
}
s/([^ ]*) *([^ ]*)/$2 $1/; # reverse 1st two fields
N o t e t h e u s e o f $ i n s t e a d o f \ i n t h e l a s t e x a m p l e . U n l i k e s e d , w e u s e t h e \ < d i g i t > f o r m i n o n l y t h e l e f t h a n d s i d e . A n y w h e r e e l s e i t ' s $ < d i g i t > .
O c c a s i o n a l l y , y o u c a n ' t u s e j u s t a / g
t o g e t a l l t h e c h a n g e s t o o c c u r . H e r e a r e t w o c o m m o n c a s e s :
# put commas in the right places in an integer
1 while s/(.*\d)(\d\d\d)/$1,$2/g; # perl4
1 while s/(\d)(\d\d\d)(?!\d)/$1,$2/g; # perl5
# expand tabs to 8-column spacing
1 while s/\t+/' ' x (length($&)*8 - length($`)%8)/e;
# t r / S E A R C H L I S T / R E P L A C E M E N T L I S T / c d s
# y / S E A R C H L I S T / R E P L A C E M E N T L I S T / c d s
T r a n s l i t e r a t e s a l l o c c u r r e n c e s o f t h e c h a r a c t e r s f o u n d i n t h e s e a r c h l i s t w i t h t h e c o r r e s p o n d i n g c h a r a c t e r i n t h e r e p l a c e m e n t l i s t . I t r e t u r n s t h e n u m b e r o f c h a r a c t e r s r e p l a c e d o r d e l e t e d . I f n o s t r i n g i s s p e c i f i e d v i a t h e = ~ o r ! ~ o p e r a t o r , t h e $ _ s t r i n g i s t r a n s l i t e r a t e d . ( T h e s t r i n g s p e c i f i e d w i t h = ~ m u s t b e a s c a l a r v a r i a b l e , a n a r r a y e l e m e n t , a h a s h e l e m e n t , o r a n a s s i g n m e n t t o o n e o f t h o s e , i . e . , a n l v a l u e . ) A c h a r a c t e r r a n g e m a y b e s p e c i f i e d w i t h a h y p h e n , s o t r / A - J / 0 - 9 /
d o e s t h e s a m e r e p l a c e m e n t a s t r / A C E G I B D F H J / 0 2 4
6 8 1 3 5 7 9 /
. F o r s e d d e v o t e e s , y
i s p r o v i d e d a s a s y n o n y m f o r tr
. I f t h e S E A R C H L I S T i s d e l i m i t e d b y b r a c k e t i n g q u o t e s , t h e R E P L A C E M E N T L I S T h a s i t s o w n p a i r o f q u o t e s , w h i c h m a y o r m a y n o t b e b r a c k e t i n g q u o t e s , e . g . , t r [ A - Z ]
[ a - z ]
or t r ( + \ - * / ) / A B C D /
.
O p t i o n s :
c Complement the SEARCHLIST.
d Delete found but unreplaced characters.
s Squash duplicate replaced characters.
I f t h e / c
m o d i f i e r i s s p e c i f i e d , t h e S E A R C H L I S T c h a r a c t e r s e t i s c o m p l e m e n t e d . I f t h e / d
m o d i f i e r i s s p e c i f i e d , a n y c h a r a c t e r s s p e c i f i e d b y S E A R C H L I S T n o t f o u n d i n R E P L A C E M E N T L I S T a r e d e l e t e d . ( N o t e t h a t t h i s i s s l i g h t l y m o r e f l e x i b l e t h a n t h e b e h a v i o r o f s o m e tr p r o g r a m s , w h i c h d e l e t e a n y t h i n g t h e y f i n d i n t h e S E A R C H L I S T , p e r i o d . ) I f t h e / s
m o d i f i e r i s s p e c i f i e d , s e q u e n c e s o f c h a r a c t e r s t h a t w e r e t r a n s l i t e r a t e d t o t h e s a m e c h a r a c t e r a r e s q u a s h e d d o w n t o a s i n g l e i n s t a n c e o f t h e c h a r a c t e r .
I f t h e / d
m o d i f i e r i s u s e d , t h e R E P L A C E M E N T L I S T i s a l w a y s i n t e r p r e t e d e x a c t l y a s s p e c i f i e d . O t h e r w i s e , i f t h e R E P L A C E M E N T L I S T i s s h o r t e r t h a n t h e S E A R C H L I S T , t h e f i n a l c h a r a c t e r i s r e p l i c a t e d t i l l i t i s l o n g e n o u g h . I f t h e R E P L A C E M E N T L I S T i s e m p t y , t h e S E A R C H L I S T i s r e p l i c a t e d . T h i s l a t t e r i s u s e f u l f o r c o u n t i n g c h a r a c t e r s i n a c l a s s o r f o r s q u a s h i n g c h a r a c t e r s e q u e n c e s i n a c l a s s .
E x a m p l e s :
$ARGV[1 ] =~ tr/A-Z/a-z/; # canonicalize to lower case
$cnt = tr/*/*/; # count the stars in $_
$cnt = $sky =~ tr/*/*/; # count the stars in $sky
$cnt = tr/0-9//; # count the digits in $_
tr/a-zA-Z//s; # bookkeeper -> bokeper
($HOST = $host) =~ tr/a-z/A-Z/;
tr/a-zA-Z/ /cs; # change non-alphas to single space
tr [\200-\377]
[\000-\177]; # delete 8th bit
I f m u l t i p l e t r a n s l i t e r a t i o n s a r e g i v e n f o r a c h a r a c t e r , o n l y t h e f i r s t o n e i s u s e d :
tr/AAA/XYZ/
w i l l t r a n s l i t e r a t e a n y A t o X .
N o t e t h a t b e c a u s e t h e t r a n s l i t e r a t i o n t a b l e i s b u i l t a t c o m p i l e t i m e , n e i t h e r t h e S E A R C H L I S T n o r t h e R E P L A C E M E N T L I S T a r e s u b j e c t e d t o d o u b l e q u o t e i n t e r p o l a t i o n . T h a t m e a n s t h a t i f y o u w a n t t o u s e v a r i a b l e s , y o u m u s t u s e a n e v a l ( ) :
eval "tr/$oldlist/$newlist/";
die $@ if $@;
eval "tr/$oldlist/$newlist/, 1" or die $@;
# G o r y d e t a i l s o f p a r s i n g q u o t e d c o n s t r u c t s
W h e n p r e s e n t e d w i t h s o m e t h i n g w h i c h m a y h a v e s e v e r a l d i f f e r e n t i n t e r p r e t a t i o n s , P e r l u s e s t h e p r i n c i p l e D W I M ( e x p a n d e d t o D o W h a t I M e a n - n o t w h a t I w r o t e ) t o p i c k u p t h e m o s t p r o b a b l e i n t e r p r e t a t i o n o f t h e s o u r c e . T h i s s t r a t e g y i s s o s u c c e s s f u l t h a t P e r l u s e r s u s u a l l y d o n o t s u s p e c t a m b i v a l e n c e o f w h a t t h e y w r i t e . H o w e v e r , t i m e t o t i m e P e r l ' s i d e a s d i f f e r f r o m w h a t t h e a u t h o r m e a n t .
T h e t a r g e t o f t h i s s e c t i o n i s t o c l a r i f y t h e P e r l ' s w a y o f i n t e r p r e t i n g q u o t e d c o n s t r u c t s . T h e m o s t f r e q u e n t r e a s o n o n e m a y h a v e t o w a n t t o k n o w t h e d e t a i l s d i s c u s s e d i n t h i s s e c t i o n i s h a i r y r e g u l a r e x p r e s s i o n s . H o w e v e r , t h e f i r s t s t e p s o f p a r s i n g a r e t h e s a m e f o r a l l P e r l q u o t i n g o p e r a t o r s , s o h e r e t h e y a r e d i s c u s s e d t o g e t h e r .
S o m e o f t h e p a s s e s d i s c u s s e d b e l o w a r e p e r f o r m e d c o n c u r r e n t l y , b u t a s f a r a s r e s u l t s a r e t h e s a m e , w e c o n s i d e r t h e m o n e - b y - o n e . F o r d i f f e r e n t q u o t i n g c o n s t r u c t s P e r l p e r f o r m s d i f f e r e n t n u m b e r o f p a s s e s , f r o m o n e t o f i v e , b u t t h e y a r e a l w a y s p e r f o r m e d i n t h e s a m e o r d e r .
# F i n d i n g t h e e n d
F i r s t p a s s i s f i n d i n g t h e e n d o f t h e q u o t e d c o n s t r u c t , b e i t m u l t i c h a r e n d e r " \ n E O F \ n "
of < < E O F
c o n s t r u c t , /
w h i c h t e r m i n a t e s q
q /
c o n s t r u c t , ]
w h i c h t e r m i n a t e s q q [
c o n s t r u c t , o r >
w h i c h t e r m i n a t e s a f i l e g l o b s t a r t e d w i t h <
.
W h e n s e a r c h i n g f o r m u l t i c h a r c o n s t r u c t n o s k i p p i n g i s p e r f o r m e d . W h e n s e a r c h i n g f o r o n e - c h a r n o n - m a t c h i n g d e l i m i t e r , s u c h a s /
, c o m b i n a t i o n s \ \
a n d \ /
a r e s k i p p e d . W h e n s e a r c h i n g f o r o n e - c h a r m a t c h i n g d e l i m i t e r , s u c h a s ]
, c o m b i n a t i o n s \ \
, \ ]
a n d \
[
a r e s k i p p e d , a n d n e s t e d [
, ]
a r e s k i p p e d a s w e l l .
F o r 3 - p a r t s c o n s t r u c t s , s / / /
e t c . t h e s e a r c h i s r e p e a t e d o n c e m o r e .
D u r i n g t h i s s e a r c h n o a t t e n t i o n i s p a i d t o t h e s e m a n t i c o f t h e c o n s t r u c t , t h u s
"$hash{"$foo/$bar"}"
or
m/
bar # This is not a comment, this slash / terminated m//!
/x
d o n o t f o r m l e g a l q u o t e d e x p r e s s i o n s . N o t e t h a t s i n c e t h e s l a s h w h i c h t e r m i n a t e d m / /
w a s f o l l o w e d b y a S P A C E
, t h i s i s n o t m /
/ x
, t h u s #
w a s i n t e r p r e t e d a s a l i t e r a l #
.
# R e m o v a l o f b a c k s l a s h e s b e f o r e d e l i m i t e r s
D u r i n g t h e s e c o n d p a s s t h e t e x t b e t w e e n t h e s t a r t i n g d e l i m i t e r a n d t h e e n d i n g d e l i m i t e r i s c o p i e d t o a s a f e l o c a t i o n , a n d t h e \
i s r e m o v e d f r o m c o m b i n a t i o n s c o n s i s t i n g o f \
a n d d e l i m i t e r ( s ) ( b o t h s t a r t i n g a n d e n d i n g d e l i m i t e r i f t h e y d i f f e r ) .
T h e r e m o v a l d o e s n o t h a p p e n f o r m u l t i - c h a r d e l i m i t e r s .
N o t e t h a t t h e c o m b i n a t i o n \ \
i s l e f t a s i t w a s !
S t a r t i n g f r o m t h i s s t e p n o i n f o r m a t i o n a b o u t t h e d e l i m i t e r ( s ) i s u s e d i n t h e p a r s i n g .
# I n t e r p o l a t i o n
N e x t s t e p i s i n t e r p o l a t i o n i n t h e o b t a i n e d d e l i m i t e r - i n d e p e n d e n t t e x t . T h e r e a r e f o u r d i f f e r e n t c a s e s .
# < < ' E O F '
, m ' '
, s ' ' '
, t r / / /
, y / /
/
N o i n t e r p o l a t i o n i s p e r f o r m e d .
# ' '
, q / /
T h e o n l y i n t e r p o l a t i o n i s r e m o v a l o f \
f r o m p a i r s \ \
.
# " "
, ` `
, q q / /
, q x / /
, < f i l e * g l o b
>
\ Q
, \ U
, \ u
, \ L
, \ l
( p o s s i b l y p a i r e d w i t h \ E
) a r e c o n v e r t e d t o c o r r e s p o n d i n g P e r l c o n s t r u c t s , t h u s " $ f o o \ Q b a z $ b a r "
i s c o n v e r t e d t o
$foo . (quotemeta("baz" . $bar));
O t h e r c o m b i n a t i o n s o f \
w i t h f o l l o w i n g c h a r s a r e s u b s t i t u t e d w i t h a p p r o p r i a t e e x p a n s i o n s .
I n t e r p o l a t e d s c a l a r s a n d a r r a y s a r e c o n v e r t e d t o j o i n
a n d .
P e r l c o n s t r u c t s , t h u s " ' @ a r r
' "
b e c o m e s
"'" . (join $", @arr) . "'";
S i n c e a l l t h r e e a b o v e s t e p s a r e p e r f o r m e d s i m u l t a n e o u s l y l e f t - t o - r i g h t , t h e i s n o w a y t o i n s e r t a l i t e r a l $
or @
i n s i d e \ Q \ E
p a i r : i t c a n n o t b e p r o t e c t e d b y \
, s i n c e a n y \
( e x c e p t i n \ E
) i s i n t e r p r e t e d a s a l i t e r a l i n s i d e \ Q \ E
, a n d a n y $
i s i n t e r p r e t e d a s s t a r t i n g a n i n t e r p o l a t e d s c a l a r .
N o t e a l s o t h a t t h e i n t e r p o l a t i n g c o d e n e e d s t o m a k e d e c i s i o n w h e r e t h e i n t e r p o l a t e d s c a l a r e n d s , s a y , w h e t h e r " a $ b
- > { c } "
m e a n s
"a " . $b . " -> {c}";
or
"a " . $b -> {c};
M o s t t h e t i m e t h e d e c i s i o n i s t o t a k e t h e l o n g e s t p o s s i b l e t e x t w h i c h d o e s n o t i n c l u d e s p a c e s b e t w e e n c o m p o n e n t s a n d c o n t a i n s m a t c h i n g b r a c e s / b r a c k e t s .
# ? R E ?
, / R E /
, m / R E /
, s / R E / f o o /
,
P r o c e s s i n g o f \ Q
, \ U
, \ u
, \ L
, \ l
a n d i n t e r p o l a t i o n h a p p e n s ( a l m o s t ) a s w i t h q q / /
c o n s t r u c t s , b u t t h e s u b s t i t u t i o n o f \
f o l l o w e d b y o t h e r c h a r s i s n o t p e r f o r m e d ! M o r e o v e r , i n s i d e ( ? { B L O C K } )
n o p r o c e s s i n g i s p e r f o r m e d a t a l l .
I n t e r p o l a t i o n h a s s e v e r a l q u i r k s : $ |
, $ (
a n d $ )
a r e n o t i n t e r p o l a t e d , a n d c o n s t r u c t s $ v
a r [ S O M E T H I N G ]
a r e v o t e d ( b y s e v e r a l d i f f e r e n t e s t i m a t o r s ) t o b e a n a r r a y e l e m e n t o r $ v a
r
f o l l o w e d b y a R E a l t e r n a t i v e . T h i s i s t h e p l a c e w h e r e t h e n o t a t i o n $ { a r r [ $ b a r ] }
c o m e s h a n d y : / $ { a r r [ 0 - 9 ] } /
i s i n t e r p r e t e d a s a n a r r a y e l e m e n t -
9
, n o t a s a r e g u l a r e x p r e s s i o n f r o m v a r i a b l e $ a r r
f o l l o w e d b y a d i g i t , w h i c h i s t h e i n t e r p r e t a t i o n o f / $ a r r [ 0 - 9 ] /
.
N o t e t h a t a b s e n c e o f p r o c e s s i n g o f \ \
c r e a t e s s p e c i f i c r e s t r i c t i o n s o n t h e p o s t - p r o c e s s e d t e x t : i f t h e d e l i m i t e r i s /
, o n e c a n n o t g e t t h e c o m b i n a t i o n \ /
i n t o t h e r e s u l t o f t h i s s t e p : /
w i l l f i n i s h t h e r e g u l a r e x p r e s s i o n , \ /
w i l l b e s t r i p p e d t o /
o n t h e p r e v i o u s s t e p , a n d \ \ /
w i l l b e l e f t a s i s . S i n c e /
i s e q u i v a l e n t t o \ /
i n s i d e a r e g u l a r e x p r e s s i o n , t h i s d o e s n o t m a t t e r u n l e s s t h e d e l i m i t e r i s s p e c i a l c h a r a c t e r f o r t h e R E e n g i n e , a s i n s * f o o * b a r *
, m [ f o o ]
, o r ? f o o ?
.
T h i s s t e p i s t h e l a s t o n e f o r a l l t h e c o n s t r u c t s e x c e p t r e g u l a r e x p r e s s i o n s , w h i c h a r e p r o c e s s e d f u r t h e r .
# I n t e r p o l a t i o n o f r e g u l a r e x p r e s s i o n s
A l l t h e p r e v i o u s s t e p s w e r e p e r f o r m e d d u r i n g t h e c o m p i l a t i o n o f P e r l c o d e , t h i s o n e h a p p e n s i n r u n t i m e ( t h o u g h i t m a y b e o p t i m i z e d t o b e c a l c u l a t e d a t c o m p i l e t i m e i f a p p r o p r i a t e ) . A f t e r a l l t h e p r e p r o c e s s i n g p e r f o r m e d a b o v e ( a n d p o s s i b l y a f t e r e v a l u a t i o n i f c a t e n a t i o n , j o i n i n g , u p / d o w n - c a s i n g a n d q u o t e m e t a ( )
i n g a r e i n v o l v e d ) t h e r e s u l t i n g s t r i n g i s p a s s e d t o R E e n g i n e f o r c o m p i l a t i o n .
W h a t e v e r h a p p e n s i n t h e R E e n g i n e i s b e t t e r b e d i s c u s s e d i n p e r l r e , b u t f o r t h e s a k e o f c o n t i n u i t y l e t u s d o i t h e r e .
T h i s i s t h e f i r s t s t e p w h e r e p r e s e n c e o f t h e / / x
s w i t c h i s r e l e v a n t . T h e R E e n g i n e s c a n s t h e s t r i n g l e f t - t o - r i g h t , a n d c o n v e r t s i t t o a f i n i t e a u t o m a t o n .
B a c k s l a s h e d c h a r s a r e e i t h e r s u b s t i t u t e d b y c o r r e s p o n d i n g l i t e r a l s t r i n g s , o r g e n e r a t e s p e c i a l n o d e s o f t h e f i n i t e a u t o m a t o n . C h a r a c t e r s w h i c h a r e s p e c i a l t o t h e R E e n g i n e g e n e r a t e c o r r e s p o n d i n g n o d e s . ( ? # . . . )
c o m m e n t s a r e i g n o r e d . A l l t h e r e s t i s e i t h e r c o n v e r t e d t o l i t e r a l s t r i n g s t o m a t c h , o r i s i g n o r e d ( a s i s w h i t e s p a c e a n d #
- s t y l e c o m m e n t s i f / / x
i s p r e s e n t ) .
N o t e t h a t t h e p a r s i n g o f t h e c o n s t r u c t [ . . . ]
i s p e r f o r m e d u s i n g a b s o l u t e l y d i f f e r e n t r u l e s t h a n t h e r e s t o f t h e r e g u l a r e x p r e s s i o n . S i m i l a r l y , t h e ( ? { . . . } )
i s o n l y c h e c k e d f o r m a t c h i n g b r a c e s .
# O p t i m i z a t i o n o f r e g u l a r e x p r e s s i o n s
T h i s s t e p i s l i s t e d f o r c o m p l e t e n e s s o n l y . S i n c e i t d o e s n o t c h a n g e s e m a n t i c s , d e t a i l s o f t h i s s t e p a r e n o t d o c u m e n t e d a n d a r e s u b j e c t t o c h a n g e .
# I / O O p e r a t o r s
T h e r e a r e s e v e r a l I / O o p e r a t o r s y o u s h o u l d k n o w a b o u t . A s t r i n g e n c l o s e d b y b a c k t i c k s ( g r a v e a c c e n t s ) f i r s t u n d e r g o e s v a r i a b l e s u b s t i t u t i o n j u s t l i k e a d o u b l e q u o t e d s t r i n g . I t i s t h e n i n t e r p r e t e d a s a c o m m a n d , a n d t h e o u t p u t o f t h a t c o m m a n d i s t h e v a l u e o f t h e p s e u d o - l i t e r a l , l i k e i n a s h e l l . I n s c a l a r c o n t e x t , a s i n g l e s t r i n g c o n s i s t i n g o f a l l t h e o u t p u t i s r e t u r n e d . I n l i s t c o n t e x t , a l i s t o f v a l u e s i s r e t u r n e d , o n e f o r e a c h l i n e o f o u t p u t . ( Y o u c a n s e t $ /
t o u s e a d i f f e r e n t l i n e t e r m i n a t o r . ) T h e c o m m a n d i s e x e c u t e d e a c h t i m e t h e p s e u d o - l i t e r a l i s e v a l u a t e d . T h e s t a t u s v a l u e o f t h e c o m m a n d i s r e t u r n e d i n $ ?
( s e e p e r l v a r f o r t h e i n t e r p r e t a t i o n o f $ ?
) . U n l i k e i n c s h , n o t r a n s l a t i o n i s d o n e o n t h e r e t u r n d a t a - - n e w l i n e s r e m a i n n e w l i n e s . U n l i k e i n a n y o f t h e s h e l l s , s i n g l e q u o t e s d o n o t h i d e v a r i a b l e n a m e s i n t h e c o m m a n d f r o m i n t e r p r e t a t i o n . T o p a s s a $ t h r o u g h t o t h e s h e l l y o u n e e d t o h i d e i t w i t h a b a c k s l a s h . T h e g e n e r a l i z e d f o r m o f b a c k t i c k s i s q x / /
. ( B e c a u s e b a c k t i c k s a l w a y s u n d e r g o s h e l l e x p a n s i o n a s w e l l , s e e p e r l s e c f o r s e c u r i t y c o n c e r n s . )
E v a l u a t i n g a f i l e h a n d l e i n a n g l e b r a c k e t s y i e l d s t h e n e x t l i n e f r o m t h a t f i l e ( n e w l i n e , i f a n y , i n c l u d e d ) , o r u n d e f
a t e n d o f f i l e . O r d i n a r i l y y o u m u s t a s s i g n t h a t v a l u e t o a v a r i a b l e , b u t t h e r e i s o n e s i t u a t i o n w h e r e a n a u t o m a t i c a s s i g n m e n t h a p p e n s . I f a n d O N L Y i f t h e i n p u t s y m b o l i s t h e o n l y t h i n g i n s i d e t h e c o n d i t i o n a l o f a w h i l e
or f o r ( ; ; )
l o o p , t h e v a l u e i s a u t o m a t i c a l l y a s s i g n e d t o t h e v a r i a b l e $ _
. I n t h e s e l o o p c o n s t r u c t s , t h e a s s i g n e d v a l u e ( w h e t h e r a s s i g n m e n t i s a u t o m a t i c o r e x p l i c i t ) i s t h e n t e s t e d t o s e e i f i t i s d e f i n e d . T h e d e f i n e d t e s t a v o i d s p r o b l e m s w h e r e l i n e h a s a s t r i n g v a l u e t h a t w o u l d b e t r e a t e d a s f a l s e b y p e r l e . g . " " o r " 0 " w i t h n o t r a i l i n g n e w l i n e . ( T h i s m a y s e e m l i k e a n o d d t h i n g t o y o u , b u t y o u ' l l u s e t h e c o n s t r u c t i n a l m o s t e v e r y P e r l s c r i p t y o u w r i t e . ) A n y w a y , t h e f o l l o w i n g l i n e s a r e e q u i v a l e n t t o e a c h o t h e r :
while (defined($_ = <STDIN>)) { print; }
while ($_ = <STDIN>) { print; }
while (<STDIN>) { print; }
for (;<STDIN>;) { print; }
print while defined($_ = <STDIN>);
print while ($_ = <STDIN>);
print while <STDIN>;
a n d t h i s a l s o b e h a v e s s i m i l a r l y , b u t a v o i d s t h e u s e o f $ _ :
while (my $line = <STDIN>) { print $line }
I f y o u r e a l l y m e a n s u c h v a l u e s t o t e r m i n a t e t h e l o o p t h e y s h o u l d b e t e s t e d f o r e x p l i c i t l y :
while (($_ = <STDIN>) ne '0') { ... }
while (<STDIN>) { last unless $_; ... }
I n o t h e r b o o l e a n c o n t e x t s , < f i l e
h a n d l e >
w i t h o u t e x p l i c i t d e f i n e d
t e s t o r c o m p a r i s o n w i l l s o l i c i t a w a r n i n g i f - w
i s i n e f f e c t .
T h e f i l e h a n d l e s S T D I N , S T D O U T , a n d S T D E R R a r e p r e d e f i n e d . ( T h e f i l e h a n d l e s s t d i n
, s t d o u t
, a n d s t
d e r r
w i l l a l s o w o r k e x c e p t i n p a c k a g e s , w h e r e t h e y w o u l d b e i n t e r p r e t e d a s l o c a l i d e n t i f i e r s r a t h e r t h a n g l o b a l . ) A d d i t i o n a l f i l e h a n d l e s m a y b e c r e a t e d w i t h t h e o p e n ( ) f u n c t i o n . S e e " o p e n ( ) " i n p e r l f u n c f o r d e t a i l s o n t h i s .
I f a < F I L E H A N D L E > i s u s e d i n a c o n t e x t t h a t i s l o o k i n g f o r a l i s t , a l i s t c o n s i s t i n g o f a l l t h e i n p u t l i n e s i s r e t u r n e d , o n e l i n e p e r l i s t e l e m e n t . I t ' s e a s y t o m a k e a L A R G E d a t a s p a c e t h i s w a y , s o u s e w i t h c a r e .
T h e n u l l f i l e h a n d l e < > i s s p e c i a l a n d c a n b e u s e d t o e m u l a t e t h e b e h a v i o r o f s e d a n d a w k . I n p u t f r o m < > c o m e s e i t h e r f r o m s t a n d a r d i n p u t , o r f r o m e a c h f i l e l i s t e d o n t h e c o m m a n d l i n e . H e r e ' s h o w i t w o r k s : t h e f i r s t t i m e < > i s e v a l u a t e d , t h e @ A R G V a r r a y i s c h e c k e d , a n d i f i t i s e m p t y , $ A R
G V [ 0 ]
i s s e t t o " - " , w h i c h w h e n o p e n e d g i v e s y o u s t a n d a r d i n p u t . T h e @ A R G V a r r a y i s t h e n p r o c e s s e d a s a l i s t o f f i l e n a m e s . T h e l o o p
while (<>) {
... # code for each line
}
i s e q u i v a l e n t t o t h e f o l l o w i n g P e r l - l i k e p s e u d o c o d e :
unshift(@ARGV, '-') unless @ARGV;
while ($ARGV = shift) {
open(ARGV, $ARGV);
while (<ARGV>) {
... # code for each line
}
}
e x c e p t t h a t i t i s n ' t s o c u m b e r s o m e t o s a y , a n d w i l l a c t u a l l y w o r k . I t r e a l l y d o e s s h i f t a r r a y @ A R G V a n d p u t t h e c u r r e n t f i l e n a m e i n t o v a r i a b l e $ A R G V . I t a l s o u s e s f i l e h a n d l e A R G V i n t e r n a l l y - - < > i s j u s t a s y n o n y m f o r < A R G V > , w h i c h i s m a g i c a l . ( T h e p s e u d o c o d e a b o v e d o e s n ' t w o r k b e c a u s e i t t r e a t s < A R G V > a s n o n - m a g i c a l . )
Y o u c a n m o d i f y @ A R G V b e f o r e t h e f i r s t < > a s l o n g a s t h e a r r a y e n d s u p c o n t a i n i n g t h e l i s t o f f i l e n a m e s y o u r e a l l y w a n t . L i n e n u m b e r s ( $ .
) c o n t i n u e a s i f t h e i n p u t w e r e o n e b i g h a p p y f i l e . ( B u t s e e e x a m p l e u n d e r e o f
f o r h o w t o r e s e t l i n e n u m b e r s o n e a c h f i l e . )
I f y o u w a n t t o s e t @ A R G V t o y o u r o w n l i s t o f f i l e s , g o r i g h t a h e a d . T h i s s e t s @ A R G V t o a l l p l a i n t e x t f i l e s i f n o @ A R G V w a s g i v e n :
@ARGV = grep { -f && -T } glob('*') unless @ARGV;
Y o u c a n e v e n s e t t h e m t o p i p e c o m m a n d s . F o r e x a m p l e , t h i s a u t o m a t i c a l l y f i l t e r s c o m p r e s s e d a r g u m e n t s t h r o u g h g z i p :
@ARGV = map { /\.(gz|Z)$/ ? "gzip -dc < $_ |" : $_ } @ARGV;
I f y o u w a n t t o p a s s s w i t c h e s i n t o y o u r s c r i p t , y o u c a n u s e o n e o f t h e G e t o p t s m o d u l e s o r p u t a l o o p o n t h e f r o n t l i k e t h i s :
while ($_ = $ARGV[0], /^-/) {
shift;
last if /^--$/;
if (/^-D(.*)/) { $debug = $1 }
if (/^-v/) { $verbose++ }
# ... # other switches
}
while (<>) {
# ... # code for each line
}
T h e < > s y m b o l w i l l r e t u r n u n d e f
f o r e n d - o f - f i l e o n l y o n c e . I f y o u c a l l i t a g a i n a f t e r t h i s i t w i l l a s s u m e y o u a r e p r o c e s s i n g a n o t h e r @ A R G V l i s t , a n d i f y o u h a v e n ' t s e t @ A R G V , w i l l i n p u t f r o m S T D I N .
I f t h e s t r i n g i n s i d e t h e a n g l e b r a c k e t s i s a r e f e r e n c e t o a s c a l a r v a r i a b l e ( e . g . , < $ f o o > ) , t h e n t h a t v a r i a b l e c o n t a i n s t h e n a m e o f t h e f i l e h a n d l e t o i n p u t f r o m , o r i t s t y p e g l o b , o r a r e f e r e n c e t o t h e s a m e . F o r e x a m p l e :
$fh = \*STDIN;
$line = <$fh>;
I f w h a t ' s w i t h i n t h e a n g l e b r a c k e t s i s n e i t h e r a f i l e h a n d l e n o r a s i m p l e s c a l a r v a r i a b l e c o n t a i n i n g a f i l e h a n d l e n a m e , t y p e g l o b , o r t y p e g l o b r e f e r e n c e , i t i s i n t e r p r e t e d a s a f i l e n a m e p a t t e r n t o b e g l o b b e d , a n d e i t h e r a l i s t o f f i l e n a m e s o r t h e n e x t f i l e n a m e i n t h e l i s t i s r e t u r n e d , d e p e n d i n g o n c o n t e x t . T h i s d i s t i n c t i o n i s d e t e r m i n e d o n s y n t a c t i c g r o u n d s a l o n e . T h a t m e a n s < $ x >
i s a l w a y s a r e a d l i n e f r o m a n i n d i r e c t h a n d l e , b u t < $ h a s h { k e y } >
i s a l w a y s a g l o b . T h a t ' s b e c a u s e $ x i s a s i m p l e s c a l a r v a r i a b l e , b u t $ h a s h { k e y }
i s n o t - - i t ' s a h a s h e l e m e n t .
O n e l e v e l o f d o u b l e - q u o t e i n t e r p r e t a t i o n i s d o n e f i r s t , b u t y o u c a n ' t s a y < $ f o o >
b e c a u s e t h a t ' s a n i n d i r e c t f i l e h a n d l e a s e x p l a i n e d i n t h e p r e v i o u s p a r a g r a p h . ( I n o l d e r v e r s i o n s o f P e r l , p r o g r a m m e r s w o u l d i n s e r t c u r l y b r a c k e t s t o f o r c e i n t e r p r e t a t i o n a s a f i l e n a m e g l o b : < $ { f o o } >
. T h e s e d a y s , i t ' s c o n s i d e r e d c l e a n e r t o c a l l t h e i n t e r n a l f u n c t i o n d i r e c t l y a s g l o b ( $ f o o )
, w h i c h i s p r o b a b l y t h e r i g h t w a y t o h a v e d o n e i t i n t h e f i r s t p l a c e . ) E x a m p l e :
while (<*.c>) {
chmod 0644, $_;
}
i s e q u i v a l e n t t o
open(FOO, "echo *.c | tr -s ' \t\r\f' '\\012\\012\\012\\012'|");
while (<FOO>) {
chop;
chmod 0644, $_;
}
I n f a c t , i t ' s c u r r e n t l y i m p l e m e n t e d t h a t w a y . ( W h i c h m e a n s i t w i l l n o t w o r k o n f i l e n a m e s w i t h s p a c e s i n t h e m u n l e s s y o u h a v e c s h ( 1 ) o n y o u r m a c h i n e . ) O f c o u r s e , t h e s h o r t e s t w a y t o d o t h e a b o v e i s :
chmod 0644, <*.c>;
B e c a u s e g l o b b i n g i n v o k e s a s h e l l , i t ' s o f t e n f a s t e r t o c a l l r e a d d i r ( ) y o u r s e l f a n d d o y o u r o w n g r e p ( ) o n t h e f i l e n a m e s . F u r t h e r m o r e , d u e t o i t s c u r r e n t i m p l e m e n t a t i o n o f u s i n g a s h e l l , t h e g l o b ( ) r o u t i n e m a y g e t " A r g l i s t t o o l o n g " e r r o r s ( u n l e s s y o u ' v e i n s t a l l e d t c s h ( 1 L ) a s / b i n / c s h ) .
A g l o b e v a l u a t e s i t s ( e m b e d d e d ) a r g u m e n t o n l y w h e n i t i s s t a r t i n g a n e w l i s t . A l l v a l u e s m u s t b e r e a d b e f o r e i t w i l l s t a r t o v e r . I n a l i s t c o n t e x t t h i s i s n ' t i m p o r t a n t , b e c a u s e y o u a u t o m a t i c a l l y g e t t h e m a l l a n y w a y . I n s c a l a r c o n t e x t , h o w e v e r , t h e o p e r a t o r r e t u r n s t h e n e x t v a l u e e a c h t i m e i t i s c a l l e d , o r a u n d e f
v a l u e i f y o u ' v e j u s t r u n o u t . A s f o r f i l e h a n d l e s a n a u t o m a t i c d e f i n e d
i s g e n e r a t e d w h e n t h e g l o b o c c u r s i n t h e t e s t p a r t o f a w h i l e
or f
o r
- b e c a u s e l e g a l g l o b r e t u r n s ( e . g . a f i l e c a l l e d 0 ) w o u l d o t h e r w i s e t e r m i n a t e t h e l o o p . A g a i n , u n d e f
i s r e t u r n e d o n l y o n c e . S o i f y o u ' r e e x p e c t i n g a s i n g l e v a l u e f r o m a g l o b , i t i s m u c h b e t t e r t o s a y
($file) = <blurch*>;
t h a n
$file = <blurch*>;
b e c a u s e t h e l a t t e r w i l l a l t e r n a t e b e t w e e n r e t u r n i n g a f i l e n a m e a n d r e t u r n i n g F A L S E .
I t y o u ' r e t r y i n g t o d o v a r i a b l e i n t e r p o l a t i o n , i t ' s d e f i n i t e l y b e t t e r t o u s e t h e g l o b ( ) f u n c t i o n , b e c a u s e t h e o l d e r n o t a t i o n c a n c a u s e p e o p l e t o b e c o m e c o n f u s e d w i t h t h e i n d i r e c t f i l e h a n d l e n o t a t i o n .
@files = glob("$dir/*.[ch ]");
@files = glob($files[$i]);
# C o n s t a n t F o l d i n g
L i k e C , P e r l d o e s a c e r t a i n a m o u n t o f e x p r e s s i o n e v a l u a t i o n a t c o m p i l e t i m e , w h e n e v e r i t d e t e r m i n e s t h a t a l l a r g u m e n t s t o a n o p e r a t o r a r e s t a t i c a n d h a v e n o s i d e e f f e c t s . I n p a r t i c u l a r , s t r i n g c o n c a t e n a t i o n h a p p e n s a t c o m p i l e t i m e b e t w e e n l i t e r a l s t h a t d o n ' t d o v a r i a b l e s u b s t i t u t i o n . B a c k s l a s h i n t e r p r e t a t i o n a l s o h a p p e n s a t c o m p i l e t i m e . Y o u c a n s a y
'Now is the time for all' . "\n" .
'good men to come to.'
a n d t h i s a l l r e d u c e s t o o n e s t r i n g i n t e r n a l l y . L i k e w i s e , i f y o u s a y
foreach $file (@filenames) {
if (-s $file > 5 + 100 * 2**16) { }
}
t h e c o m p i l e r w i l l p r e c o m p u t e t h e n u m b e r t h a t e x p r e s s i o n r e p r e s e n t s s o t h a t t h e i n t e r p r e t e r w o n ' t h a v e t o .
# B i t w i s e S t r i n g O p e r a t o r s
B i t s t r i n g s o f a n y s i z e m a y b e m a n i p u l a t e d b y t h e b i t w i s e o p e r a t o r s ( ~ | & ^
) .
I f t h e o p e r a n d s t o a b i n a r y b i t w i s e o p a r e s t r i n g s o f d i f f e r e n t s i z e s , or a n d x o r o p s w i l l a c t a s i f t h e s h o r t e r o p e r a n d h a d a d d i t i o n a l z e r o b i t s o n t h e r i g h t , w h i l e t h e a n d o p w i l l a c t a s i f t h e l o n g e r o p e r a n d w e r e t r u n c a t e d t o t h e l e n g t h o f t h e s h o r t e r .
# ASCII-based examples
print "j p \n" ^ " a h"; # prints "JAPH\n"
print "JA" | " ph\n"; # prints "japh\n"
print "japh\nJunk" & '_____'; # prints "JAPH\n";
print 'p N$' ^ " E<H\n"; # prints "Perl\n";
I f y o u a r e i n t e n d i n g t o m a n i p u l a t e b i t s t r i n g s , y o u s h o u l d b e c e r t a i n t h a t y o u ' r e s u p p l y i n g b i t s t r i n g s : I f a n o p e r a n d i s a n u m b e r , t h a t w i l l i m p l y a n u m e r i c b i t w i s e o p e r a t i o n . Y o u m a y e x p l i c i t l y s h o w w h i c h t y p e o f o p e r a t i o n y o u i n t e n d b y u s i n g " "
or 0 +
, a s i n t h e e x a m p l e s b e l o w .
$foo = 150 | 105 ; # yields 255 (0x96 | 0x69 is 0xFF)
$foo = '150' | 105 ; # yields 255
$foo = 150 | '105'; # yields 255
$foo = '150' | '105'; # yields string '155' (under ASCII)
$baz = 0+$foo & 0+$bar; # both ops explicitly numeric
$biz = "$foo" ^ "$bar"; # both ops explicitly stringy
# I n t e g e r A r i t h m e t i c
B y d e f a u l t P e r l a s s u m e s t h a t i t m u s t d o m o s t o f i t s a r i t h m e t i c i n f l o a t i n g p o i n t . B u t b y s a y i n g
use integer;
y o u m a y t e l l t h e c o m p i l e r t h a t i t ' s o k a y t o u s e i n t e g e r o p e r a t i o n s f r o m h e r e t o t h e e n d o f t h e e n c l o s i n g B L O C K . A n i n n e r B L O C K m a y c o u n t e r m a n d t h i s b y s a y i n g
no integer;
w h i c h l a s t s u n t i l t h e e n d o f t h a t B L O C K .
T h e b i t w i s e o p e r a t o r s ( " & " , " | " , " ^ " , " ~ " , " < < " , a n d " > > " ) a l w a y s p r o d u c e i n t e g r a l r e s u l t s . ( B u t s e e a l s o " B i t w i s e S t r i n g O p e r a t o r s " . ) H o w e v e r , u s e i n t e g e r
s t i l l h a s m e a n i n g f o r t h e m . B y d e f a u l t , t h e i r r e s u l t s a r e i n t e r p r e t e d a s u n s i g n e d i n t e g e r s . H o w e v e r , i f u s e i n t e g e r
i s i n e f f e c t , t h e i r r e s u l t s a r e i n t e r p r e t e d a s s i g n e d i n t e g e r s . F o r e x a m p l e , ~ 0
u s u a l l y e v a l u a t e s t o a l a r g e i n t e g r a l v a l u e . H o w e v e r , u s e i n t e g e
r ; ~ 0
i s - 1 o n t w o s - c o m p l e m e n t m a c h i n e s .
# F l o a t i n g - p o i n t A r i t h m e t i c
W h i l e u s e i n t e g e r
p r o v i d e s i n t e g e r - o n l y a r i t h m e t i c , t h e r e i s n o s i m i l a r w a y s t o p r o v i d e r o u n d i n g o r t r u n c a t i o n a t a c e r t a i n n u m b e r o f d e c i m a l p l a c e s . F o r r o u n d i n g t o a c e r t a i n n u m b e r o f d i g i t s , s p r i n t f ( ) o r p r i n t f ( ) i s u s u a l l y t h e e a s i e s t r o u t e .
F l o a t i n g - p o i n t n u m b e r s a r e o n l y a p p r o x i m a t i o n s t o w h a t a m a t h e m a t i c i a n w o u l d c a l l r e a l n u m b e r s . T h e r e a r e i n f i n i t e l y m o r e r e a l s t h a n f l o a t s , s o s o m e c o r n e r s m u s t b e c u t . F o r e x a m p l e :
printf "%.20g\n", 123456789123456789;
# produces 123456789123456784
T e s t i n g f o r e x a c t e q u a l i t y o f f l o a t i n g - p o i n t e q u a l i t y o r i n e q u a l i t y i s n o t a g o o d i d e a . H e r e ' s a ( r e l a t i v e l y e x p e n s i v e ) w o r k - a r o u n d t o c o m p a r e w h e t h e r t w o f l o a t i n g - p o i n t n u m b e r s a r e e q u a l t o a p a r t i c u l a r n u m b e r o f d e c i m a l p l a c e s . S e e K n u t h , v o l u m e I I , f o r a m o r e r o b u s t t r e a t m e n t o f t h i s t o p i c .
sub fp_equal {
my ($X, $Y, $POINTS) = @_;
my ($tX, $tY);
$tX = sprintf("%.${POINTS}g", $X);
$tY = sprintf("%.${POINTS}g", $Y);
return $tX eq $tY;
}
T h e P O S I X m o d u l e ( p a r t o f t h e s t a n d a r d p e r l d i s t r i b u t i o n ) i m p l e m e n t s c e i l ( ) , f l o o r ( ) , a n d a n u m b e r o f o t h e r m a t h e m a t i c a l a n d t r i g o n o m e t r i c f u n c t i o n s . T h e M a t h : : C o m p l e x m o d u l e ( p a r t o f t h e s t a n d a r d p e r l d i s t r i b u t i o n ) d e f i n e s a n u m b e r o f m a t h e m a t i c a l f u n c t i o n s t h a t c a n a l s o w o r k o n r e a l n u m b e r s . M a t h : : C o m p l e x n o t a s e f f i c i e n t a s P O S I X , b u t P O S I X c a n ' t w o r k w i t h c o m p l e x n u m b e r s .
R o u n d i n g i n f i n a n c i a l a p p l i c a t i o n s c a n h a v e s e r i o u s i m p l i c a t i o n s , a n d t h e r o u n d i n g m e t h o d u s e d s h o u l d b e s p e c i f i e d p r e c i s e l y . I n t h e s e c a s e s , i t p r o b a b l y p a y s n o t t o t r u s t w h i c h e v e r s y s t e m r o u n d i n g i s b e i n g u s e d b y P e r l , b u t t o i n s t e a d i m p l e m e n t t h e r o u n d i n g f u n c t i o n y o u n e e d y o u r s e l f .
# B i g g e r N u m b e r s
T h e s t a n d a r d M a t h : : B i g I n t a n d M a t h : : B i g F l o a t m o d u l e s p r o v i d e v a r i a b l e p r e c i s i o n a r i t h m e t i c a n d o v e r l o a d e d o p e r a t o r s . A t t h e c o s t o f s o m e s p a c e a n d c o n s i d e r a b l e s p e e d , t h e y a v o i d t h e n o r m a l p i t f a l l s a s s o c i a t e d w i t h l i m i t e d - p r e c i s i o n r e p r e s e n t a t i o n s .
use Math::BigInt;
$x = Math::BigInt->new('123456789123456789');
print $x * $x;
# prints +15241578780673678515622620750190521
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 .