P e r l d o c B r o w s e r
●
5 . 8 . 8
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 . 8 . 8 .
V i e w t h e l a t e s t v e r s i o n
●
N A M E
●
D E S C R I P T I O N
●
P r e d e f i n e d N a m e s
●
E r r o r I n d i c a t o r s
●
T e c h n i c a l N o t e 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
●
B U G 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
# P r e d e f i n e d N a m 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 . I n g e n e r a l , i t ' s b e s t t o u s e t h e
use English '-no_match_vars';
i n v o c a t i o n i f y o u d o n ' t n e e d $ P R E M A T C H , $ M A T C H , o r $ P O S T M A T C H , a s i t a v o i d s a c e r t a i n p e r f o r m a n c e h i t w i t h t h e u s e o f r e g u l a r e x p r e s s i o n s . S e e E n g l i s h .
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..5){
nasty_break();
print "$_ ";
}
sub nasty_break {
$_ = 5;
# 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 :
1 2 3 4 5
b u t i n s t e a d y o u g e t :
5 5 5 5 5
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 f i x i s t o a d d l o c a l ( ) :
local $_ = 5;
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 .
T h e f o l l o w i n g l i s t i s o r d e r e d b y s c a l a r v a r i a b l e s f i r s t , t h e n t h e a r r a y s , t h e n t h e h a s h 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 :
V a r i o u s u n a r y f u n c t i o n s , i n c l u d i n g f u n c t i o n s l i k e o r d ( ) a n d i n t ( ) , a s w e l l a s t h e 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 .
V a r i o u s l i s t f u n c t i o n s l i k e p r i n t ( ) a n d u n l i n k ( ) .
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 / / /
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 d e f a u l t p l a c e t o p u t a n i n p u t r e c o r d w h e n a < F 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 .
( 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
# $ 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 .
# $ < d i g i t s >
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 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 . ( M n e m o n i c : l i k e \ d i g i t s . ) T h e s e v a r i a b l e s a r e a l l 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 t o t h e c u r r e n t B L O C K .
# $ 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 ) . ( 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 . ) 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 t o t h e c u r r e n t B L O C K .
T h e u s e o f t h i s v a r i a b l e a n y w h e r e i n a p r o g r a m i m p o s e s 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 o n a l l r e g u l a r e x p r e s s i o n m a t c h e s . S e e " B U G S " .
# $ 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 ) . ( 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 . ) T h i s v a r i a b l e i s r e a d - o n l y .
T h e u s e o f t h i s v a r i a b l e a n y w h e r e i n a p r o g r a m i m p o s e s 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 o n a l l r e g u l a r e x p r e s s i o n m a t c h e s . S e e " B U G S " .
# $ 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 ) . ( 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 . ) E x a m p l e :
local $_ = 'abcdefghi';
/def/;
print "$`:$&:$'\n"; # prints abc:def:ghi
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 t o t h e c u r r e n t B L O C K .
T h e u s e o f t h i s v a r i a b l e a n y w h e r e i n a p r o g r a m i m p o s e s 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 o n a l l r e g u l a r e x p r e s s i o n m a t c h e s . S e e " B U G S " .
# $ 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 l a s t b r a c k e t 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 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 = $+);
( 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 . ) 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 t o t h e c u r r e n t B L O C K .
# $ ^ 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 . ( 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 . )
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 i s d y n a m i c a l l y s c o p e d t o t h e c u r r e n t B L O C K .
# @ 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 .
# $ *
S e t t o a n o n - z e r o i n t e g e r v a l u e t o d o m u l t i - l i n e m a t c h i n g w i t h i n a s t r i n g , 0 ( o r u n d e f i n e d ) t o t e l l P e r l t h a t i t c a n a s s u m e t h a t s t r i n g s c o n t a i n a s i n g l e l i n e , f o r t h e p u r p o s e o f o p t i m i z i n g p a t t e r n m a t c h e s . P a t t e r n m a t c h e s o n s t r i n g s c o n t a i n i n g m u l t i p l e n e w l i n e s c a n p r o d u c e c o n f u s i n g r e s u l t s w h e n $ *
i s 0 o r u n d e f i n e d . D e f a u l t i s u n d e f i n e d . ( M n e m o n i c : * m a t c h e s m u l t i p l e t h i n g s . ) T h i s v a r i a b l e i n f l u e n c e s t h e i n t e r p r e t a t i o n o f o n l y ^
a n d $
. A l i t e r a l n e w l i n e c a n b e s e a r c h e d f o r e v e n w h e n $ * = =
0
.
U s e o f $ *
i s d e p r e c a t e d i n m o d e r n P e r l , s u p p l a n t e d b y t h e / s
a n d / m
m o d i f i e r s o n p a t t e r n m a t c h i n g .
A s s i g n i n g a n o n - n u m e r i c a l v a l u e t o $ *
t r i g g e r s a w a r n i n g ( a n d m a k e s $ *
a c t i f $ * = = 0
) , w h i l e a s s i g n i n g a n u m e r i c a l v a l u e t o $ *
m a k e s t h a t a n i m p l i c i t i n t
i s a p p l i e d o n t h e v a l u 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 )
# $ 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 ( ) o r < >
) , o r w h e n t e l l ( ) o r 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 . ( 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 . )
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 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 . 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 b y t e s f r o m F I L E . 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 .
O n V M S , r e c o r d r e a d s a r e d o n e w i t h t h e e q u i v a l e n t o f s y s r e a d
, s o i t ' s b e s t n o t t o 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 . ( T h i s i s u n l i k e l y t o b e a p r o b l e m , b e c a u s e a n y f i l e y o u ' d w a n t t o r e a d i n r e c o r d m o d e i s p r o b a b l y u n u s a b l e i n l i n e m o d e . ) N o n - V M S s y s t e m s d o n o r m a l I / O , s o i t ' s s a f e t o 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 f a f i 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 $ .
.
# 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 . ( 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 . )
# 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
. ( 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 . )
# 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
. ( 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 . )
# $ L I S T _ S E P A R A T O R
# $ "
T h i s i s l i k e $ ,
e x c e p t t h a t i t a p p l i e s t o a r r a y a n d s l i c e v a l u e 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 s i m i l a r i n t e r p r e t e d s t r i n g ) . D e f a u l t i s a s p a c e . ( M n e m o n i c : o b v i o u s , I t h i n k . )
# $ 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{$a,$b,$c}
i t r e a l l y m e a n s
$foo{join($;, $a, $b, $c)}
B u t d o n ' t p u t
@foo{$a,$b,$c} # a slice--note the @
w h i c h m e a n s
($foo{$a},$foo{$b},$foo{$c})
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 $ ;
. ( 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 . Y e a h , I k n o w , i t ' s p r e t t y l a m e , b u t $ ,
i s a l r e a d y t a k e n f o r s o m e t h i n g m o r e i m p o r t a n t . )
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 .
# $ #
T h e o u t p u t f o r m a t f o r p r i n t e d n u m b e r s . T h i s v a r i a b l e i s a h a l f - h e a r t e d a t t e m p t t o e m u l a t e a w k ' s O F M T v a r i a b l e . T h e r e a r e t i m e s , h o w e v e r , w h e n a w k a n d P e r l h a v e d i f f e r i n g n o t i o n s o f w h a t c o u n t s a s n u m e r i c . T h e i n i t i a l v a l u e i s " % . n g " , w h e r e n i s t h e v a l u e o f t h e m a c r o D B L _ D I G f r o m y o u r s y s t e m ' s f l o a t . h . T h i s i s d i f f e r e n t f r o m a w k ' s d e f a u l t O F M T s e t t i n g o f " % . 6 g " , s o y o u n e e d t o s e t $ #
e x p l i c i t l y t o g e t a w k ' s v a l u e . ( M n e m o n i c : # i s t h e n u m b e r s i g n . )
U s e o f $ #
i s d e p r e c a t e d .
# 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 . U s e d w i t h f o r m a t s . ( 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 _ 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 . D e f a u l t i s 6 0 . U s e d w i t h f o r m a t s . ( 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 _ 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 . U s e d w i t h f o r m a t s . ( 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 . )
# @ 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 ] )
# 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 . 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 . ( M n e m o n i c : b r o t h e r t o $ ^
. )
# 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 . 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 . ( 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 . )
# 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 . D e f a u l t i s " \ n - " , t o b r e a k o n w h i t e s p a c e o r h y p h e n s . ( 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 . )
# 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 . D e f a u l t i s \ f .
# $ 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 ( ) " i n p e r l f u n c .
# $ 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 ( ) o r 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 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 . ( M n e m o n i c : s i m i l a r t o sh a n d k s h . )
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 .
A l s o s e e " E r r o r I n d i c a t o r s " .
# $ { ^ E N C O D I N G }
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 i 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 . T h a n k s t o t h i s v a r i a b l e y o u r p e r l s c r i p t d o e s n o t h a v e t o b e w r i t t e n i n U T F - 8 . D e f a u l t i s u n d e f . T h e d i r e c t m a n i p u l a t i o n o f t h i s v a r i a b l e i s h i g h l y d i s c o u r a g e d . S e e e n c o d i n g f o r m o r e d e t a i l s .
# $ O S _ E R R O R
# $ E R R N O
# $ !
I f u s e d n u m e r i c a l l y , y i e l d 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
v a r i a b l e , o r i n o t h e r w o r d s , i f a s y s t e m o r l i b r a r y c a l l f a i l s , i t s e t s t h i s v a r i a b l e . T h i s m e a n s t h a t t h e v a l u e o f $ !
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(FH, $filename)) {
# Here $! is meaningless.
...
} else {
# ONLY here is $! meaningful.
...
# Already here $! might be meaningless.
}
# Since here we might have either success or failure,
# here $! is meaningless.
I n t h e a b o v e m e a n i n g l e s s s t a n d s f o r a n y t h i n g : z e r o , n o n - z e r o , u n d e f
. A s u c c e s s f u l s y s t e m o r l i b r a r y c a l l d o e s n o t s e t t h e v a r i a b l e t o z e r o .
I f u s e d a s a s t r i n g , y i e l d s t h e c o r r e s p o n d i n g s y s t e m e r r o r s t r i n g . Y o u c a n a s s i g n a n u m b e r t o $ !
t o s e t e r r n o i f , f o r i n s t a n c e , y o u w a n t " $ ! "
t o r e t u r n t h e s t r i n g f o r e r r o r n , o r y o u w a n t t o s e t t h e e x i t v a l u e f o r t h e d i e ( ) o p e r a t o r . ( M n e m o n i c : W h a t j u s t w e n t b a n g ? )
A l s o s e e " E r r o r I n d i c a t o r s " .
# % !
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 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 a b o v e f o r t h e v a l i d i t y o f $ !
.
# $ 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 . ( 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 . )
A l s o s e e " E r r o r I n d i c a t o r s " .
# $ E V A L _ E R R O R
# $ @
T h e P e r l s y n t a x e r r o r m e s s a g e f r o m t h e l a s t e v a l ( ) o p e r a t o r . I f $ @ i s t h e n u l l s t r i n g , t h e l a s t e v a l ( ) p a r s e d a n d e x e c u t e d c o r r e c t l y ( a l t h o u g h t h e o p e r a t i o n s y o u i n v o k e d m a y h a v e f a i l e d i n t h e n o r m a l f a s h i o n ) . ( M n e m o n i c : W h e r e w a s t h e s y n t a x e r r o r " a t " ? )
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 b e l o w .
A l s o s e e " E r r o r I n d i c a t o r s " .
# $ 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 . Y o u s h o u l d c o n s i d e r t h i s v a r i a b l e r e a d - o n l y , a l t h o u g h i t w i l l b e a l t e r e d a c r o s s f o r k ( ) c a l l s . ( M n e m o n i c : s a m e a s s h e l l s . )
N o t e f o r L i n u x u s e r s : o n L i n u x , t h e C f u n c t i o n s g e t p i d ( )
a n d g
e t p p i d ( )
r e t u r n d i f f e r e n t v a l u e s f r o m d i f f e r e n t t h r e a d s . I n o r d e r t o b e p o r t a b l e , t h i s b e h a v i o r i s n o t r e f l e c t e d b y $ $
, w h o s e v a l u e r e m a i n s c o n s i s t e n t a c r o s s t h r e a d s . I f y o u w a n t t o c a l l t h e u n d e r l y i n g g e t p i d ( )
, y o u m a y u s e t h e C P A N m o d u l e L i n u x : : P
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 . ( 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 . ) 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 .
# $ 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 . E x a m p l e :
$< = $>; # set real to effective uid
($<,$>) = ($>,$<); # swap real and effective uid
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 .
( 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 . ) $ <
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 ( ) .
# $ 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 .
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 .
( 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 . )
$ <
, $ >
, $ (
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 ( ) .
# $ 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 ( r e a d : 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 . ( M n e m o n i c : s a m e a s sh a n d k s h . )
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 .
# $ [
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 . 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 . ( M n e m o n i c : [ b e g i n s s u b s c r i p t 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 .
N o t e t h a t , 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 ) , a s s i g n m e n t t o $ [
c a n 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 . H o w e v e r , y o u c a n u s e l o c a l ( ) o n i t t o s t r i c t l y b i n d i t s v a l u e t o a l e x i c a l b l o c k .
# $ ]
T h e v e r s i o n + p a t c h l e v e l / 1 0 0 0 o f t h e P e r l i n t e r p r e t e r . 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 . ( 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 ? ) E x a m p l e :
warn "No checksumming!\n" if $] < 3.019;
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 .
W h e n t e s t i n g t h e v a r i a b l e , t o s t e e r c l e a r o f f l o a t i n g p o i n t i n a c c u r a c i e s y o u m i g h t w a n t t o p r e f e r t h e i n e q u a l i t y t e s t s <
a n d >
t o t h e t e s t s c o n t a i n i n g e q u i v a l e n c e : < =
, = =
, a n d > =
.
T h e 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 c a n s o m e t i m e s l e a d t o i n a c c u r a t e n u m e r i c c o m p a r i s o n s . S e e $
^ V
f o r a m o r e m o d e r n 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 a t a l l o w s a c c u r a t e s t r i n g c o m p a r i s o n s .
# $ 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 e p e r l c c . 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
.
# $ 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 n e m o n i c : v a l u e o f - D s w i t c h . ) 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 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 ( ) .
# $ ^ 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 .
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 t h e a b o v e B E G I N 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 }
# % ^ 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 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 .
# $ 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 . )
# $ ^ 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 .
# $ 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 . 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 .
# $ { ^ 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 .
# $ 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 .
# 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
D e b u g a s s e r t i o n s u b r o u t i n e s e n t e r / e x i t .
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 .
# $ 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 .
# $ 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
true (1 ) Executing an eval
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 .
# $ 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 .
# $ { ^ 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 ) .
# $ { ^ 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 .
# $ { ^ 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 n 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 .
# $ 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 s t r i n g c o m p o s e d o f c h a r a c t e r s w i t h t h o s e o r d i n a l s . T h u s i n P e r l v 5 . 6 . 0 i t e q u a l s c h r ( 5 ) . c h r ( 6 ) . c h r ( 0 )
a n d w i l l r e t u r n t r u e f o r $ ^ V e q
v 5 . 6 . 0
. N o t e t h a t t h e c h a r a c t e r s i n t h i s s t r i n g v a l u e c a n p o t e n t i a l l y b e i n U n i c o d e r a n g e .
T h i s 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 . ( M n e m o n i c : u s e ^ V f o r V e r s i o n C o n t r o l . ) E x a m p l e :
warn "No \"our\" declarations!\n" if $^V and $^V lt v5.6.0;
T o 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 :
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 n o l d e r 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 .
# $ 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 . ( 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 . ) S e e a l s o w a r n i n g s .
# $ { ^ 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 . S e e t h e d o c u m e n t a t i o n o f w a r n i n g s
f o r m o r e d e t a i l s .
# $ 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;
$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;
$secure_perl_path = $Config{perlpath};
if ($^O ne 'VMS')
{$secure_perl_path .= $Config{_exe}
unless $secure_perl_path =~ m/$Config{_exe}$/i;}
# 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
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 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 .
# @ 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 , f o l l o w e d b y " . " , t o r e p r e s e n t t h e c u r r e n t d i r e c t o r y . ( " . " w i l l n o t b e a p p e n d e d i f t a i n t c h e c k s a r e e n a b l e d , e i t h e r b y - T
o r b y - t
. ) I f y o u n e e d t o m o d i f y t h i s 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 l s o :
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 .
# @ _
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 . S e e p e r l s u b .
# % 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 .
# % E N V
# $ E N V { e x p r }
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 .
# % S I G
# $ S I G { e x p r }
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 .
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 .
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 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;
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
, 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 _ _
.
D u e t o a n i m p l e m e n t a t i o n g l i t c h , 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 ( ) . D o n o t u s e t h i s t o r e w r i t e a p e n d i n g e x c e p t i o n i n $ @
, o r a s a b i z a r r e s u b s t i t u t e f o r o v e r r i d i n g C O R E : : G L O B A L : : d i e ( ) . T h i s s t r a n g e a c t i o n a t a d i s t a n c e m a y b e f i x e d i n a f u t u r e r e l e a s e s o t h a t $ S I G { _ _ D I E _ _ }
i s o n l y c a l l e d i f y o u r p r o g r a m i s a b o u t t o e x i t , a s w a s t h e o r i g i n a l i n t e n t . A n y o t h e r u s e i s d e p r e c a t e d .
_ _ 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...
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 .
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 .
# E r r o r I n d i c a t o r 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 :
eval q{
open my $pipe, "/cdrom/install |" or die $!;
my @res = <$pipe>;
close $pipe or die "bad pipe: $?, $!";
};
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 a l l 4 v a r i a b l e s m a y h a v e b e e n s e t .
$ @
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 . )
W h e n t h e e v a l ( ) e x p r e s s i o n a b o v e i s e x e c u t e d , o p e n ( ) , < P I P E >
, a n d c l o s e
a r e t r a n s l a t e d t o 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 . $ !
i s s e t 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 .
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 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 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 $ ?
.
# T e c h n i c a l N o t e 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 .
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 o r a s i n g l e p u n c t u a t i o n o r c o n t r o l c h a r a c t e r . 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 . P e r l h a s a s p e c i a l s y n t a x f o r t h e s i n g l e - c o n t r o l - c h a r a c t e r n a m e s : I t u n d e r s t a n d s ^ X
( c a r e t X
) t o m e a n t h e c o n t r o l - X
c h a r a c t e r . F o r e x a m p l e , t h e n o t a t i o n $ ^ W
( d o l l a r - s i g n c a r e t W
) i 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 t h e s i n g l e c h a r a c t e r c o n t r o l - W
. T h i s i s b e t t e r t h a n t y p i n g a l i t e r a l c o n t r o l - W
i n t o y o u r p r o g r a m .
F i n a l l y , n e w i n P e r l 5 . 6 , P e r l v a r i a b l e n a m e s m a y b e a l p h a n u m e r i c s t r i n g s t h a t b e g i n w i t h c o n t r o l c h a r a c t e r s ( o r b e t t e r y e t , a c a r e t ) . T h e s e v a r i a b l e s m u s t b e w r i t t e n i n t h e f o r m $ { ^ 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 a c o n t r o l - F
f o l l o w e d b y t w o o
' s . 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 o n t r o l - u n d e r s c o r e o r c a r e t - u n d e r s c o r e ) . N o c o n t r o l - c h a r a c t e r 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 .
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 , c o n t r o l c h a r a c t e r 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 n e w 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 .
# B U G S
D u e t o a n u n f o r t u n a t e a c c i d e n t o f P e r l ' s i m p l e m e n t a t i o n , u s e E n g
l i s h
i m p o s e s 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 o n a l l r e g u l a r e x p r e s s i o n m a t c h e s i n a p r o g r a m , r e g a r d l e s s o f w h e t h e r t h e y o c c u r i n t h e s c o p e o f u s e E n g l i s h
. F o r t h a t r e a s o n , s a y i n g u s e E n g l i s
h
i n l i b r a r i e s i s s t r o n g l y d i s c o u r a g e d . S e e t h e D e v e l : : S a w A m p e r s a n d m o d u l e d o c u m e n t a t i o n f r o m C P A N ( h t t p : / / w w w . c p a n . o r g / m o d u l e s / b y - m o d u l e / D e v e l / ) f o r m o r e i n f o r m a t i o n .
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 .
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 .