Initial commit of OpenSPARC T2 design and verification files.
[OpenSPARC-T2-DV] / tools / perl-5.8.0 / man / man1 / perlfunc.1
CommitLineData
86530b38
AT
1.\" Automatically generated by Pod::Man v1.34, Pod::Parser v1.13
2.\"
3.\" Standard preamble:
4.\" ========================================================================
5.de Sh \" Subsection heading
6.br
7.if t .Sp
8.ne 5
9.PP
10\fB\\$1\fR
11.PP
12..
13.de Sp \" Vertical space (when we can't use .PP)
14.if t .sp .5v
15.if n .sp
16..
17.de Vb \" Begin verbatim text
18.ft CW
19.nf
20.ne \\$1
21..
22.de Ve \" End verbatim text
23.ft R
24.fi
25..
26.\" Set up some character translations and predefined strings. \*(-- will
27.\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left
28.\" double quote, and \*(R" will give a right double quote. | will give a
29.\" real vertical bar. \*(C+ will give a nicer C++. Capital omega is used to
30.\" do unbreakable dashes and therefore won't be available. \*(C` and \*(C'
31.\" expand to `' in nroff, nothing in troff, for use with C<>.
32.tr \(*W-|\(bv\*(Tr
33.ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'
34.ie n \{\
35. ds -- \(*W-
36. ds PI pi
37. if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch
38. if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch
39. ds L" ""
40. ds R" ""
41. ds C` ""
42. ds C' ""
43'br\}
44.el\{\
45. ds -- \|\(em\|
46. ds PI \(*p
47. ds L" ``
48. ds R" ''
49'br\}
50.\"
51.\" If the F register is turned on, we'll generate index entries on stderr for
52.\" titles (.TH), headers (.SH), subsections (.Sh), items (.Ip), and index
53.\" entries marked with X<> in POD. Of course, you'll have to process the
54.\" output yourself in some meaningful fashion.
55.if \nF \{\
56. de IX
57. tm Index:\\$1\t\\n%\t"\\$2"
58..
59. nr % 0
60. rr F
61.\}
62.\"
63.\" For nroff, turn off justification. Always turn off hyphenation; it makes
64.\" way too many mistakes in technical documents.
65.hy 0
66.if n .na
67.\"
68.\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2).
69.\" Fear. Run. Save yourself. No user-serviceable parts.
70. \" fudge factors for nroff and troff
71.if n \{\
72. ds #H 0
73. ds #V .8m
74. ds #F .3m
75. ds #[ \f1
76. ds #] \fP
77.\}
78.if t \{\
79. ds #H ((1u-(\\\\n(.fu%2u))*.13m)
80. ds #V .6m
81. ds #F 0
82. ds #[ \&
83. ds #] \&
84.\}
85. \" simple accents for nroff and troff
86.if n \{\
87. ds ' \&
88. ds ` \&
89. ds ^ \&
90. ds , \&
91. ds ~ ~
92. ds /
93.\}
94.if t \{\
95. ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u"
96. ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u'
97. ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u'
98. ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u'
99. ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u'
100. ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u'
101.\}
102. \" troff and (daisy-wheel) nroff accents
103.ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V'
104.ds 8 \h'\*(#H'\(*b\h'-\*(#H'
105.ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#]
106.ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H'
107.ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u'
108.ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#]
109.ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#]
110.ds ae a\h'-(\w'a'u*4/10)'e
111.ds Ae A\h'-(\w'A'u*4/10)'E
112. \" corrections for vroff
113.if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u'
114.if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u'
115. \" for low resolution devices (crt and lpr)
116.if \n(.H>23 .if \n(.V>19 \
117\{\
118. ds : e
119. ds 8 ss
120. ds o a
121. ds d- d\h'-1'\(ga
122. ds D- D\h'-1'\(hy
123. ds th \o'bp'
124. ds Th \o'LP'
125. ds ae ae
126. ds Ae AE
127.\}
128.rm #[ #] #H #V #F C
129.\" ========================================================================
130.\"
131.IX Title "PERLFUNC 1"
132.TH PERLFUNC 1 "2002-06-08" "perl v5.8.0" "Perl Programmers Reference Guide"
133.SH "NAME"
134perlfunc \- Perl builtin functions
135.SH "DESCRIPTION"
136.IX Header "DESCRIPTION"
137The functions in this section can serve as terms in an expression.
138They fall into two major categories: list operators and named unary
139operators. These differ in their precedence relationship with a
140following comma. (See the precedence table in perlop.) List
141operators take more than one argument, while unary operators can never
142take more than one argument. Thus, a comma terminates the argument of
143a unary operator, but merely separates the arguments of a list
144operator. A unary operator generally provides a scalar context to its
145argument, while a list operator may provide either scalar or list
146contexts for its arguments. If it does both, the scalar arguments will
147be first, and the list argument will follow. (Note that there can ever
148be only one such list argument.) For instance, \fIsplice()\fR has three scalar
149arguments followed by a list, whereas \fIgethostbyname()\fR has four scalar
150arguments.
151.PP
152In the syntax descriptions that follow, list operators that expect a
153list (and provide list context for the elements of the list) are shown
154with \s-1LIST\s0 as an argument. Such a list may consist of any combination
155of scalar arguments or list values; the list values will be included
156in the list as if each individual element were interpolated at that
157point in the list, forming a longer single-dimensional list value.
158Elements of the \s-1LIST\s0 should be separated by commas.
159.PP
160Any function in the list below may be used either with or without
161parentheses around its arguments. (The syntax descriptions omit the
162parentheses.) If you use the parentheses, the simple (but occasionally
163surprising) rule is this: It \fIlooks\fR like a function, therefore it \fIis\fR a
164function, and precedence doesn't matter. Otherwise it's a list
165operator or unary operator, and precedence does matter. And whitespace
166between the function and left parenthesis doesn't count\*(--so you need to
167be careful sometimes:
168.PP
169.Vb 5
170\& print 1+2+4; # Prints 7.
171\& print(1+2) + 4; # Prints 3.
172\& print (1+2)+4; # Also prints 3!
173\& print +(1+2)+4; # Prints 7.
174\& print ((1+2)+4); # Prints 7.
175.Ve
176.PP
177If you run Perl with the \fB\-w\fR switch it can warn you about this. For
178example, the third line above produces:
179.PP
180.Vb 2
181\& print (...) interpreted as function at - line 1.
182\& Useless use of integer addition in void context at - line 1.
183.Ve
184.PP
185A few functions take no arguments at all, and therefore work as neither
186unary nor list operators. These include such functions as \f(CW\*(C`time\*(C'\fR
187and \f(CW\*(C`endpwent\*(C'\fR. For example, \f(CW\*(C`time+86_400\*(C'\fR always means
188\&\f(CW\*(C`time() + 86_400\*(C'\fR.
189.PP
190For functions that can be used in either a scalar or list context,
191nonabortive failure is generally indicated in a scalar context by
192returning the undefined value, and in a list context by returning the
193null list.
194.PP
195Remember the following important rule: There is \fBno rule\fR that relates
196the behavior of an expression in list context to its behavior in scalar
197context, or vice versa. It might do two totally different things.
198Each operator and function decides which sort of value it would be most
199appropriate to return in scalar context. Some operators return the
200length of the list that would have been returned in list context. Some
201operators return the first value in the list. Some operators return the
202last value in the list. Some operators return a count of successful
203operations. In general, they do what you want, unless you want
204consistency.
205.PP
206A named array in scalar context is quite different from what would at
207first glance appear to be a list in scalar context. You can't get a list
208like \f(CW\*(C`(1,2,3)\*(C'\fR into being in scalar context, because the compiler knows
209the context at compile time. It would generate the scalar comma operator
210there, not the list construction version of the comma. That means it
211was never a list to start with.
212.PP
213In general, functions in Perl that serve as wrappers for system calls
214of the same name (like \fIchown\fR\|(2), \fIfork\fR\|(2), \fIclosedir\fR\|(2), etc.) all return
215true when they succeed and \f(CW\*(C`undef\*(C'\fR otherwise, as is usually mentioned
216in the descriptions below. This is different from the C interfaces,
217which return \f(CW\*(C`\-1\*(C'\fR on failure. Exceptions to this rule are \f(CW\*(C`wait\*(C'\fR,
218\&\f(CW\*(C`waitpid\*(C'\fR, and \f(CW\*(C`syscall\*(C'\fR. System calls also set the special \f(CW$!\fR
219variable on failure. Other functions do not, except accidentally.
220.Sh "Perl Functions by Category"
221.IX Subsection "Perl Functions by Category"
222Here are Perl's functions (including things that look like
223functions, like some keywords and named operators)
224arranged by category. Some functions appear in more
225than one place.
226.IP "Functions for SCALARs or strings" 4
227.IX Item "Functions for SCALARs or strings"
228\&\f(CW\*(C`chomp\*(C'\fR, \f(CW\*(C`chop\*(C'\fR, \f(CW\*(C`chr\*(C'\fR, \f(CW\*(C`crypt\*(C'\fR, \f(CW\*(C`hex\*(C'\fR, \f(CW\*(C`index\*(C'\fR, \f(CW\*(C`lc\*(C'\fR, \f(CW\*(C`lcfirst\*(C'\fR,
229\&\f(CW\*(C`length\*(C'\fR, \f(CW\*(C`oct\*(C'\fR, \f(CW\*(C`ord\*(C'\fR, \f(CW\*(C`pack\*(C'\fR, \f(CW\*(C`q/STRING/\*(C'\fR, \f(CW\*(C`qq/STRING/\*(C'\fR, \f(CW\*(C`reverse\*(C'\fR,
230\&\f(CW\*(C`rindex\*(C'\fR, \f(CW\*(C`sprintf\*(C'\fR, \f(CW\*(C`substr\*(C'\fR, \f(CW\*(C`tr///\*(C'\fR, \f(CW\*(C`uc\*(C'\fR, \f(CW\*(C`ucfirst\*(C'\fR, \f(CW\*(C`y///\*(C'\fR
231.IP "Regular expressions and pattern matching" 4
232.IX Item "Regular expressions and pattern matching"
233\&\f(CW\*(C`m//\*(C'\fR, \f(CW\*(C`pos\*(C'\fR, \f(CW\*(C`quotemeta\*(C'\fR, \f(CW\*(C`s///\*(C'\fR, \f(CW\*(C`split\*(C'\fR, \f(CW\*(C`study\*(C'\fR, \f(CW\*(C`qr//\*(C'\fR
234.IP "Numeric functions" 4
235.IX Item "Numeric functions"
236\&\f(CW\*(C`abs\*(C'\fR, \f(CW\*(C`atan2\*(C'\fR, \f(CW\*(C`cos\*(C'\fR, \f(CW\*(C`exp\*(C'\fR, \f(CW\*(C`hex\*(C'\fR, \f(CW\*(C`int\*(C'\fR, \f(CW\*(C`log\*(C'\fR, \f(CW\*(C`oct\*(C'\fR, \f(CW\*(C`rand\*(C'\fR,
237\&\f(CW\*(C`sin\*(C'\fR, \f(CW\*(C`sqrt\*(C'\fR, \f(CW\*(C`srand\*(C'\fR
238.ie n .IP "Functions for real @ARRAYs" 4
239.el .IP "Functions for real \f(CW@ARRAYs\fR" 4
240.IX Item "Functions for real @ARRAYs"
241\&\f(CW\*(C`pop\*(C'\fR, \f(CW\*(C`push\*(C'\fR, \f(CW\*(C`shift\*(C'\fR, \f(CW\*(C`splice\*(C'\fR, \f(CW\*(C`unshift\*(C'\fR
242.IP "Functions for list data" 4
243.IX Item "Functions for list data"
244\&\f(CW\*(C`grep\*(C'\fR, \f(CW\*(C`join\*(C'\fR, \f(CW\*(C`map\*(C'\fR, \f(CW\*(C`qw/STRING/\*(C'\fR, \f(CW\*(C`reverse\*(C'\fR, \f(CW\*(C`sort\*(C'\fR, \f(CW\*(C`unpack\*(C'\fR
245.ie n .IP "Functions for real %HASHes" 4
246.el .IP "Functions for real \f(CW%HASHes\fR" 4
247.IX Item "Functions for real %HASHes"
248\&\f(CW\*(C`delete\*(C'\fR, \f(CW\*(C`each\*(C'\fR, \f(CW\*(C`exists\*(C'\fR, \f(CW\*(C`keys\*(C'\fR, \f(CW\*(C`values\*(C'\fR
249.IP "Input and output functions" 4
250.IX Item "Input and output functions"
251\&\f(CW\*(C`binmode\*(C'\fR, \f(CW\*(C`close\*(C'\fR, \f(CW\*(C`closedir\*(C'\fR, \f(CW\*(C`dbmclose\*(C'\fR, \f(CW\*(C`dbmopen\*(C'\fR, \f(CW\*(C`die\*(C'\fR, \f(CW\*(C`eof\*(C'\fR,
252\&\f(CW\*(C`fileno\*(C'\fR, \f(CW\*(C`flock\*(C'\fR, \f(CW\*(C`format\*(C'\fR, \f(CW\*(C`getc\*(C'\fR, \f(CW\*(C`print\*(C'\fR, \f(CW\*(C`printf\*(C'\fR, \f(CW\*(C`read\*(C'\fR,
253\&\f(CW\*(C`readdir\*(C'\fR, \f(CW\*(C`rewinddir\*(C'\fR, \f(CW\*(C`seek\*(C'\fR, \f(CW\*(C`seekdir\*(C'\fR, \f(CW\*(C`select\*(C'\fR, \f(CW\*(C`syscall\*(C'\fR,
254\&\f(CW\*(C`sysread\*(C'\fR, \f(CW\*(C`sysseek\*(C'\fR, \f(CW\*(C`syswrite\*(C'\fR, \f(CW\*(C`tell\*(C'\fR, \f(CW\*(C`telldir\*(C'\fR, \f(CW\*(C`truncate\*(C'\fR,
255\&\f(CW\*(C`warn\*(C'\fR, \f(CW\*(C`write\*(C'\fR
256.IP "Functions for fixed length data or records" 4
257.IX Item "Functions for fixed length data or records"
258\&\f(CW\*(C`pack\*(C'\fR, \f(CW\*(C`read\*(C'\fR, \f(CW\*(C`syscall\*(C'\fR, \f(CW\*(C`sysread\*(C'\fR, \f(CW\*(C`syswrite\*(C'\fR, \f(CW\*(C`unpack\*(C'\fR, \f(CW\*(C`vec\*(C'\fR
259.IP "Functions for filehandles, files, or directories" 4
260.IX Item "Functions for filehandles, files, or directories"
261\&\f(CW\*(C`\-\f(CIX\f(CW\*(C'\fR, \f(CW\*(C`chdir\*(C'\fR, \f(CW\*(C`chmod\*(C'\fR, \f(CW\*(C`chown\*(C'\fR, \f(CW\*(C`chroot\*(C'\fR, \f(CW\*(C`fcntl\*(C'\fR, \f(CW\*(C`glob\*(C'\fR,
262\&\f(CW\*(C`ioctl\*(C'\fR, \f(CW\*(C`link\*(C'\fR, \f(CW\*(C`lstat\*(C'\fR, \f(CW\*(C`mkdir\*(C'\fR, \f(CW\*(C`open\*(C'\fR, \f(CW\*(C`opendir\*(C'\fR,
263\&\f(CW\*(C`readlink\*(C'\fR, \f(CW\*(C`rename\*(C'\fR, \f(CW\*(C`rmdir\*(C'\fR, \f(CW\*(C`stat\*(C'\fR, \f(CW\*(C`symlink\*(C'\fR, \f(CW\*(C`sysopen\*(C'\fR,
264\&\f(CW\*(C`umask\*(C'\fR, \f(CW\*(C`unlink\*(C'\fR, \f(CW\*(C`utime\*(C'\fR
265.IP "Keywords related to the control flow of your perl program" 4
266.IX Item "Keywords related to the control flow of your perl program"
267\&\f(CW\*(C`caller\*(C'\fR, \f(CW\*(C`continue\*(C'\fR, \f(CW\*(C`die\*(C'\fR, \f(CW\*(C`do\*(C'\fR, \f(CW\*(C`dump\*(C'\fR, \f(CW\*(C`eval\*(C'\fR, \f(CW\*(C`exit\*(C'\fR,
268\&\f(CW\*(C`goto\*(C'\fR, \f(CW\*(C`last\*(C'\fR, \f(CW\*(C`next\*(C'\fR, \f(CW\*(C`redo\*(C'\fR, \f(CW\*(C`return\*(C'\fR, \f(CW\*(C`sub\*(C'\fR, \f(CW\*(C`wantarray\*(C'\fR
269.IP "Keywords related to scoping" 4
270.IX Item "Keywords related to scoping"
271\&\f(CW\*(C`caller\*(C'\fR, \f(CW\*(C`import\*(C'\fR, \f(CW\*(C`local\*(C'\fR, \f(CW\*(C`my\*(C'\fR, \f(CW\*(C`our\*(C'\fR, \f(CW\*(C`package\*(C'\fR, \f(CW\*(C`use\*(C'\fR
272.IP "Miscellaneous functions" 4
273.IX Item "Miscellaneous functions"
274\&\f(CW\*(C`defined\*(C'\fR, \f(CW\*(C`dump\*(C'\fR, \f(CW\*(C`eval\*(C'\fR, \f(CW\*(C`formline\*(C'\fR, \f(CW\*(C`local\*(C'\fR, \f(CW\*(C`my\*(C'\fR, \f(CW\*(C`our\*(C'\fR, \f(CW\*(C`reset\*(C'\fR,
275\&\f(CW\*(C`scalar\*(C'\fR, \f(CW\*(C`undef\*(C'\fR, \f(CW\*(C`wantarray\*(C'\fR
276.IP "Functions for processes and process groups" 4
277.IX Item "Functions for processes and process groups"
278\&\f(CW\*(C`alarm\*(C'\fR, \f(CW\*(C`exec\*(C'\fR, \f(CW\*(C`fork\*(C'\fR, \f(CW\*(C`getpgrp\*(C'\fR, \f(CW\*(C`getppid\*(C'\fR, \f(CW\*(C`getpriority\*(C'\fR, \f(CW\*(C`kill\*(C'\fR,
279\&\f(CW\*(C`pipe\*(C'\fR, \f(CW\*(C`qx/STRING/\*(C'\fR, \f(CW\*(C`setpgrp\*(C'\fR, \f(CW\*(C`setpriority\*(C'\fR, \f(CW\*(C`sleep\*(C'\fR, \f(CW\*(C`system\*(C'\fR,
280\&\f(CW\*(C`times\*(C'\fR, \f(CW\*(C`wait\*(C'\fR, \f(CW\*(C`waitpid\*(C'\fR
281.IP "Keywords related to perl modules" 4
282.IX Item "Keywords related to perl modules"
283\&\f(CW\*(C`do\*(C'\fR, \f(CW\*(C`import\*(C'\fR, \f(CW\*(C`no\*(C'\fR, \f(CW\*(C`package\*(C'\fR, \f(CW\*(C`require\*(C'\fR, \f(CW\*(C`use\*(C'\fR
284.IP "Keywords related to classes and object-orientedness" 4
285.IX Item "Keywords related to classes and object-orientedness"
286\&\f(CW\*(C`bless\*(C'\fR, \f(CW\*(C`dbmclose\*(C'\fR, \f(CW\*(C`dbmopen\*(C'\fR, \f(CW\*(C`package\*(C'\fR, \f(CW\*(C`ref\*(C'\fR, \f(CW\*(C`tie\*(C'\fR, \f(CW\*(C`tied\*(C'\fR,
287\&\f(CW\*(C`untie\*(C'\fR, \f(CW\*(C`use\*(C'\fR
288.IP "Low-level socket functions" 4
289.IX Item "Low-level socket functions"
290\&\f(CW\*(C`accept\*(C'\fR, \f(CW\*(C`bind\*(C'\fR, \f(CW\*(C`connect\*(C'\fR, \f(CW\*(C`getpeername\*(C'\fR, \f(CW\*(C`getsockname\*(C'\fR,
291\&\f(CW\*(C`getsockopt\*(C'\fR, \f(CW\*(C`listen\*(C'\fR, \f(CW\*(C`recv\*(C'\fR, \f(CW\*(C`send\*(C'\fR, \f(CW\*(C`setsockopt\*(C'\fR, \f(CW\*(C`shutdown\*(C'\fR,
292\&\f(CW\*(C`socket\*(C'\fR, \f(CW\*(C`socketpair\*(C'\fR
293.IP "System V interprocess communication functions" 4
294.IX Item "System V interprocess communication functions"
295\&\f(CW\*(C`msgctl\*(C'\fR, \f(CW\*(C`msgget\*(C'\fR, \f(CW\*(C`msgrcv\*(C'\fR, \f(CW\*(C`msgsnd\*(C'\fR, \f(CW\*(C`semctl\*(C'\fR, \f(CW\*(C`semget\*(C'\fR, \f(CW\*(C`semop\*(C'\fR,
296\&\f(CW\*(C`shmctl\*(C'\fR, \f(CW\*(C`shmget\*(C'\fR, \f(CW\*(C`shmread\*(C'\fR, \f(CW\*(C`shmwrite\*(C'\fR
297.IP "Fetching user and group info" 4
298.IX Item "Fetching user and group info"
299\&\f(CW\*(C`endgrent\*(C'\fR, \f(CW\*(C`endhostent\*(C'\fR, \f(CW\*(C`endnetent\*(C'\fR, \f(CW\*(C`endpwent\*(C'\fR, \f(CW\*(C`getgrent\*(C'\fR,
300\&\f(CW\*(C`getgrgid\*(C'\fR, \f(CW\*(C`getgrnam\*(C'\fR, \f(CW\*(C`getlogin\*(C'\fR, \f(CW\*(C`getpwent\*(C'\fR, \f(CW\*(C`getpwnam\*(C'\fR,
301\&\f(CW\*(C`getpwuid\*(C'\fR, \f(CW\*(C`setgrent\*(C'\fR, \f(CW\*(C`setpwent\*(C'\fR
302.IP "Fetching network info" 4
303.IX Item "Fetching network info"
304\&\f(CW\*(C`endprotoent\*(C'\fR, \f(CW\*(C`endservent\*(C'\fR, \f(CW\*(C`gethostbyaddr\*(C'\fR, \f(CW\*(C`gethostbyname\*(C'\fR,
305\&\f(CW\*(C`gethostent\*(C'\fR, \f(CW\*(C`getnetbyaddr\*(C'\fR, \f(CW\*(C`getnetbyname\*(C'\fR, \f(CW\*(C`getnetent\*(C'\fR,
306\&\f(CW\*(C`getprotobyname\*(C'\fR, \f(CW\*(C`getprotobynumber\*(C'\fR, \f(CW\*(C`getprotoent\*(C'\fR,
307\&\f(CW\*(C`getservbyname\*(C'\fR, \f(CW\*(C`getservbyport\*(C'\fR, \f(CW\*(C`getservent\*(C'\fR, \f(CW\*(C`sethostent\*(C'\fR,
308\&\f(CW\*(C`setnetent\*(C'\fR, \f(CW\*(C`setprotoent\*(C'\fR, \f(CW\*(C`setservent\*(C'\fR
309.IP "Time-related functions" 4
310.IX Item "Time-related functions"
311\&\f(CW\*(C`gmtime\*(C'\fR, \f(CW\*(C`localtime\*(C'\fR, \f(CW\*(C`time\*(C'\fR, \f(CW\*(C`times\*(C'\fR
312.IP "Functions new in perl5" 4
313.IX Item "Functions new in perl5"
314\&\f(CW\*(C`abs\*(C'\fR, \f(CW\*(C`bless\*(C'\fR, \f(CW\*(C`chomp\*(C'\fR, \f(CW\*(C`chr\*(C'\fR, \f(CW\*(C`exists\*(C'\fR, \f(CW\*(C`formline\*(C'\fR, \f(CW\*(C`glob\*(C'\fR,
315\&\f(CW\*(C`import\*(C'\fR, \f(CW\*(C`lc\*(C'\fR, \f(CW\*(C`lcfirst\*(C'\fR, \f(CW\*(C`map\*(C'\fR, \f(CW\*(C`my\*(C'\fR, \f(CW\*(C`no\*(C'\fR, \f(CW\*(C`our\*(C'\fR, \f(CW\*(C`prototype\*(C'\fR,
316\&\f(CW\*(C`qx\*(C'\fR, \f(CW\*(C`qw\*(C'\fR, \f(CW\*(C`readline\*(C'\fR, \f(CW\*(C`readpipe\*(C'\fR, \f(CW\*(C`ref\*(C'\fR, \f(CW\*(C`sub*\*(C'\fR, \f(CW\*(C`sysopen\*(C'\fR, \f(CW\*(C`tie\*(C'\fR,
317\&\f(CW\*(C`tied\*(C'\fR, \f(CW\*(C`uc\*(C'\fR, \f(CW\*(C`ucfirst\*(C'\fR, \f(CW\*(C`untie\*(C'\fR, \f(CW\*(C`use\*(C'\fR
318.Sp
319* \- \f(CW\*(C`sub\*(C'\fR was a keyword in perl4, but in perl5 it is an
320operator, which can be used in expressions.
321.IP "Functions obsoleted in perl5" 4
322.IX Item "Functions obsoleted in perl5"
323\&\f(CW\*(C`dbmclose\*(C'\fR, \f(CW\*(C`dbmopen\*(C'\fR
324.Sh "Portability"
325.IX Subsection "Portability"
326Perl was born in Unix and can therefore access all common Unix
327system calls. In non-Unix environments, the functionality of some
328Unix system calls may not be available, or details of the available
329functionality may differ slightly. The Perl functions affected
330by this are:
331.PP
332\&\f(CW\*(C`\-X\*(C'\fR, \f(CW\*(C`binmode\*(C'\fR, \f(CW\*(C`chmod\*(C'\fR, \f(CW\*(C`chown\*(C'\fR, \f(CW\*(C`chroot\*(C'\fR, \f(CW\*(C`crypt\*(C'\fR,
333\&\f(CW\*(C`dbmclose\*(C'\fR, \f(CW\*(C`dbmopen\*(C'\fR, \f(CW\*(C`dump\*(C'\fR, \f(CW\*(C`endgrent\*(C'\fR, \f(CW\*(C`endhostent\*(C'\fR,
334\&\f(CW\*(C`endnetent\*(C'\fR, \f(CW\*(C`endprotoent\*(C'\fR, \f(CW\*(C`endpwent\*(C'\fR, \f(CW\*(C`endservent\*(C'\fR, \f(CW\*(C`exec\*(C'\fR,
335\&\f(CW\*(C`fcntl\*(C'\fR, \f(CW\*(C`flock\*(C'\fR, \f(CW\*(C`fork\*(C'\fR, \f(CW\*(C`getgrent\*(C'\fR, \f(CW\*(C`getgrgid\*(C'\fR, \f(CW\*(C`gethostent\*(C'\fR,
336\&\f(CW\*(C`getlogin\*(C'\fR, \f(CW\*(C`getnetbyaddr\*(C'\fR, \f(CW\*(C`getnetbyname\*(C'\fR, \f(CW\*(C`getnetent\*(C'\fR,
337\&\f(CW\*(C`getppid\*(C'\fR, \f(CW\*(C`getprgp\*(C'\fR, \f(CW\*(C`getpriority\*(C'\fR, \f(CW\*(C`getprotobynumber\*(C'\fR,
338\&\f(CW\*(C`getprotoent\*(C'\fR, \f(CW\*(C`getpwent\*(C'\fR, \f(CW\*(C`getpwnam\*(C'\fR, \f(CW\*(C`getpwuid\*(C'\fR,
339\&\f(CW\*(C`getservbyport\*(C'\fR, \f(CW\*(C`getservent\*(C'\fR, \f(CW\*(C`getsockopt\*(C'\fR, \f(CW\*(C`glob\*(C'\fR, \f(CW\*(C`ioctl\*(C'\fR,
340\&\f(CW\*(C`kill\*(C'\fR, \f(CW\*(C`link\*(C'\fR, \f(CW\*(C`lstat\*(C'\fR, \f(CW\*(C`msgctl\*(C'\fR, \f(CW\*(C`msgget\*(C'\fR, \f(CW\*(C`msgrcv\*(C'\fR,
341\&\f(CW\*(C`msgsnd\*(C'\fR, \f(CW\*(C`open\*(C'\fR, \f(CW\*(C`pipe\*(C'\fR, \f(CW\*(C`readlink\*(C'\fR, \f(CW\*(C`rename\*(C'\fR, \f(CW\*(C`select\*(C'\fR, \f(CW\*(C`semctl\*(C'\fR,
342\&\f(CW\*(C`semget\*(C'\fR, \f(CW\*(C`semop\*(C'\fR, \f(CW\*(C`setgrent\*(C'\fR, \f(CW\*(C`sethostent\*(C'\fR, \f(CW\*(C`setnetent\*(C'\fR,
343\&\f(CW\*(C`setpgrp\*(C'\fR, \f(CW\*(C`setpriority\*(C'\fR, \f(CW\*(C`setprotoent\*(C'\fR, \f(CW\*(C`setpwent\*(C'\fR,
344\&\f(CW\*(C`setservent\*(C'\fR, \f(CW\*(C`setsockopt\*(C'\fR, \f(CW\*(C`shmctl\*(C'\fR, \f(CW\*(C`shmget\*(C'\fR, \f(CW\*(C`shmread\*(C'\fR,
345\&\f(CW\*(C`shmwrite\*(C'\fR, \f(CW\*(C`socket\*(C'\fR, \f(CW\*(C`socketpair\*(C'\fR,
346\&\f(CW\*(C`stat\*(C'\fR, \f(CW\*(C`symlink\*(C'\fR, \f(CW\*(C`syscall\*(C'\fR, \f(CW\*(C`sysopen\*(C'\fR, \f(CW\*(C`system\*(C'\fR,
347\&\f(CW\*(C`times\*(C'\fR, \f(CW\*(C`truncate\*(C'\fR, \f(CW\*(C`umask\*(C'\fR, \f(CW\*(C`unlink\*(C'\fR,
348\&\f(CW\*(C`utime\*(C'\fR, \f(CW\*(C`wait\*(C'\fR, \f(CW\*(C`waitpid\*(C'\fR
349.PP
350For more information about the portability of these functions, see
351perlport and other available platform-specific documentation.
352.Sh "Alphabetical Listing of Perl Functions"
353.IX Subsection "Alphabetical Listing of Perl Functions"
354.IP "\fI\-X\fR \s-1FILEHANDLE\s0" 8
355.IX Item "-X FILEHANDLE"
356.PD 0
357.IP "\fI\-X\fR \s-1EXPR\s0" 8
358.IX Item "-X EXPR"
359.IP "\fI\-X\fR" 8
360.IX Item "-X"
361.PD
362A file test, where X is one of the letters listed below. This unary
363operator takes one argument, either a filename or a filehandle, and
364tests the associated file to see if something is true about it. If the
365argument is omitted, tests \f(CW$_\fR, except for \f(CW\*(C`\-t\*(C'\fR, which tests \s-1STDIN\s0.
366Unless otherwise documented, it returns \f(CW1\fR for true and \f(CW''\fR for false, or
367the undefined value if the file doesn't exist. Despite the funny
368names, precedence is the same as any other named unary operator, and
369the argument may be parenthesized like any other unary operator. The
370operator may be any of:
371.IX Xref "-r -w -x -o -R -W -X -O -e -z -s -f -d -l -p -S -b -c -t -u -g -k -T -B -M -A -C"
372.Sp
373.Vb 4
374\& -r File is readable by effective uid/gid.
375\& -w File is writable by effective uid/gid.
376\& -x File is executable by effective uid/gid.
377\& -o File is owned by effective uid.
378.Ve
379.Sp
380.Vb 4
381\& -R File is readable by real uid/gid.
382\& -W File is writable by real uid/gid.
383\& -X File is executable by real uid/gid.
384\& -O File is owned by real uid.
385.Ve
386.Sp
387.Vb 3
388\& -e File exists.
389\& -z File has zero size (is empty).
390\& -s File has nonzero size (returns size in bytes).
391.Ve
392.Sp
393.Vb 8
394\& -f File is a plain file.
395\& -d File is a directory.
396\& -l File is a symbolic link.
397\& -p File is a named pipe (FIFO), or Filehandle is a pipe.
398\& -S File is a socket.
399\& -b File is a block special file.
400\& -c File is a character special file.
401\& -t Filehandle is opened to a tty.
402.Ve
403.Sp
404.Vb 3
405\& -u File has setuid bit set.
406\& -g File has setgid bit set.
407\& -k File has sticky bit set.
408.Ve
409.Sp
410.Vb 2
411\& -T File is an ASCII text file (heuristic guess).
412\& -B File is a "binary" file (opposite of -T).
413.Ve
414.Sp
415.Vb 3
416\& -M Script start time minus file modification time, in days.
417\& -A Same for access time.
418\& -C Same for inode change time (Unix, may differ for other platforms)
419.Ve
420.Sp
421Example:
422.Sp
423.Vb 5
424\& while (<>) {
425\& chomp;
426\& next unless -f $_; # ignore specials
427\& #...
428\& }
429.Ve
430.Sp
431The interpretation of the file permission operators \f(CW\*(C`\-r\*(C'\fR, \f(CW\*(C`\-R\*(C'\fR,
432\&\f(CW\*(C`\-w\*(C'\fR, \f(CW\*(C`\-W\*(C'\fR, \f(CW\*(C`\-x\*(C'\fR, and \f(CW\*(C`\-X\*(C'\fR is by default based solely on the mode
433of the file and the uids and gids of the user. There may be other
434reasons you can't actually read, write, or execute the file. Such
435reasons may be for example network filesystem access controls, ACLs
436(access control lists), read-only filesystems, and unrecognized
437executable formats.
438.Sp
439Also note that, for the superuser on the local filesystems, the \f(CW\*(C`\-r\*(C'\fR,
440\&\f(CW\*(C`\-R\*(C'\fR, \f(CW\*(C`\-w\*(C'\fR, and \f(CW\*(C`\-W\*(C'\fR tests always return 1, and \f(CW\*(C`\-x\*(C'\fR and \f(CW\*(C`\-X\*(C'\fR return 1
441if any execute bit is set in the mode. Scripts run by the superuser
442may thus need to do a \fIstat()\fR to determine the actual mode of the file,
443or temporarily set their effective uid to something else.
444.Sp
445If you are using ACLs, there is a pragma called \f(CW\*(C`filetest\*(C'\fR that may
446produce more accurate results than the bare \fIstat()\fR mode bits.
447When under the \f(CW\*(C`use filetest 'access'\*(C'\fR the above-mentioned filetests
448will test whether the permission can (not) be granted using the
449\&\fIaccess()\fR family of system calls. Also note that the \f(CW\*(C`\-x\*(C'\fR and \f(CW\*(C`\-X\*(C'\fR may
450under this pragma return true even if there are no execute permission
451bits set (nor any extra execute permission ACLs). This strangeness is
452due to the underlying system calls' definitions. Read the
453documentation for the \f(CW\*(C`filetest\*(C'\fR pragma for more information.
454.Sp
455Note that \f(CW\*(C`\-s/a/b/\*(C'\fR does not do a negated substitution. Saying
456\&\f(CW\*(C`\-exp($foo)\*(C'\fR still works as expected, however\*(--only single letters
457following a minus are interpreted as file tests.
458.Sp
459The \f(CW\*(C`\-T\*(C'\fR and \f(CW\*(C`\-B\*(C'\fR switches work as follows. The first block or so of the
460file is examined for odd characters such as strange control codes or
461characters with the high bit set. If too many strange characters (>30%)
462are found, it's a \f(CW\*(C`\-B\*(C'\fR file, otherwise it's a \f(CW\*(C`\-T\*(C'\fR file. Also, any file
463containing null in the first block is considered a binary file. If \f(CW\*(C`\-T\*(C'\fR
464or \f(CW\*(C`\-B\*(C'\fR is used on a filehandle, the current \s-1IO\s0 buffer is examined
465rather than the first block. Both \f(CW\*(C`\-T\*(C'\fR and \f(CW\*(C`\-B\*(C'\fR return true on a null
466file, or a file at \s-1EOF\s0 when testing a filehandle. Because you have to
467read a file to do the \f(CW\*(C`\-T\*(C'\fR test, on most occasions you want to use a \f(CW\*(C`\-f\*(C'\fR
468against the file first, as in \f(CW\*(C`next unless \-f $file && \-T $file\*(C'\fR.
469.Sp
470If any of the file tests (or either the \f(CW\*(C`stat\*(C'\fR or \f(CW\*(C`lstat\*(C'\fR operators) are given
471the special filehandle consisting of a solitary underline, then the stat
472structure of the previous file test (or stat operator) is used, saving
473a system call. (This doesn't work with \f(CW\*(C`\-t\*(C'\fR, and you need to remember
474that \fIlstat()\fR and \f(CW\*(C`\-l\*(C'\fR will leave values in the stat structure for the
475symbolic link, not the real file.) (Also, if the stat buffer was filled by
476a \f(CW\*(C`lstat\*(C'\fR call, \f(CW\*(C`\-T\*(C'\fR and \f(CW\*(C`\-B\*(C'\fR will reset it with the results of \f(CW\*(C`stat _\*(C'\fR).
477Example:
478.Sp
479.Vb 1
480\& print "Can do.\en" if -r $a || -w _ || -x _;
481.Ve
482.Sp
483.Vb 9
484\& stat($filename);
485\& print "Readable\en" if -r _;
486\& print "Writable\en" if -w _;
487\& print "Executable\en" if -x _;
488\& print "Setuid\en" if -u _;
489\& print "Setgid\en" if -g _;
490\& print "Sticky\en" if -k _;
491\& print "Text\en" if -T _;
492\& print "Binary\en" if -B _;
493.Ve
494.IP "abs \s-1VALUE\s0" 8
495.IX Item "abs VALUE"
496.PD 0
497.IP "abs" 8
498.IX Item "abs"
499.PD
500Returns the absolute value of its argument.
501If \s-1VALUE\s0 is omitted, uses \f(CW$_\fR.
502.IP "accept \s-1NEWSOCKET\s0,GENERICSOCKET" 8
503.IX Item "accept NEWSOCKET,GENERICSOCKET"
504Accepts an incoming socket connect, just as the \fIaccept\fR\|(2) system call
505does. Returns the packed address if it succeeded, false otherwise.
506See the example in \*(L"Sockets: Client/Server Communication\*(R" in perlipc.
507.Sp
508On systems that support a close-on-exec flag on files, the flag will
509be set for the newly opened file descriptor, as determined by the
510value of $^F. See \*(L"$^F\*(R" in perlvar.
511.IP "alarm \s-1SECONDS\s0" 8
512.IX Item "alarm SECONDS"
513.PD 0
514.IP "alarm" 8
515.IX Item "alarm"
516.PD
517Arranges to have a \s-1SIGALRM\s0 delivered to this process after the
518specified number of wallclock seconds have elapsed. If \s-1SECONDS\s0 is not
519specified, the value stored in \f(CW$_\fR is used. (On some machines,
520unfortunately, the elapsed time may be up to one second less or more
521than you specified because of how seconds are counted, and process
522scheduling may delay the delivery of the signal even further.)
523.Sp
524Only one timer may be counting at once. Each call disables the
525previous timer, and an argument of \f(CW0\fR may be supplied to cancel the
526previous timer without starting a new one. The returned value is the
527amount of time remaining on the previous timer.
528.Sp
529For delays of finer granularity than one second, you may use Perl's
530four-argument version of \fIselect()\fR leaving the first three arguments
531undefined, or you might be able to use the \f(CW\*(C`syscall\*(C'\fR interface to
532access \fIsetitimer\fR\|(2) if your system supports it. The Time::HiRes
533module (from \s-1CPAN\s0, and starting from Perl 5.8 part of the standard
534distribution) may also prove useful.
535.Sp
536It is usually a mistake to intermix \f(CW\*(C`alarm\*(C'\fR and \f(CW\*(C`sleep\*(C'\fR calls.
537(\f(CW\*(C`sleep\*(C'\fR may be internally implemented in your system with \f(CW\*(C`alarm\*(C'\fR)
538.Sp
539If you want to use \f(CW\*(C`alarm\*(C'\fR to time out a system call you need to use an
540\&\f(CW\*(C`eval\*(C'\fR/\f(CW\*(C`die\*(C'\fR pair. You can't rely on the alarm causing the system call to
541fail with \f(CW$!\fR set to \f(CW\*(C`EINTR\*(C'\fR because Perl sets up signal handlers to
542restart system calls on some systems. Using \f(CW\*(C`eval\*(C'\fR/\f(CW\*(C`die\*(C'\fR always works,
543modulo the caveats given in \*(L"Signals\*(R" in perlipc.
544.Sp
545.Vb 13
546\& eval {
547\& local $SIG{ALRM} = sub { die "alarm\en" }; # NB: \en required
548\& alarm $timeout;
549\& $nread = sysread SOCKET, $buffer, $size;
550\& alarm 0;
551\& };
552\& if ($@) {
553\& die unless $@ eq "alarm\en"; # propagate unexpected errors
554\& # timed out
555\& }
556\& else {
557\& # didn't
558\& }
559.Ve
560.IP "atan2 Y,X" 8
561.IX Item "atan2 Y,X"
562Returns the arctangent of Y/X in the range \-PI to \s-1PI\s0.
563.Sp
564For the tangent operation, you may use the \f(CW\*(C`Math::Trig::tan\*(C'\fR
565function, or use the familiar relation:
566.Sp
567.Vb 1
568\& sub tan { sin($_[0]) / cos($_[0]) }
569.Ve
570.IP "bind \s-1SOCKET\s0,NAME" 8
571.IX Item "bind SOCKET,NAME"
572Binds a network address to a socket, just as the bind system call
573does. Returns true if it succeeded, false otherwise. \s-1NAME\s0 should be a
574packed address of the appropriate type for the socket. See the examples in
575\&\*(L"Sockets: Client/Server Communication\*(R" in perlipc.
576.IP "binmode \s-1FILEHANDLE\s0, \s-1LAYER\s0" 8
577.IX Item "binmode FILEHANDLE, LAYER"
578.PD 0
579.IP "binmode \s-1FILEHANDLE\s0" 8
580.IX Item "binmode FILEHANDLE"
581.PD
582Arranges for \s-1FILEHANDLE\s0 to be read or written in \*(L"binary\*(R" or \*(L"text\*(R"
583mode on systems where the run-time libraries distinguish between
584binary and text files. If \s-1FILEHANDLE\s0 is an expression, the value is
585taken as the name of the filehandle. Returns true on success,
586\&\f(CW\*(C`undef\*(C'\fR on failure.
587.Sp
588If \s-1LAYER\s0 is omitted or specified as \f(CW\*(C`:raw\*(C'\fR the filehandle is made
589suitable for passing binary data. This includes turning off possible \s-1CRLF\s0
590translation and marking it as bytes (as opposed to Unicode characters).
591Note that as desipite what may be implied in \fI\*(L"Programming Perl\*(R"\fR
592(the Camel) or elsewhere \f(CW\*(C`:raw\*(C'\fR is \fInot\fR the simply inverse of \f(CW\*(C`:crlf\*(C'\fR
593\&\*(-- other layers which would affect binary nature of the stream are
594\&\fIalso\fR disabled. See PerlIO, perlrun and the discussion about the
595\&\s-1PERLIO\s0 environment variable.
596.Sp
597\&\fIThe \s-1LAYER\s0 parameter of the \fIbinmode()\fI function is described as \*(L"\s-1DISCIPLINE\s0\*(R"
598in \*(L"Programming Perl, 3rd Edition\*(R". However, since the publishing of this
599book, by many known as \*(L"Camel \s-1III\s0\*(R", the consensus of the naming of this
600functionality has moved from \*(L"discipline\*(R" to \*(L"layer\*(R". All documentation
601of this version of Perl therefore refers to \*(L"layers\*(R" rather than to
602\&\*(L"disciplines\*(R". Now back to the regularly scheduled documentation...\fR
603.Sp
604On some systems (in general, \s-1DOS\s0 and Windows-based systems) \fIbinmode()\fR
605is necessary when you're not working with a text file. For the sake
606of portability it is a good idea to always use it when appropriate,
607and to never use it when it isn't appropriate.
608.Sp
609In other words: regardless of platform, use \fIbinmode()\fR on binary files
610(like for example images).
611.Sp
612If \s-1LAYER\s0 is present it is a single string, but may contain
613multiple directives. The directives alter the behaviour of the
614file handle. When \s-1LAYER\s0 is present using binmode on text
615file makes sense.
616.Sp
617To mark \s-1FILEHANDLE\s0 as \s-1UTF\-8\s0, use \f(CW\*(C`:utf8\*(C'\fR.
618.Sp
619The \f(CW\*(C`:bytes\*(C'\fR, \f(CW\*(C`:crlf\*(C'\fR, and \f(CW\*(C`:utf8\*(C'\fR, and any other directives of the
620form \f(CW\*(C`:...\*(C'\fR, are called I/O \fIlayers\fR. The \f(CW\*(C`open\*(C'\fR pragma can be used to
621establish default I/O layers. See open.
622.Sp
623In general, \fIbinmode()\fR should be called after \fIopen()\fR but before any I/O
624is done on the filehandle. Calling \fIbinmode()\fR will normally flush any
625pending buffered output data (and perhaps pending input data) on the
626handle. An exception to this is the \f(CW\*(C`:encoding\*(C'\fR layer that
627changes the default character encoding of the handle, see open.
628The \f(CW\*(C`:encoding\*(C'\fR layer sometimes needs to be called in
629mid\-stream, and it doesn't flush the stream.
630.Sp
631The operating system, device drivers, C libraries, and Perl run-time
632system all work together to let the programmer treat a single
633character (\f(CW\*(C`\en\*(C'\fR) as the line terminator, irrespective of the external
634representation. On many operating systems, the native text file
635representation matches the internal representation, but on some
636platforms the external representation of \f(CW\*(C`\en\*(C'\fR is made up of more than
637one character.
638.Sp
639Mac \s-1OS\s0, all variants of Unix, and Stream_LF files on \s-1VMS\s0 use a single
640character to end each line in the external representation of text (even
641though that single character is \s-1CARRIAGE\s0 \s-1RETURN\s0 on Mac \s-1OS\s0 and \s-1LINE\s0 \s-1FEED\s0
642on Unix and most \s-1VMS\s0 files). In other systems like \s-1OS/2\s0, \s-1DOS\s0 and the
643various flavors of MS-Windows your program sees a \f(CW\*(C`\en\*(C'\fR as a simple \f(CW\*(C`\ecJ\*(C'\fR,
644but what's stored in text files are the two characters \f(CW\*(C`\ecM\ecJ\*(C'\fR. That
645means that, if you don't use \fIbinmode()\fR on these systems, \f(CW\*(C`\ecM\ecJ\*(C'\fR
646sequences on disk will be converted to \f(CW\*(C`\en\*(C'\fR on input, and any \f(CW\*(C`\en\*(C'\fR in
647your program will be converted back to \f(CW\*(C`\ecM\ecJ\*(C'\fR on output. This is what
648you want for text files, but it can be disastrous for binary files.
649.Sp
650Another consequence of using \fIbinmode()\fR (on some systems) is that
651special end-of-file markers will be seen as part of the data stream.
652For systems from the Microsoft family this means that if your binary
653data contains \f(CW\*(C`\ecZ\*(C'\fR, the I/O subsystem will regard it as the end of
654the file, unless you use \fIbinmode()\fR.
655.Sp
656\&\fIbinmode()\fR is not only important for \fIreadline()\fR and \fIprint()\fR operations,
657but also when using \fIread()\fR, \fIseek()\fR, \fIsysread()\fR, \fIsyswrite()\fR and \fItell()\fR
658(see perlport for more details). See the \f(CW$/\fR and \f(CW\*(C`$\e\*(C'\fR variables
659in perlvar for how to manually set your input and output
660line-termination sequences.
661.IP "bless \s-1REF\s0,CLASSNAME" 8
662.IX Item "bless REF,CLASSNAME"
663.PD 0
664.IP "bless \s-1REF\s0" 8
665.IX Item "bless REF"
666.PD
667This function tells the thingy referenced by \s-1REF\s0 that it is now an object
668in the \s-1CLASSNAME\s0 package. If \s-1CLASSNAME\s0 is omitted, the current package
669is used. Because a \f(CW\*(C`bless\*(C'\fR is often the last thing in a constructor,
670it returns the reference for convenience. Always use the two-argument
671version if the function doing the blessing might be inherited by a
672derived class. See perltoot and perlobj for more about the blessing
673(and blessings) of objects.
674.Sp
675Consider always blessing objects in CLASSNAMEs that are mixed case.
676Namespaces with all lowercase names are considered reserved for
677Perl pragmata. Builtin types have all uppercase names, so to prevent
678confusion, you may wish to avoid such package names as well. Make sure
679that \s-1CLASSNAME\s0 is a true value.
680.Sp
681See \*(L"Perl Modules\*(R" in perlmod.
682.IP "caller \s-1EXPR\s0" 8
683.IX Item "caller EXPR"
684.PD 0
685.IP "caller" 8
686.IX Item "caller"
687.PD
688Returns the context of the current subroutine call. In scalar context,
689returns the caller's package name if there is a caller, that is, if
690we're in a subroutine or \f(CW\*(C`eval\*(C'\fR or \f(CW\*(C`require\*(C'\fR, and the undefined value
691otherwise. In list context, returns
692.Sp
693.Vb 1
694\& ($package, $filename, $line) = caller;
695.Ve
696.Sp
697With \s-1EXPR\s0, it returns some extra information that the debugger uses to
698print a stack trace. The value of \s-1EXPR\s0 indicates how many call frames
699to go back before the current one.
700.Sp
701.Vb 2
702\& ($package, $filename, $line, $subroutine, $hasargs,
703\& $wantarray, $evaltext, $is_require, $hints, $bitmask) = caller($i);
704.Ve
705.Sp
706Here \f(CW$subroutine\fR may be \f(CW\*(C`(eval)\*(C'\fR if the frame is not a subroutine
707call, but an \f(CW\*(C`eval\*(C'\fR. In such a case additional elements \f(CW$evaltext\fR and
708\&\f(CW$is_require\fR are set: \f(CW$is_require\fR is true if the frame is created by a
709\&\f(CW\*(C`require\*(C'\fR or \f(CW\*(C`use\*(C'\fR statement, \f(CW$evaltext\fR contains the text of the
710\&\f(CW\*(C`eval EXPR\*(C'\fR statement. In particular, for an \f(CW\*(C`eval BLOCK\*(C'\fR statement,
711\&\f(CW$filename\fR is \f(CW\*(C`(eval)\*(C'\fR, but \f(CW$evaltext\fR is undefined. (Note also that
712each \f(CW\*(C`use\*(C'\fR statement creates a \f(CW\*(C`require\*(C'\fR frame inside an \f(CW\*(C`eval EXPR\*(C'\fR
713frame.) \f(CW$subroutine\fR may also be \f(CW\*(C`(unknown)\*(C'\fR if this particular
714subroutine happens to have been deleted from the symbol table.
715\&\f(CW$hasargs\fR is true if a new instance of \f(CW@_\fR was set up for the frame.
716\&\f(CW$hints\fR and \f(CW$bitmask\fR contain pragmatic hints that the caller was
717compiled with. The \f(CW$hints\fR and \f(CW$bitmask\fR values are subject to change
718between versions of Perl, and are not meant for external use.
719.Sp
720Furthermore, when called from within the \s-1DB\s0 package, caller returns more
721detailed information: it sets the list variable \f(CW@DB::args\fR to be the
722arguments with which the subroutine was invoked.
723.Sp
724Be aware that the optimizer might have optimized call frames away before
725\&\f(CW\*(C`caller\*(C'\fR had a chance to get the information. That means that \f(CWcaller(N)\fR
726might not return information about the call frame you expect it do, for
727\&\f(CW\*(C`N > 1\*(C'\fR. In particular, \f(CW@DB::args\fR might have information from the
728previous time \f(CW\*(C`caller\*(C'\fR was called.
729.IP "chdir \s-1EXPR\s0" 8
730.IX Item "chdir EXPR"
731Changes the working directory to \s-1EXPR\s0, if possible. If \s-1EXPR\s0 is omitted,
732changes to the directory specified by \f(CW$ENV{HOME}\fR, if set; if not,
733changes to the directory specified by \f(CW$ENV{LOGDIR}\fR. (Under \s-1VMS\s0, the
734variable \f(CW$ENV{SYS$LOGIN}\fR is also checked, and used if it is set.) If
735neither is set, \f(CW\*(C`chdir\*(C'\fR does nothing. It returns true upon success,
736false otherwise. See the example under \f(CW\*(C`die\*(C'\fR.
737.IP "chmod \s-1LIST\s0" 8
738.IX Item "chmod LIST"
739Changes the permissions of a list of files. The first element of the
740list must be the numerical mode, which should probably be an octal
741number, and which definitely should \fInot\fR a string of octal digits:
742\&\f(CW0644\fR is okay, \f(CW'0644'\fR is not. Returns the number of files
743successfully changed. See also \*(L"oct\*(R", if all you have is a string.
744.Sp
745.Vb 6
746\& $cnt = chmod 0755, 'foo', 'bar';
747\& chmod 0755, @executables;
748\& $mode = '0644'; chmod $mode, 'foo'; # !!! sets mode to
749\& # --w----r-T
750\& $mode = '0644'; chmod oct($mode), 'foo'; # this is better
751\& $mode = 0644; chmod $mode, 'foo'; # this is best
752.Ve
753.Sp
754You can also import the symbolic \f(CW\*(C`S_I*\*(C'\fR constants from the Fcntl
755module:
756.Sp
757.Vb 1
758\& use Fcntl ':mode';
759.Ve
760.Sp
761.Vb 2
762\& chmod S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH, @executables;
763\& # This is identical to the chmod 0755 of the above example.
764.Ve
765.IP "chomp \s-1VARIABLE\s0" 8
766.IX Item "chomp VARIABLE"
767.PD 0
768.IP "chomp( \s-1LIST\s0 )" 8
769.IX Item "chomp( LIST )"
770.IP "chomp" 8
771.IX Item "chomp"
772.PD
773This safer version of \*(L"chop\*(R" removes any trailing string
774that corresponds to the current value of \f(CW$/\fR (also known as
775\&\f(CW$INPUT_RECORD_SEPARATOR\fR in the \f(CW\*(C`English\*(C'\fR module). It returns the total
776number of characters removed from all its arguments. It's often used to
777remove the newline from the end of an input record when you're worried
778that the final record may be missing its newline. When in paragraph
779mode (\f(CW\*(C`$/ = ""\*(C'\fR), it removes all trailing newlines from the string.
780When in slurp mode (\f(CW\*(C`$/ = undef\*(C'\fR) or fixed-length record mode (\f(CW$/\fR is
781a reference to an integer or the like, see perlvar) \fIchomp()\fR won't
782remove anything.
783If \s-1VARIABLE\s0 is omitted, it chomps \f(CW$_\fR. Example:
784.Sp
785.Vb 5
786\& while (<>) {
787\& chomp; # avoid \en on last field
788\& @array = split(/:/);
789\& # ...
790\& }
791.Ve
792.Sp
793If \s-1VARIABLE\s0 is a hash, it chomps the hash's values, but not its keys.
794.Sp
795You can actually chomp anything that's an lvalue, including an assignment:
796.Sp
797.Vb 2
798\& chomp($cwd = `pwd`);
799\& chomp($answer = <STDIN>);
800.Ve
801.Sp
802If you chomp a list, each element is chomped, and the total number of
803characters removed is returned.
804.Sp
805Note that parentheses are necessary when you're chomping anything
806that is not a simple variable. This is because \f(CW\*(C`chomp $cwd = `pwd`;\*(C'\fR
807is interpreted as \f(CW\*(C`(chomp $cwd) = `pwd`;\*(C'\fR, rather than as
808\&\f(CW\*(C`chomp( $cwd = `pwd` )\*(C'\fR which you might expect. Similarly,
809\&\f(CW\*(C`chomp $a, $b\*(C'\fR is interpreted as \f(CW\*(C`chomp($a), $b\*(C'\fR rather than
810as \f(CW\*(C`chomp($a, $b)\*(C'\fR.
811.IP "chop \s-1VARIABLE\s0" 8
812.IX Item "chop VARIABLE"
813.PD 0
814.IP "chop( \s-1LIST\s0 )" 8
815.IX Item "chop( LIST )"
816.IP "chop" 8
817.IX Item "chop"
818.PD
819Chops off the last character of a string and returns the character
820chopped. It is much more efficient than \f(CW\*(C`s/.$//s\*(C'\fR because it neither
821scans nor copies the string. If \s-1VARIABLE\s0 is omitted, chops \f(CW$_\fR.
822If \s-1VARIABLE\s0 is a hash, it chops the hash's values, but not its keys.
823.Sp
824You can actually chop anything that's an lvalue, including an assignment.
825.Sp
826If you chop a list, each element is chopped. Only the value of the
827last \f(CW\*(C`chop\*(C'\fR is returned.
828.Sp
829Note that \f(CW\*(C`chop\*(C'\fR returns the last character. To return all but the last
830character, use \f(CW\*(C`substr($string, 0, \-1)\*(C'\fR.
831.Sp
832See also \*(L"chomp\*(R".
833.IP "chown \s-1LIST\s0" 8
834.IX Item "chown LIST"
835Changes the owner (and group) of a list of files. The first two
836elements of the list must be the \fInumeric\fR uid and gid, in that
837order. A value of \-1 in either position is interpreted by most
838systems to leave that value unchanged. Returns the number of files
839successfully changed.
840.Sp
841.Vb 2
842\& $cnt = chown $uid, $gid, 'foo', 'bar';
843\& chown $uid, $gid, @filenames;
844.Ve
845.Sp
846Here's an example that looks up nonnumeric uids in the passwd file:
847.Sp
848.Vb 4
849\& print "User: ";
850\& chomp($user = <STDIN>);
851\& print "Files: ";
852\& chomp($pattern = <STDIN>);
853.Ve
854.Sp
855.Vb 2
856\& ($login,$pass,$uid,$gid) = getpwnam($user)
857\& or die "$user not in passwd file";
858.Ve
859.Sp
860.Vb 2
861\& @ary = glob($pattern); # expand filenames
862\& chown $uid, $gid, @ary;
863.Ve
864.Sp
865On most systems, you are not allowed to change the ownership of the
866file unless you're the superuser, although you should be able to change
867the group to any of your secondary groups. On insecure systems, these
868restrictions may be relaxed, but this is not a portable assumption.
869On \s-1POSIX\s0 systems, you can detect this condition this way:
870.Sp
871.Vb 2
872\& use POSIX qw(sysconf _PC_CHOWN_RESTRICTED);
873\& $can_chown_giveaway = not sysconf(_PC_CHOWN_RESTRICTED);
874.Ve
875.IP "chr \s-1NUMBER\s0" 8
876.IX Item "chr NUMBER"
877.PD 0
878.IP "chr" 8
879.IX Item "chr"
880.PD
881Returns the character represented by that \s-1NUMBER\s0 in the character set.
882For example, \f(CW\*(C`chr(65)\*(C'\fR is \f(CW"A"\fR in either \s-1ASCII\s0 or Unicode, and
883chr(0x263a) is a Unicode smiley face. Note that characters from 127
884to 255 (inclusive) are by default not encoded in Unicode for backward
885compatibility reasons (but see encoding).
886.Sp
887For the reverse, use \*(L"ord\*(R".
888See perlunicode and encoding for more about Unicode.
889.Sp
890If \s-1NUMBER\s0 is omitted, uses \f(CW$_\fR.
891.IP "chroot \s-1FILENAME\s0" 8
892.IX Item "chroot FILENAME"
893.PD 0
894.IP "chroot" 8
895.IX Item "chroot"
896.PD
897This function works like the system call by the same name: it makes the
898named directory the new root directory for all further pathnames that
899begin with a \f(CW\*(C`/\*(C'\fR by your process and all its children. (It doesn't
900change your current working directory, which is unaffected.) For security
901reasons, this call is restricted to the superuser. If \s-1FILENAME\s0 is
902omitted, does a \f(CW\*(C`chroot\*(C'\fR to \f(CW$_\fR.
903.IP "close \s-1FILEHANDLE\s0" 8
904.IX Item "close FILEHANDLE"
905.PD 0
906.IP "close" 8
907.IX Item "close"
908.PD
909Closes the file or pipe associated with the file handle, returning
910true only if \s-1IO\s0 buffers are successfully flushed and closes the system
911file descriptor. Closes the currently selected filehandle if the
912argument is omitted.
913.Sp
914You don't have to close \s-1FILEHANDLE\s0 if you are immediately going to do
915another \f(CW\*(C`open\*(C'\fR on it, because \f(CW\*(C`open\*(C'\fR will close it for you. (See
916\&\f(CW\*(C`open\*(C'\fR.) However, an explicit \f(CW\*(C`close\*(C'\fR on an input file resets the line
917counter (\f(CW$.\fR), while the implicit close done by \f(CW\*(C`open\*(C'\fR does not.
918.Sp
919If the file handle came from a piped open \f(CW\*(C`close\*(C'\fR will additionally
920return false if one of the other system calls involved fails or if the
921program exits with non-zero status. (If the only problem was that the
922program exited non-zero \f(CW$!\fR will be set to \f(CW0\fR.) Closing a pipe
923also waits for the process executing on the pipe to complete, in case you
924want to look at the output of the pipe afterwards, and
925implicitly puts the exit status value of that command into \f(CW$?\fR.
926.Sp
927Prematurely closing the read end of a pipe (i.e. before the process
928writing to it at the other end has closed it) will result in a
929\&\s-1SIGPIPE\s0 being delivered to the writer. If the other end can't
930handle that, be sure to read all the data before closing the pipe.
931.Sp
932Example:
933.Sp
934.Vb 8
935\& open(OUTPUT, '|sort >foo') # pipe to sort
936\& or die "Can't start sort: $!";
937\& #... # print stuff to output
938\& close OUTPUT # wait for sort to finish
939\& or warn $! ? "Error closing sort pipe: $!"
940\& : "Exit status $? from sort";
941\& open(INPUT, 'foo') # get sort's results
942\& or die "Can't open 'foo' for input: $!";
943.Ve
944.Sp
945\&\s-1FILEHANDLE\s0 may be an expression whose value can be used as an indirect
946filehandle, usually the real filehandle name.
947.IP "closedir \s-1DIRHANDLE\s0" 8
948.IX Item "closedir DIRHANDLE"
949Closes a directory opened by \f(CW\*(C`opendir\*(C'\fR and returns the success of that
950system call.
951.Sp
952\&\s-1DIRHANDLE\s0 may be an expression whose value can be used as an indirect
953dirhandle, usually the real dirhandle name.
954.IP "connect \s-1SOCKET\s0,NAME" 8
955.IX Item "connect SOCKET,NAME"
956Attempts to connect to a remote socket, just as the connect system call
957does. Returns true if it succeeded, false otherwise. \s-1NAME\s0 should be a
958packed address of the appropriate type for the socket. See the examples in
959\&\*(L"Sockets: Client/Server Communication\*(R" in perlipc.
960.IP "continue \s-1BLOCK\s0" 8
961.IX Item "continue BLOCK"
962Actually a flow control statement rather than a function. If there is a
963\&\f(CW\*(C`continue\*(C'\fR \s-1BLOCK\s0 attached to a \s-1BLOCK\s0 (typically in a \f(CW\*(C`while\*(C'\fR or
964\&\f(CW\*(C`foreach\*(C'\fR), it is always executed just before the conditional is about to
965be evaluated again, just like the third part of a \f(CW\*(C`for\*(C'\fR loop in C. Thus
966it can be used to increment a loop variable, even when the loop has been
967continued via the \f(CW\*(C`next\*(C'\fR statement (which is similar to the C \f(CW\*(C`continue\*(C'\fR
968statement).
969.Sp
970\&\f(CW\*(C`last\*(C'\fR, \f(CW\*(C`next\*(C'\fR, or \f(CW\*(C`redo\*(C'\fR may appear within a \f(CW\*(C`continue\*(C'\fR
971block. \f(CW\*(C`last\*(C'\fR and \f(CW\*(C`redo\*(C'\fR will behave as if they had been executed within
972the main block. So will \f(CW\*(C`next\*(C'\fR, but since it will execute a \f(CW\*(C`continue\*(C'\fR
973block, it may be more entertaining.
974.Sp
975.Vb 9
976\& while (EXPR) {
977\& ### redo always comes here
978\& do_something;
979\& } continue {
980\& ### next always comes here
981\& do_something_else;
982\& # then back the top to re-check EXPR
983\& }
984\& ### last always comes here
985.Ve
986.Sp
987Omitting the \f(CW\*(C`continue\*(C'\fR section is semantically equivalent to using an
988empty one, logically enough. In that case, \f(CW\*(C`next\*(C'\fR goes directly back
989to check the condition at the top of the loop.
990.IP "cos \s-1EXPR\s0" 8
991.IX Item "cos EXPR"
992.PD 0
993.IP "cos" 8
994.IX Item "cos"
995.PD
996Returns the cosine of \s-1EXPR\s0 (expressed in radians). If \s-1EXPR\s0 is omitted,
997takes cosine of \f(CW$_\fR.
998.Sp
999For the inverse cosine operation, you may use the \f(CW\*(C`Math::Trig::acos()\*(C'\fR
1000function, or use this relation:
1001.Sp
1002.Vb 1
1003\& sub acos { atan2( sqrt(1 - $_[0] * $_[0]), $_[0] ) }
1004.Ve
1005.IP "crypt \s-1PLAINTEXT\s0,SALT" 8
1006.IX Item "crypt PLAINTEXT,SALT"
1007Encrypts a string exactly like the \fIcrypt\fR\|(3) function in the C library
1008(assuming that you actually have a version there that has not been
1009extirpated as a potential munition). This can prove useful for checking
1010the password file for lousy passwords, amongst other things. Only the
1011guys wearing white hats should do this.
1012.Sp
1013Note that \f(CW\*(C`crypt\*(C'\fR is intended to be a one-way function, much like
1014breaking eggs to make an omelette. There is no (known) corresponding
1015decrypt function (in other words, the \fIcrypt()\fR is a one-way hash
1016function). As a result, this function isn't all that useful for
1017cryptography. (For that, see your nearby \s-1CPAN\s0 mirror.)
1018.Sp
1019When verifying an existing encrypted string you should use the
1020encrypted text as the salt (like \f(CW\*(C`crypt($plain, $crypted) eq
1021$crypted\*(C'\fR). This allows your code to work with the standard \f(CW\*(C`crypt\*(C'\fR
1022and with more exotic implementations. In other words, do not assume
1023anything about the returned string itself, or how many bytes in
1024the encrypted string matter.
1025.Sp
1026Traditionally the result is a string of 13 bytes: two first bytes of
1027the salt, followed by 11 bytes from the set \f(CW\*(C`[./0\-9A\-Za\-z]\*(C'\fR, and only
1028the first eight bytes of the encrypted string mattered, but
1029alternative hashing schemes (like \s-1MD5\s0), higher level security schemes
1030(like C2), and implementations on non-UNIX platforms may produce
1031different strings.
1032.Sp
1033When choosing a new salt create a random two character string whose
1034characters come from the set \f(CW\*(C`[./0\-9A\-Za\-z]\*(C'\fR (like \f(CW\*(C`join '', ('.',
1035\&'/', 0..9, 'A'..'Z', 'a'..'z')[rand 64, rand 64]\*(C'\fR).
1036.Sp
1037Here's an example that makes sure that whoever runs this program knows
1038their own password:
1039.Sp
1040.Vb 1
1041\& $pwd = (getpwuid($<))[1];
1042.Ve
1043.Sp
1044.Vb 5
1045\& system "stty -echo";
1046\& print "Password: ";
1047\& chomp($word = <STDIN>);
1048\& print "\en";
1049\& system "stty echo";
1050.Ve
1051.Sp
1052.Vb 5
1053\& if (crypt($word, $pwd) ne $pwd) {
1054\& die "Sorry...\en";
1055\& } else {
1056\& print "ok\en";
1057\& }
1058.Ve
1059.Sp
1060Of course, typing in your own password to whoever asks you
1061for it is unwise.
1062.Sp
1063The crypt function is unsuitable for encrypting large quantities
1064of data, not least of all because you can't get the information
1065back. Look at the \fIby\-module/Crypt\fR and \fIby\-module/PGP\fR directories
1066on your favorite \s-1CPAN\s0 mirror for a slew of potentially useful
1067modules.
1068.Sp
1069If using \fIcrypt()\fR on a Unicode string (which \fIpotentially\fR has
1070characters with codepoints above 255), Perl tries to make sense
1071of the situation by trying to downgrade (a copy of the string)
1072the string back to an eight-bit byte string before calling \fIcrypt()\fR
1073(on that copy). If that works, good. If not, \fIcrypt()\fR dies with
1074\&\f(CW\*(C`Wide character in crypt\*(C'\fR.
1075.IP "dbmclose \s-1HASH\s0" 8
1076.IX Item "dbmclose HASH"
1077[This function has been largely superseded by the \f(CW\*(C`untie\*(C'\fR function.]
1078.Sp
1079Breaks the binding between a \s-1DBM\s0 file and a hash.
1080.IP "dbmopen \s-1HASH\s0,DBNAME,MASK" 8
1081.IX Item "dbmopen HASH,DBNAME,MASK"
1082[This function has been largely superseded by the \f(CW\*(C`tie\*(C'\fR function.]
1083.Sp
1084This binds a \fIdbm\fR\|(3), \fIndbm\fR\|(3), \fIsdbm\fR\|(3), \fIgdbm\fR\|(3), or Berkeley \s-1DB\s0 file to a
1085hash. \s-1HASH\s0 is the name of the hash. (Unlike normal \f(CW\*(C`open\*(C'\fR, the first
1086argument is \fInot\fR a filehandle, even though it looks like one). \s-1DBNAME\s0
1087is the name of the database (without the \fI.dir\fR or \fI.pag\fR extension if
1088any). If the database does not exist, it is created with protection
1089specified by \s-1MASK\s0 (as modified by the \f(CW\*(C`umask\*(C'\fR). If your system supports
1090only the older \s-1DBM\s0 functions, you may perform only one \f(CW\*(C`dbmopen\*(C'\fR in your
1091program. In older versions of Perl, if your system had neither \s-1DBM\s0 nor
1092ndbm, calling \f(CW\*(C`dbmopen\*(C'\fR produced a fatal error; it now falls back to
1093\&\fIsdbm\fR\|(3).
1094.Sp
1095If you don't have write access to the \s-1DBM\s0 file, you can only read hash
1096variables, not set them. If you want to test whether you can write,
1097either use file tests or try setting a dummy hash entry inside an \f(CW\*(C`eval\*(C'\fR,
1098which will trap the error.
1099.Sp
1100Note that functions such as \f(CW\*(C`keys\*(C'\fR and \f(CW\*(C`values\*(C'\fR may return huge lists
1101when used on large \s-1DBM\s0 files. You may prefer to use the \f(CW\*(C`each\*(C'\fR
1102function to iterate over large \s-1DBM\s0 files. Example:
1103.Sp
1104.Vb 6
1105\& # print out history file offsets
1106\& dbmopen(%HIST,'/usr/lib/news/history',0666);
1107\& while (($key,$val) = each %HIST) {
1108\& print $key, ' = ', unpack('L',$val), "\en";
1109\& }
1110\& dbmclose(%HIST);
1111.Ve
1112.Sp
1113See also AnyDBM_File for a more general description of the pros and
1114cons of the various dbm approaches, as well as DB_File for a particularly
1115rich implementation.
1116.Sp
1117You can control which \s-1DBM\s0 library you use by loading that library
1118before you call \fIdbmopen()\fR:
1119.Sp
1120.Vb 3
1121\& use DB_File;
1122\& dbmopen(%NS_Hist, "$ENV{HOME}/.netscape/history.db")
1123\& or die "Can't open netscape history file: $!";
1124.Ve
1125.IP "defined \s-1EXPR\s0" 8
1126.IX Item "defined EXPR"
1127.PD 0
1128.IP "defined" 8
1129.IX Item "defined"
1130.PD
1131Returns a Boolean value telling whether \s-1EXPR\s0 has a value other than
1132the undefined value \f(CW\*(C`undef\*(C'\fR. If \s-1EXPR\s0 is not present, \f(CW$_\fR will be
1133checked.
1134.Sp
1135Many operations return \f(CW\*(C`undef\*(C'\fR to indicate failure, end of file,
1136system error, uninitialized variable, and other exceptional
1137conditions. This function allows you to distinguish \f(CW\*(C`undef\*(C'\fR from
1138other values. (A simple Boolean test will not distinguish among
1139\&\f(CW\*(C`undef\*(C'\fR, zero, the empty string, and \f(CW"0"\fR, which are all equally
1140false.) Note that since \f(CW\*(C`undef\*(C'\fR is a valid scalar, its presence
1141doesn't \fInecessarily\fR indicate an exceptional condition: \f(CW\*(C`pop\*(C'\fR
1142returns \f(CW\*(C`undef\*(C'\fR when its argument is an empty array, \fIor\fR when the
1143element to return happens to be \f(CW\*(C`undef\*(C'\fR.
1144.Sp
1145You may also use \f(CW\*(C`defined(&func)\*(C'\fR to check whether subroutine \f(CW&func\fR
1146has ever been defined. The return value is unaffected by any forward
1147declarations of \f(CW&foo\fR. Note that a subroutine which is not defined
1148may still be callable: its package may have an \f(CW\*(C`AUTOLOAD\*(C'\fR method that
1149makes it spring into existence the first time that it is called \*(-- see
1150perlsub.
1151.Sp
1152Use of \f(CW\*(C`defined\*(C'\fR on aggregates (hashes and arrays) is deprecated. It
1153used to report whether memory for that aggregate has ever been
1154allocated. This behavior may disappear in future versions of Perl.
1155You should instead use a simple test for size:
1156.Sp
1157.Vb 2
1158\& if (@an_array) { print "has array elements\en" }
1159\& if (%a_hash) { print "has hash members\en" }
1160.Ve
1161.Sp
1162When used on a hash element, it tells you whether the value is defined,
1163not whether the key exists in the hash. Use \*(L"exists\*(R" for the latter
1164purpose.
1165.Sp
1166Examples:
1167.Sp
1168.Vb 6
1169\& print if defined $switch{'D'};
1170\& print "$val\en" while defined($val = pop(@ary));
1171\& die "Can't readlink $sym: $!"
1172\& unless defined($value = readlink $sym);
1173\& sub foo { defined &$bar ? &$bar(@_) : die "No bar"; }
1174\& $debugging = 0 unless defined $debugging;
1175.Ve
1176.Sp
1177Note: Many folks tend to overuse \f(CW\*(C`defined\*(C'\fR, and then are surprised to
1178discover that the number \f(CW0\fR and \f(CW""\fR (the zero-length string) are, in fact,
1179defined values. For example, if you say
1180.Sp
1181.Vb 1
1182\& "ab" =~ /a(.*)b/;
1183.Ve
1184.Sp
1185The pattern match succeeds, and \f(CW$1\fR is defined, despite the fact that it
1186matched \*(L"nothing\*(R". But it didn't really match nothing\*(--rather, it
1187matched something that happened to be zero characters long. This is all
1188very above-board and honest. When a function returns an undefined value,
1189it's an admission that it couldn't give you an honest answer. So you
1190should use \f(CW\*(C`defined\*(C'\fR only when you're questioning the integrity of what
1191you're trying to do. At other times, a simple comparison to \f(CW0\fR or \f(CW""\fR is
1192what you want.
1193.Sp
1194See also \*(L"undef\*(R", \*(L"exists\*(R", \*(L"ref\*(R".
1195.IP "delete \s-1EXPR\s0" 8
1196.IX Item "delete EXPR"
1197Given an expression that specifies a hash element, array element, hash slice,
1198or array slice, deletes the specified element(s) from the hash or array.
1199In the case of an array, if the array elements happen to be at the end,
1200the size of the array will shrink to the highest element that tests
1201true for \fIexists()\fR (or 0 if no such element exists).
1202.Sp
1203Returns each element so deleted or the undefined value if there was no such
1204element. Deleting from \f(CW$ENV{}\fR modifies the environment. Deleting from
1205a hash tied to a \s-1DBM\s0 file deletes the entry from the \s-1DBM\s0 file. Deleting
1206from a \f(CW\*(C`tie\*(C'\fRd hash or array may not necessarily return anything.
1207.Sp
1208Deleting an array element effectively returns that position of the array
1209to its initial, uninitialized state. Subsequently testing for the same
1210element with \fIexists()\fR will return false. Note that deleting array
1211elements in the middle of an array will not shift the index of the ones
1212after them down\*(--use \fIsplice()\fR for that. See \*(L"exists\*(R".
1213.Sp
1214The following (inefficiently) deletes all the values of \f(CW%HASH\fR and \f(CW@ARRAY:\fR
1215.Sp
1216.Vb 3
1217\& foreach $key (keys %HASH) {
1218\& delete $HASH{$key};
1219\& }
1220.Ve
1221.Sp
1222.Vb 3
1223\& foreach $index (0 .. $#ARRAY) {
1224\& delete $ARRAY[$index];
1225\& }
1226.Ve
1227.Sp
1228And so do these:
1229.Sp
1230.Vb 1
1231\& delete @HASH{keys %HASH};
1232.Ve
1233.Sp
1234.Vb 1
1235\& delete @ARRAY[0 .. $#ARRAY];
1236.Ve
1237.Sp
1238But both of these are slower than just assigning the empty list
1239or undefining \f(CW%HASH\fR or \f(CW@ARRAY:\fR
1240.Sp
1241.Vb 2
1242\& %HASH = (); # completely empty %HASH
1243\& undef %HASH; # forget %HASH ever existed
1244.Ve
1245.Sp
1246.Vb 2
1247\& @ARRAY = (); # completely empty @ARRAY
1248\& undef @ARRAY; # forget @ARRAY ever existed
1249.Ve
1250.Sp
1251Note that the \s-1EXPR\s0 can be arbitrarily complicated as long as the final
1252operation is a hash element, array element, hash slice, or array slice
1253lookup:
1254.Sp
1255.Vb 2
1256\& delete $ref->[$x][$y]{$key};
1257\& delete @{$ref->[$x][$y]}{$key1, $key2, @morekeys};
1258.Ve
1259.Sp
1260.Vb 2
1261\& delete $ref->[$x][$y][$index];
1262\& delete @{$ref->[$x][$y]}[$index1, $index2, @moreindices];
1263.Ve
1264.IP "die \s-1LIST\s0" 8
1265.IX Item "die LIST"
1266Outside an \f(CW\*(C`eval\*(C'\fR, prints the value of \s-1LIST\s0 to \f(CW\*(C`STDERR\*(C'\fR and
1267exits with the current value of \f(CW$!\fR (errno). If \f(CW$!\fR is \f(CW0\fR,
1268exits with the value of \f(CW\*(C`($? >> 8)\*(C'\fR (backtick `command`
1269status). If \f(CW\*(C`($? >> 8)\*(C'\fR is \f(CW0\fR, exits with \f(CW255\fR. Inside
1270an \f(CW\*(C`eval(),\*(C'\fR the error message is stuffed into \f(CW$@\fR and the
1271\&\f(CW\*(C`eval\*(C'\fR is terminated with the undefined value. This makes
1272\&\f(CW\*(C`die\*(C'\fR the way to raise an exception.
1273.Sp
1274Equivalent examples:
1275.Sp
1276.Vb 2
1277\& die "Can't cd to spool: $!\en" unless chdir '/usr/spool/news';
1278\& chdir '/usr/spool/news' or die "Can't cd to spool: $!\en"
1279.Ve
1280.Sp
1281If the last element of \s-1LIST\s0 does not end in a newline, the current
1282script line number and input line number (if any) are also printed,
1283and a newline is supplied. Note that the \*(L"input line number\*(R" (also
1284known as \*(L"chunk\*(R") is subject to whatever notion of \*(L"line\*(R" happens to
1285be currently in effect, and is also available as the special variable
1286\&\f(CW$.\fR. See \*(L"$/\*(R" in perlvar and \*(L"$.\*(R" in perlvar.
1287.Sp
1288Hint: sometimes appending \f(CW", stopped"\fR to your message will cause it
1289to make better sense when the string \f(CW"at foo line 123"\fR is appended.
1290Suppose you are running script \*(L"canasta\*(R".
1291.Sp
1292.Vb 2
1293\& die "/etc/games is no good";
1294\& die "/etc/games is no good, stopped";
1295.Ve
1296.Sp
1297produce, respectively
1298.Sp
1299.Vb 2
1300\& /etc/games is no good at canasta line 123.
1301\& /etc/games is no good, stopped at canasta line 123.
1302.Ve
1303.Sp
1304See also \fIexit()\fR, \fIwarn()\fR, and the Carp module.
1305.Sp
1306If \s-1LIST\s0 is empty and \f(CW$@\fR already contains a value (typically from a
1307previous eval) that value is reused after appending \f(CW"\et...propagated"\fR.
1308This is useful for propagating exceptions:
1309.Sp
1310.Vb 2
1311\& eval { ... };
1312\& die unless $@ =~ /Expected exception/;
1313.Ve
1314.Sp
1315If \s-1LIST\s0 is empty and \f(CW$@\fR contains an object reference that has a
1316\&\f(CW\*(C`PROPAGATE\*(C'\fR method, that method will be called with additional file
1317and line number parameters. The return value replaces the value in
1318\&\f(CW$@\fR. ie. as if \f(CW\*(C`<$@ = eval { $@\-\*(C'\fR\s-1PROPAGATE\s0(_\|_FILE_\|_, _\|_LINE_\|_) };>>
1319were called.
1320.Sp
1321If \f(CW$@\fR is empty then the string \f(CW"Died"\fR is used.
1322.Sp
1323\&\fIdie()\fR can also be called with a reference argument. If this happens to be
1324trapped within an \fIeval()\fR, $@ contains the reference. This behavior permits
1325a more elaborate exception handling implementation using objects that
1326maintain arbitrary state about the nature of the exception. Such a scheme
1327is sometimes preferable to matching particular string values of $@ using
1328regular expressions. Here's an example:
1329.Sp
1330.Vb 9
1331\& eval { ... ; die Some::Module::Exception->new( FOO => "bar" ) };
1332\& if ($@) {
1333\& if (ref($@) && UNIVERSAL::isa($@,"Some::Module::Exception")) {
1334\& # handle Some::Module::Exception
1335\& }
1336\& else {
1337\& # handle all other possible exceptions
1338\& }
1339\& }
1340.Ve
1341.Sp
1342Because perl will stringify uncaught exception messages before displaying
1343them, you may want to overload stringification operations on such custom
1344exception objects. See overload for details about that.
1345.Sp
1346You can arrange for a callback to be run just before the \f(CW\*(C`die\*(C'\fR
1347does its deed, by setting the \f(CW$SIG{_\|_DIE_\|_}\fR hook. The associated
1348handler will be called with the error text and can change the error
1349message, if it sees fit, by calling \f(CW\*(C`die\*(C'\fR again. See
1350\&\*(L"$SIG{expr}\*(R" in perlvar for details on setting \f(CW%SIG\fR entries, and
1351\&\*(L"eval \s-1BLOCK\s0\*(R" for some examples. Although this feature was meant
1352to be run only right before your program was to exit, this is not
1353currently the case\*(--the \f(CW$SIG{_\|_DIE_\|_}\fR hook is currently called
1354even inside \fIeval()\fRed blocks/strings! If one wants the hook to do
1355nothing in such situations, put
1356.Sp
1357.Vb 1
1358\& die @_ if $^S;
1359.Ve
1360.Sp
1361as the first line of the handler (see \*(L"$^S\*(R" in perlvar). Because
1362this promotes strange action at a distance, this counterintuitive
1363behavior may be fixed in a future release.
1364.IP "do \s-1BLOCK\s0" 8
1365.IX Item "do BLOCK"
1366Not really a function. Returns the value of the last command in the
1367sequence of commands indicated by \s-1BLOCK\s0. When modified by a loop
1368modifier, executes the \s-1BLOCK\s0 once before testing the loop condition.
1369(On other statements the loop modifiers test the conditional first.)
1370.Sp
1371\&\f(CW\*(C`do BLOCK\*(C'\fR does \fInot\fR count as a loop, so the loop control statements
1372\&\f(CW\*(C`next\*(C'\fR, \f(CW\*(C`last\*(C'\fR, or \f(CW\*(C`redo\*(C'\fR cannot be used to leave or restart the block.
1373See perlsyn for alternative strategies.
1374.IP "do \s-1SUBROUTINE\s0(\s-1LIST\s0)" 8
1375.IX Item "do SUBROUTINE(LIST)"
1376A deprecated form of subroutine call. See perlsub.
1377.IP "do \s-1EXPR\s0" 8
1378.IX Item "do EXPR"
1379Uses the value of \s-1EXPR\s0 as a filename and executes the contents of the
1380file as a Perl script. Its primary use is to include subroutines
1381from a Perl subroutine library.
1382.Sp
1383.Vb 1
1384\& do 'stat.pl';
1385.Ve
1386.Sp
1387is just like
1388.Sp
1389.Vb 1
1390\& eval `cat stat.pl`;
1391.Ve
1392.Sp
1393except that it's more efficient and concise, keeps track of the current
1394filename for error messages, searches the \f(CW@INC\fR libraries, and updates
1395\&\f(CW%INC\fR if the file is found. See \*(L"Predefined Names\*(R" in perlvar for these
1396variables. It also differs in that code evaluated with \f(CW\*(C`do FILENAME\*(C'\fR
1397cannot see lexicals in the enclosing scope; \f(CW\*(C`eval STRING\*(C'\fR does. It's the
1398same, however, in that it does reparse the file every time you call it,
1399so you probably don't want to do this inside a loop.
1400.Sp
1401If \f(CW\*(C`do\*(C'\fR cannot read the file, it returns undef and sets \f(CW$!\fR to the
1402error. If \f(CW\*(C`do\*(C'\fR can read the file but cannot compile it, it
1403returns undef and sets an error message in \f(CW$@\fR. If the file is
1404successfully compiled, \f(CW\*(C`do\*(C'\fR returns the value of the last expression
1405evaluated.
1406.Sp
1407Note that inclusion of library modules is better done with the
1408\&\f(CW\*(C`use\*(C'\fR and \f(CW\*(C`require\*(C'\fR operators, which also do automatic error checking
1409and raise an exception if there's a problem.
1410.Sp
1411You might like to use \f(CW\*(C`do\*(C'\fR to read in a program configuration
1412file. Manual error checking can be done this way:
1413.Sp
1414.Vb 10
1415\& # read in config files: system first, then user
1416\& for $file ("/share/prog/defaults.rc",
1417\& "$ENV{HOME}/.someprogrc")
1418\& {
1419\& unless ($return = do $file) {
1420\& warn "couldn't parse $file: $@" if $@;
1421\& warn "couldn't do $file: $!" unless defined $return;
1422\& warn "couldn't run $file" unless $return;
1423\& }
1424\& }
1425.Ve
1426.IP "dump \s-1LABEL\s0" 8
1427.IX Item "dump LABEL"
1428.PD 0
1429.IP "dump" 8
1430.IX Item "dump"
1431.PD
1432This function causes an immediate core dump. See also the \fB\-u\fR
1433command-line switch in perlrun, which does the same thing.
1434Primarily this is so that you can use the \fBundump\fR program (not
1435supplied) to turn your core dump into an executable binary after
1436having initialized all your variables at the beginning of the
1437program. When the new binary is executed it will begin by executing
1438a \f(CW\*(C`goto LABEL\*(C'\fR (with all the restrictions that \f(CW\*(C`goto\*(C'\fR suffers).
1439Think of it as a goto with an intervening core dump and reincarnation.
1440If \f(CW\*(C`LABEL\*(C'\fR is omitted, restarts the program from the top.
1441.Sp
1442\&\fB\s-1WARNING\s0\fR: Any files opened at the time of the dump will \fInot\fR
1443be open any more when the program is reincarnated, with possible
1444resulting confusion on the part of Perl.
1445.Sp
1446This function is now largely obsolete, partly because it's very
1447hard to convert a core file into an executable, and because the
1448real compiler backends for generating portable bytecode and compilable
1449C code have superseded it. That's why you should now invoke it as
1450\&\f(CW\*(C`CORE::dump()\*(C'\fR, if you don't want to be warned against a possible
1451typo.
1452.Sp
1453If you're looking to use dump to speed up your program, consider
1454generating bytecode or native C code as described in perlcc. If
1455you're just trying to accelerate a \s-1CGI\s0 script, consider using the
1456\&\f(CW\*(C`mod_perl\*(C'\fR extension to \fBApache\fR, or the \s-1CPAN\s0 module, CGI::Fast.
1457You might also consider autoloading or selfloading, which at least
1458make your program \fIappear\fR to run faster.
1459.IP "each \s-1HASH\s0" 8
1460.IX Item "each HASH"
1461When called in list context, returns a 2\-element list consisting of the
1462key and value for the next element of a hash, so that you can iterate over
1463it. When called in scalar context, returns only the key for the next
1464element in the hash.
1465.Sp
1466Entries are returned in an apparently random order. The actual random
1467order is subject to change in future versions of perl, but it is guaranteed
1468to be in the same order as either the \f(CW\*(C`keys\*(C'\fR or \f(CW\*(C`values\*(C'\fR function
1469would produce on the same (unmodified) hash.
1470.Sp
1471When the hash is entirely read, a null array is returned in list context
1472(which when assigned produces a false (\f(CW0\fR) value), and \f(CW\*(C`undef\*(C'\fR in
1473scalar context. The next call to \f(CW\*(C`each\*(C'\fR after that will start iterating
1474again. There is a single iterator for each hash, shared by all \f(CW\*(C`each\*(C'\fR,
1475\&\f(CW\*(C`keys\*(C'\fR, and \f(CW\*(C`values\*(C'\fR function calls in the program; it can be reset by
1476reading all the elements from the hash, or by evaluating \f(CW\*(C`keys HASH\*(C'\fR or
1477\&\f(CW\*(C`values HASH\*(C'\fR. If you add or delete elements of a hash while you're
1478iterating over it, you may get entries skipped or duplicated, so
1479don't. Exception: It is always safe to delete the item most recently
1480returned by \f(CW\*(C`each()\*(C'\fR, which means that the following code will work:
1481.Sp
1482.Vb 4
1483\& while (($key, $value) = each %hash) {
1484\& print $key, "\en";
1485\& delete $hash{$key}; # This is safe
1486\& }
1487.Ve
1488.Sp
1489The following prints out your environment like the \fIprintenv\fR\|(1) program,
1490only in a different order:
1491.Sp
1492.Vb 3
1493\& while (($key,$value) = each %ENV) {
1494\& print "$key=$value\en";
1495\& }
1496.Ve
1497.Sp
1498See also \f(CW\*(C`keys\*(C'\fR, \f(CW\*(C`values\*(C'\fR and \f(CW\*(C`sort\*(C'\fR.
1499.IP "eof \s-1FILEHANDLE\s0" 8
1500.IX Item "eof FILEHANDLE"
1501.PD 0
1502.IP "eof ()" 8
1503.IX Item "eof ()"
1504.IP "eof" 8
1505.IX Item "eof"
1506.PD
1507Returns 1 if the next read on \s-1FILEHANDLE\s0 will return end of file, or if
1508\&\s-1FILEHANDLE\s0 is not open. \s-1FILEHANDLE\s0 may be an expression whose value
1509gives the real filehandle. (Note that this function actually
1510reads a character and then \f(CW\*(C`ungetc\*(C'\fRs it, so isn't very useful in an
1511interactive context.) Do not read from a terminal file (or call
1512\&\f(CW\*(C`eof(FILEHANDLE)\*(C'\fR on it) after end-of-file is reached. File types such
1513as terminals may lose the end-of-file condition if you do.
1514.Sp
1515An \f(CW\*(C`eof\*(C'\fR without an argument uses the last file read. Using \f(CW\*(C`eof()\*(C'\fR
1516with empty parentheses is very different. It refers to the pseudo file
1517formed from the files listed on the command line and accessed via the
1518\&\f(CW\*(C`<>\*(C'\fR operator. Since \f(CW\*(C`<>\*(C'\fR isn't explicitly opened,
1519as a normal filehandle is, an \f(CW\*(C`eof()\*(C'\fR before \f(CW\*(C`<>\*(C'\fR has been
1520used will cause \f(CW@ARGV\fR to be examined to determine if input is
1521available. Similarly, an \f(CW\*(C`eof()\*(C'\fR after \f(CW\*(C`<>\*(C'\fR has returned
1522end-of-file will assume you are processing another \f(CW@ARGV\fR list,
1523and if you haven't set \f(CW@ARGV\fR, will read input from \f(CW\*(C`STDIN\*(C'\fR;
1524see \*(L"I/O Operators\*(R" in perlop.
1525.Sp
1526In a \f(CW\*(C`while (<>)\*(C'\fR loop, \f(CW\*(C`eof\*(C'\fR or \f(CW\*(C`eof(ARGV)\*(C'\fR can be used to
1527detect the end of each file, \f(CW\*(C`eof()\*(C'\fR will only detect the end of the
1528last file. Examples:
1529.Sp
1530.Vb 7
1531\& # reset line numbering on each input file
1532\& while (<>) {
1533\& next if /^\es*#/; # skip comments
1534\& print "$.\et$_";
1535\& } continue {
1536\& close ARGV if eof; # Not eof()!
1537\& }
1538.Ve
1539.Sp
1540.Vb 9
1541\& # insert dashes just before last line of last file
1542\& while (<>) {
1543\& if (eof()) { # check for end of current file
1544\& print "--------------\en";
1545\& close(ARGV); # close or last; is needed if we
1546\& # are reading from the terminal
1547\& }
1548\& print;
1549\& }
1550.Ve
1551.Sp
1552Practical hint: you almost never need to use \f(CW\*(C`eof\*(C'\fR in Perl, because the
1553input operators typically return \f(CW\*(C`undef\*(C'\fR when they run out of data, or if
1554there was an error.
1555.IP "eval \s-1EXPR\s0" 8
1556.IX Item "eval EXPR"
1557.PD 0
1558.IP "eval \s-1BLOCK\s0" 8
1559.IX Item "eval BLOCK"
1560.PD
1561In the first form, the return value of \s-1EXPR\s0 is parsed and executed as if it
1562were a little Perl program. The value of the expression (which is itself
1563determined within scalar context) is first parsed, and if there weren't any
1564errors, executed in the lexical context of the current Perl program, so
1565that any variable settings or subroutine and format definitions remain
1566afterwards. Note that the value is parsed every time the eval executes.
1567If \s-1EXPR\s0 is omitted, evaluates \f(CW$_\fR. This form is typically used to
1568delay parsing and subsequent execution of the text of \s-1EXPR\s0 until run time.
1569.Sp
1570In the second form, the code within the \s-1BLOCK\s0 is parsed only once\*(--at the
1571same time the code surrounding the eval itself was parsed\*(--and executed
1572within the context of the current Perl program. This form is typically
1573used to trap exceptions more efficiently than the first (see below), while
1574also providing the benefit of checking the code within \s-1BLOCK\s0 at compile
1575time.
1576.Sp
1577The final semicolon, if any, may be omitted from the value of \s-1EXPR\s0 or within
1578the \s-1BLOCK\s0.
1579.Sp
1580In both forms, the value returned is the value of the last expression
1581evaluated inside the mini\-program; a return statement may be also used, just
1582as with subroutines. The expression providing the return value is evaluated
1583in void, scalar, or list context, depending on the context of the eval itself.
1584See \*(L"wantarray\*(R" for more on how the evaluation context can be determined.
1585.Sp
1586If there is a syntax error or runtime error, or a \f(CW\*(C`die\*(C'\fR statement is
1587executed, an undefined value is returned by \f(CW\*(C`eval\*(C'\fR, and \f(CW$@\fR is set to the
1588error message. If there was no error, \f(CW$@\fR is guaranteed to be a null
1589string. Beware that using \f(CW\*(C`eval\*(C'\fR neither silences perl from printing
1590warnings to \s-1STDERR\s0, nor does it stuff the text of warning messages into \f(CW$@\fR.
1591To do either of those, you have to use the \f(CW$SIG{_\|_WARN_\|_}\fR facility, or
1592turn off warnings inside the \s-1BLOCK\s0 or \s-1EXPR\s0 using \f(CW\*(C`no\ warnings\ 'all'\*(C'\fR.
1593See \*(L"warn\*(R", perlvar, warnings and perllexwarn.
1594.Sp
1595Note that, because \f(CW\*(C`eval\*(C'\fR traps otherwise-fatal errors, it is useful for
1596determining whether a particular feature (such as \f(CW\*(C`socket\*(C'\fR or \f(CW\*(C`symlink\*(C'\fR)
1597is implemented. It is also Perl's exception trapping mechanism, where
1598the die operator is used to raise exceptions.
1599.Sp
1600If the code to be executed doesn't vary, you may use the eval-BLOCK
1601form to trap run-time errors without incurring the penalty of
1602recompiling each time. The error, if any, is still returned in \f(CW$@\fR.
1603Examples:
1604.Sp
1605.Vb 2
1606\& # make divide-by-zero nonfatal
1607\& eval { $answer = $a / $b; }; warn $@ if $@;
1608.Ve
1609.Sp
1610.Vb 2
1611\& # same thing, but less efficient
1612\& eval '$answer = $a / $b'; warn $@ if $@;
1613.Ve
1614.Sp
1615.Vb 2
1616\& # a compile-time error
1617\& eval { $answer = }; # WRONG
1618.Ve
1619.Sp
1620.Vb 2
1621\& # a run-time error
1622\& eval '$answer ='; # sets $@
1623.Ve
1624.Sp
1625Due to the current arguably broken state of \f(CW\*(C`_\|_DIE_\|_\*(C'\fR hooks, when using
1626the \f(CW\*(C`eval{}\*(C'\fR form as an exception trap in libraries, you may wish not
1627to trigger any \f(CW\*(C`_\|_DIE_\|_\*(C'\fR hooks that user code may have installed.
1628You can use the \f(CW\*(C`local $SIG{_\|_DIE_\|_}\*(C'\fR construct for this purpose,
1629as shown in this example:
1630.Sp
1631.Vb 3
1632\& # a very private exception trap for divide-by-zero
1633\& eval { local $SIG{'__DIE__'}; $answer = $a / $b; };
1634\& warn $@ if $@;
1635.Ve
1636.Sp
1637This is especially significant, given that \f(CW\*(C`_\|_DIE_\|_\*(C'\fR hooks can call
1638\&\f(CW\*(C`die\*(C'\fR again, which has the effect of changing their error messages:
1639.Sp
1640.Vb 7
1641\& # __DIE__ hooks may modify error messages
1642\& {
1643\& local $SIG{'__DIE__'} =
1644\& sub { (my $x = $_[0]) =~ s/foo/bar/g; die $x };
1645\& eval { die "foo lives here" };
1646\& print $@ if $@; # prints "bar lives here"
1647\& }
1648.Ve
1649.Sp
1650Because this promotes action at a distance, this counterintuitive behavior
1651may be fixed in a future release.
1652.Sp
1653With an \f(CW\*(C`eval\*(C'\fR, you should be especially careful to remember what's
1654being looked at when:
1655.Sp
1656.Vb 2
1657\& eval $x; # CASE 1
1658\& eval "$x"; # CASE 2
1659.Ve
1660.Sp
1661.Vb 2
1662\& eval '$x'; # CASE 3
1663\& eval { $x }; # CASE 4
1664.Ve
1665.Sp
1666.Vb 2
1667\& eval "\e$$x++"; # CASE 5
1668\& $$x++; # CASE 6
1669.Ve
1670.Sp
1671Cases 1 and 2 above behave identically: they run the code contained in
1672the variable \f(CW$x\fR. (Although case 2 has misleading double quotes making
1673the reader wonder what else might be happening (nothing is).) Cases 3
1674and 4 likewise behave in the same way: they run the code \f(CW'$x'\fR, which
1675does nothing but return the value of \f(CW$x\fR. (Case 4 is preferred for
1676purely visual reasons, but it also has the advantage of compiling at
1677compile-time instead of at run\-time.) Case 5 is a place where
1678normally you \fIwould\fR like to use double quotes, except that in this
1679particular situation, you can just use symbolic references instead, as
1680in case 6.
1681.Sp
1682\&\f(CW\*(C`eval BLOCK\*(C'\fR does \fInot\fR count as a loop, so the loop control statements
1683\&\f(CW\*(C`next\*(C'\fR, \f(CW\*(C`last\*(C'\fR, or \f(CW\*(C`redo\*(C'\fR cannot be used to leave or restart the block.
1684.IP "exec \s-1LIST\s0" 8
1685.IX Item "exec LIST"
1686.PD 0
1687.IP "exec \s-1PROGRAM\s0 \s-1LIST\s0" 8
1688.IX Item "exec PROGRAM LIST"
1689.PD
1690The \f(CW\*(C`exec\*(C'\fR function executes a system command \fIand never returns\fR\-\-
1691use \f(CW\*(C`system\*(C'\fR instead of \f(CW\*(C`exec\*(C'\fR if you want it to return. It fails and
1692returns false only if the command does not exist \fIand\fR it is executed
1693directly instead of via your system's command shell (see below).
1694.Sp
1695Since it's a common mistake to use \f(CW\*(C`exec\*(C'\fR instead of \f(CW\*(C`system\*(C'\fR, Perl
1696warns you if there is a following statement which isn't \f(CW\*(C`die\*(C'\fR, \f(CW\*(C`warn\*(C'\fR,
1697or \f(CW\*(C`exit\*(C'\fR (if \f(CW\*(C`\-w\*(C'\fR is set \- but you always do that). If you
1698\&\fIreally\fR want to follow an \f(CW\*(C`exec\*(C'\fR with some other statement, you
1699can use one of these styles to avoid the warning:
1700.Sp
1701.Vb 2
1702\& exec ('foo') or print STDERR "couldn't exec foo: $!";
1703\& { exec ('foo') }; print STDERR "couldn't exec foo: $!";
1704.Ve
1705.Sp
1706If there is more than one argument in \s-1LIST\s0, or if \s-1LIST\s0 is an array
1707with more than one value, calls \fIexecvp\fR\|(3) with the arguments in \s-1LIST\s0.
1708If there is only one scalar argument or an array with one element in it,
1709the argument is checked for shell metacharacters, and if there are any,
1710the entire argument is passed to the system's command shell for parsing
1711(this is \f(CW\*(C`/bin/sh \-c\*(C'\fR on Unix platforms, but varies on other platforms).
1712If there are no shell metacharacters in the argument, it is split into
1713words and passed directly to \f(CW\*(C`execvp\*(C'\fR, which is more efficient.
1714Examples:
1715.Sp
1716.Vb 2
1717\& exec '/bin/echo', 'Your arguments are: ', @ARGV;
1718\& exec "sort $outfile | uniq";
1719.Ve
1720.Sp
1721If you don't really want to execute the first argument, but want to lie
1722to the program you are executing about its own name, you can specify
1723the program you actually want to run as an \*(L"indirect object\*(R" (without a
1724comma) in front of the \s-1LIST\s0. (This always forces interpretation of the
1725\&\s-1LIST\s0 as a multivalued list, even if there is only a single scalar in
1726the list.) Example:
1727.Sp
1728.Vb 2
1729\& $shell = '/bin/csh';
1730\& exec $shell '-sh'; # pretend it's a login shell
1731.Ve
1732.Sp
1733or, more directly,
1734.Sp
1735.Vb 1
1736\& exec {'/bin/csh'} '-sh'; # pretend it's a login shell
1737.Ve
1738.Sp
1739When the arguments get executed via the system shell, results will
1740be subject to its quirks and capabilities. See \*(L"`STRING`\*(R" in perlop
1741for details.
1742.Sp
1743Using an indirect object with \f(CW\*(C`exec\*(C'\fR or \f(CW\*(C`system\*(C'\fR is also more
1744secure. This usage (which also works fine with \fIsystem()\fR) forces
1745interpretation of the arguments as a multivalued list, even if the
1746list had just one argument. That way you're safe from the shell
1747expanding wildcards or splitting up words with whitespace in them.
1748.Sp
1749.Vb 1
1750\& @args = ( "echo surprise" );
1751.Ve
1752.Sp
1753.Vb 3
1754\& exec @args; # subject to shell escapes
1755\& # if @args == 1
1756\& exec { $args[0] } @args; # safe even with one-arg list
1757.Ve
1758.Sp
1759The first version, the one without the indirect object, ran the \fIecho\fR
1760program, passing it \f(CW"surprise"\fR an argument. The second version
1761didn't\*(--it tried to run a program literally called \fI\*(L"echo surprise\*(R"\fR,
1762didn't find it, and set \f(CW$?\fR to a non-zero value indicating failure.
1763.Sp
1764Beginning with v5.6.0, Perl will attempt to flush all files opened for
1765output before the exec, but this may not be supported on some platforms
1766(see perlport). To be safe, you may need to set \f(CW$|\fR ($AUTOFLUSH
1767in English) or call the \f(CW\*(C`autoflush()\*(C'\fR method of \f(CW\*(C`IO::Handle\*(C'\fR on any
1768open handles in order to avoid lost output.
1769.Sp
1770Note that \f(CW\*(C`exec\*(C'\fR will not call your \f(CW\*(C`END\*(C'\fR blocks, nor will it call
1771any \f(CW\*(C`DESTROY\*(C'\fR methods in your objects.
1772.IP "exists \s-1EXPR\s0" 8
1773.IX Item "exists EXPR"
1774Given an expression that specifies a hash element or array element,
1775returns true if the specified element in the hash or array has ever
1776been initialized, even if the corresponding value is undefined. The
1777element is not autovivified if it doesn't exist.
1778.Sp
1779.Vb 3
1780\& print "Exists\en" if exists $hash{$key};
1781\& print "Defined\en" if defined $hash{$key};
1782\& print "True\en" if $hash{$key};
1783.Ve
1784.Sp
1785.Vb 3
1786\& print "Exists\en" if exists $array[$index];
1787\& print "Defined\en" if defined $array[$index];
1788\& print "True\en" if $array[$index];
1789.Ve
1790.Sp
1791A hash or array element can be true only if it's defined, and defined if
1792it exists, but the reverse doesn't necessarily hold true.
1793.Sp
1794Given an expression that specifies the name of a subroutine,
1795returns true if the specified subroutine has ever been declared, even
1796if it is undefined. Mentioning a subroutine name for exists or defined
1797does not count as declaring it. Note that a subroutine which does not
1798exist may still be callable: its package may have an \f(CW\*(C`AUTOLOAD\*(C'\fR
1799method that makes it spring into existence the first time that it is
1800called \*(-- see perlsub.
1801.Sp
1802.Vb 2
1803\& print "Exists\en" if exists &subroutine;
1804\& print "Defined\en" if defined &subroutine;
1805.Ve
1806.Sp
1807Note that the \s-1EXPR\s0 can be arbitrarily complicated as long as the final
1808operation is a hash or array key lookup or subroutine name:
1809.Sp
1810.Vb 2
1811\& if (exists $ref->{A}->{B}->{$key}) { }
1812\& if (exists $hash{A}{B}{$key}) { }
1813.Ve
1814.Sp
1815.Vb 2
1816\& if (exists $ref->{A}->{B}->[$ix]) { }
1817\& if (exists $hash{A}{B}[$ix]) { }
1818.Ve
1819.Sp
1820.Vb 1
1821\& if (exists &{$ref->{A}{B}{$key}}) { }
1822.Ve
1823.Sp
1824Although the deepest nested array or hash will not spring into existence
1825just because its existence was tested, any intervening ones will.
1826Thus \f(CW\*(C`$ref\->{"A"}\*(C'\fR and \f(CW\*(C`$ref\->{"A"}\->{"B"}\*(C'\fR will spring
1827into existence due to the existence test for the \f(CW$key\fR element above.
1828This happens anywhere the arrow operator is used, including even:
1829.Sp
1830.Vb 3
1831\& undef $ref;
1832\& if (exists $ref->{"Some key"}) { }
1833\& print $ref; # prints HASH(0x80d3d5c)
1834.Ve
1835.Sp
1836This surprising autovivification in what does not at first\*(--or even
1837second\*(--glance appear to be an lvalue context may be fixed in a future
1838release.
1839.Sp
1840See \*(L"Pseudo\-hashes: Using an array as a hash\*(R" in perlref for specifics
1841on how \fIexists()\fR acts when used on a pseudo\-hash.
1842.Sp
1843Use of a subroutine call, rather than a subroutine name, as an argument
1844to \fIexists()\fR is an error.
1845.Sp
1846.Vb 2
1847\& exists &sub; # OK
1848\& exists &sub(); # Error
1849.Ve
1850.IP "exit \s-1EXPR\s0" 8
1851.IX Item "exit EXPR"
1852Evaluates \s-1EXPR\s0 and exits immediately with that value. Example:
1853.Sp
1854.Vb 2
1855\& $ans = <STDIN>;
1856\& exit 0 if $ans =~ /^[Xx]/;
1857.Ve
1858.Sp
1859See also \f(CW\*(C`die\*(C'\fR. If \s-1EXPR\s0 is omitted, exits with \f(CW0\fR status. The only
1860universally recognized values for \s-1EXPR\s0 are \f(CW0\fR for success and \f(CW1\fR
1861for error; other values are subject to interpretation depending on the
1862environment in which the Perl program is running. For example, exiting
186369 (\s-1EX_UNAVAILABLE\s0) from a \fIsendmail\fR incoming-mail filter will cause
1864the mailer to return the item undelivered, but that's not true everywhere.
1865.Sp
1866Don't use \f(CW\*(C`exit\*(C'\fR to abort a subroutine if there's any chance that
1867someone might want to trap whatever error happened. Use \f(CW\*(C`die\*(C'\fR instead,
1868which can be trapped by an \f(CW\*(C`eval\*(C'\fR.
1869.Sp
1870The \fIexit()\fR function does not always exit immediately. It calls any
1871defined \f(CW\*(C`END\*(C'\fR routines first, but these \f(CW\*(C`END\*(C'\fR routines may not
1872themselves abort the exit. Likewise any object destructors that need to
1873be called are called before the real exit. If this is a problem, you
1874can call \f(CW\*(C`POSIX:_exit($status)\*(C'\fR to avoid \s-1END\s0 and destructor processing.
1875See perlmod for details.
1876.IP "exp \s-1EXPR\s0" 8
1877.IX Item "exp EXPR"
1878.PD 0
1879.IP "exp" 8
1880.IX Item "exp"
1881.PD
1882Returns \fIe\fR (the natural logarithm base) to the power of \s-1EXPR\s0.
1883If \s-1EXPR\s0 is omitted, gives \f(CW\*(C`exp($_)\*(C'\fR.
1884.IP "fcntl \s-1FILEHANDLE\s0,FUNCTION,SCALAR" 8
1885.IX Item "fcntl FILEHANDLE,FUNCTION,SCALAR"
1886Implements the \fIfcntl\fR\|(2) function. You'll probably have to say
1887.Sp
1888.Vb 1
1889\& use Fcntl;
1890.Ve
1891.Sp
1892first to get the correct constant definitions. Argument processing and
1893value return works just like \f(CW\*(C`ioctl\*(C'\fR below.
1894For example:
1895.Sp
1896.Vb 3
1897\& use Fcntl;
1898\& fcntl($filehandle, F_GETFL, $packed_return_buffer)
1899\& or die "can't fcntl F_GETFL: $!";
1900.Ve
1901.Sp
1902You don't have to check for \f(CW\*(C`defined\*(C'\fR on the return from \f(CW\*(C`fnctl\*(C'\fR.
1903Like \f(CW\*(C`ioctl\*(C'\fR, it maps a \f(CW0\fR return from the system call into
1904\&\f(CW"0 but true"\fR in Perl. This string is true in boolean context and \f(CW0\fR
1905in numeric context. It is also exempt from the normal \fB\-w\fR warnings
1906on improper numeric conversions.
1907.Sp
1908Note that \f(CW\*(C`fcntl\*(C'\fR will produce a fatal error if used on a machine that
1909doesn't implement \fIfcntl\fR\|(2). See the Fcntl module or your \fIfcntl\fR\|(2)
1910manpage to learn what functions are available on your system.
1911.IP "fileno \s-1FILEHANDLE\s0" 8
1912.IX Item "fileno FILEHANDLE"
1913Returns the file descriptor for a filehandle, or undefined if the
1914filehandle is not open. This is mainly useful for constructing
1915bitmaps for \f(CW\*(C`select\*(C'\fR and low-level \s-1POSIX\s0 tty-handling operations.
1916If \s-1FILEHANDLE\s0 is an expression, the value is taken as an indirect
1917filehandle, generally its name.
1918.Sp
1919You can use this to find out whether two handles refer to the
1920same underlying descriptor:
1921.Sp
1922.Vb 3
1923\& if (fileno(THIS) == fileno(THAT)) {
1924\& print "THIS and THAT are dups\en";
1925\& }
1926.Ve
1927.Sp
1928(Filehandles connected to memory objects via new features of \f(CW\*(C`open\*(C'\fR may
1929return undefined even though they are open.)
1930.IP "flock \s-1FILEHANDLE\s0,OPERATION" 8
1931.IX Item "flock FILEHANDLE,OPERATION"
1932Calls \fIflock\fR\|(2), or an emulation of it, on \s-1FILEHANDLE\s0. Returns true
1933for success, false on failure. Produces a fatal error if used on a
1934machine that doesn't implement \fIflock\fR\|(2), \fIfcntl\fR\|(2) locking, or \fIlockf\fR\|(3).
1935\&\f(CW\*(C`flock\*(C'\fR is Perl's portable file locking interface, although it locks
1936only entire files, not records.
1937.Sp
1938Two potentially non-obvious but traditional \f(CW\*(C`flock\*(C'\fR semantics are
1939that it waits indefinitely until the lock is granted, and that its locks
1940\&\fBmerely advisory\fR. Such discretionary locks are more flexible, but offer
1941fewer guarantees. This means that files locked with \f(CW\*(C`flock\*(C'\fR may be
1942modified by programs that do not also use \f(CW\*(C`flock\*(C'\fR. See perlport,
1943your port's specific documentation, or your system-specific local manpages
1944for details. It's best to assume traditional behavior if you're writing
1945portable programs. (But if you're not, you should as always feel perfectly
1946free to write for your own system's idiosyncrasies (sometimes called
1947\&\*(L"features\*(R"). Slavish adherence to portability concerns shouldn't get
1948in the way of your getting your job done.)
1949.Sp
1950\&\s-1OPERATION\s0 is one of \s-1LOCK_SH\s0, \s-1LOCK_EX\s0, or \s-1LOCK_UN\s0, possibly combined with
1951\&\s-1LOCK_NB\s0. These constants are traditionally valued 1, 2, 8 and 4, but
1952you can use the symbolic names if you import them from the Fcntl module,
1953either individually, or as a group using the ':flock' tag. \s-1LOCK_SH\s0
1954requests a shared lock, \s-1LOCK_EX\s0 requests an exclusive lock, and \s-1LOCK_UN\s0
1955releases a previously requested lock. If \s-1LOCK_NB\s0 is bitwise\-or'ed with
1956\&\s-1LOCK_SH\s0 or \s-1LOCK_EX\s0 then \f(CW\*(C`flock\*(C'\fR will return immediately rather than blocking
1957waiting for the lock (check the return status to see if you got it).
1958.Sp
1959To avoid the possibility of miscoordination, Perl now flushes \s-1FILEHANDLE\s0
1960before locking or unlocking it.
1961.Sp
1962Note that the emulation built with \fIlockf\fR\|(3) doesn't provide shared
1963locks, and it requires that \s-1FILEHANDLE\s0 be open with write intent. These
1964are the semantics that \fIlockf\fR\|(3) implements. Most if not all systems
1965implement \fIlockf\fR\|(3) in terms of \fIfcntl\fR\|(2) locking, though, so the
1966differing semantics shouldn't bite too many people.
1967.Sp
1968Note that the \fIfcntl\fR\|(2) emulation of \fIflock\fR\|(3) requires that \s-1FILEHANDLE\s0
1969be open with read intent to use \s-1LOCK_SH\s0 and requires that it be open
1970with write intent to use \s-1LOCK_EX\s0.
1971.Sp
1972Note also that some versions of \f(CW\*(C`flock\*(C'\fR cannot lock things over the
1973network; you would need to use the more system-specific \f(CW\*(C`fcntl\*(C'\fR for
1974that. If you like you can force Perl to ignore your system's \fIflock\fR\|(2)
1975function, and so provide its own \fIfcntl\fR\|(2)\-based emulation, by passing
1976the switch \f(CW\*(C`\-Ud_flock\*(C'\fR to the \fIConfigure\fR program when you configure
1977perl.
1978.Sp
1979Here's a mailbox appender for \s-1BSD\s0 systems.
1980.Sp
1981.Vb 1
1982\& use Fcntl ':flock'; # import LOCK_* constants
1983.Ve
1984.Sp
1985.Vb 6
1986\& sub lock {
1987\& flock(MBOX,LOCK_EX);
1988\& # and, in case someone appended
1989\& # while we were waiting...
1990\& seek(MBOX, 0, 2);
1991\& }
1992.Ve
1993.Sp
1994.Vb 3
1995\& sub unlock {
1996\& flock(MBOX,LOCK_UN);
1997\& }
1998.Ve
1999.Sp
2000.Vb 2
2001\& open(MBOX, ">>/usr/spool/mail/$ENV{'USER'}")
2002\& or die "Can't open mailbox: $!";
2003.Ve
2004.Sp
2005.Vb 3
2006\& lock();
2007\& print MBOX $msg,"\en\en";
2008\& unlock();
2009.Ve
2010.Sp
2011On systems that support a real \fIflock()\fR, locks are inherited across \fIfork()\fR
2012calls, whereas those that must resort to the more capricious \fIfcntl()\fR
2013function lose the locks, making it harder to write servers.
2014.Sp
2015See also DB_File for other \fIflock()\fR examples.
2016.IP "fork" 8
2017.IX Item "fork"
2018Does a \fIfork\fR\|(2) system call to create a new process running the
2019same program at the same point. It returns the child pid to the
2020parent process, \f(CW0\fR to the child process, or \f(CW\*(C`undef\*(C'\fR if the fork is
2021unsuccessful. File descriptors (and sometimes locks on those descriptors)
2022are shared, while everything else is copied. On most systems supporting
2023\&\fIfork()\fR, great care has gone into making it extremely efficient (for
2024example, using copy-on-write technology on data pages), making it the
2025dominant paradigm for multitasking over the last few decades.
2026.Sp
2027Beginning with v5.6.0, Perl will attempt to flush all files opened for
2028output before forking the child process, but this may not be supported
2029on some platforms (see perlport). To be safe, you may need to set
2030\&\f(CW$|\fR ($AUTOFLUSH in English) or call the \f(CW\*(C`autoflush()\*(C'\fR method of
2031\&\f(CW\*(C`IO::Handle\*(C'\fR on any open handles in order to avoid duplicate output.
2032.Sp
2033If you \f(CW\*(C`fork\*(C'\fR without ever waiting on your children, you will
2034accumulate zombies. On some systems, you can avoid this by setting
2035\&\f(CW$SIG{CHLD}\fR to \f(CW"IGNORE"\fR. See also perlipc for more examples of
2036forking and reaping moribund children.
2037.Sp
2038Note that if your forked child inherits system file descriptors like
2039\&\s-1STDIN\s0 and \s-1STDOUT\s0 that are actually connected by a pipe or socket, even
2040if you exit, then the remote server (such as, say, a \s-1CGI\s0 script or a
2041backgrounded job launched from a remote shell) won't think you're done.
2042You should reopen those to \fI/dev/null\fR if it's any issue.
2043.IP "format" 8
2044.IX Item "format"
2045Declare a picture format for use by the \f(CW\*(C`write\*(C'\fR function. For
2046example:
2047.Sp
2048.Vb 4
2049\& format Something =
2050\& Test: @<<<<<<<< @||||| @>>>>>
2051\& $str, $%, '$' . int($num)
2052\& .
2053.Ve
2054.Sp
2055.Vb 4
2056\& $str = "widget";
2057\& $num = $cost/$quantity;
2058\& $~ = 'Something';
2059\& write;
2060.Ve
2061.Sp
2062See perlform for many details and examples.
2063.IP "formline \s-1PICTURE\s0,LIST" 8
2064.IX Item "formline PICTURE,LIST"
2065This is an internal function used by \f(CW\*(C`format\*(C'\fRs, though you may call it,
2066too. It formats (see perlform) a list of values according to the
2067contents of \s-1PICTURE\s0, placing the output into the format output
2068accumulator, \f(CW$^A\fR (or \f(CW$ACCUMULATOR\fR in English).
2069Eventually, when a \f(CW\*(C`write\*(C'\fR is done, the contents of
2070\&\f(CW$^A\fR are written to some filehandle, but you could also read \f(CW$^A\fR
2071yourself and then set \f(CW$^A\fR back to \f(CW""\fR. Note that a format typically
2072does one \f(CW\*(C`formline\*(C'\fR per line of form, but the \f(CW\*(C`formline\*(C'\fR function itself
2073doesn't care how many newlines are embedded in the \s-1PICTURE\s0. This means
2074that the \f(CW\*(C`~\*(C'\fR and \f(CW\*(C`~~\*(C'\fR tokens will treat the entire \s-1PICTURE\s0 as a single line.
2075You may therefore need to use multiple formlines to implement a single
2076record format, just like the format compiler.
2077.Sp
2078Be careful if you put double quotes around the picture, because an \f(CW\*(C`@\*(C'\fR
2079character may be taken to mean the beginning of an array name.
2080\&\f(CW\*(C`formline\*(C'\fR always returns true. See perlform for other examples.
2081.IP "getc \s-1FILEHANDLE\s0" 8
2082.IX Item "getc FILEHANDLE"
2083.PD 0
2084.IP "getc" 8
2085.IX Item "getc"
2086.PD
2087Returns the next character from the input file attached to \s-1FILEHANDLE\s0,
2088or the undefined value at end of file, or if there was an error.
2089If \s-1FILEHANDLE\s0 is omitted, reads from \s-1STDIN\s0. This is not particularly
2090efficient. However, it cannot be used by itself to fetch single
2091characters without waiting for the user to hit enter. For that, try
2092something more like:
2093.Sp
2094.Vb 6
2095\& if ($BSD_STYLE) {
2096\& system "stty cbreak </dev/tty >/dev/tty 2>&1";
2097\& }
2098\& else {
2099\& system "stty", '-icanon', 'eol', "\e001";
2100\& }
2101.Ve
2102.Sp
2103.Vb 1
2104\& $key = getc(STDIN);
2105.Ve
2106.Sp
2107.Vb 7
2108\& if ($BSD_STYLE) {
2109\& system "stty -cbreak </dev/tty >/dev/tty 2>&1";
2110\& }
2111\& else {
2112\& system "stty", 'icanon', 'eol', '^@'; # ASCII null
2113\& }
2114\& print "\en";
2115.Ve
2116.Sp
2117Determination of whether \f(CW$BSD_STYLE\fR should be set
2118is left as an exercise to the reader.
2119.Sp
2120The \f(CW\*(C`POSIX::getattr\*(C'\fR function can do this more portably on
2121systems purporting \s-1POSIX\s0 compliance. See also the \f(CW\*(C`Term::ReadKey\*(C'\fR
2122module from your nearest \s-1CPAN\s0 site; details on \s-1CPAN\s0 can be found on
2123\&\*(L"\s-1CPAN\s0\*(R" in perlmodlib.
2124.IP "getlogin" 8
2125.IX Item "getlogin"
2126Implements the C library function of the same name, which on most
2127systems returns the current login from \fI/etc/utmp\fR, if any. If null,
2128use \f(CW\*(C`getpwuid\*(C'\fR.
2129.Sp
2130.Vb 1
2131\& $login = getlogin || getpwuid($<) || "Kilroy";
2132.Ve
2133.Sp
2134Do not consider \f(CW\*(C`getlogin\*(C'\fR for authentication: it is not as
2135secure as \f(CW\*(C`getpwuid\*(C'\fR.
2136.IP "getpeername \s-1SOCKET\s0" 8
2137.IX Item "getpeername SOCKET"
2138Returns the packed sockaddr address of other end of the \s-1SOCKET\s0 connection.
2139.Sp
2140.Vb 5
2141\& use Socket;
2142\& $hersockaddr = getpeername(SOCK);
2143\& ($port, $iaddr) = sockaddr_in($hersockaddr);
2144\& $herhostname = gethostbyaddr($iaddr, AF_INET);
2145\& $herstraddr = inet_ntoa($iaddr);
2146.Ve
2147.IP "getpgrp \s-1PID\s0" 8
2148.IX Item "getpgrp PID"
2149Returns the current process group for the specified \s-1PID\s0. Use
2150a \s-1PID\s0 of \f(CW0\fR to get the current process group for the
2151current process. Will raise an exception if used on a machine that
2152doesn't implement \fIgetpgrp\fR\|(2). If \s-1PID\s0 is omitted, returns process
2153group of current process. Note that the \s-1POSIX\s0 version of \f(CW\*(C`getpgrp\*(C'\fR
2154does not accept a \s-1PID\s0 argument, so only \f(CW\*(C`PID==0\*(C'\fR is truly portable.
2155.IP "getppid" 8
2156.IX Item "getppid"
2157Returns the process id of the parent process.
2158.IP "getpriority \s-1WHICH\s0,WHO" 8
2159.IX Item "getpriority WHICH,WHO"
2160Returns the current priority for a process, a process group, or a user.
2161(See \fIgetpriority\fR\|(2).) Will raise a fatal exception if used on a
2162machine that doesn't implement \fIgetpriority\fR\|(2).
2163.IP "getpwnam \s-1NAME\s0" 8
2164.IX Item "getpwnam NAME"
2165.PD 0
2166.IP "getgrnam \s-1NAME\s0" 8
2167.IX Item "getgrnam NAME"
2168.IP "gethostbyname \s-1NAME\s0" 8
2169.IX Item "gethostbyname NAME"
2170.IP "getnetbyname \s-1NAME\s0" 8
2171.IX Item "getnetbyname NAME"
2172.IP "getprotobyname \s-1NAME\s0" 8
2173.IX Item "getprotobyname NAME"
2174.IP "getpwuid \s-1UID\s0" 8
2175.IX Item "getpwuid UID"
2176.IP "getgrgid \s-1GID\s0" 8
2177.IX Item "getgrgid GID"
2178.IP "getservbyname \s-1NAME\s0,PROTO" 8
2179.IX Item "getservbyname NAME,PROTO"
2180.IP "gethostbyaddr \s-1ADDR\s0,ADDRTYPE" 8
2181.IX Item "gethostbyaddr ADDR,ADDRTYPE"
2182.IP "getnetbyaddr \s-1ADDR\s0,ADDRTYPE" 8
2183.IX Item "getnetbyaddr ADDR,ADDRTYPE"
2184.IP "getprotobynumber \s-1NUMBER\s0" 8
2185.IX Item "getprotobynumber NUMBER"
2186.IP "getservbyport \s-1PORT\s0,PROTO" 8
2187.IX Item "getservbyport PORT,PROTO"
2188.IP "getpwent" 8
2189.IX Item "getpwent"
2190.IP "getgrent" 8
2191.IX Item "getgrent"
2192.IP "gethostent" 8
2193.IX Item "gethostent"
2194.IP "getnetent" 8
2195.IX Item "getnetent"
2196.IP "getprotoent" 8
2197.IX Item "getprotoent"
2198.IP "getservent" 8
2199.IX Item "getservent"
2200.IP "setpwent" 8
2201.IX Item "setpwent"
2202.IP "setgrent" 8
2203.IX Item "setgrent"
2204.IP "sethostent \s-1STAYOPEN\s0" 8
2205.IX Item "sethostent STAYOPEN"
2206.IP "setnetent \s-1STAYOPEN\s0" 8
2207.IX Item "setnetent STAYOPEN"
2208.IP "setprotoent \s-1STAYOPEN\s0" 8
2209.IX Item "setprotoent STAYOPEN"
2210.IP "setservent \s-1STAYOPEN\s0" 8
2211.IX Item "setservent STAYOPEN"
2212.IP "endpwent" 8
2213.IX Item "endpwent"
2214.IP "endgrent" 8
2215.IX Item "endgrent"
2216.IP "endhostent" 8
2217.IX Item "endhostent"
2218.IP "endnetent" 8
2219.IX Item "endnetent"
2220.IP "endprotoent" 8
2221.IX Item "endprotoent"
2222.IP "endservent" 8
2223.IX Item "endservent"
2224.PD
2225These routines perform the same functions as their counterparts in the
2226system library. In list context, the return values from the
2227various get routines are as follows:
2228.Sp
2229.Vb 7
2230\& ($name,$passwd,$uid,$gid,
2231\& $quota,$comment,$gcos,$dir,$shell,$expire) = getpw*
2232\& ($name,$passwd,$gid,$members) = getgr*
2233\& ($name,$aliases,$addrtype,$length,@addrs) = gethost*
2234\& ($name,$aliases,$addrtype,$net) = getnet*
2235\& ($name,$aliases,$proto) = getproto*
2236\& ($name,$aliases,$port,$proto) = getserv*
2237.Ve
2238.Sp
2239(If the entry doesn't exist you get a null list.)
2240.Sp
2241The exact meaning of the \f(CW$gcos\fR field varies but it usually contains
2242the real name of the user (as opposed to the login name) and other
2243information pertaining to the user. Beware, however, that in many
2244system users are able to change this information and therefore it
2245cannot be trusted and therefore the \f(CW$gcos\fR is tainted (see
2246perlsec). The \f(CW$passwd\fR and \f(CW$shell\fR, user's encrypted password and
2247login shell, are also tainted, because of the same reason.
2248.Sp
2249In scalar context, you get the name, unless the function was a
2250lookup by name, in which case you get the other thing, whatever it is.
2251(If the entry doesn't exist you get the undefined value.) For example:
2252.Sp
2253.Vb 7
2254\& $uid = getpwnam($name);
2255\& $name = getpwuid($num);
2256\& $name = getpwent();
2257\& $gid = getgrnam($name);
2258\& $name = getgrgid($num;
2259\& $name = getgrent();
2260\& #etc.
2261.Ve
2262.Sp
2263In \fIgetpw*()\fR the fields \f(CW$quota\fR, \f(CW$comment\fR, and \f(CW$expire\fR are special
2264cases in the sense that in many systems they are unsupported. If the
2265\&\f(CW$quota\fR is unsupported, it is an empty scalar. If it is supported, it
2266usually encodes the disk quota. If the \f(CW$comment\fR field is unsupported,
2267it is an empty scalar. If it is supported it usually encodes some
2268administrative comment about the user. In some systems the \f(CW$quota\fR
2269field may be \f(CW$change\fR or \f(CW$age\fR, fields that have to do with password
2270aging. In some systems the \f(CW$comment\fR field may be \f(CW$class\fR. The \f(CW$expire\fR
2271field, if present, encodes the expiration period of the account or the
2272password. For the availability and the exact meaning of these fields
2273in your system, please consult your \fIgetpwnam\fR\|(3) documentation and your
2274\&\fIpwd.h\fR file. You can also find out from within Perl what your
2275\&\f(CW$quota\fR and \f(CW$comment\fR fields mean and whether you have the \f(CW$expire\fR field
2276by using the \f(CW\*(C`Config\*(C'\fR module and the values \f(CW\*(C`d_pwquota\*(C'\fR, \f(CW\*(C`d_pwage\*(C'\fR,
2277\&\f(CW\*(C`d_pwchange\*(C'\fR, \f(CW\*(C`d_pwcomment\*(C'\fR, and \f(CW\*(C`d_pwexpire\*(C'\fR. Shadow password
2278files are only supported if your vendor has implemented them in the
2279intuitive fashion that calling the regular C library routines gets the
2280shadow versions if you're running under privilege or if there exists
2281the \fIshadow\fR\|(3) functions as found in System V ( this includes Solaris
2282and Linux.) Those systems which implement a proprietary shadow password
2283facility are unlikely to be supported.
2284.Sp
2285The \f(CW$members\fR value returned by \fIgetgr*()\fR is a space separated list of
2286the login names of the members of the group.
2287.Sp
2288For the \fIgethost*()\fR functions, if the \f(CW\*(C`h_errno\*(C'\fR variable is supported in
2289C, it will be returned to you via \f(CW$?\fR if the function call fails. The
2290\&\f(CW@addrs\fR value returned by a successful call is a list of the raw
2291addresses returned by the corresponding system library call. In the
2292Internet domain, each address is four bytes long and you can unpack it
2293by saying something like:
2294.Sp
2295.Vb 1
2296\& ($a,$b,$c,$d) = unpack('C4',$addr[0]);
2297.Ve
2298.Sp
2299The Socket library makes this slightly easier:
2300.Sp
2301.Vb 3
2302\& use Socket;
2303\& $iaddr = inet_aton("127.1"); # or whatever address
2304\& $name = gethostbyaddr($iaddr, AF_INET);
2305.Ve
2306.Sp
2307.Vb 2
2308\& # or going the other way
2309\& $straddr = inet_ntoa($iaddr);
2310.Ve
2311.Sp
2312If you get tired of remembering which element of the return list
2313contains which return value, by-name interfaces are provided
2314in standard modules: \f(CW\*(C`File::stat\*(C'\fR, \f(CW\*(C`Net::hostent\*(C'\fR, \f(CW\*(C`Net::netent\*(C'\fR,
2315\&\f(CW\*(C`Net::protoent\*(C'\fR, \f(CW\*(C`Net::servent\*(C'\fR, \f(CW\*(C`Time::gmtime\*(C'\fR, \f(CW\*(C`Time::localtime\*(C'\fR,
2316and \f(CW\*(C`User::grent\*(C'\fR. These override the normal built\-ins, supplying
2317versions that return objects with the appropriate names
2318for each field. For example:
2319.Sp
2320.Vb 3
2321\& use File::stat;
2322\& use User::pwent;
2323\& $is_his = (stat($filename)->uid == pwent($whoever)->uid);
2324.Ve
2325.Sp
2326Even though it looks like they're the same method calls (uid),
2327they aren't, because a \f(CW\*(C`File::stat\*(C'\fR object is different from
2328a \f(CW\*(C`User::pwent\*(C'\fR object.
2329.IP "getsockname \s-1SOCKET\s0" 8
2330.IX Item "getsockname SOCKET"
2331Returns the packed sockaddr address of this end of the \s-1SOCKET\s0 connection,
2332in case you don't know the address because you have several different
2333IPs that the connection might have come in on.
2334.Sp
2335.Vb 6
2336\& use Socket;
2337\& $mysockaddr = getsockname(SOCK);
2338\& ($port, $myaddr) = sockaddr_in($mysockaddr);
2339\& printf "Connect to %s [%s]\en",
2340\& scalar gethostbyaddr($myaddr, AF_INET),
2341\& inet_ntoa($myaddr);
2342.Ve
2343.IP "getsockopt \s-1SOCKET\s0,LEVEL,OPTNAME" 8
2344.IX Item "getsockopt SOCKET,LEVEL,OPTNAME"
2345Returns the socket option requested, or undef if there is an error.
2346.IP "glob \s-1EXPR\s0" 8
2347.IX Item "glob EXPR"
2348.PD 0
2349.IP "glob" 8
2350.IX Item "glob"
2351.PD
2352In list context, returns a (possibly empty) list of filename expansions on
2353the value of \s-1EXPR\s0 such as the standard Unix shell \fI/bin/csh\fR would do. In
2354scalar context, glob iterates through such filename expansions, returning
2355undef when the list is exhausted. This is the internal function
2356implementing the \f(CW\*(C`<*.c>\*(C'\fR operator, but you can use it directly. If
2357\&\s-1EXPR\s0 is omitted, \f(CW$_\fR is used. The \f(CW\*(C`<*.c>\*(C'\fR operator is discussed in
2358more detail in \*(L"I/O Operators\*(R" in perlop.
2359.Sp
2360Beginning with v5.6.0, this operator is implemented using the standard
2361\&\f(CW\*(C`File::Glob\*(C'\fR extension. See File::Glob for details.
2362.IP "gmtime \s-1EXPR\s0" 8
2363.IX Item "gmtime EXPR"
2364Converts a time as returned by the time function to an 8\-element list
2365with the time localized for the standard Greenwich time zone.
2366Typically used as follows:
2367.Sp
2368.Vb 3
2369\& # 0 1 2 3 4 5 6 7
2370\& ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =
2371\& gmtime(time);
2372.Ve
2373.Sp
2374All list elements are numeric, and come straight out of the C `struct
2375tm'. \f(CW$sec\fR, \f(CW$min\fR, and \f(CW$hour\fR are the seconds, minutes, and hours of the
2376specified time. \f(CW$mday\fR is the day of the month, and \f(CW$mon\fR is the month
2377itself, in the range \f(CW0..11\fR with 0 indicating January and 11
2378indicating December. \f(CW$year\fR is the number of years since 1900. That
2379is, \f(CW$year\fR is \f(CW123\fR in year 2023. \f(CW$wday\fR is the day of the week, with
23800 indicating Sunday and 3 indicating Wednesday. \f(CW$yday\fR is the day of
2381the year, in the range \f(CW0..364\fR (or \f(CW0..365\fR in leap years.)
2382.Sp
2383Note that the \f(CW$year\fR element is \fInot\fR simply the last two digits of
2384the year. If you assume it is, then you create non\-Y2K\-compliant
2385programs\*(--and you wouldn't want to do that, would you?
2386.Sp
2387The proper way to get a complete 4\-digit year is simply:
2388.Sp
2389.Vb 1
2390\& $year += 1900;
2391.Ve
2392.Sp
2393And to get the last two digits of the year (e.g., '01' in 2001) do:
2394.Sp
2395.Vb 1
2396\& $year = sprintf("%02d", $year % 100);
2397.Ve
2398.Sp
2399If \s-1EXPR\s0 is omitted, \f(CW\*(C`gmtime()\*(C'\fR uses the current time (\f(CW\*(C`gmtime(time)\*(C'\fR).
2400.Sp
2401In scalar context, \f(CW\*(C`gmtime()\*(C'\fR returns the \fIctime\fR\|(3) value:
2402.Sp
2403.Vb 1
2404\& $now_string = gmtime; # e.g., "Thu Oct 13 04:54:34 1994"
2405.Ve
2406.Sp
2407Also see the \f(CW\*(C`timegm\*(C'\fR function provided by the \f(CW\*(C`Time::Local\*(C'\fR module,
2408and the \fIstrftime\fR\|(3) function available via the \s-1POSIX\s0 module.
2409.Sp
2410This scalar value is \fBnot\fR locale dependent (see perllocale), but
2411is instead a Perl builtin. Also see the \f(CW\*(C`Time::Local\*(C'\fR module, and the
2412\&\fIstrftime\fR\|(3) and \fImktime\fR\|(3) functions available via the \s-1POSIX\s0 module. To
2413get somewhat similar but locale dependent date strings, set up your
2414locale environment variables appropriately (please see perllocale)
2415and try for example:
2416.Sp
2417.Vb 2
2418\& use POSIX qw(strftime);
2419\& $now_string = strftime "%a %b %e %H:%M:%S %Y", gmtime;
2420.Ve
2421.Sp
2422Note that the \f(CW%a\fR and \f(CW%b\fR escapes, which represent the short forms
2423of the day of the week and the month of the year, may not necessarily
2424be three characters wide in all locales.
2425.IP "goto \s-1LABEL\s0" 8
2426.IX Item "goto LABEL"
2427.PD 0
2428.IP "goto \s-1EXPR\s0" 8
2429.IX Item "goto EXPR"
2430.IP "goto &NAME" 8
2431.IX Item "goto &NAME"
2432.PD
2433The \f(CW\*(C`goto\-LABEL\*(C'\fR form finds the statement labeled with \s-1LABEL\s0 and resumes
2434execution there. It may not be used to go into any construct that
2435requires initialization, such as a subroutine or a \f(CW\*(C`foreach\*(C'\fR loop. It
2436also can't be used to go into a construct that is optimized away,
2437or to get out of a block or subroutine given to \f(CW\*(C`sort\*(C'\fR.
2438It can be used to go almost anywhere else within the dynamic scope,
2439including out of subroutines, but it's usually better to use some other
2440construct such as \f(CW\*(C`last\*(C'\fR or \f(CW\*(C`die\*(C'\fR. The author of Perl has never felt the
2441need to use this form of \f(CW\*(C`goto\*(C'\fR (in Perl, that is\*(--C is another matter).
2442(The difference being that C does not offer named loops combined with
2443loop control. Perl does, and this replaces most structured uses of \f(CW\*(C`goto\*(C'\fR
2444in other languages.)
2445.Sp
2446The \f(CW\*(C`goto\-EXPR\*(C'\fR form expects a label name, whose scope will be resolved
2447dynamically. This allows for computed \f(CW\*(C`goto\*(C'\fRs per \s-1FORTRAN\s0, but isn't
2448necessarily recommended if you're optimizing for maintainability:
2449.Sp
2450.Vb 1
2451\& goto ("FOO", "BAR", "GLARCH")[$i];
2452.Ve
2453.Sp
2454The \f(CW\*(C`goto\-&NAME\*(C'\fR form is quite different from the other forms of
2455\&\f(CW\*(C`goto\*(C'\fR. In fact, it isn't a goto in the normal sense at all, and
2456doesn't have the stigma associated with other gotos. Instead, it
2457exits the current subroutine (losing any changes set by \fIlocal()\fR) and
2458immediately calls in its place the named subroutine using the current
2459value of \f(CW@_\fR. This is used by \f(CW\*(C`AUTOLOAD\*(C'\fR subroutines that wish to
2460load another subroutine and then pretend that the other subroutine had
2461been called in the first place (except that any modifications to \f(CW@_\fR
2462in the current subroutine are propagated to the other subroutine.)
2463After the \f(CW\*(C`goto\*(C'\fR, not even \f(CW\*(C`caller\*(C'\fR will be able to tell that this
2464routine was called first.
2465.Sp
2466\&\s-1NAME\s0 needn't be the name of a subroutine; it can be a scalar variable
2467containing a code reference, or a block which evaluates to a code
2468reference.
2469.IP "grep \s-1BLOCK\s0 \s-1LIST\s0" 8
2470.IX Item "grep BLOCK LIST"
2471.PD 0
2472.IP "grep \s-1EXPR\s0,LIST" 8
2473.IX Item "grep EXPR,LIST"
2474.PD
2475This is similar in spirit to, but not the same as, \fIgrep\fR\|(1) and its
2476relatives. In particular, it is not limited to using regular expressions.
2477.Sp
2478Evaluates the \s-1BLOCK\s0 or \s-1EXPR\s0 for each element of \s-1LIST\s0 (locally setting
2479\&\f(CW$_\fR to each element) and returns the list value consisting of those
2480elements for which the expression evaluated to true. In scalar
2481context, returns the number of times the expression was true.
2482.Sp
2483.Vb 1
2484\& @foo = grep(!/^#/, @bar); # weed out comments
2485.Ve
2486.Sp
2487or equivalently,
2488.Sp
2489.Vb 1
2490\& @foo = grep {!/^#/} @bar; # weed out comments
2491.Ve
2492.Sp
2493Note that \f(CW$_\fR is an alias to the list value, so it can be used to
2494modify the elements of the \s-1LIST\s0. While this is useful and supported,
2495it can cause bizarre results if the elements of \s-1LIST\s0 are not variables.
2496Similarly, grep returns aliases into the original list, much as a for
2497loop's index variable aliases the list elements. That is, modifying an
2498element of a list returned by grep (for example, in a \f(CW\*(C`foreach\*(C'\fR, \f(CW\*(C`map\*(C'\fR
2499or another \f(CW\*(C`grep\*(C'\fR) actually modifies the element in the original list.
2500This is usually something to be avoided when writing clear code.
2501.Sp
2502See also \*(L"map\*(R" for a list composed of the results of the \s-1BLOCK\s0 or \s-1EXPR\s0.
2503.IP "hex \s-1EXPR\s0" 8
2504.IX Item "hex EXPR"
2505.PD 0
2506.IP "hex" 8
2507.IX Item "hex"
2508.PD
2509Interprets \s-1EXPR\s0 as a hex string and returns the corresponding value.
2510(To convert strings that might start with either 0, 0x, or 0b, see
2511\&\*(L"oct\*(R".) If \s-1EXPR\s0 is omitted, uses \f(CW$_\fR.
2512.Sp
2513.Vb 2
2514\& print hex '0xAf'; # prints '175'
2515\& print hex 'aF'; # same
2516.Ve
2517.Sp
2518Hex strings may only represent integers. Strings that would cause
2519integer overflow trigger a warning. Leading whitespace is not stripped,
2520unlike \fIoct()\fR.
2521.IP "import" 8
2522.IX Item "import"
2523There is no builtin \f(CW\*(C`import\*(C'\fR function. It is just an ordinary
2524method (subroutine) defined (or inherited) by modules that wish to export
2525names to another module. The \f(CW\*(C`use\*(C'\fR function calls the \f(CW\*(C`import\*(C'\fR method
2526for the package used. See also \*(L"use\*(R", perlmod, and Exporter.
2527.IP "index \s-1STR\s0,SUBSTR,POSITION" 8
2528.IX Item "index STR,SUBSTR,POSITION"
2529.PD 0
2530.IP "index \s-1STR\s0,SUBSTR" 8
2531.IX Item "index STR,SUBSTR"
2532.PD
2533The index function searches for one string within another, but without
2534the wildcard-like behavior of a full regular-expression pattern match.
2535It returns the position of the first occurrence of \s-1SUBSTR\s0 in \s-1STR\s0 at
2536or after \s-1POSITION\s0. If \s-1POSITION\s0 is omitted, starts searching from the
2537beginning of the string. The return value is based at \f(CW0\fR (or whatever
2538you've set the \f(CW$[\fR variable to\*(--but don't do that). If the substring
2539is not found, returns one less than the base, ordinarily \f(CW\*(C`\-1\*(C'\fR.
2540.IP "int \s-1EXPR\s0" 8
2541.IX Item "int EXPR"
2542.PD 0
2543.IP "int" 8
2544.IX Item "int"
2545.PD
2546Returns the integer portion of \s-1EXPR\s0. If \s-1EXPR\s0 is omitted, uses \f(CW$_\fR.
2547You should not use this function for rounding: one because it truncates
2548towards \f(CW0\fR, and two because machine representations of floating point
2549numbers can sometimes produce counterintuitive results. For example,
2550\&\f(CW\*(C`int(\-6.725/0.025)\*(C'\fR produces \-268 rather than the correct \-269; that's
2551because it's really more like \-268.99999999999994315658 instead. Usually,
2552the \f(CW\*(C`sprintf\*(C'\fR, \f(CW\*(C`printf\*(C'\fR, or the \f(CW\*(C`POSIX::floor\*(C'\fR and \f(CW\*(C`POSIX::ceil\*(C'\fR
2553functions will serve you better than will \fIint()\fR.
2554.IP "ioctl \s-1FILEHANDLE\s0,FUNCTION,SCALAR" 8
2555.IX Item "ioctl FILEHANDLE,FUNCTION,SCALAR"
2556Implements the \fIioctl\fR\|(2) function. You'll probably first have to say
2557.Sp
2558.Vb 1
2559\& require "ioctl.ph"; # probably in /usr/local/lib/perl/ioctl.ph
2560.Ve
2561.Sp
2562to get the correct function definitions. If \fIioctl.ph\fR doesn't
2563exist or doesn't have the correct definitions you'll have to roll your
2564own, based on your C header files such as \fI<sys/ioctl.h>\fR.
2565(There is a Perl script called \fBh2ph\fR that comes with the Perl kit that
2566may help you in this, but it's nontrivial.) \s-1SCALAR\s0 will be read and/or
2567written depending on the FUNCTION\*(--a pointer to the string value of \s-1SCALAR\s0
2568will be passed as the third argument of the actual \f(CW\*(C`ioctl\*(C'\fR call. (If \s-1SCALAR\s0
2569has no string value but does have a numeric value, that value will be
2570passed rather than a pointer to the string value. To guarantee this to be
2571true, add a \f(CW0\fR to the scalar before using it.) The \f(CW\*(C`pack\*(C'\fR and \f(CW\*(C`unpack\*(C'\fR
2572functions may be needed to manipulate the values of structures used by
2573\&\f(CW\*(C`ioctl\*(C'\fR.
2574.Sp
2575The return value of \f(CW\*(C`ioctl\*(C'\fR (and \f(CW\*(C`fcntl\*(C'\fR) is as follows:
2576.Sp
2577.Vb 4
2578\& if OS returns: then Perl returns:
2579\& -1 undefined value
2580\& 0 string "0 but true"
2581\& anything else that number
2582.Ve
2583.Sp
2584Thus Perl returns true on success and false on failure, yet you can
2585still easily determine the actual value returned by the operating
2586system:
2587.Sp
2588.Vb 2
2589\& $retval = ioctl(...) || -1;
2590\& printf "System returned %d\en", $retval;
2591.Ve
2592.Sp
2593The special string "\f(CW0\fR but true" is exempt from \fB\-w\fR complaints
2594about improper numeric conversions.
2595.Sp
2596Here's an example of setting a filehandle named \f(CW\*(C`REMOTE\*(C'\fR to be
2597non-blocking at the system level. You'll have to negotiate \f(CW$|\fR
2598on your own, though.
2599.Sp
2600.Vb 1
2601\& use Fcntl qw(F_GETFL F_SETFL O_NONBLOCK);
2602.Ve
2603.Sp
2604.Vb 2
2605\& $flags = fcntl(REMOTE, F_GETFL, 0)
2606\& or die "Can't get flags for the socket: $!\en";
2607.Ve
2608.Sp
2609.Vb 2
2610\& $flags = fcntl(REMOTE, F_SETFL, $flags | O_NONBLOCK)
2611\& or die "Can't set flags for the socket: $!\en";
2612.Ve
2613.IP "join \s-1EXPR\s0,LIST" 8
2614.IX Item "join EXPR,LIST"
2615Joins the separate strings of \s-1LIST\s0 into a single string with fields
2616separated by the value of \s-1EXPR\s0, and returns that new string. Example:
2617.Sp
2618.Vb 1
2619\& $rec = join(':', $login,$passwd,$uid,$gid,$gcos,$home,$shell);
2620.Ve
2621.Sp
2622Beware that unlike \f(CW\*(C`split\*(C'\fR, \f(CW\*(C`join\*(C'\fR doesn't take a pattern as its
2623first argument. Compare \*(L"split\*(R".
2624.IP "keys \s-1HASH\s0" 8
2625.IX Item "keys HASH"
2626Returns a list consisting of all the keys of the named hash. (In
2627scalar context, returns the number of keys.) The keys are returned in
2628an apparently random order. The actual random order is subject to
2629change in future versions of perl, but it is guaranteed to be the same
2630order as either the \f(CW\*(C`values\*(C'\fR or \f(CW\*(C`each\*(C'\fR function produces (given
2631that the hash has not been modified). As a side effect, it resets
2632\&\s-1HASH\s0's iterator.
2633.Sp
2634Here is yet another way to print your environment:
2635.Sp
2636.Vb 5
2637\& @keys = keys %ENV;
2638\& @values = values %ENV;
2639\& while (@keys) {
2640\& print pop(@keys), '=', pop(@values), "\en";
2641\& }
2642.Ve
2643.Sp
2644or how about sorted by key:
2645.Sp
2646.Vb 3
2647\& foreach $key (sort(keys %ENV)) {
2648\& print $key, '=', $ENV{$key}, "\en";
2649\& }
2650.Ve
2651.Sp
2652The returned values are copies of the original keys in the hash, so
2653modifying them will not affect the original hash. Compare \*(L"values\*(R".
2654.Sp
2655To sort a hash by value, you'll need to use a \f(CW\*(C`sort\*(C'\fR function.
2656Here's a descending numeric sort of a hash by its values:
2657.Sp
2658.Vb 3
2659\& foreach $key (sort { $hash{$b} <=> $hash{$a} } keys %hash) {
2660\& printf "%4d %s\en", $hash{$key}, $key;
2661\& }
2662.Ve
2663.Sp
2664As an lvalue \f(CW\*(C`keys\*(C'\fR allows you to increase the number of hash buckets
2665allocated for the given hash. This can gain you a measure of efficiency if
2666you know the hash is going to get big. (This is similar to pre-extending
2667an array by assigning a larger number to $#array.) If you say
2668.Sp
2669.Vb 1
2670\& keys %hash = 200;
2671.Ve
2672.Sp
2673then \f(CW%hash\fR will have at least 200 buckets allocated for it\-\-256 of them,
2674in fact, since it rounds up to the next power of two. These
2675buckets will be retained even if you do \f(CW\*(C`%hash = ()\*(C'\fR, use \f(CW\*(C`undef
2676%hash\*(C'\fR if you want to free the storage while \f(CW%hash\fR is still in scope.
2677You can't shrink the number of buckets allocated for the hash using
2678\&\f(CW\*(C`keys\*(C'\fR in this way (but you needn't worry about doing this by accident,
2679as trying has no effect).
2680.Sp
2681See also \f(CW\*(C`each\*(C'\fR, \f(CW\*(C`values\*(C'\fR and \f(CW\*(C`sort\*(C'\fR.
2682.IP "kill \s-1SIGNAL\s0, \s-1LIST\s0" 8
2683.IX Item "kill SIGNAL, LIST"
2684Sends a signal to a list of processes. Returns the number of
2685processes successfully signaled (which is not necessarily the
2686same as the number actually killed).
2687.Sp
2688.Vb 2
2689\& $cnt = kill 1, $child1, $child2;
2690\& kill 9, @goners;
2691.Ve
2692.Sp
2693If \s-1SIGNAL\s0 is zero, no signal is sent to the process. This is a
2694useful way to check that the process is alive and hasn't changed
2695its \s-1UID\s0. See perlport for notes on the portability of this
2696construct.
2697.Sp
2698Unlike in the shell, if \s-1SIGNAL\s0 is negative, it kills
2699process groups instead of processes. (On System V, a negative \fI\s-1PROCESS\s0\fR
2700number will also kill process groups, but that's not portable.) That
2701means you usually want to use positive not negative signals. You may also
2702use a signal name in quotes. See \*(L"Signals\*(R" in perlipc for details.
2703.IP "last \s-1LABEL\s0" 8
2704.IX Item "last LABEL"
2705.PD 0
2706.IP "last" 8
2707.IX Item "last"
2708.PD
2709The \f(CW\*(C`last\*(C'\fR command is like the \f(CW\*(C`break\*(C'\fR statement in C (as used in
2710loops); it immediately exits the loop in question. If the \s-1LABEL\s0 is
2711omitted, the command refers to the innermost enclosing loop. The
2712\&\f(CW\*(C`continue\*(C'\fR block, if any, is not executed:
2713.Sp
2714.Vb 4
2715\& LINE: while (<STDIN>) {
2716\& last LINE if /^$/; # exit when done with header
2717\& #...
2718\& }
2719.Ve
2720.Sp
2721\&\f(CW\*(C`last\*(C'\fR cannot be used to exit a block which returns a value such as
2722\&\f(CW\*(C`eval {}\*(C'\fR, \f(CW\*(C`sub {}\*(C'\fR or \f(CW\*(C`do {}\*(C'\fR, and should not be used to exit
2723a \fIgrep()\fR or \fImap()\fR operation.
2724.Sp
2725Note that a block by itself is semantically identical to a loop
2726that executes once. Thus \f(CW\*(C`last\*(C'\fR can be used to effect an early
2727exit out of such a block.
2728.Sp
2729See also \*(L"continue\*(R" for an illustration of how \f(CW\*(C`last\*(C'\fR, \f(CW\*(C`next\*(C'\fR, and
2730\&\f(CW\*(C`redo\*(C'\fR work.
2731.IP "lc \s-1EXPR\s0" 8
2732.IX Item "lc EXPR"
2733.PD 0
2734.IP "lc" 8
2735.IX Item "lc"
2736.PD
2737Returns a lowercased version of \s-1EXPR\s0. This is the internal function
2738implementing the \f(CW\*(C`\eL\*(C'\fR escape in double-quoted strings. Respects
2739current \s-1LC_CTYPE\s0 locale if \f(CW\*(C`use locale\*(C'\fR in force. See perllocale
2740and perlunicode for more details about locale and Unicode support.
2741.Sp
2742If \s-1EXPR\s0 is omitted, uses \f(CW$_\fR.
2743.IP "lcfirst \s-1EXPR\s0" 8
2744.IX Item "lcfirst EXPR"
2745.PD 0
2746.IP "lcfirst" 8
2747.IX Item "lcfirst"
2748.PD
2749Returns the value of \s-1EXPR\s0 with the first character lowercased. This
2750is the internal function implementing the \f(CW\*(C`\el\*(C'\fR escape in
2751double-quoted strings. Respects current \s-1LC_CTYPE\s0 locale if \f(CW\*(C`use
2752locale\*(C'\fR in force. See perllocale and perlunicode for more
2753details about locale and Unicode support.
2754.Sp
2755If \s-1EXPR\s0 is omitted, uses \f(CW$_\fR.
2756.IP "length \s-1EXPR\s0" 8
2757.IX Item "length EXPR"
2758.PD 0
2759.IP "length" 8
2760.IX Item "length"
2761.PD
2762Returns the length in characters of the value of \s-1EXPR\s0. If \s-1EXPR\s0 is
2763omitted, returns length of \f(CW$_\fR. Note that this cannot be used on
2764an entire array or hash to find out how many elements these have.
2765For that, use \f(CW\*(C`scalar @array\*(C'\fR and \f(CW\*(C`scalar keys %hash\*(C'\fR respectively.
2766.IP "link \s-1OLDFILE\s0,NEWFILE" 8
2767.IX Item "link OLDFILE,NEWFILE"
2768Creates a new filename linked to the old filename. Returns true for
2769success, false otherwise.
2770.IP "listen \s-1SOCKET\s0,QUEUESIZE" 8
2771.IX Item "listen SOCKET,QUEUESIZE"
2772Does the same thing that the listen system call does. Returns true if
2773it succeeded, false otherwise. See the example in
2774\&\*(L"Sockets: Client/Server Communication\*(R" in perlipc.
2775.IP "local \s-1EXPR\s0" 8
2776.IX Item "local EXPR"
2777You really probably want to be using \f(CW\*(C`my\*(C'\fR instead, because \f(CW\*(C`local\*(C'\fR isn't
2778what most people think of as \*(L"local\*(R". See
2779\&\*(L"Private Variables via \fImy()\fR\*(R" in perlsub for details.
2780.Sp
2781A local modifies the listed variables to be local to the enclosing
2782block, file, or eval. If more than one value is listed, the list must
2783be placed in parentheses. See \*(L"Temporary Values via \fIlocal()\fR\*(R" in perlsub
2784for details, including issues with tied arrays and hashes.
2785.IP "localtime \s-1EXPR\s0" 8
2786.IX Item "localtime EXPR"
2787Converts a time as returned by the time function to a 9\-element list
2788with the time analyzed for the local time zone. Typically used as
2789follows:
2790.Sp
2791.Vb 3
2792\& # 0 1 2 3 4 5 6 7 8
2793\& ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
2794\& localtime(time);
2795.Ve
2796.Sp
2797All list elements are numeric, and come straight out of the C `struct
2798tm'. \f(CW$sec\fR, \f(CW$min\fR, and \f(CW$hour\fR are the seconds, minutes, and hours of the
2799specified time. \f(CW$mday\fR is the day of the month, and \f(CW$mon\fR is the month
2800itself, in the range \f(CW0..11\fR with 0 indicating January and 11
2801indicating December. \f(CW$year\fR is the number of years since 1900. That
2802is, \f(CW$year\fR is \f(CW123\fR in year 2023. \f(CW$wday\fR is the day of the week, with
28030 indicating Sunday and 3 indicating Wednesday. \f(CW$yday\fR is the day of
2804the year, in the range \f(CW0..364\fR (or \f(CW0..365\fR in leap years.) \f(CW$isdst\fR
2805is true if the specified time occurs during daylight savings time,
2806false otherwise.
2807.Sp
2808Note that the \f(CW$year\fR element is \fInot\fR simply the last two digits of
2809the year. If you assume it is, then you create non\-Y2K\-compliant
2810programs\*(--and you wouldn't want to do that, would you?
2811.Sp
2812The proper way to get a complete 4\-digit year is simply:
2813.Sp
2814.Vb 1
2815\& $year += 1900;
2816.Ve
2817.Sp
2818And to get the last two digits of the year (e.g., '01' in 2001) do:
2819.Sp
2820.Vb 1
2821\& $year = sprintf("%02d", $year % 100);
2822.Ve
2823.Sp
2824If \s-1EXPR\s0 is omitted, \f(CW\*(C`localtime()\*(C'\fR uses the current time (\f(CW\*(C`localtime(time)\*(C'\fR).
2825.Sp
2826In scalar context, \f(CW\*(C`localtime()\*(C'\fR returns the \fIctime\fR\|(3) value:
2827.Sp
2828.Vb 1
2829\& $now_string = localtime; # e.g., "Thu Oct 13 04:54:34 1994"
2830.Ve
2831.Sp
2832This scalar value is \fBnot\fR locale dependent, see perllocale, but
2833instead a Perl builtin. Also see the \f(CW\*(C`Time::Local\*(C'\fR module
2834(to convert the second, minutes, hours, ... back to seconds since the
2835stroke of midnight the 1st of January 1970, the value returned by
2836\&\fItime()\fR), and the \fIstrftime\fR\|(3) and \fImktime\fR\|(3) functions available via the
2837\&\s-1POSIX\s0 module. To get somewhat similar but locale dependent date
2838strings, set up your locale environment variables appropriately
2839(please see perllocale) and try for example:
2840.Sp
2841.Vb 2
2842\& use POSIX qw(strftime);
2843\& $now_string = strftime "%a %b %e %H:%M:%S %Y", localtime;
2844.Ve
2845.Sp
2846Note that the \f(CW%a\fR and \f(CW%b\fR, the short forms of the day of the week
2847and the month of the year, may not necessarily be three characters wide.
2848.IP "lock \s-1THING\s0" 8
2849.IX Item "lock THING"
2850This function places an advisory lock on a shared variable, or referenced
2851object contained in \fI\s-1THING\s0\fR until the lock goes out of scope.
2852.Sp
2853\&\fIlock()\fR is a \*(L"weak keyword\*(R" : this means that if you've defined a function
2854by this name (before any calls to it), that function will be called
2855instead. (However, if you've said \f(CW\*(C`use threads\*(C'\fR, \fIlock()\fR is always a
2856keyword.) See threads.
2857.IP "log \s-1EXPR\s0" 8
2858.IX Item "log EXPR"
2859.PD 0
2860.IP "log" 8
2861.IX Item "log"
2862.PD
2863Returns the natural logarithm (base \fIe\fR) of \s-1EXPR\s0. If \s-1EXPR\s0 is omitted,
2864returns log of \f(CW$_\fR. To get the log of another base, use basic algebra:
2865The base-N log of a number is equal to the natural log of that number
2866divided by the natural log of N. For example:
2867.Sp
2868.Vb 4
2869\& sub log10 {
2870\& my $n = shift;
2871\& return log($n)/log(10);
2872\& }
2873.Ve
2874.Sp
2875See also \*(L"exp\*(R" for the inverse operation.
2876.IP "lstat \s-1EXPR\s0" 8
2877.IX Item "lstat EXPR"
2878.PD 0
2879.IP "lstat" 8
2880.IX Item "lstat"
2881.PD
2882Does the same thing as the \f(CW\*(C`stat\*(C'\fR function (including setting the
2883special \f(CW\*(C`_\*(C'\fR filehandle) but stats a symbolic link instead of the file
2884the symbolic link points to. If symbolic links are unimplemented on
2885your system, a normal \f(CW\*(C`stat\*(C'\fR is done.
2886.Sp
2887If \s-1EXPR\s0 is omitted, stats \f(CW$_\fR.
2888.IP "m//" 8
2889.IX Item "m//"
2890The match operator. See perlop.
2891.IP "map \s-1BLOCK\s0 \s-1LIST\s0" 8
2892.IX Item "map BLOCK LIST"
2893.PD 0
2894.IP "map \s-1EXPR\s0,LIST" 8
2895.IX Item "map EXPR,LIST"
2896.PD
2897Evaluates the \s-1BLOCK\s0 or \s-1EXPR\s0 for each element of \s-1LIST\s0 (locally setting
2898\&\f(CW$_\fR to each element) and returns the list value composed of the
2899results of each such evaluation. In scalar context, returns the
2900total number of elements so generated. Evaluates \s-1BLOCK\s0 or \s-1EXPR\s0 in
2901list context, so each element of \s-1LIST\s0 may produce zero, one, or
2902more elements in the returned value.
2903.Sp
2904.Vb 1
2905\& @chars = map(chr, @nums);
2906.Ve
2907.Sp
2908translates a list of numbers to the corresponding characters. And
2909.Sp
2910.Vb 1
2911\& %hash = map { getkey($_) => $_ } @array;
2912.Ve
2913.Sp
2914is just a funny way to write
2915.Sp
2916.Vb 4
2917\& %hash = ();
2918\& foreach $_ (@array) {
2919\& $hash{getkey($_)} = $_;
2920\& }
2921.Ve
2922.Sp
2923Note that \f(CW$_\fR is an alias to the list value, so it can be used to
2924modify the elements of the \s-1LIST\s0. While this is useful and supported,
2925it can cause bizarre results if the elements of \s-1LIST\s0 are not variables.
2926Using a regular \f(CW\*(C`foreach\*(C'\fR loop for this purpose would be clearer in
2927most cases. See also \*(L"grep\*(R" for an array composed of those items of
2928the original list for which the \s-1BLOCK\s0 or \s-1EXPR\s0 evaluates to true.
2929.Sp
2930\&\f(CW\*(C`{\*(C'\fR starts both hash references and blocks, so \f(CW\*(C`map { ...\*(C'\fR could be either
2931the start of map \s-1BLOCK\s0 \s-1LIST\s0 or map \s-1EXPR\s0, \s-1LIST\s0. Because perl doesn't look
2932ahead for the closing \f(CW\*(C`}\*(C'\fR it has to take a guess at which its dealing with
2933based what it finds just after the \f(CW\*(C`{\*(C'\fR. Usually it gets it right, but if it
2934doesn't it won't realize something is wrong until it gets to the \f(CW\*(C`}\*(C'\fR and
2935encounters the missing (or unexpected) comma. The syntax error will be
2936reported close to the \f(CW\*(C`}\*(C'\fR but you'll need to change something near the \f(CW\*(C`{\*(C'\fR
2937such as using a unary \f(CW\*(C`+\*(C'\fR to give perl some help:
2938.Sp
2939.Vb 5
2940\& %hash = map { "\eL$_", 1 } @array # perl guesses EXPR. wrong
2941\& %hash = map { +"\eL$_", 1 } @array # perl guesses BLOCK. right
2942\& %hash = map { ("\eL$_", 1) } @array # this also works
2943\& %hash = map { lc($_), 1 } @array # as does this.
2944\& %hash = map +( lc($_), 1 ), @array # this is EXPR and works!
2945.Ve
2946.Sp
2947.Vb 1
2948\& %hash = map ( lc($_), 1 ), @array # evaluates to (1, @array)
2949.Ve
2950.Sp
2951or to force an anon hash constructor use \f(CW\*(C`+{\*(C'\fR
2952.Sp
2953.Vb 1
2954\& @hashes = map +{ lc($_), 1 }, @array # EXPR, so needs , at end
2955.Ve
2956.Sp
2957and you get list of anonymous hashes each with only 1 entry.
2958.IP "mkdir \s-1FILENAME\s0,MASK" 8
2959.IX Item "mkdir FILENAME,MASK"
2960.PD 0
2961.IP "mkdir \s-1FILENAME\s0" 8
2962.IX Item "mkdir FILENAME"
2963.PD
2964Creates the directory specified by \s-1FILENAME\s0, with permissions
2965specified by \s-1MASK\s0 (as modified by \f(CW\*(C`umask\*(C'\fR). If it succeeds it
2966returns true, otherwise it returns false and sets \f(CW$!\fR (errno).
2967If omitted, \s-1MASK\s0 defaults to 0777.
2968.Sp
2969In general, it is better to create directories with permissive \s-1MASK\s0,
2970and let the user modify that with their \f(CW\*(C`umask\*(C'\fR, than it is to supply
2971a restrictive \s-1MASK\s0 and give the user no way to be more permissive.
2972The exceptions to this rule are when the file or directory should be
2973kept private (mail files, for instance). The \fIperlfunc\fR\|(1) entry on
2974\&\f(CW\*(C`umask\*(C'\fR discusses the choice of \s-1MASK\s0 in more detail.
2975.Sp
2976Note that according to the \s-1POSIX\s0 1003.1\-1996 the \s-1FILENAME\s0 may have any
2977number of trailing slashes. Some operating and filesystems do not get
2978this right, so Perl automatically removes all trailing slashes to keep
2979everyone happy.
2980.IP "msgctl \s-1ID\s0,CMD,ARG" 8
2981.IX Item "msgctl ID,CMD,ARG"
2982Calls the System V \s-1IPC\s0 function \fImsgctl\fR\|(2). You'll probably have to say
2983.Sp
2984.Vb 1
2985\& use IPC::SysV;
2986.Ve
2987.Sp
2988first to get the correct constant definitions. If \s-1CMD\s0 is \f(CW\*(C`IPC_STAT\*(C'\fR,
2989then \s-1ARG\s0 must be a variable which will hold the returned \f(CW\*(C`msqid_ds\*(C'\fR
2990structure. Returns like \f(CW\*(C`ioctl\*(C'\fR: the undefined value for error,
2991\&\f(CW"0 but true"\fR for zero, or the actual return value otherwise. See also
2992\&\*(L"SysV \s-1IPC\s0\*(R" in perlipc, \f(CW\*(C`IPC::SysV\*(C'\fR, and \f(CW\*(C`IPC::Semaphore\*(C'\fR documentation.
2993.IP "msgget \s-1KEY\s0,FLAGS" 8
2994.IX Item "msgget KEY,FLAGS"
2995Calls the System V \s-1IPC\s0 function \fImsgget\fR\|(2). Returns the message queue
2996id, or the undefined value if there is an error. See also
2997\&\*(L"SysV \s-1IPC\s0\*(R" in perlipc and \f(CW\*(C`IPC::SysV\*(C'\fR and \f(CW\*(C`IPC::Msg\*(C'\fR documentation.
2998.IP "msgrcv \s-1ID\s0,VAR,SIZE,TYPE,FLAGS" 8
2999.IX Item "msgrcv ID,VAR,SIZE,TYPE,FLAGS"
3000Calls the System V \s-1IPC\s0 function msgrcv to receive a message from
3001message queue \s-1ID\s0 into variable \s-1VAR\s0 with a maximum message size of
3002\&\s-1SIZE\s0. Note that when a message is received, the message type as a
3003native long integer will be the first thing in \s-1VAR\s0, followed by the
3004actual message. This packing may be opened with \f(CW\*(C`unpack("l! a*")\*(C'\fR.
3005Taints the variable. Returns true if successful, or false if there is
3006an error. See also \*(L"SysV \s-1IPC\s0\*(R" in perlipc, \f(CW\*(C`IPC::SysV\*(C'\fR, and
3007\&\f(CW\*(C`IPC::SysV::Msg\*(C'\fR documentation.
3008.IP "msgsnd \s-1ID\s0,MSG,FLAGS" 8
3009.IX Item "msgsnd ID,MSG,FLAGS"
3010Calls the System V \s-1IPC\s0 function msgsnd to send the message \s-1MSG\s0 to the
3011message queue \s-1ID\s0. \s-1MSG\s0 must begin with the native long integer message
3012type, and be followed by the length of the actual message, and finally
3013the message itself. This kind of packing can be achieved with
3014\&\f(CW\*(C`pack("l! a*", $type, $message)\*(C'\fR. Returns true if successful,
3015or false if there is an error. See also \f(CW\*(C`IPC::SysV\*(C'\fR
3016and \f(CW\*(C`IPC::SysV::Msg\*(C'\fR documentation.
3017.IP "my \s-1EXPR\s0" 8
3018.IX Item "my EXPR"
3019.PD 0
3020.IP "my \s-1TYPE\s0 \s-1EXPR\s0" 8
3021.IX Item "my TYPE EXPR"
3022.IP "my \s-1EXPR\s0 : \s-1ATTRS\s0" 8
3023.IX Item "my EXPR : ATTRS"
3024.IP "my \s-1TYPE\s0 \s-1EXPR\s0 : \s-1ATTRS\s0" 8
3025.IX Item "my TYPE EXPR : ATTRS"
3026.PD
3027A \f(CW\*(C`my\*(C'\fR declares the listed variables to be local (lexically) to the
3028enclosing block, file, or \f(CW\*(C`eval\*(C'\fR. If more than one value is listed,
3029the list must be placed in parentheses.
3030.Sp
3031The exact semantics and interface of \s-1TYPE\s0 and \s-1ATTRS\s0 are still
3032evolving. \s-1TYPE\s0 is currently bound to the use of \f(CW\*(C`fields\*(C'\fR pragma,
3033and attributes are handled using the \f(CW\*(C`attributes\*(C'\fR pragma, or starting
3034from Perl 5.8.0 also via the \f(CW\*(C`Attribute::Handlers\*(C'\fR module. See
3035\&\*(L"Private Variables via \fImy()\fR\*(R" in perlsub for details, and fields,
3036attributes, and Attribute::Handlers.
3037.IP "next \s-1LABEL\s0" 8
3038.IX Item "next LABEL"
3039.PD 0
3040.IP "next" 8
3041.IX Item "next"
3042.PD
3043The \f(CW\*(C`next\*(C'\fR command is like the \f(CW\*(C`continue\*(C'\fR statement in C; it starts
3044the next iteration of the loop:
3045.Sp
3046.Vb 4
3047\& LINE: while (<STDIN>) {
3048\& next LINE if /^#/; # discard comments
3049\& #...
3050\& }
3051.Ve
3052.Sp
3053Note that if there were a \f(CW\*(C`continue\*(C'\fR block on the above, it would get
3054executed even on discarded lines. If the \s-1LABEL\s0 is omitted, the command
3055refers to the innermost enclosing loop.
3056.Sp
3057\&\f(CW\*(C`next\*(C'\fR cannot be used to exit a block which returns a value such as
3058\&\f(CW\*(C`eval {}\*(C'\fR, \f(CW\*(C`sub {}\*(C'\fR or \f(CW\*(C`do {}\*(C'\fR, and should not be used to exit
3059a \fIgrep()\fR or \fImap()\fR operation.
3060.Sp
3061Note that a block by itself is semantically identical to a loop
3062that executes once. Thus \f(CW\*(C`next\*(C'\fR will exit such a block early.
3063.Sp
3064See also \*(L"continue\*(R" for an illustration of how \f(CW\*(C`last\*(C'\fR, \f(CW\*(C`next\*(C'\fR, and
3065\&\f(CW\*(C`redo\*(C'\fR work.
3066.IP "no Module \s-1VERSION\s0 \s-1LIST\s0" 8
3067.IX Item "no Module VERSION LIST"
3068.PD 0
3069.IP "no Module \s-1VERSION\s0" 8
3070.IX Item "no Module VERSION"
3071.IP "no Module \s-1LIST\s0" 8
3072.IX Item "no Module LIST"
3073.IP "no Module" 8
3074.IX Item "no Module"
3075.PD
3076See the \*(L"use\*(R" function, which \f(CW\*(C`no\*(C'\fR is the opposite of.
3077.IP "oct \s-1EXPR\s0" 8
3078.IX Item "oct EXPR"
3079.PD 0
3080.IP "oct" 8
3081.IX Item "oct"
3082.PD
3083Interprets \s-1EXPR\s0 as an octal string and returns the corresponding
3084value. (If \s-1EXPR\s0 happens to start off with \f(CW\*(C`0x\*(C'\fR, interprets it as a
3085hex string. If \s-1EXPR\s0 starts off with \f(CW\*(C`0b\*(C'\fR, it is interpreted as a
3086binary string. Leading whitespace is ignored in all three cases.)
3087The following will handle decimal, binary, octal, and hex in the standard
3088Perl or C notation:
3089.Sp
3090.Vb 1
3091\& $val = oct($val) if $val =~ /^0/;
3092.Ve
3093.Sp
3094If \s-1EXPR\s0 is omitted, uses \f(CW$_\fR. To go the other way (produce a number
3095in octal), use \fIsprintf()\fR or \fIprintf()\fR:
3096.Sp
3097.Vb 2
3098\& $perms = (stat("filename"))[2] & 07777;
3099\& $oct_perms = sprintf "%lo", $perms;
3100.Ve
3101.Sp
3102The \fIoct()\fR function is commonly used when a string such as \f(CW644\fR needs
3103to be converted into a file mode, for example. (Although perl will
3104automatically convert strings into numbers as needed, this automatic
3105conversion assumes base 10.)
3106.IP "open \s-1FILEHANDLE\s0,EXPR" 8
3107.IX Item "open FILEHANDLE,EXPR"
3108.PD 0
3109.IP "open \s-1FILEHANDLE\s0,MODE,EXPR" 8
3110.IX Item "open FILEHANDLE,MODE,EXPR"
3111.IP "open \s-1FILEHANDLE\s0,MODE,EXPR,LIST" 8
3112.IX Item "open FILEHANDLE,MODE,EXPR,LIST"
3113.IP "open \s-1FILEHANDLE\s0,MODE,REFERENCE" 8
3114.IX Item "open FILEHANDLE,MODE,REFERENCE"
3115.IP "open \s-1FILEHANDLE\s0" 8
3116.IX Item "open FILEHANDLE"
3117.PD
3118Opens the file whose filename is given by \s-1EXPR\s0, and associates it with
3119\&\s-1FILEHANDLE\s0.
3120.Sp
3121(The following is a comprehensive reference to \fIopen()\fR: for a gentler
3122introduction you may consider perlopentut.)
3123.Sp
3124If \s-1FILEHANDLE\s0 is an undefined lexical (\f(CW\*(C`my\*(C'\fR) variable the variable is
3125assigned a reference to a new anonymous filehandle, otherwise if
3126\&\s-1FILEHANDLE\s0 is an expression, its value is used as the name of the real
3127filehandle wanted. (This is considered a symbolic reference, so \f(CW\*(C`use
3128strict 'refs'\*(C'\fR should \fInot\fR be in effect.)
3129.Sp
3130If \s-1EXPR\s0 is omitted, the scalar variable of the same name as the
3131\&\s-1FILEHANDLE\s0 contains the filename. (Note that lexical variables\*(--those
3132declared with \f(CW\*(C`my\*(C'\fR\-\-will not work for this purpose; so if you're
3133using \f(CW\*(C`my\*(C'\fR, specify \s-1EXPR\s0 in your call to open.)
3134.Sp
3135If three or more arguments are specified then the mode of opening and
3136the file name are separate. If \s-1MODE\s0 is \f(CW'<'\fR or nothing, the file
3137is opened for input. If \s-1MODE\s0 is \f(CW'>'\fR, the file is truncated and
3138opened for output, being created if necessary. If \s-1MODE\s0 is \f(CW'>>'\fR,
3139the file is opened for appending, again being created if necessary.
3140.Sp
3141You can put a \f(CW'+'\fR in front of the \f(CW'>'\fR or \f(CW'<'\fR to
3142indicate that you want both read and write access to the file; thus
3143\&\f(CW'+<'\fR is almost always preferred for read/write updates\*(--the \f(CW'+>'\fR mode would clobber the file first. You can't usually use
3144either read-write mode for updating textfiles, since they have
3145variable length records. See the \fB\-i\fR switch in perlrun for a
3146better approach. The file is created with permissions of \f(CW0666\fR
3147modified by the process' \f(CW\*(C`umask\*(C'\fR value.
3148.Sp
3149These various prefixes correspond to the \fIfopen\fR\|(3) modes of \f(CW'r'\fR,
3150\&\f(CW'r+'\fR, \f(CW'w'\fR, \f(CW'w+'\fR, \f(CW'a'\fR, and \f(CW'a+'\fR.
3151.Sp
3152In the 2\-arguments (and 1\-argument) form of the call the mode and
3153filename should be concatenated (in this order), possibly separated by
3154spaces. It is possible to omit the mode in these forms if the mode is
3155\&\f(CW'<'\fR.
3156.Sp
3157If the filename begins with \f(CW'|'\fR, the filename is interpreted as a
3158command to which output is to be piped, and if the filename ends with a
3159\&\f(CW'|'\fR, the filename is interpreted as a command which pipes output to
3160us. See \*(L"Using \fIopen()\fR for \s-1IPC\s0\*(R" in perlipc
3161for more examples of this. (You are not allowed to \f(CW\*(C`open\*(C'\fR to a command
3162that pipes both in \fIand\fR out, but see IPC::Open2, IPC::Open3,
3163and \*(L"Bidirectional Communication with Another Process\*(R" in perlipc
3164for alternatives.)
3165.Sp
3166For three or more arguments if \s-1MODE\s0 is \f(CW'|\-'\fR, the filename is
3167interpreted as a command to which output is to be piped, and if \s-1MODE\s0
3168is \f(CW'\-|'\fR, the filename is interpreted as a command which pipes
3169output to us. In the 2\-arguments (and 1\-argument) form one should
3170replace dash (\f(CW'\-'\fR) with the command.
3171See \*(L"Using \fIopen()\fR for \s-1IPC\s0\*(R" in perlipc for more examples of this.
3172(You are not allowed to \f(CW\*(C`open\*(C'\fR to a command that pipes both in \fIand\fR
3173out, but see IPC::Open2, IPC::Open3, and
3174\&\*(L"Bidirectional Communication\*(R" in perlipc for alternatives.)
3175.Sp
3176In the three-or-more argument form of pipe opens, if \s-1LIST\s0 is specified
3177(extra arguments after the command name) then \s-1LIST\s0 becomes arguments
3178to the command invoked if the platform supports it. The meaning of
3179\&\f(CW\*(C`open\*(C'\fR with more than three arguments for non-pipe modes is not yet
3180specified. Experimental \*(L"layers\*(R" may give extra \s-1LIST\s0 arguments
3181meaning.
3182.Sp
3183In the 2\-arguments (and 1\-argument) form opening \f(CW'\-'\fR opens \s-1STDIN\s0
3184and opening \f(CW'>\-'\fR opens \s-1STDOUT\s0.
3185.Sp
3186You may use the three-argument form of open to specify \s-1IO\s0 \*(L"layers\*(R"
3187(sometimes also referred to as \*(L"disciplines\*(R") to be applied to the handle
3188that affect how the input and output are processed (see open and
3189PerlIO for more details). For example
3190.Sp
3191.Vb 1
3192\& open(FH, "<:utf8", "file")
3193.Ve
3194.Sp
3195will open the \s-1UTF\-8\s0 encoded file containing Unicode characters,
3196see perluniintro. (Note that if layers are specified in the
3197three-arg form then default layers set by the \f(CW\*(C`open\*(C'\fR pragma are
3198ignored.)
3199.Sp
3200Open returns nonzero upon success, the undefined value otherwise. If
3201the \f(CW\*(C`open\*(C'\fR involved a pipe, the return value happens to be the pid of
3202the subprocess.
3203.Sp
3204If you're running Perl on a system that distinguishes between text
3205files and binary files, then you should check out \*(L"binmode\*(R" for tips
3206for dealing with this. The key distinction between systems that need
3207\&\f(CW\*(C`binmode\*(C'\fR and those that don't is their text file formats. Systems
3208like Unix, Mac \s-1OS\s0, and Plan 9, which delimit lines with a single
3209character, and which encode that character in C as \f(CW"\en"\fR, do not
3210need \f(CW\*(C`binmode\*(C'\fR. The rest need it.
3211.Sp
3212When opening a file, it's usually a bad idea to continue normal execution
3213if the request failed, so \f(CW\*(C`open\*(C'\fR is frequently used in connection with
3214\&\f(CW\*(C`die\*(C'\fR. Even if \f(CW\*(C`die\*(C'\fR won't do what you want (say, in a \s-1CGI\s0 script,
3215where you want to make a nicely formatted error message (but there are
3216modules that can help with that problem)) you should always check
3217the return value from opening a file. The infrequent exception is when
3218working with an unopened filehandle is actually what you want to do.
3219.Sp
3220As a special case the 3 arg form with a read/write mode and the third
3221argument being \f(CW\*(C`undef\*(C'\fR:
3222.Sp
3223.Vb 1
3224\& open(TMP, "+>", undef) or die ...
3225.Ve
3226.Sp
3227opens a filehandle to an anonymous temporary file.
3228.Sp
3229File handles can be opened to \*(L"in memory\*(R" files held in Perl scalars via:
3230.Sp
3231.Vb 1
3232\& open($fh, '>', \e$variable) || ..
3233.Ve
3234.Sp
3235Though if you try to re-open \f(CW\*(C`STDOUT\*(C'\fR or \f(CW\*(C`STDERR\*(C'\fR as an \*(L"in memory\*(R"
3236file, you have to close it first:
3237.Sp
3238.Vb 2
3239\& close STDOUT;
3240\& open STDOUT, '>', \e$variable or die "Can't open STDOUT: $!";
3241.Ve
3242.Sp
3243Examples:
3244.Sp
3245.Vb 3
3246\& $ARTICLE = 100;
3247\& open ARTICLE or die "Can't find article $ARTICLE: $!\en";
3248\& while (<ARTICLE>) {...
3249.Ve
3250.Sp
3251.Vb 2
3252\& open(LOG, '>>/usr/spool/news/twitlog'); # (log is reserved)
3253\& # if the open fails, output is discarded
3254.Ve
3255.Sp
3256.Vb 2
3257\& open(DBASE, '+<', 'dbase.mine') # open for update
3258\& or die "Can't open 'dbase.mine' for update: $!";
3259.Ve
3260.Sp
3261.Vb 2
3262\& open(DBASE, '+<dbase.mine') # ditto
3263\& or die "Can't open 'dbase.mine' for update: $!";
3264.Ve
3265.Sp
3266.Vb 2
3267\& open(ARTICLE, '-|', "caesar <$article") # decrypt article
3268\& or die "Can't start caesar: $!";
3269.Ve
3270.Sp
3271.Vb 2
3272\& open(ARTICLE, "caesar <$article |") # ditto
3273\& or die "Can't start caesar: $!";
3274.Ve
3275.Sp
3276.Vb 2
3277\& open(EXTRACT, "|sort >/tmp/Tmp$$") # $$ is our process id
3278\& or die "Can't start sort: $!";
3279.Ve
3280.Sp
3281.Vb 4
3282\& # in memory files
3283\& open(MEMORY,'>', \e$var)
3284\& or die "Can't open memory file: $!";
3285\& print MEMORY "foo!\en"; # output will end up in $var
3286.Ve
3287.Sp
3288.Vb 1
3289\& # process argument list of files along with any includes
3290.Ve
3291.Sp
3292.Vb 3
3293\& foreach $file (@ARGV) {
3294\& process($file, 'fh00');
3295\& }
3296.Ve
3297.Sp
3298.Vb 7
3299\& sub process {
3300\& my($filename, $input) = @_;
3301\& $input++; # this is a string increment
3302\& unless (open($input, $filename)) {
3303\& print STDERR "Can't open $filename: $!\en";
3304\& return;
3305\& }
3306.Ve
3307.Sp
3308.Vb 9
3309\& local $_;
3310\& while (<$input>) { # note use of indirection
3311\& if (/^#include "(.*)"/) {
3312\& process($1, $input);
3313\& next;
3314\& }
3315\& #... # whatever
3316\& }
3317\& }
3318.Ve
3319.Sp
3320You may also, in the Bourne shell tradition, specify an \s-1EXPR\s0 beginning
3321with \f(CW'>&'\fR, in which case the rest of the string is interpreted as the
3322name of a filehandle (or file descriptor, if numeric) to be
3323duped and opened. You may use \f(CW\*(C`&\*(C'\fR after \f(CW\*(C`>\*(C'\fR, \f(CW\*(C`>>\*(C'\fR,
3324\&\f(CW\*(C`<\*(C'\fR, \f(CW\*(C`+>\*(C'\fR, \f(CW\*(C`+>>\*(C'\fR, and \f(CW\*(C`+<\*(C'\fR. The
3325mode you specify should match the mode of the original filehandle.
3326(Duping a filehandle does not take into account any existing contents of
3327\&\s-1IO\s0 buffers.) If you use the 3 arg form then you can pass either a number,
3328the name of a filehandle or the normal \*(L"reference to a glob\*(R".
3329.Sp
3330Here is a script that saves, redirects, and restores \f(CW\*(C`STDOUT\*(C'\fR and
3331\&\f(CW\*(C`STDERR\*(C'\fR using various methods:
3332.Sp
3333.Vb 3
3334\& #!/usr/bin/perl
3335\& open my $oldout, ">&STDOUT" or die "Can't dup STDOUT: $!";
3336\& open OLDERR, ">&", \e*STDERR or die "Can't dup STDERR: $!";
3337.Ve
3338.Sp
3339.Vb 2
3340\& open STDOUT, '>', "foo.out" or die "Can't redirect STDOUT: $!";
3341\& open STDERR, ">&STDOUT" or die "Can't dup STDOUT: $!";
3342.Ve
3343.Sp
3344.Vb 2
3345\& select STDERR; $| = 1; # make unbuffered
3346\& select STDOUT; $| = 1; # make unbuffered
3347.Ve
3348.Sp
3349.Vb 2
3350\& print STDOUT "stdout 1\en"; # this works for
3351\& print STDERR "stderr 1\en"; # subprocesses too
3352.Ve
3353.Sp
3354.Vb 2
3355\& close STDOUT;
3356\& close STDERR;
3357.Ve
3358.Sp
3359.Vb 2
3360\& open STDOUT, ">&", $oldout or die "Can't dup \e$oldout: $!";
3361\& open STDERR, ">&OLDERR" or die "Can't dup OLDERR: $!";
3362.Ve
3363.Sp
3364.Vb 2
3365\& print STDOUT "stdout 2\en";
3366\& print STDERR "stderr 2\en";
3367.Ve
3368.Sp
3369If you specify \f(CW'<&=N'\fR, where \f(CW\*(C`N\*(C'\fR is a number, then Perl will
3370do an equivalent of C's \f(CW\*(C`fdopen\*(C'\fR of that file descriptor; this is
3371more parsimonious of file descriptors. For example:
3372.Sp
3373.Vb 1
3374\& open(FILEHANDLE, "<&=$fd")
3375.Ve
3376.Sp
3377or
3378.Sp
3379.Vb 1
3380\& open(FILEHANDLE, "<&=", $fd)
3381.Ve
3382.Sp
3383Note that if Perl is using the standard C libraries' \fIfdopen()\fR then on
3384many \s-1UNIX\s0 systems, \fIfdopen()\fR is known to fail when file descriptors
3385exceed a certain value, typically 255. If you need more file
3386descriptors than that, consider rebuilding Perl to use the \f(CW\*(C`PerlIO\*(C'\fR.
3387.Sp
3388You can see whether Perl has been compiled with PerlIO or not by
3389running \f(CW\*(C`perl \-V\*(C'\fR and looking for \f(CW\*(C`useperlio=\*(C'\fR line. If \f(CW\*(C`useperlio\*(C'\fR
3390is \f(CW\*(C`define\*(C'\fR, you have PerlIO, otherwise you don't.
3391.Sp
3392If you open a pipe on the command \f(CW'\-'\fR, i.e., either \f(CW'|\-'\fR or \f(CW'\-|'\fR
3393with 2\-arguments (or 1\-argument) form of \fIopen()\fR, then
3394there is an implicit fork done, and the return value of open is the pid
3395of the child within the parent process, and \f(CW0\fR within the child
3396process. (Use \f(CW\*(C`defined($pid)\*(C'\fR to determine whether the open was successful.)
3397The filehandle behaves normally for the parent, but i/o to that
3398filehandle is piped from/to the \s-1STDOUT/STDIN\s0 of the child process.
3399In the child process the filehandle isn't opened\*(--i/o happens from/to
3400the new \s-1STDOUT\s0 or \s-1STDIN\s0. Typically this is used like the normal
3401piped open when you want to exercise more control over just how the
3402pipe command gets executed, such as when you are running setuid, and
3403don't want to have to scan shell commands for metacharacters.
3404The following triples are more or less equivalent:
3405.Sp
3406.Vb 4
3407\& open(FOO, "|tr '[a-z]' '[A-Z]'");
3408\& open(FOO, '|-', "tr '[a-z]' '[A-Z]'");
3409\& open(FOO, '|-') || exec 'tr', '[a-z]', '[A-Z]';
3410\& open(FOO, '|-', "tr", '[a-z]', '[A-Z]');
3411.Ve
3412.Sp
3413.Vb 4
3414\& open(FOO, "cat -n '$file'|");
3415\& open(FOO, '-|', "cat -n '$file'");
3416\& open(FOO, '-|') || exec 'cat', '-n', $file;
3417\& open(FOO, '-|', "cat", '-n', $file);
3418.Ve
3419.Sp
3420The last example in each block shows the pipe as \*(L"list form\*(R", which is
3421not yet supported on all platforms. A good rule of thumb is that if
3422your platform has true \f(CW\*(C`fork()\*(C'\fR (in other words, if your platform is
3423\&\s-1UNIX\s0) you can use the list form.
3424.Sp
3425See \*(L"Safe Pipe Opens\*(R" in perlipc for more examples of this.
3426.Sp
3427Beginning with v5.6.0, Perl will attempt to flush all files opened for
3428output before any operation that may do a fork, but this may not be
3429supported on some platforms (see perlport). To be safe, you may need
3430to set \f(CW$|\fR ($AUTOFLUSH in English) or call the \f(CW\*(C`autoflush()\*(C'\fR method
3431of \f(CW\*(C`IO::Handle\*(C'\fR on any open handles.
3432.Sp
3433On systems that support a close-on-exec flag on files, the flag will
3434be set for the newly opened file descriptor as determined by the value
3435of $^F. See \*(L"$^F\*(R" in perlvar.
3436.Sp
3437Closing any piped filehandle causes the parent process to wait for the
3438child to finish, and returns the status value in \f(CW$?\fR.
3439.Sp
3440The filename passed to 2\-argument (or 1\-argument) form of \fIopen()\fR will
3441have leading and trailing whitespace deleted, and the normal
3442redirection characters honored. This property, known as \*(L"magic open\*(R",
3443can often be used to good effect. A user could specify a filename of
3444\&\fI\*(L"rsh cat file |\*(R"\fR, or you could change certain filenames as needed:
3445.Sp
3446.Vb 2
3447\& $filename =~ s/(.*\e.gz)\es*$/gzip -dc < $1|/;
3448\& open(FH, $filename) or die "Can't open $filename: $!";
3449.Ve
3450.Sp
3451Use 3\-argument form to open a file with arbitrary weird characters in it,
3452.Sp
3453.Vb 1
3454\& open(FOO, '<', $file);
3455.Ve
3456.Sp
3457otherwise it's necessary to protect any leading and trailing whitespace:
3458.Sp
3459.Vb 2
3460\& $file =~ s#^(\es)#./$1#;
3461\& open(FOO, "< $file\e0");
3462.Ve
3463.Sp
3464(this may not work on some bizarre filesystems). One should
3465conscientiously choose between the \fImagic\fR and 3\-arguments form
3466of \fIopen()\fR:
3467.Sp
3468.Vb 1
3469\& open IN, $ARGV[0];
3470.Ve
3471.Sp
3472will allow the user to specify an argument of the form \f(CW"rsh cat file |"\fR,
3473but will not work on a filename which happens to have a trailing space, while
3474.Sp
3475.Vb 1
3476\& open IN, '<', $ARGV[0];
3477.Ve
3478.Sp
3479will have exactly the opposite restrictions.
3480.Sp
3481If you want a \*(L"real\*(R" C \f(CW\*(C`open\*(C'\fR (see \fIopen\fR\|(2) on your system), then you
3482should use the \f(CW\*(C`sysopen\*(C'\fR function, which involves no such magic (but
3483may use subtly different filemodes than Perl \fIopen()\fR, which is mapped
3484to C \fIfopen()\fR). This is
3485another way to protect your filenames from interpretation. For example:
3486.Sp
3487.Vb 7
3488\& use IO::Handle;
3489\& sysopen(HANDLE, $path, O_RDWR|O_CREAT|O_EXCL)
3490\& or die "sysopen $path: $!";
3491\& $oldfh = select(HANDLE); $| = 1; select($oldfh);
3492\& print HANDLE "stuff $$\en";
3493\& seek(HANDLE, 0, 0);
3494\& print "File contains: ", <HANDLE>;
3495.Ve
3496.Sp
3497Using the constructor from the \f(CW\*(C`IO::Handle\*(C'\fR package (or one of its
3498subclasses, such as \f(CW\*(C`IO::File\*(C'\fR or \f(CW\*(C`IO::Socket\*(C'\fR), you can generate anonymous
3499filehandles that have the scope of whatever variables hold references to
3500them, and automatically close whenever and however you leave that scope:
3501.Sp
3502.Vb 12
3503\& use IO::File;
3504\& #...
3505\& sub read_myfile_munged {
3506\& my $ALL = shift;
3507\& my $handle = new IO::File;
3508\& open($handle, "myfile") or die "myfile: $!";
3509\& $first = <$handle>
3510\& or return (); # Automatically closed here.
3511\& mung $first or die "mung failed"; # Or here.
3512\& return $first, <$handle> if $ALL; # Or here.
3513\& $first; # Or here.
3514\& }
3515.Ve
3516.Sp
3517See \*(L"seek\*(R" for some details about mixing reading and writing.
3518.IP "opendir \s-1DIRHANDLE\s0,EXPR" 8
3519.IX Item "opendir DIRHANDLE,EXPR"
3520Opens a directory named \s-1EXPR\s0 for processing by \f(CW\*(C`readdir\*(C'\fR, \f(CW\*(C`telldir\*(C'\fR,
3521\&\f(CW\*(C`seekdir\*(C'\fR, \f(CW\*(C`rewinddir\*(C'\fR, and \f(CW\*(C`closedir\*(C'\fR. Returns true if successful.
3522DIRHANDLEs have their own namespace separate from FILEHANDLEs.
3523.IP "ord \s-1EXPR\s0" 8
3524.IX Item "ord EXPR"
3525.PD 0
3526.IP "ord" 8
3527.IX Item "ord"
3528.PD
3529Returns the numeric (the native 8\-bit encoding, like \s-1ASCII\s0 or \s-1EBCDIC\s0,
3530or Unicode) value of the first character of \s-1EXPR\s0. If \s-1EXPR\s0 is omitted,
3531uses \f(CW$_\fR.
3532.Sp
3533For the reverse, see \*(L"chr\*(R".
3534See perlunicode and encoding for more about Unicode.
3535.IP "our \s-1EXPR\s0" 8
3536.IX Item "our EXPR"
3537.PD 0
3538.IP "our \s-1EXPR\s0 \s-1TYPE\s0" 8
3539.IX Item "our EXPR TYPE"
3540.IP "our \s-1EXPR\s0 : \s-1ATTRS\s0" 8
3541.IX Item "our EXPR : ATTRS"
3542.IP "our \s-1TYPE\s0 \s-1EXPR\s0 : \s-1ATTRS\s0" 8
3543.IX Item "our TYPE EXPR : ATTRS"
3544.PD
3545An \f(CW\*(C`our\*(C'\fR declares the listed variables to be valid globals within
3546the enclosing block, file, or \f(CW\*(C`eval\*(C'\fR. That is, it has the same
3547scoping rules as a \*(L"my\*(R" declaration, but does not create a local
3548variable. If more than one value is listed, the list must be placed
3549in parentheses. The \f(CW\*(C`our\*(C'\fR declaration has no semantic effect unless
3550\&\*(L"use strict vars\*(R" is in effect, in which case it lets you use the
3551declared global variable without qualifying it with a package name.
3552(But only within the lexical scope of the \f(CW\*(C`our\*(C'\fR declaration. In this
3553it differs from \*(L"use vars\*(R", which is package scoped.)
3554.Sp
3555An \f(CW\*(C`our\*(C'\fR declaration declares a global variable that will be visible
3556across its entire lexical scope, even across package boundaries. The
3557package in which the variable is entered is determined at the point
3558of the declaration, not at the point of use. This means the following
3559behavior holds:
3560.Sp
3561.Vb 3
3562\& package Foo;
3563\& our $bar; # declares $Foo::bar for rest of lexical scope
3564\& $bar = 20;
3565.Ve
3566.Sp
3567.Vb 2
3568\& package Bar;
3569\& print $bar; # prints 20
3570.Ve
3571.Sp
3572Multiple \f(CW\*(C`our\*(C'\fR declarations in the same lexical scope are allowed
3573if they are in different packages. If they happened to be in the same
3574package, Perl will emit warnings if you have asked for them.
3575.Sp
3576.Vb 4
3577\& use warnings;
3578\& package Foo;
3579\& our $bar; # declares $Foo::bar for rest of lexical scope
3580\& $bar = 20;
3581.Ve
3582.Sp
3583.Vb 3
3584\& package Bar;
3585\& our $bar = 30; # declares $Bar::bar for rest of lexical scope
3586\& print $bar; # prints 30
3587.Ve
3588.Sp
3589.Vb 1
3590\& our $bar; # emits warning
3591.Ve
3592.Sp
3593An \f(CW\*(C`our\*(C'\fR declaration may also have a list of attributes associated
3594with it.
3595.Sp
3596The exact semantics and interface of \s-1TYPE\s0 and \s-1ATTRS\s0 are still
3597evolving. \s-1TYPE\s0 is currently bound to the use of \f(CW\*(C`fields\*(C'\fR pragma,
3598and attributes are handled using the \f(CW\*(C`attributes\*(C'\fR pragma, or starting
3599from Perl 5.8.0 also via the \f(CW\*(C`Attribute::Handlers\*(C'\fR module. See
3600\&\*(L"Private Variables via \fImy()\fR\*(R" in perlsub for details, and fields,
3601attributes, and Attribute::Handlers.
3602.Sp
3603The only currently recognized \f(CW\*(C`our()\*(C'\fR attribute is \f(CW\*(C`unique\*(C'\fR which
3604indicates that a single copy of the global is to be used by all
3605interpreters should the program happen to be running in a
3606multi-interpreter environment. (The default behaviour would be for
3607each interpreter to have its own copy of the global.) Examples:
3608.Sp
3609.Vb 3
3610\& our @EXPORT : unique = qw(foo);
3611\& our %EXPORT_TAGS : unique = (bar => [qw(aa bb cc)]);
3612\& our $VERSION : unique = "1.00";
3613.Ve
3614.Sp
3615Note that this attribute also has the effect of making the global
3616readonly when the first new interpreter is cloned (for example,
3617when the first new thread is created).
3618.Sp
3619Multi-interpreter environments can come to being either through the
3620\&\fIfork()\fR emulation on Windows platforms, or by embedding perl in a
3621multi-threaded application. The \f(CW\*(C`unique\*(C'\fR attribute does nothing in
3622all other environments.
3623.IP "pack \s-1TEMPLATE\s0,LIST" 8
3624.IX Item "pack TEMPLATE,LIST"
3625Takes a \s-1LIST\s0 of values and converts it into a string using the rules
3626given by the \s-1TEMPLATE\s0. The resulting string is the concatenation of
3627the converted values. Typically, each converted value looks
3628like its machine-level representation. For example, on 32\-bit machines
3629a converted integer may be represented by a sequence of 4 bytes.
3630.Sp
3631The \s-1TEMPLATE\s0 is a sequence of characters that give the order and type
3632of values, as follows:
3633.Sp
3634.Vb 3
3635\& a A string with arbitrary binary data, will be null padded.
3636\& A A text (ASCII) string, will be space padded.
3637\& Z A null terminated (ASCIZ) string, will be null padded.
3638.Ve
3639.Sp
3640.Vb 4
3641\& b A bit string (ascending bit order inside each byte, like vec()).
3642\& B A bit string (descending bit order inside each byte).
3643\& h A hex string (low nybble first).
3644\& H A hex string (high nybble first).
3645.Ve
3646.Sp
3647.Vb 2
3648\& c A signed char value.
3649\& C An unsigned char value. Only does bytes. See U for Unicode.
3650.Ve
3651.Sp
3652.Vb 5
3653\& s A signed short value.
3654\& S An unsigned short value.
3655\& (This 'short' is _exactly_ 16 bits, which may differ from
3656\& what a local C compiler calls 'short'. If you want
3657\& native-length shorts, use the '!' suffix.)
3658.Ve
3659.Sp
3660.Vb 6
3661\& i A signed integer value.
3662\& I An unsigned integer value.
3663\& (This 'integer' is _at_least_ 32 bits wide. Its exact
3664\& size depends on what a local C compiler calls 'int',
3665\& and may even be larger than the 'long' described in
3666\& the next item.)
3667.Ve
3668.Sp
3669.Vb 5
3670\& l A signed long value.
3671\& L An unsigned long value.
3672\& (This 'long' is _exactly_ 32 bits, which may differ from
3673\& what a local C compiler calls 'long'. If you want
3674\& native-length longs, use the '!' suffix.)
3675.Ve
3676.Sp
3677.Vb 6
3678\& n An unsigned short in "network" (big-endian) order.
3679\& N An unsigned long in "network" (big-endian) order.
3680\& v An unsigned short in "VAX" (little-endian) order.
3681\& V An unsigned long in "VAX" (little-endian) order.
3682\& (These 'shorts' and 'longs' are _exactly_ 16 bits and
3683\& _exactly_ 32 bits, respectively.)
3684.Ve
3685.Sp
3686.Vb 5
3687\& q A signed quad (64-bit) value.
3688\& Q An unsigned quad value.
3689\& (Quads are available only if your system supports 64-bit
3690\& integer values _and_ if Perl has been compiled to support those.
3691\& Causes a fatal error otherwise.)
3692.Ve
3693.Sp
3694.Vb 2
3695\& j A signed integer value (a Perl internal integer, IV).
3696\& J An unsigned integer value (a Perl internal unsigned integer, UV).
3697.Ve
3698.Sp
3699.Vb 2
3700\& f A single-precision float in the native format.
3701\& d A double-precision float in the native format.
3702.Ve
3703.Sp
3704.Vb 6
3705\& F A floating point value in the native native format
3706\& (a Perl internal floating point value, NV).
3707\& D A long double-precision float in the native format.
3708\& (Long doubles are available only if your system supports long
3709\& double values _and_ if Perl has been compiled to support those.
3710\& Causes a fatal error otherwise.)
3711.Ve
3712.Sp
3713.Vb 2
3714\& p A pointer to a null-terminated string.
3715\& P A pointer to a structure (fixed-length string).
3716.Ve
3717.Sp
3718.Vb 3
3719\& u A uuencoded string.
3720\& U A Unicode character number. Encodes to UTF-8 internally
3721\& (or UTF-EBCDIC in EBCDIC platforms).
3722.Ve
3723.Sp
3724.Vb 4
3725\& w A BER compressed integer. Its bytes represent an unsigned
3726\& integer in base 128, most significant digit first, with as
3727\& few digits as possible. Bit eight (the high bit) is set
3728\& on each byte except the last.
3729.Ve
3730.Sp
3731.Vb 4
3732\& x A null byte.
3733\& X Back up a byte.
3734\& @ Null fill to absolute position.
3735\& ( Start of a ()-group.
3736.Ve
3737.Sp
3738The following rules apply:
3739.RS 8
3740.IP "\(bu" 8
3741Each letter may optionally be followed by a number giving a repeat
3742count. With all types except \f(CW\*(C`a\*(C'\fR, \f(CW\*(C`A\*(C'\fR, \f(CW\*(C`Z\*(C'\fR, \f(CW\*(C`b\*(C'\fR, \f(CW\*(C`B\*(C'\fR, \f(CW\*(C`h\*(C'\fR,
3743\&\f(CW\*(C`H\*(C'\fR, \f(CW\*(C`@\*(C'\fR, \f(CW\*(C`x\*(C'\fR, \f(CW\*(C`X\*(C'\fR and \f(CW\*(C`P\*(C'\fR the pack function will gobble up that
3744many values from the \s-1LIST\s0. A \f(CW\*(C`*\*(C'\fR for the repeat count means to use
3745however many items are left, except for \f(CW\*(C`@\*(C'\fR, \f(CW\*(C`x\*(C'\fR, \f(CW\*(C`X\*(C'\fR, where it is
3746equivalent to \f(CW0\fR, and \f(CW\*(C`u\*(C'\fR, where it is equivalent to 1 (or 45, what
3747is the same). A numeric repeat count may optionally be enclosed in
3748brackets, as in \f(CW\*(C`pack 'C[80]', @arr\*(C'\fR.
3749.Sp
3750One can replace the numeric repeat count by a template enclosed in brackets;
3751then the packed length of this template in bytes is used as a count.
3752For example, \f(CW\*(C`x[L]\*(C'\fR skips a long (it skips the number of bytes in a long);
3753the template \f(CW\*(C`$t X[$t] $t\*(C'\fR \fIunpack()\fRs twice what \f(CW$t\fR unpacks.
3754If the template in brackets contains alignment commands (such as \f(CW\*(C`x![d]\*(C'\fR),
3755its packed length is calculated as if the start of the template has the maximal
3756possible alignment.
3757.Sp
3758When used with \f(CW\*(C`Z\*(C'\fR, \f(CW\*(C`*\*(C'\fR results in the addition of a trailing null
3759byte (so the packed result will be one longer than the byte \f(CW\*(C`length\*(C'\fR
3760of the item).
3761.Sp
3762The repeat count for \f(CW\*(C`u\*(C'\fR is interpreted as the maximal number of bytes
3763to encode per line of output, with 0 and 1 replaced by 45.
3764.IP "\(bu" 8
3765The \f(CW\*(C`a\*(C'\fR, \f(CW\*(C`A\*(C'\fR, and \f(CW\*(C`Z\*(C'\fR types gobble just one value, but pack it as a
3766string of length count, padding with nulls or spaces as necessary. When
3767unpacking, \f(CW\*(C`A\*(C'\fR strips trailing spaces and nulls, \f(CW\*(C`Z\*(C'\fR strips everything
3768after the first null, and \f(CW\*(C`a\*(C'\fR returns data verbatim. When packing,
3769\&\f(CW\*(C`a\*(C'\fR, and \f(CW\*(C`Z\*(C'\fR are equivalent.
3770.Sp
3771If the value-to-pack is too long, it is truncated. If too long and an
3772explicit count is provided, \f(CW\*(C`Z\*(C'\fR packs only \f(CW\*(C`$count\-1\*(C'\fR bytes, followed
3773by a null byte. Thus \f(CW\*(C`Z\*(C'\fR always packs a trailing null byte under
3774all circumstances.
3775.IP "\(bu" 8
3776Likewise, the \f(CW\*(C`b\*(C'\fR and \f(CW\*(C`B\*(C'\fR fields pack a string that many bits long.
3777Each byte of the input field of \fIpack()\fR generates 1 bit of the result.
3778Each result bit is based on the least-significant bit of the corresponding
3779input byte, i.e., on \f(CW\*(C`ord($byte)%2\*(C'\fR. In particular, bytes \f(CW"0"\fR and
3780\&\f(CW"1"\fR generate bits 0 and 1, as do bytes \f(CW"\e0"\fR and \f(CW"\e1"\fR.
3781.Sp
3782Starting from the beginning of the input string of \fIpack()\fR, each 8\-tuple
3783of bytes is converted to 1 byte of output. With format \f(CW\*(C`b\*(C'\fR
3784the first byte of the 8\-tuple determines the least-significant bit of a
3785byte, and with format \f(CW\*(C`B\*(C'\fR it determines the most-significant bit of
3786a byte.
3787.Sp
3788If the length of the input string is not exactly divisible by 8, the
3789remainder is packed as if the input string were padded by null bytes
3790at the end. Similarly, during \fIunpack()\fRing the \*(L"extra\*(R" bits are ignored.
3791.Sp
3792If the input string of \fIpack()\fR is longer than needed, extra bytes are ignored.
3793A \f(CW\*(C`*\*(C'\fR for the repeat count of \fIpack()\fR means to use all the bytes of
3794the input field. On \fIunpack()\fRing the bits are converted to a string
3795of \f(CW"0"\fRs and \f(CW"1"\fRs.
3796.IP "\(bu" 8
3797The \f(CW\*(C`h\*(C'\fR and \f(CW\*(C`H\*(C'\fR fields pack a string that many nybbles (4\-bit groups,
3798representable as hexadecimal digits, 0\-9a\-f) long.
3799.Sp
3800Each byte of the input field of \fIpack()\fR generates 4 bits of the result.
3801For non-alphabetical bytes the result is based on the 4 least-significant
3802bits of the input byte, i.e., on \f(CW\*(C`ord($byte)%16\*(C'\fR. In particular,
3803bytes \f(CW"0"\fR and \f(CW"1"\fR generate nybbles 0 and 1, as do bytes
3804\&\f(CW"\e0"\fR and \f(CW"\e1"\fR. For bytes \f(CW"a".."f"\fR and \f(CW"A".."F"\fR the result
3805is compatible with the usual hexadecimal digits, so that \f(CW"a"\fR and
3806\&\f(CW"A"\fR both generate the nybble \f(CW\*(C`0xa==10\*(C'\fR. The result for bytes
3807\&\f(CW"g".."z"\fR and \f(CW"G".."Z"\fR is not well\-defined.
3808.Sp
3809Starting from the beginning of the input string of \fIpack()\fR, each pair
3810of bytes is converted to 1 byte of output. With format \f(CW\*(C`h\*(C'\fR the
3811first byte of the pair determines the least-significant nybble of the
3812output byte, and with format \f(CW\*(C`H\*(C'\fR it determines the most-significant
3813nybble.
3814.Sp
3815If the length of the input string is not even, it behaves as if padded
3816by a null byte at the end. Similarly, during \fIunpack()\fRing the \*(L"extra\*(R"
3817nybbles are ignored.
3818.Sp
3819If the input string of \fIpack()\fR is longer than needed, extra bytes are ignored.
3820A \f(CW\*(C`*\*(C'\fR for the repeat count of \fIpack()\fR means to use all the bytes of
3821the input field. On \fIunpack()\fRing the bits are converted to a string
3822of hexadecimal digits.
3823.IP "\(bu" 8
3824The \f(CW\*(C`p\*(C'\fR type packs a pointer to a null-terminated string. You are
3825responsible for ensuring the string is not a temporary value (which can
3826potentially get deallocated before you get around to using the packed result).
3827The \f(CW\*(C`P\*(C'\fR type packs a pointer to a structure of the size indicated by the
3828length. A \s-1NULL\s0 pointer is created if the corresponding value for \f(CW\*(C`p\*(C'\fR or
3829\&\f(CW\*(C`P\*(C'\fR is \f(CW\*(C`undef\*(C'\fR, similarly for \fIunpack()\fR.
3830.IP "\(bu" 8
3831The \f(CW\*(C`/\*(C'\fR template character allows packing and unpacking of strings where
3832the packed structure contains a byte count followed by the string itself.
3833You write \fIlength-item\fR\f(CW\*(C`/\*(C'\fR\fIstring-item\fR.
3834.Sp
3835The \fIlength-item\fR can be any \f(CW\*(C`pack\*(C'\fR template letter, and describes
3836how the length value is packed. The ones likely to be of most use are
3837integer-packing ones like \f(CW\*(C`n\*(C'\fR (for Java strings), \f(CW\*(C`w\*(C'\fR (for \s-1ASN\s0.1 or
3838\&\s-1SNMP\s0) and \f(CW\*(C`N\*(C'\fR (for Sun \s-1XDR\s0).
3839.Sp
3840The \fIstring-item\fR must, at present, be \f(CW"A*"\fR, \f(CW"a*"\fR or \f(CW"Z*"\fR.
3841For \f(CW\*(C`unpack\*(C'\fR the length of the string is obtained from the \fIlength-item\fR,
3842but if you put in the '*' it will be ignored.
3843.Sp
3844.Vb 3
3845\& unpack 'C/a', "\e04Gurusamy"; gives 'Guru'
3846\& unpack 'a3/A* A*', '007 Bond J '; gives (' Bond','J')
3847\& pack 'n/a* w/a*','hello,','world'; gives "\e000\e006hello,\e005world"
3848.Ve
3849.Sp
3850The \fIlength-item\fR is not returned explicitly from \f(CW\*(C`unpack\*(C'\fR.
3851.Sp
3852Adding a count to the \fIlength-item\fR letter is unlikely to do anything
3853useful, unless that letter is \f(CW\*(C`A\*(C'\fR, \f(CW\*(C`a\*(C'\fR or \f(CW\*(C`Z\*(C'\fR. Packing with a
3854\&\fIlength-item\fR of \f(CW\*(C`a\*(C'\fR or \f(CW\*(C`Z\*(C'\fR may introduce \f(CW"\e000"\fR characters,
3855which Perl does not regard as legal in numeric strings.
3856.IP "\(bu" 8
3857The integer types \f(CW\*(C`s\*(C'\fR, \f(CW\*(C`S\*(C'\fR, \f(CW\*(C`l\*(C'\fR, and \f(CW\*(C`L\*(C'\fR may be
3858immediately followed by a \f(CW\*(C`!\*(C'\fR suffix to signify native shorts or
3859longs\*(--as you can see from above for example a bare \f(CW\*(C`l\*(C'\fR does mean
3860exactly 32 bits, the native \f(CW\*(C`long\*(C'\fR (as seen by the local C compiler)
3861may be larger. This is an issue mainly in 64\-bit platforms. You can
3862see whether using \f(CW\*(C`!\*(C'\fR makes any difference by
3863.Sp
3864.Vb 2
3865\& print length(pack("s")), " ", length(pack("s!")), "\en";
3866\& print length(pack("l")), " ", length(pack("l!")), "\en";
3867.Ve
3868.Sp
3869\&\f(CW\*(C`i!\*(C'\fR and \f(CW\*(C`I!\*(C'\fR also work but only because of completeness;
3870they are identical to \f(CW\*(C`i\*(C'\fR and \f(CW\*(C`I\*(C'\fR.
3871.Sp
3872The actual sizes (in bytes) of native shorts, ints, longs, and long
3873longs on the platform where Perl was built are also available via
3874Config:
3875.Sp
3876.Vb 5
3877\& use Config;
3878\& print $Config{shortsize}, "\en";
3879\& print $Config{intsize}, "\en";
3880\& print $Config{longsize}, "\en";
3881\& print $Config{longlongsize}, "\en";
3882.Ve
3883.Sp
3884(The \f(CW$Config{longlongsize}\fR will be undefine if your system does
3885not support long longs.)
3886.IP "\(bu" 8
3887The integer formats \f(CW\*(C`s\*(C'\fR, \f(CW\*(C`S\*(C'\fR, \f(CW\*(C`i\*(C'\fR, \f(CW\*(C`I\*(C'\fR, \f(CW\*(C`l\*(C'\fR, \f(CW\*(C`L\*(C'\fR, \f(CW\*(C`j\*(C'\fR, and \f(CW\*(C`J\*(C'\fR
3888are inherently non-portable between processors and operating systems
3889because they obey the native byteorder and endianness. For example a
38904\-byte integer 0x12345678 (305419896 decimal) would be ordered natively
3891(arranged in and handled by the \s-1CPU\s0 registers) into bytes as
3892.Sp
3893.Vb 2
3894\& 0x12 0x34 0x56 0x78 # big-endian
3895\& 0x78 0x56 0x34 0x12 # little-endian
3896.Ve
3897.Sp
3898Basically, the Intel and \s-1VAX\s0 CPUs are little\-endian, while everybody
3899else, for example Motorola m68k/88k, \s-1PPC\s0, Sparc, \s-1HP\s0 \s-1PA\s0, Power, and
3900Cray are big\-endian. Alpha and \s-1MIPS\s0 can be either: Digital/Compaq
3901used/uses them in little-endian mode; SGI/Cray uses them in big-endian
3902mode.
3903.Sp
3904The names `big\-endian' and `little\-endian' are comic references to
3905the classic \*(L"Gulliver's Travels\*(R" (via the paper \*(L"On Holy Wars and a
3906Plea for Peace\*(R" by Danny Cohen, \s-1USC/ISI\s0 \s-1IEN\s0 137, April 1, 1980) and
3907the egg-eating habits of the Lilliputians.
3908.Sp
3909Some systems may have even weirder byte orders such as
3910.Sp
3911.Vb 2
3912\& 0x56 0x78 0x12 0x34
3913\& 0x34 0x12 0x78 0x56
3914.Ve
3915.Sp
3916You can see your system's preference with
3917.Sp
3918.Vb 2
3919\& print join(" ", map { sprintf "%#02x", $_ }
3920\& unpack("C*",pack("L",0x12345678))), "\en";
3921.Ve
3922.Sp
3923The byteorder on the platform where Perl was built is also available
3924via Config:
3925.Sp
3926.Vb 2
3927\& use Config;
3928\& print $Config{byteorder}, "\en";
3929.Ve
3930.Sp
3931Byteorders \f(CW'1234'\fR and \f(CW'12345678'\fR are little\-endian, \f(CW'4321'\fR
3932and \f(CW'87654321'\fR are big\-endian.
3933.Sp
3934If you want portable packed integers use the formats \f(CW\*(C`n\*(C'\fR, \f(CW\*(C`N\*(C'\fR,
3935\&\f(CW\*(C`v\*(C'\fR, and \f(CW\*(C`V\*(C'\fR, their byte endianness and size are known.
3936See also perlport.
3937.IP "\(bu" 8
3938Real numbers (floats and doubles) are in the native machine format only;
3939due to the multiplicity of floating formats around, and the lack of a
3940standard \*(L"network\*(R" representation, no facility for interchange has been
3941made. This means that packed floating point data written on one machine
3942may not be readable on another \- even if both use \s-1IEEE\s0 floating point
3943arithmetic (as the endian-ness of the memory representation is not part
3944of the \s-1IEEE\s0 spec). See also perlport.
3945.Sp
3946Note that Perl uses doubles internally for all numeric calculation, and
3947converting from double into float and thence back to double again will
3948lose precision (i.e., \f(CW\*(C`unpack("f", pack("f", $foo)\*(C'\fR) will not in general
3949equal \f(CW$foo\fR).
3950.IP "\(bu" 8
3951If the pattern begins with a \f(CW\*(C`U\*(C'\fR, the resulting string will be treated
3952as Unicode\-encoded. You can force \s-1UTF8\s0 encoding on in a string with an
3953initial \f(CW\*(C`U0\*(C'\fR, and the bytes that follow will be interpreted as Unicode
3954characters. If you don't want this to happen, you can begin your pattern
3955with \f(CW\*(C`C0\*(C'\fR (or anything else) to force Perl not to \s-1UTF8\s0 encode your
3956string, and then follow this with a \f(CW\*(C`U*\*(C'\fR somewhere in your pattern.
3957.IP "\(bu" 8
3958You must yourself do any alignment or padding by inserting for example
3959enough \f(CW'x'\fRes while packing. There is no way to \fIpack()\fR and \fIunpack()\fR
3960could know where the bytes are going to or coming from. Therefore
3961\&\f(CW\*(C`pack\*(C'\fR (and \f(CW\*(C`unpack\*(C'\fR) handle their output and input as flat
3962sequences of bytes.
3963.IP "\(bu" 8
3964A ()\-group is a sub-TEMPLATE enclosed in parentheses. A group may
3965take a repeat count, both as postfix, and via the \f(CW\*(C`/\*(C'\fR template
3966character.
3967.IP "\(bu" 8
3968\&\f(CW\*(C`x\*(C'\fR and \f(CW\*(C`X\*(C'\fR accept \f(CW\*(C`!\*(C'\fR modifier. In this case they act as
3969alignment commands: they jump forward/back to the closest position
3970aligned at a multiple of \f(CW\*(C`count\*(C'\fR bytes. For example, to \fIpack()\fR or
3971\&\fIunpack()\fR C's \f(CW\*(C`struct {char c; double d; char cc[2]}\*(C'\fR one may need to
3972use the template \f(CW\*(C`C x![d] d C[2]\*(C'\fR; this assumes that doubles must be
3973aligned on the double's size.
3974.Sp
3975For alignment commands \f(CW\*(C`count\*(C'\fR of 0 is equivalent to \f(CW\*(C`count\*(C'\fR of 1;
3976both result in no\-ops.
3977.IP "\(bu" 8
3978A comment in a \s-1TEMPLATE\s0 starts with \f(CW\*(C`#\*(C'\fR and goes to the end of line.
3979.IP "\(bu" 8
3980If \s-1TEMPLATE\s0 requires more arguments to \fIpack()\fR than actually given, \fIpack()\fR
3981assumes additional \f(CW""\fR arguments. If \s-1TEMPLATE\s0 requires less arguments
3982to \fIpack()\fR than actually given, extra arguments are ignored.
3983.RE
3984.RS 8
3985.Sp
3986Examples:
3987.Sp
3988.Vb 6
3989\& $foo = pack("CCCC",65,66,67,68);
3990\& # foo eq "ABCD"
3991\& $foo = pack("C4",65,66,67,68);
3992\& # same thing
3993\& $foo = pack("U4",0x24b6,0x24b7,0x24b8,0x24b9);
3994\& # same thing with Unicode circled letters
3995.Ve
3996.Sp
3997.Vb 2
3998\& $foo = pack("ccxxcc",65,66,67,68);
3999\& # foo eq "AB\e0\e0CD"
4000.Ve
4001.Sp
4002.Vb 4
4003\& # note: the above examples featuring "C" and "c" are true
4004\& # only on ASCII and ASCII-derived systems such as ISO Latin 1
4005\& # and UTF-8. In EBCDIC the first example would be
4006\& # $foo = pack("CCCC",193,194,195,196);
4007.Ve
4008.Sp
4009.Vb 3
4010\& $foo = pack("s2",1,2);
4011\& # "\e1\e0\e2\e0" on little-endian
4012\& # "\e0\e1\e0\e2" on big-endian
4013.Ve
4014.Sp
4015.Vb 2
4016\& $foo = pack("a4","abcd","x","y","z");
4017\& # "abcd"
4018.Ve
4019.Sp
4020.Vb 2
4021\& $foo = pack("aaaa","abcd","x","y","z");
4022\& # "axyz"
4023.Ve
4024.Sp
4025.Vb 2
4026\& $foo = pack("a14","abcdefg");
4027\& # "abcdefg\e0\e0\e0\e0\e0\e0\e0"
4028.Ve
4029.Sp
4030.Vb 2
4031\& $foo = pack("i9pl", gmtime);
4032\& # a real struct tm (on my system anyway)
4033.Ve
4034.Sp
4035.Vb 3
4036\& $utmp_template = "Z8 Z8 Z16 L";
4037\& $utmp = pack($utmp_template, @utmp1);
4038\& # a struct utmp (BSDish)
4039.Ve
4040.Sp
4041.Vb 2
4042\& @utmp2 = unpack($utmp_template, $utmp);
4043\& # "@utmp1" eq "@utmp2"
4044.Ve
4045.Sp
4046.Vb 3
4047\& sub bintodec {
4048\& unpack("N", pack("B32", substr("0" x 32 . shift, -32)));
4049\& }
4050.Ve
4051.Sp
4052.Vb 5
4053\& $foo = pack('sx2l', 12, 34);
4054\& # short 12, two zero bytes padding, long 34
4055\& $bar = pack('s@4l', 12, 34);
4056\& # short 12, zero fill to position 4, long 34
4057\& # $foo eq $bar
4058.Ve
4059.Sp
4060The same template may generally also be used in \fIunpack()\fR.
4061.RE
4062.IP "package \s-1NAMESPACE\s0" 8
4063.IX Item "package NAMESPACE"
4064.PD 0
4065.IP "package" 8
4066.IX Item "package"
4067.PD
4068Declares the compilation unit as being in the given namespace. The scope
4069of the package declaration is from the declaration itself through the end
4070of the enclosing block, file, or eval (the same as the \f(CW\*(C`my\*(C'\fR operator).
4071All further unqualified dynamic identifiers will be in this namespace.
4072A package statement affects only dynamic variables\*(--including those
4073you've used \f(CW\*(C`local\*(C'\fR on\*(--but \fInot\fR lexical variables, which are created
4074with \f(CW\*(C`my\*(C'\fR. Typically it would be the first declaration in a file to
4075be included by the \f(CW\*(C`require\*(C'\fR or \f(CW\*(C`use\*(C'\fR operator. You can switch into a
4076package in more than one place; it merely influences which symbol table
4077is used by the compiler for the rest of that block. You can refer to
4078variables and filehandles in other packages by prefixing the identifier
4079with the package name and a double colon: \f(CW$Package::Variable\fR.
4080If the package name is null, the \f(CW\*(C`main\*(C'\fR package as assumed. That is,
4081\&\f(CW$::sail\fR is equivalent to \f(CW$main::sail\fR (as well as to \f(CW$main'sail\fR,
4082still seen in older code).
4083.Sp
4084If \s-1NAMESPACE\s0 is omitted, then there is no current package, and all
4085identifiers must be fully qualified or lexicals. However, you are
4086strongly advised not to make use of this feature. Its use can cause
4087unexpected behaviour, even crashing some versions of Perl. It is
4088deprecated, and will be removed from a future release.
4089.Sp
4090See \*(L"Packages\*(R" in perlmod for more information about packages, modules,
4091and classes. See perlsub for other scoping issues.
4092.IP "pipe \s-1READHANDLE\s0,WRITEHANDLE" 8
4093.IX Item "pipe READHANDLE,WRITEHANDLE"
4094Opens a pair of connected pipes like the corresponding system call.
4095Note that if you set up a loop of piped processes, deadlock can occur
4096unless you are very careful. In addition, note that Perl's pipes use
4097\&\s-1IO\s0 buffering, so you may need to set \f(CW$|\fR to flush your \s-1WRITEHANDLE\s0
4098after each command, depending on the application.
4099.Sp
4100See IPC::Open2, IPC::Open3, and \*(L"Bidirectional Communication\*(R" in perlipc
4101for examples of such things.
4102.Sp
4103On systems that support a close-on-exec flag on files, the flag will be set
4104for the newly opened file descriptors as determined by the value of $^F.
4105See \*(L"$^F\*(R" in perlvar.
4106.IP "pop \s-1ARRAY\s0" 8
4107.IX Item "pop ARRAY"
4108.PD 0
4109.IP "pop" 8
4110.IX Item "pop"
4111.PD
4112Pops and returns the last value of the array, shortening the array by
4113one element. Has an effect similar to
4114.Sp
4115.Vb 1
4116\& $ARRAY[$#ARRAY--]
4117.Ve
4118.Sp
4119If there are no elements in the array, returns the undefined value
4120(although this may happen at other times as well). If \s-1ARRAY\s0 is
4121omitted, pops the \f(CW@ARGV\fR array in the main program, and the \f(CW@_\fR
4122array in subroutines, just like \f(CW\*(C`shift\*(C'\fR.
4123.IP "pos \s-1SCALAR\s0" 8
4124.IX Item "pos SCALAR"
4125.PD 0
4126.IP "pos" 8
4127.IX Item "pos"
4128.PD
4129Returns the offset of where the last \f(CW\*(C`m//g\*(C'\fR search left off for the variable
4130in question (\f(CW$_\fR is used when the variable is not specified). May be
4131modified to change that offset. Such modification will also influence
4132the \f(CW\*(C`\eG\*(C'\fR zero-width assertion in regular expressions. See perlre and
4133perlop.
4134.IP "print \s-1FILEHANDLE\s0 \s-1LIST\s0" 8
4135.IX Item "print FILEHANDLE LIST"
4136.PD 0
4137.IP "print \s-1LIST\s0" 8
4138.IX Item "print LIST"
4139.IP "print" 8
4140.IX Item "print"
4141.PD
4142Prints a string or a list of strings. Returns true if successful.
4143\&\s-1FILEHANDLE\s0 may be a scalar variable name, in which case the variable
4144contains the name of or a reference to the filehandle, thus introducing
4145one level of indirection. (\s-1NOTE:\s0 If \s-1FILEHANDLE\s0 is a variable and
4146the next token is a term, it may be misinterpreted as an operator
4147unless you interpose a \f(CW\*(C`+\*(C'\fR or put parentheses around the arguments.)
4148If \s-1FILEHANDLE\s0 is omitted, prints by default to standard output (or
4149to the last selected output channel\*(--see \*(L"select\*(R"). If \s-1LIST\s0 is
4150also omitted, prints \f(CW$_\fR to the currently selected output channel.
4151To set the default output channel to something other than \s-1STDOUT\s0
4152use the select operation. The current value of \f(CW$,\fR (if any) is
4153printed between each \s-1LIST\s0 item. The current value of \f(CW\*(C`$\e\*(C'\fR (if
4154any) is printed after the entire \s-1LIST\s0 has been printed. Because
4155print takes a \s-1LIST\s0, anything in the \s-1LIST\s0 is evaluated in list
4156context, and any subroutine that you call will have one or more of
4157its expressions evaluated in list context. Also be careful not to
4158follow the print keyword with a left parenthesis unless you want
4159the corresponding right parenthesis to terminate the arguments to
4160the print\*(--interpose a \f(CW\*(C`+\*(C'\fR or put parentheses around all the
4161arguments.
4162.Sp
4163Note that if you're storing \s-1FILEHANDLES\s0 in an array or other expression,
4164you will have to use a block returning its value instead:
4165.Sp
4166.Vb 2
4167\& print { $files[$i] } "stuff\en";
4168\& print { $OK ? STDOUT : STDERR } "stuff\en";
4169.Ve
4170.IP "printf \s-1FILEHANDLE\s0 \s-1FORMAT\s0, \s-1LIST\s0" 8
4171.IX Item "printf FILEHANDLE FORMAT, LIST"
4172.PD 0
4173.IP "printf \s-1FORMAT\s0, \s-1LIST\s0" 8
4174.IX Item "printf FORMAT, LIST"
4175.PD
4176Equivalent to \f(CW\*(C`print FILEHANDLE sprintf(FORMAT, LIST)\*(C'\fR, except that \f(CW\*(C`$\e\*(C'\fR
4177(the output record separator) is not appended. The first argument
4178of the list will be interpreted as the \f(CW\*(C`printf\*(C'\fR format. See \f(CW\*(C`sprintf\*(C'\fR
4179for an explanation of the format argument. If \f(CW\*(C`use locale\*(C'\fR is in effect,
4180the character used for the decimal point in formatted real numbers is
4181affected by the \s-1LC_NUMERIC\s0 locale. See perllocale.
4182.Sp
4183Don't fall into the trap of using a \f(CW\*(C`printf\*(C'\fR when a simple
4184\&\f(CW\*(C`print\*(C'\fR would do. The \f(CW\*(C`print\*(C'\fR is more efficient and less
4185error prone.
4186.IP "prototype \s-1FUNCTION\s0" 8
4187.IX Item "prototype FUNCTION"
4188Returns the prototype of a function as a string (or \f(CW\*(C`undef\*(C'\fR if the
4189function has no prototype). \s-1FUNCTION\s0 is a reference to, or the name of,
4190the function whose prototype you want to retrieve.
4191.Sp
4192If \s-1FUNCTION\s0 is a string starting with \f(CW\*(C`CORE::\*(C'\fR, the rest is taken as a
4193name for Perl builtin. If the builtin is not \fIoverridable\fR (such as
4194\&\f(CW\*(C`qw//\*(C'\fR) or its arguments cannot be expressed by a prototype (such as
4195\&\f(CW\*(C`system\*(C'\fR) returns \f(CW\*(C`undef\*(C'\fR because the builtin does not really behave
4196like a Perl function. Otherwise, the string describing the equivalent
4197prototype is returned.
4198.IP "push \s-1ARRAY\s0,LIST" 8
4199.IX Item "push ARRAY,LIST"
4200Treats \s-1ARRAY\s0 as a stack, and pushes the values of \s-1LIST\s0
4201onto the end of \s-1ARRAY\s0. The length of \s-1ARRAY\s0 increases by the length of
4202\&\s-1LIST\s0. Has the same effect as
4203.Sp
4204.Vb 3
4205\& for $value (LIST) {
4206\& $ARRAY[++$#ARRAY] = $value;
4207\& }
4208.Ve
4209.Sp
4210but is more efficient. Returns the new number of elements in the array.
4211.IP "q/STRING/" 8
4212.IX Item "q/STRING/"
4213.PD 0
4214.IP "qq/STRING/" 8
4215.IX Item "qq/STRING/"
4216.IP "qr/STRING/" 8
4217.IX Item "qr/STRING/"
4218.IP "qx/STRING/" 8
4219.IX Item "qx/STRING/"
4220.IP "qw/STRING/" 8
4221.IX Item "qw/STRING/"
4222.PD
4223Generalized quotes. See \*(L"Regexp Quote-Like Operators\*(R" in perlop.
4224.IP "quotemeta \s-1EXPR\s0" 8
4225.IX Item "quotemeta EXPR"
4226.PD 0
4227.IP "quotemeta" 8
4228.IX Item "quotemeta"
4229.PD
4230Returns the value of \s-1EXPR\s0 with all non\-\*(L"word\*(R"
4231characters backslashed. (That is, all characters not matching
4232\&\f(CW\*(C`/[A\-Za\-z_0\-9]/\*(C'\fR will be preceded by a backslash in the
4233returned string, regardless of any locale settings.)
4234This is the internal function implementing
4235the \f(CW\*(C`\eQ\*(C'\fR escape in double-quoted strings.
4236.Sp
4237If \s-1EXPR\s0 is omitted, uses \f(CW$_\fR.
4238.IP "rand \s-1EXPR\s0" 8
4239.IX Item "rand EXPR"
4240.PD 0
4241.IP "rand" 8
4242.IX Item "rand"
4243.PD
4244Returns a random fractional number greater than or equal to \f(CW0\fR and less
4245than the value of \s-1EXPR\s0. (\s-1EXPR\s0 should be positive.) If \s-1EXPR\s0 is
4246omitted, the value \f(CW1\fR is used. Currently \s-1EXPR\s0 with the value \f(CW0\fR is
4247also special-cased as \f(CW1\fR \- this has not been documented before perl 5.8.0
4248and is subject to change in future versions of perl. Automatically calls
4249\&\f(CW\*(C`srand\*(C'\fR unless \f(CW\*(C`srand\*(C'\fR has already been called. See also \f(CW\*(C`srand\*(C'\fR.
4250.Sp
4251Apply \f(CW\*(C`int()\*(C'\fR to the value returned by \f(CW\*(C`rand()\*(C'\fR if you want random
4252integers instead of random fractional numbers. For example,
4253.Sp
4254.Vb 1
4255\& int(rand(10))
4256.Ve
4257.Sp
4258returns a random integer between \f(CW0\fR and \f(CW9\fR, inclusive.
4259.Sp
4260(Note: If your rand function consistently returns numbers that are too
4261large or too small, then your version of Perl was probably compiled
4262with the wrong number of \s-1RANDBITS\s0.)
4263.IP "read \s-1FILEHANDLE\s0,SCALAR,LENGTH,OFFSET" 8
4264.IX Item "read FILEHANDLE,SCALAR,LENGTH,OFFSET"
4265.PD 0
4266.IP "read \s-1FILEHANDLE\s0,SCALAR,LENGTH" 8
4267.IX Item "read FILEHANDLE,SCALAR,LENGTH"
4268.PD
4269Attempts to read \s-1LENGTH\s0 \fIcharacters\fR of data into variable \s-1SCALAR\s0
4270from the specified \s-1FILEHANDLE\s0. Returns the number of characters
4271actually read, \f(CW0\fR at end of file, or undef if there was an error.
4272\&\s-1SCALAR\s0 will be grown or shrunk to the length actually read. If \s-1SCALAR\s0
4273needs growing, the new bytes will be zero bytes. An \s-1OFFSET\s0 may be
4274specified to place the read data into some other place in \s-1SCALAR\s0 than
4275the beginning. The call is actually implemented in terms of either
4276Perl's or system's \fIfread()\fR call. To get a true \fIread\fR\|(2) system call,
4277see \f(CW\*(C`sysread\*(C'\fR.
4278.Sp
4279Note the \fIcharacters\fR: depending on the status of the filehandle,
4280either (8\-bit) bytes or characters are read. By default all
4281filehandles operate on bytes, but for example if the filehandle has
4282been opened with the \f(CW\*(C`:utf8\*(C'\fR I/O layer (see \*(L"open\*(R", and the \f(CW\*(C`open\*(C'\fR
4283pragma, open), the I/O will operate on characters, not bytes.
4284.IP "readdir \s-1DIRHANDLE\s0" 8
4285.IX Item "readdir DIRHANDLE"
4286Returns the next directory entry for a directory opened by \f(CW\*(C`opendir\*(C'\fR.
4287If used in list context, returns all the rest of the entries in the
4288directory. If there are no more entries, returns an undefined value in
4289scalar context or a null list in list context.
4290.Sp
4291If you're planning to filetest the return values out of a \f(CW\*(C`readdir\*(C'\fR, you'd
4292better prepend the directory in question. Otherwise, because we didn't
4293\&\f(CW\*(C`chdir\*(C'\fR there, it would have been testing the wrong file.
4294.Sp
4295.Vb 3
4296\& opendir(DIR, $some_dir) || die "can't opendir $some_dir: $!";
4297\& @dots = grep { /^\e./ && -f "$some_dir/$_" } readdir(DIR);
4298\& closedir DIR;
4299.Ve
4300.IP "readline \s-1EXPR\s0" 8
4301.IX Item "readline EXPR"
4302Reads from the filehandle whose typeglob is contained in \s-1EXPR\s0. In scalar
4303context, each call reads and returns the next line, until end-of-file is
4304reached, whereupon the subsequent call returns undef. In list context,
4305reads until end-of-file is reached and returns a list of lines. Note that
4306the notion of \*(L"line\*(R" used here is however you may have defined it
4307with \f(CW$/\fR or \f(CW$INPUT_RECORD_SEPARATOR\fR). See \*(L"$/\*(R" in perlvar.
4308.Sp
4309When \f(CW$/\fR is set to \f(CW\*(C`undef\*(C'\fR, when \fIreadline()\fR is in scalar
4310context (i.e. file slurp mode), and when an empty file is read, it
4311returns \f(CW''\fR the first time, followed by \f(CW\*(C`undef\*(C'\fR subsequently.
4312.Sp
4313This is the internal function implementing the \f(CW\*(C`<EXPR>\*(C'\fR
4314operator, but you can use it directly. The \f(CW\*(C`<EXPR>\*(C'\fR
4315operator is discussed in more detail in \*(L"I/O Operators\*(R" in perlop.
4316.Sp
4317.Vb 2
4318\& $line = <STDIN>;
4319\& $line = readline(*STDIN); # same thing
4320.Ve
4321.IP "readlink \s-1EXPR\s0" 8
4322.IX Item "readlink EXPR"
4323.PD 0
4324.IP "readlink" 8
4325.IX Item "readlink"
4326.PD
4327Returns the value of a symbolic link, if symbolic links are
4328implemented. If not, gives a fatal error. If there is some system
4329error, returns the undefined value and sets \f(CW$!\fR (errno). If \s-1EXPR\s0 is
4330omitted, uses \f(CW$_\fR.
4331.IP "readpipe \s-1EXPR\s0" 8
4332.IX Item "readpipe EXPR"
4333\&\s-1EXPR\s0 is executed as a system command.
4334The collected standard output of the command is returned.
4335In scalar context, it comes back as a single (potentially
4336multi\-line) string. In list context, returns a list of lines
4337(however you've defined lines with \f(CW$/\fR or \f(CW$INPUT_RECORD_SEPARATOR\fR).
4338This is the internal function implementing the \f(CW\*(C`qx/EXPR/\*(C'\fR
4339operator, but you can use it directly. The \f(CW\*(C`qx/EXPR/\*(C'\fR
4340operator is discussed in more detail in \*(L"I/O Operators\*(R" in perlop.
4341.IP "recv \s-1SOCKET\s0,SCALAR,LENGTH,FLAGS" 8
4342.IX Item "recv SOCKET,SCALAR,LENGTH,FLAGS"
4343Receives a message on a socket. Attempts to receive \s-1LENGTH\s0 characters
4344of data into variable \s-1SCALAR\s0 from the specified \s-1SOCKET\s0 filehandle.
4345\&\s-1SCALAR\s0 will be grown or shrunk to the length actually read. Takes the
4346same flags as the system call of the same name. Returns the address
4347of the sender if \s-1SOCKET\s0's protocol supports this; returns an empty
4348string otherwise. If there's an error, returns the undefined value.
4349This call is actually implemented in terms of \fIrecvfrom\fR\|(2) system call.
4350See \*(L"\s-1UDP:\s0 Message Passing\*(R" in perlipc for examples.
4351.Sp
4352Note the \fIcharacters\fR: depending on the status of the socket, either
4353(8\-bit) bytes or characters are received. By default all sockets
4354operate on bytes, but for example if the socket has been changed using
4355\&\fIbinmode()\fR to operate with the \f(CW\*(C`:utf8\*(C'\fR I/O layer (see the \f(CW\*(C`open\*(C'\fR
4356pragma, open), the I/O will operate on characters, not bytes.
4357.IP "redo \s-1LABEL\s0" 8
4358.IX Item "redo LABEL"
4359.PD 0
4360.IP "redo" 8
4361.IX Item "redo"
4362.PD
4363The \f(CW\*(C`redo\*(C'\fR command restarts the loop block without evaluating the
4364conditional again. The \f(CW\*(C`continue\*(C'\fR block, if any, is not executed. If
4365the \s-1LABEL\s0 is omitted, the command refers to the innermost enclosing
4366loop. This command is normally used by programs that want to lie to
4367themselves about what was just input:
4368.Sp
4369.Vb 16
4370\& # a simpleminded Pascal comment stripper
4371\& # (warning: assumes no { or } in strings)
4372\& LINE: while (<STDIN>) {
4373\& while (s|({.*}.*){.*}|$1 |) {}
4374\& s|{.*}| |;
4375\& if (s|{.*| |) {
4376\& $front = $_;
4377\& while (<STDIN>) {
4378\& if (/}/) { # end of comment?
4379\& s|^|$front\e{|;
4380\& redo LINE;
4381\& }
4382\& }
4383\& }
4384\& print;
4385\& }
4386.Ve
4387.Sp
4388\&\f(CW\*(C`redo\*(C'\fR cannot be used to retry a block which returns a value such as
4389\&\f(CW\*(C`eval {}\*(C'\fR, \f(CW\*(C`sub {}\*(C'\fR or \f(CW\*(C`do {}\*(C'\fR, and should not be used to exit
4390a \fIgrep()\fR or \fImap()\fR operation.
4391.Sp
4392Note that a block by itself is semantically identical to a loop
4393that executes once. Thus \f(CW\*(C`redo\*(C'\fR inside such a block will effectively
4394turn it into a looping construct.
4395.Sp
4396See also \*(L"continue\*(R" for an illustration of how \f(CW\*(C`last\*(C'\fR, \f(CW\*(C`next\*(C'\fR, and
4397\&\f(CW\*(C`redo\*(C'\fR work.
4398.IP "ref \s-1EXPR\s0" 8
4399.IX Item "ref EXPR"
4400.PD 0
4401.IP "ref" 8
4402.IX Item "ref"
4403.PD
4404Returns a true value if \s-1EXPR\s0 is a reference, false otherwise. If \s-1EXPR\s0
4405is not specified, \f(CW$_\fR will be used. The value returned depends on the
4406type of thing the reference is a reference to.
4407Builtin types include:
4408.Sp
4409.Vb 7
4410\& SCALAR
4411\& ARRAY
4412\& HASH
4413\& CODE
4414\& REF
4415\& GLOB
4416\& LVALUE
4417.Ve
4418.Sp
4419If the referenced object has been blessed into a package, then that package
4420name is returned instead. You can think of \f(CW\*(C`ref\*(C'\fR as a \f(CW\*(C`typeof\*(C'\fR operator.
4421.Sp
4422.Vb 9
4423\& if (ref($r) eq "HASH") {
4424\& print "r is a reference to a hash.\en";
4425\& }
4426\& unless (ref($r)) {
4427\& print "r is not a reference at all.\en";
4428\& }
4429\& if (UNIVERSAL::isa($r, "HASH")) { # for subclassing
4430\& print "r is a reference to something that isa hash.\en";
4431\& }
4432.Ve
4433.Sp
4434See also perlref.
4435.IP "rename \s-1OLDNAME\s0,NEWNAME" 8
4436.IX Item "rename OLDNAME,NEWNAME"
4437Changes the name of a file; an existing file \s-1NEWNAME\s0 will be
4438clobbered. Returns true for success, false otherwise.
4439.Sp
4440Behavior of this function varies wildly depending on your system
4441implementation. For example, it will usually not work across file system
4442boundaries, even though the system \fImv\fR command sometimes compensates
4443for this. Other restrictions include whether it works on directories,
4444open files, or pre-existing files. Check perlport and either the
4445\&\fIrename\fR\|(2) manpage or equivalent system documentation for details.
4446.IP "require \s-1VERSION\s0" 8
4447.IX Item "require VERSION"
4448.PD 0
4449.IP "require \s-1EXPR\s0" 8
4450.IX Item "require EXPR"
4451.IP "require" 8
4452.IX Item "require"
4453.PD
4454Demands a version of Perl specified by \s-1VERSION\s0, or demands some semantics
4455specified by \s-1EXPR\s0 or by \f(CW$_\fR if \s-1EXPR\s0 is not supplied.
4456.Sp
4457\&\s-1VERSION\s0 may be either a numeric argument such as 5.006, which will be
4458compared to \f(CW$]\fR, or a literal of the form v5.6.1, which will be compared
4459to \f(CW$^V\fR (aka \f(CW$PERL_VERSION\fR). A fatal error is produced at run time if
4460\&\s-1VERSION\s0 is greater than the version of the current Perl interpreter.
4461Compare with \*(L"use\*(R", which can do a similar check at compile time.
4462.Sp
4463Specifying \s-1VERSION\s0 as a literal of the form v5.6.1 should generally be
4464avoided, because it leads to misleading error messages under earlier
4465versions of Perl which do not support this syntax. The equivalent numeric
4466version should be used instead.
4467.Sp
4468.Vb 3
4469\& require v5.6.1; # run time version check
4470\& require 5.6.1; # ditto
4471\& require 5.006_001; # ditto; preferred for backwards compatibility
4472.Ve
4473.Sp
4474Otherwise, demands that a library file be included if it hasn't already
4475been included. The file is included via the do-FILE mechanism, which is
4476essentially just a variety of \f(CW\*(C`eval\*(C'\fR. Has semantics similar to the following
4477subroutine:
4478.Sp
4479.Vb 20
4480\& sub require {
4481\& my($filename) = @_;
4482\& return 1 if $INC{$filename};
4483\& my($realfilename,$result);
4484\& ITER: {
4485\& foreach $prefix (@INC) {
4486\& $realfilename = "$prefix/$filename";
4487\& if (-f $realfilename) {
4488\& $INC{$filename} = $realfilename;
4489\& $result = do $realfilename;
4490\& last ITER;
4491\& }
4492\& }
4493\& die "Can't find $filename in \e@INC";
4494\& }
4495\& delete $INC{$filename} if $@ || !$result;
4496\& die $@ if $@;
4497\& die "$filename did not return true value" unless $result;
4498\& return $result;
4499\& }
4500.Ve
4501.Sp
4502Note that the file will not be included twice under the same specified
4503name. The file must return true as the last statement to indicate
4504successful execution of any initialization code, so it's customary to
4505end such a file with \f(CW\*(C`1;\*(C'\fR unless you're sure it'll return true
4506otherwise. But it's better just to put the \f(CW\*(C`1;\*(C'\fR, in case you add more
4507statements.
4508.Sp
4509If \s-1EXPR\s0 is a bareword, the require assumes a "\fI.pm\fR\*(L" extension and
4510replaces \*(R"\fI::\fR\*(L" with \*(R"\fI/\fR" in the filename for you,
4511to make it easy to load standard modules. This form of loading of
4512modules does not risk altering your namespace.
4513.Sp
4514In other words, if you try this:
4515.Sp
4516.Vb 1
4517\& require Foo::Bar; # a splendid bareword
4518.Ve
4519.Sp
4520The require function will actually look for the "\fIFoo/Bar.pm\fR" file in the
4521directories specified in the \f(CW@INC\fR array.
4522.Sp
4523But if you try this:
4524.Sp
4525.Vb 4
4526\& $class = 'Foo::Bar';
4527\& require $class; # $class is not a bareword
4528\& #or
4529\& require "Foo::Bar"; # not a bareword because of the ""
4530.Ve
4531.Sp
4532The require function will look for the "\fIFoo::Bar\fR\*(L" file in the \f(CW@INC\fR array and
4533will complain about not finding \*(R"\fIFoo::Bar\fR" there. In this case you can do:
4534.Sp
4535.Vb 1
4536\& eval "require $class";
4537.Ve
4538.Sp
4539You can also insert hooks into the import facility, by putting directly
4540Perl code into the \f(CW@INC\fR array. There are three forms of hooks: subroutine
4541references, array references and blessed objects.
4542.Sp
4543Subroutine references are the simplest case. When the inclusion system
4544walks through \f(CW@INC\fR and encounters a subroutine, this subroutine gets
4545called with two parameters, the first being a reference to itself, and the
4546second the name of the file to be included (e.g. "\fIFoo/Bar.pm\fR"). The
4547subroutine should return \f(CW\*(C`undef\*(C'\fR or a filehandle, from which the file to
4548include will be read. If \f(CW\*(C`undef\*(C'\fR is returned, \f(CW\*(C`require\*(C'\fR will look at
4549the remaining elements of \f(CW@INC\fR.
4550.Sp
4551If the hook is an array reference, its first element must be a subroutine
4552reference. This subroutine is called as above, but the first parameter is
4553the array reference. This enables to pass indirectly some arguments to
4554the subroutine.
4555.Sp
4556In other words, you can write:
4557.Sp
4558.Vb 5
4559\& push @INC, \e&my_sub;
4560\& sub my_sub {
4561\& my ($coderef, $filename) = @_; # $coderef is \e&my_sub
4562\& ...
4563\& }
4564.Ve
4565.Sp
4566or:
4567.Sp
4568.Vb 7
4569\& push @INC, [ \e&my_sub, $x, $y, ... ];
4570\& sub my_sub {
4571\& my ($arrayref, $filename) = @_;
4572\& # Retrieve $x, $y, ...
4573\& my @parameters = @$arrayref[1..$#$arrayref];
4574\& ...
4575\& }
4576.Ve
4577.Sp
4578If the hook is an object, it must provide an \s-1INC\s0 method, that will be
4579called as above, the first parameter being the object itself. (Note that
4580you must fully qualify the sub's name, as it is always forced into package
4581\&\f(CW\*(C`main\*(C'\fR.) Here is a typical code layout:
4582.Sp
4583.Vb 7
4584\& # In Foo.pm
4585\& package Foo;
4586\& sub new { ... }
4587\& sub Foo::INC {
4588\& my ($self, $filename) = @_;
4589\& ...
4590\& }
4591.Ve
4592.Sp
4593.Vb 2
4594\& # In the main program
4595\& push @INC, new Foo(...);
4596.Ve
4597.Sp
4598Note that these hooks are also permitted to set the \f(CW%INC\fR entry
4599corresponding to the files they have loaded. See \*(L"%INC\*(R" in perlvar.
4600.Sp
4601For a yet-more-powerful import facility, see \*(L"use\*(R" and perlmod.
4602.IP "reset \s-1EXPR\s0" 8
4603.IX Item "reset EXPR"
4604.PD 0
4605.IP "reset" 8
4606.IX Item "reset"
4607.PD
4608Generally used in a \f(CW\*(C`continue\*(C'\fR block at the end of a loop to clear
4609variables and reset \f(CW\*(C`??\*(C'\fR searches so that they work again. The
4610expression is interpreted as a list of single characters (hyphens
4611allowed for ranges). All variables and arrays beginning with one of
4612those letters are reset to their pristine state. If the expression is
4613omitted, one-match searches (\f(CW\*(C`?pattern?\*(C'\fR) are reset to match again. Resets
4614only variables or searches in the current package. Always returns
46151. Examples:
4616.Sp
4617.Vb 3
4618\& reset 'X'; # reset all X variables
4619\& reset 'a-z'; # reset lower case variables
4620\& reset; # just reset ?one-time? searches
4621.Ve
4622.Sp
4623Resetting \f(CW"A\-Z"\fR is not recommended because you'll wipe out your
4624\&\f(CW@ARGV\fR and \f(CW@INC\fR arrays and your \f(CW%ENV\fR hash. Resets only package
4625variables\*(--lexical variables are unaffected, but they clean themselves
4626up on scope exit anyway, so you'll probably want to use them instead.
4627See \*(L"my\*(R".
4628.IP "return \s-1EXPR\s0" 8
4629.IX Item "return EXPR"
4630.PD 0
4631.IP "return" 8
4632.IX Item "return"
4633.PD
4634Returns from a subroutine, \f(CW\*(C`eval\*(C'\fR, or \f(CW\*(C`do FILE\*(C'\fR with the value
4635given in \s-1EXPR\s0. Evaluation of \s-1EXPR\s0 may be in list, scalar, or void
4636context, depending on how the return value will be used, and the context
4637may vary from one execution to the next (see \f(CW\*(C`wantarray\*(C'\fR). If no \s-1EXPR\s0
4638is given, returns an empty list in list context, the undefined value in
4639scalar context, and (of course) nothing at all in a void context.
4640.Sp
4641(Note that in the absence of an explicit \f(CW\*(C`return\*(C'\fR, a subroutine, eval,
4642or do \s-1FILE\s0 will automatically return the value of the last expression
4643evaluated.)
4644.IP "reverse \s-1LIST\s0" 8
4645.IX Item "reverse LIST"
4646In list context, returns a list value consisting of the elements
4647of \s-1LIST\s0 in the opposite order. In scalar context, concatenates the
4648elements of \s-1LIST\s0 and returns a string value with all characters
4649in the opposite order.
4650.Sp
4651.Vb 1
4652\& print reverse <>; # line tac, last line first
4653.Ve
4654.Sp
4655.Vb 2
4656\& undef $/; # for efficiency of <>
4657\& print scalar reverse <>; # character tac, last line tsrif
4658.Ve
4659.Sp
4660This operator is also handy for inverting a hash, although there are some
4661caveats. If a value is duplicated in the original hash, only one of those
4662can be represented as a key in the inverted hash. Also, this has to
4663unwind one hash and build a whole new one, which may take some time
4664on a large hash, such as from a \s-1DBM\s0 file.
4665.Sp
4666.Vb 1
4667\& %by_name = reverse %by_address; # Invert the hash
4668.Ve
4669.IP "rewinddir \s-1DIRHANDLE\s0" 8
4670.IX Item "rewinddir DIRHANDLE"
4671Sets the current position to the beginning of the directory for the
4672\&\f(CW\*(C`readdir\*(C'\fR routine on \s-1DIRHANDLE\s0.
4673.IP "rindex \s-1STR\s0,SUBSTR,POSITION" 8
4674.IX Item "rindex STR,SUBSTR,POSITION"
4675.PD 0
4676.IP "rindex \s-1STR\s0,SUBSTR" 8
4677.IX Item "rindex STR,SUBSTR"
4678.PD
4679Works just like \fIindex()\fR except that it returns the position of the \s-1LAST\s0
4680occurrence of \s-1SUBSTR\s0 in \s-1STR\s0. If \s-1POSITION\s0 is specified, returns the
4681last occurrence at or before that position.
4682.IP "rmdir \s-1FILENAME\s0" 8
4683.IX Item "rmdir FILENAME"
4684.PD 0
4685.IP "rmdir" 8
4686.IX Item "rmdir"
4687.PD
4688Deletes the directory specified by \s-1FILENAME\s0 if that directory is empty. If it
4689succeeds it returns true, otherwise it returns false and sets \f(CW$!\fR (errno). If
4690\&\s-1FILENAME\s0 is omitted, uses \f(CW$_\fR.
4691.IP "s///" 8
4692.IX Item "s///"
4693The substitution operator. See perlop.
4694.IP "scalar \s-1EXPR\s0" 8
4695.IX Item "scalar EXPR"
4696Forces \s-1EXPR\s0 to be interpreted in scalar context and returns the value
4697of \s-1EXPR\s0.
4698.Sp
4699.Vb 1
4700\& @counts = ( scalar @a, scalar @b, scalar @c );
4701.Ve
4702.Sp
4703There is no equivalent operator to force an expression to
4704be interpolated in list context because in practice, this is never
4705needed. If you really wanted to do so, however, you could use
4706the construction \f(CW\*(C`@{[ (some expression) ]}\*(C'\fR, but usually a simple
4707\&\f(CW\*(C`(some expression)\*(C'\fR suffices.
4708.Sp
4709Because \f(CW\*(C`scalar\*(C'\fR is unary operator, if you accidentally use for \s-1EXPR\s0 a
4710parenthesized list, this behaves as a scalar comma expression, evaluating
4711all but the last element in void context and returning the final element
4712evaluated in scalar context. This is seldom what you want.
4713.Sp
4714The following single statement:
4715.Sp
4716.Vb 1
4717\& print uc(scalar(&foo,$bar)),$baz;
4718.Ve
4719.Sp
4720is the moral equivalent of these two:
4721.Sp
4722.Vb 2
4723\& &foo;
4724\& print(uc($bar),$baz);
4725.Ve
4726.Sp
4727See perlop for more details on unary operators and the comma operator.
4728.IP "seek \s-1FILEHANDLE\s0,POSITION,WHENCE" 8
4729.IX Item "seek FILEHANDLE,POSITION,WHENCE"
4730Sets \s-1FILEHANDLE\s0's position, just like the \f(CW\*(C`fseek\*(C'\fR call of \f(CW\*(C`stdio\*(C'\fR.
4731\&\s-1FILEHANDLE\s0 may be an expression whose value gives the name of the
4732filehandle. The values for \s-1WHENCE\s0 are \f(CW0\fR to set the new position
4733\&\fIin bytes\fR to \s-1POSITION\s0, \f(CW1\fR to set it to the current position plus
4734\&\s-1POSITION\s0, and \f(CW2\fR to set it to \s-1EOF\s0 plus \s-1POSITION\s0 (typically
4735negative). For \s-1WHENCE\s0 you may use the constants \f(CW\*(C`SEEK_SET\*(C'\fR,
4736\&\f(CW\*(C`SEEK_CUR\*(C'\fR, and \f(CW\*(C`SEEK_END\*(C'\fR (start of the file, current position, end
4737of the file) from the Fcntl module. Returns \f(CW1\fR upon success, \f(CW0\fR
4738otherwise.
4739.Sp
4740Note the \fIin bytes\fR: even if the filehandle has been set to
4741operate on characters (for example by using the \f(CW\*(C`:utf8\*(C'\fR open
4742layer), \fItell()\fR will return byte offsets, not character offsets
4743(because implementing that would render \fIseek()\fR and \fItell()\fR rather slow).
4744.Sp
4745If you want to position file for \f(CW\*(C`sysread\*(C'\fR or \f(CW\*(C`syswrite\*(C'\fR, don't use
4746\&\f(CW\*(C`seek\*(C'\fR\-\-buffering makes its effect on the file's system position
4747unpredictable and non\-portable. Use \f(CW\*(C`sysseek\*(C'\fR instead.
4748.Sp
4749Due to the rules and rigors of \s-1ANSI\s0 C, on some systems you have to do a
4750seek whenever you switch between reading and writing. Amongst other
4751things, this may have the effect of calling stdio's \fIclearerr\fR\|(3).
4752A \s-1WHENCE\s0 of \f(CW1\fR (\f(CW\*(C`SEEK_CUR\*(C'\fR) is useful for not moving the file position:
4753.Sp
4754.Vb 1
4755\& seek(TEST,0,1);
4756.Ve
4757.Sp
4758This is also useful for applications emulating \f(CW\*(C`tail \-f\*(C'\fR. Once you hit
4759\&\s-1EOF\s0 on your read, and then sleep for a while, you might have to stick in a
4760\&\fIseek()\fR to reset things. The \f(CW\*(C`seek\*(C'\fR doesn't change the current position,
4761but it \fIdoes\fR clear the end-of-file condition on the handle, so that the
4762next \f(CW\*(C`<FILE>\*(C'\fR makes Perl try again to read something. We hope.
4763.Sp
4764If that doesn't work (some \s-1IO\s0 implementations are particularly
4765cantankerous), then you may need something more like this:
4766.Sp
4767.Vb 8
4768\& for (;;) {
4769\& for ($curpos = tell(FILE); $_ = <FILE>;
4770\& $curpos = tell(FILE)) {
4771\& # search for some stuff and put it into files
4772\& }
4773\& sleep($for_a_while);
4774\& seek(FILE, $curpos, 0);
4775\& }
4776.Ve
4777.IP "seekdir \s-1DIRHANDLE\s0,POS" 8
4778.IX Item "seekdir DIRHANDLE,POS"
4779Sets the current position for the \f(CW\*(C`readdir\*(C'\fR routine on \s-1DIRHANDLE\s0. \s-1POS\s0
4780must be a value returned by \f(CW\*(C`telldir\*(C'\fR. Has the same caveats about
4781possible directory compaction as the corresponding system library
4782routine.
4783.IP "select \s-1FILEHANDLE\s0" 8
4784.IX Item "select FILEHANDLE"
4785.PD 0
4786.IP "select" 8
4787.IX Item "select"
4788.PD
4789Returns the currently selected filehandle. Sets the current default
4790filehandle for output, if \s-1FILEHANDLE\s0 is supplied. This has two
4791effects: first, a \f(CW\*(C`write\*(C'\fR or a \f(CW\*(C`print\*(C'\fR without a filehandle will
4792default to this \s-1FILEHANDLE\s0. Second, references to variables related to
4793output will refer to this output channel. For example, if you have to
4794set the top of form format for more than one output channel, you might
4795do the following:
4796.Sp
4797.Vb 4
4798\& select(REPORT1);
4799\& $^ = 'report1_top';
4800\& select(REPORT2);
4801\& $^ = 'report2_top';
4802.Ve
4803.Sp
4804\&\s-1FILEHANDLE\s0 may be an expression whose value gives the name of the
4805actual filehandle. Thus:
4806.Sp
4807.Vb 1
4808\& $oldfh = select(STDERR); $| = 1; select($oldfh);
4809.Ve
4810.Sp
4811Some programmers may prefer to think of filehandles as objects with
4812methods, preferring to write the last example as:
4813.Sp
4814.Vb 2
4815\& use IO::Handle;
4816\& STDERR->autoflush(1);
4817.Ve
4818.IP "select \s-1RBITS\s0,WBITS,EBITS,TIMEOUT" 8
4819.IX Item "select RBITS,WBITS,EBITS,TIMEOUT"
4820This calls the \fIselect\fR\|(2) system call with the bit masks specified, which
4821can be constructed using \f(CW\*(C`fileno\*(C'\fR and \f(CW\*(C`vec\*(C'\fR, along these lines:
4822.Sp
4823.Vb 4
4824\& $rin = $win = $ein = '';
4825\& vec($rin,fileno(STDIN),1) = 1;
4826\& vec($win,fileno(STDOUT),1) = 1;
4827\& $ein = $rin | $win;
4828.Ve
4829.Sp
4830If you want to select on many filehandles you might wish to write a
4831subroutine:
4832.Sp
4833.Vb 9
4834\& sub fhbits {
4835\& my(@fhlist) = split(' ',$_[0]);
4836\& my($bits);
4837\& for (@fhlist) {
4838\& vec($bits,fileno($_),1) = 1;
4839\& }
4840\& $bits;
4841\& }
4842\& $rin = fhbits('STDIN TTY SOCK');
4843.Ve
4844.Sp
4845The usual idiom is:
4846.Sp
4847.Vb 2
4848\& ($nfound,$timeleft) =
4849\& select($rout=$rin, $wout=$win, $eout=$ein, $timeout);
4850.Ve
4851.Sp
4852or to block until something becomes ready just do this
4853.Sp
4854.Vb 1
4855\& $nfound = select($rout=$rin, $wout=$win, $eout=$ein, undef);
4856.Ve
4857.Sp
4858Most systems do not bother to return anything useful in \f(CW$timeleft\fR, so
4859calling \fIselect()\fR in scalar context just returns \f(CW$nfound\fR.
4860.Sp
4861Any of the bit masks can also be undef. The timeout, if specified, is
4862in seconds, which may be fractional. Note: not all implementations are
4863capable of returning the \f(CW$timeleft\fR. If not, they always return
4864\&\f(CW$timeleft\fR equal to the supplied \f(CW$timeout\fR.
4865.Sp
4866You can effect a sleep of 250 milliseconds this way:
4867.Sp
4868.Vb 1
4869\& select(undef, undef, undef, 0.25);
4870.Ve
4871.Sp
4872Note that whether \f(CW\*(C`select\*(C'\fR gets restarted after signals (say, \s-1SIGALRM\s0)
4873is implementation\-dependent.
4874.Sp
4875\&\fB\s-1WARNING\s0\fR: One should not attempt to mix buffered I/O (like \f(CW\*(C`read\*(C'\fR
4876or <\s-1FH\s0>) with \f(CW\*(C`select\*(C'\fR, except as permitted by \s-1POSIX\s0, and even
4877then only on \s-1POSIX\s0 systems. You have to use \f(CW\*(C`sysread\*(C'\fR instead.
4878.IP "semctl \s-1ID\s0,SEMNUM,CMD,ARG" 8
4879.IX Item "semctl ID,SEMNUM,CMD,ARG"
4880Calls the System V \s-1IPC\s0 function \f(CW\*(C`semctl\*(C'\fR. You'll probably have to say
4881.Sp
4882.Vb 1
4883\& use IPC::SysV;
4884.Ve
4885.Sp
4886first to get the correct constant definitions. If \s-1CMD\s0 is \s-1IPC_STAT\s0 or
4887\&\s-1GETALL\s0, then \s-1ARG\s0 must be a variable which will hold the returned
4888semid_ds structure or semaphore value array. Returns like \f(CW\*(C`ioctl\*(C'\fR:
4889the undefined value for error, "\f(CW\*(C`0 but true\*(C'\fR" for zero, or the actual
4890return value otherwise. The \s-1ARG\s0 must consist of a vector of native
4891short integers, which may be created with \f(CW\*(C`pack("s!",(0)x$nsem)\*(C'\fR.
4892See also \*(L"SysV \s-1IPC\s0\*(R" in perlipc, \f(CW\*(C`IPC::SysV\*(C'\fR, \f(CW\*(C`IPC::Semaphore\*(C'\fR
4893documentation.
4894.IP "semget \s-1KEY\s0,NSEMS,FLAGS" 8
4895.IX Item "semget KEY,NSEMS,FLAGS"
4896Calls the System V \s-1IPC\s0 function semget. Returns the semaphore id, or
4897the undefined value if there is an error. See also
4898\&\*(L"SysV \s-1IPC\s0\*(R" in perlipc, \f(CW\*(C`IPC::SysV\*(C'\fR, \f(CW\*(C`IPC::SysV::Semaphore\*(C'\fR
4899documentation.
4900.IP "semop \s-1KEY\s0,OPSTRING" 8
4901.IX Item "semop KEY,OPSTRING"
4902Calls the System V \s-1IPC\s0 function semop to perform semaphore operations
4903such as signalling and waiting. \s-1OPSTRING\s0 must be a packed array of
4904semop structures. Each semop structure can be generated with
4905\&\f(CW\*(C`pack("s!3", $semnum, $semop, $semflag)\*(C'\fR. The number of semaphore
4906operations is implied by the length of \s-1OPSTRING\s0. Returns true if
4907successful, or false if there is an error. As an example, the
4908following code waits on semaphore \f(CW$semnum\fR of semaphore id \f(CW$semid:\fR
4909.Sp
4910.Vb 2
4911\& $semop = pack("s!3", $semnum, -1, 0);
4912\& die "Semaphore trouble: $!\en" unless semop($semid, $semop);
4913.Ve
4914.Sp
4915To signal the semaphore, replace \f(CW\*(C`\-1\*(C'\fR with \f(CW1\fR. See also
4916\&\*(L"SysV \s-1IPC\s0\*(R" in perlipc, \f(CW\*(C`IPC::SysV\*(C'\fR, and \f(CW\*(C`IPC::SysV::Semaphore\*(C'\fR
4917documentation.
4918.IP "send \s-1SOCKET\s0,MSG,FLAGS,TO" 8
4919.IX Item "send SOCKET,MSG,FLAGS,TO"
4920.PD 0
4921.IP "send \s-1SOCKET\s0,MSG,FLAGS" 8
4922.IX Item "send SOCKET,MSG,FLAGS"
4923.PD
4924Sends a message on a socket. Attempts to send the scalar \s-1MSG\s0 to the
4925\&\s-1SOCKET\s0 filehandle. Takes the same flags as the system call of the
4926same name. On unconnected sockets you must specify a destination to
4927send \s-1TO\s0, in which case it does a C \f(CW\*(C`sendto\*(C'\fR. Returns the number of
4928characters sent, or the undefined value if there is an error. The C
4929system call \fIsendmsg\fR\|(2) is currently unimplemented. See
4930\&\*(L"\s-1UDP:\s0 Message Passing\*(R" in perlipc for examples.
4931.Sp
4932Note the \fIcharacters\fR: depending on the status of the socket, either
4933(8\-bit) bytes or characters are sent. By default all sockets operate
4934on bytes, but for example if the socket has been changed using
4935\&\fIbinmode()\fR to operate with the \f(CW\*(C`:utf8\*(C'\fR I/O layer (see \*(L"open\*(R", or
4936the \f(CW\*(C`open\*(C'\fR pragma, open), the I/O will operate on characters, not
4937bytes.
4938.IP "setpgrp \s-1PID\s0,PGRP" 8
4939.IX Item "setpgrp PID,PGRP"
4940Sets the current process group for the specified \s-1PID\s0, \f(CW0\fR for the current
4941process. Will produce a fatal error if used on a machine that doesn't
4942implement \s-1POSIX\s0 \fIsetpgid\fR\|(2) or \s-1BSD\s0 \fIsetpgrp\fR\|(2). If the arguments are omitted,
4943it defaults to \f(CW\*(C`0,0\*(C'\fR. Note that the \s-1BSD\s0 4.2 version of \f(CW\*(C`setpgrp\*(C'\fR does not
4944accept any arguments, so only \f(CW\*(C`setpgrp(0,0)\*(C'\fR is portable. See also
4945\&\f(CW\*(C`POSIX::setsid()\*(C'\fR.
4946.IP "setpriority \s-1WHICH\s0,WHO,PRIORITY" 8
4947.IX Item "setpriority WHICH,WHO,PRIORITY"
4948Sets the current priority for a process, a process group, or a user.
4949(See \fIsetpriority\fR\|(2).) Will produce a fatal error if used on a machine
4950that doesn't implement \fIsetpriority\fR\|(2).
4951.IP "setsockopt \s-1SOCKET\s0,LEVEL,OPTNAME,OPTVAL" 8
4952.IX Item "setsockopt SOCKET,LEVEL,OPTNAME,OPTVAL"
4953Sets the socket option requested. Returns undefined if there is an
4954error. \s-1OPTVAL\s0 may be specified as \f(CW\*(C`undef\*(C'\fR if you don't want to pass an
4955argument.
4956.IP "shift \s-1ARRAY\s0" 8
4957.IX Item "shift ARRAY"
4958.PD 0
4959.IP "shift" 8
4960.IX Item "shift"
4961.PD
4962Shifts the first value of the array off and returns it, shortening the
4963array by 1 and moving everything down. If there are no elements in the
4964array, returns the undefined value. If \s-1ARRAY\s0 is omitted, shifts the
4965\&\f(CW@_\fR array within the lexical scope of subroutines and formats, and the
4966\&\f(CW@ARGV\fR array at file scopes or within the lexical scopes established by
4967the \f(CW\*(C`eval ''\*(C'\fR, \f(CW\*(C`BEGIN {}\*(C'\fR, \f(CW\*(C`INIT {}\*(C'\fR, \f(CW\*(C`CHECK {}\*(C'\fR, and \f(CW\*(C`END {}\*(C'\fR
4968constructs.
4969.Sp
4970See also \f(CW\*(C`unshift\*(C'\fR, \f(CW\*(C`push\*(C'\fR, and \f(CW\*(C`pop\*(C'\fR. \f(CW\*(C`shift\*(C'\fR and \f(CW\*(C`unshift\*(C'\fR do the
4971same thing to the left end of an array that \f(CW\*(C`pop\*(C'\fR and \f(CW\*(C`push\*(C'\fR do to the
4972right end.
4973.IP "shmctl \s-1ID\s0,CMD,ARG" 8
4974.IX Item "shmctl ID,CMD,ARG"
4975Calls the System V \s-1IPC\s0 function shmctl. You'll probably have to say
4976.Sp
4977.Vb 1
4978\& use IPC::SysV;
4979.Ve
4980.Sp
4981first to get the correct constant definitions. If \s-1CMD\s0 is \f(CW\*(C`IPC_STAT\*(C'\fR,
4982then \s-1ARG\s0 must be a variable which will hold the returned \f(CW\*(C`shmid_ds\*(C'\fR
4983structure. Returns like ioctl: the undefined value for error, "\f(CW0\fR but
4984true" for zero, or the actual return value otherwise.
4985See also \*(L"SysV \s-1IPC\s0\*(R" in perlipc and \f(CW\*(C`IPC::SysV\*(C'\fR documentation.
4986.IP "shmget \s-1KEY\s0,SIZE,FLAGS" 8
4987.IX Item "shmget KEY,SIZE,FLAGS"
4988Calls the System V \s-1IPC\s0 function shmget. Returns the shared memory
4989segment id, or the undefined value if there is an error.
4990See also \*(L"SysV \s-1IPC\s0\*(R" in perlipc and \f(CW\*(C`IPC::SysV\*(C'\fR documentation.
4991.IP "shmread \s-1ID\s0,VAR,POS,SIZE" 8
4992.IX Item "shmread ID,VAR,POS,SIZE"
4993.PD 0
4994.IP "shmwrite \s-1ID\s0,STRING,POS,SIZE" 8
4995.IX Item "shmwrite ID,STRING,POS,SIZE"
4996.PD
4997Reads or writes the System V shared memory segment \s-1ID\s0 starting at
4998position \s-1POS\s0 for size \s-1SIZE\s0 by attaching to it, copying in/out, and
4999detaching from it. When reading, \s-1VAR\s0 must be a variable that will
5000hold the data read. When writing, if \s-1STRING\s0 is too long, only \s-1SIZE\s0
5001bytes are used; if \s-1STRING\s0 is too short, nulls are written to fill out
5002\&\s-1SIZE\s0 bytes. Return true if successful, or false if there is an error.
5003\&\fIshmread()\fR taints the variable. See also \*(L"SysV \s-1IPC\s0\*(R" in perlipc,
5004\&\f(CW\*(C`IPC::SysV\*(C'\fR documentation, and the \f(CW\*(C`IPC::Shareable\*(C'\fR module from \s-1CPAN\s0.
5005.IP "shutdown \s-1SOCKET\s0,HOW" 8
5006.IX Item "shutdown SOCKET,HOW"
5007Shuts down a socket connection in the manner indicated by \s-1HOW\s0, which
5008has the same interpretation as in the system call of the same name.
5009.Sp
5010.Vb 3
5011\& shutdown(SOCKET, 0); # I/we have stopped reading data
5012\& shutdown(SOCKET, 1); # I/we have stopped writing data
5013\& shutdown(SOCKET, 2); # I/we have stopped using this socket
5014.Ve
5015.Sp
5016This is useful with sockets when you want to tell the other
5017side you're done writing but not done reading, or vice versa.
5018It's also a more insistent form of close because it also
5019disables the file descriptor in any forked copies in other
5020processes.
5021.IP "sin \s-1EXPR\s0" 8
5022.IX Item "sin EXPR"
5023.PD 0
5024.IP "sin" 8
5025.IX Item "sin"
5026.PD
5027Returns the sine of \s-1EXPR\s0 (expressed in radians). If \s-1EXPR\s0 is omitted,
5028returns sine of \f(CW$_\fR.
5029.Sp
5030For the inverse sine operation, you may use the \f(CW\*(C`Math::Trig::asin\*(C'\fR
5031function, or use this relation:
5032.Sp
5033.Vb 1
5034\& sub asin { atan2($_[0], sqrt(1 - $_[0] * $_[0])) }
5035.Ve
5036.IP "sleep \s-1EXPR\s0" 8
5037.IX Item "sleep EXPR"
5038.PD 0
5039.IP "sleep" 8
5040.IX Item "sleep"
5041.PD
5042Causes the script to sleep for \s-1EXPR\s0 seconds, or forever if no \s-1EXPR\s0.
5043May be interrupted if the process receives a signal such as \f(CW\*(C`SIGALRM\*(C'\fR.
5044Returns the number of seconds actually slept. You probably cannot
5045mix \f(CW\*(C`alarm\*(C'\fR and \f(CW\*(C`sleep\*(C'\fR calls, because \f(CW\*(C`sleep\*(C'\fR is often implemented
5046using \f(CW\*(C`alarm\*(C'\fR.
5047.Sp
5048On some older systems, it may sleep up to a full second less than what
5049you requested, depending on how it counts seconds. Most modern systems
5050always sleep the full amount. They may appear to sleep longer than that,
5051however, because your process might not be scheduled right away in a
5052busy multitasking system.
5053.Sp
5054For delays of finer granularity than one second, you may use Perl's
5055\&\f(CW\*(C`syscall\*(C'\fR interface to access \fIsetitimer\fR\|(2) if your system supports
5056it, or else see \*(L"select\*(R" above. The Time::HiRes module (from \s-1CPAN\s0,
5057and starting from Perl 5.8 part of the standard distribution) may also
5058help.
5059.Sp
5060See also the \s-1POSIX\s0 module's \f(CW\*(C`pause\*(C'\fR function.
5061.IP "socket \s-1SOCKET\s0,DOMAIN,TYPE,PROTOCOL" 8
5062.IX Item "socket SOCKET,DOMAIN,TYPE,PROTOCOL"
5063Opens a socket of the specified kind and attaches it to filehandle
5064\&\s-1SOCKET\s0. \s-1DOMAIN\s0, \s-1TYPE\s0, and \s-1PROTOCOL\s0 are specified the same as for
5065the system call of the same name. You should \f(CW\*(C`use Socket\*(C'\fR first
5066to get the proper definitions imported. See the examples in
5067\&\*(L"Sockets: Client/Server Communication\*(R" in perlipc.
5068.Sp
5069On systems that support a close-on-exec flag on files, the flag will
5070be set for the newly opened file descriptor, as determined by the
5071value of $^F. See \*(L"$^F\*(R" in perlvar.
5072.IP "socketpair \s-1SOCKET1\s0,SOCKET2,DOMAIN,TYPE,PROTOCOL" 8
5073.IX Item "socketpair SOCKET1,SOCKET2,DOMAIN,TYPE,PROTOCOL"
5074Creates an unnamed pair of sockets in the specified domain, of the
5075specified type. \s-1DOMAIN\s0, \s-1TYPE\s0, and \s-1PROTOCOL\s0 are specified the same as
5076for the system call of the same name. If unimplemented, yields a fatal
5077error. Returns true if successful.
5078.Sp
5079On systems that support a close-on-exec flag on files, the flag will
5080be set for the newly opened file descriptors, as determined by the value
5081of $^F. See \*(L"$^F\*(R" in perlvar.
5082.Sp
5083Some systems defined \f(CW\*(C`pipe\*(C'\fR in terms of \f(CW\*(C`socketpair\*(C'\fR, in which a call
5084to \f(CW\*(C`pipe(Rdr, Wtr)\*(C'\fR is essentially:
5085.Sp
5086.Vb 4
5087\& use Socket;
5088\& socketpair(Rdr, Wtr, AF_UNIX, SOCK_STREAM, PF_UNSPEC);
5089\& shutdown(Rdr, 1); # no more writing for reader
5090\& shutdown(Wtr, 0); # no more reading for writer
5091.Ve
5092.Sp
5093See perlipc for an example of socketpair use. Perl 5.8 and later will
5094emulate socketpair using \s-1IP\s0 sockets to localhost if your system implements
5095sockets but not socketpair.
5096.IP "sort \s-1SUBNAME\s0 \s-1LIST\s0" 8
5097.IX Item "sort SUBNAME LIST"
5098.PD 0
5099.IP "sort \s-1BLOCK\s0 \s-1LIST\s0" 8
5100.IX Item "sort BLOCK LIST"
5101.IP "sort \s-1LIST\s0" 8
5102.IX Item "sort LIST"
5103.PD
5104In list context, this sorts the \s-1LIST\s0 and returns the sorted list value.
5105In scalar context, the behaviour of \f(CW\*(C`sort()\*(C'\fR is undefined.
5106.Sp
5107If \s-1SUBNAME\s0 or \s-1BLOCK\s0 is omitted, \f(CW\*(C`sort\*(C'\fRs in standard string comparison
5108order. If \s-1SUBNAME\s0 is specified, it gives the name of a subroutine
5109that returns an integer less than, equal to, or greater than \f(CW0\fR,
5110depending on how the elements of the list are to be ordered. (The \f(CW\*(C`<=>\*(C'\fR and \f(CW\*(C`cmp\*(C'\fR operators are extremely useful in such routines.)
5111\&\s-1SUBNAME\s0 may be a scalar variable name (unsubscripted), in which case
5112the value provides the name of (or a reference to) the actual
5113subroutine to use. In place of a \s-1SUBNAME\s0, you can provide a \s-1BLOCK\s0 as
5114an anonymous, in-line sort subroutine.
5115.Sp
5116If the subroutine's prototype is \f(CW\*(C`($$)\*(C'\fR, the elements to be compared
5117are passed by reference in \f(CW@_\fR, as for a normal subroutine. This is
5118slower than unprototyped subroutines, where the elements to be
5119compared are passed into the subroutine
5120as the package global variables \f(CW$a\fR and \f(CW$b\fR (see example below). Note that
5121in the latter case, it is usually counter-productive to declare \f(CW$a\fR and
5122\&\f(CW$b\fR as lexicals.
5123.Sp
5124In either case, the subroutine may not be recursive. The values to be
5125compared are always passed by reference, so don't modify them.
5126.Sp
5127You also cannot exit out of the sort block or subroutine using any of the
5128loop control operators described in perlsyn or with \f(CW\*(C`goto\*(C'\fR.
5129.Sp
5130When \f(CW\*(C`use locale\*(C'\fR is in effect, \f(CW\*(C`sort LIST\*(C'\fR sorts \s-1LIST\s0 according to the
5131current collation locale. See perllocale.
5132.Sp
5133Perl 5.6 and earlier used a quicksort algorithm to implement sort.
5134That algorithm was not stable, and \fIcould\fR go quadratic. (A \fIstable\fR sort
5135preserves the input order of elements that compare equal. Although
5136quicksort's run time is O(NlogN) when averaged over all arrays of
5137length N, the time can be O(N**2), \fIquadratic\fR behavior, for some
5138inputs.) In 5.7, the quicksort implementation was replaced with
5139a stable mergesort algorithm whose worst case behavior is O(NlogN).
5140But benchmarks indicated that for some inputs, on some platforms,
5141the original quicksort was faster. 5.8 has a sort pragma for
5142limited control of the sort. Its rather blunt control of the
5143underlying algorithm may not persist into future perls, but the
5144ability to characterize the input or output in implementation
5145independent ways quite probably will. See \*(L"use\*(R".
5146.Sp
5147Examples:
5148.Sp
5149.Vb 2
5150\& # sort lexically
5151\& @articles = sort @files;
5152.Ve
5153.Sp
5154.Vb 2
5155\& # same thing, but with explicit sort routine
5156\& @articles = sort {$a cmp $b} @files;
5157.Ve
5158.Sp
5159.Vb 2
5160\& # now case-insensitively
5161\& @articles = sort {uc($a) cmp uc($b)} @files;
5162.Ve
5163.Sp
5164.Vb 2
5165\& # same thing in reversed order
5166\& @articles = sort {$b cmp $a} @files;
5167.Ve
5168.Sp
5169.Vb 2
5170\& # sort numerically ascending
5171\& @articles = sort {$a <=> $b} @files;
5172.Ve
5173.Sp
5174.Vb 2
5175\& # sort numerically descending
5176\& @articles = sort {$b <=> $a} @files;
5177.Ve
5178.Sp
5179.Vb 3
5180\& # this sorts the %age hash by value instead of key
5181\& # using an in-line function
5182\& @eldest = sort { $age{$b} <=> $age{$a} } keys %age;
5183.Ve
5184.Sp
5185.Vb 5
5186\& # sort using explicit subroutine name
5187\& sub byage {
5188\& $age{$a} <=> $age{$b}; # presuming numeric
5189\& }
5190\& @sortedclass = sort byage @class;
5191.Ve
5192.Sp
5193.Vb 9
5194\& sub backwards { $b cmp $a }
5195\& @harry = qw(dog cat x Cain Abel);
5196\& @george = qw(gone chased yz Punished Axed);
5197\& print sort @harry;
5198\& # prints AbelCaincatdogx
5199\& print sort backwards @harry;
5200\& # prints xdogcatCainAbel
5201\& print sort @george, 'to', @harry;
5202\& # prints AbelAxedCainPunishedcatchaseddoggonetoxyz
5203.Ve
5204.Sp
5205.Vb 3
5206\& # inefficiently sort by descending numeric compare using
5207\& # the first integer after the first = sign, or the
5208\& # whole record case-insensitively otherwise
5209.Ve
5210.Sp
5211.Vb 5
5212\& @new = sort {
5213\& ($b =~ /=(\ed+)/)[0] <=> ($a =~ /=(\ed+)/)[0]
5214\& ||
5215\& uc($a) cmp uc($b)
5216\& } @old;
5217.Ve
5218.Sp
5219.Vb 8
5220\& # same thing, but much more efficiently;
5221\& # we'll build auxiliary indices instead
5222\& # for speed
5223\& @nums = @caps = ();
5224\& for (@old) {
5225\& push @nums, /=(\ed+)/;
5226\& push @caps, uc($_);
5227\& }
5228.Ve
5229.Sp
5230.Vb 6
5231\& @new = @old[ sort {
5232\& $nums[$b] <=> $nums[$a]
5233\& ||
5234\& $caps[$a] cmp $caps[$b]
5235\& } 0..$#old
5236\& ];
5237.Ve
5238.Sp
5239.Vb 6
5240\& # same thing, but without any temps
5241\& @new = map { $_->[0] }
5242\& sort { $b->[1] <=> $a->[1]
5243\& ||
5244\& $a->[2] cmp $b->[2]
5245\& } map { [$_, /=(\ed+)/, uc($_)] } @old;
5246.Ve
5247.Sp
5248.Vb 4
5249\& # using a prototype allows you to use any comparison subroutine
5250\& # as a sort subroutine (including other package's subroutines)
5251\& package other;
5252\& sub backwards ($$) { $_[1] cmp $_[0]; } # $a and $b are not set here
5253.Ve
5254.Sp
5255.Vb 2
5256\& package main;
5257\& @new = sort other::backwards @old;
5258.Ve
5259.Sp
5260.Vb 3
5261\& # guarantee stability, regardless of algorithm
5262\& use sort 'stable';
5263\& @new = sort { substr($a, 3, 5) cmp substr($b, 3, 5) } @old;
5264.Ve
5265.Sp
5266.Vb 3
5267\& # force use of mergesort (not portable outside Perl 5.8)
5268\& use sort '_mergesort'; # note discouraging _
5269\& @new = sort { substr($a, 3, 5) cmp substr($b, 3, 5) } @old;
5270.Ve
5271.Sp
5272If you're using strict, you \fImust not\fR declare \f(CW$a\fR
5273and \f(CW$b\fR as lexicals. They are package globals. That means
5274if you're in the \f(CW\*(C`main\*(C'\fR package and type
5275.Sp
5276.Vb 1
5277\& @articles = sort {$b <=> $a} @files;
5278.Ve
5279.Sp
5280then \f(CW$a\fR and \f(CW$b\fR are \f(CW$main::a\fR and \f(CW$main::b\fR (or \f(CW$::a\fR and \f(CW$::b\fR),
5281but if you're in the \f(CW\*(C`FooPack\*(C'\fR package, it's the same as typing
5282.Sp
5283.Vb 1
5284\& @articles = sort {$FooPack::b <=> $FooPack::a} @files;
5285.Ve
5286.Sp
5287The comparison function is required to behave. If it returns
5288inconsistent results (sometimes saying \f(CW$x[1]\fR is less than \f(CW$x[2]\fR and
5289sometimes saying the opposite, for example) the results are not
5290well\-defined.
5291.IP "splice \s-1ARRAY\s0,OFFSET,LENGTH,LIST" 8
5292.IX Item "splice ARRAY,OFFSET,LENGTH,LIST"
5293.PD 0
5294.IP "splice \s-1ARRAY\s0,OFFSET,LENGTH" 8
5295.IX Item "splice ARRAY,OFFSET,LENGTH"
5296.IP "splice \s-1ARRAY\s0,OFFSET" 8
5297.IX Item "splice ARRAY,OFFSET"
5298.IP "splice \s-1ARRAY\s0" 8
5299.IX Item "splice ARRAY"
5300.PD
5301Removes the elements designated by \s-1OFFSET\s0 and \s-1LENGTH\s0 from an array, and
5302replaces them with the elements of \s-1LIST\s0, if any. In list context,
5303returns the elements removed from the array. In scalar context,
5304returns the last element removed, or \f(CW\*(C`undef\*(C'\fR if no elements are
5305removed. The array grows or shrinks as necessary.
5306If \s-1OFFSET\s0 is negative then it starts that far from the end of the array.
5307If \s-1LENGTH\s0 is omitted, removes everything from \s-1OFFSET\s0 onward.
5308If \s-1LENGTH\s0 is negative, removes the elements from \s-1OFFSET\s0 onward
5309except for \-LENGTH elements at the end of the array.
5310If both \s-1OFFSET\s0 and \s-1LENGTH\s0 are omitted, removes everything. If \s-1OFFSET\s0 is
5311past the end of the array, perl issues a warning, and splices at the
5312end of the array.
5313.Sp
5314The following equivalences hold (assuming \f(CW\*(C`$[ == 0\*(C'\fR):
5315.Sp
5316.Vb 5
5317\& push(@a,$x,$y) splice(@a,@a,0,$x,$y)
5318\& pop(@a) splice(@a,-1)
5319\& shift(@a) splice(@a,0,1)
5320\& unshift(@a,$x,$y) splice(@a,0,0,$x,$y)
5321\& $a[$x] = $y splice(@a,$x,1,$y)
5322.Ve
5323.Sp
5324Example, assuming array lengths are passed before arrays:
5325.Sp
5326.Vb 10
5327\& sub aeq { # compare two list values
5328\& my(@a) = splice(@_,0,shift);
5329\& my(@b) = splice(@_,0,shift);
5330\& return 0 unless @a == @b; # same len?
5331\& while (@a) {
5332\& return 0 if pop(@a) ne pop(@b);
5333\& }
5334\& return 1;
5335\& }
5336\& if (&aeq($len,@foo[1..$len],0+@bar,@bar)) { ... }
5337.Ve
5338.IP "split /PATTERN/,EXPR,LIMIT" 8
5339.IX Item "split /PATTERN/,EXPR,LIMIT"
5340.PD 0
5341.IP "split /PATTERN/,EXPR" 8
5342.IX Item "split /PATTERN/,EXPR"
5343.IP "split /PATTERN/" 8
5344.IX Item "split /PATTERN/"
5345.IP "split" 8
5346.IX Item "split"
5347.PD
5348Splits a string into a list of strings and returns that list. By default,
5349empty leading fields are preserved, and empty trailing ones are deleted.
5350.Sp
5351In scalar context, returns the number of fields found and splits into
5352the \f(CW@_\fR array. Use of split in scalar context is deprecated, however,
5353because it clobbers your subroutine arguments.
5354.Sp
5355If \s-1EXPR\s0 is omitted, splits the \f(CW$_\fR string. If \s-1PATTERN\s0 is also omitted,
5356splits on whitespace (after skipping any leading whitespace). Anything
5357matching \s-1PATTERN\s0 is taken to be a delimiter separating the fields. (Note
5358that the delimiter may be longer than one character.)
5359.Sp
5360If \s-1LIMIT\s0 is specified and positive, it represents the maximum number
5361of fields the \s-1EXPR\s0 will be split into, though the actual number of
5362fields returned depends on the number of times \s-1PATTERN\s0 matches within
5363\&\s-1EXPR\s0. If \s-1LIMIT\s0 is unspecified or zero, trailing null fields are
5364stripped (which potential users of \f(CW\*(C`pop\*(C'\fR would do well to remember).
5365If \s-1LIMIT\s0 is negative, it is treated as if an arbitrarily large \s-1LIMIT\s0
5366had been specified. Note that splitting an \s-1EXPR\s0 that evaluates to the
5367empty string always returns the empty list, regardless of the \s-1LIMIT\s0
5368specified.
5369.Sp
5370A pattern matching the null string (not to be confused with
5371a null pattern \f(CW\*(C`//\*(C'\fR, which is just one member of the set of patterns
5372matching a null string) will split the value of \s-1EXPR\s0 into separate
5373characters at each point it matches that way. For example:
5374.Sp
5375.Vb 1
5376\& print join(':', split(/ */, 'hi there'));
5377.Ve
5378.Sp
5379produces the output 'h:i:t:h:e:r:e'.
5380.Sp
5381Using the empty pattern \f(CW\*(C`//\*(C'\fR specifically matches the null string, and is
5382not be confused with the use of \f(CW\*(C`//\*(C'\fR to mean \*(L"the last successful pattern
5383match\*(R".
5384.Sp
5385Empty leading (or trailing) fields are produced when there are positive width
5386matches at the beginning (or end) of the string; a zero-width match at the
5387beginning (or end) of the string does not produce an empty field. For
5388example:
5389.Sp
5390.Vb 1
5391\& print join(':', split(/(?=\ew)/, 'hi there!'));
5392.Ve
5393.Sp
5394produces the output 'h:i :t:h:e:r:e!'.
5395.Sp
5396The \s-1LIMIT\s0 parameter can be used to split a line partially
5397.Sp
5398.Vb 1
5399\& ($login, $passwd, $remainder) = split(/:/, $_, 3);
5400.Ve
5401.Sp
5402When assigning to a list, if \s-1LIMIT\s0 is omitted, Perl supplies a \s-1LIMIT\s0
5403one larger than the number of variables in the list, to avoid
5404unnecessary work. For the list above \s-1LIMIT\s0 would have been 4 by
5405default. In time critical applications it behooves you not to split
5406into more fields than you really need.
5407.Sp
5408If the \s-1PATTERN\s0 contains parentheses, additional list elements are
5409created from each matching substring in the delimiter.
5410.Sp
5411.Vb 1
5412\& split(/([,-])/, "1-10,20", 3);
5413.Ve
5414.Sp
5415produces the list value
5416.Sp
5417.Vb 1
5418\& (1, '-', 10, ',', 20)
5419.Ve
5420.Sp
5421If you had the entire header of a normal Unix email message in \f(CW$header\fR,
5422you could split it up into fields and their values this way:
5423.Sp
5424.Vb 2
5425\& $header =~ s/\en\es+/ /g; # fix continuation lines
5426\& %hdrs = (UNIX_FROM => split /^(\eS*?):\es*/m, $header);
5427.Ve
5428.Sp
5429The pattern \f(CW\*(C`/PATTERN/\*(C'\fR may be replaced with an expression to specify
5430patterns that vary at runtime. (To do runtime compilation only once,
5431use \f(CW\*(C`/$variable/o\*(C'\fR.)
5432.Sp
5433As a special case, specifying a \s-1PATTERN\s0 of space (\f(CW' '\fR) will split on
5434white space just as \f(CW\*(C`split\*(C'\fR with no arguments does. Thus, \f(CW\*(C`split(' ')\*(C'\fR can
5435be used to emulate \fBawk\fR's default behavior, whereas \f(CW\*(C`split(/ /)\*(C'\fR
5436will give you as many null initial fields as there are leading spaces.
5437A \f(CW\*(C`split\*(C'\fR on \f(CW\*(C`/\es+/\*(C'\fR is like a \f(CW\*(C`split(' ')\*(C'\fR except that any leading
5438whitespace produces a null first field. A \f(CW\*(C`split\*(C'\fR with no arguments
5439really does a \f(CW\*(C`split(' ', $_)\*(C'\fR internally.
5440.Sp
5441A \s-1PATTERN\s0 of \f(CW\*(C`/^/\*(C'\fR is treated as if it were \f(CW\*(C`/^/m\*(C'\fR, since it isn't
5442much use otherwise.
5443.Sp
5444Example:
5445.Sp
5446.Vb 7
5447\& open(PASSWD, '/etc/passwd');
5448\& while (<PASSWD>) {
5449\& chomp;
5450\& ($login, $passwd, $uid, $gid,
5451\& $gcos, $home, $shell) = split(/:/);
5452\& #...
5453\& }
5454.Ve
5455.Sp
5456As with regular pattern matching, any capturing parentheses that are not
5457matched in a \f(CW\*(C`split()\*(C'\fR will be set to \f(CW\*(C`undef\*(C'\fR when returned:
5458.Sp
5459.Vb 2
5460\& @fields = split /(A)|B/, "1A2B3";
5461\& # @fields is (1, 'A', 2, undef, 3)
5462.Ve
5463.IP "sprintf \s-1FORMAT\s0, \s-1LIST\s0" 8
5464.IX Item "sprintf FORMAT, LIST"
5465Returns a string formatted by the usual \f(CW\*(C`printf\*(C'\fR conventions of the C
5466library function \f(CW\*(C`sprintf\*(C'\fR. See below for more details
5467and see \fIsprintf\fR\|(3) or \fIprintf\fR\|(3) on your system for an explanation of
5468the general principles.
5469.Sp
5470For example:
5471.Sp
5472.Vb 2
5473\& # Format number with up to 8 leading zeroes
5474\& $result = sprintf("%08d", $number);
5475.Ve
5476.Sp
5477.Vb 2
5478\& # Round number to 3 digits after decimal point
5479\& $rounded = sprintf("%.3f", $number);
5480.Ve
5481.Sp
5482Perl does its own \f(CW\*(C`sprintf\*(C'\fR formatting\*(--it emulates the C
5483function \f(CW\*(C`sprintf\*(C'\fR, but it doesn't use it (except for floating-point
5484numbers, and even then only the standard modifiers are allowed). As a
5485result, any non-standard extensions in your local \f(CW\*(C`sprintf\*(C'\fR are not
5486available from Perl.
5487.Sp
5488Unlike \f(CW\*(C`printf\*(C'\fR, \f(CW\*(C`sprintf\*(C'\fR does not do what you probably mean when you
5489pass it an array as your first argument. The array is given scalar context,
5490and instead of using the 0th element of the array as the format, Perl will
5491use the count of elements in the array as the format, which is almost never
5492useful.
5493.Sp
5494Perl's \f(CW\*(C`sprintf\*(C'\fR permits the following universally-known conversions:
5495.Sp
5496.Vb 10
5497\& %% a percent sign
5498\& %c a character with the given number
5499\& %s a string
5500\& %d a signed integer, in decimal
5501\& %u an unsigned integer, in decimal
5502\& %o an unsigned integer, in octal
5503\& %x an unsigned integer, in hexadecimal
5504\& %e a floating-point number, in scientific notation
5505\& %f a floating-point number, in fixed decimal notation
5506\& %g a floating-point number, in %e or %f notation
5507.Ve
5508.Sp
5509In addition, Perl permits the following widely-supported conversions:
5510.Sp
5511.Vb 7
5512\& %X like %x, but using upper-case letters
5513\& %E like %e, but using an upper-case "E"
5514\& %G like %g, but with an upper-case "E" (if applicable)
5515\& %b an unsigned integer, in binary
5516\& %p a pointer (outputs the Perl value's address in hexadecimal)
5517\& %n special: *stores* the number of characters output so far
5518\& into the next variable in the parameter list
5519.Ve
5520.Sp
5521Finally, for backward (and we do mean \*(L"backward\*(R") compatibility, Perl
5522permits these unnecessary but widely-supported conversions:
5523.Sp
5524.Vb 5
5525\& %i a synonym for %d
5526\& %D a synonym for %ld
5527\& %U a synonym for %lu
5528\& %O a synonym for %lo
5529\& %F a synonym for %f
5530.Ve
5531.Sp
5532Note that the number of exponent digits in the scientific notation produced
5533by \f(CW%e\fR, \f(CW%E\fR, \f(CW%g\fR and \f(CW%G\fR for numbers with the modulus of the
5534exponent less than 100 is system\-dependent: it may be three or less
5535(zero\-padded as necessary). In other words, 1.23 times ten to the
553699th may be either \*(L"1.23e99\*(R" or \*(L"1.23e099\*(R".
5537.Sp
5538Between the \f(CW\*(C`%\*(C'\fR and the format letter, you may specify a number of
5539additional attributes controlling the interpretation of the format.
5540In order, these are:
5541.RS 8
5542.IP "format parameter index" 4
5543.IX Item "format parameter index"
5544An explicit format parameter index, such as \f(CW\*(C`2$\*(C'\fR. By default sprintf
5545will format the next unused argument in the list, but this allows you
5546to take the arguments out of order. Eg:
5547.Sp
5548.Vb 2
5549\& printf '%2$d %1$d', 12, 34; # prints "34 12"
5550\& printf '%3$d %d %1$d', 1, 2, 3; # prints "3 1 1"
5551.Ve
5552.IP "flags" 4
5553.IX Item "flags"
5554one or more of:
5555 space prefix positive number with a space
5556 + prefix positive number with a plus sign
5557 \- left-justify within the field
5558 0 use zeros, not spaces, to right-justify
5559 # prefix non-zero octal with \*(L"0\*(R", non-zero hex with \*(L"0x\*(R",
5560 non-zero binary with \*(L"0b\*(R"
5561.Sp
5562For example:
5563.Sp
5564.Vb 6
5565\& printf '<% d>', 12; # prints "< 12>"
5566\& printf '<%+d>', 12; # prints "<+12>"
5567\& printf '<%6s>', 12; # prints "< 12>"
5568\& printf '<%-6s>', 12; # prints "<12 >"
5569\& printf '<%06s>', 12; # prints "<000012>"
5570\& printf '<%#x>', 12; # prints "<0xc>"
5571.Ve
5572.IP "vector flag" 4
5573.IX Item "vector flag"
5574The vector flag \f(CW\*(C`v\*(C'\fR, optionally specifying the join string to use.
5575This flag tells perl to interpret the supplied string as a vector
5576of integers, one for each character in the string, separated by
5577a given string (a dot \f(CW\*(C`.\*(C'\fR by default). This can be useful for
5578displaying ordinal values of characters in arbitrary strings:
5579.Sp
5580.Vb 1
5581\& printf "version is v%vd\en", $^V; # Perl's version
5582.Ve
5583.Sp
5584Put an asterisk \f(CW\*(C`*\*(C'\fR before the \f(CW\*(C`v\*(C'\fR to override the string to
5585use to separate the numbers:
5586.Sp
5587.Vb 2
5588\& printf "address is %*vX\en", ":", $addr; # IPv6 address
5589\& printf "bits are %0*v8b\en", " ", $bits; # random bitstring
5590.Ve
5591.Sp
5592You can also explicitly specify the argument number to use for
5593the join string using eg \f(CW\*(C`*2$v\*(C'\fR:
5594.Sp
5595.Vb 1
5596\& printf '%*4$vX %*4$vX %*4$vX', @addr[1..3], ":"; # 3 IPv6 addresses
5597.Ve
5598.IP "(minimum) width" 4
5599.IX Item "(minimum) width"
5600Arguments are usually formatted to be only as wide as required to
5601display the given value. You can override the width by putting
5602a number here, or get the width from the next argument (with \f(CW\*(C`*\*(C'\fR)
5603or from a specified argument (with eg \f(CW\*(C`*2$\*(C'\fR):
5604.Sp
5605.Vb 5
5606\& printf '<%s>', "a"; # prints "<a>"
5607\& printf '<%6s>', "a"; # prints "< a>"
5608\& printf '<%*s>', 6, "a"; # prints "< a>"
5609\& printf '<%*2$s>', "a", 6; # prints "< a>"
5610\& printf '<%2s>', "long"; # prints "<long>" (does not truncate)
5611.Ve
5612.Sp
5613If a field width obtained through \f(CW\*(C`*\*(C'\fR is negative, it has the same
5614effect as the \f(CW\*(C`\-\*(C'\fR flag: left\-justification.
5615.IP "precision, or maximum width" 4
5616.IX Item "precision, or maximum width"
5617You can specify a precision (for numeric conversions) or a maximum
5618width (for string conversions) by specifying a \f(CW\*(C`.\*(C'\fR followed by a number.
5619For floating point formats, this specifies the number of decimal places
5620to show (the default being 6), eg:
5621.Sp
5622.Vb 6
5623\& # these examples are subject to system-specific variation
5624\& printf '<%f>', 1; # prints "<1.000000>"
5625\& printf '<%.1f>', 1; # prints "<1.0>"
5626\& printf '<%.0f>', 1; # prints "<1>"
5627\& printf '<%e>', 10; # prints "<1.000000e+01>"
5628\& printf '<%.1e>', 10; # prints "<1.0e+01>"
5629.Ve
5630.Sp
5631For integer conversions, specifying a precision implies that the
5632output of the number itself should be zero-padded to this width:
5633.Sp
5634.Vb 3
5635\& printf '<%.6x>', 1; # prints "<000001>"
5636\& printf '<%#.6x>', 1; # prints "<0x000001>"
5637\& printf '<%-10.6x>', 1; # prints "<000001 >"
5638.Ve
5639.Sp
5640For string conversions, specifying a precision truncates the string
5641to fit in the specified width:
5642.Sp
5643.Vb 2
5644\& printf '<%.5s>', "truncated"; # prints "<trunc>"
5645\& printf '<%10.5s>', "truncated"; # prints "< trunc>"
5646.Ve
5647.Sp
5648You can also get the precision from the next argument using \f(CW\*(C`.*\*(C'\fR:
5649.Sp
5650.Vb 2
5651\& printf '<%.6x>', 1; # prints "<000001>"
5652\& printf '<%.*x>', 6, 1; # prints "<000001>"
5653.Ve
5654.Sp
5655You cannot currently get the precision from a specified number,
5656but it is intended that this will be possible in the future using
5657eg \f(CW\*(C`.*2$\*(C'\fR:
5658.Sp
5659.Vb 1
5660\& printf '<%.*2$x>', 1, 6; # INVALID, but in future will print "<000001>"
5661.Ve
5662.IP "size" 4
5663.IX Item "size"
5664For numeric conversions, you can specify the size to interpret the
5665number as using \f(CW\*(C`l\*(C'\fR, \f(CW\*(C`h\*(C'\fR, \f(CW\*(C`V\*(C'\fR, \f(CW\*(C`q\*(C'\fR, \f(CW\*(C`L\*(C'\fR or \f(CW\*(C`ll\*(C'\fR. For integer
5666conversions, numbers are usually assumed to be whatever the default
5667integer size is on your platform (usually 32 or 64 bits), but you
5668can override this to use instead one of the standard C types, as
5669supported by the compiler used to build Perl:
5670.Sp
5671.Vb 4
5672\& l interpret integer as C type "long" or "unsigned long"
5673\& h interpret integer as C type "short" or "unsigned short"
5674\& q, L or ll interpret integer as C type "long long" or "unsigned long long"
5675\& (if your platform supports such a type, else it is an error)
5676.Ve
5677.Sp
5678For floating point conversions, numbers are usually assumed to be
5679the default floating point size on your platform (double or long double),
5680but you can force 'long double' with \f(CW\*(C`q\*(C'\fR, \f(CW\*(C`L\*(C'\fR or \f(CW\*(C`ll\*(C'\fR if your
5681platform supports them.
5682.Sp
5683The size specifier 'V' has no effect for Perl code, but it supported
5684for compatibility with \s-1XS\s0 code; it means 'use the standard size for
5685a Perl integer (or floating-point number)', which is already the
5686default for Perl code.
5687.IP "order of arguments" 4
5688.IX Item "order of arguments"
5689Normally, sprintf takes the next unused argument as the value to
5690format for each format specification. If the format specification
5691uses \f(CW\*(C`*\*(C'\fR to require additional arguments, these are consumed from
5692the argument list in the order in which they appear in the format
5693specification \fIbefore\fR the value to format. Where an argument is
5694specified using an explicit index, this does not affect the normal
5695order for the arguments (even when the explicitly specified index
5696would have been the next argument in any case).
5697.Sp
5698So:
5699.Sp
5700.Vb 1
5701\& printf '<%*.*s>', $a, $b, $c;
5702.Ve
5703.Sp
5704would use \f(CW$a\fR for the width, \f(CW$b\fR for the precision and \f(CW$c\fR
5705as the value to format, while:
5706.Sp
5707.Vb 1
5708\& print '<%*1$.*s>', $a, $b;
5709.Ve
5710.Sp
5711would use \f(CW$a\fR for the width and the precision, and \f(CW$b\fR as the
5712value to format.
5713.Sp
5714Here are some more examples \- beware that when using an explicit
5715index, the \f(CW\*(C`$\*(C'\fR may need to be escaped:
5716.Sp
5717.Vb 4
5718\& printf "%2\e$d %d\en", 12, 34; # will print "34 12\en"
5719\& printf "%2\e$d %d %d\en", 12, 34; # will print "34 12 34\en"
5720\& printf "%3\e$d %d %d\en", 12, 34, 56; # will print "56 12 34\en"
5721\& printf "%2\e$*3\e$d %d\en", 12, 34, 3; # will print " 34 12\en"
5722.Ve
5723.RE
5724.RS 8
5725.Sp
5726If \f(CW\*(C`use locale\*(C'\fR is in effect, the character used for the decimal
5727point in formatted real numbers is affected by the \s-1LC_NUMERIC\s0 locale.
5728See perllocale.
5729.Sp
5730If Perl understands \*(L"quads\*(R" (64\-bit integers) (this requires
5731either that the platform natively support quads or that Perl
5732be specifically compiled to support quads), the characters
5733.Sp
5734.Vb 1
5735\& d u o x X b i D U O
5736.Ve
5737.Sp
5738print quads, and they may optionally be preceded by
5739.Sp
5740.Vb 1
5741\& ll L q
5742.Ve
5743.Sp
5744For example
5745.Sp
5746.Vb 1
5747\& %lld %16LX %qo
5748.Ve
5749.Sp
5750You can find out whether your Perl supports quads via Config:
5751.Sp
5752.Vb 3
5753\& use Config;
5754\& ($Config{use64bitint} eq 'define' || $Config{longsize} == 8) &&
5755\& print "quads\en";
5756.Ve
5757.Sp
5758If Perl understands \*(L"long doubles\*(R" (this requires that the platform
5759support long doubles), the flags
5760.Sp
5761.Vb 1
5762\& e f g E F G
5763.Ve
5764.Sp
5765may optionally be preceded by
5766.Sp
5767.Vb 1
5768\& ll L
5769.Ve
5770.Sp
5771For example
5772.Sp
5773.Vb 1
5774\& %llf %Lg
5775.Ve
5776.Sp
5777You can find out whether your Perl supports long doubles via Config:
5778.Sp
5779.Vb 2
5780\& use Config;
5781\& $Config{d_longdbl} eq 'define' && print "long doubles\en";
5782.Ve
5783.RE
5784.IP "sqrt \s-1EXPR\s0" 8
5785.IX Item "sqrt EXPR"
5786.PD 0
5787.IP "sqrt" 8
5788.IX Item "sqrt"
5789.PD
5790Return the square root of \s-1EXPR\s0. If \s-1EXPR\s0 is omitted, returns square
5791root of \f(CW$_\fR. Only works on non-negative operands, unless you've
5792loaded the standard Math::Complex module.
5793.Sp
5794.Vb 2
5795\& use Math::Complex;
5796\& print sqrt(-2); # prints 1.4142135623731i
5797.Ve
5798.IP "srand \s-1EXPR\s0" 8
5799.IX Item "srand EXPR"
5800.PD 0
5801.IP "srand" 8
5802.IX Item "srand"
5803.PD
5804Sets the random number seed for the \f(CW\*(C`rand\*(C'\fR operator.
5805.Sp
5806The point of the function is to \*(L"seed\*(R" the \f(CW\*(C`rand\*(C'\fR function so that
5807\&\f(CW\*(C`rand\*(C'\fR can produce a different sequence each time you run your
5808program.
5809.Sp
5810If \fIsrand()\fR is not called explicitly, it is called implicitly at the
5811first use of the \f(CW\*(C`rand\*(C'\fR operator. However, this was not the case in
5812versions of Perl before 5.004, so if your script will run under older
5813Perl versions, it should call \f(CW\*(C`srand\*(C'\fR.
5814.Sp
5815Most programs won't even call \fIsrand()\fR at all, except those that
5816need a cryptographically-strong starting point rather than the
5817generally acceptable default, which is based on time of day,
5818process \s-1ID\s0, and memory allocation, or the \fI/dev/urandom\fR device,
5819if available.
5820.Sp
5821You can call srand($seed) with the same \f(CW$seed\fR to reproduce the
5822\&\fIsame\fR sequence from \fIrand()\fR, but this is usually reserved for
5823generating predictable results for testing or debugging.
5824Otherwise, don't call \fIsrand()\fR more than once in your program.
5825.Sp
5826Do \fBnot\fR call \fIsrand()\fR (i.e. without an argument) more than once in
5827a script. The internal state of the random number generator should
5828contain more entropy than can be provided by any seed, so calling
5829\&\fIsrand()\fR again actually \fIloses\fR randomness.
5830.Sp
5831Most implementations of \f(CW\*(C`srand\*(C'\fR take an integer and will silently
5832truncate decimal numbers. This means \f(CW\*(C`srand(42)\*(C'\fR will usually
5833produce the same results as \f(CW\*(C`srand(42.1)\*(C'\fR. To be safe, always pass
5834\&\f(CW\*(C`srand\*(C'\fR an integer.
5835.Sp
5836In versions of Perl prior to 5.004 the default seed was just the
5837current \f(CW\*(C`time\*(C'\fR. This isn't a particularly good seed, so many old
5838programs supply their own seed value (often \f(CW\*(C`time ^ $$\*(C'\fR or \f(CW\*(C`time ^
5839($$ + ($$ << 15))\*(C'\fR), but that isn't necessary any more.
5840.Sp
5841Note that you need something much more random than the default seed for
5842cryptographic purposes. Checksumming the compressed output of one or more
5843rapidly changing operating system status programs is the usual method. For
5844example:
5845.Sp
5846.Vb 1
5847\& srand (time ^ $$ ^ unpack "%L*", `ps axww | gzip`);
5848.Ve
5849.Sp
5850If you're particularly concerned with this, see the \f(CW\*(C`Math::TrulyRandom\*(C'\fR
5851module in \s-1CPAN\s0.
5852.Sp
5853Frequently called programs (like \s-1CGI\s0 scripts) that simply use
5854.Sp
5855.Vb 1
5856\& time ^ $$
5857.Ve
5858.Sp
5859for a seed can fall prey to the mathematical property that
5860.Sp
5861.Vb 1
5862\& a^b == (a+1)^(b+1)
5863.Ve
5864.Sp
5865one-third of the time. So don't do that.
5866.IP "stat \s-1FILEHANDLE\s0" 8
5867.IX Item "stat FILEHANDLE"
5868.PD 0
5869.IP "stat \s-1EXPR\s0" 8
5870.IX Item "stat EXPR"
5871.IP "stat" 8
5872.IX Item "stat"
5873.PD
5874Returns a 13\-element list giving the status info for a file, either
5875the file opened via \s-1FILEHANDLE\s0, or named by \s-1EXPR\s0. If \s-1EXPR\s0 is omitted,
5876it stats \f(CW$_\fR. Returns a null list if the stat fails. Typically used
5877as follows:
5878.Sp
5879.Vb 3
5880\& ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
5881\& $atime,$mtime,$ctime,$blksize,$blocks)
5882\& = stat($filename);
5883.Ve
5884.Sp
5885Not all fields are supported on all filesystem types. Here are the
5886meaning of the fields:
5887.Sp
5888.Vb 13
5889\& 0 dev device number of filesystem
5890\& 1 ino inode number
5891\& 2 mode file mode (type and permissions)
5892\& 3 nlink number of (hard) links to the file
5893\& 4 uid numeric user ID of file's owner
5894\& 5 gid numeric group ID of file's owner
5895\& 6 rdev the device identifier (special files only)
5896\& 7 size total size of file, in bytes
5897\& 8 atime last access time in seconds since the epoch
5898\& 9 mtime last modify time in seconds since the epoch
5899\& 10 ctime inode change time in seconds since the epoch (*)
5900\& 11 blksize preferred block size for file system I/O
5901\& 12 blocks actual number of blocks allocated
5902.Ve
5903.Sp
5904(The epoch was at 00:00 January 1, 1970 \s-1GMT\s0.)
5905.Sp
5906(*) The ctime field is non\-portable, in particular you cannot expect
5907it to be a \*(L"creation time\*(R", see \*(L"Files and Filesystems\*(R" in perlport
5908for details.
5909.Sp
5910If stat is passed the special filehandle consisting of an underline, no
5911stat is done, but the current contents of the stat structure from the
5912last stat or filetest are returned. Example:
5913.Sp
5914.Vb 3
5915\& if (-x $file && (($d) = stat(_)) && $d < 0) {
5916\& print "$file is executable NFS file\en";
5917\& }
5918.Ve
5919.Sp
5920(This works on machines only for which the device number is negative
5921under \s-1NFS\s0.)
5922.Sp
5923Because the mode contains both the file type and its permissions, you
5924should mask off the file type portion and (s)printf using a \f(CW"%o"\fR
5925if you want to see the real permissions.
5926.Sp
5927.Vb 2
5928\& $mode = (stat($filename))[2];
5929\& printf "Permissions are %04o\en", $mode & 07777;
5930.Ve
5931.Sp
5932In scalar context, \f(CW\*(C`stat\*(C'\fR returns a boolean value indicating success
5933or failure, and, if successful, sets the information associated with
5934the special filehandle \f(CW\*(C`_\*(C'\fR.
5935.Sp
5936The File::stat module provides a convenient, by-name access mechanism:
5937.Sp
5938.Vb 5
5939\& use File::stat;
5940\& $sb = stat($filename);
5941\& printf "File is %s, size is %s, perm %04o, mtime %s\en",
5942\& $filename, $sb->size, $sb->mode & 07777,
5943\& scalar localtime $sb->mtime;
5944.Ve
5945.Sp
5946You can import symbolic mode constants (\f(CW\*(C`S_IF*\*(C'\fR) and functions
5947(\f(CW\*(C`S_IS*\*(C'\fR) from the Fcntl module:
5948.Sp
5949.Vb 1
5950\& use Fcntl ':mode';
5951.Ve
5952.Sp
5953.Vb 1
5954\& $mode = (stat($filename))[2];
5955.Ve
5956.Sp
5957.Vb 3
5958\& $user_rwx = ($mode & S_IRWXU) >> 6;
5959\& $group_read = ($mode & S_IRGRP) >> 3;
5960\& $other_execute = $mode & S_IXOTH;
5961.Ve
5962.Sp
5963.Vb 1
5964\& printf "Permissions are %04o\en", S_ISMODE($mode), "\en";
5965.Ve
5966.Sp
5967.Vb 2
5968\& $is_setuid = $mode & S_ISUID;
5969\& $is_setgid = S_ISDIR($mode);
5970.Ve
5971.Sp
5972You could write the last two using the \f(CW\*(C`\-u\*(C'\fR and \f(CW\*(C`\-d\*(C'\fR operators.
5973The commonly available S_IF* constants are
5974.Sp
5975.Vb 1
5976\& # Permissions: read, write, execute, for user, group, others.
5977.Ve
5978.Sp
5979.Vb 3
5980\& S_IRWXU S_IRUSR S_IWUSR S_IXUSR
5981\& S_IRWXG S_IRGRP S_IWGRP S_IXGRP
5982\& S_IRWXO S_IROTH S_IWOTH S_IXOTH
5983.Ve
5984.Sp
5985.Vb 1
5986\& # Setuid/Setgid/Stickiness.
5987.Ve
5988.Sp
5989.Vb 1
5990\& S_ISUID S_ISGID S_ISVTX S_ISTXT
5991.Ve
5992.Sp
5993.Vb 1
5994\& # File types. Not necessarily all are available on your system.
5995.Ve
5996.Sp
5997.Vb 1
5998\& S_IFREG S_IFDIR S_IFLNK S_IFBLK S_ISCHR S_IFIFO S_IFSOCK S_IFWHT S_ENFMT
5999.Ve
6000.Sp
6001.Vb 1
6002\& # The following are compatibility aliases for S_IRUSR, S_IWUSR, S_IXUSR.
6003.Ve
6004.Sp
6005.Vb 1
6006\& S_IREAD S_IWRITE S_IEXEC
6007.Ve
6008.Sp
6009and the S_IF* functions are
6010.Sp
6011.Vb 2
6012\& S_IFMODE($mode) the part of $mode containing the permission bits
6013\& and the setuid/setgid/sticky bits
6014.Ve
6015.Sp
6016.Vb 3
6017\& S_IFMT($mode) the part of $mode containing the file type
6018\& which can be bit-anded with e.g. S_IFREG
6019\& or with the following functions
6020.Ve
6021.Sp
6022.Vb 1
6023\& # The operators -f, -d, -l, -b, -c, -p, and -s.
6024.Ve
6025.Sp
6026.Vb 2
6027\& S_ISREG($mode) S_ISDIR($mode) S_ISLNK($mode)
6028\& S_ISBLK($mode) S_ISCHR($mode) S_ISFIFO($mode) S_ISSOCK($mode)
6029.Ve
6030.Sp
6031.Vb 3
6032\& # No direct -X operator counterpart, but for the first one
6033\& # the -g operator is often equivalent. The ENFMT stands for
6034\& # record flocking enforcement, a platform-dependent feature.
6035.Ve
6036.Sp
6037.Vb 1
6038\& S_ISENFMT($mode) S_ISWHT($mode)
6039.Ve
6040.Sp
6041See your native \fIchmod\fR\|(2) and \fIstat\fR\|(2) documentation for more details
6042about the S_* constants.
6043.IP "study \s-1SCALAR\s0" 8
6044.IX Item "study SCALAR"
6045.PD 0
6046.IP "study" 8
6047.IX Item "study"
6048.PD
6049Takes extra time to study \s-1SCALAR\s0 (\f(CW$_\fR if unspecified) in anticipation of
6050doing many pattern matches on the string before it is next modified.
6051This may or may not save time, depending on the nature and number of
6052patterns you are searching on, and on the distribution of character
6053frequencies in the string to be searched\*(--you probably want to compare
6054run times with and without it to see which runs faster. Those loops
6055which scan for many short constant strings (including the constant
6056parts of more complex patterns) will benefit most. You may have only
6057one \f(CW\*(C`study\*(C'\fR active at a time\*(--if you study a different scalar the first
6058is \*(L"unstudied\*(R". (The way \f(CW\*(C`study\*(C'\fR works is this: a linked list of every
6059character in the string to be searched is made, so we know, for
6060example, where all the \f(CW'k'\fR characters are. From each search string,
6061the rarest character is selected, based on some static frequency tables
6062constructed from some C programs and English text. Only those places
6063that contain this \*(L"rarest\*(R" character are examined.)
6064.Sp
6065For example, here is a loop that inserts index producing entries
6066before any line containing a certain pattern:
6067.Sp
6068.Vb 8
6069\& while (<>) {
6070\& study;
6071\& print ".IX foo\en" if /\ebfoo\eb/;
6072\& print ".IX bar\en" if /\ebbar\eb/;
6073\& print ".IX blurfl\en" if /\ebblurfl\eb/;
6074\& # ...
6075\& print;
6076\& }
6077.Ve
6078.Sp
6079In searching for \f(CW\*(C`/\ebfoo\eb/\*(C'\fR, only those locations in \f(CW$_\fR that contain \f(CW\*(C`f\*(C'\fR
6080will be looked at, because \f(CW\*(C`f\*(C'\fR is rarer than \f(CW\*(C`o\*(C'\fR. In general, this is
6081a big win except in pathological cases. The only question is whether
6082it saves you more time than it took to build the linked list in the
6083first place.
6084.Sp
6085Note that if you have to look for strings that you don't know till
6086runtime, you can build an entire loop as a string and \f(CW\*(C`eval\*(C'\fR that to
6087avoid recompiling all your patterns all the time. Together with
6088undefining \f(CW$/\fR to input entire files as one record, this can be very
6089fast, often faster than specialized programs like \fIfgrep\fR\|(1). The following
6090scans a list of files (\f(CW@files\fR) for a list of words (\f(CW@words\fR), and prints
6091out the names of those files that contain a match:
6092.Sp
6093.Vb 12
6094\& $search = 'while (<>) { study;';
6095\& foreach $word (@words) {
6096\& $search .= "++\e$seen{\e$ARGV} if /\e\eb$word\e\eb/;\en";
6097\& }
6098\& $search .= "}";
6099\& @ARGV = @files;
6100\& undef $/;
6101\& eval $search; # this screams
6102\& $/ = "\en"; # put back to normal input delimiter
6103\& foreach $file (sort keys(%seen)) {
6104\& print $file, "\en";
6105\& }
6106.Ve
6107.IP "sub \s-1NAME\s0 \s-1BLOCK\s0" 8
6108.IX Item "sub NAME BLOCK"
6109.PD 0
6110.IP "sub \s-1NAME\s0 (\s-1PROTO\s0) \s-1BLOCK\s0" 8
6111.IX Item "sub NAME (PROTO) BLOCK"
6112.IP "sub \s-1NAME\s0 : \s-1ATTRS\s0 \s-1BLOCK\s0" 8
6113.IX Item "sub NAME : ATTRS BLOCK"
6114.IP "sub \s-1NAME\s0 (\s-1PROTO\s0) : \s-1ATTRS\s0 \s-1BLOCK\s0" 8
6115.IX Item "sub NAME (PROTO) : ATTRS BLOCK"
6116.PD
6117This is subroutine definition, not a real function \fIper se\fR.
6118Without a \s-1BLOCK\s0 it's just a forward declaration. Without a \s-1NAME\s0,
6119it's an anonymous function declaration, and does actually return
6120a value: the \s-1CODE\s0 ref of the closure you just created.
6121.Sp
6122See perlsub and perlref for details about subroutines and
6123references, and attributes and Attribute::Handlers for more
6124information about attributes.
6125.IP "substr \s-1EXPR\s0,OFFSET,LENGTH,REPLACEMENT" 8
6126.IX Item "substr EXPR,OFFSET,LENGTH,REPLACEMENT"
6127.PD 0
6128.IP "substr \s-1EXPR\s0,OFFSET,LENGTH" 8
6129.IX Item "substr EXPR,OFFSET,LENGTH"
6130.IP "substr \s-1EXPR\s0,OFFSET" 8
6131.IX Item "substr EXPR,OFFSET"
6132.PD
6133Extracts a substring out of \s-1EXPR\s0 and returns it. First character is at
6134offset \f(CW0\fR, or whatever you've set \f(CW$[\fR to (but don't do that).
6135If \s-1OFFSET\s0 is negative (or more precisely, less than \f(CW$[\fR), starts
6136that far from the end of the string. If \s-1LENGTH\s0 is omitted, returns
6137everything to the end of the string. If \s-1LENGTH\s0 is negative, leaves that
6138many characters off the end of the string.
6139.Sp
6140You can use the \fIsubstr()\fR function as an lvalue, in which case \s-1EXPR\s0
6141must itself be an lvalue. If you assign something shorter than \s-1LENGTH\s0,
6142the string will shrink, and if you assign something longer than \s-1LENGTH\s0,
6143the string will grow to accommodate it. To keep the string the same
6144length you may need to pad or chop your value using \f(CW\*(C`sprintf\*(C'\fR.
6145.Sp
6146If \s-1OFFSET\s0 and \s-1LENGTH\s0 specify a substring that is partly outside the
6147string, only the part within the string is returned. If the substring
6148is beyond either end of the string, \fIsubstr()\fR returns the undefined
6149value and produces a warning. When used as an lvalue, specifying a
6150substring that is entirely outside the string is a fatal error.
6151Here's an example showing the behavior for boundary cases:
6152.Sp
6153.Vb 5
6154\& my $name = 'fred';
6155\& substr($name, 4) = 'dy'; # $name is now 'freddy'
6156\& my $null = substr $name, 6, 2; # returns '' (no warning)
6157\& my $oops = substr $name, 7; # returns undef, with warning
6158\& substr($name, 7) = 'gap'; # fatal error
6159.Ve
6160.Sp
6161An alternative to using \fIsubstr()\fR as an lvalue is to specify the
6162replacement string as the 4th argument. This allows you to replace
6163parts of the \s-1EXPR\s0 and return what was there before in one operation,
6164just as you can with \fIsplice()\fR.
6165.IP "symlink \s-1OLDFILE\s0,NEWFILE" 8
6166.IX Item "symlink OLDFILE,NEWFILE"
6167Creates a new filename symbolically linked to the old filename.
6168Returns \f(CW1\fR for success, \f(CW0\fR otherwise. On systems that don't support
6169symbolic links, produces a fatal error at run time. To check for that,
6170use eval:
6171.Sp
6172.Vb 1
6173\& $symlink_exists = eval { symlink("",""); 1 };
6174.Ve
6175.IP "syscall \s-1LIST\s0" 8
6176.IX Item "syscall LIST"
6177Calls the system call specified as the first element of the list,
6178passing the remaining elements as arguments to the system call. If
6179unimplemented, produces a fatal error. The arguments are interpreted
6180as follows: if a given argument is numeric, the argument is passed as
6181an int. If not, the pointer to the string value is passed. You are
6182responsible to make sure a string is pre-extended long enough to
6183receive any result that might be written into a string. You can't use a
6184string literal (or other read-only string) as an argument to \f(CW\*(C`syscall\*(C'\fR
6185because Perl has to assume that any string pointer might be written
6186through. If your
6187integer arguments are not literals and have never been interpreted in a
6188numeric context, you may need to add \f(CW0\fR to them to force them to look
6189like numbers. This emulates the \f(CW\*(C`syswrite\*(C'\fR function (or vice versa):
6190.Sp
6191.Vb 3
6192\& require 'syscall.ph'; # may need to run h2ph
6193\& $s = "hi there\en";
6194\& syscall(&SYS_write, fileno(STDOUT), $s, length $s);
6195.Ve
6196.Sp
6197Note that Perl supports passing of up to only 14 arguments to your system call,
6198which in practice should usually suffice.
6199.Sp
6200Syscall returns whatever value returned by the system call it calls.
6201If the system call fails, \f(CW\*(C`syscall\*(C'\fR returns \f(CW\*(C`\-1\*(C'\fR and sets \f(CW$!\fR (errno).
6202Note that some system calls can legitimately return \f(CW\*(C`\-1\*(C'\fR. The proper
6203way to handle such calls is to assign \f(CW\*(C`$!=0;\*(C'\fR before the call and
6204check the value of \f(CW$!\fR if syscall returns \f(CW\*(C`\-1\*(C'\fR.
6205.Sp
6206There's a problem with \f(CW\*(C`syscall(&SYS_pipe)\*(C'\fR: it returns the file
6207number of the read end of the pipe it creates. There is no way
6208to retrieve the file number of the other end. You can avoid this
6209problem by using \f(CW\*(C`pipe\*(C'\fR instead.
6210.IP "sysopen \s-1FILEHANDLE\s0,FILENAME,MODE" 8
6211.IX Item "sysopen FILEHANDLE,FILENAME,MODE"
6212.PD 0
6213.IP "sysopen \s-1FILEHANDLE\s0,FILENAME,MODE,PERMS" 8
6214.IX Item "sysopen FILEHANDLE,FILENAME,MODE,PERMS"
6215.PD
6216Opens the file whose filename is given by \s-1FILENAME\s0, and associates it
6217with \s-1FILEHANDLE\s0. If \s-1FILEHANDLE\s0 is an expression, its value is used as
6218the name of the real filehandle wanted. This function calls the
6219underlying operating system's \f(CW\*(C`open\*(C'\fR function with the parameters
6220\&\s-1FILENAME\s0, \s-1MODE\s0, \s-1PERMS\s0.
6221.Sp
6222The possible values and flag bits of the \s-1MODE\s0 parameter are
6223system\-dependent; they are available via the standard module \f(CW\*(C`Fcntl\*(C'\fR.
6224See the documentation of your operating system's \f(CW\*(C`open\*(C'\fR to see which
6225values and flag bits are available. You may combine several flags
6226using the \f(CW\*(C`|\*(C'\fR\-operator.
6227.Sp
6228Some of the most common values are \f(CW\*(C`O_RDONLY\*(C'\fR for opening the file in
6229read-only mode, \f(CW\*(C`O_WRONLY\*(C'\fR for opening the file in write-only mode,
6230and \f(CW\*(C`O_RDWR\*(C'\fR for opening the file in read-write mode, and.
6231.Sp
6232For historical reasons, some values work on almost every system
6233supported by perl: zero means read\-only, one means write\-only, and two
6234means read/write. We know that these values do \fInot\fR work under
6235\&\s-1OS/390\s0 & \s-1VM/ESA\s0 Unix and on the Macintosh; you probably don't want to
6236use them in new code.
6237.Sp
6238If the file named by \s-1FILENAME\s0 does not exist and the \f(CW\*(C`open\*(C'\fR call creates
6239it (typically because \s-1MODE\s0 includes the \f(CW\*(C`O_CREAT\*(C'\fR flag), then the value of
6240\&\s-1PERMS\s0 specifies the permissions of the newly created file. If you omit
6241the \s-1PERMS\s0 argument to \f(CW\*(C`sysopen\*(C'\fR, Perl uses the octal value \f(CW0666\fR.
6242These permission values need to be in octal, and are modified by your
6243process's current \f(CW\*(C`umask\*(C'\fR.
6244.Sp
6245In many systems the \f(CW\*(C`O_EXCL\*(C'\fR flag is available for opening files in
6246exclusive mode. This is \fBnot\fR locking: exclusiveness means here that
6247if the file already exists, \fIsysopen()\fR fails. The \f(CW\*(C`O_EXCL\*(C'\fR wins
6248\&\f(CW\*(C`O_TRUNC\*(C'\fR.
6249.Sp
6250Sometimes you may want to truncate an already-existing file: \f(CW\*(C`O_TRUNC\*(C'\fR.
6251.Sp
6252You should seldom if ever use \f(CW0644\fR as argument to \f(CW\*(C`sysopen\*(C'\fR, because
6253that takes away the user's option to have a more permissive umask.
6254Better to omit it. See the \fIperlfunc\fR\|(1) entry on \f(CW\*(C`umask\*(C'\fR for more
6255on this.
6256.Sp
6257Note that \f(CW\*(C`sysopen\*(C'\fR depends on the \fIfdopen()\fR C library function.
6258On many \s-1UNIX\s0 systems, \fIfdopen()\fR is known to fail when file descriptors
6259exceed a certain value, typically 255. If you need more file
6260descriptors than that, consider rebuilding Perl to use the \f(CW\*(C`sfio\*(C'\fR
6261library, or perhaps using the \fIPOSIX::open()\fR function.
6262.Sp
6263See perlopentut for a kinder, gentler explanation of opening files.
6264.IP "sysread \s-1FILEHANDLE\s0,SCALAR,LENGTH,OFFSET" 8
6265.IX Item "sysread FILEHANDLE,SCALAR,LENGTH,OFFSET"
6266.PD 0
6267.IP "sysread \s-1FILEHANDLE\s0,SCALAR,LENGTH" 8
6268.IX Item "sysread FILEHANDLE,SCALAR,LENGTH"
6269.PD
6270Attempts to read \s-1LENGTH\s0 \fIcharacters\fR of data into variable \s-1SCALAR\s0 from
6271the specified \s-1FILEHANDLE\s0, using the system call \fIread\fR\|(2). It bypasses
6272buffered \s-1IO\s0, so mixing this with other kinds of reads, \f(CW\*(C`print\*(C'\fR,
6273\&\f(CW\*(C`write\*(C'\fR, \f(CW\*(C`seek\*(C'\fR, \f(CW\*(C`tell\*(C'\fR, or \f(CW\*(C`eof\*(C'\fR can cause confusion because
6274stdio usually buffers data. Returns the number of characters actually
6275read, \f(CW0\fR at end of file, or undef if there was an error. \s-1SCALAR\s0
6276will be grown or shrunk so that the last byte actually read is the
6277last byte of the scalar after the read.
6278.Sp
6279Note the \fIcharacters\fR: depending on the status of the filehandle,
6280either (8\-bit) bytes or characters are read. By default all
6281filehandles operate on bytes, but for example if the filehandle has
6282been opened with the \f(CW\*(C`:utf8\*(C'\fR I/O layer (see \*(L"open\*(R", and the \f(CW\*(C`open\*(C'\fR
6283pragma, open), the I/O will operate on characters, not bytes.
6284.Sp
6285An \s-1OFFSET\s0 may be specified to place the read data at some place in the
6286string other than the beginning. A negative \s-1OFFSET\s0 specifies
6287placement at that many characters counting backwards from the end of
6288the string. A positive \s-1OFFSET\s0 greater than the length of \s-1SCALAR\s0
6289results in the string being padded to the required size with \f(CW"\e0"\fR
6290bytes before the result of the read is appended.
6291.Sp
6292There is no \fIsyseof()\fR function, which is ok, since \fIeof()\fR doesn't work
6293very well on device files (like ttys) anyway. Use \fIsysread()\fR and check
6294for a return value for 0 to decide whether you're done.
6295.IP "sysseek \s-1FILEHANDLE\s0,POSITION,WHENCE" 8
6296.IX Item "sysseek FILEHANDLE,POSITION,WHENCE"
6297Sets \s-1FILEHANDLE\s0's system position \fIin bytes\fR using the system call
6298\&\fIlseek\fR\|(2). \s-1FILEHANDLE\s0 may be an expression whose value gives the name
6299of the filehandle. The values for \s-1WHENCE\s0 are \f(CW0\fR to set the new
6300position to \s-1POSITION\s0, \f(CW1\fR to set the it to the current position plus
6301\&\s-1POSITION\s0, and \f(CW2\fR to set it to \s-1EOF\s0 plus \s-1POSITION\s0 (typically
6302negative).
6303.Sp
6304Note the \fIin bytes\fR: even if the filehandle has been set to operate
6305on characters (for example by using the \f(CW\*(C`:utf8\*(C'\fR I/O layer), \fItell()\fR
6306will return byte offsets, not character offsets (because implementing
6307that would render \fIsysseek()\fR very slow).
6308.Sp
6309\&\fIsysseek()\fR bypasses normal buffered io, so mixing this with reads (other
6310than \f(CW\*(C`sysread\*(C'\fR, for example &gt;&lt or \fIread()\fR) \f(CW\*(C`print\*(C'\fR, \f(CW\*(C`write\*(C'\fR,
6311\&\f(CW\*(C`seek\*(C'\fR, \f(CW\*(C`tell\*(C'\fR, or \f(CW\*(C`eof\*(C'\fR may cause confusion.
6312.Sp
6313For \s-1WHENCE\s0, you may also use the constants \f(CW\*(C`SEEK_SET\*(C'\fR, \f(CW\*(C`SEEK_CUR\*(C'\fR,
6314and \f(CW\*(C`SEEK_END\*(C'\fR (start of the file, current position, end of the file)
6315from the Fcntl module. Use of the constants is also more portable
6316than relying on 0, 1, and 2. For example to define a \*(L"systell\*(R" function:
6317.Sp
6318.Vb 2
6319\& use Fnctl 'SEEK_CUR';
6320\& sub systell { sysseek($_[0], 0, SEEK_CUR) }
6321.Ve
6322.Sp
6323Returns the new position, or the undefined value on failure. A position
6324of zero is returned as the string \f(CW"0 but true"\fR; thus \f(CW\*(C`sysseek\*(C'\fR returns
6325true on success and false on failure, yet you can still easily determine
6326the new position.
6327.IP "system \s-1LIST\s0" 8
6328.IX Item "system LIST"
6329.PD 0
6330.IP "system \s-1PROGRAM\s0 \s-1LIST\s0" 8
6331.IX Item "system PROGRAM LIST"
6332.PD
6333Does exactly the same thing as \f(CW\*(C`exec LIST\*(C'\fR, except that a fork is
6334done first, and the parent process waits for the child process to
6335complete. Note that argument processing varies depending on the
6336number of arguments. If there is more than one argument in \s-1LIST\s0,
6337or if \s-1LIST\s0 is an array with more than one value, starts the program
6338given by the first element of the list with arguments given by the
6339rest of the list. If there is only one scalar argument, the argument
6340is checked for shell metacharacters, and if there are any, the
6341entire argument is passed to the system's command shell for parsing
6342(this is \f(CW\*(C`/bin/sh \-c\*(C'\fR on Unix platforms, but varies on other
6343platforms). If there are no shell metacharacters in the argument,
6344it is split into words and passed directly to \f(CW\*(C`execvp\*(C'\fR, which is
6345more efficient.
6346.Sp
6347Beginning with v5.6.0, Perl will attempt to flush all files opened for
6348output before any operation that may do a fork, but this may not be
6349supported on some platforms (see perlport). To be safe, you may need
6350to set \f(CW$|\fR ($AUTOFLUSH in English) or call the \f(CW\*(C`autoflush()\*(C'\fR method
6351of \f(CW\*(C`IO::Handle\*(C'\fR on any open handles.
6352.Sp
6353The return value is the exit status of the program as returned by the
6354\&\f(CW\*(C`wait\*(C'\fR call. To get the actual exit value shift right by eight (see below).
6355See also \*(L"exec\*(R". This is \fInot\fR what you want to use to capture
6356the output from a command, for that you should use merely backticks or
6357\&\f(CW\*(C`qx//\*(C'\fR, as described in \*(L"`STRING`\*(R" in perlop. Return value of \-1
6358indicates a failure to start the program (inspect $! for the reason).
6359.Sp
6360Like \f(CW\*(C`exec\*(C'\fR, \f(CW\*(C`system\*(C'\fR allows you to lie to a program about its name if
6361you use the \f(CW\*(C`system PROGRAM LIST\*(C'\fR syntax. Again, see \*(L"exec\*(R".
6362.Sp
6363Because \f(CW\*(C`system\*(C'\fR and backticks block \f(CW\*(C`SIGINT\*(C'\fR and \f(CW\*(C`SIGQUIT\*(C'\fR,
6364killing the program they're running doesn't actually interrupt
6365your program.
6366.Sp
6367.Vb 3
6368\& @args = ("command", "arg1", "arg2");
6369\& system(@args) == 0
6370\& or die "system @args failed: $?"
6371.Ve
6372.Sp
6373You can check all the failure possibilities by inspecting
6374\&\f(CW$?\fR like this:
6375.Sp
6376.Vb 3
6377\& $exit_value = $? >> 8;
6378\& $signal_num = $? & 127;
6379\& $dumped_core = $? & 128;
6380.Ve
6381.Sp
6382or more portably by using the W*() calls of the \s-1POSIX\s0 extension;
6383see perlport for more information.
6384.Sp
6385When the arguments get executed via the system shell, results
6386and return codes will be subject to its quirks and capabilities.
6387See \*(L"`STRING`\*(R" in perlop and \*(L"exec\*(R" for details.
6388.IP "syswrite \s-1FILEHANDLE\s0,SCALAR,LENGTH,OFFSET" 8
6389.IX Item "syswrite FILEHANDLE,SCALAR,LENGTH,OFFSET"
6390.PD 0
6391.IP "syswrite \s-1FILEHANDLE\s0,SCALAR,LENGTH" 8
6392.IX Item "syswrite FILEHANDLE,SCALAR,LENGTH"
6393.IP "syswrite \s-1FILEHANDLE\s0,SCALAR" 8
6394.IX Item "syswrite FILEHANDLE,SCALAR"
6395.PD
6396Attempts to write \s-1LENGTH\s0 characters of data from variable \s-1SCALAR\s0 to
6397the specified \s-1FILEHANDLE\s0, using the system call \fIwrite\fR\|(2). If \s-1LENGTH\s0
6398is not specified, writes whole \s-1SCALAR\s0. It bypasses buffered \s-1IO\s0, so
6399mixing this with reads (other than \f(CWsysread())\fR, \f(CW\*(C`print\*(C'\fR, \f(CW\*(C`write\*(C'\fR,
6400\&\f(CW\*(C`seek\*(C'\fR, \f(CW\*(C`tell\*(C'\fR, or \f(CW\*(C`eof\*(C'\fR may cause confusion because stdio usually
6401buffers data. Returns the number of characters actually written, or
6402\&\f(CW\*(C`undef\*(C'\fR if there was an error. If the \s-1LENGTH\s0 is greater than the
6403available data in the \s-1SCALAR\s0 after the \s-1OFFSET\s0, only as much data as is
6404available will be written.
6405.Sp
6406An \s-1OFFSET\s0 may be specified to write the data from some part of the
6407string other than the beginning. A negative \s-1OFFSET\s0 specifies writing
6408that many characters counting backwards from the end of the string.
6409In the case the \s-1SCALAR\s0 is empty you can use \s-1OFFSET\s0 but only zero offset.
6410.Sp
6411Note the \fIcharacters\fR: depending on the status of the filehandle,
6412either (8\-bit) bytes or characters are written. By default all
6413filehandles operate on bytes, but for example if the filehandle has
6414been opened with the \f(CW\*(C`:utf8\*(C'\fR I/O layer (see \*(L"open\*(R", and the open
6415pragma, open), the I/O will operate on characters, not bytes.
6416.IP "tell \s-1FILEHANDLE\s0" 8
6417.IX Item "tell FILEHANDLE"
6418.PD 0
6419.IP "tell" 8
6420.IX Item "tell"
6421.PD
6422Returns the current position \fIin bytes\fR for \s-1FILEHANDLE\s0, or \-1 on
6423error. \s-1FILEHANDLE\s0 may be an expression whose value gives the name of
6424the actual filehandle. If \s-1FILEHANDLE\s0 is omitted, assumes the file
6425last read.
6426.Sp
6427Note the \fIin bytes\fR: even if the filehandle has been set to
6428operate on characters (for example by using the \f(CW\*(C`:utf8\*(C'\fR open
6429layer), \fItell()\fR will return byte offsets, not character offsets
6430(because that would render \fIseek()\fR and \fItell()\fR rather slow).
6431.Sp
6432The return value of \fItell()\fR for the standard streams like the \s-1STDIN\s0
6433depends on the operating system: it may return \-1 or something else.
6434\&\fItell()\fR on pipes, fifos, and sockets usually returns \-1.
6435.Sp
6436There is no \f(CW\*(C`systell\*(C'\fR function. Use \f(CW\*(C`sysseek(FH, 0, 1)\*(C'\fR for that.
6437.Sp
6438Do not use \fItell()\fR on a filehandle that has been opened using
6439\&\fIsysopen()\fR, use \fIsysseek()\fR for that as described above. Why? Because
6440\&\fIsysopen()\fR creates unbuffered, \*(L"raw\*(R", filehandles, while \fIopen()\fR creates
6441buffered filehandles. \fIsysseek()\fR make sense only on the first kind,
6442\&\fItell()\fR only makes sense on the second kind.
6443.IP "telldir \s-1DIRHANDLE\s0" 8
6444.IX Item "telldir DIRHANDLE"
6445Returns the current position of the \f(CW\*(C`readdir\*(C'\fR routines on \s-1DIRHANDLE\s0.
6446Value may be given to \f(CW\*(C`seekdir\*(C'\fR to access a particular location in a
6447directory. Has the same caveats about possible directory compaction as
6448the corresponding system library routine.
6449.IP "tie \s-1VARIABLE\s0,CLASSNAME,LIST" 8
6450.IX Item "tie VARIABLE,CLASSNAME,LIST"
6451This function binds a variable to a package class that will provide the
6452implementation for the variable. \s-1VARIABLE\s0 is the name of the variable
6453to be enchanted. \s-1CLASSNAME\s0 is the name of a class implementing objects
6454of correct type. Any additional arguments are passed to the \f(CW\*(C`new\*(C'\fR
6455method of the class (meaning \f(CW\*(C`TIESCALAR\*(C'\fR, \f(CW\*(C`TIEHANDLE\*(C'\fR, \f(CW\*(C`TIEARRAY\*(C'\fR,
6456or \f(CW\*(C`TIEHASH\*(C'\fR). Typically these are arguments such as might be passed
6457to the \f(CW\*(C`dbm_open()\*(C'\fR function of C. The object returned by the \f(CW\*(C`new\*(C'\fR
6458method is also returned by the \f(CW\*(C`tie\*(C'\fR function, which would be useful
6459if you want to access other methods in \s-1CLASSNAME\s0.
6460.Sp
6461Note that functions such as \f(CW\*(C`keys\*(C'\fR and \f(CW\*(C`values\*(C'\fR may return huge lists
6462when used on large objects, like \s-1DBM\s0 files. You may prefer to use the
6463\&\f(CW\*(C`each\*(C'\fR function to iterate over such. Example:
6464.Sp
6465.Vb 7
6466\& # print out history file offsets
6467\& use NDBM_File;
6468\& tie(%HIST, 'NDBM_File', '/usr/lib/news/history', 1, 0);
6469\& while (($key,$val) = each %HIST) {
6470\& print $key, ' = ', unpack('L',$val), "\en";
6471\& }
6472\& untie(%HIST);
6473.Ve
6474.Sp
6475A class implementing a hash should have the following methods:
6476.Sp
6477.Vb 10
6478\& TIEHASH classname, LIST
6479\& FETCH this, key
6480\& STORE this, key, value
6481\& DELETE this, key
6482\& CLEAR this
6483\& EXISTS this, key
6484\& FIRSTKEY this
6485\& NEXTKEY this, lastkey
6486\& DESTROY this
6487\& UNTIE this
6488.Ve
6489.Sp
6490A class implementing an ordinary array should have the following methods:
6491.Sp
6492.Vb 14
6493\& TIEARRAY classname, LIST
6494\& FETCH this, key
6495\& STORE this, key, value
6496\& FETCHSIZE this
6497\& STORESIZE this, count
6498\& CLEAR this
6499\& PUSH this, LIST
6500\& POP this
6501\& SHIFT this
6502\& UNSHIFT this, LIST
6503\& SPLICE this, offset, length, LIST
6504\& EXTEND this, count
6505\& DESTROY this
6506\& UNTIE this
6507.Ve
6508.Sp
6509A class implementing a file handle should have the following methods:
6510.Sp
6511.Vb 16
6512\& TIEHANDLE classname, LIST
6513\& READ this, scalar, length, offset
6514\& READLINE this
6515\& GETC this
6516\& WRITE this, scalar, length, offset
6517\& PRINT this, LIST
6518\& PRINTF this, format, LIST
6519\& BINMODE this
6520\& EOF this
6521\& FILENO this
6522\& SEEK this, position, whence
6523\& TELL this
6524\& OPEN this, mode, LIST
6525\& CLOSE this
6526\& DESTROY this
6527\& UNTIE this
6528.Ve
6529.Sp
6530A class implementing a scalar should have the following methods:
6531.Sp
6532.Vb 5
6533\& TIESCALAR classname, LIST
6534\& FETCH this,
6535\& STORE this, value
6536\& DESTROY this
6537\& UNTIE this
6538.Ve
6539.Sp
6540Not all methods indicated above need be implemented. See perltie,
6541Tie::Hash, Tie::Array, Tie::Scalar, and Tie::Handle.
6542.Sp
6543Unlike \f(CW\*(C`dbmopen\*(C'\fR, the \f(CW\*(C`tie\*(C'\fR function will not use or require a module
6544for you\*(--you need to do that explicitly yourself. See DB_File
6545or the \fIConfig\fR module for interesting \f(CW\*(C`tie\*(C'\fR implementations.
6546.Sp
6547For further details see perltie, \*(L"tied \s-1VARIABLE\s0\*(R".
6548.IP "tied \s-1VARIABLE\s0" 8
6549.IX Item "tied VARIABLE"
6550Returns a reference to the object underlying \s-1VARIABLE\s0 (the same value
6551that was originally returned by the \f(CW\*(C`tie\*(C'\fR call that bound the variable
6552to a package.) Returns the undefined value if \s-1VARIABLE\s0 isn't tied to a
6553package.
6554.IP "time" 8
6555.IX Item "time"
6556Returns the number of non-leap seconds since whatever time the system
6557considers to be the epoch (that's 00:00:00, January 1, 1904 for Mac \s-1OS\s0,
6558and 00:00:00 \s-1UTC\s0, January 1, 1970 for most other systems).
6559Suitable for feeding to \f(CW\*(C`gmtime\*(C'\fR and \f(CW\*(C`localtime\*(C'\fR.
6560.Sp
6561For measuring time in better granularity than one second,
6562you may use either the Time::HiRes module from \s-1CPAN\s0, or
6563if you have \fIgettimeofday\fR\|(2), you may be able to use the
6564\&\f(CW\*(C`syscall\*(C'\fR interface of Perl, see perlfaq8 for details.
6565.IP "times" 8
6566.IX Item "times"
6567Returns a four-element list giving the user and system times, in
6568seconds, for this process and the children of this process.
6569.Sp
6570.Vb 1
6571\& ($user,$system,$cuser,$csystem) = times;
6572.Ve
6573.Sp
6574In scalar context, \f(CW\*(C`times\*(C'\fR returns \f(CW$user\fR.
6575.IP "tr///" 8
6576.IX Item "tr///"
6577The transliteration operator. Same as \f(CW\*(C`y///\*(C'\fR. See perlop.
6578.IP "truncate \s-1FILEHANDLE\s0,LENGTH" 8
6579.IX Item "truncate FILEHANDLE,LENGTH"
6580.PD 0
6581.IP "truncate \s-1EXPR\s0,LENGTH" 8
6582.IX Item "truncate EXPR,LENGTH"
6583.PD
6584Truncates the file opened on \s-1FILEHANDLE\s0, or named by \s-1EXPR\s0, to the
6585specified length. Produces a fatal error if truncate isn't implemented
6586on your system. Returns true if successful, the undefined value
6587otherwise.
6588.Sp
6589The behavior is undefined if \s-1LENGTH\s0 is greater than the length of the
6590file.
6591.IP "uc \s-1EXPR\s0" 8
6592.IX Item "uc EXPR"
6593.PD 0
6594.IP "uc" 8
6595.IX Item "uc"
6596.PD
6597Returns an uppercased version of \s-1EXPR\s0. This is the internal function
6598implementing the \f(CW\*(C`\eU\*(C'\fR escape in double-quoted strings. Respects
6599current \s-1LC_CTYPE\s0 locale if \f(CW\*(C`use locale\*(C'\fR in force. See perllocale
6600and perlunicode for more details about locale and Unicode support.
6601It does not attempt to do titlecase mapping on initial letters. See
6602\&\f(CW\*(C`ucfirst\*(C'\fR for that.
6603.Sp
6604If \s-1EXPR\s0 is omitted, uses \f(CW$_\fR.
6605.IP "ucfirst \s-1EXPR\s0" 8
6606.IX Item "ucfirst EXPR"
6607.PD 0
6608.IP "ucfirst" 8
6609.IX Item "ucfirst"
6610.PD
6611Returns the value of \s-1EXPR\s0 with the first character in uppercase
6612(titlecase in Unicode). This is the internal function implementing
6613the \f(CW\*(C`\eu\*(C'\fR escape in double-quoted strings. Respects current \s-1LC_CTYPE\s0
6614locale if \f(CW\*(C`use locale\*(C'\fR in force. See perllocale and perlunicode
6615for more details about locale and Unicode support.
6616.Sp
6617If \s-1EXPR\s0 is omitted, uses \f(CW$_\fR.
6618.IP "umask \s-1EXPR\s0" 8
6619.IX Item "umask EXPR"
6620.PD 0
6621.IP "umask" 8
6622.IX Item "umask"
6623.PD
6624Sets the umask for the process to \s-1EXPR\s0 and returns the previous value.
6625If \s-1EXPR\s0 is omitted, merely returns the current umask.
6626.Sp
6627The Unix permission \f(CW\*(C`rwxr\-x\-\-\-\*(C'\fR is represented as three sets of three
6628bits, or three octal digits: \f(CW0750\fR (the leading 0 indicates octal
6629and isn't one of the digits). The \f(CW\*(C`umask\*(C'\fR value is such a number
6630representing disabled permissions bits. The permission (or \*(L"mode\*(R")
6631values you pass \f(CW\*(C`mkdir\*(C'\fR or \f(CW\*(C`sysopen\*(C'\fR are modified by your umask, so
6632even if you tell \f(CW\*(C`sysopen\*(C'\fR to create a file with permissions \f(CW0777\fR,
6633if your umask is \f(CW0022\fR then the file will actually be created with
6634permissions \f(CW0755\fR. If your \f(CW\*(C`umask\*(C'\fR were \f(CW0027\fR (group can't
6635write; others can't read, write, or execute), then passing
6636\&\f(CW\*(C`sysopen\*(C'\fR \f(CW0666\fR would create a file with mode \f(CW0640\fR (\f(CW\*(C`0666 &~
6637027\*(C'\fR is \f(CW0640\fR).
6638.Sp
6639Here's some advice: supply a creation mode of \f(CW0666\fR for regular
6640files (in \f(CW\*(C`sysopen\*(C'\fR) and one of \f(CW0777\fR for directories (in
6641\&\f(CW\*(C`mkdir\*(C'\fR) and executable files. This gives users the freedom of
6642choice: if they want protected files, they might choose process umasks
6643of \f(CW022\fR, \f(CW027\fR, or even the particularly antisocial mask of \f(CW077\fR.
6644Programs should rarely if ever make policy decisions better left to
6645the user. The exception to this is when writing files that should be
6646kept private: mail files, web browser cookies, \fI.rhosts\fR files, and
6647so on.
6648.Sp
6649If \fIumask\fR\|(2) is not implemented on your system and you are trying to
6650restrict access for \fIyourself\fR (i.e., (\s-1EXPR\s0 & 0700) > 0), produces a
6651fatal error at run time. If \fIumask\fR\|(2) is not implemented and you are
6652not trying to restrict access for yourself, returns \f(CW\*(C`undef\*(C'\fR.
6653.Sp
6654Remember that a umask is a number, usually given in octal; it is \fInot\fR a
6655string of octal digits. See also \*(L"oct\*(R", if all you have is a string.
6656.IP "undef \s-1EXPR\s0" 8
6657.IX Item "undef EXPR"
6658.PD 0
6659.IP "undef" 8
6660.IX Item "undef"
6661.PD
6662Undefines the value of \s-1EXPR\s0, which must be an lvalue. Use only on a
6663scalar value, an array (using \f(CW\*(C`@\*(C'\fR), a hash (using \f(CW\*(C`%\*(C'\fR), a subroutine
6664(using \f(CW\*(C`&\*(C'\fR), or a typeglob (using <*>). (Saying \f(CW\*(C`undef $hash{$key}\*(C'\fR
6665will probably not do what you expect on most predefined variables or
6666\&\s-1DBM\s0 list values, so don't do that; see delete.) Always returns the
6667undefined value. You can omit the \s-1EXPR\s0, in which case nothing is
6668undefined, but you still get an undefined value that you could, for
6669instance, return from a subroutine, assign to a variable or pass as a
6670parameter. Examples:
6671.Sp
6672.Vb 9
6673\& undef $foo;
6674\& undef $bar{'blurfl'}; # Compare to: delete $bar{'blurfl'};
6675\& undef @ary;
6676\& undef %hash;
6677\& undef &mysub;
6678\& undef *xyz; # destroys $xyz, @xyz, %xyz, &xyz, etc.
6679\& return (wantarray ? (undef, $errmsg) : undef) if $they_blew_it;
6680\& select undef, undef, undef, 0.25;
6681\& ($a, $b, undef, $c) = &foo; # Ignore third value returned
6682.Ve
6683.Sp
6684Note that this is a unary operator, not a list operator.
6685.IP "unlink \s-1LIST\s0" 8
6686.IX Item "unlink LIST"
6687.PD 0
6688.IP "unlink" 8
6689.IX Item "unlink"
6690.PD
6691Deletes a list of files. Returns the number of files successfully
6692deleted.
6693.Sp
6694.Vb 3
6695\& $cnt = unlink 'a', 'b', 'c';
6696\& unlink @goners;
6697\& unlink <*.bak>;
6698.Ve
6699.Sp
6700Note: \f(CW\*(C`unlink\*(C'\fR will not delete directories unless you are superuser and
6701the \fB\-U\fR flag is supplied to Perl. Even if these conditions are
6702met, be warned that unlinking a directory can inflict damage on your
6703filesystem. Use \f(CW\*(C`rmdir\*(C'\fR instead.
6704.Sp
6705If \s-1LIST\s0 is omitted, uses \f(CW$_\fR.
6706.IP "unpack \s-1TEMPLATE\s0,EXPR" 8
6707.IX Item "unpack TEMPLATE,EXPR"
6708\&\f(CW\*(C`unpack\*(C'\fR does the reverse of \f(CW\*(C`pack\*(C'\fR: it takes a string
6709and expands it out into a list of values.
6710(In scalar context, it returns merely the first value produced.)
6711.Sp
6712The string is broken into chunks described by the \s-1TEMPLATE\s0. Each chunk
6713is converted separately to a value. Typically, either the string is a result
6714of \f(CW\*(C`pack\*(C'\fR, or the bytes of the string represent a C structure of some
6715kind.
6716.Sp
6717The \s-1TEMPLATE\s0 has the same format as in the \f(CW\*(C`pack\*(C'\fR function.
6718Here's a subroutine that does substring:
6719.Sp
6720.Vb 4
6721\& sub substr {
6722\& my($what,$where,$howmuch) = @_;
6723\& unpack("x$where a$howmuch", $what);
6724\& }
6725.Ve
6726.Sp
6727and then there's
6728.Sp
6729.Vb 1
6730\& sub ordinal { unpack("c",$_[0]); } # same as ord()
6731.Ve
6732.Sp
6733In addition to fields allowed in \fIpack()\fR, you may prefix a field with
6734a %<number> to indicate that
6735you want a <number>\-bit checksum of the items instead of the items
6736themselves. Default is a 16\-bit checksum. Checksum is calculated by
6737summing numeric values of expanded values (for string fields the sum of
6738\&\f(CW\*(C`ord($char)\*(C'\fR is taken, for bit fields the sum of zeroes and ones).
6739.Sp
6740For example, the following
6741computes the same number as the System V sum program:
6742.Sp
6743.Vb 4
6744\& $checksum = do {
6745\& local $/; # slurp!
6746\& unpack("%32C*",<>) % 65535;
6747\& };
6748.Ve
6749.Sp
6750The following efficiently counts the number of set bits in a bit vector:
6751.Sp
6752.Vb 1
6753\& $setbits = unpack("%32b*", $selectmask);
6754.Ve
6755.Sp
6756The \f(CW\*(C`p\*(C'\fR and \f(CW\*(C`P\*(C'\fR formats should be used with care. Since Perl
6757has no way of checking whether the value passed to \f(CW\*(C`unpack()\*(C'\fR
6758corresponds to a valid memory location, passing a pointer value that's
6759not known to be valid is likely to have disastrous consequences.
6760.Sp
6761If the repeat count of a field is larger than what the remainder of
6762the input string allows, repeat count is decreased. If the input string
6763is longer than one described by the \s-1TEMPLATE\s0, the rest is ignored.
6764.Sp
6765See \*(L"pack\*(R" for more examples and notes.
6766.IP "untie \s-1VARIABLE\s0" 8
6767.IX Item "untie VARIABLE"
6768Breaks the binding between a variable and a package. (See \f(CW\*(C`tie\*(C'\fR.)
6769Has no effect if the variable is not tied.
6770.IP "unshift \s-1ARRAY\s0,LIST" 8
6771.IX Item "unshift ARRAY,LIST"
6772Does the opposite of a \f(CW\*(C`shift\*(C'\fR. Or the opposite of a \f(CW\*(C`push\*(C'\fR,
6773depending on how you look at it. Prepends list to the front of the
6774array, and returns the new number of elements in the array.
6775.Sp
6776.Vb 1
6777\& unshift(@ARGV, '-e') unless $ARGV[0] =~ /^-/;
6778.Ve
6779.Sp
6780Note the \s-1LIST\s0 is prepended whole, not one element at a time, so the
6781prepended elements stay in the same order. Use \f(CW\*(C`reverse\*(C'\fR to do the
6782reverse.
6783.IP "use Module \s-1VERSION\s0 \s-1LIST\s0" 8
6784.IX Item "use Module VERSION LIST"
6785.PD 0
6786.IP "use Module \s-1VERSION\s0" 8
6787.IX Item "use Module VERSION"
6788.IP "use Module \s-1LIST\s0" 8
6789.IX Item "use Module LIST"
6790.IP "use Module" 8
6791.IX Item "use Module"
6792.IP "use \s-1VERSION\s0" 8
6793.IX Item "use VERSION"
6794.PD
6795Imports some semantics into the current package from the named module,
6796generally by aliasing certain subroutine or variable names into your
6797package. It is exactly equivalent to
6798.Sp
6799.Vb 1
6800\& BEGIN { require Module; import Module LIST; }
6801.Ve
6802.Sp
6803except that Module \fImust\fR be a bareword.
6804.Sp
6805\&\s-1VERSION\s0 may be either a numeric argument such as 5.006, which will be
6806compared to \f(CW$]\fR, or a literal of the form v5.6.1, which will be compared
6807to \f(CW$^V\fR (aka \f(CW$PERL_VERSION\fR. A fatal error is produced if \s-1VERSION\s0 is
6808greater than the version of the current Perl interpreter; Perl will not
6809attempt to parse the rest of the file. Compare with \*(L"require\*(R", which can
6810do a similar check at run time.
6811.Sp
6812Specifying \s-1VERSION\s0 as a literal of the form v5.6.1 should generally be
6813avoided, because it leads to misleading error messages under earlier
6814versions of Perl which do not support this syntax. The equivalent numeric
6815version should be used instead.
6816.Sp
6817.Vb 3
6818\& use v5.6.1; # compile time version check
6819\& use 5.6.1; # ditto
6820\& use 5.006_001; # ditto; preferred for backwards compatibility
6821.Ve
6822.Sp
6823This is often useful if you need to check the current Perl version before
6824\&\f(CW\*(C`use\*(C'\fRing library modules that have changed in incompatible ways from
6825older versions of Perl. (We try not to do this more than we have to.)
6826.Sp
6827The \f(CW\*(C`BEGIN\*(C'\fR forces the \f(CW\*(C`require\*(C'\fR and \f(CW\*(C`import\*(C'\fR to happen at compile time. The
6828\&\f(CW\*(C`require\*(C'\fR makes sure the module is loaded into memory if it hasn't been
6829yet. The \f(CW\*(C`import\*(C'\fR is not a builtin\*(--it's just an ordinary static method
6830call into the \f(CW\*(C`Module\*(C'\fR package to tell the module to import the list of
6831features back into the current package. The module can implement its
6832\&\f(CW\*(C`import\*(C'\fR method any way it likes, though most modules just choose to
6833derive their \f(CW\*(C`import\*(C'\fR method via inheritance from the \f(CW\*(C`Exporter\*(C'\fR class that
6834is defined in the \f(CW\*(C`Exporter\*(C'\fR module. See Exporter. If no \f(CW\*(C`import\*(C'\fR
6835method can be found then the call is skipped.
6836.Sp
6837If you do not want to call the package's \f(CW\*(C`import\*(C'\fR method (for instance,
6838to stop your namespace from being altered), explicitly supply the empty list:
6839.Sp
6840.Vb 1
6841\& use Module ();
6842.Ve
6843.Sp
6844That is exactly equivalent to
6845.Sp
6846.Vb 1
6847\& BEGIN { require Module }
6848.Ve
6849.Sp
6850If the \s-1VERSION\s0 argument is present between Module and \s-1LIST\s0, then the
6851\&\f(CW\*(C`use\*(C'\fR will call the \s-1VERSION\s0 method in class Module with the given
6852version as an argument. The default \s-1VERSION\s0 method, inherited from
6853the \s-1UNIVERSAL\s0 class, croaks if the given version is larger than the
6854value of the variable \f(CW$Module::VERSION\fR.
6855.Sp
6856Again, there is a distinction between omitting \s-1LIST\s0 (\f(CW\*(C`import\*(C'\fR called
6857with no arguments) and an explicit empty \s-1LIST\s0 \f(CW\*(C`()\*(C'\fR (\f(CW\*(C`import\*(C'\fR not
6858called). Note that there is no comma after \s-1VERSION\s0!
6859.Sp
6860Because this is a wide-open interface, pragmas (compiler directives)
6861are also implemented this way. Currently implemented pragmas are:
6862.Sp
6863.Vb 8
6864\& use constant;
6865\& use diagnostics;
6866\& use integer;
6867\& use sigtrap qw(SEGV BUS);
6868\& use strict qw(subs vars refs);
6869\& use subs qw(afunc blurfl);
6870\& use warnings qw(all);
6871\& use sort qw(stable _quicksort _mergesort);
6872.Ve
6873.Sp
6874Some of these pseudo-modules import semantics into the current
6875block scope (like \f(CW\*(C`strict\*(C'\fR or \f(CW\*(C`integer\*(C'\fR, unlike ordinary modules,
6876which import symbols into the current package (which are effective
6877through the end of the file).
6878.Sp
6879There's a corresponding \f(CW\*(C`no\*(C'\fR command that unimports meanings imported
6880by \f(CW\*(C`use\*(C'\fR, i.e., it calls \f(CW\*(C`unimport Module LIST\*(C'\fR instead of \f(CW\*(C`import\*(C'\fR.
6881.Sp
6882.Vb 3
6883\& no integer;
6884\& no strict 'refs';
6885\& no warnings;
6886.Ve
6887.Sp
6888See perlmodlib for a list of standard modules and pragmas. See perlrun
6889for the \f(CW\*(C`\-M\*(C'\fR and \f(CW\*(C`\-m\*(C'\fR command-line options to perl that give \f(CW\*(C`use\*(C'\fR
6890functionality from the command\-line.
6891.IP "utime \s-1LIST\s0" 8
6892.IX Item "utime LIST"
6893Changes the access and modification times on each file of a list of
6894files. The first two elements of the list must be the \s-1NUMERICAL\s0 access
6895and modification times, in that order. Returns the number of files
6896successfully changed. The inode change time of each file is set
6897to the current time. This code has the same effect as the \f(CW\*(C`touch\*(C'\fR
6898command if the files already exist:
6899.Sp
6900.Vb 3
6901\& #!/usr/bin/perl
6902\& $now = time;
6903\& utime $now, $now, @ARGV;
6904.Ve
6905.Sp
6906If the first two elements of the list are \f(CW\*(C`undef\*(C'\fR, then the \fIutime\fR\|(2)
6907function in the C library will be called with a null second argument.
6908On most systems, this will set the file's access and modification
6909times to the current time. (i.e. equivalent to the example above.)
6910.Sp
6911.Vb 1
6912\& utime undef, undef, @ARGV;
6913.Ve
6914.IP "values \s-1HASH\s0" 8
6915.IX Item "values HASH"
6916Returns a list consisting of all the values of the named hash. (In a
6917scalar context, returns the number of values.) The values are
6918returned in an apparently random order. The actual random order is
6919subject to change in future versions of perl, but it is guaranteed to
6920be the same order as either the \f(CW\*(C`keys\*(C'\fR or \f(CW\*(C`each\*(C'\fR function would
6921produce on the same (unmodified) hash.
6922.Sp
6923Note that the values are not copied, which means modifying them will
6924modify the contents of the hash:
6925.Sp
6926.Vb 2
6927\& for (values %hash) { s/foo/bar/g } # modifies %hash values
6928\& for (@hash{keys %hash}) { s/foo/bar/g } # same
6929.Ve
6930.Sp
6931As a side effect, calling \fIvalues()\fR resets the \s-1HASH\s0's internal iterator.
6932See also \f(CW\*(C`keys\*(C'\fR, \f(CW\*(C`each\*(C'\fR, and \f(CW\*(C`sort\*(C'\fR.
6933.IP "vec \s-1EXPR\s0,OFFSET,BITS" 8
6934.IX Item "vec EXPR,OFFSET,BITS"
6935Treats the string in \s-1EXPR\s0 as a bit vector made up of elements of
6936width \s-1BITS\s0, and returns the value of the element specified by \s-1OFFSET\s0
6937as an unsigned integer. \s-1BITS\s0 therefore specifies the number of bits
6938that are reserved for each element in the bit vector. This must
6939be a power of two from 1 to 32 (or 64, if your platform supports
6940that).
6941.Sp
6942If \s-1BITS\s0 is 8, \*(L"elements\*(R" coincide with bytes of the input string.
6943.Sp
6944If \s-1BITS\s0 is 16 or more, bytes of the input string are grouped into chunks
6945of size \s-1BITS/8\s0, and each group is converted to a number as with
6946\&\fIpack()\fR/\fIunpack()\fR with big-endian formats \f(CW\*(C`n\*(C'\fR/\f(CW\*(C`N\*(C'\fR (and analogously
6947for BITS==64). See \*(L"pack\*(R" for details.
6948.Sp
6949If bits is 4 or less, the string is broken into bytes, then the bits
6950of each byte are broken into 8/BITS groups. Bits of a byte are
6951numbered in a little-endian-ish way, as in \f(CW0x01\fR, \f(CW0x02\fR,
6952\&\f(CW0x04\fR, \f(CW0x08\fR, \f(CW0x10\fR, \f(CW0x20\fR, \f(CW0x40\fR, \f(CW0x80\fR. For example,
6953breaking the single input byte \f(CW\*(C`chr(0x36)\*(C'\fR into two groups gives a list
6954\&\f(CW\*(C`(0x6, 0x3)\*(C'\fR; breaking it into 4 groups gives \f(CW\*(C`(0x2, 0x1, 0x3, 0x0)\*(C'\fR.
6955.Sp
6956\&\f(CW\*(C`vec\*(C'\fR may also be assigned to, in which case parentheses are needed
6957to give the expression the correct precedence as in
6958.Sp
6959.Vb 1
6960\& vec($image, $max_x * $x + $y, 8) = 3;
6961.Ve
6962.Sp
6963If the selected element is outside the string, the value 0 is returned.
6964If an element off the end of the string is written to, Perl will first
6965extend the string with sufficiently many zero bytes. It is an error
6966to try to write off the beginning of the string (i.e. negative \s-1OFFSET\s0).
6967.Sp
6968The string should not contain any character with the value > 255 (which
6969can only happen if you're using \s-1UTF8\s0 encoding). If it does, it will be
6970treated as something which is not \s-1UTF8\s0 encoded. When the \f(CW\*(C`vec\*(C'\fR was
6971assigned to, other parts of your program will also no longer consider the
6972string to be \s-1UTF8\s0 encoded. In other words, if you do have such characters
6973in your string, \fIvec()\fR will operate on the actual byte string, and not the
6974conceptual character string.
6975.Sp
6976Strings created with \f(CW\*(C`vec\*(C'\fR can also be manipulated with the logical
6977operators \f(CW\*(C`|\*(C'\fR, \f(CW\*(C`&\*(C'\fR, \f(CW\*(C`^\*(C'\fR, and \f(CW\*(C`~\*(C'\fR. These operators will assume a bit
6978vector operation is desired when both operands are strings.
6979See \*(L"Bitwise String Operators\*(R" in perlop.
6980.Sp
6981The following code will build up an \s-1ASCII\s0 string saying \f(CW'PerlPerlPerl'\fR.
6982The comments show the string after each step. Note that this code works
6983in the same way on big-endian or little-endian machines.
6984.Sp
6985.Vb 2
6986\& my $foo = '';
6987\& vec($foo, 0, 32) = 0x5065726C; # 'Perl'
6988.Ve
6989.Sp
6990.Vb 2
6991\& # $foo eq "Perl" eq "\ex50\ex65\ex72\ex6C", 32 bits
6992\& print vec($foo, 0, 8); # prints 80 == 0x50 == ord('P')
6993.Ve
6994.Sp
6995.Vb 11
6996\& vec($foo, 2, 16) = 0x5065; # 'PerlPe'
6997\& vec($foo, 3, 16) = 0x726C; # 'PerlPerl'
6998\& vec($foo, 8, 8) = 0x50; # 'PerlPerlP'
6999\& vec($foo, 9, 8) = 0x65; # 'PerlPerlPe'
7000\& vec($foo, 20, 4) = 2; # 'PerlPerlPe' . "\ex02"
7001\& vec($foo, 21, 4) = 7; # 'PerlPerlPer'
7002\& # 'r' is "\ex72"
7003\& vec($foo, 45, 2) = 3; # 'PerlPerlPer' . "\ex0c"
7004\& vec($foo, 93, 1) = 1; # 'PerlPerlPer' . "\ex2c"
7005\& vec($foo, 94, 1) = 1; # 'PerlPerlPerl'
7006\& # 'l' is "\ex6c"
7007.Ve
7008.Sp
7009To transform a bit vector into a string or list of 0's and 1's, use these:
7010.Sp
7011.Vb 2
7012\& $bits = unpack("b*", $vector);
7013\& @bits = split(//, unpack("b*", $vector));
7014.Ve
7015.Sp
7016If you know the exact length in bits, it can be used in place of the \f(CW\*(C`*\*(C'\fR.
7017.Sp
7018Here is an example to illustrate how the bits actually fall in place:
7019.Sp
7020.Vb 1
7021\& #!/usr/bin/perl -wl
7022.Ve
7023.Sp
7024.Vb 5
7025\& print <<'EOT';
7026\& 0 1 2 3
7027\& unpack("V",$_) 01234567890123456789012345678901
7028\& ------------------------------------------------------------------
7029\& EOT
7030.Ve
7031.Sp
7032.Vb 13
7033\& for $w (0..3) {
7034\& $width = 2**$w;
7035\& for ($shift=0; $shift < $width; ++$shift) {
7036\& for ($off=0; $off < 32/$width; ++$off) {
7037\& $str = pack("B*", "0"x32);
7038\& $bits = (1<<$shift);
7039\& vec($str, $off, $width) = $bits;
7040\& $res = unpack("b*",$str);
7041\& $val = unpack("V", $str);
7042\& write;
7043\& }
7044\& }
7045\& }
7046.Ve
7047.Sp
7048.Vb 5
7049\& format STDOUT =
7050\& vec($_,@#,@#) = @<< == @######### @>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
7051\& $off, $width, $bits, $val, $res
7052\& .
7053\& __END__
7054.Ve
7055.Sp
7056Regardless of the machine architecture on which it is run, the above
7057example should print the following table:
7058.Sp
7059.Vb 131
7060\& 0 1 2 3
7061\& unpack("V",$_) 01234567890123456789012345678901
7062\& ------------------------------------------------------------------
7063\& vec($_, 0, 1) = 1 == 1 10000000000000000000000000000000
7064\& vec($_, 1, 1) = 1 == 2 01000000000000000000000000000000
7065\& vec($_, 2, 1) = 1 == 4 00100000000000000000000000000000
7066\& vec($_, 3, 1) = 1 == 8 00010000000000000000000000000000
7067\& vec($_, 4, 1) = 1 == 16 00001000000000000000000000000000
7068\& vec($_, 5, 1) = 1 == 32 00000100000000000000000000000000
7069\& vec($_, 6, 1) = 1 == 64 00000010000000000000000000000000
7070\& vec($_, 7, 1) = 1 == 128 00000001000000000000000000000000
7071\& vec($_, 8, 1) = 1 == 256 00000000100000000000000000000000
7072\& vec($_, 9, 1) = 1 == 512 00000000010000000000000000000000
7073\& vec($_,10, 1) = 1 == 1024 00000000001000000000000000000000
7074\& vec($_,11, 1) = 1 == 2048 00000000000100000000000000000000
7075\& vec($_,12, 1) = 1 == 4096 00000000000010000000000000000000
7076\& vec($_,13, 1) = 1 == 8192 00000000000001000000000000000000
7077\& vec($_,14, 1) = 1 == 16384 00000000000000100000000000000000
7078\& vec($_,15, 1) = 1 == 32768 00000000000000010000000000000000
7079\& vec($_,16, 1) = 1 == 65536 00000000000000001000000000000000
7080\& vec($_,17, 1) = 1 == 131072 00000000000000000100000000000000
7081\& vec($_,18, 1) = 1 == 262144 00000000000000000010000000000000
7082\& vec($_,19, 1) = 1 == 524288 00000000000000000001000000000000
7083\& vec($_,20, 1) = 1 == 1048576 00000000000000000000100000000000
7084\& vec($_,21, 1) = 1 == 2097152 00000000000000000000010000000000
7085\& vec($_,22, 1) = 1 == 4194304 00000000000000000000001000000000
7086\& vec($_,23, 1) = 1 == 8388608 00000000000000000000000100000000
7087\& vec($_,24, 1) = 1 == 16777216 00000000000000000000000010000000
7088\& vec($_,25, 1) = 1 == 33554432 00000000000000000000000001000000
7089\& vec($_,26, 1) = 1 == 67108864 00000000000000000000000000100000
7090\& vec($_,27, 1) = 1 == 134217728 00000000000000000000000000010000
7091\& vec($_,28, 1) = 1 == 268435456 00000000000000000000000000001000
7092\& vec($_,29, 1) = 1 == 536870912 00000000000000000000000000000100
7093\& vec($_,30, 1) = 1 == 1073741824 00000000000000000000000000000010
7094\& vec($_,31, 1) = 1 == 2147483648 00000000000000000000000000000001
7095\& vec($_, 0, 2) = 1 == 1 10000000000000000000000000000000
7096\& vec($_, 1, 2) = 1 == 4 00100000000000000000000000000000
7097\& vec($_, 2, 2) = 1 == 16 00001000000000000000000000000000
7098\& vec($_, 3, 2) = 1 == 64 00000010000000000000000000000000
7099\& vec($_, 4, 2) = 1 == 256 00000000100000000000000000000000
7100\& vec($_, 5, 2) = 1 == 1024 00000000001000000000000000000000
7101\& vec($_, 6, 2) = 1 == 4096 00000000000010000000000000000000
7102\& vec($_, 7, 2) = 1 == 16384 00000000000000100000000000000000
7103\& vec($_, 8, 2) = 1 == 65536 00000000000000001000000000000000
7104\& vec($_, 9, 2) = 1 == 262144 00000000000000000010000000000000
7105\& vec($_,10, 2) = 1 == 1048576 00000000000000000000100000000000
7106\& vec($_,11, 2) = 1 == 4194304 00000000000000000000001000000000
7107\& vec($_,12, 2) = 1 == 16777216 00000000000000000000000010000000
7108\& vec($_,13, 2) = 1 == 67108864 00000000000000000000000000100000
7109\& vec($_,14, 2) = 1 == 268435456 00000000000000000000000000001000
7110\& vec($_,15, 2) = 1 == 1073741824 00000000000000000000000000000010
7111\& vec($_, 0, 2) = 2 == 2 01000000000000000000000000000000
7112\& vec($_, 1, 2) = 2 == 8 00010000000000000000000000000000
7113\& vec($_, 2, 2) = 2 == 32 00000100000000000000000000000000
7114\& vec($_, 3, 2) = 2 == 128 00000001000000000000000000000000
7115\& vec($_, 4, 2) = 2 == 512 00000000010000000000000000000000
7116\& vec($_, 5, 2) = 2 == 2048 00000000000100000000000000000000
7117\& vec($_, 6, 2) = 2 == 8192 00000000000001000000000000000000
7118\& vec($_, 7, 2) = 2 == 32768 00000000000000010000000000000000
7119\& vec($_, 8, 2) = 2 == 131072 00000000000000000100000000000000
7120\& vec($_, 9, 2) = 2 == 524288 00000000000000000001000000000000
7121\& vec($_,10, 2) = 2 == 2097152 00000000000000000000010000000000
7122\& vec($_,11, 2) = 2 == 8388608 00000000000000000000000100000000
7123\& vec($_,12, 2) = 2 == 33554432 00000000000000000000000001000000
7124\& vec($_,13, 2) = 2 == 134217728 00000000000000000000000000010000
7125\& vec($_,14, 2) = 2 == 536870912 00000000000000000000000000000100
7126\& vec($_,15, 2) = 2 == 2147483648 00000000000000000000000000000001
7127\& vec($_, 0, 4) = 1 == 1 10000000000000000000000000000000
7128\& vec($_, 1, 4) = 1 == 16 00001000000000000000000000000000
7129\& vec($_, 2, 4) = 1 == 256 00000000100000000000000000000000
7130\& vec($_, 3, 4) = 1 == 4096 00000000000010000000000000000000
7131\& vec($_, 4, 4) = 1 == 65536 00000000000000001000000000000000
7132\& vec($_, 5, 4) = 1 == 1048576 00000000000000000000100000000000
7133\& vec($_, 6, 4) = 1 == 16777216 00000000000000000000000010000000
7134\& vec($_, 7, 4) = 1 == 268435456 00000000000000000000000000001000
7135\& vec($_, 0, 4) = 2 == 2 01000000000000000000000000000000
7136\& vec($_, 1, 4) = 2 == 32 00000100000000000000000000000000
7137\& vec($_, 2, 4) = 2 == 512 00000000010000000000000000000000
7138\& vec($_, 3, 4) = 2 == 8192 00000000000001000000000000000000
7139\& vec($_, 4, 4) = 2 == 131072 00000000000000000100000000000000
7140\& vec($_, 5, 4) = 2 == 2097152 00000000000000000000010000000000
7141\& vec($_, 6, 4) = 2 == 33554432 00000000000000000000000001000000
7142\& vec($_, 7, 4) = 2 == 536870912 00000000000000000000000000000100
7143\& vec($_, 0, 4) = 4 == 4 00100000000000000000000000000000
7144\& vec($_, 1, 4) = 4 == 64 00000010000000000000000000000000
7145\& vec($_, 2, 4) = 4 == 1024 00000000001000000000000000000000
7146\& vec($_, 3, 4) = 4 == 16384 00000000000000100000000000000000
7147\& vec($_, 4, 4) = 4 == 262144 00000000000000000010000000000000
7148\& vec($_, 5, 4) = 4 == 4194304 00000000000000000000001000000000
7149\& vec($_, 6, 4) = 4 == 67108864 00000000000000000000000000100000
7150\& vec($_, 7, 4) = 4 == 1073741824 00000000000000000000000000000010
7151\& vec($_, 0, 4) = 8 == 8 00010000000000000000000000000000
7152\& vec($_, 1, 4) = 8 == 128 00000001000000000000000000000000
7153\& vec($_, 2, 4) = 8 == 2048 00000000000100000000000000000000
7154\& vec($_, 3, 4) = 8 == 32768 00000000000000010000000000000000
7155\& vec($_, 4, 4) = 8 == 524288 00000000000000000001000000000000
7156\& vec($_, 5, 4) = 8 == 8388608 00000000000000000000000100000000
7157\& vec($_, 6, 4) = 8 == 134217728 00000000000000000000000000010000
7158\& vec($_, 7, 4) = 8 == 2147483648 00000000000000000000000000000001
7159\& vec($_, 0, 8) = 1 == 1 10000000000000000000000000000000
7160\& vec($_, 1, 8) = 1 == 256 00000000100000000000000000000000
7161\& vec($_, 2, 8) = 1 == 65536 00000000000000001000000000000000
7162\& vec($_, 3, 8) = 1 == 16777216 00000000000000000000000010000000
7163\& vec($_, 0, 8) = 2 == 2 01000000000000000000000000000000
7164\& vec($_, 1, 8) = 2 == 512 00000000010000000000000000000000
7165\& vec($_, 2, 8) = 2 == 131072 00000000000000000100000000000000
7166\& vec($_, 3, 8) = 2 == 33554432 00000000000000000000000001000000
7167\& vec($_, 0, 8) = 4 == 4 00100000000000000000000000000000
7168\& vec($_, 1, 8) = 4 == 1024 00000000001000000000000000000000
7169\& vec($_, 2, 8) = 4 == 262144 00000000000000000010000000000000
7170\& vec($_, 3, 8) = 4 == 67108864 00000000000000000000000000100000
7171\& vec($_, 0, 8) = 8 == 8 00010000000000000000000000000000
7172\& vec($_, 1, 8) = 8 == 2048 00000000000100000000000000000000
7173\& vec($_, 2, 8) = 8 == 524288 00000000000000000001000000000000
7174\& vec($_, 3, 8) = 8 == 134217728 00000000000000000000000000010000
7175\& vec($_, 0, 8) = 16 == 16 00001000000000000000000000000000
7176\& vec($_, 1, 8) = 16 == 4096 00000000000010000000000000000000
7177\& vec($_, 2, 8) = 16 == 1048576 00000000000000000000100000000000
7178\& vec($_, 3, 8) = 16 == 268435456 00000000000000000000000000001000
7179\& vec($_, 0, 8) = 32 == 32 00000100000000000000000000000000
7180\& vec($_, 1, 8) = 32 == 8192 00000000000001000000000000000000
7181\& vec($_, 2, 8) = 32 == 2097152 00000000000000000000010000000000
7182\& vec($_, 3, 8) = 32 == 536870912 00000000000000000000000000000100
7183\& vec($_, 0, 8) = 64 == 64 00000010000000000000000000000000
7184\& vec($_, 1, 8) = 64 == 16384 00000000000000100000000000000000
7185\& vec($_, 2, 8) = 64 == 4194304 00000000000000000000001000000000
7186\& vec($_, 3, 8) = 64 == 1073741824 00000000000000000000000000000010
7187\& vec($_, 0, 8) = 128 == 128 00000001000000000000000000000000
7188\& vec($_, 1, 8) = 128 == 32768 00000000000000010000000000000000
7189\& vec($_, 2, 8) = 128 == 8388608 00000000000000000000000100000000
7190\& vec($_, 3, 8) = 128 == 2147483648 00000000000000000000000000000001
7191.Ve
7192.IP "wait" 8
7193.IX Item "wait"
7194Behaves like the \fIwait\fR\|(2) system call on your system: it waits for a child
7195process to terminate and returns the pid of the deceased process, or
7196\&\f(CW\*(C`\-1\*(C'\fR if there are no child processes. The status is returned in \f(CW$?\fR.
7197Note that a return value of \f(CW\*(C`\-1\*(C'\fR could mean that child processes are
7198being automatically reaped, as described in perlipc.
7199.IP "waitpid \s-1PID\s0,FLAGS" 8
7200.IX Item "waitpid PID,FLAGS"
7201Waits for a particular child process to terminate and returns the pid of
7202the deceased process, or \f(CW\*(C`\-1\*(C'\fR if there is no such child process. On some
7203systems, a value of 0 indicates that there are processes still running.
7204The status is returned in \f(CW$?\fR. If you say
7205.Sp
7206.Vb 5
7207\& use POSIX ":sys_wait_h";
7208\& #...
7209\& do {
7210\& $kid = waitpid(-1, WNOHANG);
7211\& } until $kid > 0;
7212.Ve
7213.Sp
7214then you can do a non-blocking wait for all pending zombie processes.
7215Non-blocking wait is available on machines supporting either the
7216\&\fIwaitpid\fR\|(2) or \fIwait4\fR\|(2) system calls. However, waiting for a particular
7217pid with \s-1FLAGS\s0 of \f(CW0\fR is implemented everywhere. (Perl emulates the
7218system call by remembering the status values of processes that have
7219exited but have not been harvested by the Perl script yet.)
7220.Sp
7221Note that on some systems, a return value of \f(CW\*(C`\-1\*(C'\fR could mean that child
7222processes are being automatically reaped. See perlipc for details,
7223and for other examples.
7224.IP "wantarray" 8
7225.IX Item "wantarray"
7226Returns true if the context of the currently executing subroutine is
7227looking for a list value. Returns false if the context is looking
7228for a scalar. Returns the undefined value if the context is looking
7229for no value (void context).
7230.Sp
7231.Vb 3
7232\& return unless defined wantarray; # don't bother doing more
7233\& my @a = complex_calculation();
7234\& return wantarray ? @a : "@a";
7235.Ve
7236.Sp
7237This function should have been named \fIwantlist()\fR instead.
7238.IP "warn \s-1LIST\s0" 8
7239.IX Item "warn LIST"
7240Produces a message on \s-1STDERR\s0 just like \f(CW\*(C`die\*(C'\fR, but doesn't exit or throw
7241an exception.
7242.Sp
7243If \s-1LIST\s0 is empty and \f(CW$@\fR already contains a value (typically from a
7244previous eval) that value is used after appending \f(CW"\et...caught"\fR
7245to \f(CW$@\fR. This is useful for staying almost, but not entirely similar to
7246\&\f(CW\*(C`die\*(C'\fR.
7247.Sp
7248If \f(CW$@\fR is empty then the string \f(CW"Warning: Something's wrong"\fR is used.
7249.Sp
7250No message is printed if there is a \f(CW$SIG{_\|_WARN_\|_}\fR handler
7251installed. It is the handler's responsibility to deal with the message
7252as it sees fit (like, for instance, converting it into a \f(CW\*(C`die\*(C'\fR). Most
7253handlers must therefore make arrangements to actually display the
7254warnings that they are not prepared to deal with, by calling \f(CW\*(C`warn\*(C'\fR
7255again in the handler. Note that this is quite safe and will not
7256produce an endless loop, since \f(CW\*(C`_\|_WARN_\|_\*(C'\fR hooks are not called from
7257inside one.
7258.Sp
7259You will find this behavior is slightly different from that of
7260\&\f(CW$SIG{_\|_DIE_\|_}\fR handlers (which don't suppress the error text, but can
7261instead call \f(CW\*(C`die\*(C'\fR again to change it).
7262.Sp
7263Using a \f(CW\*(C`_\|_WARN_\|_\*(C'\fR handler provides a powerful way to silence all
7264warnings (even the so-called mandatory ones). An example:
7265.Sp
7266.Vb 7
7267\& # wipe out *all* compile-time warnings
7268\& BEGIN { $SIG{'__WARN__'} = sub { warn $_[0] if $DOWARN } }
7269\& my $foo = 10;
7270\& my $foo = 20; # no warning about duplicate my $foo,
7271\& # but hey, you asked for it!
7272\& # no compile-time or run-time warnings before here
7273\& $DOWARN = 1;
7274.Ve
7275.Sp
7276.Vb 2
7277\& # run-time warnings enabled after here
7278\& warn "\e$foo is alive and $foo!"; # does show up
7279.Ve
7280.Sp
7281See perlvar for details on setting \f(CW%SIG\fR entries, and for more
7282examples. See the Carp module for other kinds of warnings using its
7283\&\fIcarp()\fR and \fIcluck()\fR functions.
7284.IP "write \s-1FILEHANDLE\s0" 8
7285.IX Item "write FILEHANDLE"
7286.PD 0
7287.IP "write \s-1EXPR\s0" 8
7288.IX Item "write EXPR"
7289.IP "write" 8
7290.IX Item "write"
7291.PD
7292Writes a formatted record (possibly multi\-line) to the specified \s-1FILEHANDLE\s0,
7293using the format associated with that file. By default the format for
7294a file is the one having the same name as the filehandle, but the
7295format for the current output channel (see the \f(CW\*(C`select\*(C'\fR function) may be set
7296explicitly by assigning the name of the format to the \f(CW$~\fR variable.
7297.Sp
7298Top of form processing is handled automatically: if there is
7299insufficient room on the current page for the formatted record, the
7300page is advanced by writing a form feed, a special top-of-page format
7301is used to format the new page header, and then the record is written.
7302By default the top-of-page format is the name of the filehandle with
7303\&\*(L"_TOP\*(R" appended, but it may be dynamically set to the format of your
7304choice by assigning the name to the \f(CW$^\fR variable while the filehandle is
7305selected. The number of lines remaining on the current page is in
7306variable \f(CW\*(C`$\-\*(C'\fR, which can be set to \f(CW0\fR to force a new page.
7307.Sp
7308If \s-1FILEHANDLE\s0 is unspecified, output goes to the current default output
7309channel, which starts out as \s-1STDOUT\s0 but may be changed by the
7310\&\f(CW\*(C`select\*(C'\fR operator. If the \s-1FILEHANDLE\s0 is an \s-1EXPR\s0, then the expression
7311is evaluated and the resulting string is used to look up the name of
7312the \s-1FILEHANDLE\s0 at run time. For more on formats, see perlform.
7313.Sp
7314Note that write is \fInot\fR the opposite of \f(CW\*(C`read\*(C'\fR. Unfortunately.
7315.IP "y///" 8
7316.IX Item "y///"
7317The transliteration operator. Same as \f(CW\*(C`tr///\*(C'\fR. See perlop.