P e r l d o c B r o w s e r
●
5 . 2 2 . 4
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 . 2 2 . 4 .
V i e w t h e l a t e s t v e r s i o n
●
N A M E
●
D E S C R I P T I O N
●
O p e r a t o r P r e c e d e n c e a n d A s s o c i a t i v i t y
●
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
●
S m a r t m a t c h O p e r a t o r
●
S m a r t m a t c h i n g o f O b j e c t 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
●
L o g i c a l D e f i n e d - 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
●
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
●
N o - o p s
●
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
# D E S C R I P T I O N
I n P e r l , t h e o p e r a t o r d e t e r m i n e s w h a t o p e r a t i o n i s p e r f o r m e d , i n d e p e n d e n t o f t h e t y p e o f t h e o p e r a n d s . F o r e x a m p l e $ x + $ y
i s a l w a y s a n u m e r i c a d d i t i o n , a n d i f $ x
or $ y
d o n o t c o n t a i n n u m b e r s , a n a t t e m p t i s m a d e t o c o n v e r t t h e m t o n u m b e r s f i r s t .
T h i s i s i n c o n t r a s t t o m a n y o t h e r d y n a m i c l a n g u a g e s , w h e r e t h e o p e r a t i o n i s d e t e r m i n e d b y t h e t y p e o f t h e f i r s t a r g u m e n t . I t a l s o m e a n s t h a t P e r l h a s t w o v e r s i o n s o f s o m e o p e r a t o r s , o n e f o r n u m e r i c a n d o n e f o r s t r i n g c o m p a r i s o n . F o r e x a m p l e $ x = = $ y
c o m p a r e s t w o n u m b e r s f o r e q u a l i t y , a n d $ x e q $ y
c o m p a r e s t w o s t r i n g s .
T h e r e a r e a f e w e x c e p t i o n s t h o u g h : x
c a n b e e i t h e r s t r i n g r e p e t i t i o n o r l i s t r e p e t i t i o n , d e p e n d i n g o n t h e t y p e o f t h e l e f t o p e r a n d , a n d &
, |
, ^
a n d ~
c a n b e e i t h e r s t r i n g o r n u m e r i c b i t o p e r a t i o n s .
# O p e r a t o r P r e c e d e n c e a n d A s s o c i a t i v i t y
O p e r a t o r p r e c e d e n c e a n d a s s o c i a t i v i t y w o r k i n P e r l m o r e o r l e s s l i k e t h e y d o i n m a t h e m a t i c s .
O p e r a t o r p r e c e d e n c e m e a n s s o m e o p e r a t o r s a r e e v a l u a t e d b e f o r e o t h e r s . F o r e x a m p l e , i n 2 + 4 * 5
, t h e m u l t i p l i c a t i o n h a s h i g h e r p r e c e d e n c e s o 4 * 5
i s e v a l u a t e d f i r s t y i e l d i n g 2 + 2 0 = = 2 2
a n d n o t 6 * 5 = = 3 0
.
O p e r a t o r a s s o c i a t i v i t y d e f i n e s w h a t h a p p e n s i f a s e q u e n c e o f t h e s a m e o p e r a t o r s i s u s e d o n e a f t e r a n o t h e r : w h e t h e r t h e e v a l u a t o r w i l l e v a l u a t e t h e l e f t o p e r a t i o n s f i r s t , o r t h e r i g h t f i r s t . F o r e x a m p l e , i n 8 - 4 - 2
, s u b t r a c t i o n i s l e f t a s s o c i a t i v e s o P e r l e v a l u a t e s t h e e x p r e s s i o n l e f t t o r i g h t . 8 - 4
i s e v a l u a t e d f i r s t m a k i n g t h e e x p r e s s i o n 4 - 2 = =
2
a n d n o t 8 - 2 = = 6
.
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 . 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. goto last next redo dump
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 .
# 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 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 a r g u m e n t s t h a t f o l l o w , 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 . 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 . T h e p a r e n t h e s e s e n c l o s e t h e a r g u m e n t l i s t f o r p r i n t
w h i c h i s e v a l u a t e d ( p r i n t i n g t h e r e s u l t o f $ f o o & 2 5 5
) . T h e n o n e i s a d d e d t o t h e r e t u r n v a l u e o f p r i n t
( u s u a l l y 1 ) . T h e r e s u l t i s s o m e t h i n g l i k e t h i s :
1 + 1, "\n"; # Obviously not what you meant.
T o d o w h a t y o u m e a n t p r o p e r l y , y o u m u s t w r i t e :
print(($foo & 255) + 1, "\n");
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 " I / O O p e r a t o r s " .
# T h e A r r o w O p e r a t o r
" - >
" 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 , j u s t a s i t i s i n C a n d C + + . I f t h e r i g h t s i d e i s e i t h e r a [ . . . ]
, { . . . }
, o r a ( . . . )
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 , a h a s h , o r a s u b r o u t i n e r e s p e c t i v e l y . ( O r t e c h n i c a l l y s p e a k i n g , 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 a r r a y o r h a s h r e f e r e n c e b e i n g u s e d f o r a s s i g n m e n t . ) S e e p e r l r e f t u t a n d 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 e i t h e r t h e m e t h o d n a m e o r a s u b r o u t i n e r e f e r e n c e , a n d t h e l e f t s i d e m u s t b e e i t h e r 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 .
T h e d e r e f e r e n c i n g c a s e s ( a s o p p o s e d t o m e t h o d - c a l l i n g c a s e s ) a r e s o m e w h a t e x t e n d e d b y t h e e x p e r i m e n t a l p o s t d e r e f
f e a t u r e . F o r t h e d e t a i l s o f t h a t f e a t u r e , c o n s u l t " P o s t f i x D e r e f e r e n c e S y n t a x " i n p e r l r e f .
# 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 y o n 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 a f t e r r e t u r n i n g t h e v a l u e .
$i = 0; $j = 0;
print $i++; # prints 0
print ++$j; # prints 1
N o t e t h a t j u s t a s i n C , P e r l d o e s n ' t d e f i n e w h e n t h e v a r i a b l e i s i n c r e m e n t e d o r d e c r e m e n t e d . Y o u j u s t k n o w i t w i l l b e d o n e s o m e t i m e b e f o r e o r a f t e r t h e v a l u e i s r e t u r n e d . T h i s a l s o m e a n s t h a t m o d i f y i n g a v a r i a b l e t w i c e i n t h e s a m e s t a t e m e n t w i l l l e a d t o u n d e f i n e d b e h a v i o r . A v o i d s t a t e m e n t s l i k e :
$i = $i ++;
print ++ $i + $i ++;
P e r l w i l l n o t g u a r a n t e e w h a t t h e r e s u l t o f t h e a b o v e s t a t e m e n t s i s .
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 ] * \ z /
, 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"
u n d e f
i s a l w a y s t r e a t e d a s n u m e r i c , a n d i n p a r t i c u l a r i s c h a n g e d t o 0
b e f o r e i n c r e m e n t i n g ( s o t h a t a p o s t - i n c r e m e n t o f a n u n d e f v a l u e w i l l r e t u r n 0
r a t h e r t h a n u n d e f
) .
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 . 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
is - ( 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 . )
N o t e t h a t c e r t a i n e x p o n e n t i a t i o n e x p r e s s i o n s a r e i l l - d e f i n e d : t h e s e i n c l u d e 0 * * 0
, 1 * * I n f
, a n d I n
f * * 0
. D o n o t e x p e c t a n y p a r t i c u l a r r e s u l t s f r o m t h e s e s p e c i a l c a s e s , t h e r e s u l t s a r e p l a t f o r m - d e p e n d e n t .
# 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 , t h a t i s , " 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 n c l u d i n g a n y s t r i n g t h a t l o o k s l i k e a n u m b e r . 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 t h e s t r i n g " - b a r e w o r
d "
. I f , h o w e v e r , t h e s t r i n g b e g i n s w i t h a n o n - a l p h a b e t i c c h a r a c t e r ( e x c l u d i n g " + "
or " - "
) , P e r l w i l l a t t e m p t t o c o n v e r t t h e s t r i n g t o a n u m e r i c , a n d t h e a r i t h m e t i c n e g a t i o n i s p e r f o r m e d . I f t h e s t r i n g c a n n o t b e c l e a n l y c o n v e r t e d t o a n u m e r i c , P e r l w i l l g i v e t h e w a r n i n g A r g u m e n t " t h e s t r i n g " i s n ' t n u m e r i c i n n e g a t i o n ( - ) a t . . . .
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 , t h a t i s , 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 " . ) N o t e t h a t t h e w i d t h o f t h e r e s u l t i s p l a t f o r m - d e p e n d e n t : ~ 0
i s 3 2 b i t s w i d e o n a 3 2 - b i t p l a t f o r m , b u t 6 4 b i t s w i d e o n a 6 4 - b i t p l a t f o r m , s o i f y o u a r e e x p e c t i n g a c e r t a i n b i t w i d t h , r e m e m b e r t o u s e t h e " & "
o p e r a t o r t o m a s k o f f t h e e x c e s s b i t s .
W h e n c o m p l e m e n t i n g s t r i n g s , i f a l l c h a r a c t e r s h a v e o r d i n a l v a l u e s u n d e r 2 5 6 , t h e n t h e i r c o m p l e m e n t s w i l l , a l s o . B u t i f t h e y d o n o t , a l l c h a r a c t e r s w i l l b e i n e i t h e r 3 2 - o r 6 4 - b i t c o m p l e m e n t s , d e p e n d i n g o n y o u r a r c h i t e c t u r e . S o f o r e x a m p l e , ~ " \ x { 3 B 1 } "
is " \ x { F
F F F _ F C 4 E } "
o n 3 2 - b i t m a c h i n e s a n d " \ x { F F F F _ F F F F _ F F F F _ F C 4 E } "
o n 6 4 - b i t m a c h i n e s .
I f t h e e x p e r i m e n t a l " b i t w i s e " f e a t u r e i s e n a b l e d v i a u s e f e a t u r e
' b i t w i s e '
, t h e n u n a r y " ~ "
a l w a y s t r e a t s i t s a r g u m e n t a s a n u m b e r , a n d a n a l t e r n a t e f o r m o f t h e o p e r a t o r , " ~ . "
, a l w a y s t r e a t s i t s a r g u m e n t a s a s t r i n g . S o ~ 0
a n d ~ " 0 "
w i l l b o t h g i v e 2 * * 3 2 - 1 o n 3 2 - b i t p l a t f o r m s , w h e r e a s ~ . 0
a n d ~ . " 0 "
w i l l b o t h y i e l d " \ x f f "
. T h i s f e a t u r e p r o d u c e s a w a r n i n g u n l e s s y o u u s e n o w a r n i n g s ' e x p
e r i m e n t a l : : b i t w i s e '
.
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 t u t a n d 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 o l 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 $ _
. W h e n u s e d i n s c a l a r c o n t e x t , t h e r e t u r n v a l u e g e n e r a l l y 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 . T h e e x c e p t i o n s a r e s u b s t i t u t i o n ( s / / /
) a n d t r a n s l i t e r a t i o n ( y / / /
) w i t h t h e / r
( n o n - d e s t r u c t i v e ) o p t i o n , w h i c h c a u s e t h e r e t u r n v a l u e t o b e t h e r e s u l t o f t h e s u b s t i t u t i o n . B e h a v i o r i n l i s t c o n t e x t d e p e n d s o n t h e p a r t i c u l a r o p e r a t o r . S e e " R e g e x p Q u o t e - L i k e O p e r a t o r s " f o r d e t a i l s a n d p e r l r e t u t f o r e x a m p l e s u s i n g t h e s e o p e r a t o r s .
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 . N o t e t h a t t h i s m e a n s t h a t i t s c o n t e n t s w i l l b e i n t e r p o l a t e d t w i c e , s o
'\\' =~ q'\\';
i s n o t o k , a s t h e r e g e x e n g i n e w i l l e n d u p t r y i n g t o c o m p i l e t h e p a t t e r n \
, w h i c h i t w i l l c o n s i d e r a s y n t a x e r r o r .
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 .
B i n a r y " ! ~ "
w i t h a n o n - d e s t r u c t i v e s u b s t i t u t i o n ( s / / / r
) o r t r a n s l i t e r a t i o n ( y / / / r
) i s a s y n t a x e r r o r .
# 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 " % "
i s t h e m o d u l o o p e r a t o r , w h i c h c o m p u t e s t h e d i v i s i o n r e m a i n d e r o f i t s f i r s t a r g u m e n t w i t h r e s p e c t t o i t s s e c o n d a r g u m e n t . G i v e n i n t e g e r o p e r a n d s $ m
a n d $ n
: I f $ n
i s p o s i t i v e , t h e n $ m
% $ n
is $ m
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 $ n
l e s s t h a n o r e q u a l t o $ m
. I f $ n
i s n e g a t i v e , t h e n $ m %
$ n
is $ m
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 $ n
t h a t i s n o t l e s s t h a n $
m
( t h a t i s , 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 ) . I f t h e o p e r a n d s $ m
a n d $ n
a r e f l o a t i n g p o i n t v a l u e s a n d t h e a b s o l u t e v a l u e o f $ n
( t h a t i s a b s ( $ n )
) i s l e s s t h a n ( U V _ M A X + 1 )
, o n l y t h e i n t e g e r p o r t i o n o f $ m
a n d $ n
w i l l b e u s e d i n t h e o p e r a t i o n ( N o t e : h e r e U V _ M A X
m e a n s t h e m a x i m u m o f t h e u n s i g n e d i n t e g e r t y p e ) . I f t h e a b s o l u t e v a l u e o f t h e r i g h t o p e r a n d ( a b s ( $ n )
) i s g r e a t e r t h a n o r e q u a l t o ( U V _ M A X + 1 )
, " % "
c o m p u t e s t h e f l o a t i n g - p o i n t r e m a i n d e r $ r
i n t h e e q u a t i o n ( $ r
= $ m - $ i * $ n )
w h e r e $ i
i s a c e r t a i n i n t e g e r t h a t m a k e s $ r
h a v e t h e s a m e s i g n a s t h e r i g h t o p e r a n d $ n
( n o t a s t h e l e f t o p e r a n d $
m
l i k e C f u n c t i o n f m o d ( )
) a n d t h e a b s o l u t e v a l u e l e s s t h a n t h a t o f $ n
. N o t e t h a t 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 s 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 o 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 o r i f t h e l e f t o p e r a n d i s n o t e n c l o s e d i n p a r e n t h e s e s , 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 e n c l o s e d i n p a r e n t h e s e s o r i s a l i s t f o r m e d b y q w / S T R I N G
/
, i t r e p e a t s t h e l i s t . I f t h e r i g h t o p e r a n d i s z e r o o r n e g a t i v e ( r a i s i n g a w a r n i n g o n n e g a t i v e ) , i t r e t u r n s a n e m p t y s t r i n g o r a n e m p t y l i s t , d e p e n d i n g o n t h e c o n t e x 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 o t e t h a t b o t h < <
a n d > >
i n P e r l a r e i m p l e m e n t e d d i r e c t l y u s i n g < <
a n d > >
i n C . I f u s e i n t e g e r
( s e e " I n t e g e r A r i t h m e t i c " ) i s i n f o r c e t h e n s i g n e d C i n t e g e r s a r e u s e d , e l s e u n s i g n e d C i n t e g e r s a r e u s e d , e v e n f o r n e g a t i v e s h i f t e e s . E i t h e r w a y , t h e i m p l e m e n t a t i o n i s n ' t g o i n g t o g e n e r a t e r e s u l t s l a r g e r t h a n t h e s i z e o f t h e i n t e g e r t y p e P e r l w a s b u i l t w i t h ( 3 2 b i t s o r 6 4 b i t s ) .
T h e r e s u l t o f o v e r f l o w i n g t h e r a n g e o f t h e i n t e g e r s i s u n d e f i n e d b e c a u s e i t i s u n d e f i n e d a l s o i n C . I n o t h e r w o r d s , u s i n g 3 2 - b i t i n t e g e r s , 1 < < 32
i s u n d e f i n e d . S h i f t i n g b y a n e g a t i v e n u m b e r o f b i t s i s a l s o u n d e f i n e d .
I f y o u g e t t i r e d o f b e i n g s u b j e c t t o y o u r p l a t f o r m ' s n a t i v e i n t e g e r s , t h e u s e b i g i n t
p r a g m a n e a t l y s i d e s t e p s t h e i s s u e a l t o g e t h e r :
print 20 << 20; # 20971520
print 20 << 40; # 5120 on 32-bit machines,
# 21990232555520 on 64-bit machines
use bigint;
print 20 << 100; # 25353012004564588029934064107520
# 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 .
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 . F o r e x a m p l e , b e c a u s e n a m e d u n a r y o p e r a t o r s a r e h i g h e r p r e c e d e n c e t h a n | |
:
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 n a m e d o p e r a t o r s :
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)
R e g a r d i n g p r e c e d e n c 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 . a r e t r e a t e d l i k e n a m e d u n a r y o p e r a t o r s , b u t t h e y d o n ' t f o l l o w t h i s f u n c t i o n a l p a r e n t h e s i s r u l e . T h a t m e a n s , f o r e x a m p l e , t h a t - f
( $ f i l e ) . " . b a k "
i s e q u i v a l e n t t o - f " $ f i l e . b a k "
.
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
P e r l o p e r a t o r s t h a t r e t u r n t r u e o r f a l s e g e n e r a l l y r e t u r n v a l u e s t h a t c a n b e s a f e l y u s e d a s n u m b e r s . F o r e x a m p l e , t h e r e l a t i o n a l o p e r a t o r s i n t h i s s e c t i o n a n d t h e e q u a l i t y o p e r a t o r s i n t h e n e x t o n e r e t u r n 1
f o r t r u e a n d a s p e c i a l v e r s i o n o f t h e d e f i n e d e m p t y s t r i n g , " "
, w h i c h c o u n t s a s a z e r o b u t i s e x e m p t f r o m w a r n i n g s a b o u t i m p r o p e r n u m e r i c c o n v e r s i o n s , j u s t a s " 0 b u t t r u e "
i 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 . I f y o u r p l a t f o r m s u p p o r t s N a N
' s ( n o t - a - n u m b e r s ) a s n u m e r i c v a l u e s , u s i n g t h e m w i t h " < = > "
r e t u r n s u n d e f . N a N
i s n o t " < "
, " = = "
, " > "
, " < = "
or " > = "
a n y t h i n g ( e v e n N a N
) , s o t h o s e 5 r e t u r n f a l s e . N a N ! = N a N
r e t u r n s t r u e , a s d o e s N a N ! =
a n y t h i n g e l s e . I f y o u r p l a t f o r m d o e s n ' t s u p p o r t N a N
' s t h e n N a N
i s j u s t a s t r i n g w i t h n u m e r i c v a l u e 0 .
$ perl -le '$x = "NaN"; print "No NaN support here" if $x == $x'
$ perl -le '$x = "NaN"; print "NaN support here" if $x != $x'
( N o t e t h a t t h e b i g i n t , b i g r a t , a n d b i g n u m p r a g m a s a l l s u p p o r t " N
a N "
. )
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 .
B i n a r y " ~ ~ "
d o e s a s m a r t m a t c h b e t w e e n i t s a r g u m e n t s . S m a r t m a t c h i n g i s d e s c r i b e d i n t h e n e x t s e c t i o n .
" 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 C _ C O L L
A T E
l o c a l e i f a u s e l o c a l e
f o r m t h a t i n c l u d e s c o l l a t i o n i s i n e f f e c t . S e e p e r l l o c a l e . D o n o t m i x t h e s e w i t h U n i c o d e , o n l y u s e t h e m w i t h l e g a c y 8 - b i t l o c a l e e n c o d i n g s . T h e s t a n d a r d U n i c o d e : : C o l
l a t e
a n d U n i c o d e : : C o l l a t e : : L o c a l
e
m o d u l e s o f f e r m u c h m o r e p o w e r f u l s o l u t i o n s t o c o l l a t i o n i s s u e s .
F o r c a s e - i n s e n s i t i v e c o m p a r i s i o n s , l o o k a t t h e " f c " i n p e r l f u n c c a s e - f o l d i n g f u n c t i o n , a v a i l a b l e i n P e r l v 5 . 1 6 o r l a t e r :
if ( fc($x) eq fc($y) ) { ... }
# S m a r t m a t c h O p e r a t o r
F i r s t a v a i l a b l e i n P e r l 5 . 1 0 . 1 ( t h e 5 . 1 0 . 0 v e r s i o n b e h a v e d d i f f e r e n t l y ) , b i n a r y ~ ~
d o e s a " s m a r t m a t c h " b e t w e e n i t s a r g u m e n t s . T h i s i s m o s t l y u s e d i m p l i c i t l y i n t h e w h e n
c o n s t r u c t d e s c r i b e d i n p e r l s y n , a l t h o u g h n o t a l l w h e n
c l a u s e s c a l l t h e s m a r t m a t c h o p e r a t o r . U n i q u e a m o n g a l l o f P e r l ' s o p e r a t o r s , t h e s m a r t m a t c h o p e r a t o r c a n r e c u r s e . T h e s m a r t m a t c h o p e r a t o r i s e x p e r i m e n t a l a n d i t s b e h a v i o r i s s u b j e c t t o c h a n g e .
I t i s a l s o u n i q u e i n t h a t a l l o t h e r P e r l o p e r a t o r s i m p o s e a c o n t e x t ( u s u a l l y s t r i n g o r n u m e r i c c o n t e x t ) o n t h e i r o p e r a n d s , a u t o c o n v e r t i n g t h o s e o p e r a n d s t o t h o s e i m p o s e d c o n t e x t s . I n c o n t r a s t , s m a r t m a t c h i n f e r s c o n t e x t s f r o m t h e a c t u a l t y p e s o f i t s o p e r a n d s a n d u s e s t h a t t y p e i n f o r m a t i o n t o s e l e c t a s u i t a b l e c o m p a r i s o n m e c h a n i s m .
T h e ~ ~
o p e r a t o r c o m p a r e s i t s o p e r a n d s " p o l y m o r p h i c a l l y " , d e t e r m i n i n g h o w t o c o m p a r e t h e m a c c o r d i n g t o t h e i r a c t u a l t y p e s ( n u m e r i c , s t r i n g , a r r a y , h a s h , e t c . ) L i k e t h e e q u a l i t y o p e r a t o r s w i t h w h i c h i t s h a r e s t h e s a m e p r e c e d e n c e , ~ ~
r e t u r n s 1 f o r t r u e a n d " "
f o r f a l s e . I t i s o f t e n b e s t r e a d a l o u d a s " i n " , " i n s i d e o f " , o r " i s c o n t a i n e d i n " , b e c a u s e t h e l e f t o p e r a n d i s o f t e n l o o k e d f o r i n s i d e t h e r i g h t o p e r a n d . T h a t m a k e s t h e o r d e r o f t h e o p e r a n d s t o t h e s m a r t m a t c h o p e r a n d o f t e n o p p o s i t e t h a t o f t h e r e g u l a r m a t c h o p e r a t o r . I n o t h e r w o r d s , t h e " s m a l l e r " t h i n g i s u s u a l l y p l a c e d i n t h e l e f t o p e r a n d a n d t h e l a r g e r o n e i n t h e r i g h t .
T h e b e h a v i o r o f a s m a r t m a t c h d e p e n d s o n w h a t t y p e o f t h i n g s i t s a r g u m e n t s a r e , a s d e t e r m i n e d b y t h e f o l l o w i n g t a b l e . T h e f i r s t r o w o f t h e t a b l e w h o s e t y p e s a p p l y d e t e r m i n e s t h e s m a r t m a t c h b e h a v i o r . B e c a u s e w h a t a c t u a l l y h a p p e n s i s m o s t l y d e t e r m i n e d b y t h e t y p e o f t h e s e c o n d o p e r a n d , t h e t a b l e i s s o r t e d o n t h e r i g h t o p e r a n d i n s t e a d o f o n t h e l e f t .
Left Right Description and pseudocode
===============================================================
Any undef check whether Any is undefined
like: !defined Any
Any Object invoke ~~ overloading on Object, or die
Right operand is an ARRAY:
Left Right Description and pseudocode
===============================================================
ARRAY1 ARRAY2 recurse on paired elements of ARRAY1 and ARRAY2[2 ]
like: (ARRAY1[0] ~~ ARRAY2[0])
&& (ARRAY1[1 ] ~~ ARRAY2[1 ]) && ...
HASH ARRAY any ARRAY elements exist as HASH keys
like: grep { exists HASH->{$_} } ARRAY
Regexp ARRAY any ARRAY elements pattern match Regexp
like: grep { /Regexp/ } ARRAY
undef ARRAY undef in ARRAY
like: grep { !defined } ARRAY
Any ARRAY smartmatch each ARRAY element[3 ]
like: grep { Any ~~ $_ } ARRAY
Right operand is a HASH:
Left Right Description and pseudocode
===============================================================
HASH1 HASH2 all same keys in both HASHes
like: keys HASH1 ==
grep { exists HASH2->{$_} } keys HASH1
ARRAY HASH any ARRAY elements exist as HASH keys
like: grep { exists HASH->{$_} } ARRAY
Regexp HASH any HASH keys pattern match Regexp
like: grep { /Regexp/ } keys HASH
undef HASH always false (undef can't be a key)
like: 0 == 1
Any HASH HASH key existence
like: exists HASH->{Any}
Right operand is CODE:
Left Right Description and pseudocode
===============================================================
ARRAY CODE sub returns true on all ARRAY elements[1 ]
like: !grep { !CODE->($_) } ARRAY
HASH CODE sub returns true on all HASH keys[1 ]
like: !grep { !CODE->($_) } keys HASH
Any CODE sub passed Any returns true
like: CODE->(Any)
R i g h t o p e r a n d i s a R e g e x p :
Left Right Description and pseudocode
===============================================================
ARRAY Regexp any ARRAY elements match Regexp
like: grep { /Regexp/ } ARRAY
HASH Regexp any HASH keys match Regexp
like: grep { /Regexp/ } keys HASH
Any Regexp pattern match
like: Any =~ /Regexp/
Other:
Left Right Description and pseudocode
===============================================================
Object Any invoke ~~ overloading on Object,
or fall back to...
Any Num numeric equality
like: Any == Num
Num nummy[4 ] numeric equality
like: Num == nummy
undef Any check whether undefined
like: !defined(Any)
Any Any string equality
like: Any eq Any
N o t e s :
# 1 . E m p t y h a s h e s o r a r r a y s m a t c h .
# 2 . T h a t i s , e a c h e l e m e n t s m a r t m a t c h e s t h e e l e m e n t o f t h e s a m e i n d e x i n t h e o t h e r a r r a y . [ 3 ]
# 3 . I f a c i r c u l a r r e f e r e n c e i s f o u n d , f a l l b a c k t o r e f e r e n t i a l e q u a l i t y .
# 4 . E i t h e r a n a c t u a l n u m b e r , o r a s t r i n g t h a t l o o k s l i k e o n e .
T h e s m a r t m a t c h i m p l i c i t l y d e r e f e r e n c e s a n y n o n - b l e s s e d h a s h o r a r r a y r e f e r e n c e , s o t h e H A S H
a n d
A R R A Y
e n t r i e s a p p l y i n t h o s e c a s e s . F o r b l e s s e d r e f e r e n c e s , t h e
O b j e c t
e n t r i e s a p p l y . S m a r t m a t c h e s i n v o l v i n g h a s h e s o n l y c o n s i d e r h a s h k e y s , n e v e r h a s h v a l u e s .
T h e " l i k e " c o d e e n t r y i s n o t a l w a y s a n e x a c t r e n d i t i o n . F o r e x a m p l e , t h e s m a r t m a t c h o p e r a t o r s h o r t - c i r c u i t s w h e n e v e r p o s s i b l e , b u t g r e p
d o e s n o t . A l s o , g r e p
i n s c a l a r c o n t e x t r e t u r n s t h e n u m b e r o f m a t c h e s , b u t ~ ~
r e t u r n s o n l y t r u e o r f a l s e .
U n l i k e m o s t o p e r a t o r s , t h e s m a r t m a t c h o p e r a t o r k n o w s t o t r e a t u n
d e f
s p e c i a l l y :
use v5.10.1;
@array = (1, 2, 3, undef, 4, 5);
say "some elements undefined" if undef ~~ @array;
E a c h o p e r a n d i s c o n s i d e r e d i n a m o d i f i e d s c a l a r c o n t e x t , t h e m o d i f i c a t i o n b e i n g t h a t a r r a y a n d h a s h v a r i a b l e s a r e p a s s e d b y r e f e r e n c e t o t h e o p e r a t o r , w h i c h i m p l i c i t l y d e r e f e r e n c e s t h e m . B o t h e l e m e n t s o f e a c h p a i r a r e t h e s a m e :
use v5.10.1;
my %hash = (red => 1, blue => 2, green => 3,
orange => 4, yellow => 5, purple => 6,
black => 7, grey => 8, white =>9 );
my @array = qw(red blue green);
say "some array elements in hash keys" if @array ~~ %hash;
say "some array elements in hash keys" if \@array ~~ \%hash;
say "red in array" if "red" ~~ @array;
say "red in array" if "red" ~~ \@array;
say "some keys end in e" if /e$/ ~~ %hash;
say "some keys end in e" if /e$/ ~~ \%hash;
T w o a r r a y s s m a r t m a t c h i f e a c h e l e m e n t i n t h e f i r s t a r r a y s m a r t m a t c h e s ( t h a t i s , i s " i n " ) t h e c o r r e s p o n d i n g e l e m e n t i n t h e s e c o n d a r r a y , r e c u r s i v e l y .
use v5.10.1;
my @little = qw(red blue green);
my @bigger = ("red", "blue", [ "orange", "green" ] );
if (@little ~~ @bigger) { # true!
say "little is contained in bigger";
}
B e c a u s e t h e s m a r t m a t c h o p e r a t o r r e c u r s e s o n n e s t e d a r r a y s , t h i s w i l l s t i l l r e p o r t t h a t " r e d " i s i n t h e a r r a y .
use v5.10.1;
my @array = qw(red blue green);
my $nested_array = [[[[[[[ @array ]]]]]]];
say "red in array" if "red" ~~ $nested_array;
I f t w o a r r a y s s m a r t m a t c h e a c h o t h e r , t h e n t h e y a r e d e e p c o p i e s o f e a c h o t h e r s ' v a l u e s , a s t h i s e x a m p l e r e p o r t s :
use v5.12.0;
my @a = (0, 1, 2, [3, [4, 5], 6], 7);
my @b = (0, 1, 2, [3, [4, 5], 6], 7);
if (@a ~~ @b && @b ~~ @a) {
say "a and b are deep copies of each other";
}
elsif (@a ~~ @b) {
say "a smartmatches in b";
}
elsif (@b ~~ @a) {
say "b smartmatches in a";
}
else {
say "a and b don't smartmatch each other at all";
}
I f y o u w e r e t o s e t $ b [ 3 ] = 4
, t h e n i n s t e a d o f r e p o r t i n g t h a t " a a n d b a r e d e e p c o p i e s o f e a c h o t h e r " , i t n o w r e p o r t s t h a t " b s m a
r t m a t c h e s i n a "
. T h a t ' s b e c a u s e t h e c o r r e s p o n d i n g p o s i t i o n i n @ a
c o n t a i n s a n a r r a y t h a t ( e v e n t u a l l y ) h a s a 4 i n i t .
S m a r t m a t c h i n g o n e h a s h a g a i n s t a n o t h e r r e p o r t s w h e t h e r b o t h c o n t a i n t h e s a m e k e y s , n o m o r e a n d n o l e s s . T h i s c o u l d b e u s e d t o s e e w h e t h e r t w o r e c o r d s h a v e t h e s a m e f i e l d n a m e s , w i t h o u t c a r i n g w h a t v a l u e s t h o s e f i e l d s m i g h t h a v e . F o r e x a m p l e :
use v5.10.1;
sub make_dogtag {
state $REQUIRED_FIELDS = { name=>1, rank=>1, serial_num=>1 };
my ($class, $init_fields) = @_;
die "Must supply (only) name, rank, and serial number"
unless $init_fields ~~ $REQUIRED_FIELDS;
...
}
o r , i f o t h e r n o n - r e q u i r e d f i e l d s a r e a l l o w e d , u s e A R R A Y ~ ~ H A S H :
use v5.10.1;
sub make_dogtag {
state $REQUIRED_FIELDS = { name=>1, rank=>1, serial_num=>1 };
my ($class, $init_fields) = @_;
die "Must supply (at least) name, rank, and serial number"
unless [keys %{$init_fields}] ~~ $REQUIRED_FIELDS;
...
}
T h e s m a r t m a t c h o p e r a t o r i s m o s t o f t e n u s e d a s t h e i m p l i c i t o p e r a t o r o f a w h e n
c l a u s e . S e e t h e s e c t i o n o n " S w i t c h S t a t e m e n t s " in p e r l s y n .
# S m a r t m a t c h i n g o f O b j e c t s
T o a v o i d r e l y i n g o n a n o b j e c t ' s u n d e r l y i n g r e p r e s e n t a t i o n , i f t h e s m a r t m a t c h ' s r i g h t o p e r a n d i s a n o b j e c t t h a t d o e s n ' t o v e r l o a d ~ ~
, i t r a i s e s t h e e x c e p t i o n " S m a
r t m a t c h i n g a n o n - o v e r l o a d e d o b j e
c t b r e a k s e n c a p s u l a t i o n
" . T h a t ' s b e c a u s e o n e h a s n o b u s i n e s s d i g g i n g a r o u n d t o s e e w h e t h e r s o m e t h i n g i s " i n " a n o b j e c t . T h e s e a r e a l l i l l e g a l o n o b j e c t s w i t h o u t a ~ ~
o v e r l o a d :
%hash ~~ $object
42 ~~ $object
"fred" ~~ $object
H o w e v e r , y o u c a n c h a n g e t h e w a y a n o b j e c t i s s m a r t m a t c h e d b y o v e r l o a d i n g t h e ~ ~
o p e r a t o r . T h i s i s a l l o w e d t o e x t e n d t h e u s u a l s m a r t m a t c h s e m a n t i c s . F o r o b j e c t s t h a t d o h a v e a n ~ ~
o v e r l o a d , s e e o v e r l o a d .
U s i n g a n o b j e c t a s t h e l e f t o p e r a n d i s a l l o w e d , a l t h o u g h n o t v e r y u s e f u l . S m a r t m a t c h i n g r u l e s t a k e p r e c e d e n c e o v e r o v e r l o a d i n g , s o e v e n i f t h e o b j e c t i n t h e l e f t o p e r a n d h a s s m a r t m a t c h o v e r l o a d i n g , t h i s w i l l b e i g n o r e d . A l e f t o p e r a n d t h a t i s a n o n - o v e r l o a d e d o b j e c t f a l l s b a c k o n a s t r i n g o r n u m e r i c c o m p a r i s o n o f w h a t e v e r t h e r e f
o p e r a t o r r e t u r n s . T h a t m e a n s t h a t
$object ~~ X
d o e s n o t i n v o k e t h e o v e r l o a d m e t h o d w i t h X
a s a n a r g u m e n t . I n s t e a d t h e a b o v e t a b l e i s c o n s u l t e d a s n o r m a l , a n d b a s e d o n t h e t y p e o f X
, o v e r l o a d i n g m a y o r m a y n o t b e i n v o k e d . F o r s i m p l e s t r i n g s o r n u m b e r s , " i n " b e c o m e s e q u i v a l e n t t o t h i s :
$object ~~ $number ref($object) == $number
$object ~~ $string ref($object) eq $string
F o r e x a m p l e , t h i s r e p o r t s t h a t t h e h a n d l e s m e l l s I O i s h ( b u t p l e a s e d o n ' t r e a l l y d o t h i s ! ) :
use IO::Handle;
my $fh = IO::Handle->new();
if ($fh ~~ /\bIO\b/) {
say "handle smells IOish";
}
T h a t ' s b e c a u s e i t t r e a t s $ f h
a s a s t r i n g l i k e " I O : : H a n d l e = G L O B ( 0
x 8 0 3 9 e 0 ) "
, t h e n p a t t e r n m a t c h e s a g a i n s t t h a t .
# 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 n d s A N D e d t o g e t h e r b i t b y b i t . A l t h o u g h n o w a r n i n g i s c u r r e n t l y r a i s e d , t h e r e s u l t i s n o t w e l l d e f i n e d w h e n t h i s o p e r a t i o n i s p e r f o r m e d o n o p e r a n d s t h a t a r e n ' t e i t h e r n u m b e r s ( s e e " I n t e g e r A r i t h m e t i c " ) n o r b i t s t r i n g s ( s e e " B i t w i s e S t r i n g O p e r a t o r s " ) .
N o t e t h a t " & "
h a s l o w e r p r i o r i t y t h a n r e l a t i o n a l o p e r a t o r s , s o f o r e x a m p l e t h e p a r e n t h e s e s a r e e s s e n t i a l i n a t e s t l i k e
print "Even\n" if ($x &1 ) == 0;
I f t h e e x p e r i m e n t a l " b i t w i s e " f e a t u r e i s e n a b l e d v i a u s e f e a t u r e
' b i t w i s e '
, t h e n t h i s o p e r a t o r a l w a y s t r e a t s i t s o p e r a n d a s n u m b e r s . T h i s f e a t u r e p r o d u c e s a w a r n i n g u n l e s s y o u a l s o u s e n o w a r n
i n g s ' e x p e r i m e n t a l : : b i t w i s e '
.
# 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 n d s O R e d t o g e t h e r b i t b y b i t .
B i n a r y " ^ "
r e t u r n s i t s o p e r a n d s X O R e d t o g e t h e r b i t b y b i t .
A l t h o u g h n o w a r n i n g i s c u r r e n t l y r a i s e d , t h e r e s u l t s a r e n o t w e l l d e f i n e d w h e n t h e s e o p e r a t i o n s a r e p e r f o r m e d o n o p e r a n d s t h a t a r e n ' t e i t h e r n u m b e r s ( s e e " I n t e g e r A r i t h m e t i c " ) n o r b i t s t r i n g s ( s e e " B i t w i s e S t r i n g O p e r a t o r s " ) .
N o t e t h a t " | "
a n d " ^ "
h a v e l o w e r p r i o r i t y t h a n r e l a t i o n a l o p e r a t o r s , s o f o r e x a m p l e t h e p a r e n t h e s e s a r e e s s e n t i a l i n a t e s t l i k e
print "false\n" if (8 | 2) != 10;
I f t h e e x p e r i m e n t a l " b i t w i s e " f e a t u r e i s e n a b l e d v i a u s e f e a t u r e
' b i t w i s e '
, t h e n t h i s o p e r a t o r a l w a y s t r e a t s i t s o p e r a n d a s n u m b e r s . T h i s f e a t u r e p r o d u c e s a w a r n i n g u n l e s s y o u a l s o u s e n o w a r n
i n g s ' e x p e r i m e n t a l : : b i t w i s e '
.
# 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 .
# L o g i c a l D e f i n e d - O r
A l t h o u g h i t h a s n o d i r e c t e q u i v a l e n t i n C , P e r l ' s / /
o p e r a t o r i s r e l a t e d t o i t s C - s t y l e " o r " . I n f a c t , i t ' s e x a c t l y t h e s a m e a s |
|
, e x c e p t t h a t i t t e s t s t h e l e f t h a n d s i d e ' s d e f i n e d n e s s i n s t e a d o f i t s t r u t h . T h u s , E X P R 1 / / E X
P R 2
r e t u r n s t h e v a l u e o f E X P R 1
i f i t ' s d e f i n e d , o t h e r w i s e , t h e v a l u e o f E X P R 2
i s r e t u r n e d . ( E X P R
1
i s e v a l u a t e d i n s c a l a r c o n t e x t , E X P R 2
i n t h e c o n t e x t o f / /
i t s e l f ) . U s u a l l y , t h i s i s t h e s a m e r e s u l t a s d e f i n e d ( E X P R 1 ) ? E X P R 1
: E X P R 2
( e x c e p t t h a t t h e t e r n a r y - o p e r a t o r f o r m c a n b e u s e d a s a l v a l u e , w h i l e E X P R 1 / / E X P R 2
c a n n o t ) . T h i s i s v e r y u s e f u l f o r p r o v i d i n g d e f a u l t v a l u e s f o r v a r i a b l e s . I f y o u a c t u a l l y w a n t t o t e s t i f a t l e a s t o n e o f $ x
a n d $ y
i s d e f i n e d , u s e d e f i n e d ( $ x / / $ y )
.
T h e | |
, / /
a n d & &
o p e r a t o r s 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 ( u n l i k e C ' s | |
a n d & &
, w h i c h r e t u r n 0 o r 1 ) . 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 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 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 t h e 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);
I t w o u l d b e e v e n m o r e r e a d a b l e t o w r i t e t h a t t h i s w a y :
unless(unlink("alpha", "beta", "gamma")) {
gripe();
next LINE;
}
U s i n g " 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 l i s t o f v a l u e s c o u n t i n g ( u p 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 . I f t h e l e f t v a l u e i s g r e a t e r t h a n t h e r i g h t v a l u e t h e n i t r e t u r n s t h e e m p t y l i s t . T h e r a n g e o p e r a t o r 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
}
T h e r a n g e o p e r a t o r a l s o w o r k s o n s t r i n g s , u s i n g t h e m a g i c a l a u t o - i n c r e m e n t , s e e b e l o w .
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 , e v e n a c r o s s c a l l s t o a s u b r o u t i n e t h a t c o n t a i n s i t . 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 u n t i 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 , j u s t u s e t h r e e d o t s ( " . . . "
) i n s t e a d o f t w o . I n a l l o t h e r r e g a r d s , " . . .
"
b e h a v e s j u s t l i k e " . . "
d o e s .
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 c o n s i d e r e d t r u e i f i t i s e q u a l ( = =
) t o t h e c u r r e n t i n p u t l i n e n u m b e r ( t h e $ .
v a r i a b l e ) .
T o b e p e d a n t i c , t h e c o m p a r i s o n i s a c t u a l l y i n t ( E X P R ) = = i n t ( E X P R )
, b u t t h a t i s o n l y a n i s s u e i f y o u u s e a f l o a t i n g p o i n t e x p r e s s i o n ; w h e n i m p l i c i t l y u s i n g $ .
a s d e s c r i b e d i n t h e p r e v i o u s p a r a g r a p h , t h e c o m p a r i s o n i s i n t ( E X P R )
= = i n t ( $ . )
w h i c h i s o n l y a n i s s u e w h e n $ .
i s s e t t o a f l o a t i n g p o i n t v a l u e a n d y o u a r e n o t r e a d i n g f r o m a f i l e . F u r t h e r m o r e , " s
p a n " . . " s p a t "
or 2 . 1 8 . . 3 . 1 4
w i l l n o t d o w h a t y o u w a n t i n s c a l a r c o n t e x t b e c a u s e e a c h o f t h e o p e r a n d s a r e e v a l u a t e d u s i n g t h e i r i n t e g e r r e p r e s e n t a t i o n .
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, short for
# if ($. == 101 .. $. == 200) { print; }
next LINE if (1 .. /^$/); # skip header lines, short for
# next LINE if ($. == 1 .. /^$/);
# (typically in a loop labeled LINE)
s/^/> / if (/^$/ .. eof()); # quote body
# parse mail messages
while (<>) {
$in_header = 1 .. /^$/;
$in_body = /^$/ .. eof;
if ($in_header) {
# do something
} else { # in body
# do something else
}
} continue {
close ARGV if eof; # reset $. each file
}
H e r e ' s a s i m p l e e x a m p l e t o i l l u s t r a t e t h e d i f f e r e n c e b e t w e e n t h e t w o r a n g e o p e r a t o r s :
@lines = (" - Foo",
"01 - Bar",
"1 - Baz",
" - Quux");
foreach (@lines) {
if (/0/ .. /1/) {
print "$_\n";
}
}
T h i s p r o g r a m w i l l p r i n t o n l y t h e l i n e c o n t a i n i n g " B a r " . I f t h e r a n g e o p e r a t o r i s c h a n g e d t o . . .
, i t w i l l a l s o p r i n t t h e " B a z " l i n e .
A n d n o w s o m e e x a m p l e s 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 n o r m a l l e t t e r s o f t h e E n g l i s h 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 .
I f t h e i n i t i a l v a l u e s p e c i f i e d i s n ' t p a r t o f a m a g i c a l i n c r e m e n t s e q u e n c e ( t h a t i s , a n o n - e m p t y s t r i n g m a t c h i n g / ^ [ a - z A - Z ] * [ 0 - 9 ]
* \ z /
) , o n l y t h e i n i t i a l v a l u e w i l l b e r e t u r n e d . S o t h e f o l l o w i n g w i l l o n l y r e t u r n a n a l p h a :
use charnames "greek";
my @greek_small = ("\N{alpha}" .. "\N{omega}");
T o g e t t h e 2 5 t r a d i t i o n a l l o w e r c a s e G r e e k l e t t e r s , i n c l u d i n g b o t h s i g m a s , y o u c o u l d u s e t h i s i n s t e a d :
use charnames "greek";
my @greek_small = map { chr } ( ord("\N{alpha}")
..
ord("\N{omega}")
);
H o w e v e r , b e c a u s e t h e r e a r e m a n y o t h e r l o w e r c a s e G r e e k c h a r a c t e r s t h a n j u s t t h o s e , t o m a t c h l o w e r c a s e G r e e k c h a r a c t e r s i n a r e g u l a r e x p r e s s i o n , y o u c o u l d u s e t h e p a t t e r n / ( ? : ( ? = \ p { G r e e k } ) \ p { L o w
e r } ) + /
( o r t h e e x p e r i m e n t a l f e a t u r e / ( ? [ \ p { G r e e k } & \ p { L o w e r } ] )
+ /
) .
B e c a u s e e a c h o p e r a n d i s e v a l u a t e d i n i n t e g e r f o r m , 2 . 1 8 . . 3 . 1 4
w i l l r e t u r n t w o e l e m e n t s i n l i s t c o n t e x t .
@list = (2.18 .. 3.14); # same as @list = (2 .. 3);
# 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 .
$x = $ok ? $y : $z; # get a scalar
@x = $ok ? @y : @z; # get an array
$x = $ok ? @y : @z; # 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 ) :
($x_or_y ? $x : $y) = $z;
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 :
$x % 2 ? $x += 10 : $x += 2
R e a l l y m e a n s t h i s :
(($x % 2) ? ($x += 10) : $x) += 2
R a t h e r t h a n t h i s :
($x % 2) ? ($x += 10) : ($x += 2)
T h a t s h o u l d p r o b a b l y b e w r i t t e n m o r e s i m p l y a s :
$x += ($x % 2) ? 10 : 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 ,
$x += 2;
i s e q u i v a l e n t t o
$x = $x + 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=
A l t h o u g h 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 . T h e s e c o m b i n e d a s s i g n m e n t o p e r a t o r s c a n o n l y o p e r a t e o n s c a l a r s , w h e r e a 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 c a n a s s i g n t o a r r a y s , h a s h e s , l i s t s a n d e v e n r e f e r e n c e s . ( S e e " C o n t e x t " a n d " L i s t v a l u e c o n s t r u c t o r s " i n p e r l d a t a , a n d " A s s i g n i n g t o R e f e r e n c e s " i n p e r l r e f . )
U n l i k e i n C , t h e s c a l a r 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/13579/24680/;
A l t h o u g h a s o f 5 . 1 4 , t h a t c a n b e a l s o b e a c c o m p l i s h e d t h i s w a y :
use v5.14;
$tmp = ($global =~ tr/13579/24680/r);
L i k e w i s e ,
($x += 2) *= 3;
i s e q u i v a l e n t t o
$x += 2;
$x *= 3;
S i m i l a r l y , a l i s t a s s i g n m e n t i n l i s t c o n t e x t p r o d u c e s t h e l i s t o f l v a l u e s a s s i g n e d t o , a n d a l i s t a s s i g n m e n t i n s c a l a r c o n t e x t r e t u r n s t h e n u m b e r o f e l e m e n t s p r o d u c e d b y t h e e x p r e s s i o n o n t h e r i g h t h a n d s i d e o f t h e a s s i g n m e n t .
T h e t h r e e d o t t e d b i t w i s e a s s i g n m e n t o p e r a t o r s ( & . =
| . =
^ . =
) a r e n e w i n P e r l 5 . 2 2 a n d e x p e r i m e n t a l . S e e " B i t w i s e S t r i n g O p e r a t o r s " .
# 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 s e a r g u m e n t s a r e a l s o e v a l u a t e d f r o m l e f t t o r i g h t .
T h e = >
o p e r a t o r ( s o m e t i m e s p r o n o u n c e d " f a t c o m m a " ) i s a s y n o n y m f o r t h e c o m m a e x c e p t t h a t i t c a u s e s a w o r d o n i t s l e f 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 i f i t b e g i n s w i t h a l e t t e r o r u n d e r s c o r e a n d i s c o m p o s e d o n l y o f l e t t e r s , d i g i t s a n d u n d e r s c o r e s . T h i s i n c l u d e s o p e r a n d s t h a t m i g h t o t h e r w i s e b e i n t e r p r e t e d a s o p e r a t o r s , c o n s t a n t s , s i n g l e n u m b e r v - s t r i n g s o r f u n c t i o n c a l l s . I f i n d o u b t a b o u t t h i s b e h a v i o r , t h e l e f t o p e r a n d c a n b e q u o t e d e x p l i c i t l y .
O t h e r w i s e , t h e = >
o p e r a t o r b e h a v e s e x a c t l y a s t h e c o m m a o p e r a t o r o r l i s t a r g u m e n t s e p a r a t o r , a c c o r d i n g t o c o n t e x t .
F o r e x a m p l e :
use constant FOO => "something";
my %h = ( FOO =>23 );
i s e q u i v a l e n t t o :
my %h = ("FOO", 23);
I t i s N O T :
my %h = ("something", 23);
T h e = >
o p e r a t o r i s h e l p f u l i n d o c u m e n t i n g t h e c o r r e s p o n d e n c e b e t w e e n k e y s a n d v a l u e s i n h a s h e s , a n d o t h e r p a i r e d e l e m e n t s i n l i s t s .
%hash = ( $key => $value );
login( $username => $password );
T h e s p e c i a l q u o t i n g b e h a v i o r i g n o r e s p r e c e d e n c e , a n d h e n c e m a y a p p l y t o p a r t o f t h e l e f t o p e r a n d :
print time.shift => "bbb";
T h a t e x a m p l e p r i n t s s o m e t h i n g l i k e " 1 3 1 4 3 6 3 2 1 5 s h i f t b b b "
, b e c a u s e t h e = >
i m p l i c i t l y q u o t e s t h e s h
i f t
i m m e d i a t e l y o n i t s l e f t , i g n o r i n g t h e f a c t t h a t t i m e . s h i f t
i s t h e e n t i r e l e f t o p e r a n d .
# 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 , t h e c o m m a 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 p a r e n t h e s e s :
open HANDLE, "< :utf8", "filename" or die "Can't open: $!\n";
H o w e v e r , s o m e p e o p l e f i n d t h a t c o d e h a r d e r t o r e a d t h a n w r i t i n g i t w i t h p a r e n t h e s e s :
open(HANDLE, "< :utf8", "filename") or die "Can't open: $!\n";
i n w h i c h c a s e y o u m i g h t a s w e l l j u s t u s e t h e m o r e c u s t o m a r y " | | "
o p e r a t o r :
open(HANDLE, "< :utf8", "filename") || 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 : 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 : 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 m u s t b e c a r e f u l t o a v o i d u s i n g i t a s r e p l a c e m e n t f o r t h e | |
o p e r a t o r . I t u s u a l l y w o r k s o u t b e t t e r f o r f l o w c o n t r o l t h a n i n a s s i g n m e n t s :
$x = $y or $z; # bug: this is wrong
($x = $y) or $z; # really means this
$x = $y || $z; # 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 ) .
T h e r e i s n o l o w p r e c e d e n c e o p e r a t o r f o r d e f i n e d - 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
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 .
Customary Generic Meaning Interpolates
'' q{} Literal no
"" qq{} Literal yes
`` qx{} Command yes*
qw{} Word list no
// m{} Pattern match yes*
qr{} Pattern yes*
s{}{} Substitution yes*
tr{}{} Transliteration no (but see below)
y{}{} Transliteration no (but see below)
<<EOF here-doc yes*
* unless the delimiter is ''.
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 f o u r s o r t s o f A S C I I 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 ) a l l n e s t , w h i c h m e a n s t h a t
q{foo{bar}baz}
i s t h e s a m e a s
'foo{bar}baz'
N o t e , h o w e v e r , t h a t t h i s d o e s n o t a l w a y s w o r k f o r q u o t i n g P e r l c o d e :
$s = q{ if($x eq "}") ... }; # WRONG
i s a s y n t a x e r r o r . T h e T e x t : : B a l
a n c e d
m o d u l e ( s t a n d a r d a s o f v 5 . 8 , a n d f r o m C P A N b e f o r e t h e n ) i s a b l e t o d o t h i s p r o p e r l y .
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 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.
T h e f o l l o w i n g e s c a p e s e q u e n c e s a r e a v a i l a b l e i n c o n s t r u c t s t h a t i n t e r p o l a t e , a n d i n t r a n s l i t e r a t i o n s :
Sequence Note Description
\t tab (HT, TAB)
\n newline (NL )
\r return (CR )
\f form feed (FF )
\b backspace (BS )
\a alarm (bell) (BEL)
\e escape (ESC)
\x{263A} [1,8] hex char (example: SMILEY)
\x1b [2,8] restricted range hex char (example: ESC)
\N{name} [3 ] named Unicode character or character sequence
\N{U+263D} [4,8] Unicode character (example: FIRST QUARTER MOON)
\c[ [5 ] control char (example: chr(27 ))
\o{23072} [6,8] octal char (example: SMILEY)
\033 [7,8] restricted range octal char (example: ESC)
# [ 1 ]
T h e r e s u l t i s t h e c h a r a c t e r s p e c i f i e d b y t h e h e x a d e c i m a l n u m b e r b e t w e e n t h e b r a c e s . S e e " [ 8 ] " b e l o w f o r d e t a i l s o n w h i c h c h a r a c t e r .
O n l y h e x a d e c i m a l d i g i t s a r e v a l i d b e t w e e n t h e b r a c e s . I f a n i n v a l i d c h a r a c t e r i s e n c o u n t e r e d , a w a r n i n g w i l l b e i s s u e d a n d t h e i n v a l i d c h a r a c t e r a n d a l l s u b s e q u e n t c h a r a c t e r s ( v a l i d o r i n v a l i d ) w i t h i n t h e b r a c e s w i l l b e d i s c a r d e d .
I f t h e r e a r e n o v a l i d d i g i t s b e t w e e n t h e b r a c e s , t h e g e n e r a t e d c h a r a c t e r i s t h e N U L L c h a r a c t e r ( \ x { 0 0 }
) . H o w e v e r , a n e x p l i c i t e m p t y b r a c e ( \ x { }
) w i l l n o t c a u s e a w a r n i n g ( c u r r e n t l y ) .
# [ 2 ]
T h e r e s u l t i s t h e c h a r a c t e r s p e c i f i e d b y t h e h e x a d e c i m a l n u m b e r i n t h e r a n g e 0 x 0 0 t o 0 x F F . S e e " [ 8 ] " b e l o w f o r d e t a i l s o n w h i c h c h a r a c t e r .
O n l y h e x a d e c i m a l d i g i t s a r e v a l i d f o l l o w i n g \ x
. W h e n \ x
i s f o l l o w e d b y f e w e r t h a n t w o v a l i d d i g i t s , a n y v a l i d d i g i t s w i l l b e z e r o - p a d d e d . T h i s m e a n s t h a t \
x 7
w i l l b e i n t e r p r e t e d a s \ x 0 7
, a n d a l o n e " \ x "
w i l l b e i n t e r p r e t e d a s \ x 0 0
. E x c e p t a t t h e e n d o f a s t r i n g , h a v i n g f e w e r t h a n t w o v a l i d d i g i t s w i l l r e s u l t i n a w a r n i n g . N o t e t h a t a l t h o u g h t h e w a r n i n g s a y s t h e i l l e g a l c h a r a c t e r i s i g n o r e d , i t i s o n l y i g n o r e d a s p a r t o f t h e e s c a p e a n d w i l l s t i l l b e u s e d a s t h e s u b s e q u e n t c h a r a c t e r i n t h e s t r i n g . F o r e x a m p l e :
Original Result Warns?
"\x7" "\x07" no
"\x" "\x00" no
"\x7q" "\x07q" yes
"\xq" "\x00q" yes
# [ 3 ]
T h e r e s u l t i s t h e U n i c o d e c h a r a c t e r o r c h a r a c t e r s e q u e n c e g i v e n b y n a m e . S e e c h a r n a m e s .
# [ 4 ]
\ N { U + h e x a d e c i m a l n u m b e r }
m e a n s t h e U n i c o d e c h a r a c t e r w h o s e U n i c o d e c o d e p o i n t i s h e x a d e c i m a l n u m b e r .
# [ 5 ]
T h e c h a r a c t e r f o l l o w i n g \ c
i s m a p p e d t o s o m e o t h e r c h a r a c t e r a s s h o w n i n t h e t a b l e :
Sequence Value
\c@ chr(0)
\cA chr(1 )
\ca chr(1 )
\cB chr(2 )
\cb chr(2 )
...
\cZ chr(26 )
\cz chr(26 )
\c[ chr(27 )
# See below for chr(28 )
\c] chr(29 )
\c^ chr(30 )
\c_ chr(31 )
\c? chr(127) # (on ASCII platforms; see below for link to
# EBCDIC discussion)
I n o t h e r w o r d s , i t ' s t h e c h a r a c t e r w h o s e c o d e p o i n t h a s h a d 6 4 x o r ' d w i t h i t s u p p e r c a s e . \ c ?
i s D E L E T E o n A S C I I p l a t f o r m s b e c a u s e o r d ( " ? " ) ^ 6 4
i s 1 2 7 , a n d \ c @
i s N U L L b e c a u s e t h e o r d o f " @ "
i s 6 4 , s o x o r ' i n g 6 4 i t s e l f p r o d u c e s 0 .
A l s o , \ c \ X
y i e l d s c h r ( 28 ) . " X "
f o r a n y X , b u t c a n n o t c o m e a t t h e e n d o f a s t r i n g , b e c a u s e t h e b a c k s l a s h w o u l d b e p a r s e d a s e s c a p i n g t h e e n d q u o t e .
O n A S C I I p l a t f o r m s , t h e r e s u l t i n g c h a r a c t e r s f r o m t h e l i s t a b o v e a r e t h e c o m p l e t e s e t o f A S C I I c o n t r o l s . T h i s i s n ' t t h e c a s e o n E B C D I C p l a t f o r m s ; s e e " O P E R A T O R D I F F E R E N C E S " i n p e r l e b c d i c f o r a f u l l d i s c u s s i o n o f t h e d i f f e r e n c e s b e t w e e n t h e s e f o r A S C I I v e r s u s E B C D I C p l a t f o r m s .
U s e o f a n y o t h e r c h a r a c t e r f o l l o w i n g t h e " c "
b e s i d e s t h o s e l i s t e d a b o v e i s d i s c o u r a g e d , a n d a s o f P e r l v 5 . 2 0 , t h e o n l y c h a r a c t e r s a c t u a l l y a l l o w e d a r e t h e p r i n t a b l e A S C I I o n e s , m i n u s t h e l e f t b r a c e " { "
. W h a t h a p p e n s f o r a n y o f t h e a l l o w e d o t h e r c h a r a c t e r s i s t h a t t h e v a l u e i s d e r i v e d b y x o r ' i n g w i t h t h e s e v e n t h b i t , w h i c h i s 6 4 , a n d a w a r n i n g r a i s e d i f e n a b l e d . U s i n g t h e n o n - a l l o w e d c h a r a c t e r s g e n e r a t e s a f a t a l e r r o r .
T o g e t p l a t f o r m i n d e p e n d e n t c o n t r o l s , y o u c a n u s e \ N { . . . }
.
# [ 6 ]
T h e r e s u l t i s t h e c h a r a c t e r s p e c i f i e d b y t h e o c t a l n u m b e r b e t w e e n t h e b r a c e s . S e e " [ 8 ] " b e l o w f o r d e t a i l s o n w h i c h c h a r a c t e r .
I f a c h a r a c t e r t h a t i s n ' t a n o c t a l d i g i t i s e n c o u n t e r e d , a w a r n i n g i s r a i s e d , a n d t h e v a l u e i s b a s e d o n t h e o c t a l d i g i t s b e f o r e i t , d i s c a r d i n g i t a n d a l l f o l l o w i n g c h a r a c t e r s u p t o t h e c l o s i n g b r a c e . I t i s a f a t a l e r r o r i f t h e r e a r e n o o c t a l d i g i t s a t a l l .
# [ 7 ]
T h e r e s u l t i s t h e c h a r a c t e r s p e c i f i e d b y t h e t h r e e - d i g i t o c t a l n u m b e r i n t h e r a n g e 0 0 0 t o 7 7 7 ( b u t b e s t t o n o t u s e a b o v e 0 7 7 , s e e n e x t p a r a g r a p h ) . S e e " [ 8 ] " b e l o w f o r d e t a i l s o n w h i c h c h a r a c t e r .
S o m e c o n t e x t s a l l o w 2 o r e v e n 1 d i g i t , b u t a n y u s a g e w i t h o u t e x a c t l y t h r e e d i g i t s , t h e f i r s t b e i n g a z e r o , m a y g i v e u n i n t e n d e d r e s u l t s . ( F o r e x a m p l e , i n a r e g u l a r e x p r e s s i o n i t m a y b e c o n f u s e d w i t h a b a c k r e f e r e n c e ; s e e " O c t a l e s c a p e s " i n p e r l r e b a c k s l a s h . ) S t a r t i n g i n P e r l 5 . 1 4 , y o u m a y u s e \ o { }
i n s t e a d , w h i c h a v o i d s a l l t h e s e p r o b l e m s . O t h e r w i s e , i t i s b e s t t o u s e t h i s c o n s t r u c t o n l y f o r o r d i n a l s \ 0 7
7
a n d b e l o w , r e m e m b e r i n g t o p a d t o t h e l e f t w i t h z e r o s t o m a k e t h r e e d i g i t s . F o r l a r g e r o r d i n a l s , e i t h e r u s e \ o { }
, o r c o n v e r t t o s o m e t h i n g e l s e , s u c h a s t o h e x a n d u s e \ N { U + }
( w h i c h i s p o r t a b l e b e t w e e n p l a t f o r m s w i t h d i f f e r e n t c h a r a c t e r s e t s ) o r \ x
{ }
i n s t e a d .
# [ 8 ]
S e v e r a l c o n s t r u c t s a b o v e s p e c i f y a c h a r a c t e r b y a n u m b e r . T h a t n u m b e r g i v e s t h e c h a r a c t e r ' s p o s i t i o n i n t h e c h a r a c t e r s e t e n c o d i n g ( i n d e x e d f r o m 0 ) . T h i s i s c a l l e d s y n o n y m o u s l y i t s o r d i n a l , c o d e p o s i t i o n , o r c o d e p o i n t . P e r l w o r k s o n p l a t f o r m s t h a t h a v e a n a t i v e e n c o d i n g c u r r e n t l y o f e i t h e r A S C I I / L a t i n 1 o r E B C D I C , e a c h o f w h i c h a l l o w s p e c i f i c a t i o n o f 2 5 6 c h a r a c t e r s . I n g e n e r a l , i f t h e n u m b e r i s 2 5 5 ( 0 x F F , 0 3 7 7 ) o r b e l o w , P e r l i n t e r p r e t s t h i s i n t h e p l a t f o r m ' s n a t i v e e n c o d i n g . I f t h e n u m b e r i s 2 5 6 ( 0 x 1 0 0 , 0 4 0 0 ) o r a b o v e , P e r l i n t e r p r e t s i t a s a U n i c o d e c o d e p o i n t a n d t h e r e s u l t i s t h e c o r r e s p o n d i n g U n i c o d e c h a r a c t e r . F o r e x a m p l e \ x { 5 0 }
a n d \ o { 1
2 0 }
b o t h a r e t h e n u m b e r 8 0 i n d e c i m a l , w h i c h i s l e s s t h a n 2 5 6 , s o t h e n u m b e r i s i n t e r p r e t e d i n t h e n a t i v e c h a r a c t e r s e t e n c o d i n g . I n A S C I I t h e c h a r a c t e r i n t h e 8 0 t h p o s i t i o n ( i n d e x e d f r o m 0 ) i s t h e l e t t e r " P "
, a n d i n E B C D I C i t i s t h e a m p e r s a n d s y m b o l " & "
. \ x { 1 0 0 }
a n d \ o { 4 0 0 }
a r e b o t h 2 5 6 i n d e c i m a l , s o t h e n u m b e r i s i n t e r p r e t e d a s a U n i c o d e c o d e p o i n t n o m a t t e r w h a t t h e n a t i v e e n c o d i n g i s . T h e n a m e o f t h e c h a r a c t e r i n t h e 2 5 6 t h p o s i t i o n ( i n d e x e d b y 0 ) i n U n i c o d e i s L A T I N C A P I T A L L E T T E R A W I T
H M A C R O N
.
T h e r e a r e a c o u p l e o f e x c e p t i o n s t o t h e a b o v e r u l e . \ N { U + h e x n
u m b e r }
i s a l w a y s i n t e r p r e t e d a s a U n i c o d e c o d e p o i n t , s o t h a t \ N { U + 0 0 5 0 }
is " P "
e v e n o n E B C D I C p l a t f o r m s . A n d i f u s e e n c o d i n g
i s i n e f f e c t , t h e n u m b e r i s c o n s i d e r e d t o b e i n t h a t e n c o d i n g , a n d i s t r a n s l a t e d f r o m t h a t i n t o t h e p l a t f o r m ' s n a t i v e e n c o d i n g i f t h e r e i s a c o r r e s p o n d i n g n a t i v e c h a r a c t e r ; o t h e r w i s e t o U n i c o d e .
N O T E : U n l i k e C a n d o t h e r l a n g u a g e s , P e r l h a s n o \ v
e s c a p e s e q u e n c e f o r t h e v e r t i c a l t a b ( V T , w h i c h i s 1 1 i n b o t h A S C I I a n d E B C D I C ) , b u t y o u m a y u s e \ N { V T }
, \ c k
, \ N { U + 0 b }
, o r \ x 0 b
. ( \ v
d o e s h a v e m e a n i n g i n r e g u l a r e x p r e s s i o n p a t t e r n s i n P e r l , s e e p e r l r e . )
T h e f o l l o w i n g e s c a p e s e q u e n c e s a r e a v a i l a b l e i n c o n s t r u c t s t h a t i n t e r p o l a t e , b u t n o t i n t r a n s l i t e r a t i o n s .
\l lowercase next character only
\u titlecase (not uppercase!) next character only
\L lowercase all characters till \E or end of string
\U uppercase all characters till \E or end of string
\F foldcase all characters till \E or end of string
\Q quote (disable) pattern metacharacters till \E or
end of string
\E end either case modification or quoted section
(whichever was last seen)
S e e " q u o t e m e t a " i n p e r l f u n c f o r t h e e x a c t d e f i n i t i o n o f c h a r a c t e r s t h a t a r e q u o t e d b y \ Q
.
\ L
, \ U
, \ F
, a n d \ Q
c a n s t a c k , i n w h i c h c a s e y o u n e e d o n e \ E
f o r e a c h . F o r e x a m p l e :
say"This \Qquoting \ubusiness \Uhere isn't quite\E done yet,\E is it?";
This quoting\ Business\ HERE\ ISN\'T\ QUITE\ done\ yet\, is it?
I f a u s e l o c a l e
f o r m t h a t i n c l u d e s L C _ C T Y P E
i s i n e f f e c t ( s e e p e r l l o c a l e ) , 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 . I f U n i c o d e ( f o r e x a m p l e , \ N { }
o r c o d e p o i n t s o f 0 x 1 0 0 o r b e y o n d ) i s b e i n g u s e d , 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 a s d e f i n e d b y U n i c o d e . T h a t m e a n s t h a t c a s e - m a p p i n g a s i n g l e c h a r a c t e r c a n s o m e t i m e s p r o d u c e a s e q u e n c e o f s e v e r a l c h a r a c t e r s . U n d e r u s e l o c a l e
, \ F
p r o d u c e s t h e s a m e r e s u l t s a s \ L
f o r a l l l o c a l e s b u t a U T F - 8 o n e , w h e r e i t i n s t e a d u s e s t h e U n i c o d e d e f i n i t i o n .
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 o n l y 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 t h e a n c i e n t M a c s ( p r e - M a c O S X ) o f y e s t e r y e a r , t h e s e u s e d t o b 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 a l i n e t e r m i n a t o r , p r i n t i n g " \ n "
m i g h t 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 5 \ 0 1 2 "
or " \ c M
\ c J "
) 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 .
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 e , 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 . S u b s c r i p t e d v a r i a b l e s s u c h a s $ a [ 3 ]
or $ h
r e f - > { k e y } [ 0 ]
a r e a l s o i n t e r p o l a t e d , a s a r e a r r a y a n d h a s h s l i c e s . B u t m e t h o d c a l l s s u c h a s $ o b j
- > m e t h
a r e n o t .
I n t e r p o l a t i n g a n a r r a y o r s l i c e i n t e r p o l a t e s t h e e l e m e n t s i n o r d e r , s e p a r a t e d b y t h e v a l u e o f $ "
, s o i s e q u i v a l e n t t o i n t e r p o l a t i n g j o i n $ " , @ a r r a y
. " P u n c t u a t i o n " a r r a y s s u c h a s @ *
a r e u s u a l l y i n t e r p o l a t e d o n l y i f t h e n a m e i s e n c l o s e d i n b r a c e s @ { * }
, b u t t h e a r r a y s @ _
, @ +
, a n d @ -
a r e i n t e r p o l a t e d e v e n w i t h o u t b r a c e s .
F o r d o u b l e - q u o t e d s t r i n g s , t h e q u o t i n g f r o m \ Q
i s a p p l i e d a f t e r i n t e r p o l a t i o n a n d e s c a p e s a r e p r o c e s s e d .
"abc\Qfoo\tbar$s\Exyz"
i s e q u i v a l e n t t o
"abc" . quotemeta("foo\tbar$s") . "xyz"
F o r t h e p a t t e r n o f r e g e x o p e r a t o r s ( q r / /
, m / /
a n d s / / /
) , t h e q u o t i n g f r o m \ Q
i s a p p l i e d a f t e r i n t e r p o l a t i o n i s p r o c e s s e d , b u t b e f o r e e s c a p e s a r e p r o c e s s e d . T h i s a l l o w s t h e p a t t e r n t o m a t c h l i t e r a l l y ( e x c e p t f o r $
a n d @
) . F o r e x a m p l e , t h e f o l l o w i n g m a t c h e s :
'\s\t' =~ /\Q\s\t/
B e c a u s e $
or @
t r i g g e r i n t e r p o l a t i o n , y o u ' l l n e e d t o u s e s o m e t h i n g l i k e / \ Q u s e r \ E \ @ \ Q h o s t /
t o m a t c h t h e m l i t e r a l l y .
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 b e h a v i o r d e s c r i b e d a b o v e , P e r l d o e s n o t e x p a n d 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 .
# q r / S T R I N G / m s i x p o d u a l n
T h i s o p e r a t o r q u o t e s ( a n d p o s s i b l y c o m p i l e s ) i t s S T R I N G a s a r e g u l a r e x p r e s s i o n . S T R I N G i s i n t e r p o l a t e d t h e s a m e w a y a s P A T T E R N in m / P A T T E R N /
. I f " ' "
i s u s e d a s t h e d e l i m i t e r , n o i n t e r p o l a t i o n i s d o n e . R e t u r n s a P e r l v a l u e w h i c h m a y b e u s e d i n s t e a d o f t h e c o r r e s p o n d i n g / S T R I N G / m s i x
p o d u a l n
e x p r e s s i o n . T h e r e t u r n e d v a l u e i s a n o r m a l i z e d v e r s i o n o f t h e o r i g i n a l p a t t e r n . I t m a g i c a l l y d i f f e r s f r o m a s t r i n g c o n t a i n i n g t h e s a m e c h a r a c t e r s : r e f ( q r / x / )
r e t u r n s " R e g e x p " ; h o w e v e r , d e r e f e r e n c i n g i t i s n o t w e l l d e f i n e d ( y o u c u r r e n t l y g e t t h e n o r m a l i z e d v e r s i o n o f t h e o r i g i n a l p a t t e r n , b u t t h i s m a y c h a n g e ) .
F o r e x a m p l e ,
$rex = qr/my.STRING/is;
print $rex; # prints (?si-xm:my.STRING)
s/$rex/foo/;
i s e q u i v a l e n t t o
s/my.STRING/foo/is;
T h e r e s u l t m a y b e u s e d a s a s u b 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
$string =~ /$re/; # or this way
S i n c e P e r l m a y c o m p i l e t h e p a t t e r n a t t h e m o m e n t o f e x e c u t i o n o f t h e q r ( )
o p e r a t o r , u s i n g q r ( )
m a y h a v e s p e e d a d v a n t a g e s i n s o m e s i t u a t i o n s , n o t a b l y i f t h e r e s u l t o f q r ( )
i s u s e d s t a n d a l o n e :
sub match {
my $patterns = shift;
my @compiled = map qr/$_/i, @$patterns;
grep {
my $success = 0;
foreach my $pat (@compiled) {
$success = 1, last if /$pat/;
}
$success;
} @_;
}
P r e c o m p i l a t i o n o f t h e p a t t e r n 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 t t h e m o m e n t o f q r ( )
a v o i d s t h e n e e d t o r e c o m p i l e t h e p a t t e r n e v e r y t i m e a m a t c h / $ p a t /
i s a t t e m p t e d . ( P e r l h a s m a n y o t h e r i n t e r n a l o p t i m i z a t i o n s , b u t n o n e w o u l d b e t r i g g e r e d i n t h e a b o v e e x a m p l e i f w e d i d n o t u s e q r
( )
o p e r a t o r . )
O p t i o n s ( s p e c i f i e d b y t h e f o l l o w i n g m o d i f i e r s ) a r e :
m Treat string as multiple lines.
s Treat string as single line. (Make . match a newline)
i Do case-insensitive pattern matching.
x Use extended regular expressions.
p When matching preserve a copy of the matched string so
that ${^PREMATCH}, ${^MATCH}, ${^POSTMATCH} will be
defined (ignored starting in v5.20) as these are always
defined starting in that relese
o Compile pattern only once.
a ASCII-restrict: Use ASCII for \d, \s, \w; specifying two
a's further restricts things to that that no ASCII
character will match a non-ASCII one under /i.
l Use the current run-time locale's rules.
u Use Unicode rules.
d Use Unicode or native charset, as in 5.12 and earlier.
n Non-capture mode. Don't let () fill in $1, $2, etc...
I f a p r e c o m p i l e d p a t t e r n i s e m b e d d e d i n a l a r g e r p a t t e r n t h e n t h e e f f e c t o f " m s i x p l u a d n "
w i l l b e p r o p a g a t e d a p p r o p r i a t e l y . T h e e f f e c t t h a t t h e / o
m o d i f i e r h a s i s n o t p r o p a g a t e d , b e i n g r e s t r i c t e d t o t h o s e p a t t e r n s e x p l i c i t l y u s i n g i t .
T h e l a s t f o u r m o d i f i e r s l i s t e d a b o v e , a d d e d i n P e r l 5 . 1 4 , c o n t r o l t h e c h a r a c t e r s e t r u l e s , b u t / a
i s t h e o n l y o n e y o u a r e l i k e l y t o w a n t t o s p e c i f y e x p l i c i t l y ; t h e o t h e r t h r e e a r e s e l e c t e d a u t o m a t i c a l l y b y v a r i o u s p r a g m a s .
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 . I n p a r t i c u l a r , a l l m o d i f i e r s e x c e p t t h e l a r g e l y o b s o l e t e / o
a r e f u r t h e r e x p l a i n e d i n " M o d i f i e r s " i n p e r l r e . / o
i s d e s c r i b e d i n t h e n e x t s e c t i o n .
# m / P A T T E R N / m s i x p o d u a l n g c
# / P A T T E R N / m s i x p o d u a l n g c
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 i f i t s u c c e e d s , f a l s e i f i t f a i l s . 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 .
O p t i o n s a r e a s d e s c r i b e d i n q r /
/
a b o v e ; i n a d d i t i o n , t h e f o l l o w i n g m a t c h p r o c e s s m o d i f i e r s a r e a v a i l a b l e :
g Match globally, i.e., find all occurrences.
c Do not reset search position on a failed match when /g is
in effect.
If " / "
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 - w h i t e s p a c e ( A S C I I ) c h a r a c t e r s a s d e l i m i t e r s . 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 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 a m a t c h - o n l y - o n c e r u l e a p p l i e s , d e s c r i b e d i n m ? P A T T E R N ?
b e l o w . I f " ' "
( s i n g l e q u o t e ) i s t h e d e l i m i t e r , 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 o n t h e P A T T E R N . W h e n u s i n g a d e l i m i t e r c h a r a c t e r v a l i d i n a n i d e n t i f i e r , w h i t e s p a c e i s r e q u i r e d a f t e r t h e m
.
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 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 , e x c e p t f o r w h e n t h e d e l i m i t e r i s a s i n g l e q u o t e . ( N o t e t h a t $ (
, $ )
, a n d $ |
a r e n o t 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 . ) P e r l w i l l n o t r e c o m p i l e t h e p a t t e r n u n l e s s a n i n t e r p o l a t e d v a r i a b l e t h a t i t c o n t a i n s c h a n g e s . Y o u c a n f o r c e P e r l t o s k i p t h e t e s t a n d n e v e r r e c o m p i l e b y a d d i n g a / o
( w h i c h s t a n d s f o r " o n c e " ) 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 . O n c e u p o n a t i m e , P e r l w o u l d r e c o m p i l e r e g u l a r e x p r e s s i o n s u n n e c e s s a r i l y , a n d t h i s m o d i f i e r w a s u s e f u l t o t e l l i t n o t t o d o s o , i n t h e i n t e r e s t s o f s p e e d . B u t n o w , t h e o n l y r e a s o n s t o u s e / o
a r e o n e o f :
T h e v a r i a b l e s a r e t h o u s a n d s o f c h a r a c t e r s l o n g a n d y o u k n o w t h a t t h e y d o n ' t c h a n g e , a n d y o u n e e d t o w r i n g o u t t h e l a s t l i t t l e b i t o f s p e e d b y h a v i n g P e r l s k i p t e s t i n g f o r t h a t . ( T h e r e i s a m a i n t e n a n c e p e n a l t y f o r d o i n g t h i s , a s 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 d o 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 . )
y o u w a n t t h e p a t t e r n t o u s e t h e i n i t i a l v a l u e s o f t h e v a r i a b l e s r e g a r d l e s s o f w h e t h e r t h e y c h a n g e o r n o t . ( B u t t h e r e a r e s a n e r w a y s o f a c c o m p l i s h i n g t h i s t h a n u s i n g / o
. )
I f t h e p a t t e r n c o n t a i n s e m b e d d e d c o d e , s u c h a s
use re 'eval';
$code = 'foo(?{ $x })';
/$code/
t h e n p e r l w i l l r e c o m p i l e e a c h t i m e , e v e n t h o u g h t h e p a t t e r n s t r i n g h a s n ' t c h a n g e d , t o e n s u r e t h a t t h e c u r r e n t v a l u e o f $ x
i s s e e n e a c h t i m e . U s e / o
i f y o u w a n t t o a v o i d t h i s .
T h e b o t t o m l i n e i s t h a t u s i n g /
o
i s a l m o s t n e v e r a g o o d i d e a .
# T h e e m p t y p a t t e r 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 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 n t h i s c a s e , o n l y t h e g
a n d c
f l a g s o n t h e e m p t y p a t t e r n a r e h o n o r e d ; t h e o t h e r f l a g s a r e t a k e n f r o m t h e o r i g i n a l p a t t e r n . I f n o m a t c h h a s p r e v i o u s l y s u c c e e d e d , t h i s w i l l ( s i l e n t l y ) a c t i n s t e a d a s a g e n u i n e e m p t y p a t t e r n ( w h i c h w i l l a l w a y s m a t c h ) .
N o t e t h a t i t ' s p o s s i b l e t o c o n f u s e P e r l i n t o t h i n k i n g / /
( t h e e m p t y r e g e x ) i s r e a l l y / /
( t h e d e f i n e d - o r o p e r a t o r ) . P e r l i s u s u a l l y p r e t t y g o o d a b o u t t h i s , b u t s o m e p a t h o l o g i c a l c a s e s m i g h t t r i g g e r t h i s , s u c h a s $ x / / /
( i s t h a t ( $ x ) / ( / / )
or $ x / / /
? ) a n d p r i n t $ f h / /
( p r i n t $ f h ( / /
or p r i n t ( $ f h / /
? ) . I n a l l o f t h e s e e x a m p l e s , P e r l w i l l a s s u m e y o u m e a n t d e f i n e d - o r . I f y o u m e a n t t h e e m p t y r e g e x , j u s t u s e p a r e n t h e s e s o r s p a c e s t o d i s a m b i g u a t e , o r e v e n p r e f i x t h e e m p t y r e g e x w i t h a n m
( so / /
b e c o m e s m / /
) .
# M a t c h i n g i n l i s t c o n t e x t
I f t h e / g
o p t i o n i s n o t u s e d , m
/ /
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 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 , t h a t i s , ( $ 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 ) . 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")
|| die "can't access /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 (no longer needed!)
}
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 ; t h a t i s , 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 t h e s u b s t r i n g s m a t c h e d b y a n y c a p t u r i n g 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 ( f o r e x a m p l e , 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 .
# \ G a s s e r 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 . W i t h o u t t h e / g
m o d i f i e r , t h e \
G
a s s e r t i o n s t i l l a n c h o r s a t p o
s ( )
a s i t w a s a t t h e s t a r t o f t h e o p e r a t i o n ( s e e " p o s " i n p e r l f u n c ) , b u t t h e m a t c h i s o f c o u r s e o n l y a t t e m p t e d o n c e . U s i n g \
G
w i t h o u t / g
o n a t a r g e t s t r i n g t h a t h a s n o t p r e v i o u s l y h a d a / g
m a t c h a p p l i e d t o i t i s t h e s a m e a s u s i n g t h e \ A
a s s e r t i o n t o m a t c h t h e b e g i n n i n g o f t h e s t r i n g . N o t e a l s o t h a t , c u r r e n t l y , \ G
i s o n l y p r o p e r l y s u p p o r t e d w h e n a n c h o r e d a t t h e v e r y b e g i n n i n g o f t h e p a t t e r n .
E x a m p l e s :
# list context
($one,$five,$fifteen) = (`uptime` =~ /(\d+\.\d+)/g);
# scalar context
local $/ = "";
while ($paragraph = <>) {
while ($paragraph =~ /\p{Ll}['")]*[.!?]+['")]*\s/g) {
$sentences++;
}
}
say $sentences;
H e r e ' s a n o t h e r w a y t o c h e c k f o r s e n t e n c e s i n a p a r a g r a p h :
my $sentence_rx = qr{
(?: (?<= ^ ) | (?<= \s ) ) # after start-of-string or
# whitespace
\p{Lu} # capital letter
.*? # a bunch of anything
(?<= \S ) # that ends in non-
# whitespace
(?<! \b [DMS]r ) # but isn't a common abbr.
(?<! \b Mrs )
(?<! \b Sra )
(?<! \b St )
[.?!] # followed by a sentence
# ender
(?= $ | \s ) # in front of end-of-string
# or whitespace
}sx;
local $/ = "";
while (my $paragraph = <>) {
say "NEW PARAGRAPH";
my $count = 0;
while ($paragraph =~ /($sentence_rx)/g) {
printf "\tgot sentence %d: <%s>\n", ++$count, $1;
}
}
H e r e ' s h o w t o u s e m / / g c
w i t h \ 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";
}
print "Final: '$1', pos=",pos,"\n" if /\G(.)/;
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
Final: 'q', pos=8
N o t i c e t h a t t h e f i n a l m a t c h m a t c h e d q
i n s t e a d o f p
, w h i c h a m a t c h w i t h o u t t h e \ G
a n c h o r w o u l d h a v e d o n e . A l s o n o t e t h a t t h e f i n a l m a t c h d i d n o t u p d a t e p o s
. p o s
i s o n l y u p d a t e d o n a / g
m a t c h . I f t h e f i n a l m a t c h d i d i n d e e d m a t c h p
, i t ' s a g o o d b e t t h a t y o u ' r e r u n n i n g a v e r y o l d ( p r e - 5 . 6 . 0 ) v e r s i o n o f P e r l .
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 = URI::URL->new( "http://example.com/" );
die if $url eq "xXx";
EOL
LOOP: {
print(" digits"), redo LOOP if /\G\d+\b[,.;]?\s*/gc;
print(" lowercase"), redo LOOP
if /\G\p{Ll}+\b[,.;]?\s*/gc;
print(" UPPERCASE"), redo LOOP
if /\G\p{Lu}+\b[,.;]?\s*/gc;
print(" Capitalized"), redo LOOP
if /\G\p{Lu}\p{Ll}+\b[,.;]?\s*/gc;
print(" MiXeD"), redo LOOP if /\G\pL+\b[,.;]?\s*/gc;
print(" alphanumeric"), redo LOOP
if /\G[\p{Alpha}\pN]+\b[,.;]?\s*/gc;
print(" line-noise"), redo LOOP if /\G\W+/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 UPPERCASE line-noise UPPERCASE
line-noise lowercase line-noise lowercase line-noise lowercase
lowercase line-noise lowercase lowercase line-noise lowercase
lowercase line-noise MiXeD line-noise. That's all!
# m ? P A T T E R N ? m s i x p o d u a l n g c
# ? P A T T E R N ? m s i x p o d u a l n g c
T h i s i s j u s t l i k e t h e m / 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 m ??
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 (m?^$?) {
# blank line between header and body
}
} continue {
reset if eof; # clear m?? status for next file
}
A n o t h e r e x a m p l e s w i t c h e d t h e f i r s t " l a t i n 1 " e n c o d i n g i t f i n d s t o " u t f 8 " i n a p o d f i l e :
s//utf8/ if m? ^ =encoding \h+ \K latin1 ?x;
T h e m a t c h - o n c e b e h a v i o r i s c o n t r o l l e d b y t h e m a t c h d e l i m i t e r b e i n g ?
; w i t h a n y o t h e r d e l i m i t e r t h i s i s t h e n o r m a l m / /
o p e r a t o r .
I n t h e p a s t , t h e l e a d i n g m
in m ? P A
T T E R N ?
w a s o p t i o n a l , b u t o m i t t i n g i t w o u l d p r o d u c e a d e p r e c a t i o n w a r n i n g . A s o f v 5 . 2 2 . 0 , o m i t t i n g i t p r o d u c e s a s y n t a x e r r o r . I f y o u e n c o u n t e r t h i s c o n s t r u c t i n o l d e r c o d e , y o u c a n j u s t a d d m
.
# s / P A T T E R N / R E P L A C E M E N T / m s i x p o d u a
l n g c e r
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 t h e / r
( n o n - d e s t r u c t i v e ) o p t i o n i s u s e d t h e n i t r u n s t h e s u b s t i t u t i o n o n a c o p y o f t h e s t r i n g a n d i n s t e a d o f r e t u r n i n g t h e n u m b e r o f s u b s t i t u t i o n s , i t r e t u r n s t h e c o p y w h e t h e r o r n o t a s u b s t i t u t i o n o c c u r r e d . T h e o r i g i n a l s t r i n g i s n e v e r c h a n g e d w h e n / r
i s u s e d . T h e c o p y w i l l a l w a y s b e a p l a i n s t r i n g , e v e n i f t h e i n p u t i s a n o b j e c t o r a t i e d v a r i a b l 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 $ _
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 . U n l e s s t h e / r
o p t i o n i s u s e d , t h e s t r i n g s p e c i f i e d 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 ; t h a t i s , s o m e s o r t o f s c a l a r 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 a s i n g l e q u o t e , n o 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 .
O p t i o n s a r e a s w i t h m / /
w i t h t h e a d d i t i o n o f t h e f o l l o w i n g r e p l a c e m e n t s p e c i f i c o p t i o n s :
e Evaluate the right side as an expression.
ee Evaluate the right side as a string then eval the
result.
r Return substitution and leave the original string
untouched.
A n y 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 . A d d s p a c e a f t e r t h e s
w h e n u s i n g a c h a r a c t e r a l l o w e d i n i d e n t i f i 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 ( 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 ) . N o t e t h a t P e r l 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 , f o r e x a m p l e , 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 t r e a 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 u a t 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 . A s e c o n d e
m o d i f i e r 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 e v a l
e d b e f o r e b e i n g r u n a s a P e r l e x p r e s s i o n .
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
($foo = "$bar") =~ s/this/that/; # convert to string,
# copy, then change
$foo = $bar =~ s/this/that/r; # Same as above using /r
$foo = $bar =~ s/this/that/r
=~ s/that/the other/r; # Chained substitutes
# using /r
@foo = map { s/this/that/r } @bar # /r is very useful in
# maps
$count = ($paragraph =~ s/Mister\b/Mr./g); # get change-cnt
$_ = '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
$_ = 'abc123xyz';
$x = s/abc/def/r; # $x is 'def123xyz' and
# $_ remains 'abc123xyz'.
# expand variables in $_, but dynamics only, using
# symbolic dereferencing
s/\$(\w+)/${$1}/g;
# Add one to the value of any numbers in the string
s/(\d+)/1 + $1/eg;
# Titlecase words in the last 30 characters only
substr($str, -30) =~ s/\b(\p{Alpha}+)\b/\u\L$1/g;
# This will expand any embedded scalar variable
# (including lexicals) in $_ : First $1 is interpolated
# to the variable name, and then evaluated
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 whitespace in $_,
# expensively
for ($variable) { # trim whitespace 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 o n l y i n 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 t h a t y o u m i g h t w a n t . 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)(?!\d)/$1,$2/g;
# expand tabs to 8-column spacing
1 while s/\t+/' ' x (length($&)*8 - length($`)%8)/e;
# Q u o t e - L i k e O p e r a t o r s
# 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 /\b(tcl|java|python)\b/i; # :-)
$baz = "\n"; # a one-character string
# 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 , o r u n
d e f
i f t h e c o m m a n d f a i l e d . 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 $ /
or $ I N P U T _ R E C O R D _ S E P A
R A T O R
) , o r a n e m p t y l i s t i f t h e c o m m a n d f a i l e d .
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 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>program.stdout 2>program.stderr");
T h e S T D I N f i l e h a n d l e u s e d b y t h e c o m m a n d i s i n h e r i t e d f r o m P e r l ' s S T D I N . F o r e x a m p l e :
open(SPLAT, "stuff") || die "can't open stuff: $!";
open(STDIN, "<&SPLAT") || die "can't dupe SPLAT: $!";
print STDOUT `sort`;
w i l l p r i n t t h e s o r t e d c o n t e n t s o f t h e f i l e n a m e d " s t u f f " .
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 $$
H o w t h a t 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 ( f o r e x a m p l e , ;
o n m a n y U n i x s h e l l s a n d &
o n t h e W i n d o w s N T c m d
s h e l l ) .
P e r l w i l l a t t e m p t t o f l u s h a l l f i l e s o p e n e d f o r o u t p u t b e f o r e s t a r t i n g t h e c h i l d p r o c e s s , b u t t h i s m a y n o t b e s u p p o r t e d o n s o m e p l a t f o r m s ( s e e p e r l p o r t ) . T o b e s a f e , y o u m a y n e e d t o s e t $ |
( $ A U T O F L U S H
in E n g l i s h
) o r c a l l t h e a u t o f l u s h ( )
m e t h o d o f I O : :
H a n d l e
o n a n y o p e n h a n d l e s .
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 " I / O 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 .
# q w / S T R I N G /
E v a l u a t e s t o 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 c a n b e u n d e r s t o o d a s b e i n g r o u g h l y e q u i v a l e n t t o :
split(" ", q/STRING/);
t h e d i f f e r e n c e s b e i n g t h a t i t g e n e r a t e s a r e a l l i s t a t c o m p i l e t i m e , a n d i n s c a l a r c o n t e x t i t r e t u r n s t h e l a s t e l e m e n t i n t h e l i s t . S o t h i s e x p r e s s i o n :
qw(foo bar baz)
i s s e m a n t i c a l l y e q u i v a l e n t t o t h e l i s t :
"foo", "bar", "baz"
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 s 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 u s e w a r n i n g s
p r a g m a a n d t h e - w s w i t c h ( t h a t i s , t h e $ ^ W
v a r i a b l e ) p r o d u c e s 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 .
# 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 r
# 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 r
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 = ~
or ! ~
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 .
I f t h e / r
( n o n - d e s t r u c t i v e ) o p t i o n i s p r e s e n t , a n e w c o p y o f t h e s t r i n g i s m a d e a n d i t s c h a r a c t e r s t r a n s l i t e r a t e d , a n d t h i s c o p y i s r e t u r n e d n o m a t t e r w h e t h e r i t w a s m o d i f i e d o r n o t : t h e o r i g i n a l s t r i n g i s a l w a y s l e f t u n c h a n g e d . T h e n e w c o p y i s a l w a y s a p l a i n s t r i n g , e v e n i f t h e i n p u t s t r i n g i s a n o b j e c t o r a t i e d v a r i a b l e .
U n l e s s t h e / r
o p t i o n i s u s 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 n o t h e r w o r d s , 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 ; f o r e x a m p l e , t r [ a e i o u y ] [ y
u o i e a ]
or t r ( + \ - * / ) / A B C D /
.
C h a r a c t e r s m a y b e l i t e r a l s o r a n y o f t h e e s c a p e s e q u e n c e s a c c e p t e d i n d o u b l e - q u o t e d s t r i n g s . B u t t h e r e i s n o i n t e r p o l a t i o n , s o " $ "
a n d " @ "
a r e t r e a t e d a s l i t e r a l s . A h y p h e n a t t h e b e g i n n i n g o r e n d , o r p r e c e d e d b y a b a c k s l a s h i s c o n s i d e r e d a l i t e r a l . E s c a p e s e q u e n c e d e t a i l s a r e i n t h e t a b l e n e a r t h e b e g i n n i n g o f t h i s s e c t i o n . I t i s a b u g i n P e r l v 5 . 2 2 t h a t s o m e t h i n g l i k e
tr/\N{U+20}-\N{U+7E}foobar//
d o e s n o t t r e a t t h a t r a n g e a s f u l l y U n i c o d e .
N o t e t h a t tr
d o e s n o t d o r e g u l a r e x p r e s s i o n c h a r a c t e r c l a s s e s s u c h a s \ d
or \ p L
. T h e tr
o p e r a t o r i s n o t e q u i v a l e n t t o t h e t r ( 1 )
u t i l i t y . I f y o u w a n t t o m a p s t r i n g s b e t w e e n l o w e r / u p p e r c a s e s , s e e " l c " i n p e r l f u n c a n d " u c " i n p e r l f u n c , a n d i n g e n e r a l c o n s i d e r u s i n g t h e s
o p e r a t o r i f y o u n e e d r e g u l a r e x p r e s s i o n s . T h e \ U
, \ u
, \ L
, a n d \ l
s t r i n g - i n t e r p o l a t i o n e s c a p e s o n t h e r i g h t s i d e o f a s u b s t i t u t i o n o p e r a t o r w i l l p e r f o r m c o r r e c t c a s e - m a p p i n g s , b u t t r [ a - z ] [ A - Z ]
w i l l n o t ( e x c e p t s o m e t i m e s o n l e g a c y 7 - b i t d a t a ) .
N o t e a l s o t h a t t h e w h o l e r a n g e i d e a i s r a t h e r u n p o r t a b l e b e t w e e n c h a r a c t e r s e t s - - a n d e v e n w i t h i n c h a r a c t e r s e t s t h e y m a y c a u s e r e s u l t s y o u p r o b a b l y d i d n ' t e x p e c t . A s o u n d p r i n c i p l e i s t o u s e o n l y r a n g e s t h a t b e g i n f r o m a n d e n d a t e i t h e r a l p h a b e t s o f e q u a l c a s e ( a - e , A - E ) , o r d i g i t s ( 0 - 4 ) . A n y t h i n g e l s e i s u n s a f e . I f i n d o u b t , s p e l l o u t t h e c h a r a c t e r s e t s i n f u l l .
O p t i o n s :
c Complement the SEARCHLIST.
d Delete found but unreplaced characters.
s Squash duplicate replaced characters.
r Return the modified string and leave the original string
untouched.
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 ASCII
$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/;
$HOST = $host =~ tr/a-z/A-Z/r; # same thing
$HOST = $host =~ tr/a-z/A-Z/r # chained with s///r
=~ s/:/ -p/r;
tr/a-zA-Z/ /cs; # change non-alphas to single space
@stripped = map tr/a-zA-Z/ /csr, @original;
# /r with map
tr [\200-\377]
[\000-\177]; # wickedly 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 .
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 $@;
# < < E O F
A l i n e - o r i e n t e d f o r m o f q u o t i n g i s b a s e d o n t h e s h e l l " h e r e - d o c u m e n t " s y n t a x . F o l l o w i n g a < <
y o u s p e c i f y a s t r i n g t o t e r m i n a t e t h e q u o t e d m a t e r i a l , a n d a l l l i n e s f o l l o w i n g t h e c u r r e n t l i n e d o w n t o t h e t e r m i n a t i n g s t r i n g a r e t h e v a l u e o f t h e i t e m .
T h e t e r m i n a t i n g s t r i n g m a y b e e i t h e r a n i d e n t i f i e r ( a w o r d ) , o r s o m e q u o t e d t e x t . A n u n q u o t e d i d e n t i f i e r w o r k s l i k e d o u b l e q u o t e s . T h e r e m a y n o t b e a s p a c e b e t w e e n t h e < <
a n d t h e i d e n t i f i e r , u n l e s s t h e i d e n t i f i e r i s e x p l i c i t l y q u o t e d . ( I f y o u p u t a s p a c e i t w i l l b e t r e a t e d a s a n u l l i d e n t i f i e r , w h i c h i s v a l i d , a n d m a t c h e s t h e f i r s t e m p t y l i n e . ) T h e t e r m i n a t i n g s t r i n g m u s t a p p e a r b y i t s e l f ( u n q u o t e d a n d w i t h n o s u r r o u n d i n g w h i t e s p a c e ) o n t h e t e r m i n a t i n g l i n e .
I f t h e t e r m i n a t i n g s t r i n g i s q u o t e d , t h e t y p e o f q u o t e s u s e d d e t e r m i n e t h e t r e a t m e n t o f t h e t e x t .
# D o u b l e Q u o t e s
D o u b l e q u o t e s i n d i c a t e t h a t t h e t e x t w i l l b e i n t e r p o l a t e d u s i n g e x a c t l y t h e s a m e r u l e s a s n o r m a l d o u b l e q u o t e d s t r i n g s .
print <<EOF;
The price is $Price.
EOF
print << "EOF"; # same as above
The price is $Price.
EOF
# S i n g l e Q u o t e s
S i n g l e q u o t e s i n d i c a t e t h e t e x t i s t o b e t r e a t e d l i t e r a l l y w i t h n o i n t e r p o l a t i o n o f i t s c o n t e n t . T h i s i s s i m i l a r t o s i n g l e q u o t e d s t r i n g s e x c e p t t h a t b a c k s l a s h e s h a v e n o s p e c i a l m e a n i n g , w i t h \ \
b e i n g t r e a t e d a s t w o b a c k s l a s h e s a n d n o t o n e a s t h e y w o u l d i n e v e r y o t h e r q u o t i n g c o n s t r u c t .
J u s t a s i n t h e s h e l l , a b a c k s l a s h e d b a r e w o r d f o l l o w i n g t h e < <
m e a n s t h e s a m e t h i n g a s a s i n g l e - q u o t e d s t r i n g d o e s :
$cost = <<'VISTA'; # hasta la ...
That'll be $10 please, ma'am.
VISTA
$cost = <<\VISTA; # Same thing!
That'll be $10 please, ma'am.
VISTA
T h i s i s t h e o n l y f o r m o f q u o t i n g i n p e r l w h e r e t h e r e i s n o n e e d t o w o r r y a b o u t e s c a p i n g c o n t e n t , s o m e t h i n g t h a t c o d e g e n e r a t o r s c a n a n d d o m a k e g o o d u s e o f .
# B a c k t i c k s
T h e c o n t e n t o f t h e h e r e d o c i s t r e a t e d j u s t a s i t w o u l d b e i f t h e s t r i n g w e r e e m b e d d e d i n b a c k t i c k s . T h u s t h e c o n t e n t i s i n t e r p o l a t e d a s t h o u g h i t w e r e d o u b l e q u o t e d a n d t h e n e x e c u t e d v i a t h e s h e l l , w i t h t h e r e s u l t s o f t h e e x e c u t i o n r e t u r n e d .
print << `EOC`; # execute command and get results
echo hi there
EOC
I t i s p o s s i b l e t o s t a c k m u l t i p l e h e r e - d o c s i n a r o w :
print <<"foo", <<"bar"; # you can stack them
I said foo.
foo
I said bar.
bar
myfunc(<< "THIS", 23, <<'THAT');
Here's a line
or two.
THIS
and here's another.
THAT
J u s t d o n ' t f o r g e t t h a t y o u h a v e t o p u t a s e m i c o l o n o n t h e e n d t o f i n i s h t h e s t a t e m e n t , a s P e r l d o e s n ' t k n o w y o u ' r e n o t g o i n g t o t r y t o d o t h i s :
print <<ABC
179231
ABC
+ 20;
I f y o u w a n t t o r e m o v e t h e l i n e t e r m i n a t o r f r o m y o u r h e r e - d o c s , u s e c h o m p ( )
.
chomp($string = <<'END');
This is a string.
END
I f y o u w a n t y o u r h e r e - d o c s t o b e i n d e n t e d w i t h t h e r e s t o f t h e c o d e , y o u ' l l n e e d t o r e m o v e l e a d i n g w h i t e s p a c e f r o m e a c h l i n e m a n u a l l y :
($quote = <<'FINIS') =~ s/^\s+//gm;
The Road goes ever on and on,
down from the door where it began.
FINIS
I f y o u u s e a h e r e - d o c w i t h i n a d e l i m i t e d c o n s t r u c t , s u c h a s i n s / / / e g
, t h e q u o t e d m a t e r i a l m u s t s t i l l c o m e o n t h e l i n e f o l l o w i n g t h e < < F O O
m a r k e r , w h i c h m e a n s i t m a y b e i n s i d e t h e d e l i m i t e d c o n s t r u c t :
s/this/<<E . 'that'
the other
E
. 'more '/eg;
I t w o r k s t h i s w a y a s o f P e r l 5 . 1 8 . H i s t o r i c a l l y , i t w a s i n c o n s i s t e n t , a n d y o u w o u l d h a v e t o w r i t e
s/this/<<E . 'that'
. 'more '/eg;
the other
E
o u t s i d e o f s t r i n g e v a l s .
A d d i t i o n a l l y , q u o t i n g r u l e s f o r t h e e n d - o f - s t r i n g i d e n t i f i e r a r e u n r e l a t e d t o P e r l ' s q u o t i n g r u l e s . q ( )
, q q ( )
, a n d t h e l i k e a r e n o t s u p p o r t e d i n p l a c e o f '
'
a n d " "
, a n d t h e o n l y i n t e r p o l a t i o n i s f o r b a c k s l a s h i n g t h e q u o t i n g c h a r a c t e r :
print << "abc\"def";
testing...
abc"def
F i n a l l y , q u o t e d s t r i n g s c a n n o t s p a n m u l t i p l e l i n e s . T h e g e n e r a l r u l e i s t h a t t h e i d e n t i f i e r m u s t b e a s t r i n g l i t e r a l . S t i c k w i t h t h a t , a n d y o u s h o u l d b e s a f e .
# 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 t h a t m i g h t 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 D W I M ( t h a t ' s " D o W h a t I M e a n " ) p r i n c i p l e t o p i c k 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 . 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 p r o g r a m m e r s o f t e n d o n o t s u s p e c t t h e 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 . B u t f r o m t i m e t o t i m e , P e r l ' s n o t i o n s d i f f e r s u b s t a n t i a l l y f r o m w h a t t h e a u t h o r h o n e s t l y m e a n t .
T h i s s e c t i o n h o p e s t o c l a r i f y h o w P e r l h a n d l e s q u o t e d c o n s t r u c t s . A l t h o u g h t h e m o s t c o m m o n r e a s o n t o l e a r n t h i s i s t o u n r a v e l l a b y r i n t h i n e r e g u l a r e x p r e s s i o n s , b e c a u s e t h e i n i t i a l 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 q u o t i n g o p e r a t o r s , t h e y a r e a l l d i s c u s s e d t o g e t h e r .
T h e m o s t i m p o r t a n t P e r l p a r s i n g r u l e i s t h e f i r s t o n e d i s c u s s e d b e l o w : w h e n p r o c e s s i n g a q u o t e d c o n s t r u c t , P e r l f i r s t f i n d s t h e e n d o f t h a t c o n s t r u c t , t h e n i n t e r p r e t s i t s c o n t e n t s . I f y o u u n d e r s t a n d t h i s r u l e , y o u m a y s k i p t h e r e s t o f t h i s s e c t i o n o n t h e f i r s t r e a d i n g . T h e o t h e r r u l e s a r e l i k e l y t o c o n t r a d i c t t h e u s e r ' s e x p e c t a t i o n s m u c h l e s s f r e q u e n t l y t h a n t h i s f i r s t o n e .
S o m 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 b e c a u s e t h e i r 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 i n d i v i d u a l l y . 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 s o f p a s s e s , f r o m o n e t o f o u r , b u t t h e s e p a s s e s 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
T h e 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 . T h i s r e s u l t s i n s a v i n g t o a s a f e l o c a t i o n a c o p y o f 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 a n d e n d i n g d e l i m i t e r s ) , n o r m a l i z e d a s n e c e s s a r y t o a v o i d n e e d i n g t o k n o w w h a t t h e o r i g i n a l d e l i m i t e r s w e r e .
I f t h e c o n s t r u c t i s a h e r e - d o c , t h e e n d i n g d e l i m i t e r i s a l i n e t h a t h a s a t e r m i n a t i n g s t r i n g a s t h e c o n t e n t . T h e r e f o r e < < E O F
i s t e r m i n a t e d b y E O F
i m m e d i a t e l y f o l l o w e d b y " \ n "
a n d s t a r t i n g f r o m t h e f i r s t c o l u m n o f t h e t e r m i n a t i n g l i n e . W h e n s e a r c h i n g f o r t h e t e r m i n a t i n g l i n e o f a h e r e - d o c , n o t h i n g i s s k i p p e d . I n o t h e r w o r d s , l i n e s a f t e r t h e h e r e - d o c s y n t a x a r e c o m p a r e d w i t h t h e t e r m i n a t i n g s t r i n g l i n e b y l i n e .
F o r t h e c o n s t r u c t s e x c e p t h e r e - d o c s , s i n g l e c h a r a c t e r s a r e u s e d a s 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 s . I f t h e s t a r t i n g d e l i m i t e r i s a n o p e n i n g p u n c t u a t i o n ( t h a t i s (
, [
, {
, o r <
) , t h e e n d i n g d e l i m i t e r i s t h e c o r r e s p o n d i n g c l o s i n g p u n c t u a t i o n ( t h a t i s )
, ]
, }
, o r >
) . I f t h e s t a r t i n g d e l i m i t e r i s a n u n p a i r e d c h a r a c t e r l i k e /
o r a c l o s i n g p u n c t u a t i o n , t h e e n d i n g d e l i m i t e r i s t h e s a m e a s t h e s t a r t i n g d e l i m i t e r . T h e r e f o r e a /
t e r m i n a t e s a q
q / /
c o n s t r u c t , w h i l e a ]
t e r m i n a t e s b o t h q q [ ]
a n d qq ] ]
c o n s t r u c t s .
W h e n s e a r c h i n g f o r s i n g l e - c h a r a c t e r d e l i m i t e r s , e s c a p e d d e l i m i t e r s a n d \ \
a r e s k i p p e d . F o r e x a m p l e , w h i l e s e a r c h i n g f o r t e r m i n a t i n g /
, c o m b i n a t i o n s o f \ \
a n d \ /
a r e s k i p p e d . I f t h e d e l i m i t e r s a r e b r a c k e t i n g , n e s t e d p a i r s a r e a l s o s k i p p e d . F o r e x a m p l e , w h i l e s e a r c h i n g f o r a c l o s i n g ]
p a i r e d w i t h t h e o p e n i n g [
, c o m b i n a t i o n s o f \ \
, \ ]
, a n d \ [
a r e a l l s k i p p e d , a n d n e s t e d [
a n d ]
a r e s k i p p e d a s w e l l . H o w e v e r , w h e n b a c k s l a s h e s a r e u s e d a s t h e d e l i m i t e r s ( l i k e q q \ \
a n d t r \ \ \
) , n o t h i n g i s s k i p p e d . D u r i n g t h e s e a r c h f o r t h e e n d , b a c k s l a s h e s t h a t e s c a p e d e l i m i t e r s o r o t h e r b a c k s l a s h e s a r e r e m o v e d ( e x a c t l y s p e a k i n g , t h e y a r e n o t c o p i e d t o t h e s a f e l o c a t i o n ) .
F o r c o n s t r u c t s w i t h t h r e e - p a r t d e l i m i t e r s ( s / / /
, y / / /
, a n d t r /
/ /
) , 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 . I f t h e f i r s t d e l i m i t e r i s n o t a n o p e n i n g p u n c t u a t i o n , t h e t h r e e d e l i m i t e r s m u s t b e t h e s a m e , s u c h a s s !!!
a n d tr ) ) )
, i n w h i c h c a s e t h e s e c o n d d e l i m i t e r t e r m i n a t e s t h e l e f t p a r t a n d s t a r t s t h e r i g h t p a r t a t o n c e . I f t h e l e f t p a r t i s d e l i m i t e d b y b r a c k e t i n g p u n c t u a t i o n ( t h a t i s ( )
, [ ]
, { }
, o r < >
) , t h e r i g h t p a r t n e e d s a n o t h e r p a i r o f d e l i m i t e r s s u c h a s s ( ) { }
a n d t r [ ]
/ /
. I n t h e s e c a s e s , w h i t e s p a c e a n d c o m m e n t s a r e a l l o w e d b e t w e e n t h e t w o p a r t s , a l t h o u g h t h e c o m m e n t m u s t f o l l o w a t l e a s t o n e w h i t e s p a c e c h a r a c t e r ; o t h e r w i s e a c h a r a c t e r e x p e c t e d a s t h e s t a r t o f t h e c o m m e n t m a y b e r e g a r d e d a s t h e s t a r t i n g d e l i m i t e r o f t h e r i g h t p a r t .
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 s o f t h e c o n s t r u c t . T h u s :
"$hash{"$foo/$bar"}"
o r :
m/
bar # 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 . T h e q u o t e d p a r t e n d s o n t h e f i r s t "
a n d /
, a n d t h e r e s t h a p p e n s t o b e a s y n t a x e r r o r . B e c a u s e t h e s l a s h t h a t 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 e e x a m p l e a b o v e i s n o t m / / x
, b u t r a t h e r m / /
w i t h n o / x
m o d i f i e r . S o t h e e m b e d d e d #
i s i n t e r p r e t e d a s a l i t e r a l #
.
A l s o n o a t t e n t i o n i s p a i d t o \ c
\
( m u l t i c h a r c o n t r o l c h a r s y n t a x ) d u r i n g t h i s s e a r c h . T h u s t h e s e c o n d \
in q q / \ c \ /
i s i n t e r p r e t e d a s a p a r t o f \ /
, a n d t h e f o l l o w i n g /
i s n o t r e c o g n i z e d a s a d e l i m i t e r . I n s t e a d , u s e \ 0 3 4
or \ x
1 c
a t t h e e n d o f q u o t e d c o n s t r u c t s .
# I n t e r p o l a t i o n
T h e 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 t e x t o b t a i n e d , w h i c h i s n o w d e l i m i t e r - i n d e p e n d e n t . T h e r e a r e m u l t i p l e c a s e s .
# < < ' E O F '
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 . 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 i n t a c t , s i n c e e s c a p e d d e l i m i t e r s a r e n o t a v a i l a b l e f o r h e r e - d o c s .
# m ' '
, t h e p a t t e r n o f s ' ' '
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 a t t h i s s t a g e . A n y b a c k s l a s h e d s e q u e n c e s i n c l u d i n g \ \
a r e t r e a t e d a t t h e s t a g e t o " p a r s i n g r e g u l a r e x p r e s s i o n s " .
# ' '
, q / /
, t r ' ' '
, y ' ' '
, t h e r e p l a c e m e n t o f s ' ' '
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 o f \ \
. T h e r e f o r e " - "
in t r ' ' '
a n d y ' ' '
i s t r e a t e d l i t e r a l l y a s a h y p h e n a n d n o c h a r a c t e r r a n g e i s a v a i l a b l e . \ 1
i n t h e r e p l a c e m e n t o f s ' ' '
d o e s n o t w o r k a s $ 1
.
# t r / / /
, y / / /
N o v a r i a b l e i n t e r p o l a t i o n o c c u r s . S t r i n g m o d i f y i n g c o m b i n a t i o n s f o r c a s e a n d q u o t i n g s u c h a s \ Q
, \ U
, a n d \ E
a r e n o t r e c o g n i z e d . T h e o t h e r e s c a p e s e q u e n c e s s u c h a s \ 2 0 0
a n d \ t
a n d b a c k s l a s h e d c h a r a c t e r s s u c h a s \ \
a n d \ -
a r e c o n v e r t e d t o a p p r o p r i a t e l i t e r a l s . T h e c h a r a c t e r " - "
i s t r e a t e d s p e c i a l l y a n d t h e r e f o r e \ -
i s t r e a t e d a s a l i t e r a l " - "
.
# " "
, ` `
, q q / /
, q x / /
, < f i l e * g l o b
>
, < < " E O F "
\ Q
, \ U
, \ u
, \ L
, \ l
, \ F
( 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 $ f o o . ( q u
o t e m e t a ( " b a z " . $ b a r ) )
i n t e r n a l l y . T h e o t h e r e s c a p e s e q u e n c e s s u c h a s \ 2 0 0
a n d \ t
a n d b a c k s l a s h e d c h a r a c t e r s s u c h a s \ \
a n d \ -
a r e r e p l a c 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 .
L e t i t b e s t r e s s e d t h a t w h a t e v e r f a l l s b e t w e e n \ Q
a n d \ E
i s i n t e r p o l a t e d i n t h e u s u a l w a y . S o m e t h i n g l i k e " \ Q \ \ E "
h a s n o \ E
i n s i d e . I n s t e a d , i t h a s \ Q
, \ \
, a n d E
, s o t h e r e s u l t i s t h e s a m e a s f o r " \ \ \ \ E "
. A s a g e n e r a l r u l e , b a c k s l a s h e s b e t w e e n \ Q
a n d \ E
m a y l e a d t o c o u n t e r i n t u i t i v e r e s u l t s . S o , " \ Q \ t \ E "
i s c o n v e r t e d t o q u o t e m e t a ( " \ t " )
, w h i c h i s t h e s a m e a s " \ \ \ t "
( s i n c e T A B i s n o t a l p h a n u m e r i c ) . N o t e a l s o t h a t :
$str = '\t';
return "\Q$str";
m a y b e c l o s e r t o t h e c o n j e c t u r a l i n t e n t i o n o f t h e w r i t e r o f " \ Q \ t \ E "
.
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 i n t e r n a l l y t o t h e j o i n
a n d " . "
c a t e n a t i o n o p e r a t i o n s . T h u s , " $ f o o X X X
' @ a r r ' "
b e c o m e s :
$foo . " XXX '" . (join $", @arr) . "'";
A l l o p e r a t i o n s a b o v e 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 .
B e c a u s e t h e r e s u l t o f " \ Q S T R
I N G \ E "
h a s a l l m e t a c h a r a c t e r s q u o t e d , t h e r 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 a \ Q \ E
p a i r . I f p r o t e c t e d b y \
, $
w i l l b e q u o t e d t o b e c o m e "
\ \ \ $ "
; i f n o t , i t i s i n t e r p r e t e d a s t h e s t a r t o f 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 o n c o d e n e e d s t o m a k e a d e c i s i o n 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 . F o r i n s t a n c e , w h e t h e r " a $ x - > { c } "
r e a l l y m e a n s :
"a " . $x . " -> {c}";
o r :
"a " . $x -> {c};
M o s t o f t h e t i m e , t h e l o n g e s t p o s s i b l e t e x t t h a t 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 w h i c h c o n t a i n s m a t c h i n g b r a c e s o r b r a c k e t s . b e c a u s e t h e o u t c o m e m a y b e d e t e r m i n e d b y v o t i n g b a s e d o n h e u r i s t i c e s t i m a t o r s , t h e r e s u l t i s n o t s t r i c t l y p r e d i c t a b l e . F o r t u n a t e l y , i t ' s u s u a l l y c o r r e c t f o r a m b i g u o u s c a s e s .
# t h e r e p l a c e m e n t o f s / / /
P r o c e s s i n g o f \ Q
, \ U
, \ u
, \ L
, \ l
, \ F
a n d i n t e r p o l a t i o n h a p p e n s a s w i t h q q / /
c o n s t r u c t s .
I t i s a t t h i s s t e p t h a t \ 1
i s b e g r u d g i n g l y c o n v e r t e d t o $ 1
i n t h e r e p l a c e m e n t t e x t o f s
/ / /
, i n o r d e r t o c o r r e c t t h e i n c o r r i g i b l e s e d h a c k e r s w h o h a v e n ' t p i c k e d u p t h e s a n e r i d i o m y e t . A w a r n i n g i s e m i t t e d i f t h e u s e w a r n i n g s
p r a g m a o r t h e - w c o m m a n d - l i n e f l a g ( t h a t i s , t h e $ ^ W
v a r i a b l e ) w a s s e t .
# RE
in ? 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
, \ F
, \ E
, 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 .
P r o c e s s i n g o f \ N { . . . }
i s a l s o d o n e h e r e , a n d c o m p i l e d i n t o a n i n t e r m e d i a t e f o r m f o r t h e r e g e x c o m p i l e r . ( T h i s i s b e c a u s e , a s m e n t i o n e d b e l o w , t h e r e g e x c o m p i l a t i o n m a y b e d o n e a t e x e c u t i o n t i m e , a n d \ N { . .
. }
i s a c o m p i l e - t i m e c o n s t r u c t . )
H o w e v e r a n y o t h e r c o m b i n a t i o n s o f \
f o l l o w e d b y a c h a r a c t e r a r e n o t s u b s t i t u t e d b u t o n l y s k i p p e d , i n o r d e r t o p a r s e t h e m a s r e g u l a r e x p r e s s i o n s a t t h e f o l l o w i n g s t e p . A s \ c
i s s k i p p e d a t t h i s s t e p , @
of \ c @
i n R E i s p o s s i b l y t r e a t e d a s a n a r r a y s y m b o l ( f o r e x a m p l e @ f o o
) , e v e n t h o u g h t h e s a m e t e x t i n q q / /
g i v e s i n t e r p o l a t i o n o f \ c @
.
C o d e b l o c k s s u c h a s ( ? { B L O C K } )
a r e h a n d l e d b y t e m p o r a r i l y p a s s i n g c o n t r o l b a c k t o t h e p e r l p a r s e r , i n a s i m i l a r w a y t h a t a n i n t e r p o l a t e d a r r a y s u b s c r i p t e x p r e s s i o n s u c h a s " f o
o $ a r r a y [ 1 + f ( " [ x y z " ) ] b a r "
w o u l d b e .
M o r e o v e r , i n s i d e ( ? { B L O C K } )
, ( ? # c o m m e n t )
, a n d a #
- c o m m e n t i n a / x
- r e g u l a r e x p r e s s i o n , n o p r o c e s s i n g i s p e r f o r m e d w h a t s o e v e r . T h i s i s t h e f i r s t s t e p a t w h i c h t h e p r e s e n c e o f t h e / x
m o d i f i e r i s r e l e v a n t .
I n t e r p o l a t i o n i n p a t t e r n s 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 e i t h e r 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 n R E a l t e r n a t i v e . T h i s i s 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 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 t h e 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 w o u l d b e t h e i n t e r p r e t a t i o n o f / $ a r r [ 0 - 9 ] /
. S i n c e v o t i n g a m o n g d i f f e r e n t e s t i m a t o r s m a y o c c u r , t h e r e s u l t i s n o t p r e d i c t a b l e .
T h e l a c k 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 . B e c a u s 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 h a p p e n s t o b e c h a r a c t e r s p e c i a l t o t h e R E e n g i n e , s u c h a s i n s * f o o * b a r *
, m [ f
o o ]
, o r ? f o o ?
; o r a n a l p h a n u m e r i c c h a r , a s i n :
m m ^ a \s* b mmx;
I n t h e R E a b o v e , w h i c h i s i n t e n t i o n a l l y o b f u s c a t e d f o r i l l u s t r a t i o n , t h e d e l i m i t e r i s m
, t h e m o d i f i e r i s mx
, a n d a f t e r d e l i m i t e r - r e m o v a l t h e R E i s t h e s a m e a s f o r m / ^ a \ s * b
/ m x
. T h e r e ' s m o r e t h a n o n e r e a s o n y o u ' r e e n c o u r a g e d t o r e s t r i c t y o u r d e l i m i t e r s t o 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 o i c e s .
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 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 .
# p a r s i n g r e g u l a r e x p r e s s i o n s
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 , b u t t h i s o n e h a p p e n s a t r u n t i m e , a l 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 p r e p r o c e s s i n g d e s c r i b 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 o n c a t e n a t i o n , j o i n i n g , c a s i n g t r a n s l a t i o n , o r m e t a q u o t 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 t h e 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 m i g h t b e b e t t e r 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 , w e s h a l l d o s o h e r e .
T h i s i s a n o t h e r s t e p w h e r e t h e p r e s e n c e o f t h e / x
m o d i f i e r 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 f r o m l e f t t o r i g h t a n d c o n v e r t s i t i n 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 a c t e r s a r e e i t h e r r e p l a c e d w i t h 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 ( a s w i t h \ {
) , o r e l s e t h e y g e n e r a t e s p e c i a l n o d e s i n t h e f i n i t e a u t o m a t o n ( a s w i t h \ b
) . C h a r a c t e r s s p e c i a l t o t h e R E e n g i n e ( s u c h a s |
) 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 o r g r o u p s o f 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 e l s e 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 ) .
P a r s i n g o f t h e b r a c k e t e d c h a r a c t e r c l a s s c o n s t r u c t , [ . . . ]
, i s r a t h e r d i f f e r e n t t h a n t h e r u l e u s e d f o r t h e r e s t o f t h e p a t t e r n . T h e t e r m i n a t o r o f t h i s c o n s t r u c t i s f o u n d u s i n g t h e s a m e r u l e s a s f o r f i n d i n g t h e t e r m i n a t o r o f a { }
- d e l i m i t e d c o n s t r u c t , t h e o n l y e x c e p t i o n b e i n g t h a t ]
i m m e d i a t e l y f o l l o w i n g [
i s t r e a t e d a s t h o u g h p r e c e d e d b y a b a c k s l a s h .
T h e t e r m i n a t o r o f r u n t i m e ( ? { . . .
} )
i s f o u n d b y t e m p o r a r i l y s w i t c h i n g c o n t r o l t o t h e p e r l p a r s e r , w h i c h s h o u l d s t o p a t t h e p o i n t w h e r e t h e l o g i c a l l y b a l a n c i n g t e r m i n a t i n g }
i s f o u n d .
I t i s p o s s i b l e t o i n s p e c t b o t h t h e s t r i n g g i v e n t o R E e n g i n e a n d t h e r e s u l t i n g f i n i t e a u t o m a t o n . S e e t h e a r g u m e n t s d e b u g
/ d e b
u g c o l o r
i n t h e u s e re
p r a g m a , a s w e l l a s P e r l ' s - D r c o m m a n d - l i n e s w i t c h d o c u m e n t e d i n " C o m m a n d S w i t c h e s " i n p e r l r u n .
# 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 w i t h o u t n o t i c e . T h i s s t e p i s p e r f o r m e d o v e r t h e f i n i t e a u t o m a t o n t h a t w a s g e n e r a t e d d u r i n g t h e p r e v i o u s p a s s .
I t i s a t t h i s s t a g e t h a t s p l i t ( )
s i l e n t l y o p t i m i z e s / ^ /
t o m e a n / ^ / m
.
# 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 d o u b l e - q u o t e i n t e r p o l a t i o n . I t i s t h e n i n t e r p r e t e d a s a n e x t e r n a l 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 b a c k t i c k s t r i n g , 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 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 p e r 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 l i t e r a l d o l l a r - s i g n 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 . )
I n s c a l a r c o n t e x t , 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 ( t h 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 o n e r r o r . W h e n $ /
i s s e t t o u n d e f
( s o m e t i m e s k n o w n a s f i l e - s l u r p m o d e ) a n d t h e f i l e i s e m p t y , i t r e t u r n s ' '
t h e f i r s t t i m e , f o l l o w e d b y u n d e f
s u b s e q u e n t l y .
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 e r e t u r n e d 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
s t a t e m e n t ( e v e n i f d i s g u i s e d a s a 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 g l o b a l v a r i a b l e $
_
, d e s t r o y i n g w h a t e v e r w a s t h e r e p r e v i o u s l y . ( 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 . ) T h e $
_
v a r i a b l e i s n o t i m p l i c i t l y l o c a l i z e d . Y o u ' l l h a v e t o p u t a l o c
a l $ _ ;
b e f o r e t h e l o o p i f y o u w a n t t h a t t o h a p p e n .
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 :
while (defined($_ = <STDIN>)) { print; }
while ($_ = <STDIN>) { print; }
while (<STDIN>) { print; }
for (;<STDIN>;) { print; }
print while defined($_ = <STDIN>);
print while ($_ = <STDIN>);
print while <STDIN>;
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 s s i g n s t o a l e x i c a l v a r i a b l e i n s t e a d o f t o $ _
:
while (my $line = <STDIN>) { print $line }
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 w h e t h e r 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 t h 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 ; f o r e x a m p l e a " " o r a " 0 "
w i t h n o t r a i l i n g n e w l i n e . I f y o u r e a l l y m e a n f o r 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 a n 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 e l i c i t s a w a r n i n g i f t h e u s e w a r n i n g s
p r a g m a o r t h e - w c o m m a n d - l i n e s w i t c h ( t h e $ ^ W
v a r i a b l e ) 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 , a m o n g s t o t h e r s . S e e p e r l o p e n t u t a n d " 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 m p r i s i n g a l l 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 g r o w t o a r a t h e r 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 .
< F I L E H A N D L E >
m a y a l s o b e s p e l l e d r e a d l i n e ( * F I L E H A N D L E )
. S e e " r e a d l i n e " i n p e r l f u n c .
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 : i t 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 , a n d a n y o t h e r U n i x f i l t e r p r o g r a m t h a t t a k e s a l i s t o f f i l e n a m e s , d o i n g t h e s a m e t o e a c h l i n e o f i n p u t f r o m a l l o f t h e m . 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 t h e @ A R G
V
a r r a y 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 t h e $ A R G V
v a r i a b l e . 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 . )
S i n c e t h e n u l l f i l e h a n d l e u s e s t h e t w o a r g u m e n t f o r m o f " o p e n " i n p e r l f u n c i t i n t e r p r e t s s p e c i a l c h a r a c t e r s , s o i f y o u h a v e a s c r i p t l i k e t h i s :
while (<>) {
print;
}
a n d c a l l i t w i t h p e r l d a n g e r o u s .
p l ' r m - r f v * | '
, i t a c t u a l l y o p e n s a p i p e , e x e c u t e s t h e rm
c o m m a n d a n d r e a d s rm
' s o u t p u t f r o m t h a t p i p e . I f y o u w a n t a l l i t e m s i n @
A R G V
t o b e i n t e r p r e t e d a s f i l e n a m e s , y o u c a n u s e t h e m o d u l e A R G
V : : r e a d o n l y
f r o m C P A N , o r u s e t h e d o u b l e b r a c k e t :
while (<<>>) {
print;
}
U s i n g d o u b l e a n g l e b r a c k e t s i n s i d e o f a w h i l e c a u s e s t h e o p e n t o u s e t h e t h r e e a r g u m e n t f o r m ( w i t h t h e s e c o n d a r g u m e n t b e i n g <
) , s o a l l a r g u m e n t s i n A R G V
a r e t r e a t e d a s l i t e r a l f i l e n a m e s ( i n c l u d i n g " - "
) . ( N o t e t h a t f o r c o n v e n i e n c e , i f y o u u s e < < > >
a n d i f @
A R G V
i s e m p t y , i t w i l l s t i l l r e a d f r o m t h e s t a n d a r d i n p u t . )
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 t h o u g h 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 . S e e t h e e x a m p l e i n " e o f " i n p e r l f u n c 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 r e a d i n p u t f r o m S T D I N .
I f w h a t t h e a n g l e b r a c k e t s c o n t a i n i s a s i m p l e s c a l a r v a r i a b l e ( f o r e x a m p l e , $ 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 . E v e n < $ x >
( n o t e t h e e x t r a s p a c e ) i s t r e a t e d a s g l o
b ( " $ x " )
, n o t r e a d l i n e ( $ x )
.
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 . ) F o r e x a m p l e :
while (<*.c>) {
chmod 0644, $_;
}
i s r o u g h l y 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>) {
chomp;
chmod 0644, $_;
}
e x c e p t t h a t t h e g l o b b i n g i s a c t u a l l y d o n e i n t e r n a l l y u s i n g t h e s t a n d a r d F i l e : : G l o b
e x t e n s i o n . 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>;
A ( f i l e ) 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 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 . H o w e v e r , i n s c a l a r c o n t e x t 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 ' s c a l l e d , o r u n d e f
w h e n t h e l i s t h a s r u n o u t . A s w i t h f i l e h a n d l e r e a d 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
, b e c a u s e l e g a l g l o b r e t u r n s ( f o r e x a m p l e , 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 f 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 o l 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 p r e c o m p u t e s t h e n u m b e r w h i c h 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 .
# N o - o p s
P e r l d o e s n ' t o f f i c i a l l y h a v e a n o - o p o p e r a t o r , b u t t h e b a r e c o n s t a n t s 0
a n d 1
a r e s p e c i a l - c a s e d n o t t o p r o d u c e a w a r n i n g i n v o i d c o n t e x t , s o y o u c a n f o r e x a m p l e s a f e l y d o
1 while foo();
# 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 , | a n d ^ o p s a c t a s t h o u g h 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 & o p a c t s a s t h o u g h 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 . T h e g r a n u l a r i t y f o r s u c h e x t e n s i o n o r t r u n c a t i o n i s o n e o r m o r e b y t e s .
# 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 , 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
T h i s s o m e w h a t u n p r e d i c t a b l e b e h a v i o r c a n b e a v o i d e d w i t h t h e e x p e r i m e n t a l " b i t w i s e " f e a t u r e , n e w i n P e r l 5 . 2 2 . Y o u c a n e n a b l e i t v i a u s e f e a t u r e ' b i t w i s e '
. B y d e f a u l t , i t w i l l w a r n u n l e s s t h e " e x p e r i m e n t a l : : b i t w i s e "
w a r n i n g s c a t e g o r y h a s b e e n d i s a b l e d . ( u s
e e x p e r i m e n t a l ' b i t w i s e '
w i l l e n a b l e t h e f e a t u r e a n d d i s a b l e t h e w a r n i n g . ) U n d e r t h i s f e a t u r e , t h e f o u r s t a n d a r d b i t w i s e o p e r a t o r s ( ~ | & ^
) a r e a l w a y s n u m e r i c . A d d i n g a d o t a f t e r e a c h o p e r a t o r ( ~ . | . & . ^ .
) f o r c e s i t t o t r e a t i t s o p e r a n d s a s s t r i n g s :
use experimental "bitwise";
$foo = 150 | 105; # yields 255 (0x96 | 0x69 is 0xFF)
$foo = '150' | 105; # yields 255
$foo = 150 | '105'; # yields 255
$foo = '150' | '105'; # yields 255
$foo = 150 |. 105; # yields string '155'
$foo = '150' |. 105; # yields string '155'
$foo = 150 |.'105'; # yields string '155'
$foo = '150' |.'105'; # yields string '155'
$baz = $foo & $bar; # both operands numeric
$biz = $foo ^. $bar; # both operands stringy
T h e a s s i g n m e n t v a r i a n t s o f t h e s e o p e r a t o r s ( & = | = ^ = & . = | . = ^ . =
) b e h a v e l i k e w i s e u n d e r t h e f e a t u r e .
T h e b e h a v i o r o f t h e s e o p e r a t o r s i s p r o b l e m a t i c ( a n d s u b j e c t t o c h a n g e ) i f e i t h e r o r b o t h o f t h e s t r i n g s a r e e n c o d e d i n U T F - 8 ( s e e " B y t e a n d C h a r a c t e r S e m a n t i c s " i n p e r l u n i c o d e .
S e e " v e c " i n p e r l f u n c f o r i n f o r m a t i o n o n h o w t o m a n i p u l a t e i n d i v i d u a l b i t s i n a b i t v e c t o r .
# 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 o u s e i n t e g e r o p e r a t i o n s ( s e e i n t e g e r f o r a d e t a i l e d e x p l a n a t i o n ) 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 . N o t e t h a t t h i s d o e s n ' t m e a n e v e r y t h i n g i s a n i n t e g e r , m e r e l y t h a t P e r l w i l l u s e i n t e g e r o p e r a t i o n s f o r a r i t h m e t i c , c o m p a r i s o n , a n d b i t w i s e o p e r a t o r s . F o r e x a m p l e , e v e n u n d e r u s e i n t e g
e r
, i f y o u t a k e t h e s q r t ( 2 )
, y o u ' l l s t i l l g e t 1 . 4 1 4 2 1 3 5 6 2 3 7 3 1
o r s o .
U s e d o n n u m b e r s , t h e b i t w i s e o p e r a t o r s ( &
|
^
~
< <
> >
) 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 , b u t 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
is - 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 a n a l o g o u s m e c h a n i s m t o p r o v i d e a u t o m a t i c r o u n d i n g o r t r u n c a t i o n t o 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 ( )
or 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 . S e e p e r l f a q 4 .
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 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 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 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 w o r k o n b o t h t h e r e a l s a n d t h e i m a g i n a r y n u m b e r s . M a t h : : C o m p l e x
i s 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
, M a t h :
: B i g R a t
, a n d M a t h : : B i g F l o a t
m o d u l e s , a l o n g w i t h t h e b i g n u m
, b i g i
n t
, a n d b i g r a t
p r a g m a 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 l t h o u g h t h e y ' r e c u r r e n t l y p r e t t y s l o w . 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 5.010;
use bigint; # easy interface to Math::BigInt
$x = 123456789123456789;
say $x * $x;
+15241578780673678515622620750190521
O r w i t h r a t i o n a l s :
use 5.010;
use bigrat;
$x = 3/22;
$y = 4/6;
say "x/y is ", $x/$y;
say "x*y is ", $x*$y;
x/y is 9/44
x*y is 1/11
S e v e r a l m o d u l e s l e t y o u c a l c u l a t e w i t h u n l i m i t e d o r f i x e d p r e c i s i o n ( b o u n d o n l y b y m e m o r y a n d C P U t i m e ) . T h e r e a r e a l s o s o m e n o n - s t a n d a r d m o d u l e s t h a t p r o v i d e f a s t e r i m p l e m e n t a t i o n s v i a e x t e r n a l C l i b r a r i e s .
H e r e i s a s h o r t , b u t i n c o m p l e t e s u m m a r y :
Math::String treat string sequences like numbers
Math::FixedPrecision calculate with a fixed precision
Math::Currency for currency calculations
Bit::Vector manipulate bit vectors fast (uses C)
Math::BigIntFast Bit::Vector wrapper for big numbers
Math::Pari provides access to the Pari C library
Math::Cephes uses the external Cephes C library (no
big numbers)
Math::Cephes::Fraction fractions via the Cephes library
Math::GMP another one using an external C library
Math::GMPz an alternative interface to libgmp's big ints
Math::GMPq an interface to libgmp's fraction numbers
Math::GMPf an interface to libgmp's floating point numbers
C h o o s e w i s e l y .
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 .