P e r l d o c B r o w s e r
●
5 . 3 7 . 6
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 v a r
( 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 . 3 7 . 6 .
T h i s i s a d e v e l o p m e n t v e r s i o n o f P e r l .
●
N A M E
●
D E S C R I P T I O N
●
T h e S y n t a x o f V a r i a b l e N a m e s
●
S P E C I A L V A R I A B L E S
●
G e n e r a l V a r i a b l e s
●
V a r i a b l e s r e l a t e d t o r e g u l a r e x p r e s s i o n s
●
P e r f o r m a n c e i s s u e s
●
V a r i a b l e s r e l a t e d t o f i l e h a n d l e s
●
V a r i a b l e s r e l a t e d t o f o r m a t s
●
E r r o r V a r i a b l e s
●
V a r i a b l e s r e l a t e d t o t h e i n t e r p r e t e r s t a t e
●
D e p r e c a t e d a n d r e m o v e d v a r i a b l e s
# N A M E
p e r l v a r - P e r l p r e d e f i n e d v a r i a b l e s
# D E S C R I P T I O N
# T h e S y n t a x o f V a r i a b l e N a m e s
V a r i a b l e n a m e s i n P e r l c a n h a v e s e v e r a l f o r m a t s . U s u a l l y , t h e y m u s t b e g i n w i t h a l e t t e r o r u n d e r s c o r e , i n w h i c h c a s e t h e y c a n b e a r b i t r a r i l y l o n g ( u p t o a n i n t e r n a l l i m i t o f 2 5 1 c h a r a c t e r s ) a n d m a y c o n t a i n l e t t e r s , d i g i t s , u n d e r s c o r e s , o r t h e s p e c i a l s e q u e n c e : :
or '
. I n t h i s c a s e , t h e p a r t b e f o r e t h e l a s t : :
or '
i s t a k e n t o b e a p a c k a g e q u a l i f i e r ; s e e p e r l m o d . A U n i c o d e l e t t e r t h a t i s n o t A S C I I i s n o t c o n s i d e r e d t o b e a l e t t e r u n l e s s " u s e u t f 8 "
i s i n e f f e c t , a n d s o m e w h a t m o r e c o m p l i c a t e d r u l e s a p p l y ; s e e " I d e n t i f i e r p a r s i n g " i n p e r l d a t a f o r d e t a i l s .
P e r l v a r i a b l e n a m e s m a y a l s o b e a s e q u e n c e o f d i g i t s , a s i n g l e p u n c t u a t i o n c h a r a c t e r , o r t h e t w o - c h a r a c t e r s e q u e n c e : ^
( c a r e t o r C I R C U M F L E X A C C E N T ) f o l l o w e d b y a n y o n e o f t h e c h a r a c t e r s [ ] [ A - Z
^ _ ? \ ]
. T h e s e n a m e s a r e a l l r e s e r v e d f o r s p e c i a l u s e s b y P e r l ; f o r e x a m p l e , t h e a l l - d i g i t s n a m e s a r e u s e d t o h o l d d a t a c a p t u r e d b y b a c k r e f e r e n c e s a f t e r a r e g u l a r e x p r e s s i o n m a t c h .
S i n c e P e r l v 5 . 6 . 0 , P e r l v a r i a b l e n a m e s m a y a l s o b e a l p h a n u m e r i c s t r i n g s p r e c e d e d b y a c a r e t . T h e s e m u s t a l l b e w r i t t e n u s i n g t h e d e m a r c a t e d v a r i a b l e f o r m u s i n g c u r l y b r a c e s s u c h a s $ { ^ F o o }
; t h e b r a c e s a r e n o t o p t i o n a l . $ { ^ F o
o }
d e n o t e s t h e s c a l a r v a r i a b l e w h o s e n a m e i s c o n s i d e r e d t o b e a c o n t r o l - F
f o l l o w e d b y t w o o
' s . ( S e e " D e m a r c a t e d v a r i a b l e n a m e s u s i n g b r a c e s " i n p e r l d a t a f o r m o r e i n f o r m a t i o n o n t h i s f o r m o f s p e l l i n g a v a r i a b l e n a m e o r s p e c i f y i n g a c c e s s t o a n e l e m e n t o f a n a r r a y o r a h a s h ) . T h e s e v a r i a b l e s a r e r e s e r v e d f o r f u t u r e s p e c i a l u s e s b y P e r l , e x c e p t f o r t h e o n e s t h a t b e g i n w i t h ^ _
( c a r e t - u n d e r s c o r e ) . N o n a m e t h a t b e g i n s w i t h ^ _
w i l l a c q u i r e a s p e c i a l m e a n i n g i n a n y f u t u r e v e r s i o n o f P e r l ; s u c h n a m e s m a y t h e r e f o r e b e u s e d s a f e l y i n p r o g r a m s . $ ^ _
i t s e l f , h o w e v e r , is r e s e r v e d .
N o t e t h a t y o u a l s o m u s t u s e t h e d e m a r c a t e d f o r m t o a c c e s s s u b s c r i p t s o f v a r i a b l e s o f t h i s t y p e w h e n i n t e r p o l a t i n g , f o r i n s t a n c e t o a c c e s s t h e f i r s t e l e m e n t o f t h e @ { ^ C A P T U R E }
v a r i a b l e i n s i d e o f a d o u b l e q u o t e d s t r i n g y o u w o u l d w r i t e " $ { ^ C A P T U R E [ 0 ] } "
a n d N O T " $ { ^ C A P T U R E } [ 0 ] "
w h i c h w o u l d m e a n t o r e f e r e n c e a s c a l a r v a r i a b l e n a m e d $ { ^ C A P T U R E }
a n d n o t i n d e x 0 o f t h e m a g i c @ { ^ C A P T U R E }
a r r a y w h i c h i s p o p u l a t e d b y t h e r e g e x e n g i n e .
P e r l i d e n t i f i e r s t h a t b e g i n w i t h d i g i t s o r p u n c t u a t i o n c h a r a c t e r s a r e e x e m p t f r o m t h e e f f e c t s o f t h e p a c k a g e
d e c l a r a t i o n a n d a r e a l w a y s f o r c e d t o b e i n p a c k a g e m a i n
; t h e y a r e a l s o e x e m p t f r o m s t r i c t ' v a r s '
e r r o r s . A f e w o t h e r n a m e s a r e a l s o e x e m p t i n t h e s e w a y s :
ENV STDIN
INC STDOUT
ARGV STDERR
ARGVOUT
SIG
I n p a r t i c u l a r , t h e s p e c i a l $ { ^ _ X
Y Z }
v a r i a b l e s a r e a l w a y s t a k e n t o b e i n p a c k a g e m a i n
, r e g a r d l e s s o f a n y p a c k a g e
d e c l a r a t i o n s p r e s e n t l y i n s c o p e .
# S P E C I A L V A R I A B L E S
T h e f o l l o w i n g n a m e s h a v e s p e c i a l m e a n i n g t o P e r l . M o s t p u n c t u a t i o n n a m e s h a v e r e a s o n a b l e m n e m o n i c s , o r a n a l o g s i n t h e s h e l l s . N e v e r t h e l e s s , i f y o u w i s h t o u s e l o n g v a r i a b l e n a m e s , y o u n e e d o n l y s a y :
use English;
a t t h e t o p o f y o u r p r o g r a m . T h i s a l i a s e s a l l t h e s h o r t n a m e s t o t h e l o n g n a m e s i n t h e c u r r e n t p a c k a g e . S o m e e v e n h a v e m e d i u m n a m e s , g e n e r a l l y b o r r o w e d f r o m a w k . F o r m o r e i n f o , p l e a s e s e e E n g l i s h .
B e f o r e y o u c o n t i n u e , n o t e t h e s o r t o r d e r f o r v a r i a b l e s . I n g e n e r a l , w e f i r s t l i s t t h e v a r i a b l e s i n c a s e - i n s e n s i t i v e , a l m o s t - l e x i g r a p h i c a l o r d e r ( i g n o r i n g t h e {
or ^
p r e c e d i n g w o r d s , a s i n $ { ^ U N I C
O D E }
or $ ^ T
) , a l t h o u g h $ _
a n d @ _
m o v e u p t o t h e t o p o f t h e p i l e . F o r v a r i a b l e s w i t h t h e s a m e i d e n t i f i e r , w e l i s t i t i n o r d e r o f s c a l a r , a r r a y , h a s h , a n d b a r e w o r d .
# G e n e r a l V a r i a b l e s
# $ A R G
# $ _
T h e d e f a u l t i n p u t a n d p a t t e r n - s e a r c h i n g s p a c e . T h e f o l l o w i n g p a i r s a r e e q u i v a l e n t :
while (<>) {...} # equivalent only in while!
while (defined($_ = <>)) {...}
/^Subject:/
$_ =~ /^Subject:/
tr/a-z/A-Z/
$_ =~ tr/a-z/A-Z/
chomp
chomp($_)
H e r e a r e t h e p l a c e s w h e r e P e r l w i l l a s s u m e $ _
e v e n i f y o u d o n ' t u s e i t :
T h e f o l l o w i n g f u n c t i o n s u s e $
_
a s a d e f a u l t a r g u m e n t :
a b s , a l a r m , c h o m p , c h o p , c h r , c h r o o t , c o s , d e f i n e d , e v a l , e v a l b y t e s , e x p , f c , g l o b , h e x , i n t , l c , l c f i r s t , l e n g t h , l o g , l s t a t , m k d i r , o c t , o r d , p o s , p r i n t , p r i n t f , q u o t e m e t a , r e a d l i n k , r e a d p i p e , r e f , r e q u i r e , r e v e r s e ( i n s c a l a r c o n t e x t o n l y ) , r m d i r , s a y , s i n , s p l i t ( f o r i t s s e c o n d a r g u m e n t ) , s q r t , s t a t , s t u d y , u c , u c f i r s t , u n l i n k , u n p a c k .
A l l f i l e t e s t s ( - f
, - d
) e x c e p t f o r - t
, w h i c h d e f a u l t s t o S T D I N . S e e " - X " i n p e r l f u n c
T h e p a t t e r n m a t c h i n g o p e r a t i o n s m / /
, s / / /
a n d t r / / /
( a k a y
/ / /
) w h e n u s e d w i t h o u t a n = ~
o p e r a t o r .
T h e d e f a u l t i t e r a t o r v a r i a b l e i n a f o r e a c h
l o o p i f n o o t h e r v a r i a b l e i s s u p p l i e d .
T h e i m p l i c i t i t e r a t o r v a r i a b l e i n t h e g r e p ( )
a n d m a p ( )
f u n c t i o n s .
T h e i m p l i c i t v a r i a b l e o f g i v e
n ( )
.
T h e d e f a u l t p l a c e t o p u t t h e n e x t v a l u e o r i n p u t r e c o r d w h e n a < F H >
, r e a d l i n e
, r e a d d i r
or e a c h
o p e r a t i o n ' s r e s u l t i s t e s t e d b y i t s e l f a s t h e s o l e c r i t e r i o n o f a w h i l e
t e s t . O u t s i d e a w h i l e
t e s t , t h i s w i l l n o t h a p p e n .
$ _
i s a g l o b a l v a r i a b l e .
H o w e v e r , b e t w e e n p e r l v 5 . 1 0 . 0 a n d v 5 . 2 4 . 0 , i t c o u l d b e u s e d l e x i c a l l y b y w r i t i n g m y $ _
. M a k i n g $ _
r e f e r t o t h e g l o b a l $ _
i n t h e s a m e s c o p e w a s t h e n p o s s i b l e w i t h o u r $ _
. T h i s e x p e r i m e n t a l f e a t u r e w a s r e m o v e d a n d i s n o w a f a t a l e r r o r , b u t y o u m a y e n c o u n t e r i t i n o l d e r c o d e .
M n e m o n i c : u n d e r l i n e i s u n d e r s t o o d i n c e r t a i n o p e r a t i o n s .
# @ A R G
# @ _
W i t h i n a s u b r o u t i n e t h e a r r a y @
_
c o n t a i n s t h e p a r a m e t e r s p a s s e d t o t h a t s u b r o u t i n e . I n s i d e a s u b r o u t i n e , @ _
i s t h e d e f a u l t a r r a y f o r t h e a r r a y o p e r a t o r s p o
p
a n d s h i f t
.
S e e p e r l s u b .
# $ L I S T _ S E P A R A T O R
# $ "
W h e n a n a r r a y o r a n a r r a y s l i c e i s i n t e r p o l a t e d i n t o a d o u b l e - q u o t e d s t r i n g o r a s i m i l a r c o n t e x t s u c h a s / . . . /
, i t s e l e m e n t s a r e s e p a r a t e d b y t h i s v a l u e . D e f a u l t i s a s p a c e . F o r e x a m p l e , t h i s :
print "The array is: @array\n";
i s e q u i v a l e n t t o t h i s :
print "The array is: " . join($", @array) . "\n";
M n e m o n i c : w o r k s i n d o u b l e - q u o t e d c o n t e x t .
# $ P R O C E S S _ I D
# $ P I D
# $ $
T h e p r o c e s s n u m b e r o f t h e P e r l r u n n i n g t h i s s c r i p t . T h o u g h y o u c a n s e t t h i s v a r i a b l e , d o i n g s o i s g e n e r a l l y d i s c o u r a g e d , a l t h o u g h i t c a n b e i n v a l u a b l e f o r s o m e t e s t i n g p u r p o s e s . I t w i l l b e r e s e t a u t o m a t i c a l l y a c r o s s f
o r k ( )
c a l l s .
N o t e f o r L i n u x a n d D e b i a n G N U / k F r e e B S D u s e r s : B e f o r e P e r l v 5 . 1 6 . 0 p e r l w o u l d e m u l a t e P O S I X s e m a n t i c s o n L i n u x s y s t e m s u s i n g L i n u x T h r e a d s , a p a r t i a l i m p l e m e n t a t i o n o f P O S I X T h r e a d s t h a t h a s s i n c e b e e n s u p e r s e d e d b y t h e N a t i v e P O S I X T h r e a d L i b r a r y ( N P T L ) .
L i n u x T h r e a d s i s n o w o b s o l e t e o n L i n u x , a n d c a c h i n g g e t p i d ( )
l i k e t h i s m a d e e m b e d d i n g p e r l u n n e c e s s a r i l y c o m p l e x ( s i n c e y o u ' d h a v e t o m a n u a l l y u p d a t e t h e v a l u e o f $ $ ) , s o n o w $ $
a n d g e t p p
i d ( )
w i l l a l w a y s r e t u r n t h e s a m e v a l u e s a s t h e u n d e r l y i n g C l i b r a r y .
D e b i a n G N U / k F r e e B S D s y s t e m s a l s o u s e d L i n u x T h r e a d s u p u n t i l a n d i n c l u d i n g t h e 6 . 0 r e l e a s e , b u t a f t e r t h a t m o v e d t o F r e e B S D t h r e a d s e m a n t i c s , w h i c h a r e P O S I X - l i k e .
T o s e e i f y o u r s y s t e m i s a f f e c t e d b y t h i s d i s c r e p a n c y c h e c k i f g e t c o n f G N U _ L I B P T H R E A D _ V E R S I O N
| g r e p - q N P T L
r e t u r n s a f a l s e v a l u e . N T P L t h r e a d s p r e s e r v e t h e P O S I X s e m a n t i c s .
M n e m o n i c : s a m e a s s h e l l s .
# $ P R O G R A M _ N A M E
# $ 0
C o n t a i n s t h e n a m e o f t h e p r o g r a m b e i n g e x e c u t e d .
O n s o m e ( b u t n o t a l l ) o p e r a t i n g s y s t e m s a s s i g n i n g t o $ 0
m o d i f i e s t h e a r g u m e n t a r e a t h a t t h e ps
p r o g r a m s e e s . O n s o m e p l a t f o r m s y o u m a y h a v e t o u s e s p e c i a l ps
o p t i o n s o r a d i f f e r e n t ps
t o s e e t h e c h a n g e s . M o d i f y i n g t h e $ 0
i s m o r e u s e f u l a s a w a y o f i n d i c a t i n g t h e c u r r e n t p r o g r a m s t a t e t h a n i t i s f o r h i d i n g t h e p r o g r a m y o u ' r e r u n n i n g .
N o t e t h a t t h e r e a r e p l a t f o r m - s p e c i f i c l i m i t a t i o n s o n t h e m a x i m u m l e n g t h o f $ 0
. I n t h e m o s t e x t r e m e c a s e i t m a y b e l i m i t e d t o t h e s p a c e o c c u p i e d b y t h e o r i g i n a l $ 0
.
I n s o m e p l a t f o r m s t h e r e m a y b e a r b i t r a r y a m o u n t o f p a d d i n g , f o r e x a m p l e s p a c e c h a r a c t e r s , a f t e r t h e m o d i f i e d n a m e a s s h o w n b y ps
. I n s o m e p l a t f o r m s t h i s p a d d i n g m a y e x t e n d a l l t h e w a y t o t h e o r i g i n a l l e n g t h o f t h e a r g u m e n t a r e a , n o m a t t e r w h a t y o u d o ( t h i s i s t h e c a s e f o r e x a m p l e w i t h L i n u x 2 . 2 ) .
N o t e f o r B S D u s e r s : s e t t i n g $ 0
d o e s n o t c o m p l e t e l y r e m o v e " p e r l " f r o m t h e p s ( 1 ) o u t p u t . F o r e x a m p l e , s e t t i n g $ 0
to " f o o b a r "
m a y r e s u l t i n " p e r l : f o o b a r ( p e r l )
"
( w h e t h e r b o t h t h e " p e r l : "
p r e f i x a n d t h e " ( p e r l ) " s u f f i x a r e s h o w n d e p e n d s o n y o u r e x a c t B S D v a r i a n t a n d v e r s i o n ) . T h i s i s a n o p e r a t i n g s y s t e m f e a t u r e , P e r l c a n n o t h e l p i t .
I n m u l t i t h r e a d e d s c r i p t s P e r l c o o r d i n a t e s t h e t h r e a d s s o t h a t a n y t h r e a d m a y m o d i f y i t s c o p y o f t h e $ 0
a n d t h e c h a n g e b e c o m e s v i s i b l e t o p s ( 1 ) ( a s s u m i n g t h e o p e r a t i n g s y s t e m p l a y s a l o n g ) . N o t e t h a t t h e v i e w o f $ 0
t h e o t h e r t h r e a d s h a v e w i l l n o t c h a n g e s i n c e t h e y h a v e t h e i r o w n c o p i e s o f i t .
I f t h e p r o g r a m h a s b e e n g i v e n t o p e r l v i a t h e s w i t c h e s - e
or - E
, $ 0
w i l l c o n t a i n t h e s t r i n g " - e "
.
O n L i n u x a s o f p e r l v 5 . 1 4 . 0 t h e l e g a c y p r o c e s s n a m e w i l l b e s e t w i t h p r c t l ( 2 )
, i n a d d i t i o n t o a l t e r i n g t h e P O S I X n a m e v i a a r
g v [ 0 ]
a s p e r l h a s d o n e s i n c e v e r s i o n 4 . 0 0 0 . N o w s y s t e m u t i l i t i e s t h a t r e a d t h e l e g a c y p r o c e s s n a m e s u c h a s p s , t o p a n d k i l l a l l w i l l r e c o g n i z e t h e n a m e y o u s e t w h e n a s s i g n i n g t o $ 0
. T h e s t r i n g y o u s u p p l y w i l l b e c u t o f f a t 1 6 b y t e s , t h i s i s a l i m i t a t i o n i m p o s e d b y L i n u x .
W i d e c h a r a c t e r s a r e i n v a l i d i n $ 0
v a l u e s . F o r h i s t o r i c a l r e a s o n s , t h o u g h , P e r l a c c e p t s t h e m a n d e n c o d e s t h e m t o U T F - 8 . W h e n t h i s h a p p e n s a w i d e - c h a r a c t e r w a r n i n g i s t r i g g e r e d .
M n e m o n i c : s a m e a s sh a n d k s h .
# $ R E A L _ G R O U P _ I D
# $ G I D
# $ (
T h e r e a l g i d o f t h i s p r o c e s s . I f y o u a r e o n a m a c h i n e t h a t s u p p o r t s m e m b e r s h i p i n m u l t i p l e g r o u p s s i m u l t a n e o u s l y , g i v e s a s p a c e s e p a r a t e d l i s t o f g r o u p s y o u a r e i n . T h e f i r s t n u m b e r i s t h e o n e r e t u r n e d b y g e t g i d ( )
, a n d t h e s u b s e q u e n t o n e s b y g e t g r o
u p s ( )
, o n e o f w h i c h m a y b e t h e s a m e a s t h e f i r s t n u m b e r .
H o w e v e r , a v a l u e a s s i g n e d t o $ (
m u s t b e a s i n g l e n u m b e r u s e d t o s e t t h e r e a l g i d . S o t h e v a l u e g i v e n b y $ (
s h o u l d n o t b e a s s i g n e d b a c k t o $ (
w i t h o u t b e i n g f o r c e d n u m e r i c , s u c h a s b y a d d i n g z e r o . N o t e t h a t t h i s i s d i f f e r e n t t o t h e e f f e c t i v e g i d ( $ )
) w h i c h d o e s t a k e a l i s t .
Y o u c a n c h a n g e b o t h t h e r e a l g i d a n d t h e e f f e c t i v e g i d a t t h e s a m e t i m e b y u s i n g P O S I X : : s e t g i
d ( )
. C h a n g e s t o $ (
r e q u i r e a c h e c k t o $ !
t o d e t e c t a n y p o s s i b l e e r r o r s a f t e r a n a t t e m p t e d c h a n g e .
M n e m o n i c : p a r e n t h e s e s a r e u s e d t o g r o u p t h i n g s . T h e r e a l g i d i s t h e g r o u p y o u l e f t , i f y o u ' r e r u n n i n g s e t g i d .
# $ E F F E C T I V E _ G R O U P _ I D
# $ E G I D
# $ )
T h e e f f e c t i v e g i d o f t h i s p r o c e s s . I f y o u a r e o n a m a c h i n e t h a t s u p p o r t s m e m b e r s h i p i n m u l t i p l e g r o u p s s i m u l t a n e o u s l y , g i v e s a s p a c e s e p a r a t e d l i s t o f g r o u p s y o u a r e i n . T h e f i r s t n u m b e r i s t h e o n e r e t u r n e d b y g e t e g i d
( )
, a n d t h e s u b s e q u e n t o n e s b y g e t g r o u p s ( )
, o n e o f w h i c h m a y b e t h e s a m e a s t h e f i r s t n u m b e r .
S i m i l a r l y , a v a l u e a s s i g n e d t o $ )
m u s t a l s o b e a s p a c e - s e p a r a t e d l i s t o f n u m b e r s . T h e f i r s t n u m b e r s e t s t h e e f f e c t i v e g i d , a n d t h e r e s t ( i f a n y ) a r e p a s s e d t o s e t g r o u p s ( )
. T o g e t t h e e f f e c t o f a n e m p t y l i s t f o r s e t g r o
u p s ( )
, j u s t r e p e a t t h e n e w e f f e c t i v e g i d ; t h a t i s , t o f o r c e a n e f f e c t i v e g i d o f 5 a n d a n e f f e c t i v e l y e m p t y s e t g r o u p s ( )
l i s t , s a y $ ) = " 5 5 "
.
Y o u c a n c h a n g e b o t h t h e e f f e c t i v e g i d a n d t h e r e a l g i d a t t h e s a m e t i m e b y u s i n g P O S I X : : s e t g i
d ( )
( u s e o n l y a s i n g l e n u m e r i c a r g u m e n t ) . C h a n g e s t o $ )
r e q u i r e a c h e c k t o $ !
t o d e t e c t a n y p o s s i b l e e r r o r s a f t e r a n a t t e m p t e d c h a n g e .
$ <
, $ >
, $ (
a n d $ )
c a n b e s e t o n l y o n m a c h i n e s t h a t s u p p o r t t h e c o r r e s p o n d i n g s e t [ re ] [ ug ] i d ( ) r o u t i n e . $ (
a n d $ )
c a n b e s w a p p e d o n l y o n m a c h i n e s s u p p o r t i n g s e
t r e g i d ( )
.
M n e m o n i c : p a r e n t h e s e s a r e u s e d t o g r o u p t h i n g s . T h e e f f e c t i v e g i d i s t h e g r o u p t h a t ' s r i g h t f o r y o u , i f y o u ' r e r u n n i n g s e t g i d .
# $ R E A L _ U S E R _ I D
# $ U I D
# $ <
T h e r e a l u i d o f t h i s p r o c e s s . Y o u c a n c h a n g e b o t h t h e r e a l u i d a n d t h e e f f e c t i v e u i d a t t h e s a m e t i m e b y u s i n g P O S I X : : s e t u i d
( )
. S i n c e c h a n g e s t o $ <
r e q u i r e a s y s t e m c a l l , c h e c k $ !
a f t e r a c h a n g e a t t e m p t t o d e t e c t a n y p o s s i b l e e r r o r s .
M n e m o n i c : i t ' s t h e u i d y o u c a m e f r o m , i f y o u ' r e r u n n i n g s e t u i d .
# $ E F F E C T I V E _ U S E R _ I D
# $ E U I D
# $ >
T h e e f f e c t i v e u i d o f t h i s p r o c e s s . F o r e x a m p l e :
$< = $>; # set real to effective uid
($<,$>) = ($>,$<); # swap real and effective uids
Y o u c a n c h a n g e b o t h t h e e f f e c t i v e u i d a n d t h e r e a l u i d a t t h e s a m e t i m e b y u s i n g P O S I X : : s e t u i
d ( )
. C h a n g e s t o $ >
r e q u i r e a c h e c k t o $ !
t o d e t e c t a n y p o s s i b l e e r r o r s a f t e r a n a t t e m p t e d c h a n g e .
$ <
a n d $ >
c a n b e s w a p p e d o n l y o n m a c h i n e s s u p p o r t i n g s e t r e u i d ( )
.
M n e m o n i c : i t ' s t h e u i d y o u w e n t to , i f y o u ' r e r u n n i n g s e t u i d .
# $ S U B S C R I P T _ S E P A R A T O R
# $ S U B S E P
# $ ;
T h e s u b s c r i p t s e p a r a t o r f o r m u l t i d i m e n s i o n a l a r r a y e m u l a t i o n . I f y o u r e f e r t o a h a s h e l e m e n t a s
$foo{$x,$y,$z}
i t r e a l l y m e a n s
$foo{join($;, $x, $y, $z)}
B u t d o n ' t p u t
@foo{$x,$y,$z} # a slice--note the @
w h i c h m e a n s
($foo{$x},$foo{$y},$foo{$z})
D e f a u l t i s " \ 0 3 4 " , t h e s a m e a s S U B S E P i n a w k . I f y o u r k e y s c o n t a i n b i n a r y d a t a t h e r e m i g h t n o t b e a n y s a f e v a l u e f o r $ ;
.
C o n s i d e r u s i n g " r e a l " m u l t i d i m e n s i o n a l a r r a y s a s d e s c r i b e d i n p e r l l o l .
M n e m o n i c : c o m m a ( t h e s y n t a c t i c s u b s c r i p t s e p a r a t o r ) i s a s e m i - s e m i c o l o n .
# $ a
# $ b
S p e c i a l p a c k a g e v a r i a b l e s w h e n u s i n g s o r t ( )
, s e e " s o r t " i n p e r l f u n c . B e c a u s e o f t h i s s p e c i a l n e s s $ a
a n d $ b
d o n ' t n e e d t o b e d e c l a r e d ( u s i n g u s e v a r s
, o r o u
r ( )
) e v e n w h e n u s i n g t h e s t r i c t
' v a r s '
p r a g m a . D o n ' t l e x i c a l i z e t h e m w i t h m y $ a
or m y $ b
i f y o u w a n t t o b e a b l e t o u s e t h e m i n t h e s o r t ( )
c o m p a r i s o n b l o c k o r f u n c t i o n .
# % E N V
T h e h a s h % E N V
c o n t a i n s y o u r c u r r e n t e n v i r o n m e n t . S e t t i n g a v a l u e i n E N V
c h a n g e s t h e e n v i r o n m e n t f o r a n y c h i l d p r o c e s s e s y o u s u b s e q u e n t l y f o r k ( )
o f f .
A s o f v 5 . 1 8 . 0 , b o t h k e y s a n d v a l u e s s t o r e d i n % E N V
a r e s t r i n g i f i e d .
my $foo = 1;
$ENV{'bar'} = \$foo;
if( ref $ENV{'bar'} ) {
say "Pre 5.18.0 Behaviour";
} else {
say "Post 5.18.0 Behaviour";
}
P r e v i o u s l y , o n l y c h i l d p r o c e s s e s r e c e i v e d s t r i n g i f i e d v a l u e s :
my $foo = 1;
$ENV{'bar'} = \$foo;
# Always printed 'non ref'
system($^X, '-e',
q/print ( ref $ENV{'bar'} ? 'ref' : 'non ref' ) /);
T h i s h a p p e n s b e c a u s e y o u c a n ' t r e a l l y s h a r e a r b i t r a r y d a t a s t r u c t u r e s w i t h f o r e i g n p r o c e s s e s .
# $ O L D _ P E R L _ V E R S I O N
# $ ]
T h e r e v i s i o n , v e r s i o n , a n d s u b v e r s i o n o f t h e P e r l i n t e r p r e t e r , r e p r e s e n t e d a s a d e c i m a l o f t h e f o r m 5 . X X X Y Y Y , w h e r e X X X i s t h e v e r s i o n / 1 e 3 a n d Y Y Y i s t h e s u b v e r s i o n / 1 e 6 . F o r e x a m p l e , P e r l v 5 . 1 0 . 1 w o u l d b e " 5 . 0 1 0 0 0 1 " .
T h i s v a r i a b l e c a n b e u s e d t o d e t e r m i n e w h e t h e r t h e P e r l i n t e r p r e t e r e x e c u t i n g a s c r i p t i s i n t h e r i g h t r a n g e o f v e r s i o n s :
warn "No PerlIO!\n" if "$]" < 5.008;
W h e n c o m p a r i n g $ ]
, n u m e r i c c o m p a r i s o n o p e r a t o r s s h o u l d b e u s e d , b u t t h e v a r i a b l e s h o u l d b e s t r i n g i f i e d f i r s t t o a v o i d i s s u e s w h e r e i t s o r i g i n a l n u m e r i c v a l u e i s i n a c c u r a t e .
S e e a l s o t h e d o c u m e n t a t i o n o f u
s e V E R S I O N
a n d r e q u i r e V E R S I O N
f o r a c o n v e n i e n t w a y t o f a i l i f t h e r u n n i n g P e r l i n t e r p r e t e r i s t o o o l d .
S e e " $ ^ V " f o r a r e p r e s e n t a t i o n o f t h e P e r l v e r s i o n a s a v e r s i o n o b j e c t , w h i c h a l l o w s m o r e f l e x i b l e s t r i n g c o m p a r i s o n s .
T h e m a i n a d v a n t a g e o f $ ]
o v e r $
^ V
i s t h a t i t w o r k s t h e s a m e o n a n y v e r s i o n o f P e r l . T h e d i s a d v a n t a g e s a r e t h a t i t c a n ' t e a s i l y b e c o m p a r e d t o v e r s i o n s i n o t h e r f o r m a t s ( e . g . l i t e r a l v - s t r i n g s , " v 1 . 2 . 3 " o r v e r s i o n o b j e c t s ) a n d n u m e r i c c o m p a r i s o n s a r e s u b j e c t t o t h e b i n a r y f l o a t i n g p o i n t r e p r e s e n t a t i o n ; i t ' s g o o d f o r n u m e r i c l i t e r a l v e r s i o n c h e c k s a n d b a d f o r c o m p a r i n g t o a v a r i a b l e t h a t h a s n ' t b e e n s a n i t y - c h e c k e d .
T h e $ O L D _ P E R L _ V E R S I O N
f o r m w a s a d d e d i n P e r l v 5 . 2 0 . 0 f o r h i s t o r i c a l r e a s o n s b u t i t s u s e i s d i s c o u r a g e d . ( I f y o u r r e a s o n t o u s e $ ]
i s t o r u n c o d e o n o l d p e r l s t h e n r e f e r r i n g t o i t a s $ O L D
_ P E R L _ V E R S I O N
w o u l d b e s e l f - d e f e a t i n g . )
M n e m o n i c : I s t h i s v e r s i o n o f p e r l i n t h e r i g h t b r a c k e t ?
# $ S Y S T E M _ F D _ M A X
# $ ^ F
T h e m a x i m u m s y s t e m f i l e d e s c r i p t o r , o r d i n a r i l y 2 . S y s t e m f i l e d e s c r i p t o r s a r e p a s s e d t o e x e c ( )
e d p r o c e s s e s , w h i l e h i g h e r f i l e d e s c r i p t o r s a r e n o t . A l s o , d u r i n g a n o p e n ( )
, s y s t e m f i l e d e s c r i p t o r s a r e p r e s e r v e d e v e n i f t h e o p e n ( )
f a i l s ( o r d i n a r y f i l e d e s c r i p t o r s a r e c l o s e d b e f o r e t h e o p e n ( )
i s a t t e m p t e d ) . T h e c l o s e - o n - e x e c s t a t u s o f a f i l e d e s c r i p t o r w i l l b e d e c i d e d a c c o r d i n g t o t h e v a l u e o f $ ^ F
w h e n t h e c o r r e s p o n d i n g f i l e , p i p e , o r s o c k e t w a s o p e n e d , n o t t h e t i m e o f t h e e x e c ( )
.
# @ F
T h e a r r a y @ F
c o n t a i n s t h e f i e l d s o f e a c h l i n e r e a d i n w h e n a u t o s p l i t m o d e i s t u r n e d o n . S e e p e r l r u n f o r t h e - a s w i t c h . T h i s a r r a y i s p a c k a g e - s p e c i f i c , a n d m u s t b e d e c l a r e d o r g i v e n a f u l l p a c k a g e n a m e i f n o t i n p a c k a g e m a i n w h e n r u n n i n g u n d e r s t r i c
t ' v a r s '
.
# @ I N C
T h e a r r a y @ I N C
c o n t a i n s t h e l i s t o f p l a c e s t h a t t h e d o E X P R
, r
e q u i r e
, o r u s e
c o n s t r u c t s l o o k f o r t h e i r l i b r a r y f i l e s . I t i n i t i a l l y c o n s i s t s o f t h e a r g u m e n t s t o a n y - I c o m m a n d - l i n e s w i t c h e s , f o l l o w e d b y t h e d e f a u l t P e r l l i b r a r y , p r o b a b l y / u s r / l o c a l / l i b / p e r l . P r i o r t o P e r l 5 . 2 6 , .
- w h i c h r e p r e s e n t s t h e c u r r e n t d i r e c t o r y , w a s i n c l u d e d i n @ I N C
; i t h a s b e e n r e m o v e d . T h i s c h a n g e i n b e h a v i o r i s d o c u m e n t e d i n P E R L _ U S E _ U N S A F E _ I N C
a n d i t i s n o t r e c o m m e n d e d t h a t .
b e r e - a d d e d t o @ I N C
. I f y o u n e e d t o m o d i f y @ I N C
a t r u n t i m e , y o u s h o u l d u s e t h e u s e l i b
p r a g m a t o g e t t h e m a c h i n e - d e p e n d e n t l i b r a r y p r o p e r l y l o a d e d a s w e l l :
use lib '/mypath/libdir/';
use SomeMod;
Y o u c a n a l s o i n s e r t h o o k s i n t o t h e f i l e i n c l u s i o n s y s t e m b y p u t t i n g P e r l c o d e d i r e c t l y i n t o @
I N C
. T h o s e h o o k s m a y b e s u b r o u t i n e r e f e r e n c e s , a r r a y r e f e r e n c e s o r b l e s s e d o b j e c t s . S e e " r e q u i r e " i n p e r l f u n c f o r d e t a i l s .
# % I N C
T h e h a s h % I N C
c o n t a i n s e n t r i e s f o r e a c h f i l e n a m e i n c l u d e d v i a t h e do
, r e q u i r e
, o r u s e
o p e r a t o r s . T h e k e y i s t h e f i l e n a m e y o u s p e c i f i e d ( w i t h m o d u l e n a m e s c o n v e r t e d t o p a t h n a m e s ) , a n d t h e v a l u e i s t h e l o c a t i o n o f t h e f i l e f o u n d . T h e r e q u i r e
o p e r a t o r u s e s t h i s h a s h t o d e t e r m i n e w h e t h e r a p a r t i c u l a r f i l e h a s a l r e a d y b e e n i n c l u d e d .
I f t h e f i l e w a s l o a d e d v i a a h o o k ( e . g . a s u b r o u t i n e r e f e r e n c e , s e e " r e q u i r e " i n p e r l f u n c f o r a d e s c r i p t i o n o f t h e s e h o o k s ) , t h i s h o o k i s b y d e f a u l t i n s e r t e d i n t o % I N C
i n p l a c e o f a f i l e n a m e . N o t e , h o w e v e r , t h a t t h e h o o k m a y h a v e s e t t h e % I N C
e n t r y b y i t s e l f t o p r o v i d e s o m e m o r e s p e c i f i c i n f o .
# $ I N P L A C E _ E D I T
# $ ^ I
T h e c u r r e n t v a l u e o f t h e i n p l a c e - e d i t e x t e n s i o n . U s e u n d e f
t o d i s a b l e i n p l a c e e d i t i n g .
M n e m o n i c : v a l u e o f - i s w i t c h .
# @ I S A
E a c h p a c k a g e c o n t a i n s a s p e c i a l a r r a y c a l l e d @ I S A
w h i c h c o n t a i n s a l i s t o f t h a t c l a s s ' s p a r e n t c l a s s e s , i f a n y . T h i s a r r a y i s s i m p l y a l i s t o f s c a l a r s , e a c h o f w h i c h i s a s t r i n g t h a t c o r r e s p o n d s t o a p a c k a g e n a m e . T h e a r r a y i s e x a m i n e d w h e n P e r l d o e s m e t h o d r e s o l u t i o n , w h i c h i s c o v e r e d i n p e r l o b j .
T o l o a d p a c k a g e s w h i l e a d d i n g t h e m t o @ I S A
, s e e t h e p a r e n t p r a g m a . T h e d i s c o u r a g e d b a s e p r a g m a d o e s t h i s a s w e l l , b u t s h o u l d n o t b e u s e d e x c e p t w h e n c o m p a t i b i l i t y w i t h t h e d i s c o u r a g e d f i e l d s p r a g m a i s r e q u i r e d .
# $ ^ M
B y d e f a u l t , r u n n i n g o u t o f m e m o r y i s a n u n t r a p p a b l e , f a t a l e r r o r . H o w e v e r , i f s u i t a b l y b u i l t , P e r l c a n u s e t h e c o n t e n t s o f $
^ M
a s a n e m e r g e n c y m e m o r y p o o l a f t e r d i e ( )
i n g . S u p p o s e t h a t y o u r P e r l w e r e c o m p i l e d w i t h - D P E
R L _ E M E R G E N C Y _ S B R K
a n d u s e d P e r l ' s m a l l o c . T h e n
$^M = 'a' x (1 <<16 );
w o u l d a l l o c a t e a 6 4 K b u f f e r f o r u s e i n a n e m e r g e n c y . S e e t h e I N S T A L L f i l e i n t h e P e r l d i s t r i b u t i o n f o r i n f o r m a t i o n o n h o w t o a d d c u s t o m C c o m p i l a t i o n f l a g s w h e n c o m p i l i n g p e r l . T o d i s c o u r a g e c a s u a l u s e o f t h i s a d v a n c e d f e a t u r e , t h e r e i s n o E n g l i s h l o n g n a m e f o r t h i s v a r i a b l e .
T h i s v a r i a b l e w a s a d d e d i n P e r l 5 . 0 0 4 .
# $ { ^ M A X _ N E S T E D _ E V A L _ B E G I N _ B L O C K S }
T h i s v a r i a b l e d e t e r m i n e s t h e m a x i m u m n u m b e r e v a l E X P R
/ B E G I N
or r e
q u i r e
/ B E G I N
b l o c k n e s t i n g t h a t i s a l l o w e d . T h i s m e a n s i t a l s o c o n t r o l s t h e m a x i m u m n e s t i n g o f u s e
s t a t e m e n t s a s w e l l .
T h e d e f a u l t o f 1 0 0 0 s h o u l d b e s u f f i c i e n t l y l a r g e f o r n o r m a l w o r k i n g p u r p o s e s , a n d i f y o u m u s t r a i s e i t t h e n y o u s h o u l d b e c o n s e r v a t i v e w i t h y o u r c h o i c e o r y o u m a y e n c o u n t e r s e g f a u l t s f r o m e x h a u s t i o n o f t h e C s t a c k . I t s e e m s u n l i k e l y t h a t r e a l c o d e h a s a u s e d e p t h a b o v e 1 0 0 0 , b u t w e h a v e l e f t t h i s c o n f i g u r a b l e j u s t i n c a s e .
W h e n s e t t o 0
t h e n B E G I N
b l o c k s i n s i d e o f e v a l E X P R
or r e q u i r e E X
P R
a r e f o r b i d d e n e n t i r e l y a n d w i l l t r i g g e r a n e x c e p t i o n w h i c h w i l l t e r m i n a t e t h e c o m p i l a t i o n a n d i n t h e c a s e o f r e q u i r e
w i l l t h r o w a n e x c e p t i o n , o r i n t h e c a s e o f e v a l
r e t u r n t h e e r r o r i n $ @
a s u s u a l .
C o n s i d e r t h e c o d e
perl -le'sub f { eval "BEGIN { f() }"; } f()'
e a c h i n v o c a t i o n o f f ( )
w i l l c o n s u m e c o n s i d e r a b l e C s t a c k , a n d t h i s v a r i a b l e i s u s e d t o c a u s e c o d e l i k e t h i s t o d i e i n s t e a d o f e x h a u s t i n g t h e C s t a c k a n d t r i g g e r i n g a s e g f a u l t . N e e d l e s s t o s a y c o d e l i k e t h i s i s u n u s u a l , i t i s u n l i k e l y y o u w i l l a c t u a l l y n e e d t o r a i s e t h e s e t t i n g . H o w e v e r i t m a y b e u s e f u l t o s e t i t t o 0 f o r a l i m i t e d t i m e p e r i o d t o p r e v e n t B E G I N { } b l o c k s f r o m b e i n g e x e c u t e d d u r i n g a n e v a
l E X P R
.
N o t e t h a t s e t t i n g t h i s t o 1 w o u l d N O T a f f e c t c o d e l i k e t h i s :
BEGIN { $n += 1; BEGIN { $n += 2; BEGIN { $n += 4 } } }
T h e r e a s o n i s t h a t B E G I N b l o c k s a r e e x e c u t e d i m m e d i a t e l y a f t e r t h e y a r e c o m p l e t e d , t h u s t h e i n n e r m o s t w i l l e x e c u t e b e f o r e t h e o n e s w h i c h c o n t a i n i t h a v e e v e n f i n i s h e d c o m p i l i n g , a n d t h e d e p t h w i l l n o t g o a b o v e 1 . I n f a c t t h e a b o v e c o d e i s e q u i v a l e n t t o
BEGIN { $n+=4 }
BEGIN { $n+=2 }
BEGIN { $n+=1 }
w h i c h m a k e s i t o b v i o u s w h y a $ { ^ M A X _ E V A L _ B E G I N _ D E P T H } o f 1 w o u l d n o t b l o c k t h i s c o d e .
O n l y B E G I N
' s e x e c u t e d i n s i d e o f a n e v a l
or r e q u i r e
( p o s s i b l y v i a u s e
) a r e a f f e c t e d .
# $ O S N A M E
# $ ^ O
T h e n a m e o f t h e o p e r a t i n g s y s t e m u n d e r w h i c h t h i s c o p y o f P e r l w a s b u i l t , a s d e t e r m i n e d d u r i n g t h e c o n f i g u r a t i o n p r o c e s s . F o r e x a m p l e s s e e " P L A T F O R M S " i n p e r l p o r t .
T h e v a l u e i s i d e n t i c a l t o $ C o n f
i g { ' o s n a m e ' }
. S e e a l s o C o n f i g a n d t h e - V 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 p e r l r u n .
I n W i n d o w s p l a t f o r m s , $ ^ O
i s n o t v e r y h e l p f u l : s i n c e i t i s a l w a y s M S W i n 3 2
, i t d o e s n ' t t e l l t h e d i f f e r e n c e b e t w e e n 9 5 / 9 8 / M E / N T / 2 0 0 0 / X P / C E / . N E T . U s e W i n 3 2 : : G
e t O S N a m e ( )
o r W i n 3 2 : : G e t O S V e r s i o n ( ) ( s e e W i n 3 2 a n d p e r l p o r t ) t o d i s t i n g u i s h b e t w e e n t h e v a r i a n t s .
T h i s v a r i a b l e w a s a d d e d i n P e r l 5 . 0 0 3 .
# % S I G
T h e h a s h % S I G
c o n t a i n s s i g n a l h a n d l e r s f o r s i g n a l s . F o r e x a m p l e :
sub handler { # 1st argument is signal name
my($sig) = @_;
print "Caught a SIG$sig--shutting down\n";
close(LOG);
exit(0);
}
$SIG{'INT'} = \&handler;
$SIG{'QUIT'} = \&handler;
...
$SIG{'INT'} = 'DEFAULT'; # restore default action
$SIG{'QUIT'} = 'IGNORE'; # ignore SIGQUIT
U s i n g a v a l u e o f ' I G N O R E '
u s u a l l y h a s t h e e f f e c t o f i g n o r i n g t h e s i g n a l , e x c e p t f o r t h e C H L D
s i g n a l . S e e p e r l i p c f o r m o r e a b o u t t h i s s p e c i a l c a s e . U s i n g a n e m p t y s t r i n g o r u n d e f
a s t h e v a l u e h a s t h e s a m e e f f e c t a s ' D E
F A U L T '
.
H e r e a r e s o m e o t h e r e x a m p l e s :
$SIG{"PIPE"} = "Plumber"; # assumes main::Plumber (not
# recommended)
$SIG{"PIPE"} = \&Plumber; # just fine; assume current
# Plumber
$SIG{"PIPE"} = *Plumber; # somewhat esoteric
$SIG{"PIPE"} = Plumber(); # oops, what did Plumber()
# return??
B e s u r e n o t t o u s e a b a r e w o r d a s t h e n a m e o f a s i g n a l h a n d l e r , l e s t y o u i n a d v e r t e n t l y c a l l i t .
U s i n g a s t r i n g t h a t d o e s n ' t c o r r e s p o n d t o a n y e x i s t i n g f u n c t i o n o r a g l o b t h a t d o e s n ' t c o n t a i n a c o d e s l o t i s e q u i v a l e n t t o ' I G N O R E '
, b u t a w a r n i n g i s e m i t t e d w h e n t h e h a n d l e r i s b e i n g c a l l e d ( t h e w a r n i n g i s n o t e m i t t e d f o r t h e i n t e r n a l h o o k s d e s c r i b e d b e l o w ) .
I f y o u r s y s t e m h a s t h e s i g a c t i o
n ( )
f u n c t i o n t h e n s i g n a l h a n d l e r s a r e i n s t a l l e d u s i n g i t . T h i s m e a n s y o u g e t r e l i a b l e s i g n a l h a n d l i n g .
T h e d e f a u l t d e l i v e r y p o l i c y o f s i g n a l s c h a n g e d i n P e r l v 5 . 8 . 0 f r o m i m m e d i a t e ( a l s o k n o w n a s " u n s a f e " ) t o d e f e r r e d , a l s o k n o w n a s " s a f e s i g n a l s " . S e e p e r l i p c f o r m o r e i n f o r m a t i o n .
C e r t a i n i n t e r n a l h o o k s c a n b e a l s o s e t u s i n g t h e % S I G
h a s h . T h e r o u t i n e i n d i c a t e d b y $ S I G { _ _ W
A R N _ _ }
i s c a l l e d w h e n a w a r n i n g m e s s a g e i s a b o u t t o b e p r i n t e d . T h e w a r n i n g m e s s a g e i s p a s s e d a s t h e f i r s t a r g u m e n t . T h e p r e s e n c e o f a _ _ W A R N _ _
h o o k c a u s e s t h e o r d i n a r y p r i n t i n g o f w a r n i n g s t o S T D E R R
t o b e s u p p r e s s e d . Y o u c a n u s e t h i s t o s a v e w a r n i n g s i n a v a r i a b l e , o r t u r n w a r n i n g s i n t o f a t a l e r r o r s , l i k e t h i s :
local $SIG{__WARN__} = sub { die $_[0] };
eval $proggie;
A s t h e ' I G N O R E '
h o o k i s n o t s u p p o r t e d b y _ _ W A R N _ _
, i t s e f f e c t i s t h e s a m e a s u s i n g ' D E F A U L T '
. Y o u c a n d i s a b l e w a r n i n g s u s i n g t h e e m p t y s u b r o u t i n e :
local $SIG{__WARN__} = sub {};
T h e r o u t i n e i n d i c a t e d b y $ S I G { _
_ D I E _ _ }
i s c a l l e d w h e n a f a t a l e x c e p t i o n i s a b o u t t o b e t h r o w n . T h e e r r o r m e s s a g e i s p a s s e d a s t h e f i r s t a r g u m e n t . W h e n a _ _ D
I E _ _
h o o k r o u t i n e r e t u r n s , t h e e x c e p t i o n p r o c e s s i n g c o n t i n u e s a s i t w o u l d h a v e i n t h e a b s e n c e o f t h e h o o k , u n l e s s t h e h o o k r o u t i n e i t s e l f e x i t s v i a a g o t o
& s u b
, a l o o p e x i t , o r a d i e ( )
. T h e _ _ D I E _ _
h a n d l e r i s e x p l i c i t l y d i s a b l e d d u r i n g t h e c a l l , s o t h a t y o u c a n d i e f r o m a _ _ D I E _
_
h a n d l e r . S i m i l a r l y f o r _ _ W A R N
_ _
.
T h e $ S I G { _ _ D I E _ _ }
h o o k i s c a l l e d e v e n i n s i d e a n e v a l ( )
. I t w a s n e v e r i n t e n d e d t o h a p p e n t h i s w a y , b u t a n i m p l e m e n t a t i o n g l i t c h m a d e t h i s p o s s i b l e . T h i s u s e d t o b e d e p r e c a t e d , a s i t a l l o w e d s t r a n g e a c t i o n a t a d i s t a n c e l i k e r e w r i t i n g a p e n d i n g e x c e p t i o n i n $ @
. P l a n s t o r e c t i f y t h i s h a v e b e e n s c r a p p e d , a s u s e r s f o u n d t h a t r e w r i t i n g a p e n d i n g e x c e p t i o n i s a c t u a l l y a u s e f u l f e a t u r e , a n d n o t a b u g .
T h e $ S I G { _ _ D I E _ _ }
d o e s n ' t s u p p o r t ' I G N O R E '
; i t h a s t h e s a m e e f f e c t a s ' D E F A U L T '
.
_ _ D I E _ _
/ _ _ W A R N _ _
h a n d l e r s a r e v e r y s p e c i a l i n o n e r e s p e c t : t h e y m a y b e c a l l e d t o r e p o r t ( p r o b a b l e ) e r r o r s f o u n d b y t h e p a r s e r . I n s u c h a c a s e t h e p a r s e r m a y b e i n i n c o n s i s t e n t s t a t e , s o a n y a t t e m p t t o e v a l u a t e P e r l c o d e f r o m s u c h a h a n d l e r w i l l p r o b a b l y r e s u l t i n a s e g f a u l t . T h i s m e a n s t h a t w a r n i n g s o r e r r o r s t h a t r e s u l t f r o m p a r s i n g P e r l s h o u l d b e u s e d w i t h e x t r e m e c a u t i o n , l i k e t h i s :
require Carp if defined $^S;
Carp::confess("Something wrong") if defined &Carp::confess;
die "Something wrong, but could not load Carp to give "
. "backtrace...\n\t"
. "To see backtrace try starting Perl with -MCarp switch";
H e r e t h e f i r s t l i n e w i l l l o a d C
a r p
u n l e s s i t i s t h e p a r s e r w h o c a l l e d t h e h a n d l e r . T h e s e c o n d l i n e w i l l p r i n t b a c k t r a c e a n d d i e i f C a r p
w a s a v a i l a b l e . T h e t h i r d l i n e w i l l b e e x e c u t e d o n l y i f C a r p
w a s n o t a v a i l a b l e .
H a v i n g t o e v e n t h i n k a b o u t t h e $ ^ S
v a r i a b l e i n y o u r e x c e p t i o n h a n d l e r s i s s i m p l y w r o n g . $ S I G {
_ _ D I E _ _ }
a s c u r r e n t l y i m p l e m e n t e d i n v i t e s g r i e v o u s a n d d i f f i c u l t t o t r a c k d o w n e r r o r s . A v o i d i t a n d u s e a n E N D { }
o r C O R E : : G L O B A L : : d i e o v e r r i d e i n s t e a d .
S e e " d i e " i n p e r l f u n c , " w a r n " i n p e r l f u n c , " e v a l " i n p e r l f u n c , a n d w a r n i n g s f o r a d d i t i o n a l i n f o r m a t i o n .
# $ B A S E T I M E
# $ ^ T
T h e t i m e a t w h i c h t h e p r o g r a m b e g a n r u n n i n g , i n s e c o n d s s i n c e t h e e p o c h ( b e g i n n i n g o f 1 9 7 0 ) . T h e v a l u e s r e t u r n e d b y t h e - M , - A , a n d - C f i l e t e s t s a r e b a s e d o n t h i s v a l u e .
# $ P E R L _ V E R S I O N
# $ ^ V
T h e r e v i s i o n , v e r s i o n , a n d s u b v e r s i o n o f t h e P e r l i n t e r p r e t e r , r e p r e s e n t e d a s a v e r s i o n o b j e c t .
T h i s v a r i a b l e f i r s t a p p e a r e d i n p e r l v 5 . 6 . 0 ; e a r l i e r v e r s i o n s o f p e r l w i l l s e e a n u n d e f i n e d v a l u e . B e f o r e p e r l v 5 . 1 0 . 0 $ ^ V
w a s r e p r e s e n t e d a s a v - s t r i n g r a t h e r t h a n a v e r s i o n o b j e c t .
$ ^ V
c a n b e u s e d t o d e t e r m i n e w h e t h e r t h e P e r l i n t e r p r e t e r e x e c u t i n g a s c r i p t i s i n t h e r i g h t r a n g e o f v e r s i o n s . F o r e x a m p l e :
warn "Hashes not randomized!\n" if !$^V or $^V lt v5.8.1
W h i l e v e r s i o n o b j e c t s o v e r l o a d s t r i n g i f i c a t i o n , t o p o r t a b l y c o n v e r t $ ^ V
i n t o i t s s t r i n g r e p r e s e n t a t i o n , u s e s p r i n t f ( )
' s " % v d "
c o n v e r s i o n , w h i c h w o r k s f o r b o t h v - s t r i n g s o r v e r s i o n o b j e c t s :
printf "version is v%vd\n", $^V; # Perl's version
S e e t h e d o c u m e n t a t i o n o f u s e V E
R S I O N
a n d r e q u i r e V E R S I O N
f o r a c o n v e n i e n t w a y t o f a i l i f t h e r u n n i n g P e r l i n t e r p r e t e r i s t o o o l d .
S e e a l s o " $ ] "
f o r a d e c i m a l r e p r e s e n t a t i o n o f t h e P e r l v e r s i o n .
T h e m a i n a d v a n t a g e o f $ ^ V
o v e r $ ]
i s t h a t , f o r P e r l v 5 . 1 0 . 0 o r l a t e r , i t o v e r l o a d s o p e r a t o r s , a l l o w i n g e a s y c o m p a r i s o n a g a i n s t o t h e r v e r s i o n r e p r e s e n t a t i o n s ( e . g . d e c i m a l , l i t e r a l v - s t r i n g , " v 1 . 2 . 3 " , o r o b j e c t s ) . T h e d i s a d v a n t a g e i s t h a t p r i o r t o v 5 . 1 0 . 0 , i t w a s o n l y a l i t e r a l v - s t r i n g , w h i c h c a n ' t b e e a s i l y p r i n t e d o r c o m p a r e d , w h e r e a s t h e b e h a v i o r o f $ ]
i s u n c h a n g e d o n a l l v e r s i o n s o f P e r l .
M n e m o n i c : u s e ^ V f o r a v e r s i o n o b j e c t .
# $ E X E C U T A B L E _ N A M E
# $ ^ X
T h e n a m e u s e d t o e x e c u t e t h e c u r r e n t c o p y o f P e r l , f r o m C ' s a r g
v [ 0 ]
o r ( w h e r e s u p p o r t e d ) / p r o c / s e l f / e x e .
D e p e n d i n g o n t h e h o s t o p e r a t i n g s y s t e m , t h e v a l u e o f $ ^ X
m a y b e a r e l a t i v e o r a b s o l u t e p a t h n a m e o f t h e p e r l p r o g r a m f i l e , o r m a y b e t h e s t r i n g u s e d t o i n v o k e p e r l b u t n o t t h e p a t h n a m e o f t h e p e r l p r o g r a m f i l e . A l s o , m o s t o p e r a t i n g s y s t e m s p e r m i t i n v o k i n g p r o g r a m s t h a t a r e n o t i n t h e P A T H e n v i r o n m e n t v a r i a b l e , s o t h e r e i s n o g u a r a n t e e t h a t t h e v a l u e o f $ ^ X
i s i n P A T H . F o r V M S , t h e v a l u e m a y o r m a y n o t i n c l u d e a v e r s i o n n u m b e r .
Y o u u s u a l l y c a n u s e t h e v a l u e o f $ ^ X
t o r e - i n v o k e a n i n d e p e n d e n t c o p y o f t h e s a m e p e r l t h a t i s c u r r e n t l y r u n n i n g , e . g . ,
@first_run = `$^X -le "print int rand 100 for 1..100"`;
B u t r e c a l l t h a t n o t a l l o p e r a t i n g s y s t e m s s u p p o r t f o r k i n g o r c a p t u r i n g o f t h e o u t p u t o f c o m m a n d s , s o t h i s c o m p l e x s t a t e m e n t m a y n o t b e p o r t a b l e .
I t i s n o t s a f e t o u s e t h e v a l u e o f $ ^ X
a s a p a t h n a m e o f a f i l e , a s s o m e o p e r a t i n g s y s t e m s t h a t h a v e a m a n d a t o r y s u f f i x o n e x e c u t a b l e f i l e s d o n o t r e q u i r e u s e o f t h e s u f f i x w h e n i n v o k i n g a c o m m a n d . T o c o n v e r t t h e v a l u e o f $ ^ X
t o a p a t h n a m e , u s e t h e f o l l o w i n g s t a t e m e n t s :
# Build up a set of file names (not command names).
use Config;
my $this_perl = $^X;
if ($^O ne 'VMS') {
$this_perl .= $Config{_exe}
unless $this_perl =~ m/$Config{_exe}$/i;
}
B e c a u s e m a n y o p e r a t i n g s y s t e m s p e r m i t a n y o n e w i t h r e a d a c c e s s t o t h e P e r l p r o g r a m f i l e t o m a k e a c o p y o f i t , p a t c h t h e c o p y , a n d t h e n e x e c u t e t h e c o p y , t h e s e c u r i t y - c o n s c i o u s P e r l p r o g r a m m e r s h o u l d t a k e c a r e t o i n v o k e t h e i n s t a l l e d c o p y o f p e r l , n o t t h e c o p y r e f e r e n c e d b y $ ^ X
. T h e f o l l o w i n g s t a t e m e n t s a c c o m p l i s h t h i s g o a l , a n d p r o d u c e a p a t h n a m e t h a t c a n b e i n v o k e d a s a c o m m a n d o r r e f e r e n c e d a s a f i l e .
use Config;
my $secure_perl_path = $Config{perlpath};
if ($^O ne 'VMS') {
$secure_perl_path .= $Config{_exe}
unless $secure_perl_path =~ m/$Config{_exe}$/i;
}
M o s t o f t h e s p e c i a l v a r i a b l e s r e l a t e d t o r e g u l a r e x p r e s s i o n s a r e s i d e e f f e c t s . P e r l s e t s t h e s e v a r i a b l e s w h e n i t h a s a s u c c e s s f u l m a t c h , s o y o u s h o u l d c h e c k t h e m a t c h r e s u l t b e f o r e u s i n g t h e m . F o r i n s t a n c e :
if( /P(A )TT(ER )N/ ) {
print "I found $1 and $2\n";
}
T h e s e v a r i a b l e s a r e r e a d - o n l y a n d d y n a m i c a l l y - s c o p e d , u n l e s s w e n o t e o t h e r w i s e .
T h e d y n a m i c n a t u r e o f t h e r e g u l a r e x p r e s s i o n v a r i a b l e s m e a n s t h a t t h e i r v a l u e i s l i m i t e d t o t h e b l o c k t h a t t h e y a r e i n , a s d e m o n s t r a t e d b y t h i s b i t o f c o d e :
my $outer = 'Wallace and Grommit';
my $inner = 'Mutt and Jeff';
my $pattern = qr/(\S+) and (\S+)/;
sub show_n { print "\$1 is $1; \$2 is $2\n" }
{
OUTER:
show_n() if $outer =~ m/$pattern/;
INNER: {
show_n() if $inner =~ m/$pattern/;
}
show_n();
}
T h e o u t p u t s h o w s t h a t w h i l e i n t h e O U T E R
b l o c k , t h e v a l u e s o f $ 1
a n d $ 2
a r e f r o m t h e m a t c h a g a i n s t $ o u t e r
. I n s i d e t h e I N N E R
b l o c k , t h e v a l u e s o f $ 1
a n d $ 2
a r e f r o m t h e m a t c h a g a i n s t $ i n n e r
, b u t o n l y u n t i l t h e e n d o f t h e b l o c k ( i . e . t h e d y n a m i c s c o p e ) . A f t e r t h e I N N E R
b l o c k c o m p l e t e s , t h e v a l u e s o f $ 1
a n d $ 2
r e t u r n t o t h e v a l u e s f o r t h e m a t c h a g a i n s t $ o u t e r
e v e n t h o u g h w e h a v e n o t m a d e a n o t h e r m a t c h :
$1 is Wallace; $2 is Grommit
$1 is Mutt; $2 is Jeff
$1 is Wallace; $2 is Grommit
T r a d i t i o n a l l y i n P e r l , a n y u s e o f a n y o f t h e t h r e e v a r i a b l e s $ `
, $ &
or $ '
( o r t h e i r u s e E n g l i s h
e q u i v a l e n t s ) a n y w h e r e i n t h e c o d e , c a u s e d a l l s u b s e q u e n t s u c c e s s f u l p a t t e r n m a t c h e s t o m a k e a c o p y o f t h e m a t c h e d s t r i n g , i n c a s e t h e c o d e m i g h t s u b s e q u e n t l y a c c e s s o n e o f t h o s e v a r i a b l e s . T h i s i m p o s e d a c o n s i d e r a b l e p e r f o r m a n c e p e n a l t y a c r o s s t h e w h o l e p r o g r a m , s o g e n e r a l l y t h e u s e o f t h e s e v a r i a b l e s h a s b e e n d i s c o u r a g e d .
I n P e r l 5 . 6 . 0 t h e @ -
a n d @ +
d y n a m i c a r r a y s w e r e i n t r o d u c e d t h a t s u p p l y t h e i n d i c e s o f s u c c e s s f u l m a t c h e s . S o y o u c o u l d f o r e x a m p l e d o t h i s :
$str =~ /pattern/;
print $`, $&, $'; # bad: performance hit
print # good: no performance hit
substr($str, 0, $-[0]),
substr($str, $-[0], $+[0]-$-[0]),
substr($str, $+[0]);
I n P e r l 5 . 1 0 . 0 t h e / p
m a t c h o p e r a t o r f l a g a n d t h e $ { ^ P R E M A T C H }
, $ { ^ M A T C H }
, a n d $ { ^ P O S T M A T C H }
v a r i a b l e s w e r e i n t r o d u c e d , t h a t a l l o w e d y o u t o s u f f e r t h e p e n a l t i e s o n l y o n p a t t e r n s m a r k e d w i t h / p
.
I n P e r l 5 . 1 8 . 0 o n w a r d s , p e r l s t a r t e d n o t i n g t h e p r e s e n c e o f e a c h o f t h e t h r e e v a r i a b l e s s e p a r a t e l y , a n d o n l y c o p i e d t h a t p a r t o f t h e s t r i n g r e q u i r e d ; s o i n
$`; $&; "abcdefgh" =~ /d/
p e r l w o u l d o n l y c o p y t h e " a b c d " p a r t o f t h e s t r i n g . T h a t c o u l d m a k e a b i g d i f f e r e n c e i n s o m e t h i n g l i k e
$str = 'x' x 1_000_000;
$&; # whoops
$str =~ /x/g # one char copied a million times, not a million chars
I n P e r l 5 . 2 0 . 0 a n e w c o p y - o n - w r i t e s y s t e m w a s e n a b l e d b y d e f a u l t , w h i c h f i n a l l y f i x e s a l l p e r f o r m a n c e i s s u e s w i t h t h e s e t h r e e v a r i a b l e s , a n d m a k e s t h e m s a f e t o u s e a n y w h e r e .
T h e D e v e l : : N Y T P r o f
a n d D e v e l : : F i
n d A m p e r s a n d
m o d u l e s c a n h e l p y o u f i n d u s e s o f t h e s e p r o b l e m a t i c m a t c h v a r i a b l e s i n y o u r c o d e .
# $ < d i g i t s > ( $ 1 , $ 2 , . . . )
C o n t a i n s t h e s u b p a t t e r n f r o m t h e c o r r e s p o n d i n g s e t o f c a p t u r i n g p a r e n t h e s e s f r o m t h e l a s t s u c c e s s f u l p a t t e r n m a t c h , n o t c o u n t i n g p a t t e r n s m a t c h e d i n n e s t e d b l o c k s t h a t h a v e b e e n e x i t e d a l r e a d y .
N o t e t h e r e i s a d i s t i n c t i o n b e t w e e n a c a p t u r e b u f f e r w h i c h m a t c h e s t h e e m p t y s t r i n g a c a p t u r e b u f f e r w h i c h i s o p t i o n a l . E g , ( x ? )
a n d ( x ) ?
T h e l a t t e r m a y b e u n d e f , t h e f o r m e r n o t .
T h e s e v a r i a b l e s a r e r e a d - o n l y a n d d y n a m i c a l l y - s c o p e d .
M n e m o n i c : l i k e \ d i g i t s .
# @ { ^ C A P T U R E }
A n a r r a y w h i c h e x p o s e s t h e c o n t e n t s o f t h e c a p t u r e b u f f e r s , i f a n y , o f t h e l a s t s u c c e s s f u l p a t t e r n m a t c h , n o t c o u n t i n g p a t t e r n s m a t c h e d i n n e s t e d b l o c k s t h a t h a v e b e e n e x i t e d a l r e a d y .
N o t e t h a t t h e 0 i n d e x o f @ { ^ C A P T U R E } i s e q u i v a l e n t t o $ 1 , t h e 1 i n d e x i s e q u i v a l e n t t o $ 2 , e t c .
if ("foal"=~/(.)(.)(.)(.)/) {
print join "-", @{^CAPTURE};
}
s h o u l d o u t p u t " f - o - a - l " .
S e e a l s o " $ < d i g i t s > ( $ 1 , $ 2 , . . . ) " , " % { ^ C A P T U R E } " a n d " % { ^ C A P T U R E _ A L L } " .
N o t e t h a t u n l i k e m o s t o t h e r r e g e x m a g i c v a r i a b l e s t h e r e i s n o s i n g l e l e t t e r e q u i v a l e n t t o @ { ^
C A P T U R E }
. A l s o b e a w a r e t h a t w h e n i n t e r p o l a t i n g s u b s c r i p t s o f t h i s a r r a y y o u m u s t u s e t h e d e m a r c a t e d v a r i a b l e f o r m , f o r i n s t a n c e
print "${^CAPTURE[0]}"
s e e " D e m a r c a t e d v a r i a b l e n a m e s u s i n g b r a c e s " i n p e r l d a t a f o r m o r e i n f o r m a t i o n o n t h i s f o r m a n d i t s u s e s .
T h i s v a r i a b l e w a s a d d e d i n 5 . 2 5 . 7
# $ M A T C H
# $ &
T h e s t r i n g m a t c h e d b y t h e l a s t s u c c e s s f u l p a t t e r n m a t c h ( n o t c o u n t i n g a n y m a t c h e s h i d d e n w i t h i n a B L O C K o r e v a l ( )
e n c l o s e d b y t h e c u r r e n t B L O C K ) .
S e e " P e r f o r m a n c e i s s u e s " a b o v e f o r t h e s e r i o u s p e r f o r m a n c e i m p l i c a t i o n s o f u s i n g t h i s v a r i a b l e ( e v e n o n c e ) i n y o u r c o d e .
T h i s v a r i a b l e i s r e a d - o n l y a n d d y n a m i c a l l y - s c o p e d .
M n e m o n i c : l i k e &
i n s o m e e d i t o r s .
# $ { ^ M A T C H }
T h i s i s s i m i l a r t o $ &
( $ M A T C H
) e x c e p t t h a t i t d o e s n o t i n c u r t h e p e r f o r m a n c e p e n a l t y a s s o c i a t e d w i t h t h a t v a r i a b l e .
S e e " P e r f o r m a n c e i s s u e s " a b o v e .
I n P e r l v 5 . 1 8 a n d e a r l i e r , i t i s o n l y g u a r a n t e e d t o r e t u r n a d e f i n e d v a l u e w h e n t h e p a t t e r n w a s c o m p i l e d o r e x e c u t e d w i t h t h e / p
m o d i f i e r . I n P e r l v 5 . 2 0 , t h e / p
m o d i f i e r d o e s n o t h i n g , s o $ { ^ M A T C H }
d o e s t h e s a m e t h i n g a s $ M A T C H
.
T h i s v a r i a b l e w a s a d d e d i n P e r l v 5 . 1 0 . 0 .
T h i s v a r i a b l e i s r e a d - o n l y a n d d y n a m i c a l l y - s c o p e d .
# $ P R E M A T C H
# $ `
T h e s t r i n g p r e c e d i n g w h a t e v e r w a s m a t c h e d b y t h e l a s t s u c c e s s f u l p a t t e r n m a t c h , n o t c o u n t i n g a n y m a t c h e s h i d d e n w i t h i n a B L O C K o r e v a l
e n c l o s e d b y t h e c u r r e n t B L O C K .
S e e " P e r f o r m a n c e i s s u e s " a b o v e f o r t h e s e r i o u s p e r f o r m a n c e i m p l i c a t i o n s o f u s i n g t h i s v a r i a b l e ( e v e n o n c e ) i n y o u r c o d e .
T h i s v a r i a b l e i s r e a d - o n l y a n d d y n a m i c a l l y - s c o p e d .
M n e m o n i c : `
o f t e n p r e c e d e s a q u o t e d s t r i n g .
# $ { ^ P R E M A T C H }
T h i s i s s i m i l a r t o $ `
( $ P R E M A T C H ) e x c e p t t h a t i t d o e s n o t i n c u r t h e p e r f o r m a n c e p e n a l t y a s s o c i a t e d w i t h t h a t v a r i a b l e .
S e e " P e r f o r m a n c e i s s u e s " a b o v e .
I n P e r l v 5 . 1 8 a n d e a r l i e r , i t i s o n l y g u a r a n t e e d t o r e t u r n a d e f i n e d v a l u e w h e n t h e p a t t e r n w a s c o m p i l e d o r e x e c u t e d w i t h t h e / p
m o d i f i e r . I n P e r l v 5 . 2 0 , t h e / p
m o d i f i e r d o e s n o t h i n g , s o $ { ^ P R E M A T C H }
d o e s t h e s a m e t h i n g a s $ P R E M A T C H
.
T h i s v a r i a b l e w a s a d d e d i n P e r l v 5 . 1 0 . 0 .
T h i s v a r i a b l e i s r e a d - o n l y a n d d y n a m i c a l l y - s c o p e d .
# $ P O S T M A T C H
# $ '
T h e s t r i n g f o l l o w i n g w h a t e v e r w a s m a t c h e d b y t h e l a s t s u c c e s s f u l p a t t e r n m a t c h ( n o t c o u n t i n g a n y m a t c h e s h i d d e n w i t h i n a B L O C K o r e v a l ( )
e n c l o s e d b y t h e c u r r e n t B L O C K ) . E x a m p l e :
local $_ = 'abcdefghi';
/def/;
print "$`:$&:$'\n"; # prints abc:def:ghi
S e e " P e r f o r m a n c e i s s u e s " a b o v e f o r t h e s e r i o u s p e r f o r m a n c e i m p l i c a t i o n s o f u s i n g t h i s v a r i a b l e ( e v e n o n c e ) i n y o u r c o d e .
T h i s v a r i a b l e i s r e a d - o n l y a n d d y n a m i c a l l y - s c o p e d .
M n e m o n i c : '
o f t e n f o l l o w s a q u o t e d s t r i n g .
# $ { ^ P O S T M A T C H }
T h i s i s s i m i l a r t o $ '
( $ P O S T M A T
C H
) e x c e p t t h a t i t d o e s n o t i n c u r t h e p e r f o r m a n c e p e n a l t y a s s o c i a t e d w i t h t h a t v a r i a b l e .
S e e " P e r f o r m a n c e i s s u e s " a b o v e .
I n P e r l v 5 . 1 8 a n d e a r l i e r , i t i s o n l y g u a r a n t e e d t o r e t u r n a d e f i n e d v a l u e w h e n t h e p a t t e r n w a s c o m p i l e d o r e x e c u t e d w i t h t h e / p
m o d i f i e r . I n P e r l v 5 . 2 0 , t h e / p
m o d i f i e r d o e s n o t h i n g , s o $ { ^ P O S T M A T C H }
d o e s t h e s a m e t h i n g a s $ P O S T M A T C H
.
T h i s v a r i a b l e w a s a d d e d i n P e r l v 5 . 1 0 . 0 .
T h i s v a r i a b l e i s r e a d - o n l y a n d d y n a m i c a l l y - s c o p e d .
# $ L A S T _ P A R E N _ M A T C H
# $ +
T h e t e x t m a t c h e d b y t h e h i g h e s t u s e d c a p t u r e g r o u p o f t h e l a s t s u c c e s s f u l s e a r c h p a t t e r n . I t i s l o g i c a l l y e q u i v a l e n t t o t h e h i g h e s t n u m b e r e d c a p t u r e v a r i a b l e ( $ 1
, $ 2
, . . . ) w h i c h h a s a d e f i n e d v a l u e .
T h i s i s u s e f u l i f y o u d o n ' t k n o w w h i c h o n e o f a s e t o f a l t e r n a t i v e p a t t e r n s m a t c h e d . F o r e x a m p l e :
/Version: (.*)|Revision: (.*)/ && ($rev = $+);
T h i s v a r i a b l e i s r e a d - o n l y a n d d y n a m i c a l l y - s c o p e d .
M n e m o n i c : b e p o s i t i v e a n d f o r w a r d l o o k i n g .
# $ L A S T _ S U B M A T C H _ R E S U L T
# $ ^ N
T h e t e x t m a t c h e d b y t h e u s e d g r o u p m o s t - r e c e n t l y c l o s e d ( i . e . t h e g r o u p w i t h t h e r i g h t m o s t c l o s i n g p a r e n t h e s i s ) o f t h e l a s t s u c c e s s f u l s e a r c h p a t t e r n . T h i s i s s u b t l y d i f f e r e n t f r o m $ +
. F o r e x a m p l e i n
"ab" =~ /^((.)(.))$/
w e h a v e
$1,$^N have the value "ab"
$2 has the value "a"
$3,$+ have the value "b"
T h i s i s p r i m a r i l y u s e d i n s i d e (
? { . . . } )
b l o c k s f o r e x a m i n i n g t e x t r e c e n t l y m a t c h e d . F o r e x a m p l e , t o e f f e c t i v e l y c a p t u r e t e x t t o a v a r i a b l e ( i n a d d i t i o n t o $
1
, $ 2
, e t c . ) , r e p l a c e ( . . . )
w i t h
(?:(...)(?{ $var = $^N }))
B y s e t t i n g a n d t h e n u s i n g $ v a r
i n t h i s w a y r e l i e v e s y o u f r o m h a v i n g t o w o r r y a b o u t e x a c t l y w h i c h n u m b e r e d s e t o f p a r e n t h e s e s t h e y a r e .
T h i s v a r i a b l e w a s a d d e d i n P e r l v 5 . 8 . 0 .
M n e m o n i c : t h e ( p o s s i b l y ) N e s t e d p a r e n t h e s i s t h a t m o s t r e c e n t l y c l o s e d .
# @ L A S T _ M A T C H _ E N D
# @ +
T h i s a r r a y h o l d s t h e o f f s e t s o f t h e e n d s o f t h e l a s t s u c c e s s f u l s u b m a t c h e s i n t h e c u r r e n t l y a c t i v e d y n a m i c s c o p e . $ + [ 0 ]
i s t h e o f f s e t i n t o t h e s t r i n g o f t h e e n d o f t h e e n t i r e m a t c h . T h i s i s t h e s a m e v a l u e a s w h a t t h e p o s
f u n c t i o n r e t u r n s w h e n c a l l e d o n t h e v a r i a b l e t h a t w a s m a t c h e d a g a i n s t . T h e n t h e l e m e n t o f t h i s a r r a y h o l d s t h e o f f s e t o f t h e n t h s u b m a t c h , s o $ + [ 1 ]
i s t h e o f f s e t p a s t w h e r e $ 1
e n d s , $ + [ 2 ]
t h e o f f s e t p a s t w h e r e $ 2
e n d s , a n d s o o n . Y o u c a n u s e $ #
+
t o d e t e r m i n e h o w m a n y s u b g r o u p s w e r e i n t h e l a s t s u c c e s s f u l m a t c h . S e e t h e e x a m p l e s g i v e n f o r t h e @ -
v a r i a b l e .
T h i s v a r i a b l e w a s a d d e d i n P e r l v 5 . 6 . 0 .
# % { ^ C A P T U R E }
# % L A S T _ P A R E N _ M A T C H
# % +
S i m i l a r t o @ +
, t h e % +
h a s h a l l o w s a c c e s s t o t h e n a m e d c a p t u r e b u f f e r s , s h o u l d t h e y e x i s t , i n t h e l a s t s u c c e s s f u l m a t c h i n t h e c u r r e n t l y a c t i v e d y n a m i c s c o p e .
F o r e x a m p l e , $ + { f o o }
i s e q u i v a l e n t t o $ 1
a f t e r t h e f o l l o w i n g m a t c h :
'foo' =~ /(?<foo>foo)/;
T h e k e y s o f t h e % +
h a s h l i s t o n l y t h e n a m e s o f b u f f e r s t h a t h a v e c a p t u r e d ( a n d t h a t a r e t h u s a s s o c i a t e d t o d e f i n e d v a l u e s ) .
I f m u l t i p l e d i s t i n c t c a p t u r e g r o u p s h a v e t h e s a m e n a m e , t h e n $
+ { N A M E }
w i l l r e f e r t o t h e l e f t m o s t d e f i n e d g r o u p i n t h e m a t c h .
T h e u n d e r l y i n g b e h a v i o u r o f % +
i s p r o v i d e d b y t h e T i e : : H a s h : : N a m e d C a p t u r e m o d u l e .
N o t e : % -
a n d % +
a r e t i e d v i e w s i n t o a c o m m o n i n t e r n a l h a s h a s s o c i a t e d w i t h t h e l a s t s u c c e s s f u l r e g u l a r e x p r e s s i o n . T h e r e f o r e m i x i n g i t e r a t i v e a c c e s s t o t h e m v i a e a c h
m a y h a v e u n p r e d i c t a b l e r e s u l t s . L i k e w i s e , i f t h e l a s t s u c c e s s f u l m a t c h c h a n g e s , t h e n t h e r e s u l t s m a y b e s u r p r i s i n g .
T h i s v a r i a b l e w a s a d d e d i n P e r l v 5 . 1 0 . 0 . T h e % { ^ C A P T U R E }
a l i a s w a s a d d e d i n 5 . 2 5 . 7 .
T h i s v a r i a b l e i s r e a d - o n l y a n d d y n a m i c a l l y - s c o p e d .
# @ L A S T _ M A T C H _ S T A R T
# @ -
$ - [ 0 ]
i s t h e o f f s e t o f t h e s t a r t o f t h e l a s t s u c c e s s f u l m a t c h . $ - [ n ]
i s t h e o f f s e t o f t h e s t a r t o f t h e s u b s t r i n g m a t c h e d b y n - t h s u b p a t t e r n , o r u n d e f i f t h e s u b p a t t e r n d i d n o t m a t c h .
T h u s , a f t e r a m a t c h a g a i n s t $ _
, $ &
c o i n c i d e s w i t h s u b s t r $ _ , $
- [ 0 ] , $ + [ 0 ] - $ - [ 0 ]
. S i m i l a r l y , $ n c o i n c i d e s w i t h s u b s t r $ _ , $
- [ n ] , $ + [ n ] - $ - [ n ]
if $ - [ n ]
i s d e f i n e d , a n d $ + c o i n c i d e s w i t h s u
b s t r $ _ , $ - [ $ # - ] , $ + [ $ # - ] - $ - [
$ # - ]
. O n e c a n u s e $ # -
t o f i n d t h e l a s t m a t c h e d s u b g r o u p i n t h e l a s t s u c c e s s f u l m a t c h . C o n t r a s t w i t h $ # +
, t h e n u m b e r o f s u b g r o u p s i n t h e r e g u l a r e x p r e s s i o n . C o m p a r e w i t h @ +
.
T h i s a r r a y h o l d s t h e o f f s e t s o f t h e b e g i n n i n g s o f t h e l a s t s u c c e s s f u l s u b m a t c h e s i n t h e c u r r e n t l y a c t i v e d y n a m i c s c o p e . $ - [ 0 ]
i s t h e o f f s e t i n t o t h e s t r i n g o f t h e b e g i n n i n g o f t h e e n t i r e m a t c h . T h e n t h e l e m e n t o f t h i s a r r a y h o l d s t h e o f f s e t o f t h e n t h s u b m a t c h , s o $ - [ 1 ]
i s t h e o f f s e t w h e r e $ 1
b e g i n s , $ - [ 2 ]
t h e o f f s e t w h e r e $ 2
b e g i n s , a n d s o o n .
A f t e r a m a t c h a g a i n s t s o m e v a r i a b l e $ v a r
:
# $ `
i s t h e s a m e a s s u b s t r ( $ v a r ,
0 , $ - [ 0 ] )
# $ &
i s t h e s a m e a s s u b s t r ( $ v a r ,
$ - [ 0 ] , $ + [ 0 ] - $ - [ 0 ] )
# $ '
i s t h e s a m e a s s u b s t r ( $ v a r ,
$ + [ 0 ] )
# $ 1
i s t h e s a m e a s s u b s t r ( $ v a r ,
$ - [ 1 ] , $ + [ 1 ] - $ - [ 1 ] )
# $ 2
i s t h e s a m e a s s u b s t r ( $ v a r ,
$ - [ 2 ] , $ + [ 2 ] - $ - [ 2 ] )
# $ 3
i s t h e s a m e a s s u b s t r ( $ v a r ,
$ - [ 3 ] , $ + [ 3 ] - $ - [ 3 ] )
T h i s v a r i a b l e w a s a d d e d i n P e r l v 5 . 6 . 0 .
# % { ^ C A P T U R E _ A L L }
# % -
S i m i l a r t o % +
, t h i s v a r i a b l e a l l o w s a c c e s s t o t h e n a m e d c a p t u r e g r o u p s i n t h e l a s t s u c c e s s f u l m a t c h i n t h e c u r r e n t l y a c t i v e d y n a m i c s c o p e . T o e a c h c a p t u r e g r o u p n a m e f o u n d i n t h e r e g u l a r e x p r e s s i o n , i t a s s o c i a t e s a r e f e r e n c e t o a n a r r a y c o n t a i n i n g t h e l i s t o f v a l u e s c a p t u r e d b y a l l b u f f e r s w i t h t h a t n a m e ( s h o u l d t h e r e b e s e v e r a l o f t h e m ) , i n t h e o r d e r w h e r e t h e y a p p e a r .
H e r e ' s a n e x a m p l e :
if ('1234' =~ /(?<A>1 )(?<B>2 )(?<A>3 )(?<B>4 )/) {
foreach my $bufname (sort keys %-) {
my $ary = $-{$bufname};
foreach my $idx (0..$#$ary) {
print "\$-{$bufname}[$idx] : ",
(defined($ary->[$idx])
? "'$ary->[$idx]'"
: "undef"),
"\n";
}
}
}
w o u l d p r i n t o u t :
$-{A}[0] : '1'
$-{A}[1 ] : '3'
$-{B}[0] : '2'
$-{B}[1 ] : '4'
T h e k e y s o f t h e % -
h a s h c o r r e s p o n d t o a l l b u f f e r n a m e s f o u n d i n t h e r e g u l a r e x p r e s s i o n .
T h e b e h a v i o u r o f % -
i s i m p l e m e n t e d v i a t h e T i e : : H a s h : : N a m e d C a p t u r e m o d u l e .
N o t e : % -
a n d % +
a r e t i e d v i e w s i n t o a c o m m o n i n t e r n a l h a s h a s s o c i a t e d w i t h t h e l a s t s u c c e s s f u l r e g u l a r e x p r e s s i o n . T h e r e f o r e m i x i n g i t e r a t i v e a c c e s s t o t h e m v i a e a c h
m a y h a v e u n p r e d i c t a b l e r e s u l t s . L i k e w i s e , i f t h e l a s t s u c c e s s f u l m a t c h c h a n g e s , t h e n t h e r e s u l t s m a y b e s u r p r i s i n g .
T h i s v a r i a b l e w a s a d d e d i n P e r l v 5 . 1 0 . 0 . T h e % { ^ C A P T U R E _ A L L }
a l i a s w a s a d d e d i n 5 . 2 5 . 7 .
T h i s v a r i a b l e i s r e a d - o n l y a n d d y n a m i c a l l y - s c o p e d .
# $ L A S T _ R E G E X P _ C O D E _ R E S U L T
# $ ^ R
T h e r e s u l t o f e v a l u a t i o n o f t h e l a s t s u c c e s s f u l ( ? { c o d e } )
r e g u l a r e x p r e s s i o n a s s e r t i o n ( s e e p e r l r e ) . M a y b e w r i t t e n t o .
T h i s v a r i a b l e w a s a d d e d i n P e r l 5 . 0 0 5 .
# $ { ^ R E _ C O M P I L E _ R E C U R S I O N _ L I M I T }
T h e c u r r e n t v a l u e g i v i n g t h e m a x i m u m n u m b e r o f o p e n b u t u n c l o s e d p a r e n t h e t i c a l g r o u p s t h e r e m a y b e a t a n y p o i n t d u r i n g a r e g u l a r e x p r e s s i o n c o m p i l a t i o n . T h e d e f a u l t i s c u r r e n t l y 1 0 0 0 n e s t e d g r o u p s . Y o u m a y a d j u s t i t d e p e n d i n g o n y o u r n e e d s a n d t h e a m o u n t o f m e m o r y a v a i l a b l e .
T h i s v a r i a b l e w a s a d d e d i n P e r l v 5 . 3 0 . 0 .
# $ { ^ R E _ D E B U G _ F L A G S }
T h e c u r r e n t v a l u e o f t h e r e g e x d e b u g g i n g f l a g s . S e t t o 0 f o r n o d e b u g o u t p u t e v e n w h e n t h e r e
' d e b u g '
m o d u l e i s l o a d e d . S e e re f o r d e t a i l s .
T h i s v a r i a b l e w a s a d d e d i n P e r l v 5 . 1 0 . 0 .
# $ { ^ R E _ T R I E _ M A X B U F }
C o n t r o l s h o w c e r t a i n r e g e x o p t i m i s a t i o n s a r e a p p l i e d a n d h o w m u c h m e m o r y t h e y u t i l i z e . T h i s v a l u e b y d e f a u l t i s 6 5 5 3 6 w h i c h c o r r e s p o n d s t o a 5 1 2 k B t e m p o r a r y c a c h e . S e t t h i s t o a h i g h e r v a l u e t o t r a d e m e m o r y f o r s p e e d w h e n m a t c h i n g l a r g e a l t e r n a t i o n s . S e t i t t o a l o w e r v a l u e i f y o u w a n t t h e o p t i m i s a t i o n s t o b e a s c o n s e r v a t i v e o f m e m o r y a s p o s s i b l e b u t s t i l l o c c u r , a n d s e t i t t o a n e g a t i v e v a l u e t o p r e v e n t t h e o p t i m i s a t i o n a n d c o n s e r v e t h e m o s t m e m o r y . U n d e r n o r m a l s i t u a t i o n s t h i s v a r i a b l e s h o u l d b e o f n o i n t e r e s t t o y o u .
T h i s v a r i a b l e w a s a d d e d i n P e r l v 5 . 1 0 . 0 .
V a r i a b l e s t h a t d e p e n d o n t h e c u r r e n t l y s e l e c t e d f i l e h a n d l e m a y b e s e t b y c a l l i n g a n a p p r o p r i a t e o b j e c t m e t h o d o n t h e I O : : H a n d l e
o b j e c t , a l t h o u g h t h i s i s l e s s e f f i c i e n t t h a n u s i n g t h e r e g u l a r b u i l t - i n v a r i a b l e s . ( S u m m a r y l i n e s b e l o w f o r t h i s c o n t a i n t h e w o r d H A N D L E . ) F i r s t y o u m u s t s a y
use IO::Handle;
a f t e r w h i c h y o u m a y u s e e i t h e r
method HANDLE EXPR
o r m o r e s a f e l y ,
HANDLE->method(EXPR)
E a c h m e t h o d r e t u r n s t h e o l d v a l u e o f t h e I O : : H a n d l e
a t t r i b u t e . T h e m e t h o d s e a c h t a k e a n o p t i o n a l E X P R , w h i c h , i f s u p p l i e d , s p e c i f i e s t h e n e w v a l u e f o r t h e I O : : H
a n d l e
a t t r i b u t e i n q u e s t i o n . I f n o t s u p p l i e d , m o s t m e t h o d s d o n o t h i n g t o t h e c u r r e n t v a l u e - - e x c e p t f o r a u t o f l u s h ( )
, w h i c h w i l l a s s u m e a 1 f o r y o u , j u s t t o b e d i f f e r e n t .
B e c a u s e l o a d i n g i n t h e I O : : H a n d l
e
c l a s s i s a n e x p e n s i v e o p e r a t i o n , y o u s h o u l d l e a r n h o w t o u s e t h e r e g u l a r b u i l t - i n v a r i a b l e s .
A f e w o f t h e s e v a r i a b l e s a r e c o n s i d e r e d " r e a d - o n l y " . T h i s m e a n s t h a t i f y o u t r y t o a s s i g n t o t h i s v a r i a b l e , e i t h e r d i r e c t l y o r i n d i r e c t l y t h r o u g h a r e f e r e n c e , y o u ' l l r a i s e a r u n - t i m e e x c e p t i o n .
Y o u s h o u l d b e v e r y c a r e f u l w h e n m o d i f y i n g t h e d e f a u l t v a l u e s o f m o s t s p e c i a l v a r i a b l e s d e s c r i b e d i n t h i s d o c u m e n t . I n m o s t c a s e s y o u w a n t t o l o c a l i z e t h e s e v a r i a b l e s b e f o r e c h a n g i n g t h e m , s i n c e i f y o u d o n ' t , t h e c h a n g e m a y a f f e c t o t h e r m o d u l e s w h i c h r e l y o n t h e d e f a u l t v a l u e s o f t h e s p e c i a l v a r i a b l e s t h a t y o u h a v e c h a n g e d . T h i s i s o n e o f t h e c o r r e c t w a y s t o r e a d t h e w h o l e f i l e a t o n c e :
open my $fh, "<", "foo" or die $!;
local $/; # enable localized slurp mode
my $content = <$fh>;
close $fh;
B u t t h e f o l l o w i n g c o d e i s q u i t e b a d :
open my $fh, "<", "foo" or die $!;
undef $/; # enable slurp mode
my $content = <$fh>;
close $fh;
s i n c e s o m e o t h e r m o d u l e , m a y w a n t t o r e a d d a t a f r o m s o m e f i l e i n t h e d e f a u l t " l i n e m o d e " , s o i f t h e c o d e w e h a v e j u s t p r e s e n t e d h a s b e e n e x e c u t e d , t h e g l o b a l v a l u e o f $ /
i s n o w c h a n g e d f o r a n y o t h e r c o d e r u n n i n g i n s i d e t h e s a m e P e r l i n t e r p r e t e r .
U s u a l l y w h e n a v a r i a b l e i s l o c a l i z e d y o u w a n t t o m a k e s u r e t h a t t h i s c h a n g e a f f e c t s t h e s h o r t e s t s c o p e p o s s i b l e . S o u n l e s s y o u a r e a l r e a d y i n s i d e s o m e s h o r t { }
b l o c k , y o u s h o u l d c r e a t e o n e y o u r s e l f . F o r e x a m p l e :
my $content = '';
open my $fh, "<", "foo" or die $!;
{
local $/;
$content = <$fh>;
}
close $fh;
H e r e i s a n e x a m p l e o f h o w y o u r o w n c o d e c a n g o b r o k e n :
for ( 1..3 ){
$\ = "\r\n";
nasty_break();
print "$_";
}
sub nasty_break {
$\ = "\f";
# do something with $_
}
Y o u p r o b a b l y e x p e c t t h i s c o d e t o p r i n t t h e e q u i v a l e n t o f
"1\r\n2\r\n3\r\n"
b u t i n s t e a d y o u g e t :
"1\f2\f3\f"
W h y ? B e c a u s e n a s t y _ b r e a k ( )
m o d i f i e s $ \
w i t h o u t l o c a l i z i n g i t f i r s t . T h e v a l u e y o u s e t i n n a s t y _ b
r e a k ( )
i s s t i l l t h e r e w h e n y o u r e t u r n . T h e f i x i s t o a d d l o c a l ( )
s o t h e v a l u e d o e s n ' t l e a k o u t o f n a s t y _ b r e a k ( )
:
local $\ = "\f";
I t ' s e a s y t o n o t i c e t h e p r o b l e m i n s u c h a s h o r t e x a m p l e , b u t i n m o r e c o m p l i c a t e d c o d e y o u a r e l o o k i n g f o r t r o u b l e i f y o u d o n ' t l o c a l i z e c h a n g e s t o t h e s p e c i a l v a r i a b l e s .
# $ A R G V
C o n t a i n s t h e n a m e o f t h e c u r r e n t f i l e w h e n r e a d i n g f r o m < >
.
# @ A R G V
T h e a r r a y @ A R G V
c o n t a i n s t h e c o m m a n d - l i n e a r g u m e n t s i n t e n d e d f o r t h e s c r i p t . $ # A R G V
i s g e n e r a l l y t h e n u m b e r o f a r g u m e n t s m i n u s o n e , b e c a u s e $ A R G V [ 0 ]
i s t h e f i r s t a r g u m e n t , n o t t h e p r o g r a m ' s c o m m a n d n a m e i t s e l f . S e e " $ 0 " f o r t h e c o m m a n d n a m e .
# A R G V
T h e s p e c i a l f i l e h a n d l e t h a t i t e r a t e s o v e r c o m m a n d - l i n e f i l e n a m e s i n @ A R G V
. U s u a l l y w r i t t e n a s t h e n u l l f i l e h a n d l e i n t h e a n g l e o p e r a t o r < >
. N o t e t h a t c u r r e n t l y A R G V
o n l y h a s i t s m a g i c a l e f f e c t w i t h i n t h e < >
o p e r a t o r ; e l s e w h e r e i t i s j u s t a p l a i n f i l e h a n d l e c o r r e s p o n d i n g t o t h e l a s t f i l e o p e n e d b y < >
. I n p a r t i c u l a r , p a s s i n g \ * A R G V
a s a p a r a m e t e r t o a f u n c t i o n t h a t e x p e c t s a f i l e h a n d l e m a y n o t c a u s e y o u r f u n c t i o n t o a u t o m a t i c a l l y r e a d t h e c o n t e n t s o f a l l t h e f i l e s i n @ A R G V
.
# A R G V O U T
T h e s p e c i a l f i l e h a n d l e t h a t p o i n t s t o t h e c u r r e n t l y o p e n o u t p u t f i l e w h e n d o i n g e d i t - i n - p l a c e p r o c e s s i n g w i t h - i . U s e f u l w h e n y o u h a v e t o d o a l o t o f i n s e r t i n g a n d d o n ' t w a n t t o k e e p m o d i f y i n g $ _
. S e e p e r l r u n f o r t h e - i s w i t c h .
# I O : : H a n d l e - > o u t p u t _ f i e l d _ s e p a r a t o r ( E X P R )
# $ O U T P U T _ F I E L D _ S E P A R A T O R
# $ O F S
# $ ,
T h e o u t p u t f i e l d s e p a r a t o r f o r t h e p r i n t o p e r a t o r . I f d e f i n e d , t h i s v a l u e i s p r i n t e d b e t w e e n e a c h o f p r i n t ' s a r g u m e n t s . D e f a u l t i s u n d e f
.
Y o u c a n n o t c a l l o u t p u t _ f i e l d _ s e
p a r a t o r ( )
o n a h a n d l e , o n l y a s a s t a t i c m e t h o d . S e e I O : : H a n d l e .
M n e m o n i c : w h a t i s p r i n t e d w h e n t h e r e i s a " , " i n y o u r p r i n t s t a t e m e n t .
# H A N D L E - > i n p u t _ l i n e _ n u m b e r ( E X P R )
# $ I N P U T _ L I N E _ N U M B E R
# $ N R
# $ .
C u r r e n t l i n e n u m b e r f o r t h e l a s t f i l e h a n d l e a c c e s s e d .
E a c h f i l e h a n d l e i n P e r l c o u n t s t h e n u m b e r o f l i n e s t h a t h a v e b e e n r e a d f r o m i t . ( D e p e n d i n g o n t h e v a l u e o f $ /
, P e r l ' s i d e a o f w h a t c o n s t i t u t e s a l i n e m a y n o t m a t c h y o u r s . ) W h e n a l i n e i s r e a d f r o m a f i l e h a n d l e ( v i a r e
a d l i n e ( )
or < >
) , o r w h e n t e l l ( )
or s e
e k ( )
i s c a l l e d o n i t , $ .
b e c o m e s a n a l i a s t o t h e l i n e c o u n t e r f o r t h a t f i l e h a n d l e .
Y o u c a n a d j u s t t h e c o u n t e r b y a s s i g n i n g t o $ .
, b u t t h i s w i l l n o t a c t u a l l y m o v e t h e s e e k p o i n t e r . L o c a l i z i n g $ .
w i l l n o t l o c a l i z e t h e f i l e h a n d l e ' s l i n e c o u n t . I n s t e a d , i t w i l l l o c a l i z e p e r l ' s n o t i o n o f w h i c h f i l e h a n d l e $ .
i s c u r r e n t l y a l i a s e d t o .
$ .
i s r e s e t w h e n t h e f i l e h a n d l e i s c l o s e d , b u t n o t w h e n a n o p e n f i l e h a n d l e i s r e o p e n e d w i t h o u t a n i n t e r v e n i n g c l o s e ( )
. F o r m o r e d e t a i l s , s e e " I / O O p e r a t o r s " i n p e r l o p . B e c a u s e < >
n e v e r d o e s a n e x p l i c i t c l o s e , l i n e n u m b e r s i n c r e a s e a c r o s s A R G V
f i l e s ( b u t s e e e x a m p l e s i n " e o f " i n p e r l f u n c ) .
Y o u c a n a l s o u s e H A N D L E - > i n p u t _
l i n e _ n u m b e r ( E X P R )
t o a c c e s s t h e l i n e c o u n t e r f o r a g i v e n f i l e h a n d l e w i t h o u t h a v i n g t o w o r r y a b o u t w h i c h h a n d l e y o u l a s t a c c e s s e d .
M n e m o n i c : m a n y p r o g r a m s u s e " . " t o m e a n t h e c u r r e n t l i n e n u m b e r .
# I O : : H a n d l e - > i n p u t _ r e c o r d _ s e p a r a t o r ( E X P R )
# $ I N P U T _ R E C O R D _ S E P A R A T O R
# $ R S
# $ /
T h e i n p u t r e c o r d s e p a r a t o r , n e w l i n e b y d e f a u l t . T h i s i n f l u e n c e s P e r l ' s i d e a o f w h a t a " l i n e " i s . W o r k s l i k e a w k ' s R S v a r i a b l e , i n c l u d i n g t r e a t i n g e m p t y l i n e s a s a t e r m i n a t o r i f s e t t o t h e n u l l s t r i n g ( a n e m p t y l i n e c a n n o t c o n t a i n a n y s p a c e s o r t a b s ) . Y o u m a y s e t i t t o a m u l t i - c h a r a c t e r s t r i n g t o m a t c h a m u l t i - c h a r a c t e r t e r m i n a t o r , o r t o u n d
e f
t o r e a d t h r o u g h t h e e n d o f f i l e . S e t t i n g i t t o " \ n \ n "
m e a n s s o m e t h i n g s l i g h t l y d i f f e r e n t t h a n s e t t i n g t o " "
, i f t h e f i l e c o n t a i n s c o n s e c u t i v e e m p t y l i n e s . S e t t i n g t o " "
w i l l t r e a t t w o o r m o r e c o n s e c u t i v e e m p t y l i n e s a s a s i n g l e e m p t y l i n e . S e t t i n g t o " \ n \ n "
w i l l b l i n d l y a s s u m e t h a t t h e n e x t i n p u t c h a r a c t e r b e l o n g s t o t h e n e x t p a r a g r a p h , e v e n i f i t ' s a n e w l i n e .
local $/; # enable "slurp" mode
local $_ = <FH>; # whole file now here
s/\n[ \t]+/ /g;
R e m e m b e r : t h e v a l u e o f $ /
i s a s t r i n g , n o t a r e g e x . a w k h a s t o b e b e t t e r f o r s o m e t h i n g . : - )
S e t t i n g $ /
t o a n e m p t y s t r i n g - - t h e s o - c a l l e d p a r a g r a p h m o d e - - m e r i t s s p e c i a l a t t e n t i o n . W h e n $ /
i s s e t t o " "
a n d t h e e n t i r e f i l e i s r e a d i n w i t h t h a t s e t t i n g , a n y s e q u e n c e o f o n e o r m o r e c o n s e c u t i v e n e w l i n e s a t t h e b e g i n n i n g o f t h e f i l e i s d i s c a r d e d . W i t h t h e e x c e p t i o n o f t h e f i n a l r e c o r d i n t h e f i l e , e a c h s e q u e n c e o f c h a r a c t e r s e n d i n g i n t w o o r m o r e n e w l i n e s i s t r e a t e d a s o n e r e c o r d a n d i s r e a d i n t o e n d i n e x a c t l y t w o n e w l i n e s . I f t h e l a s t r e c o r d i n t h e f i l e e n d s i n z e r o o r o n e c o n s e c u t i v e n e w l i n e s , t h a t r e c o r d i s r e a d i n w i t h t h a t n u m b e r o f n e w l i n e s . I f t h e l a s t r e c o r d e n d s i n t w o o r m o r e c o n s e c u t i v e n e w l i n e s , i t i s r e a d i n w i t h t w o n e w l i n e s l i k e a l l p r e c e d i n g r e c o r d s .
S u p p o s e w e w r o t e t h e f o l l o w i n g s t r i n g t o a f i l e :
my $string = "\n\n\n";
$string .= "alpha beta\ngamma delta\n\n\n";
$string .= "epsilon zeta eta\n\n";
$string .= "theta\n";
my $file = 'simple_file.txt';
open my $OUT, '>', $file or die;
print $OUT $string;
close $OUT or die;
N o w w e r e a d t h a t f i l e i n p a r a g r a p h m o d e :
local $/ = ""; # paragraph mode
open my $IN, '<', $file or die;
my @records = <$IN>;
close $IN or die;
@ r e c o r d s
w i l l c o n s i s t o f t h e s e 3 s t r i n g s :
(
"alpha beta\ngamma delta\n\n",
"epsilon zeta eta\n\n",
"theta\n",
)
S e t t i n g $ /
t o a r e f e r e n c e t o a n i n t e g e r , s c a l a r c o n t a i n i n g a n i n t e g e r , o r s c a l a r t h a t ' s c o n v e r t i b l e t o a n i n t e g e r w i l l a t t e m p t t o r e a d r e c o r d s i n s t e a d o f l i n e s , w i t h t h e m a x i m u m r e c o r d s i z e b e i n g t h e r e f e r e n c e d i n t e g e r n u m b e r o f c h a r a c t e r s . S o t h i s :
local $/ = \32768; # or \"32768", or \$var_containing_32768
open my $fh, "<", $myfile or die $!;
local $_ = <$fh>;
w i l l r e a d a r e c o r d o f n o m o r e t h a n 3 2 7 6 8 c h a r a c t e r s f r o m $ f h . I f y o u ' r e n o t r e a d i n g f r o m a r e c o r d - o r i e n t e d f i l e ( o r y o u r O S d o e s n ' t h a v e r e c o r d - o r i e n t e d f i l e s ) , t h e n y o u ' l l l i k e l y g e t a f u l l c h u n k o f d a t a w i t h e v e r y r e a d . I f a r e c o r d i s l a r g e r t h a n t h e r e c o r d s i z e y o u ' v e s e t , y o u ' l l g e t t h e r e c o r d b a c k i n p i e c e s . T r y i n g t o s e t t h e r e c o r d s i z e t o z e r o o r l e s s i s d e p r e c a t e d a n d w i l l c a u s e $ / t o h a v e t h e v a l u e o f " u n d e f " , w h i c h w i l l c a u s e r e a d i n g i n t h e ( r e s t o f t h e ) w h o l e f i l e .
A s o f 5 . 1 9 . 9 s e t t i n g $ /
t o a n y o t h e r f o r m o f r e f e r e n c e w i l l t h r o w a f a t a l e x c e p t i o n . T h i s i s i n p r e p a r a t i o n f o r s u p p o r t i n g n e w w a y s t o s e t $ /
i n t h e f u t u r e .
O n V M S o n l y , r e c o r d r e a d s b y p a s s P e r l I O l a y e r s a n d a n y a s s o c i a t e d b u f f e r i n g , s o y o u m u s t n o t m i x r e c o r d a n d n o n - r e c o r d r e a d s o n t h e s a m e f i l e h a n d l e . R e c o r d m o d e m i x e s w i t h l i n e m o d e o n l y w h e n t h e s a m e b u f f e r i n g l a y e r i s i n u s e f o r b o t h m o d e s .
Y o u c a n n o t c a l l i n p u t _ r e c o r d _ s e
p a r a t o r ( )
o n a h a n d l e , o n l y a s a s t a t i c m e t h o d . S e e I O : : H a n d l e .
S e e a l s o " N e w l i n e s " i n p e r l p o r t . A l s o s e e " $ . " .
M n e m o n i c : / d e l i m i t s l i n e b o u n d a r i e s w h e n q u o t i n g p o e t r y .
# I O : : H a n d l e - > o u t p u t _ r e c o r d _ s e p a r a t o r ( E X P R )
# $ O U T P U T _ R E C O R D _ S E P A R A T O R
# $ O R S
# $ \
T h e o u t p u t r e c o r d s e p a r a t o r f o r t h e p r i n t o p e r a t o r . I f d e f i n e d , t h i s v a l u e i s p r i n t e d a f t e r t h e l a s t o f p r i n t ' s a r g u m e n t s . D e f a u l t i s u n d e f
.
Y o u c a n n o t c a l l o u t p u t _ r e c o r d _ s
e p a r a t o r ( )
o n a h a n d l e , o n l y a s a s t a t i c m e t h o d . S e e I O : : H a n d l e .
M n e m o n i c : y o u s e t $ \
i n s t e a d o f a d d i n g " \ n " a t t h e e n d o f t h e p r i n t . A l s o , i t ' s j u s t l i k e $ /
, b u t i t ' s w h a t y o u g e t " b a c k " f r o m P e r l .
# H A N D L E - > a u t o f l u s h ( E X P R )
# $ O U T P U T _ A U T O F L U S H
# $ |
I f s e t t o n o n z e r o , f o r c e s a f l u s h r i g h t a w a y a n d a f t e r e v e r y w r i t e o r p r i n t o n t h e c u r r e n t l y s e l e c t e d o u t p u t c h a n n e l . D e f a u l t i s 0 ( r e g a r d l e s s o f w h e t h e r t h e c h a n n e l i s r e a l l y b u f f e r e d b y t h e s y s t e m o r n o t ; $ |
t e l l s y o u o n l y w h e t h e r y o u ' v e a s k e d P e r l e x p l i c i t l y t o f l u s h a f t e r e a c h w r i t e ) . S T D O U T w i l l t y p i c a l l y b e l i n e b u f f e r e d i f o u t p u t i s t o t h e t e r m i n a l a n d b l o c k b u f f e r e d o t h e r w i s e . S e t t i n g t h i s v a r i a b l e i s u s e f u l p r i m a r i l y w h e n y o u a r e o u t p u t t i n g t o a p i p e o r s o c k e t , s u c h a s w h e n y o u a r e r u n n i n g a P e r l p r o g r a m u n d e r r s h a n d w a n t t o s e e t h e o u t p u t a s i t ' s h a p p e n i n g . T h i s h a s n o e f f e c t o n i n p u t b u f f e r i n g . S e e " g e t c " i n p e r l f u n c f o r t h a t . S e e " s e l e c t " i n p e r l f u n c o n h o w t o s e l e c t t h e o u t p u t c h a n n e l . S e e a l s o I O : : H a n d l e .
M n e m o n i c : w h e n y o u w a n t y o u r p i p e s t o b e p i p i n g h o t .
# $ { ^ L A S T _ F H }
T h i s r e a d - o n l y v a r i a b l e c o n t a i n s a r e f e r e n c e t o t h e l a s t - r e a d f i l e h a n d l e . T h i s i s s e t b y < H A N
D L E >
, r e a d l i n e
, t e l l
, e o f
a n d s
e e k
. T h i s i s t h e s a m e h a n d l e t h a t $ .
a n d t e l l
a n d e o f
w i t h o u t a r g u m e n t s u s e . I t i s a l s o t h e h a n d l e u s e d w h e n P e r l a p p e n d s " , < S T D I N > l i n e 1 " t o a n e r r o r o r w a r n i n g m e s s a g e .
T h i s v a r i a b l e w a s a d d e d i n P e r l v 5 . 1 8 . 0 .
T h e s p e c i a l v a r i a b l e s f o r f o r m a t s a r e a s u b s e t o f t h o s e f o r f i l e h a n d l e s . S e e p e r l f o r m f o r m o r e i n f o r m a t i o n a b o u t P e r l ' s f o r m a t s .
# $ A C C U M U L A T O R
# $ ^ A
T h e c u r r e n t v a l u e o f t h e w r i t e ( )
a c c u m u l a t o r f o r f o r m a t ( )
l i n e s . A f o r m a t c o n t a i n s f o r m l i n e ( )
c a l l s t h a t p u t t h e i r r e s u l t i n t o $ ^ A
. A f t e r c a l l i n g i t s f o r m a t , w r i t e ( )
p r i n t s o u t t h e c o n t e n t s o f $ ^ A
a n d e m p t i e s . S o y o u n e v e r r e a l l y s e e t h e c o n t e n t s o f $ ^ A
u n l e s s y o u c a l l f o r m l i n e ( )
y o u r s e l f a n d t h e n l o o k a t i t . S e e p e r l f o r m a n d " f o r m l i n e P I C T U R E , L I S T " i n p e r l f u n c .
# I O : : H a n d l e - > f o r m a t _ f o r m f e e d ( E X P R )
# $ F O R M A T _ F O R M F E E D
# $ ^ L
W h a t f o r m a t s o u t p u t a s a f o r m f e e d . T h e d e f a u l t i s \ f
.
Y o u c a n n o t c a l l f o r m a t _ f o r m f e e d
( )
o n a h a n d l e , o n l y a s a s t a t i c m e t h o d . S e e I O : : H a n d l e .
# H A N D L E - > f o r m a t _ p a g e _ n u m b e r ( E X P R )
# $ F O R M A T _ P A G E _ N U M B E R
# $ %
T h e c u r r e n t p a g e n u m b e r o f t h e c u r r e n t l y s e l e c t e d o u t p u t c h a n n e l .
M n e m o n i c : %
i s p a g e n u m b e r i n n r o f f .
# H A N D L E - > f o r m a t _ l i n e s _ l e f t ( E X P R )
# $ F O R M A T _ L I N E S _ L E F T
# $ -
T h e n u m b e r o f l i n e s l e f t o n t h e p a g e o f t h e c u r r e n t l y s e l e c t e d o u t p u t c h a n n e l .
M n e m o n i c : l i n e s _ o n _ p a g e - l i n e s _ p r i n t e d .
# I O : : H a n d l e - > f o r m a t _ l i n e _ b r e a k _ c h a r a c t e r s E X P R
# $ F O R M A T _ L I N E _ B R E A K _ C H A R A C T E R S
# $ :
T h e c u r r e n t s e t o f c h a r a c t e r s a f t e r w h i c h a s t r i n g m a y b e b r o k e n t o f i l l c o n t i n u a t i o n f i e l d s ( s t a r t i n g w i t h ^
) i n a f o r m a t . T h e d e f a u l t i s " \ n - " , t o b r e a k o n a s p a c e , n e w l i n e , o r a h y p h e n .
Y o u c a n n o t c a l l f o r m a t _ l i n e _ b r e
a k _ c h a r a c t e r s ( )
o n a h a n d l e , o n l y a s a s t a t i c m e t h o d . S e e I O : : H a n d l e .
M n e m o n i c : a " c o l o n " i n p o e t r y i s a p a r t o f a l i n e .
# H A N D L E - > f o r m a t _ l i n e s _ p e r _ p a g e ( E X P R )
# $ F O R M A T _ L I N E S _ P E R _ P A G E
# $ =
T h e c u r r e n t p a g e l e n g t h ( p r i n t a b l e l i n e s ) o f t h e c u r r e n t l y s e l e c t e d o u t p u t c h a n n e l . T h e d e f a u l t i s 6 0 .
M n e m o n i c : = h a s h o r i z o n t a l l i n e s .
# H A N D L E - > f o r m a t _ t o p _ n a m e ( E X P R )
# $ F O R M A T _ T O P _ N A M E
# $ ^
T h e n a m e o f t h e c u r r e n t t o p - o f - p a g e f o r m a t f o r t h e c u r r e n t l y s e l e c t e d o u t p u t c h a n n e l . T h e d e f a u l t i s t h e n a m e o f t h e f i l e h a n d l e w i t h _ T O P
a p p e n d e d . F o r e x a m p l e , t h e d e f a u l t f o r m a t t o p n a m e f o r t h e S T D O U T
f i l e h a n d l e i s S T D O U T _ T O P
.
M n e m o n i c : p o i n t s t o t o p o f p a g e .
# H A N D L E - > f o r m a t _ n a m e ( E X P R )
# $ F O R M A T _ N A M E
# $ ~
T h e n a m e o f t h e c u r r e n t r e p o r t f o r m a t f o r t h e c u r r e n t l y s e l e c t e d o u t p u t c h a n n e l . T h e d e f a u l t f o r m a t n a m e i s t h e s a m e a s t h e f i l e h a n d l e n a m e . F o r e x a m p l e , t h e d e f a u l t f o r m a t n a m e f o r t h e S T D O U T
f i l e h a n d l e i s j u s t S T D O U
T
.
M n e m o n i c : b r o t h e r t o $ ^
.
# E r r o r V a r i a b l e s
T h e v a r i a b l e s $ @
, $ !
, $ ^ E
, a n d $
?
c o n t a i n i n f o r m a t i o n a b o u t d i f f e r e n t t y p e s o f e r r o r c o n d i t i o n s t h a t m a y a p p e a r d u r i n g e x e c u t i o n o f a P e r l p r o g r a m . T h e v a r i a b l e s a r e s h o w n o r d e r e d b y t h e " d i s t a n c e " b e t w e e n t h e s u b s y s t e m w h i c h r e p o r t e d t h e e r r o r a n d t h e P e r l p r o c e s s . T h e y c o r r e s p o n d t o e r r o r s d e t e c t e d b y t h e P e r l i n t e r p r e t e r , C l i b r a r y , o p e r a t i n g s y s t e m , o r a n e x t e r n a l p r o g r a m , r e s p e c t i v e l y .
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 s b e t w e e n t h e s e v a r i a b l e s , c o n s i d e r t h e f o l l o w i n g P e r l e x p r e s s i o n , w h i c h u s e s a s i n g l e - q u o t e d s t r i n g . A f t e r e x e c u t i o n o f t h i s s t a t e m e n t , p e r l m a y h a v e s e t a l l f o u r s p e c i a l e r r o r v a r i a b l e s :
eval q{
open my $pipe, "/cdrom/install |" or die $!;
my @res = <$pipe>;
close $pipe or die "bad pipe: $?, $!";
};
W h e n p e r l e x e c u t e s t h e e v a l ( )
e x p r e s s i o n , i t t r a n s l a t e s t h e o p e n
( )
, < P I P E >
, a n d c l o s e
c a l l s i n t h e C r u n - t i m e l i b r a r y a n d t h e n c e t o t h e o p e r a t i n g s y s t e m k e r n e l . p e r l s e t s $ !
t o t h e C l i b r a r y ' s e r r n o
i f o n e o f t h e s e c a l l s f a i l s .
$ @
i s s e t i f t h e s t r i n g t o b e e v
a l
- e d d i d n o t c o m p i l e ( t h i s m a y h a p p e n i f o p e n
or c l o s e
w e r e i m p o r t e d w i t h b a d p r o t o t y p e s ) , o r i f P e r l c o d e e x e c u t e d d u r i n g e v a l u a t i o n d i e ( )
d . I n t h e s e c a s e s t h e v a l u e o f $ @
i s t h e c o m p i l e e r r o r , o r t h e a r g u m e n t t o d i e
( w h i c h w i l l i n t e r p o l a t e $ !
a n d $ ?
) . ( S e e a l s o F a t a l , t h o u g h . )
U n d e r a f e w o p e r a t i n g s y s t e m s , $
^ E
m a y c o n t a i n a m o r e v e r b o s e e r r o r i n d i c a t o r , s u c h a s i n t h i s c a s e , " C D R O M t r a y n o t c l o s e d . " S y s t e m s t h a t d o n o t s u p p o r t e x t e n d e d e r r o r m e s s a g e s l e a v e $ ^ E
t h e s a m e a s $ !
.
F i n a l l y , $ ?
m a y b e s e t t o a n o n - 0 v a l u e i f t h e e x t e r n a l p r o g r a m / c d r o m / i n s t a l l f a i l s . T h e u p p e r e i g h t b i t s r e f l e c t s p e c i f i c e r r o r c o n d i t i o n s e n c o u n t e r e d b y t h e p r o g r a m ( t h e p r o g r a m ' s e x i t ( )
v a l u e ) . T h e l o w e r e i g h t b i t s r e f l e c t m o d e o f f a i l u r e , l i k e s i g n a l d e a t h a n d c o r e d u m p i n f o r m a t i o n . S e e w a i t ( 2 ) f o r d e t a i l s . I n c o n t r a s t t o $ !
a n d $ ^ E
, w h i c h a r e s e t o n l y i f a n e r r o r c o n d i t i o n i s d e t e c t e d , t h e v a r i a b l e $ ?
i s s e t o n e a c h w a i t
o r p i p e c l o s e
, o v e r w r i t i n g t h e o l d v a l u e . T h i s i s m o r e l i k e $ @
, w h i c h o n e v e r y e v a
l ( )
i s a l w a y s s e t o n f a i l u r e a n d c l e a r e d o n s u c c e s s .
F o r m o r e d e t a i l s , s e e t h e i n d i v i d u a l d e s c r i p t i o n s a t $ @
, $ !
, $ ^ E
, a n d $ ?
.
# $ { ^ C H I L D _ E R R O R _ N A T I V E }
T h e n a t i v e s t a t u s r e t u r n e d b y t h e l a s t p i p e c l o s e , b a c k t i c k ( `
`
) c o m m a n d , s u c c e s s f u l c a l l t o w a i t ( )
or w a i t p i d ( )
, o r f r o m t h e s
y s t e m ( )
o p e r a t o r . O n P O S I X - l i k e s y s t e m s t h i s v a l u e c a n b e d e c o d e d w i t h t h e W I F E X I T E D , W E X I T S T A T U S , W I F S I G N A L E D , W T E R M S I G , W I F S T O P P E D , a n d W S T O P S I G f u n c t i o n s p r o v i d e d b y t h e P O S I X m o d u l e .
U n d e r V M S t h i s r e f l e c t s t h e a c t u a l V M S e x i t s t a t u s ; i . e . i t i s t h e s a m e a s $ ?
w h e n t h e p r a g m a u s e v m s i s h ' s t a t u s '
i s i n e f f e c t .
T h i s v a r i a b l e w a s a d d e d i n P e r l v 5 . 1 0 . 0 .
# $ E X T E N D E D _ O S _ E R R O R
# $ ^ E
E r r o r i n f o r m a t i o n s p e c i f i c t o t h e c u r r e n t o p e r a t i n g s y s t e m . A t t h e m o m e n t , t h i s d i f f e r s f r o m
" $ ! "
u n d e r o n l y V M S , O S / 2 , a n d W i n 3 2 ( a n d f o r M a c P e r l ) . O n a l l o t h e r p l a t f o r m s , $ ^ E
i s a l w a y s j u s t t h e s a m e a s $ !
.
U n d e r V M S , $ ^ E
p r o v i d e s t h e V M S s t a t u s v a l u e f r o m t h e l a s t s y s t e m e r r o r . T h i s i s m o r e s p e c i f i c i n f o r m a t i o n a b o u t t h e l a s t s y s t e m e r r o r t h a n t h a t p r o v i d e d b y $ !
. T h i s i s p a r t i c u l a r l y i m p o r t a n t w h e n $ !
i s s e t t o E V M S E R R .
U n d e r O S / 2 , $ ^ E
i s s e t t o t h e e r r o r c o d e o f t h e l a s t c a l l t o O S / 2 A P I e i t h e r v i a C R T , o r d i r e c t l y f r o m p e r l .
U n d e r W i n 3 2 , $ ^ E
a l w a y s r e t u r n s t h e l a s t e r r o r i n f o r m a t i o n r e p o r t e d b y t h e W i n 3 2 c a l l G e t L a s t
E r r o r ( )
w h i c h d e s c r i b e s t h e l a s t e r r o r f r o m w i t h i n t h e W i n 3 2 A P I . M o s t W i n 3 2 - s p e c i f i c c o d e w i l l r e p o r t e r r o r s v i a $ ^ E
. A N S I C a n d U n i x - l i k e c a l l s s e t e r r n o
a n d s o m o s t p o r t a b l e P e r l c o d e w i l l r e p o r t e r r o r s v i a $ !
.
C a v e a t s m e n t i o n e d i n t h e d e s c r i p t i o n o f " $ ! "
g e n e r a l l y a p p l y t o $ ^ E
, a l s o .
T h i s v a r i a b l e w a s a d d e d i n P e r l 5 . 0 0 3 .
M n e m o n i c : E x t r a e r r o r e x p l a n a t i o n .
# $ E X C E P T I O N S _ B E I N G _ C A U G H T
# $ ^ S
C u r r e n t s t a t e o f t h e i n t e r p r e t e r .
$^S State
--------- -------------------------------------
undef Parsing module, eval, or main program
true (1 ) Executing an eval or try block
false (0) Otherwise
T h e f i r s t s t a t e m a y h a p p e n i n $
S I G { _ _ D I E _ _ }
a n d $ S I G { _ _ W A R N _ _ }
h a n d l e r s .
T h e E n g l i s h n a m e $ E X C E P T I O N S _ B E I N G _ C A U G H T i s s l i g h t l y m i s l e a d i n g , b e c a u s e t h e u n d e f
v a l u e d o e s n o t i n d i c a t e w h e t h e r e x c e p t i o n s a r e b e i n g c a u g h t , s i n c e c o m p i l a t i o n o f t h e m a i n p r o g r a m d o e s n o t c a t c h e x c e p t i o n s .
T h i s v a r i a b l e w a s a d d e d i n P e r l 5 . 0 0 4 .
# $ W A R N I N G
# $ ^ W
T h e c u r r e n t v a l u e o f t h e w a r n i n g s w i t c h , i n i t i a l l y t r u e i f - w w a s u s e d , f a l s e o t h e r w i s e , b u t d i r e c t l y m o d i f i a b l e .
S e e a l s o w a r n i n g s .
M n e m o n i c : r e l a t e d t o t h e - w s w i t c h .
# $ { ^ W A R N I N G _ B I T S }
T h e c u r r e n t s e t o f w a r n i n g c h e c k s e n a b l e d b y t h e u s e w a r n i n g s
p r a g m a . I t h a s t h e s a m e s c o p i n g a s t h e $ ^ H
a n d % ^ H
v a r i a b l e s . T h e e x a c t v a l u e s a r e c o n s i d e r e d i n t e r n a l t o t h e w a r n i n g s p r a g m a a n d m a y c h a n g e b e t w e e n v e r s i o n s o f P e r l .
E a c h t i m e a s t a t e m e n t c o m p l e t e s b e i n g c o m p i l e d , t h e c u r r e n t v a l u e o f $ { ^ W A R N I N G _ B I T S }
i s s t o r e d w i t h t h a t s t a t e m e n t , a n d c a n l a t e r b e r e t r i e v e d v i a ( c a l l e r
( $ l e v e l ) ) [ 9 ]
.
T h i s v a r i a b l e w a s a d d e d i n P e r l v 5 . 6 . 0 .
# $ O S _ E R R O R
# $ E R R N O
# $ !
W h e n r e f e r e n c e d , $ !
r e t r i e v e s t h e c u r r e n t v a l u e o f t h e C e r r n o
i n t e g e r v a r i a b l e . I f $ !
i s a s s i g n e d a n u m e r i c a l v a l u e , t h a t v a l u e i s s t o r e d i n e r r n o
. W h e n r e f e r e n c e d a s a s t r i n g , $ !
y i e l d s t h e s y s t e m e r r o r s t r i n g c o r r e s p o n d i n g t o e r r n o
.
M a n y s y s t e m o r l i b r a r y c a l l s s e t e r r n o
i f t h e y f a i l , t o i n d i c a t e t h e c a u s e o f f a i l u r e . T h e y u s u a l l y d o n o t s e t e r r n o
t o z e r o i f t h e y s u c c e e d a n d m a y s e t e r
r n o
t o a n o n - z e r o v a l u e o n s u c c e s s . T h i s m e a n s e r r n o
, h e n c e $ !
, i s m e a n i n g f u l o n l y i m m e d i a t e l y a f t e r a f a i l u r e :
if (open my $fh, "<", $filename) {
# Here $! is meaningless.
...
}
else {
# ONLY here is $! meaningful.
...
# Already here $! might be meaningless.
}
# Since here we might have either success or failure,
# $! is meaningless.
H e r e , m e a n i n g l e s s m e a n s t h a t $ !
m a y b e u n r e l a t e d t o t h e o u t c o m e o f t h e o p e n ( )
o p e r a t o r . A s s i g n m e n t t o $ !
i s s i m i l a r l y e p h e m e r a l . I t c a n b e u s e d i m m e d i a t e l y b e f o r e i n v o k i n g t h e d i e ( )
o p e r a t o r , t o s e t t h e e x i t v a l u e , o r t o i n s p e c t t h e s y s t e m e r r o r s t r i n g c o r r e s p o n d i n g t o e r r o r n , o r t o r e s t o r e $ !
t o a m e a n i n g f u l s t a t e .
P e r l i t s e l f m a y s e t e r r n o
t o a n o n - z e r o o n f a i l u r e e v e n i f n o s y s t e m c a l l i s p e r f o r m e d .
M n e m o n i c : W h a t j u s t w e n t b a n g ?
# % O S _ E R R O R
# % E R R N O
# % !
E a c h e l e m e n t o f % !
h a s a t r u e v a l u e o n l y i f $ !
i s s e t t o t h a t v a l u e . F o r e x a m p l e , $ ! { E N O E N T }
i s t r u e i f a n d o n l y i f t h e c u r r e n t v a l u e o f $ !
is E N O E N T
; t h a t i s , i f t h e m o s t r e c e n t e r r o r w a s " N o s u c h f i l e o r d i r e c t o r y " ( o r i t s m o r a l e q u i v a l e n t : n o t a l l o p e r a t i n g s y s t e m s g i v e t h a t e x a c t e r r o r , a n d c e r t a i n l y n o t a l l l a n g u a g e s ) . T h e s p e c i f i c t r u e v a l u e i s n o t g u a r a n t e e d , b u t i n t h e p a s t h a s g e n e r a l l y b e e n t h e n u m e r i c v a l u e o f $ !
. T o c h e c k i f a p a r t i c u l a r k e y i s m e a n i n g f u l o n y o u r s y s t e m , u s e e x i s t s $
! { t h e _ k e y }
; f o r a l i s t o f l e g a l k e y s , u s e k e y s % !
. S e e E r r n o f o r m o r e i n f o r m a t i o n , a n d a l s o s e e " $ ! " .
T h i s v a r i a b l e w a s a d d e d i n P e r l 5 . 0 0 5 .
# $ C H I L D _ E R R O R
# $ ?
T h e s t a t u s r e t u r n e d b y t h e l a s t p i p e c l o s e , b a c k t i c k ( ` `
) c o m m a n d , s u c c e s s f u l c a l l t o w a i t ( )
or w a i t p i d ( )
, o r f r o m t h e s y s t e m ( )
o p e r a t o r . T h i s i s j u s t t h e 1 6 - b i t s t a t u s w o r d r e t u r n e d b y t h e t r a d i t i o n a l U n i x w a i t ( )
s y s t e m c a l l ( o r e l s e i s m a d e u p t o l o o k l i k e i t ) . T h u s , t h e e x i t v a l u e o f t h e s u b p r o c e s s i s r e a l l y ( $ ? > > 8
) , a n d $ ? & 1 2 7
g i v e s w h i c h s i g n a l , i f a n y , t h e p r o c e s s d i e d f r o m , a n d $ ? & 1 2 8
r e p o r t s w h e t h e r t h e r e w a s a c o r e d u m p .
A d d i t i o n a l l y , i f t h e h _ e r r n o
v a r i a b l e i s s u p p o r t e d i n C , i t s v a l u e i s r e t u r n e d v i a $ ?
i f a n y g e t h o s t * ( )
f u n c t i o n f a i l s .
I f y o u h a v e i n s t a l l e d a s i g n a l h a n d l e r f o r S I G C H L D
, t h e v a l u e o f $ ?
w i l l u s u a l l y b e w r o n g o u t s i d e t h a t h a n d l e r .
I n s i d e a n E N D
s u b r o u t i n e $ ?
c o n t a i n s t h e v a l u e t h a t i s g o i n g t o b e g i v e n t o e x i t ( )
. Y o u c a n m o d i f y $ ?
i n a n E N D
s u b r o u t i n e t o c h a n g e t h e e x i t s t a t u s o f y o u r p r o g r a m . F o r e x a m p l e :
END {
$? = 1 if $? == 255; # die would make it 255
}
U n d e r V M S , t h e p r a g m a u s e v m s i s
h ' s t a t u s '
m a k e s $ ?
r e f l e c t t h e a c t u a l V M S e x i t s t a t u s , i n s t e a d o f t h e d e f a u l t e m u l a t i o n o f P O S I X s t a t u s ; s e e " $ ? " i n p e r l v m s f o r d e t a i l s .
M n e m o n i c : s i m i l a r t o sh a n d k s h .
# $ E V A L _ E R R O R
# $ @
T h e P e r l e r r o r f r o m t h e l a s t e v
a l
o p e r a t o r , i . e . t h e l a s t e x c e p t i o n t h a t w a s c a u g h t . F o r e v a l
B L O C K
, t h i s i s e i t h e r a r u n t i m e e r r o r m e s s a g e o r t h e s t r i n g o r r e f e r e n c e d i e
w a s c a l l e d w i t h . T h e e v a l S T R I N G
f o r m a l s o c a t c h e s s y n t a x e r r o r s a n d o t h e r c o m p i l e t i m e e x c e p t i o n s .
I f n o e r r o r o c c u r s , e v a l
s e t s $
@
t o t h e e m p t y s t r i n g .
W a r n i n g m e s s a g e s a r e n o t c o l l e c t e d i n t h i s v a r i a b l e . Y o u c a n , h o w e v e r , s e t u p a r o u t i n e t o p r o c e s s w a r n i n g s b y s e t t i n g $ S I G {
_ _ W A R N _ _ }
a s d e s c r i b e d i n " % S I G " .
M n e m o n i c : W h e r e w a s t h e e r r o r " a t " ?
T h e s e v a r i a b l e s p r o v i d e i n f o r m a t i o n a b o u t t h e c u r r e n t i n t e r p r e t e r s t a t e .
# $ C O M P I L I N G
# $ ^ C
T h e c u r r e n t v a l u e o f t h e f l a g a s s o c i a t e d w i t h t h e - c s w i t c h . M a i n l y o f u s e w i t h - M O = . . . t o a l l o w c o d e t o a l t e r i t s b e h a v i o r w h e n b e i n g c o m p i l e d , s u c h a s f o r e x a m p l e t o A U T O L O A D
a t c o m p i l e t i m e r a t h e r t h a n n o r m a l , d e f e r r e d l o a d i n g . S e t t i n g $ ^ C = 1
i s s i m i l a r t o c a l l i n g B : : m i n u s _ c
.
T h i s v a r i a b l e w a s a d d e d i n P e r l v 5 . 6 . 0 .
# $ D E B U G G I N G
# $ ^ D
T h e c u r r e n t v a l u e o f t h e d e b u g g i n g f l a g s . M a y b e r e a d o r s e t . L i k e i t s c o m m a n d - l i n e e q u i v a l e n t , y o u c a n u s e n u m e r i c o r s y m b o l i c v a l u e s , e . g . $ ^ D = 1 0
or $ ^ D =
" s t "
. S e e " - D n u m b e r " i n p e r l r u n . T h e c o n t e n t s o f t h i s v a r i a b l e a l s o a f f e c t s t h e d e b u g g e r o p e r a t i o n . S e e " D e b u g g e r I n t e r n a l s " i n p e r l d e b g u t s .
M n e m o n i c : v a l u e o f - D s w i t c h .
# $ { ^ G L O B A L _ P H A S E }
T h e c u r r e n t p h a s e o f t h e p e r l i n t e r p r e t e r .
P o s s i b l e v a l u e s a r e :
# C O N S T R U C T
T h e P e r l I n t e r p r e t e r *
i s b e i n g c o n s t r u c t e d v i a p e r l _ c o n s t r u
c t
. T h i s v a l u e i s m o s t l y t h e r e f o r c o m p l e t e n e s s a n d f o r u s e v i a t h e u n d e r l y i n g C v a r i a b l e P L _ p h a s e
. I t ' s n o t r e a l l y p o s s i b l e f o r P e r l c o d e t o b e e x e c u t e d u n l e s s c o n s t r u c t i o n o f t h e i n t e r p r e t e r i s f i n i s h e d .
# S T A R T
T h i s i s t h e g l o b a l c o m p i l e - t i m e . T h a t i n c l u d e s , b a s i c a l l y , e v e r y B E G I N
b l o c k e x e c u t e d d i r e c t l y o r i n d i r e c t l y f r o m d u r i n g t h e c o m p i l e - t i m e o f t h e t o p - l e v e l p r o g r a m .
T h i s p h a s e i s n o t c a l l e d " B E G I N " t o a v o i d c o n f u s i o n w i t h B
E G I N
- b l o c k s , a s t h o s e a r e e x e c u t e d d u r i n g c o m p i l e - t i m e o f a n y c o m p i l a t i o n u n i t , n o t j u s t t h e t o p - l e v e l p r o g r a m . A n e w , l o c a l i s e d c o m p i l e - t i m e e n t e r e d a t r u n - t i m e , f o r e x a m p l e b y c o n s t r u c t s a s e v a l " u s e S
o m e M o d u l e "
a r e n o t g l o b a l i n t e r p r e t e r p h a s e s , a n d t h e r e f o r e a r e n ' t r e f l e c t e d b y $ { ^ G L O B
A L _ P H A S E }
.
# C H E C K
E x e c u t i o n o f a n y C H E C K
b l o c k s .
# I N I T
S i m i l a r t o " C H E C K " , b u t f o r I
N I T
- b l o c k s , n o t C H E C K
b l o c k s .
# R U N
T h e m a i n r u n - t i m e , i . e . t h e e x e c u t i o n o f P L _ m a i n _ r o o t
.
# E N D
E x e c u t i o n o f a n y E N D
b l o c k s .
# D E S T R U C T
G l o b a l d e s t r u c t i o n .
A l s o n o t e t h a t t h e r e ' s n o v a l u e f o r U N I T C H E C K - b l o c k s . T h a t ' s b e c a u s e t h o s e a r e r u n f o r e a c h c o m p i l a t i o n u n i t i n d i v i d u a l l y , a n d t h e r e f o r e i s n o t a g l o b a l i n t e r p r e t e r p h a s e .
N o t e v e r y p r o g r a m h a s t o g o t h r o u g h e a c h o f t h e p o s s i b l e p h a s e s , b u t t r a n s i t i o n f r o m o n e p h a s e t o a n o t h e r c a n o n l y h a p p e n i n t h e o r d e r d e s c r i b e d i n t h e a b o v e l i s t .
A n e x a m p l e o f a l l o f t h e p h a s e s P e r l c o d e c a n s e e :
BEGIN { print "compile-time: ${^GLOBAL_PHASE}\n" }
INIT { print "init-time: ${^GLOBAL_PHASE}\n" }
CHECK { print "check-time: ${^GLOBAL_PHASE}\n" }
{
package Print::Phase;
sub new {
my ($class, $time) = @_;
return bless \$time, $class;
}
sub DESTROY {
my $self = shift;
print "$$self: ${^GLOBAL_PHASE}\n";
}
}
print "run-time: ${^GLOBAL_PHASE}\n";
my $runtime = Print::Phase->new(
"lexical variables are garbage collected before END"
);
END { print "end-time: ${^GLOBAL_PHASE}\n" }
our $destruct = Print::Phase->new(
"package variables are garbage collected after END"
);
T h i s w i l l p r i n t o u t
compile-time: START
check-time: CHECK
init-time: INIT
run-time: RUN
lexical variables are garbage collected before END: RUN
end-time: END
package variables are garbage collected after END: DESTRUCT
T h i s v a r i a b l e w a s a d d e d i n P e r l 5 . 1 4 . 0 .
# $ ^ H
W A R N I N G : T h i s v a r i a b l e i s s t r i c t l y f o r i n t e r n a l u s e o n l y . I t s a v a i l a b i l i t y , b e h a v i o r , a n d c o n t e n t s 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 v a r i a b l e c o n t a i n s c o m p i l e - t i m e h i n t s f o r t h e P e r l i n t e r p r e t e r . A t t h e e n d o f c o m p i l a t i o n o f a B L O C K t h e v a l u e o f t h i s v a r i a b l e i s r e s t o r e d t o t h e v a l u e w h e n t h e i n t e r p r e t e r s t a r t e d t o c o m p i l e t h e B L O C K .
E a c h t i m e a s t a t e m e n t c o m p l e t e s b e i n g c o m p i l e d , t h e c u r r e n t v a l u e o f $ ^ H
i s s t o r e d w i t h t h a t s t a t e m e n t , a n d c a n l a t e r b e r e t r i e v e d v i a ( c a l l e r ( $ l e v e l ) ) [ 8 ]
.
W h e n p e r l b e g i n s t o p a r s e a n y b l o c k c o n s t r u c t t h a t p r o v i d e s a l e x i c a l s c o p e ( e . g . , e v a l b o d y , r e q u i r e d f i l e , s u b r o u t i n e b o d y , l o o p b o d y , o r c o n d i t i o n a l b l o c k ) , t h e e x i s t i n g v a l u e o f $ ^ H
i s s a v e d , b u t i t s v a l u e i s l e f t u n c h a n g e d . W h e n t h e c o m p i l a t i o n o f t h e b l o c k i s c o m p l e t e d , i t r e g a i n s t h e s a v e d v a l u e . B e t w e e n t h e p o i n t s w h e r e i t s v a l u e i s s a v e d a n d r e s t o r e d , c o d e t h a t e x e c u t e s w i t h i n B E G I N b l o c k s i s f r e e t o c h a n g e t h e v a l u e o f $ ^
H
.
T h i s b e h a v i o r p r o v i d e s t h e s e m a n t i c o f l e x i c a l s c o p i n g , a n d i s u s e d i n , f o r i n s t a n c e , t h e u s e
s t r i c t
p r a g m a .
T h e c o n t e n t s s h o u l d b e a n i n t e g e r ; d i f f e r e n t b i t s o f i t a r e u s e d f o r d i f f e r e n t p r a g m a t i c f l a g s . H e r e ' s a n e x a m p l e :
sub add_100 { $^H |= 0x100 }
sub foo {
BEGIN { add_100() }
bar->baz($boon);
}
C o n s i d e r w h a t h a p p e n s d u r i n g e x e c u t i o n o f t h e B E G I N b l o c k . A t t h i s p o i n t t h e B E G I N b l o c k h a s a l r e a d y b e e n c o m p i l e d , b u t t h e b o d y o f f o o ( )
i s s t i l l b e i n g c o m p i l e d . T h e n e w v a l u e o f $ ^ H
w i l l t h e r e f o r e b e v i s i b l e o n l y w h i l e t h e b o d y o f f o o ( )
i s b e i n g c o m p i l e d .
S u b s t i t u t i o n o f B E G I N { a d d _ 1 0 0
( ) }
b l o c k w i t h :
BEGIN { require strict; strict->import('vars') }
d e m o n s t r a t e s h o w u s e s t r i c t ' v a
r s '
i s i m p l e m e n t e d . H e r e ' s a c o n d i t i o n a l v e r s i o n o f t h e s a m e l e x i c a l p r a g m a :
BEGIN {
require strict; strict->import('vars') if $condition
}
T h i s v a r i a b l e w a s a d d e d i n P e r l 5 . 0 0 3 .
# % ^ H
T h e % ^ H
h a s h p r o v i d e s t h e s a m e s c o p i n g s e m a n t i c a s $ ^ H
. T h i s m a k e s i t u s e f u l f o r i m p l e m e n t a t i o n o f l e x i c a l l y s c o p e d p r a g m a s . S e e p e r l p r a g m a . A l l t h e e n t r i e s a r e s t r i n g i f i e d w h e n a c c e s s e d a t r u n t i m e , s o o n l y s i m p l e v a l u e s c a n b e a c c o m m o d a t e d . T h i s m e a n s n o p o i n t e r s t o o b j e c t s , f o r e x a m p l e .
E a c h t i m e a s t a t e m e n t c o m p l e t e s b e i n g c o m p i l e d , t h e c u r r e n t v a l u e o f % ^ H
i s s t o r e d w i t h t h a t s t a t e m e n t , a n d c a n l a t e r b e r e t r i e v e d v i a ( c a l l e r ( $ l e v e l ) ) [ 10 ]
.
W h e n p u t t i n g i t e m s i n t o % ^ H
, i n o r d e r t o a v o i d c o n f l i c t i n g w i t h o t h e r u s e r s o f t h e h a s h t h e r e i s a c o n v e n t i o n r e g a r d i n g w h i c h k e y s t o u s e . A m o d u l e s h o u l d u s e o n l y k e y s t h a t b e g i n w i t h t h e m o d u l e ' s n a m e ( t h e n a m e o f i t s m a i n p a c k a g e ) a n d a " / " c h a r a c t e r . F o r e x a m p l e , a m o d u l e F o
o : : B a r
s h o u l d u s e k e y s s u c h a s F o o : : B a r / b a z
.
T h i s v a r i a b l e w a s a d d e d i n P e r l v 5 . 6 . 0 .
# $ { ^ O P E N }
A n i n t e r n a l v a r i a b l e u s e d b y P e r l I O . A s t r i n g i n t w o p a r t s , s e p a r a t e d b y a \ 0
b y t e , t h e f i r s t p a r t d e s c r i b e s t h e i n p u t l a y e r s , t h e s e c o n d p a r t d e s c r i b e s t h e o u t p u t l a y e r s .
T h i s i s t h e m e c h a n i s m t h a t a p p l i e s t h e l e x i c a l e f f e c t s o f t h e o p e n p r a g m a , a n d t h e m a i n p r o g r a m s c o p e e f f e c t s o f t h e io
or D
o p t i o n s f o r t h e - C c o m m a n d - l i n e s w i t c h a n d P E R L _ U N I C O D E e n v i r o n m e n t v a r i a b l e .
T h e f u n c t i o n s a c c e p t ( )
, o p e n ( )
, p i p e ( )
, r e a d p i p e ( )
( a s w e l l a s t h e r e l a t e d qx
a n d ` S T R I N G `
o p e r a t o r s ) , s o c k e t ( )
, s o c k e t p a i r ( )
, a n d s y s o p e n ( )
a r e a f f e c t e d b y t h e l e x i c a l v a l u e o f t h i s v a r i a b l e . T h e i m p l i c i t " A R G V " h a n d l e o p e n e d b y r e a d l i n e ( )
( o r t h e r e l a t e d < >
a n d < < > >
o p e r a t o r s ) o n p a s s e d f i l e n a m e s i s a l s o a f f e c t e d ( b u t n o t i f i t o p e n s S T D I N
) . I f t h i s v a r i a b l e i s n o t s e t , t h e s e f u n c t i o n s w i l l s e t t h e d e f a u l t l a y e r s a s d e s c r i b e d i n " D e f a u l t s a n d h o w t o o v e r r i d e t h e m " i n P e r l I O .
o p e n ( )
i g n o r e s t h i s v a r i a b l e ( a n d t h e d e f a u l t l a y e r s ) w h e n c a l l e d w i t h 3 a r g u m e n t s a n d e x p l i c i t l a y e r s a r e s p e c i f i e d . I n d i r e c t c a l l s t o t h e s e f u n c t i o n s v i a m o d u l e s l i k e I O : : H a n d l e a r e n o t a f f e c t e d a s t h e y o c c u r i n a d i f f e r e n t l e x i c a l s c o p e . D i r e c t o r y h a n d l e s s u c h a s o p e n e d b y o p
e n d i r ( )
a r e n o t c u r r e n t l y a f f e c t e d .
T h i s v a r i a b l e w a s a d d e d i n P e r l v 5 . 8 . 0 .
# $ P E R L D B
# $ ^ P
T h e i n t e r n a l v a r i a b l e f o r d e b u g g i n g s u p p o r t . T h e m e a n i n g s o f t h e v a r i o u s b i t s a r e s u b j e c t t o c h a n g e , b u t c u r r e n t l y i n d i c a t e :
# 0 x 0 1
D e b u g s u b r o u t i n e e n t e r / e x i t .
# 0 x 0 2
L i n e - b y - l i n e d e b u g g i n g . C a u s e s D B : : D B ( )
s u b r o u t i n e t o b e c a l l e d f o r e a c h s t a t e m e n t e x e c u t e d . A l s o c a u s e s s a v i n g s o u r c e c o d e l i n e s ( l i k e 0 x 4 0 0 ) .
# 0 x 0 4
S w i t c h o f f o p t i m i z a t i o n s .
# 0 x 0 8
P r e s e r v e m o r e d a t a f o r f u t u r e i n t e r a c t i v e i n s p e c t i o n s .
# 0 x 1 0
K e e p i n f o a b o u t s o u r c e l i n e s o n w h i c h a s u b r o u t i n e i s d e f i n e d .
# 0 x 2 0
S t a r t w i t h s i n g l e - s t e p o n .
# 0 x 4 0
U s e s u b r o u t i n e a d d r e s s i n s t e a d o f n a m e w h e n r e p o r t i n g .
# 0 x 8 0
R e p o r t g o t o & s u b r o u t i n e
a s w e l l .
# 0 x 1 0 0
P r o v i d e i n f o r m a t i v e " f i l e " n a m e s f o r e v a l s b a s e d o n t h e p l a c e t h e y w e r e c o m p i l e d .
# 0 x 2 0 0
P r o v i d e i n f o r m a t i v e n a m e s t o a n o n y m o u s s u b r o u t i n e s b a s e d o n t h e p l a c e t h e y w e r e c o m p i l e d .
# 0 x 4 0 0
S a v e s o u r c e c o d e l i n e s i n t o @
{ " _ < $ f i l e n a m e " }
.
# 0 x 8 0 0
W h e n s a v i n g s o u r c e , i n c l u d e e v a l s t h a t g e n e r a t e n o s u b r o u t i n e s .
# 0 x 1 0 0 0
W h e n s a v i n g s o u r c e , i n c l u d e s o u r c e t h a t d i d n o t c o m p i l e .
S o m e b i t s m a y b e r e l e v a n t a t c o m p i l e - t i m e o n l y , s o m e a t r u n - t i m e o n l y . T h i s i s a n e w m e c h a n i s m a n d t h e d e t a i l s m a y c h a n g e . S e e a l s o p e r l d e b g u t s .
# $ { ^ T A I N T }
R e f l e c t s i f t a i n t m o d e i s o n o r o f f . 1 f o r o n ( t h e p r o g r a m w a s r u n w i t h - T ) , 0 f o r o f f , - 1 w h e n o n l y t a i n t w a r n i n g s a r e e n a b l e d ( i . e . w i t h - t or - T U ) .
N o t e : i f y o u r p e r l w a s b u i l t w i t h o u t t a i n t s u p p o r t ( s e e p e r l s e c ) , t h e n $ { ^ T A I N T }
w i l l a l w a y s b e 0 , e v e n i f t h e p r o g r a m w a s r u n w i t h - T ) .
T h i s v a r i a b l e i s r e a d - o n l y .
T h i s v a r i a b l e w a s a d d e d i n P e r l v 5 . 8 . 0 .
# $ { ^ S A F E _ L O C A L E S }
R e f l e c t s i f s a f e l o c a l e o p e r a t i o n s a r e a v a i l a b l e t o t h i s p e r l ( w h e n t h e v a l u e i s 1 ) o r n o t ( t h e v a l u e i s 0 ) . T h i s v a r i a b l e i s a l w a y s 1 i f t h e p e r l h a s b e e n c o m p i l e d w i t h o u t t h r e a d s . I t i s a l s o 1 i f t h i s p e r l i s u s i n g t h r e a d - s a f e l o c a l e o p e r a t i o n s . N o t e t h a t a n i n d i v i d u a l t h r e a d m a y c h o o s e t o u s e t h e g l o b a l l o c a l e ( g e n e r a l l y u n s a f e ) b y c a l l i n g " s w i t c h _ t o _ g l o b a l _ l o c a l e " i n p e r l a p i . T h i s v a r i a b l e c u r r e n t l y i s s t i l l s e t t o 1 i n s u c h t h r e a d s .
T h i s v a r i a b l e i s r e a d - o n l y .
T h i s v a r i a b l e w a s a d d e d i n P e r l v 5 . 2 8 . 0 .
# $ { ^ U N I C O D E }
R e f l e c t s c e r t a i n U n i c o d e s e t t i n g s o f P e r l . S e e p e r l r u n d o c u m e n t a t i o n f o r t h e - C
s w i t c h f o r m o r e i n f o r m a t i o n a b o u t t h e p o s s i b l e v a l u e s .
T h i s v a r i a b l e i s s e t d u r i n g P e r l s t a r t u p a n d i s t h e r e a f t e r r e a d - o n l y .
T h i s v a r i a b l e w a s a d d e d i n P e r l v 5 . 8 . 2 .
# $ { ^ U T F 8 C A C H E }
T h i s v a r i a b l e c o n t r o l s t h e s t a t e o f t h e i n t e r n a l U T F - 8 o f f s e t c a c h i n g c o d e . 1 f o r o n ( t h e d e f a u l t ) , 0 f o r o f f , - 1 t o d e b u g t h e c a c h i n g c o d e b y c h e c k i n g a l l i t s r e s u l t s a g a i n s t l i n e a r s c a n s , a n d p a n i c k i n g o n a n y d i s c r e p a n c y .
T h i s v a r i a b l e w a s a d d e d i n P e r l v 5 . 8 . 9 . I t i s s u b j e c t t o c h a n g e o r r e m o v a l w i t h o u t n o t i c e , b u t i s c u r r e n t l y u s e d t o a v o i d r e c a l c u l a t i n g t h e b o u n d a r i e s o f m u l t i - b y t e U T F - 8 - e n c o d e d c h a r a c t e r s .
# $ { ^ U T F 8 L O C A L E }
T h i s v a r i a b l e i n d i c a t e s w h e t h e r a U T F - 8 l o c a l e w a s d e t e c t e d b y p e r l a t s t a r t u p . T h i s i n f o r m a t i o n i s u s e d b y p e r l w h e n i t ' s i n a d j u s t - u t f 8 n e s s - t o - l o c a l e m o d e ( a s w h e n r u n w i t h t h e - C L
c o m m a n d - l i n e s w i t c h ) ; s e e p e r l r u n f o r m o r e i n f o o n t h i s .
T h i s v a r i a b l e w a s a d d e d i n P e r l v 5 . 8 . 8 .
# D e p r e c a t e d a n d r e m o v e d v a r i a b l e s
D e p r e c a t i n g a v a r i a b l e a n n o u n c e s t h e i n t e n t o f t h e p e r l m a i n t a i n e r s t o e v e n t u a l l y r e m o v e t h e v a r i a b l e f r o m t h e l a n g u a g e . I t m a y s t i l l b e a v a i l a b l e d e s p i t e i t s s t a t u s . U s i n g a d e p r e c a t e d v a r i a b l e t r i g g e r s a w a r n i n g .
O n c e a v a r i a b l e i s r e m o v e d , i t s u s e t r i g g e r s a n e r r o r t e l l i n g y o u t h e v a r i a b l e i s u n s u p p o r t e d .
S e e p e r l d i a g f o r d e t a i l s a b o u t e r r o r m e s s a g e s .
# $ #
$ #
w a s a v a r i a b l e t h a t c o u l d b e u s e d t o f o r m a t p r i n t e d n u m b e r s . A f t e r a d e p r e c a t i o n c y c l e , i t s m a g i c w a s r e m o v e d i n P e r l v 5 . 1 0 . 0 a n d u s i n g i t n o w t r i g g e r s a w a r n i n g : $ # i s n o l o n g e r s u p p o
r t e d
.
T h i s i s n o t t h e s i g i l y o u u s e i n f r o n t o f a n a r r a y n a m e t o g e t t h e l a s t i n d e x , l i k e $ # a r r a y
. T h a t ' s s t i l l h o w y o u g e t t h e l a s t i n d e x o f a n a r r a y i n P e r l . T h e t w o h a v e n o t h i n g t o d o w i t h e a c h o t h e r .
D e p r e c a t e d i n P e r l 5 .
R e m o v e d i n P e r l v 5 . 1 0 . 0 .
# $ *
$ *
w a s a v a r i a b l e t h a t y o u c o u l d u s e t o e n a b l e m u l t i l i n e m a t c h i n g . A f t e r a d e p r e c a t i o n c y c l e , i t s m a g i c w a s r e m o v e d i n P e r l v 5 . 1 0 . 0 . U s i n g i t n o w t r i g g e r s a w a r n i n g : $ * i s n o l o n g e r s u p p
o r t e d
. Y o u s h o u l d u s e t h e / s
a n d / m
r e g e x p m o d i f i e r s i n s t e a d .
D e p r e c a t e d i n P e r l 5 .
R e m o v e d i n P e r l v 5 . 1 0 . 0 .
# $ [
T h i s v a r i a b l e s t o r e s t h e i n d e x o f t h e f i r s t e l e m e n t i n a n a r r a y , a n d o f t h e f i r s t c h a r a c t e r i n a s u b s t r i n g . T h e d e f a u l t i s 0 , b u t y o u c o u l d t h e o r e t i c a l l y s e t i t t o 1 t o m a k e P e r l b e h a v e m o r e l i k e a w k ( o r F o r t r a n ) w h e n s u b s c r i p t i n g a n d w h e n e v a l u a t i n g t h e i n d e x ( ) a n d s u b s t r ( ) f u n c t i o n s .
A s o f r e l e a s e 5 o f P e r l , a s s i g n m e n t t o $ [
i s t r e a t e d a s a c o m p i l e r d i r e c t i v e , a n d c a n n o t i n f l u e n c e t h e b e h a v i o r o f a n y o t h e r f i l e . ( T h a t ' s w h y y o u c a n o n l y a s s i g n c o m p i l e - t i m e c o n s t a n t s t o i t . ) I t s u s e i s h i g h l y d i s c o u r a g e d .
P r i o r t o P e r l v 5 . 1 0 . 0 , a s s i g n m e n t t o $ [
c o u l d b e s e e n f r o m o u t e r l e x i c a l s c o p e s i n t h e s a m e f i l e , u n l i k e o t h e r c o m p i l e - t i m e d i r e c t i v e s ( s u c h a s s t r i c t ) . U s i n g l o c a l ( ) o n i t w o u l d b i n d i t s v a l u e s t r i c t l y t o a l e x i c a l b l o c k . N o w i t i s a l w a y s l e x i c a l l y s c o p e d .
A s o f P e r l v 5 . 1 6 . 0 , i t i s i m p l e m e n t e d b y t h e a r y b a s e m o d u l e .
A s o f P e r l v 5 . 3 0 . 0 , o r u n d e r u s
e v 5 . 1 6
, o r n o f e a t u r e " a r r a y _ b
a s e "
, $ [
n o l o n g e r h a s a n y e f f e c t , a n d a l w a y s c o n t a i n s 0 . A s s i g n i n g 0 t o i t i s p e r m i t t e d , b u t a n y o t h e r v a l u e w i l l p r o d u c e a n e r r o r .
M n e m o n i c : [ b e g i n s s u b s c r i p t s .
D e p r e c a t e d i n P e r l v 5 . 1 2 . 0 .
# $ { ^ E N C O D I N G }
T h i s v a r i a b l e i s n o l o n g e r s u p p o r t e d .
I t u s e d t o h o l d t h e o b j e c t r e f e r e n c e t o t h e E n c o d e
o b j e c t t h a t w a s u s e d t o c o n v e r t t h e s o u r c e c o d e t o U n i c o d e .
I t s p u r p o s e w a s t o a l l o w y o u r n o n - A S C I I P e r l s c r i p t s n o t t o h a v e t o b e w r i t t e n i n U T F - 8 ; t h i s w a s u s e f u l b e f o r e e d i t o r s t h a t w o r k e d o n U T F - 8 e n c o d e d t e x t w e r e c o m m o n , b u t t h a t w a s l o n g a g o . I t c a u s e d p r o b l e m s , s u c h a s a f f e c t i n g t h e o p e r a t i o n o f o t h e r m o d u l e s t h a t w e r e n ' t e x p e c t i n g i t , c a u s i n g g e n e r a l m a y h e m .
I f y o u n e e d s o m e t h i n g l i k e t h i s f u n c t i o n a l i t y , i t i s r e c o m m e n d e d t h a t u s e y o u a s i m p l e s o u r c e f i l t e r , s u c h a s F i l t e r : : E n c o d i n g .
I f y o u a r e c o m i n g h e r e b e c a u s e c o d e o f y o u r s i s b e i n g a d v e r s e l y a f f e c t e d b y s o m e o n e ' s u s e o f t h i s v a r i a b l e , y o u c a n u s u a l l y w o r k a r o u n d i t b y d o i n g t h i s :
local ${^ENCODING};
n e a r t h e b e g i n n i n g o f t h e f u n c t i o n s t h a t a r e g e t t i n g b r o k e n . T h i s u n d e f i n e s t h e v a r i a b l e d u r i n g t h e s c o p e o f e x e c u t i o n o f t h e i n c l u d i n g f u n c t i o n .
T h i s v a r i a b l e w a s a d d e d i n P e r l 5 . 8 . 2 a n d r e m o v e d i n 5 . 2 6 . 0 . S e t t i n g i t t o a n y t h i n g o t h e r t h a n u n d e f
w a s m a d e f a t a l i n P e r l 5 . 2 8 . 0 .
# $ { ^ W I N 3 2 _ S L O P P Y _ S T A T }
T h i s v a r i a b l e n o l o n g e r h a s a n y f u n c t i o n .
T h i s v a r i a b l e w a s a d d e d i n P e r l v 5 . 1 0 . 0 a n d r e m o v e d i n P e r l v 5 . 3 4 . 0 .
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 .