Commit | Line | Data |
---|---|---|
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" | |
134 | perlfunc \- Perl builtin functions | |
135 | .SH "DESCRIPTION" | |
136 | .IX Header "DESCRIPTION" | |
137 | The functions in this section can serve as terms in an expression. | |
138 | They fall into two major categories: list operators and named unary | |
139 | operators. These differ in their precedence relationship with a | |
140 | following comma. (See the precedence table in perlop.) List | |
141 | operators take more than one argument, while unary operators can never | |
142 | take more than one argument. Thus, a comma terminates the argument of | |
143 | a unary operator, but merely separates the arguments of a list | |
144 | operator. A unary operator generally provides a scalar context to its | |
145 | argument, while a list operator may provide either scalar or list | |
146 | contexts for its arguments. If it does both, the scalar arguments will | |
147 | be first, and the list argument will follow. (Note that there can ever | |
148 | be only one such list argument.) For instance, \fIsplice()\fR has three scalar | |
149 | arguments followed by a list, whereas \fIgethostbyname()\fR has four scalar | |
150 | arguments. | |
151 | .PP | |
152 | In the syntax descriptions that follow, list operators that expect a | |
153 | list (and provide list context for the elements of the list) are shown | |
154 | with \s-1LIST\s0 as an argument. Such a list may consist of any combination | |
155 | of scalar arguments or list values; the list values will be included | |
156 | in the list as if each individual element were interpolated at that | |
157 | point in the list, forming a longer single-dimensional list value. | |
158 | Elements of the \s-1LIST\s0 should be separated by commas. | |
159 | .PP | |
160 | Any function in the list below may be used either with or without | |
161 | parentheses around its arguments. (The syntax descriptions omit the | |
162 | parentheses.) If you use the parentheses, the simple (but occasionally | |
163 | surprising) rule is this: It \fIlooks\fR like a function, therefore it \fIis\fR a | |
164 | function, and precedence doesn't matter. Otherwise it's a list | |
165 | operator or unary operator, and precedence does matter. And whitespace | |
166 | between the function and left parenthesis doesn't count\*(--so you need to | |
167 | be 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 | |
177 | If you run Perl with the \fB\-w\fR switch it can warn you about this. For | |
178 | example, 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 | |
185 | A few functions take no arguments at all, and therefore work as neither | |
186 | unary nor list operators. These include such functions as \f(CW\*(C`time\*(C'\fR | |
187 | and \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 | |
190 | For functions that can be used in either a scalar or list context, | |
191 | nonabortive failure is generally indicated in a scalar context by | |
192 | returning the undefined value, and in a list context by returning the | |
193 | null list. | |
194 | .PP | |
195 | Remember the following important rule: There is \fBno rule\fR that relates | |
196 | the behavior of an expression in list context to its behavior in scalar | |
197 | context, or vice versa. It might do two totally different things. | |
198 | Each operator and function decides which sort of value it would be most | |
199 | appropriate to return in scalar context. Some operators return the | |
200 | length of the list that would have been returned in list context. Some | |
201 | operators return the first value in the list. Some operators return the | |
202 | last value in the list. Some operators return a count of successful | |
203 | operations. In general, they do what you want, unless you want | |
204 | consistency. | |
205 | .PP | |
206 | A named array in scalar context is quite different from what would at | |
207 | first glance appear to be a list in scalar context. You can't get a list | |
208 | like \f(CW\*(C`(1,2,3)\*(C'\fR into being in scalar context, because the compiler knows | |
209 | the context at compile time. It would generate the scalar comma operator | |
210 | there, not the list construction version of the comma. That means it | |
211 | was never a list to start with. | |
212 | .PP | |
213 | In general, functions in Perl that serve as wrappers for system calls | |
214 | of the same name (like \fIchown\fR\|(2), \fIfork\fR\|(2), \fIclosedir\fR\|(2), etc.) all return | |
215 | true when they succeed and \f(CW\*(C`undef\*(C'\fR otherwise, as is usually mentioned | |
216 | in the descriptions below. This is different from the C interfaces, | |
217 | which 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 | |
219 | variable on failure. Other functions do not, except accidentally. | |
220 | .Sh "Perl Functions by Category" | |
221 | .IX Subsection "Perl Functions by Category" | |
222 | Here are Perl's functions (including things that look like | |
223 | functions, like some keywords and named operators) | |
224 | arranged by category. Some functions appear in more | |
225 | than 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 | |
320 | operator, 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" | |
326 | Perl was born in Unix and can therefore access all common Unix | |
327 | system calls. In non-Unix environments, the functionality of some | |
328 | Unix system calls may not be available, or details of the available | |
329 | functionality may differ slightly. The Perl functions affected | |
330 | by 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 | |
350 | For more information about the portability of these functions, see | |
351 | perlport 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 | |
362 | A file test, where X is one of the letters listed below. This unary | |
363 | operator takes one argument, either a filename or a filehandle, and | |
364 | tests the associated file to see if something is true about it. If the | |
365 | argument is omitted, tests \f(CW$_\fR, except for \f(CW\*(C`\-t\*(C'\fR, which tests \s-1STDIN\s0. | |
366 | Unless otherwise documented, it returns \f(CW1\fR for true and \f(CW''\fR for false, or | |
367 | the undefined value if the file doesn't exist. Despite the funny | |
368 | names, precedence is the same as any other named unary operator, and | |
369 | the argument may be parenthesized like any other unary operator. The | |
370 | operator 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 | |
421 | Example: | |
422 | .Sp | |
423 | .Vb 5 | |
424 | \& while (<>) { | |
425 | \& chomp; | |
426 | \& next unless -f $_; # ignore specials | |
427 | \& #... | |
428 | \& } | |
429 | .Ve | |
430 | .Sp | |
431 | The 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 | |
433 | of the file and the uids and gids of the user. There may be other | |
434 | reasons you can't actually read, write, or execute the file. Such | |
435 | reasons may be for example network filesystem access controls, ACLs | |
436 | (access control lists), read-only filesystems, and unrecognized | |
437 | executable formats. | |
438 | .Sp | |
439 | Also 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 | |
441 | if any execute bit is set in the mode. Scripts run by the superuser | |
442 | may thus need to do a \fIstat()\fR to determine the actual mode of the file, | |
443 | or temporarily set their effective uid to something else. | |
444 | .Sp | |
445 | If you are using ACLs, there is a pragma called \f(CW\*(C`filetest\*(C'\fR that may | |
446 | produce more accurate results than the bare \fIstat()\fR mode bits. | |
447 | When under the \f(CW\*(C`use filetest 'access'\*(C'\fR the above-mentioned filetests | |
448 | will 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 | |
450 | under this pragma return true even if there are no execute permission | |
451 | bits set (nor any extra execute permission ACLs). This strangeness is | |
452 | due to the underlying system calls' definitions. Read the | |
453 | documentation for the \f(CW\*(C`filetest\*(C'\fR pragma for more information. | |
454 | .Sp | |
455 | Note 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 | |
457 | following a minus are interpreted as file tests. | |
458 | .Sp | |
459 | The \f(CW\*(C`\-T\*(C'\fR and \f(CW\*(C`\-B\*(C'\fR switches work as follows. The first block or so of the | |
460 | file is examined for odd characters such as strange control codes or | |
461 | characters with the high bit set. If too many strange characters (>30%) | |
462 | are 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 | |
463 | containing null in the first block is considered a binary file. If \f(CW\*(C`\-T\*(C'\fR | |
464 | or \f(CW\*(C`\-B\*(C'\fR is used on a filehandle, the current \s-1IO\s0 buffer is examined | |
465 | rather than the first block. Both \f(CW\*(C`\-T\*(C'\fR and \f(CW\*(C`\-B\*(C'\fR return true on a null | |
466 | file, or a file at \s-1EOF\s0 when testing a filehandle. Because you have to | |
467 | read 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 | |
468 | against the file first, as in \f(CW\*(C`next unless \-f $file && \-T $file\*(C'\fR. | |
469 | .Sp | |
470 | If any of the file tests (or either the \f(CW\*(C`stat\*(C'\fR or \f(CW\*(C`lstat\*(C'\fR operators) are given | |
471 | the special filehandle consisting of a solitary underline, then the stat | |
472 | structure of the previous file test (or stat operator) is used, saving | |
473 | a system call. (This doesn't work with \f(CW\*(C`\-t\*(C'\fR, and you need to remember | |
474 | that \fIlstat()\fR and \f(CW\*(C`\-l\*(C'\fR will leave values in the stat structure for the | |
475 | symbolic link, not the real file.) (Also, if the stat buffer was filled by | |
476 | a \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). | |
477 | Example: | |
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 | |
500 | Returns the absolute value of its argument. | |
501 | If \s-1VALUE\s0 is omitted, uses \f(CW$_\fR. | |
502 | .IP "accept \s-1NEWSOCKET\s0,GENERICSOCKET" 8 | |
503 | .IX Item "accept NEWSOCKET,GENERICSOCKET" | |
504 | Accepts an incoming socket connect, just as the \fIaccept\fR\|(2) system call | |
505 | does. Returns the packed address if it succeeded, false otherwise. | |
506 | See the example in \*(L"Sockets: Client/Server Communication\*(R" in perlipc. | |
507 | .Sp | |
508 | On systems that support a close-on-exec flag on files, the flag will | |
509 | be set for the newly opened file descriptor, as determined by the | |
510 | value 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 | |
517 | Arranges to have a \s-1SIGALRM\s0 delivered to this process after the | |
518 | specified number of wallclock seconds have elapsed. If \s-1SECONDS\s0 is not | |
519 | specified, the value stored in \f(CW$_\fR is used. (On some machines, | |
520 | unfortunately, the elapsed time may be up to one second less or more | |
521 | than you specified because of how seconds are counted, and process | |
522 | scheduling may delay the delivery of the signal even further.) | |
523 | .Sp | |
524 | Only one timer may be counting at once. Each call disables the | |
525 | previous timer, and an argument of \f(CW0\fR may be supplied to cancel the | |
526 | previous timer without starting a new one. The returned value is the | |
527 | amount of time remaining on the previous timer. | |
528 | .Sp | |
529 | For delays of finer granularity than one second, you may use Perl's | |
530 | four-argument version of \fIselect()\fR leaving the first three arguments | |
531 | undefined, or you might be able to use the \f(CW\*(C`syscall\*(C'\fR interface to | |
532 | access \fIsetitimer\fR\|(2) if your system supports it. The Time::HiRes | |
533 | module (from \s-1CPAN\s0, and starting from Perl 5.8 part of the standard | |
534 | distribution) may also prove useful. | |
535 | .Sp | |
536 | It 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 | |
539 | If 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 | |
541 | fail with \f(CW$!\fR set to \f(CW\*(C`EINTR\*(C'\fR because Perl sets up signal handlers to | |
542 | restart system calls on some systems. Using \f(CW\*(C`eval\*(C'\fR/\f(CW\*(C`die\*(C'\fR always works, | |
543 | modulo 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" | |
562 | Returns the arctangent of Y/X in the range \-PI to \s-1PI\s0. | |
563 | .Sp | |
564 | For the tangent operation, you may use the \f(CW\*(C`Math::Trig::tan\*(C'\fR | |
565 | function, 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" | |
572 | Binds a network address to a socket, just as the bind system call | |
573 | does. Returns true if it succeeded, false otherwise. \s-1NAME\s0 should be a | |
574 | packed 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 | |
582 | Arranges for \s-1FILEHANDLE\s0 to be read or written in \*(L"binary\*(R" or \*(L"text\*(R" | |
583 | mode on systems where the run-time libraries distinguish between | |
584 | binary and text files. If \s-1FILEHANDLE\s0 is an expression, the value is | |
585 | taken as the name of the filehandle. Returns true on success, | |
586 | \&\f(CW\*(C`undef\*(C'\fR on failure. | |
587 | .Sp | |
588 | If \s-1LAYER\s0 is omitted or specified as \f(CW\*(C`:raw\*(C'\fR the filehandle is made | |
589 | suitable for passing binary data. This includes turning off possible \s-1CRLF\s0 | |
590 | translation and marking it as bytes (as opposed to Unicode characters). | |
591 | Note 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" | |
598 | in \*(L"Programming Perl, 3rd Edition\*(R". However, since the publishing of this | |
599 | book, by many known as \*(L"Camel \s-1III\s0\*(R", the consensus of the naming of this | |
600 | functionality has moved from \*(L"discipline\*(R" to \*(L"layer\*(R". All documentation | |
601 | of 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 | |
604 | On some systems (in general, \s-1DOS\s0 and Windows-based systems) \fIbinmode()\fR | |
605 | is necessary when you're not working with a text file. For the sake | |
606 | of portability it is a good idea to always use it when appropriate, | |
607 | and to never use it when it isn't appropriate. | |
608 | .Sp | |
609 | In other words: regardless of platform, use \fIbinmode()\fR on binary files | |
610 | (like for example images). | |
611 | .Sp | |
612 | If \s-1LAYER\s0 is present it is a single string, but may contain | |
613 | multiple directives. The directives alter the behaviour of the | |
614 | file handle. When \s-1LAYER\s0 is present using binmode on text | |
615 | file makes sense. | |
616 | .Sp | |
617 | To mark \s-1FILEHANDLE\s0 as \s-1UTF\-8\s0, use \f(CW\*(C`:utf8\*(C'\fR. | |
618 | .Sp | |
619 | The \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 | |
620 | form \f(CW\*(C`:...\*(C'\fR, are called I/O \fIlayers\fR. The \f(CW\*(C`open\*(C'\fR pragma can be used to | |
621 | establish default I/O layers. See open. | |
622 | .Sp | |
623 | In general, \fIbinmode()\fR should be called after \fIopen()\fR but before any I/O | |
624 | is done on the filehandle. Calling \fIbinmode()\fR will normally flush any | |
625 | pending buffered output data (and perhaps pending input data) on the | |
626 | handle. An exception to this is the \f(CW\*(C`:encoding\*(C'\fR layer that | |
627 | changes the default character encoding of the handle, see open. | |
628 | The \f(CW\*(C`:encoding\*(C'\fR layer sometimes needs to be called in | |
629 | mid\-stream, and it doesn't flush the stream. | |
630 | .Sp | |
631 | The operating system, device drivers, C libraries, and Perl run-time | |
632 | system all work together to let the programmer treat a single | |
633 | character (\f(CW\*(C`\en\*(C'\fR) as the line terminator, irrespective of the external | |
634 | representation. On many operating systems, the native text file | |
635 | representation matches the internal representation, but on some | |
636 | platforms the external representation of \f(CW\*(C`\en\*(C'\fR is made up of more than | |
637 | one character. | |
638 | .Sp | |
639 | Mac \s-1OS\s0, all variants of Unix, and Stream_LF files on \s-1VMS\s0 use a single | |
640 | character to end each line in the external representation of text (even | |
641 | though that single character is \s-1CARRIAGE\s0 \s-1RETURN\s0 on Mac \s-1OS\s0 and \s-1LINE\s0 \s-1FEED\s0 | |
642 | on Unix and most \s-1VMS\s0 files). In other systems like \s-1OS/2\s0, \s-1DOS\s0 and the | |
643 | various flavors of MS-Windows your program sees a \f(CW\*(C`\en\*(C'\fR as a simple \f(CW\*(C`\ecJ\*(C'\fR, | |
644 | but what's stored in text files are the two characters \f(CW\*(C`\ecM\ecJ\*(C'\fR. That | |
645 | means that, if you don't use \fIbinmode()\fR on these systems, \f(CW\*(C`\ecM\ecJ\*(C'\fR | |
646 | sequences on disk will be converted to \f(CW\*(C`\en\*(C'\fR on input, and any \f(CW\*(C`\en\*(C'\fR in | |
647 | your program will be converted back to \f(CW\*(C`\ecM\ecJ\*(C'\fR on output. This is what | |
648 | you want for text files, but it can be disastrous for binary files. | |
649 | .Sp | |
650 | Another consequence of using \fIbinmode()\fR (on some systems) is that | |
651 | special end-of-file markers will be seen as part of the data stream. | |
652 | For systems from the Microsoft family this means that if your binary | |
653 | data contains \f(CW\*(C`\ecZ\*(C'\fR, the I/O subsystem will regard it as the end of | |
654 | the file, unless you use \fIbinmode()\fR. | |
655 | .Sp | |
656 | \&\fIbinmode()\fR is not only important for \fIreadline()\fR and \fIprint()\fR operations, | |
657 | but 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 | |
659 | in perlvar for how to manually set your input and output | |
660 | line-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 | |
667 | This function tells the thingy referenced by \s-1REF\s0 that it is now an object | |
668 | in the \s-1CLASSNAME\s0 package. If \s-1CLASSNAME\s0 is omitted, the current package | |
669 | is used. Because a \f(CW\*(C`bless\*(C'\fR is often the last thing in a constructor, | |
670 | it returns the reference for convenience. Always use the two-argument | |
671 | version if the function doing the blessing might be inherited by a | |
672 | derived class. See perltoot and perlobj for more about the blessing | |
673 | (and blessings) of objects. | |
674 | .Sp | |
675 | Consider always blessing objects in CLASSNAMEs that are mixed case. | |
676 | Namespaces with all lowercase names are considered reserved for | |
677 | Perl pragmata. Builtin types have all uppercase names, so to prevent | |
678 | confusion, you may wish to avoid such package names as well. Make sure | |
679 | that \s-1CLASSNAME\s0 is a true value. | |
680 | .Sp | |
681 | See \*(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 | |
688 | Returns the context of the current subroutine call. In scalar context, | |
689 | returns the caller's package name if there is a caller, that is, if | |
690 | we're in a subroutine or \f(CW\*(C`eval\*(C'\fR or \f(CW\*(C`require\*(C'\fR, and the undefined value | |
691 | otherwise. In list context, returns | |
692 | .Sp | |
693 | .Vb 1 | |
694 | \& ($package, $filename, $line) = caller; | |
695 | .Ve | |
696 | .Sp | |
697 | With \s-1EXPR\s0, it returns some extra information that the debugger uses to | |
698 | print a stack trace. The value of \s-1EXPR\s0 indicates how many call frames | |
699 | to 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 | |
706 | Here \f(CW$subroutine\fR may be \f(CW\*(C`(eval)\*(C'\fR if the frame is not a subroutine | |
707 | call, 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 | |
712 | each \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 | |
713 | frame.) \f(CW$subroutine\fR may also be \f(CW\*(C`(unknown)\*(C'\fR if this particular | |
714 | subroutine 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 | |
717 | compiled with. The \f(CW$hints\fR and \f(CW$bitmask\fR values are subject to change | |
718 | between versions of Perl, and are not meant for external use. | |
719 | .Sp | |
720 | Furthermore, when called from within the \s-1DB\s0 package, caller returns more | |
721 | detailed information: it sets the list variable \f(CW@DB::args\fR to be the | |
722 | arguments with which the subroutine was invoked. | |
723 | .Sp | |
724 | Be 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 | |
726 | might 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 | |
728 | previous time \f(CW\*(C`caller\*(C'\fR was called. | |
729 | .IP "chdir \s-1EXPR\s0" 8 | |
730 | .IX Item "chdir EXPR" | |
731 | Changes the working directory to \s-1EXPR\s0, if possible. If \s-1EXPR\s0 is omitted, | |
732 | changes to the directory specified by \f(CW$ENV{HOME}\fR, if set; if not, | |
733 | changes to the directory specified by \f(CW$ENV{LOGDIR}\fR. (Under \s-1VMS\s0, the | |
734 | variable \f(CW$ENV{SYS$LOGIN}\fR is also checked, and used if it is set.) If | |
735 | neither is set, \f(CW\*(C`chdir\*(C'\fR does nothing. It returns true upon success, | |
736 | false otherwise. See the example under \f(CW\*(C`die\*(C'\fR. | |
737 | .IP "chmod \s-1LIST\s0" 8 | |
738 | .IX Item "chmod LIST" | |
739 | Changes the permissions of a list of files. The first element of the | |
740 | list must be the numerical mode, which should probably be an octal | |
741 | number, 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 | |
743 | successfully 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 | |
754 | You can also import the symbolic \f(CW\*(C`S_I*\*(C'\fR constants from the Fcntl | |
755 | module: | |
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 | |
773 | This safer version of \*(L"chop\*(R" removes any trailing string | |
774 | that 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 | |
776 | number of characters removed from all its arguments. It's often used to | |
777 | remove the newline from the end of an input record when you're worried | |
778 | that the final record may be missing its newline. When in paragraph | |
779 | mode (\f(CW\*(C`$/ = ""\*(C'\fR), it removes all trailing newlines from the string. | |
780 | When in slurp mode (\f(CW\*(C`$/ = undef\*(C'\fR) or fixed-length record mode (\f(CW$/\fR is | |
781 | a reference to an integer or the like, see perlvar) \fIchomp()\fR won't | |
782 | remove anything. | |
783 | If \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 | |
793 | If \s-1VARIABLE\s0 is a hash, it chomps the hash's values, but not its keys. | |
794 | .Sp | |
795 | You 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 | |
802 | If you chomp a list, each element is chomped, and the total number of | |
803 | characters removed is returned. | |
804 | .Sp | |
805 | Note that parentheses are necessary when you're chomping anything | |
806 | that is not a simple variable. This is because \f(CW\*(C`chomp $cwd = `pwd`;\*(C'\fR | |
807 | is 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 | |
810 | as \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 | |
819 | Chops off the last character of a string and returns the character | |
820 | chopped. It is much more efficient than \f(CW\*(C`s/.$//s\*(C'\fR because it neither | |
821 | scans nor copies the string. If \s-1VARIABLE\s0 is omitted, chops \f(CW$_\fR. | |
822 | If \s-1VARIABLE\s0 is a hash, it chops the hash's values, but not its keys. | |
823 | .Sp | |
824 | You can actually chop anything that's an lvalue, including an assignment. | |
825 | .Sp | |
826 | If you chop a list, each element is chopped. Only the value of the | |
827 | last \f(CW\*(C`chop\*(C'\fR is returned. | |
828 | .Sp | |
829 | Note that \f(CW\*(C`chop\*(C'\fR returns the last character. To return all but the last | |
830 | character, use \f(CW\*(C`substr($string, 0, \-1)\*(C'\fR. | |
831 | .Sp | |
832 | See also \*(L"chomp\*(R". | |
833 | .IP "chown \s-1LIST\s0" 8 | |
834 | .IX Item "chown LIST" | |
835 | Changes the owner (and group) of a list of files. The first two | |
836 | elements of the list must be the \fInumeric\fR uid and gid, in that | |
837 | order. A value of \-1 in either position is interpreted by most | |
838 | systems to leave that value unchanged. Returns the number of files | |
839 | successfully changed. | |
840 | .Sp | |
841 | .Vb 2 | |
842 | \& $cnt = chown $uid, $gid, 'foo', 'bar'; | |
843 | \& chown $uid, $gid, @filenames; | |
844 | .Ve | |
845 | .Sp | |
846 | Here'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 | |
865 | On most systems, you are not allowed to change the ownership of the | |
866 | file unless you're the superuser, although you should be able to change | |
867 | the group to any of your secondary groups. On insecure systems, these | |
868 | restrictions may be relaxed, but this is not a portable assumption. | |
869 | On \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 | |
881 | Returns the character represented by that \s-1NUMBER\s0 in the character set. | |
882 | For example, \f(CW\*(C`chr(65)\*(C'\fR is \f(CW"A"\fR in either \s-1ASCII\s0 or Unicode, and | |
883 | chr(0x263a) is a Unicode smiley face. Note that characters from 127 | |
884 | to 255 (inclusive) are by default not encoded in Unicode for backward | |
885 | compatibility reasons (but see encoding). | |
886 | .Sp | |
887 | For the reverse, use \*(L"ord\*(R". | |
888 | See perlunicode and encoding for more about Unicode. | |
889 | .Sp | |
890 | If \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 | |
897 | This function works like the system call by the same name: it makes the | |
898 | named directory the new root directory for all further pathnames that | |
899 | begin with a \f(CW\*(C`/\*(C'\fR by your process and all its children. (It doesn't | |
900 | change your current working directory, which is unaffected.) For security | |
901 | reasons, this call is restricted to the superuser. If \s-1FILENAME\s0 is | |
902 | omitted, 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 | |
909 | Closes the file or pipe associated with the file handle, returning | |
910 | true only if \s-1IO\s0 buffers are successfully flushed and closes the system | |
911 | file descriptor. Closes the currently selected filehandle if the | |
912 | argument is omitted. | |
913 | .Sp | |
914 | You don't have to close \s-1FILEHANDLE\s0 if you are immediately going to do | |
915 | another \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 | |
917 | counter (\f(CW$.\fR), while the implicit close done by \f(CW\*(C`open\*(C'\fR does not. | |
918 | .Sp | |
919 | If the file handle came from a piped open \f(CW\*(C`close\*(C'\fR will additionally | |
920 | return false if one of the other system calls involved fails or if the | |
921 | program exits with non-zero status. (If the only problem was that the | |
922 | program exited non-zero \f(CW$!\fR will be set to \f(CW0\fR.) Closing a pipe | |
923 | also waits for the process executing on the pipe to complete, in case you | |
924 | want to look at the output of the pipe afterwards, and | |
925 | implicitly puts the exit status value of that command into \f(CW$?\fR. | |
926 | .Sp | |
927 | Prematurely closing the read end of a pipe (i.e. before the process | |
928 | writing 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 | |
930 | handle that, be sure to read all the data before closing the pipe. | |
931 | .Sp | |
932 | Example: | |
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 | |
946 | filehandle, usually the real filehandle name. | |
947 | .IP "closedir \s-1DIRHANDLE\s0" 8 | |
948 | .IX Item "closedir DIRHANDLE" | |
949 | Closes a directory opened by \f(CW\*(C`opendir\*(C'\fR and returns the success of that | |
950 | system call. | |
951 | .Sp | |
952 | \&\s-1DIRHANDLE\s0 may be an expression whose value can be used as an indirect | |
953 | dirhandle, usually the real dirhandle name. | |
954 | .IP "connect \s-1SOCKET\s0,NAME" 8 | |
955 | .IX Item "connect SOCKET,NAME" | |
956 | Attempts to connect to a remote socket, just as the connect system call | |
957 | does. Returns true if it succeeded, false otherwise. \s-1NAME\s0 should be a | |
958 | packed 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" | |
962 | Actually 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 | |
965 | be evaluated again, just like the third part of a \f(CW\*(C`for\*(C'\fR loop in C. Thus | |
966 | it can be used to increment a loop variable, even when the loop has been | |
967 | continued via the \f(CW\*(C`next\*(C'\fR statement (which is similar to the C \f(CW\*(C`continue\*(C'\fR | |
968 | statement). | |
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 | |
971 | block. \f(CW\*(C`last\*(C'\fR and \f(CW\*(C`redo\*(C'\fR will behave as if they had been executed within | |
972 | the main block. So will \f(CW\*(C`next\*(C'\fR, but since it will execute a \f(CW\*(C`continue\*(C'\fR | |
973 | block, 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 | |
987 | Omitting the \f(CW\*(C`continue\*(C'\fR section is semantically equivalent to using an | |
988 | empty one, logically enough. In that case, \f(CW\*(C`next\*(C'\fR goes directly back | |
989 | to 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 | |
996 | Returns the cosine of \s-1EXPR\s0 (expressed in radians). If \s-1EXPR\s0 is omitted, | |
997 | takes cosine of \f(CW$_\fR. | |
998 | .Sp | |
999 | For the inverse cosine operation, you may use the \f(CW\*(C`Math::Trig::acos()\*(C'\fR | |
1000 | function, 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" | |
1007 | Encrypts 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 | |
1009 | extirpated as a potential munition). This can prove useful for checking | |
1010 | the password file for lousy passwords, amongst other things. Only the | |
1011 | guys wearing white hats should do this. | |
1012 | .Sp | |
1013 | Note that \f(CW\*(C`crypt\*(C'\fR is intended to be a one-way function, much like | |
1014 | breaking eggs to make an omelette. There is no (known) corresponding | |
1015 | decrypt function (in other words, the \fIcrypt()\fR is a one-way hash | |
1016 | function). As a result, this function isn't all that useful for | |
1017 | cryptography. (For that, see your nearby \s-1CPAN\s0 mirror.) | |
1018 | .Sp | |
1019 | When verifying an existing encrypted string you should use the | |
1020 | encrypted 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 | |
1022 | and with more exotic implementations. In other words, do not assume | |
1023 | anything about the returned string itself, or how many bytes in | |
1024 | the encrypted string matter. | |
1025 | .Sp | |
1026 | Traditionally the result is a string of 13 bytes: two first bytes of | |
1027 | the salt, followed by 11 bytes from the set \f(CW\*(C`[./0\-9A\-Za\-z]\*(C'\fR, and only | |
1028 | the first eight bytes of the encrypted string mattered, but | |
1029 | alternative hashing schemes (like \s-1MD5\s0), higher level security schemes | |
1030 | (like C2), and implementations on non-UNIX platforms may produce | |
1031 | different strings. | |
1032 | .Sp | |
1033 | When choosing a new salt create a random two character string whose | |
1034 | characters 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 | |
1037 | Here's an example that makes sure that whoever runs this program knows | |
1038 | their 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 | |
1060 | Of course, typing in your own password to whoever asks you | |
1061 | for it is unwise. | |
1062 | .Sp | |
1063 | The crypt function is unsuitable for encrypting large quantities | |
1064 | of data, not least of all because you can't get the information | |
1065 | back. Look at the \fIby\-module/Crypt\fR and \fIby\-module/PGP\fR directories | |
1066 | on your favorite \s-1CPAN\s0 mirror for a slew of potentially useful | |
1067 | modules. | |
1068 | .Sp | |
1069 | If using \fIcrypt()\fR on a Unicode string (which \fIpotentially\fR has | |
1070 | characters with codepoints above 255), Perl tries to make sense | |
1071 | of the situation by trying to downgrade (a copy of the string) | |
1072 | the 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 | |
1079 | Breaks 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 | |
1084 | This binds a \fIdbm\fR\|(3), \fIndbm\fR\|(3), \fIsdbm\fR\|(3), \fIgdbm\fR\|(3), or Berkeley \s-1DB\s0 file to a | |
1085 | hash. \s-1HASH\s0 is the name of the hash. (Unlike normal \f(CW\*(C`open\*(C'\fR, the first | |
1086 | argument is \fInot\fR a filehandle, even though it looks like one). \s-1DBNAME\s0 | |
1087 | is the name of the database (without the \fI.dir\fR or \fI.pag\fR extension if | |
1088 | any). If the database does not exist, it is created with protection | |
1089 | specified by \s-1MASK\s0 (as modified by the \f(CW\*(C`umask\*(C'\fR). If your system supports | |
1090 | only the older \s-1DBM\s0 functions, you may perform only one \f(CW\*(C`dbmopen\*(C'\fR in your | |
1091 | program. In older versions of Perl, if your system had neither \s-1DBM\s0 nor | |
1092 | ndbm, calling \f(CW\*(C`dbmopen\*(C'\fR produced a fatal error; it now falls back to | |
1093 | \&\fIsdbm\fR\|(3). | |
1094 | .Sp | |
1095 | If you don't have write access to the \s-1DBM\s0 file, you can only read hash | |
1096 | variables, not set them. If you want to test whether you can write, | |
1097 | either use file tests or try setting a dummy hash entry inside an \f(CW\*(C`eval\*(C'\fR, | |
1098 | which will trap the error. | |
1099 | .Sp | |
1100 | Note that functions such as \f(CW\*(C`keys\*(C'\fR and \f(CW\*(C`values\*(C'\fR may return huge lists | |
1101 | when used on large \s-1DBM\s0 files. You may prefer to use the \f(CW\*(C`each\*(C'\fR | |
1102 | function 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 | |
1113 | See also AnyDBM_File for a more general description of the pros and | |
1114 | cons of the various dbm approaches, as well as DB_File for a particularly | |
1115 | rich implementation. | |
1116 | .Sp | |
1117 | You can control which \s-1DBM\s0 library you use by loading that library | |
1118 | before 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 | |
1131 | Returns a Boolean value telling whether \s-1EXPR\s0 has a value other than | |
1132 | the undefined value \f(CW\*(C`undef\*(C'\fR. If \s-1EXPR\s0 is not present, \f(CW$_\fR will be | |
1133 | checked. | |
1134 | .Sp | |
1135 | Many operations return \f(CW\*(C`undef\*(C'\fR to indicate failure, end of file, | |
1136 | system error, uninitialized variable, and other exceptional | |
1137 | conditions. This function allows you to distinguish \f(CW\*(C`undef\*(C'\fR from | |
1138 | other 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 | |
1140 | false.) Note that since \f(CW\*(C`undef\*(C'\fR is a valid scalar, its presence | |
1141 | doesn't \fInecessarily\fR indicate an exceptional condition: \f(CW\*(C`pop\*(C'\fR | |
1142 | returns \f(CW\*(C`undef\*(C'\fR when its argument is an empty array, \fIor\fR when the | |
1143 | element to return happens to be \f(CW\*(C`undef\*(C'\fR. | |
1144 | .Sp | |
1145 | You may also use \f(CW\*(C`defined(&func)\*(C'\fR to check whether subroutine \f(CW&func\fR | |
1146 | has ever been defined. The return value is unaffected by any forward | |
1147 | declarations of \f(CW&foo\fR. Note that a subroutine which is not defined | |
1148 | may still be callable: its package may have an \f(CW\*(C`AUTOLOAD\*(C'\fR method that | |
1149 | makes it spring into existence the first time that it is called \*(-- see | |
1150 | perlsub. | |
1151 | .Sp | |
1152 | Use of \f(CW\*(C`defined\*(C'\fR on aggregates (hashes and arrays) is deprecated. It | |
1153 | used to report whether memory for that aggregate has ever been | |
1154 | allocated. This behavior may disappear in future versions of Perl. | |
1155 | You 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 | |
1162 | When used on a hash element, it tells you whether the value is defined, | |
1163 | not whether the key exists in the hash. Use \*(L"exists\*(R" for the latter | |
1164 | purpose. | |
1165 | .Sp | |
1166 | Examples: | |
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 | |
1177 | Note: Many folks tend to overuse \f(CW\*(C`defined\*(C'\fR, and then are surprised to | |
1178 | discover that the number \f(CW0\fR and \f(CW""\fR (the zero-length string) are, in fact, | |
1179 | defined values. For example, if you say | |
1180 | .Sp | |
1181 | .Vb 1 | |
1182 | \& "ab" =~ /a(.*)b/; | |
1183 | .Ve | |
1184 | .Sp | |
1185 | The pattern match succeeds, and \f(CW$1\fR is defined, despite the fact that it | |
1186 | matched \*(L"nothing\*(R". But it didn't really match nothing\*(--rather, it | |
1187 | matched something that happened to be zero characters long. This is all | |
1188 | very above-board and honest. When a function returns an undefined value, | |
1189 | it's an admission that it couldn't give you an honest answer. So you | |
1190 | should use \f(CW\*(C`defined\*(C'\fR only when you're questioning the integrity of what | |
1191 | you're trying to do. At other times, a simple comparison to \f(CW0\fR or \f(CW""\fR is | |
1192 | what you want. | |
1193 | .Sp | |
1194 | See also \*(L"undef\*(R", \*(L"exists\*(R", \*(L"ref\*(R". | |
1195 | .IP "delete \s-1EXPR\s0" 8 | |
1196 | .IX Item "delete EXPR" | |
1197 | Given an expression that specifies a hash element, array element, hash slice, | |
1198 | or array slice, deletes the specified element(s) from the hash or array. | |
1199 | In the case of an array, if the array elements happen to be at the end, | |
1200 | the size of the array will shrink to the highest element that tests | |
1201 | true for \fIexists()\fR (or 0 if no such element exists). | |
1202 | .Sp | |
1203 | Returns each element so deleted or the undefined value if there was no such | |
1204 | element. Deleting from \f(CW$ENV{}\fR modifies the environment. Deleting from | |
1205 | a hash tied to a \s-1DBM\s0 file deletes the entry from the \s-1DBM\s0 file. Deleting | |
1206 | from a \f(CW\*(C`tie\*(C'\fRd hash or array may not necessarily return anything. | |
1207 | .Sp | |
1208 | Deleting an array element effectively returns that position of the array | |
1209 | to its initial, uninitialized state. Subsequently testing for the same | |
1210 | element with \fIexists()\fR will return false. Note that deleting array | |
1211 | elements in the middle of an array will not shift the index of the ones | |
1212 | after them down\*(--use \fIsplice()\fR for that. See \*(L"exists\*(R". | |
1213 | .Sp | |
1214 | The 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 | |
1228 | And 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 | |
1238 | But both of these are slower than just assigning the empty list | |
1239 | or 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 | |
1251 | Note that the \s-1EXPR\s0 can be arbitrarily complicated as long as the final | |
1252 | operation is a hash element, array element, hash slice, or array slice | |
1253 | lookup: | |
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" | |
1266 | Outside an \f(CW\*(C`eval\*(C'\fR, prints the value of \s-1LIST\s0 to \f(CW\*(C`STDERR\*(C'\fR and | |
1267 | exits with the current value of \f(CW$!\fR (errno). If \f(CW$!\fR is \f(CW0\fR, | |
1268 | exits with the value of \f(CW\*(C`($? >> 8)\*(C'\fR (backtick `command` | |
1269 | status). If \f(CW\*(C`($? >> 8)\*(C'\fR is \f(CW0\fR, exits with \f(CW255\fR. Inside | |
1270 | an \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 | |
1274 | Equivalent 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 | |
1281 | If the last element of \s-1LIST\s0 does not end in a newline, the current | |
1282 | script line number and input line number (if any) are also printed, | |
1283 | and a newline is supplied. Note that the \*(L"input line number\*(R" (also | |
1284 | known as \*(L"chunk\*(R") is subject to whatever notion of \*(L"line\*(R" happens to | |
1285 | be 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 | |
1288 | Hint: sometimes appending \f(CW", stopped"\fR to your message will cause it | |
1289 | to make better sense when the string \f(CW"at foo line 123"\fR is appended. | |
1290 | Suppose 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 | |
1297 | produce, 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 | |
1304 | See also \fIexit()\fR, \fIwarn()\fR, and the Carp module. | |
1305 | .Sp | |
1306 | If \s-1LIST\s0 is empty and \f(CW$@\fR already contains a value (typically from a | |
1307 | previous eval) that value is reused after appending \f(CW"\et...propagated"\fR. | |
1308 | This is useful for propagating exceptions: | |
1309 | .Sp | |
1310 | .Vb 2 | |
1311 | \& eval { ... }; | |
1312 | \& die unless $@ =~ /Expected exception/; | |
1313 | .Ve | |
1314 | .Sp | |
1315 | If \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 | |
1317 | and 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_\|_) };>> | |
1319 | were called. | |
1320 | .Sp | |
1321 | If \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 | |
1324 | trapped within an \fIeval()\fR, $@ contains the reference. This behavior permits | |
1325 | a more elaborate exception handling implementation using objects that | |
1326 | maintain arbitrary state about the nature of the exception. Such a scheme | |
1327 | is sometimes preferable to matching particular string values of $@ using | |
1328 | regular 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 | |
1342 | Because perl will stringify uncaught exception messages before displaying | |
1343 | them, you may want to overload stringification operations on such custom | |
1344 | exception objects. See overload for details about that. | |
1345 | .Sp | |
1346 | You can arrange for a callback to be run just before the \f(CW\*(C`die\*(C'\fR | |
1347 | does its deed, by setting the \f(CW$SIG{_\|_DIE_\|_}\fR hook. The associated | |
1348 | handler will be called with the error text and can change the error | |
1349 | message, 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 | |
1352 | to be run only right before your program was to exit, this is not | |
1353 | currently the case\*(--the \f(CW$SIG{_\|_DIE_\|_}\fR hook is currently called | |
1354 | even inside \fIeval()\fRed blocks/strings! If one wants the hook to do | |
1355 | nothing in such situations, put | |
1356 | .Sp | |
1357 | .Vb 1 | |
1358 | \& die @_ if $^S; | |
1359 | .Ve | |
1360 | .Sp | |
1361 | as the first line of the handler (see \*(L"$^S\*(R" in perlvar). Because | |
1362 | this promotes strange action at a distance, this counterintuitive | |
1363 | behavior may be fixed in a future release. | |
1364 | .IP "do \s-1BLOCK\s0" 8 | |
1365 | .IX Item "do BLOCK" | |
1366 | Not really a function. Returns the value of the last command in the | |
1367 | sequence of commands indicated by \s-1BLOCK\s0. When modified by a loop | |
1368 | modifier, 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. | |
1373 | See perlsyn for alternative strategies. | |
1374 | .IP "do \s-1SUBROUTINE\s0(\s-1LIST\s0)" 8 | |
1375 | .IX Item "do SUBROUTINE(LIST)" | |
1376 | A deprecated form of subroutine call. See perlsub. | |
1377 | .IP "do \s-1EXPR\s0" 8 | |
1378 | .IX Item "do EXPR" | |
1379 | Uses the value of \s-1EXPR\s0 as a filename and executes the contents of the | |
1380 | file as a Perl script. Its primary use is to include subroutines | |
1381 | from a Perl subroutine library. | |
1382 | .Sp | |
1383 | .Vb 1 | |
1384 | \& do 'stat.pl'; | |
1385 | .Ve | |
1386 | .Sp | |
1387 | is just like | |
1388 | .Sp | |
1389 | .Vb 1 | |
1390 | \& eval `cat stat.pl`; | |
1391 | .Ve | |
1392 | .Sp | |
1393 | except that it's more efficient and concise, keeps track of the current | |
1394 | filename 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 | |
1396 | variables. It also differs in that code evaluated with \f(CW\*(C`do FILENAME\*(C'\fR | |
1397 | cannot see lexicals in the enclosing scope; \f(CW\*(C`eval STRING\*(C'\fR does. It's the | |
1398 | same, however, in that it does reparse the file every time you call it, | |
1399 | so you probably don't want to do this inside a loop. | |
1400 | .Sp | |
1401 | If \f(CW\*(C`do\*(C'\fR cannot read the file, it returns undef and sets \f(CW$!\fR to the | |
1402 | error. If \f(CW\*(C`do\*(C'\fR can read the file but cannot compile it, it | |
1403 | returns undef and sets an error message in \f(CW$@\fR. If the file is | |
1404 | successfully compiled, \f(CW\*(C`do\*(C'\fR returns the value of the last expression | |
1405 | evaluated. | |
1406 | .Sp | |
1407 | Note 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 | |
1409 | and raise an exception if there's a problem. | |
1410 | .Sp | |
1411 | You might like to use \f(CW\*(C`do\*(C'\fR to read in a program configuration | |
1412 | file. 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 | |
1432 | This function causes an immediate core dump. See also the \fB\-u\fR | |
1433 | command-line switch in perlrun, which does the same thing. | |
1434 | Primarily this is so that you can use the \fBundump\fR program (not | |
1435 | supplied) to turn your core dump into an executable binary after | |
1436 | having initialized all your variables at the beginning of the | |
1437 | program. When the new binary is executed it will begin by executing | |
1438 | a \f(CW\*(C`goto LABEL\*(C'\fR (with all the restrictions that \f(CW\*(C`goto\*(C'\fR suffers). | |
1439 | Think of it as a goto with an intervening core dump and reincarnation. | |
1440 | If \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 | |
1443 | be open any more when the program is reincarnated, with possible | |
1444 | resulting confusion on the part of Perl. | |
1445 | .Sp | |
1446 | This function is now largely obsolete, partly because it's very | |
1447 | hard to convert a core file into an executable, and because the | |
1448 | real compiler backends for generating portable bytecode and compilable | |
1449 | C 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 | |
1451 | typo. | |
1452 | .Sp | |
1453 | If you're looking to use dump to speed up your program, consider | |
1454 | generating bytecode or native C code as described in perlcc. If | |
1455 | you'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. | |
1457 | You might also consider autoloading or selfloading, which at least | |
1458 | make your program \fIappear\fR to run faster. | |
1459 | .IP "each \s-1HASH\s0" 8 | |
1460 | .IX Item "each HASH" | |
1461 | When called in list context, returns a 2\-element list consisting of the | |
1462 | key and value for the next element of a hash, so that you can iterate over | |
1463 | it. When called in scalar context, returns only the key for the next | |
1464 | element in the hash. | |
1465 | .Sp | |
1466 | Entries are returned in an apparently random order. The actual random | |
1467 | order is subject to change in future versions of perl, but it is guaranteed | |
1468 | to be in the same order as either the \f(CW\*(C`keys\*(C'\fR or \f(CW\*(C`values\*(C'\fR function | |
1469 | would produce on the same (unmodified) hash. | |
1470 | .Sp | |
1471 | When 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 | |
1473 | scalar context. The next call to \f(CW\*(C`each\*(C'\fR after that will start iterating | |
1474 | again. 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 | |
1476 | reading 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 | |
1478 | iterating over it, you may get entries skipped or duplicated, so | |
1479 | don't. Exception: It is always safe to delete the item most recently | |
1480 | returned 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 | |
1489 | The following prints out your environment like the \fIprintenv\fR\|(1) program, | |
1490 | only 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 | |
1498 | See 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 | |
1507 | Returns 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 | |
1509 | gives the real filehandle. (Note that this function actually | |
1510 | reads a character and then \f(CW\*(C`ungetc\*(C'\fRs it, so isn't very useful in an | |
1511 | interactive 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 | |
1513 | as terminals may lose the end-of-file condition if you do. | |
1514 | .Sp | |
1515 | An \f(CW\*(C`eof\*(C'\fR without an argument uses the last file read. Using \f(CW\*(C`eof()\*(C'\fR | |
1516 | with empty parentheses is very different. It refers to the pseudo file | |
1517 | formed 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, | |
1519 | as a normal filehandle is, an \f(CW\*(C`eof()\*(C'\fR before \f(CW\*(C`<>\*(C'\fR has been | |
1520 | used will cause \f(CW@ARGV\fR to be examined to determine if input is | |
1521 | available. Similarly, an \f(CW\*(C`eof()\*(C'\fR after \f(CW\*(C`<>\*(C'\fR has returned | |
1522 | end-of-file will assume you are processing another \f(CW@ARGV\fR list, | |
1523 | and if you haven't set \f(CW@ARGV\fR, will read input from \f(CW\*(C`STDIN\*(C'\fR; | |
1524 | see \*(L"I/O Operators\*(R" in perlop. | |
1525 | .Sp | |
1526 | In 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 | |
1527 | detect the end of each file, \f(CW\*(C`eof()\*(C'\fR will only detect the end of the | |
1528 | last 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 | |
1552 | Practical hint: you almost never need to use \f(CW\*(C`eof\*(C'\fR in Perl, because the | |
1553 | input operators typically return \f(CW\*(C`undef\*(C'\fR when they run out of data, or if | |
1554 | there 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 | |
1561 | In the first form, the return value of \s-1EXPR\s0 is parsed and executed as if it | |
1562 | were a little Perl program. The value of the expression (which is itself | |
1563 | determined within scalar context) is first parsed, and if there weren't any | |
1564 | errors, executed in the lexical context of the current Perl program, so | |
1565 | that any variable settings or subroutine and format definitions remain | |
1566 | afterwards. Note that the value is parsed every time the eval executes. | |
1567 | If \s-1EXPR\s0 is omitted, evaluates \f(CW$_\fR. This form is typically used to | |
1568 | delay parsing and subsequent execution of the text of \s-1EXPR\s0 until run time. | |
1569 | .Sp | |
1570 | In the second form, the code within the \s-1BLOCK\s0 is parsed only once\*(--at the | |
1571 | same time the code surrounding the eval itself was parsed\*(--and executed | |
1572 | within the context of the current Perl program. This form is typically | |
1573 | used to trap exceptions more efficiently than the first (see below), while | |
1574 | also providing the benefit of checking the code within \s-1BLOCK\s0 at compile | |
1575 | time. | |
1576 | .Sp | |
1577 | The final semicolon, if any, may be omitted from the value of \s-1EXPR\s0 or within | |
1578 | the \s-1BLOCK\s0. | |
1579 | .Sp | |
1580 | In both forms, the value returned is the value of the last expression | |
1581 | evaluated inside the mini\-program; a return statement may be also used, just | |
1582 | as with subroutines. The expression providing the return value is evaluated | |
1583 | in void, scalar, or list context, depending on the context of the eval itself. | |
1584 | See \*(L"wantarray\*(R" for more on how the evaluation context can be determined. | |
1585 | .Sp | |
1586 | If there is a syntax error or runtime error, or a \f(CW\*(C`die\*(C'\fR statement is | |
1587 | executed, an undefined value is returned by \f(CW\*(C`eval\*(C'\fR, and \f(CW$@\fR is set to the | |
1588 | error message. If there was no error, \f(CW$@\fR is guaranteed to be a null | |
1589 | string. Beware that using \f(CW\*(C`eval\*(C'\fR neither silences perl from printing | |
1590 | warnings to \s-1STDERR\s0, nor does it stuff the text of warning messages into \f(CW$@\fR. | |
1591 | To do either of those, you have to use the \f(CW$SIG{_\|_WARN_\|_}\fR facility, or | |
1592 | turn off warnings inside the \s-1BLOCK\s0 or \s-1EXPR\s0 using \f(CW\*(C`no\ warnings\ 'all'\*(C'\fR. | |
1593 | See \*(L"warn\*(R", perlvar, warnings and perllexwarn. | |
1594 | .Sp | |
1595 | Note that, because \f(CW\*(C`eval\*(C'\fR traps otherwise-fatal errors, it is useful for | |
1596 | determining whether a particular feature (such as \f(CW\*(C`socket\*(C'\fR or \f(CW\*(C`symlink\*(C'\fR) | |
1597 | is implemented. It is also Perl's exception trapping mechanism, where | |
1598 | the die operator is used to raise exceptions. | |
1599 | .Sp | |
1600 | If the code to be executed doesn't vary, you may use the eval-BLOCK | |
1601 | form to trap run-time errors without incurring the penalty of | |
1602 | recompiling each time. The error, if any, is still returned in \f(CW$@\fR. | |
1603 | Examples: | |
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 | |
1625 | Due to the current arguably broken state of \f(CW\*(C`_\|_DIE_\|_\*(C'\fR hooks, when using | |
1626 | the \f(CW\*(C`eval{}\*(C'\fR form as an exception trap in libraries, you may wish not | |
1627 | to trigger any \f(CW\*(C`_\|_DIE_\|_\*(C'\fR hooks that user code may have installed. | |
1628 | You can use the \f(CW\*(C`local $SIG{_\|_DIE_\|_}\*(C'\fR construct for this purpose, | |
1629 | as 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 | |
1637 | This 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 | |
1650 | Because this promotes action at a distance, this counterintuitive behavior | |
1651 | may be fixed in a future release. | |
1652 | .Sp | |
1653 | With an \f(CW\*(C`eval\*(C'\fR, you should be especially careful to remember what's | |
1654 | being 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 | |
1671 | Cases 1 and 2 above behave identically: they run the code contained in | |
1672 | the variable \f(CW$x\fR. (Although case 2 has misleading double quotes making | |
1673 | the reader wonder what else might be happening (nothing is).) Cases 3 | |
1674 | and 4 likewise behave in the same way: they run the code \f(CW'$x'\fR, which | |
1675 | does nothing but return the value of \f(CW$x\fR. (Case 4 is preferred for | |
1676 | purely visual reasons, but it also has the advantage of compiling at | |
1677 | compile-time instead of at run\-time.) Case 5 is a place where | |
1678 | normally you \fIwould\fR like to use double quotes, except that in this | |
1679 | particular situation, you can just use symbolic references instead, as | |
1680 | in 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 | |
1690 | The \f(CW\*(C`exec\*(C'\fR function executes a system command \fIand never returns\fR\-\- | |
1691 | use \f(CW\*(C`system\*(C'\fR instead of \f(CW\*(C`exec\*(C'\fR if you want it to return. It fails and | |
1692 | returns false only if the command does not exist \fIand\fR it is executed | |
1693 | directly instead of via your system's command shell (see below). | |
1694 | .Sp | |
1695 | Since it's a common mistake to use \f(CW\*(C`exec\*(C'\fR instead of \f(CW\*(C`system\*(C'\fR, Perl | |
1696 | warns you if there is a following statement which isn't \f(CW\*(C`die\*(C'\fR, \f(CW\*(C`warn\*(C'\fR, | |
1697 | or \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 | |
1699 | can 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 | |
1706 | If there is more than one argument in \s-1LIST\s0, or if \s-1LIST\s0 is an array | |
1707 | with more than one value, calls \fIexecvp\fR\|(3) with the arguments in \s-1LIST\s0. | |
1708 | If there is only one scalar argument or an array with one element in it, | |
1709 | the argument is checked for shell metacharacters, and if there are any, | |
1710 | the 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). | |
1712 | If there are no shell metacharacters in the argument, it is split into | |
1713 | words and passed directly to \f(CW\*(C`execvp\*(C'\fR, which is more efficient. | |
1714 | Examples: | |
1715 | .Sp | |
1716 | .Vb 2 | |
1717 | \& exec '/bin/echo', 'Your arguments are: ', @ARGV; | |
1718 | \& exec "sort $outfile | uniq"; | |
1719 | .Ve | |
1720 | .Sp | |
1721 | If you don't really want to execute the first argument, but want to lie | |
1722 | to the program you are executing about its own name, you can specify | |
1723 | the program you actually want to run as an \*(L"indirect object\*(R" (without a | |
1724 | comma) 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 | |
1726 | the 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 | |
1733 | or, more directly, | |
1734 | .Sp | |
1735 | .Vb 1 | |
1736 | \& exec {'/bin/csh'} '-sh'; # pretend it's a login shell | |
1737 | .Ve | |
1738 | .Sp | |
1739 | When the arguments get executed via the system shell, results will | |
1740 | be subject to its quirks and capabilities. See \*(L"`STRING`\*(R" in perlop | |
1741 | for details. | |
1742 | .Sp | |
1743 | Using an indirect object with \f(CW\*(C`exec\*(C'\fR or \f(CW\*(C`system\*(C'\fR is also more | |
1744 | secure. This usage (which also works fine with \fIsystem()\fR) forces | |
1745 | interpretation of the arguments as a multivalued list, even if the | |
1746 | list had just one argument. That way you're safe from the shell | |
1747 | expanding 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 | |
1759 | The first version, the one without the indirect object, ran the \fIecho\fR | |
1760 | program, passing it \f(CW"surprise"\fR an argument. The second version | |
1761 | didn't\*(--it tried to run a program literally called \fI\*(L"echo surprise\*(R"\fR, | |
1762 | didn't find it, and set \f(CW$?\fR to a non-zero value indicating failure. | |
1763 | .Sp | |
1764 | Beginning with v5.6.0, Perl will attempt to flush all files opened for | |
1765 | output 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 | |
1767 | in English) or call the \f(CW\*(C`autoflush()\*(C'\fR method of \f(CW\*(C`IO::Handle\*(C'\fR on any | |
1768 | open handles in order to avoid lost output. | |
1769 | .Sp | |
1770 | Note that \f(CW\*(C`exec\*(C'\fR will not call your \f(CW\*(C`END\*(C'\fR blocks, nor will it call | |
1771 | any \f(CW\*(C`DESTROY\*(C'\fR methods in your objects. | |
1772 | .IP "exists \s-1EXPR\s0" 8 | |
1773 | .IX Item "exists EXPR" | |
1774 | Given an expression that specifies a hash element or array element, | |
1775 | returns true if the specified element in the hash or array has ever | |
1776 | been initialized, even if the corresponding value is undefined. The | |
1777 | element 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 | |
1791 | A hash or array element can be true only if it's defined, and defined if | |
1792 | it exists, but the reverse doesn't necessarily hold true. | |
1793 | .Sp | |
1794 | Given an expression that specifies the name of a subroutine, | |
1795 | returns true if the specified subroutine has ever been declared, even | |
1796 | if it is undefined. Mentioning a subroutine name for exists or defined | |
1797 | does not count as declaring it. Note that a subroutine which does not | |
1798 | exist may still be callable: its package may have an \f(CW\*(C`AUTOLOAD\*(C'\fR | |
1799 | method that makes it spring into existence the first time that it is | |
1800 | called \*(-- 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 | |
1807 | Note that the \s-1EXPR\s0 can be arbitrarily complicated as long as the final | |
1808 | operation 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 | |
1824 | Although the deepest nested array or hash will not spring into existence | |
1825 | just because its existence was tested, any intervening ones will. | |
1826 | Thus \f(CW\*(C`$ref\->{"A"}\*(C'\fR and \f(CW\*(C`$ref\->{"A"}\->{"B"}\*(C'\fR will spring | |
1827 | into existence due to the existence test for the \f(CW$key\fR element above. | |
1828 | This 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 | |
1836 | This surprising autovivification in what does not at first\*(--or even | |
1837 | second\*(--glance appear to be an lvalue context may be fixed in a future | |
1838 | release. | |
1839 | .Sp | |
1840 | See \*(L"Pseudo\-hashes: Using an array as a hash\*(R" in perlref for specifics | |
1841 | on how \fIexists()\fR acts when used on a pseudo\-hash. | |
1842 | .Sp | |
1843 | Use of a subroutine call, rather than a subroutine name, as an argument | |
1844 | to \fIexists()\fR is an error. | |
1845 | .Sp | |
1846 | .Vb 2 | |
1847 | \& exists ⊂ # OK | |
1848 | \& exists &sub(); # Error | |
1849 | .Ve | |
1850 | .IP "exit \s-1EXPR\s0" 8 | |
1851 | .IX Item "exit EXPR" | |
1852 | Evaluates \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 | |
1859 | See also \f(CW\*(C`die\*(C'\fR. If \s-1EXPR\s0 is omitted, exits with \f(CW0\fR status. The only | |
1860 | universally recognized values for \s-1EXPR\s0 are \f(CW0\fR for success and \f(CW1\fR | |
1861 | for error; other values are subject to interpretation depending on the | |
1862 | environment in which the Perl program is running. For example, exiting | |
1863 | 69 (\s-1EX_UNAVAILABLE\s0) from a \fIsendmail\fR incoming-mail filter will cause | |
1864 | the mailer to return the item undelivered, but that's not true everywhere. | |
1865 | .Sp | |
1866 | Don't use \f(CW\*(C`exit\*(C'\fR to abort a subroutine if there's any chance that | |
1867 | someone might want to trap whatever error happened. Use \f(CW\*(C`die\*(C'\fR instead, | |
1868 | which can be trapped by an \f(CW\*(C`eval\*(C'\fR. | |
1869 | .Sp | |
1870 | The \fIexit()\fR function does not always exit immediately. It calls any | |
1871 | defined \f(CW\*(C`END\*(C'\fR routines first, but these \f(CW\*(C`END\*(C'\fR routines may not | |
1872 | themselves abort the exit. Likewise any object destructors that need to | |
1873 | be called are called before the real exit. If this is a problem, you | |
1874 | can call \f(CW\*(C`POSIX:_exit($status)\*(C'\fR to avoid \s-1END\s0 and destructor processing. | |
1875 | See 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 | |
1882 | Returns \fIe\fR (the natural logarithm base) to the power of \s-1EXPR\s0. | |
1883 | If \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" | |
1886 | Implements the \fIfcntl\fR\|(2) function. You'll probably have to say | |
1887 | .Sp | |
1888 | .Vb 1 | |
1889 | \& use Fcntl; | |
1890 | .Ve | |
1891 | .Sp | |
1892 | first to get the correct constant definitions. Argument processing and | |
1893 | value return works just like \f(CW\*(C`ioctl\*(C'\fR below. | |
1894 | For 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 | |
1902 | You don't have to check for \f(CW\*(C`defined\*(C'\fR on the return from \f(CW\*(C`fnctl\*(C'\fR. | |
1903 | Like \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 | |
1905 | in numeric context. It is also exempt from the normal \fB\-w\fR warnings | |
1906 | on improper numeric conversions. | |
1907 | .Sp | |
1908 | Note that \f(CW\*(C`fcntl\*(C'\fR will produce a fatal error if used on a machine that | |
1909 | doesn't implement \fIfcntl\fR\|(2). See the Fcntl module or your \fIfcntl\fR\|(2) | |
1910 | manpage to learn what functions are available on your system. | |
1911 | .IP "fileno \s-1FILEHANDLE\s0" 8 | |
1912 | .IX Item "fileno FILEHANDLE" | |
1913 | Returns the file descriptor for a filehandle, or undefined if the | |
1914 | filehandle is not open. This is mainly useful for constructing | |
1915 | bitmaps for \f(CW\*(C`select\*(C'\fR and low-level \s-1POSIX\s0 tty-handling operations. | |
1916 | If \s-1FILEHANDLE\s0 is an expression, the value is taken as an indirect | |
1917 | filehandle, generally its name. | |
1918 | .Sp | |
1919 | You can use this to find out whether two handles refer to the | |
1920 | same 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 | |
1929 | return undefined even though they are open.) | |
1930 | .IP "flock \s-1FILEHANDLE\s0,OPERATION" 8 | |
1931 | .IX Item "flock FILEHANDLE,OPERATION" | |
1932 | Calls \fIflock\fR\|(2), or an emulation of it, on \s-1FILEHANDLE\s0. Returns true | |
1933 | for success, false on failure. Produces a fatal error if used on a | |
1934 | machine 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 | |
1936 | only entire files, not records. | |
1937 | .Sp | |
1938 | Two potentially non-obvious but traditional \f(CW\*(C`flock\*(C'\fR semantics are | |
1939 | that it waits indefinitely until the lock is granted, and that its locks | |
1940 | \&\fBmerely advisory\fR. Such discretionary locks are more flexible, but offer | |
1941 | fewer guarantees. This means that files locked with \f(CW\*(C`flock\*(C'\fR may be | |
1942 | modified by programs that do not also use \f(CW\*(C`flock\*(C'\fR. See perlport, | |
1943 | your port's specific documentation, or your system-specific local manpages | |
1944 | for details. It's best to assume traditional behavior if you're writing | |
1945 | portable programs. (But if you're not, you should as always feel perfectly | |
1946 | free to write for your own system's idiosyncrasies (sometimes called | |
1947 | \&\*(L"features\*(R"). Slavish adherence to portability concerns shouldn't get | |
1948 | in 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 | |
1952 | you can use the symbolic names if you import them from the Fcntl module, | |
1953 | either individually, or as a group using the ':flock' tag. \s-1LOCK_SH\s0 | |
1954 | requests a shared lock, \s-1LOCK_EX\s0 requests an exclusive lock, and \s-1LOCK_UN\s0 | |
1955 | releases 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 | |
1957 | waiting for the lock (check the return status to see if you got it). | |
1958 | .Sp | |
1959 | To avoid the possibility of miscoordination, Perl now flushes \s-1FILEHANDLE\s0 | |
1960 | before locking or unlocking it. | |
1961 | .Sp | |
1962 | Note that the emulation built with \fIlockf\fR\|(3) doesn't provide shared | |
1963 | locks, and it requires that \s-1FILEHANDLE\s0 be open with write intent. These | |
1964 | are the semantics that \fIlockf\fR\|(3) implements. Most if not all systems | |
1965 | implement \fIlockf\fR\|(3) in terms of \fIfcntl\fR\|(2) locking, though, so the | |
1966 | differing semantics shouldn't bite too many people. | |
1967 | .Sp | |
1968 | Note that the \fIfcntl\fR\|(2) emulation of \fIflock\fR\|(3) requires that \s-1FILEHANDLE\s0 | |
1969 | be open with read intent to use \s-1LOCK_SH\s0 and requires that it be open | |
1970 | with write intent to use \s-1LOCK_EX\s0. | |
1971 | .Sp | |
1972 | Note also that some versions of \f(CW\*(C`flock\*(C'\fR cannot lock things over the | |
1973 | network; you would need to use the more system-specific \f(CW\*(C`fcntl\*(C'\fR for | |
1974 | that. If you like you can force Perl to ignore your system's \fIflock\fR\|(2) | |
1975 | function, and so provide its own \fIfcntl\fR\|(2)\-based emulation, by passing | |
1976 | the switch \f(CW\*(C`\-Ud_flock\*(C'\fR to the \fIConfigure\fR program when you configure | |
1977 | perl. | |
1978 | .Sp | |
1979 | Here'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 | |
2011 | On systems that support a real \fIflock()\fR, locks are inherited across \fIfork()\fR | |
2012 | calls, whereas those that must resort to the more capricious \fIfcntl()\fR | |
2013 | function lose the locks, making it harder to write servers. | |
2014 | .Sp | |
2015 | See also DB_File for other \fIflock()\fR examples. | |
2016 | .IP "fork" 8 | |
2017 | .IX Item "fork" | |
2018 | Does a \fIfork\fR\|(2) system call to create a new process running the | |
2019 | same program at the same point. It returns the child pid to the | |
2020 | parent process, \f(CW0\fR to the child process, or \f(CW\*(C`undef\*(C'\fR if the fork is | |
2021 | unsuccessful. File descriptors (and sometimes locks on those descriptors) | |
2022 | are shared, while everything else is copied. On most systems supporting | |
2023 | \&\fIfork()\fR, great care has gone into making it extremely efficient (for | |
2024 | example, using copy-on-write technology on data pages), making it the | |
2025 | dominant paradigm for multitasking over the last few decades. | |
2026 | .Sp | |
2027 | Beginning with v5.6.0, Perl will attempt to flush all files opened for | |
2028 | output before forking the child process, but this may not be supported | |
2029 | on 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 | |
2033 | If you \f(CW\*(C`fork\*(C'\fR without ever waiting on your children, you will | |
2034 | accumulate 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 | |
2036 | forking and reaping moribund children. | |
2037 | .Sp | |
2038 | Note 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 | |
2040 | if you exit, then the remote server (such as, say, a \s-1CGI\s0 script or a | |
2041 | backgrounded job launched from a remote shell) won't think you're done. | |
2042 | You should reopen those to \fI/dev/null\fR if it's any issue. | |
2043 | .IP "format" 8 | |
2044 | .IX Item "format" | |
2045 | Declare a picture format for use by the \f(CW\*(C`write\*(C'\fR function. For | |
2046 | example: | |
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 | |
2062 | See perlform for many details and examples. | |
2063 | .IP "formline \s-1PICTURE\s0,LIST" 8 | |
2064 | .IX Item "formline PICTURE,LIST" | |
2065 | This is an internal function used by \f(CW\*(C`format\*(C'\fRs, though you may call it, | |
2066 | too. It formats (see perlform) a list of values according to the | |
2067 | contents of \s-1PICTURE\s0, placing the output into the format output | |
2068 | accumulator, \f(CW$^A\fR (or \f(CW$ACCUMULATOR\fR in English). | |
2069 | Eventually, 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 | |
2071 | yourself and then set \f(CW$^A\fR back to \f(CW""\fR. Note that a format typically | |
2072 | does one \f(CW\*(C`formline\*(C'\fR per line of form, but the \f(CW\*(C`formline\*(C'\fR function itself | |
2073 | doesn't care how many newlines are embedded in the \s-1PICTURE\s0. This means | |
2074 | that the \f(CW\*(C`~\*(C'\fR and \f(CW\*(C`~~\*(C'\fR tokens will treat the entire \s-1PICTURE\s0 as a single line. | |
2075 | You may therefore need to use multiple formlines to implement a single | |
2076 | record format, just like the format compiler. | |
2077 | .Sp | |
2078 | Be careful if you put double quotes around the picture, because an \f(CW\*(C`@\*(C'\fR | |
2079 | character 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 | |
2087 | Returns the next character from the input file attached to \s-1FILEHANDLE\s0, | |
2088 | or the undefined value at end of file, or if there was an error. | |
2089 | If \s-1FILEHANDLE\s0 is omitted, reads from \s-1STDIN\s0. This is not particularly | |
2090 | efficient. However, it cannot be used by itself to fetch single | |
2091 | characters without waiting for the user to hit enter. For that, try | |
2092 | something 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 | |
2117 | Determination of whether \f(CW$BSD_STYLE\fR should be set | |
2118 | is left as an exercise to the reader. | |
2119 | .Sp | |
2120 | The \f(CW\*(C`POSIX::getattr\*(C'\fR function can do this more portably on | |
2121 | systems purporting \s-1POSIX\s0 compliance. See also the \f(CW\*(C`Term::ReadKey\*(C'\fR | |
2122 | module 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" | |
2126 | Implements the C library function of the same name, which on most | |
2127 | systems returns the current login from \fI/etc/utmp\fR, if any. If null, | |
2128 | use \f(CW\*(C`getpwuid\*(C'\fR. | |
2129 | .Sp | |
2130 | .Vb 1 | |
2131 | \& $login = getlogin || getpwuid($<) || "Kilroy"; | |
2132 | .Ve | |
2133 | .Sp | |
2134 | Do not consider \f(CW\*(C`getlogin\*(C'\fR for authentication: it is not as | |
2135 | secure as \f(CW\*(C`getpwuid\*(C'\fR. | |
2136 | .IP "getpeername \s-1SOCKET\s0" 8 | |
2137 | .IX Item "getpeername SOCKET" | |
2138 | Returns 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" | |
2149 | Returns the current process group for the specified \s-1PID\s0. Use | |
2150 | a \s-1PID\s0 of \f(CW0\fR to get the current process group for the | |
2151 | current process. Will raise an exception if used on a machine that | |
2152 | doesn't implement \fIgetpgrp\fR\|(2). If \s-1PID\s0 is omitted, returns process | |
2153 | group of current process. Note that the \s-1POSIX\s0 version of \f(CW\*(C`getpgrp\*(C'\fR | |
2154 | does 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" | |
2157 | Returns the process id of the parent process. | |
2158 | .IP "getpriority \s-1WHICH\s0,WHO" 8 | |
2159 | .IX Item "getpriority WHICH,WHO" | |
2160 | Returns 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 | |
2162 | machine 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 | |
2225 | These routines perform the same functions as their counterparts in the | |
2226 | system library. In list context, the return values from the | |
2227 | various 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 | |
2241 | The exact meaning of the \f(CW$gcos\fR field varies but it usually contains | |
2242 | the real name of the user (as opposed to the login name) and other | |
2243 | information pertaining to the user. Beware, however, that in many | |
2244 | system users are able to change this information and therefore it | |
2245 | cannot be trusted and therefore the \f(CW$gcos\fR is tainted (see | |
2246 | perlsec). The \f(CW$passwd\fR and \f(CW$shell\fR, user's encrypted password and | |
2247 | login shell, are also tainted, because of the same reason. | |
2248 | .Sp | |
2249 | In scalar context, you get the name, unless the function was a | |
2250 | lookup 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 | |
2263 | In \fIgetpw*()\fR the fields \f(CW$quota\fR, \f(CW$comment\fR, and \f(CW$expire\fR are special | |
2264 | cases 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 | |
2266 | usually encodes the disk quota. If the \f(CW$comment\fR field is unsupported, | |
2267 | it is an empty scalar. If it is supported it usually encodes some | |
2268 | administrative comment about the user. In some systems the \f(CW$quota\fR | |
2269 | field may be \f(CW$change\fR or \f(CW$age\fR, fields that have to do with password | |
2270 | aging. In some systems the \f(CW$comment\fR field may be \f(CW$class\fR. The \f(CW$expire\fR | |
2271 | field, if present, encodes the expiration period of the account or the | |
2272 | password. For the availability and the exact meaning of these fields | |
2273 | in 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 | |
2276 | by 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 | |
2278 | files are only supported if your vendor has implemented them in the | |
2279 | intuitive fashion that calling the regular C library routines gets the | |
2280 | shadow versions if you're running under privilege or if there exists | |
2281 | the \fIshadow\fR\|(3) functions as found in System V ( this includes Solaris | |
2282 | and Linux.) Those systems which implement a proprietary shadow password | |
2283 | facility are unlikely to be supported. | |
2284 | .Sp | |
2285 | The \f(CW$members\fR value returned by \fIgetgr*()\fR is a space separated list of | |
2286 | the login names of the members of the group. | |
2287 | .Sp | |
2288 | For the \fIgethost*()\fR functions, if the \f(CW\*(C`h_errno\*(C'\fR variable is supported in | |
2289 | C, 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 | |
2291 | addresses returned by the corresponding system library call. In the | |
2292 | Internet domain, each address is four bytes long and you can unpack it | |
2293 | by saying something like: | |
2294 | .Sp | |
2295 | .Vb 1 | |
2296 | \& ($a,$b,$c,$d) = unpack('C4',$addr[0]); | |
2297 | .Ve | |
2298 | .Sp | |
2299 | The 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 | |
2312 | If you get tired of remembering which element of the return list | |
2313 | contains which return value, by-name interfaces are provided | |
2314 | in 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, | |
2316 | and \f(CW\*(C`User::grent\*(C'\fR. These override the normal built\-ins, supplying | |
2317 | versions that return objects with the appropriate names | |
2318 | for 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 | |
2326 | Even though it looks like they're the same method calls (uid), | |
2327 | they aren't, because a \f(CW\*(C`File::stat\*(C'\fR object is different from | |
2328 | a \f(CW\*(C`User::pwent\*(C'\fR object. | |
2329 | .IP "getsockname \s-1SOCKET\s0" 8 | |
2330 | .IX Item "getsockname SOCKET" | |
2331 | Returns the packed sockaddr address of this end of the \s-1SOCKET\s0 connection, | |
2332 | in case you don't know the address because you have several different | |
2333 | IPs 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" | |
2345 | Returns 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 | |
2352 | In list context, returns a (possibly empty) list of filename expansions on | |
2353 | the value of \s-1EXPR\s0 such as the standard Unix shell \fI/bin/csh\fR would do. In | |
2354 | scalar context, glob iterates through such filename expansions, returning | |
2355 | undef when the list is exhausted. This is the internal function | |
2356 | implementing 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 | |
2358 | more detail in \*(L"I/O Operators\*(R" in perlop. | |
2359 | .Sp | |
2360 | Beginning 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" | |
2364 | Converts a time as returned by the time function to an 8\-element list | |
2365 | with the time localized for the standard Greenwich time zone. | |
2366 | Typically 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 | |
2374 | All list elements are numeric, and come straight out of the C `struct | |
2375 | tm'. \f(CW$sec\fR, \f(CW$min\fR, and \f(CW$hour\fR are the seconds, minutes, and hours of the | |
2376 | specified time. \f(CW$mday\fR is the day of the month, and \f(CW$mon\fR is the month | |
2377 | itself, in the range \f(CW0..11\fR with 0 indicating January and 11 | |
2378 | indicating December. \f(CW$year\fR is the number of years since 1900. That | |
2379 | is, \f(CW$year\fR is \f(CW123\fR in year 2023. \f(CW$wday\fR is the day of the week, with | |
2380 | 0 indicating Sunday and 3 indicating Wednesday. \f(CW$yday\fR is the day of | |
2381 | the year, in the range \f(CW0..364\fR (or \f(CW0..365\fR in leap years.) | |
2382 | .Sp | |
2383 | Note that the \f(CW$year\fR element is \fInot\fR simply the last two digits of | |
2384 | the year. If you assume it is, then you create non\-Y2K\-compliant | |
2385 | programs\*(--and you wouldn't want to do that, would you? | |
2386 | .Sp | |
2387 | The proper way to get a complete 4\-digit year is simply: | |
2388 | .Sp | |
2389 | .Vb 1 | |
2390 | \& $year += 1900; | |
2391 | .Ve | |
2392 | .Sp | |
2393 | And 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 | |
2399 | If \s-1EXPR\s0 is omitted, \f(CW\*(C`gmtime()\*(C'\fR uses the current time (\f(CW\*(C`gmtime(time)\*(C'\fR). | |
2400 | .Sp | |
2401 | In 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 | |
2407 | Also see the \f(CW\*(C`timegm\*(C'\fR function provided by the \f(CW\*(C`Time::Local\*(C'\fR module, | |
2408 | and the \fIstrftime\fR\|(3) function available via the \s-1POSIX\s0 module. | |
2409 | .Sp | |
2410 | This scalar value is \fBnot\fR locale dependent (see perllocale), but | |
2411 | is 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 | |
2413 | get somewhat similar but locale dependent date strings, set up your | |
2414 | locale environment variables appropriately (please see perllocale) | |
2415 | and 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 | |
2422 | Note that the \f(CW%a\fR and \f(CW%b\fR escapes, which represent the short forms | |
2423 | of the day of the week and the month of the year, may not necessarily | |
2424 | be 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 | |
2433 | The \f(CW\*(C`goto\-LABEL\*(C'\fR form finds the statement labeled with \s-1LABEL\s0 and resumes | |
2434 | execution there. It may not be used to go into any construct that | |
2435 | requires initialization, such as a subroutine or a \f(CW\*(C`foreach\*(C'\fR loop. It | |
2436 | also can't be used to go into a construct that is optimized away, | |
2437 | or to get out of a block or subroutine given to \f(CW\*(C`sort\*(C'\fR. | |
2438 | It can be used to go almost anywhere else within the dynamic scope, | |
2439 | including out of subroutines, but it's usually better to use some other | |
2440 | construct such as \f(CW\*(C`last\*(C'\fR or \f(CW\*(C`die\*(C'\fR. The author of Perl has never felt the | |
2441 | need 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 | |
2443 | loop control. Perl does, and this replaces most structured uses of \f(CW\*(C`goto\*(C'\fR | |
2444 | in other languages.) | |
2445 | .Sp | |
2446 | The \f(CW\*(C`goto\-EXPR\*(C'\fR form expects a label name, whose scope will be resolved | |
2447 | dynamically. This allows for computed \f(CW\*(C`goto\*(C'\fRs per \s-1FORTRAN\s0, but isn't | |
2448 | necessarily recommended if you're optimizing for maintainability: | |
2449 | .Sp | |
2450 | .Vb 1 | |
2451 | \& goto ("FOO", "BAR", "GLARCH")[$i]; | |
2452 | .Ve | |
2453 | .Sp | |
2454 | The \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 | |
2456 | doesn't have the stigma associated with other gotos. Instead, it | |
2457 | exits the current subroutine (losing any changes set by \fIlocal()\fR) and | |
2458 | immediately calls in its place the named subroutine using the current | |
2459 | value of \f(CW@_\fR. This is used by \f(CW\*(C`AUTOLOAD\*(C'\fR subroutines that wish to | |
2460 | load another subroutine and then pretend that the other subroutine had | |
2461 | been called in the first place (except that any modifications to \f(CW@_\fR | |
2462 | in the current subroutine are propagated to the other subroutine.) | |
2463 | After the \f(CW\*(C`goto\*(C'\fR, not even \f(CW\*(C`caller\*(C'\fR will be able to tell that this | |
2464 | routine was called first. | |
2465 | .Sp | |
2466 | \&\s-1NAME\s0 needn't be the name of a subroutine; it can be a scalar variable | |
2467 | containing a code reference, or a block which evaluates to a code | |
2468 | reference. | |
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 | |
2475 | This is similar in spirit to, but not the same as, \fIgrep\fR\|(1) and its | |
2476 | relatives. In particular, it is not limited to using regular expressions. | |
2477 | .Sp | |
2478 | Evaluates 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 | |
2480 | elements for which the expression evaluated to true. In scalar | |
2481 | context, 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 | |
2487 | or equivalently, | |
2488 | .Sp | |
2489 | .Vb 1 | |
2490 | \& @foo = grep {!/^#/} @bar; # weed out comments | |
2491 | .Ve | |
2492 | .Sp | |
2493 | Note that \f(CW$_\fR is an alias to the list value, so it can be used to | |
2494 | modify the elements of the \s-1LIST\s0. While this is useful and supported, | |
2495 | it can cause bizarre results if the elements of \s-1LIST\s0 are not variables. | |
2496 | Similarly, grep returns aliases into the original list, much as a for | |
2497 | loop's index variable aliases the list elements. That is, modifying an | |
2498 | element of a list returned by grep (for example, in a \f(CW\*(C`foreach\*(C'\fR, \f(CW\*(C`map\*(C'\fR | |
2499 | or another \f(CW\*(C`grep\*(C'\fR) actually modifies the element in the original list. | |
2500 | This is usually something to be avoided when writing clear code. | |
2501 | .Sp | |
2502 | See 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 | |
2509 | Interprets \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 | |
2518 | Hex strings may only represent integers. Strings that would cause | |
2519 | integer overflow trigger a warning. Leading whitespace is not stripped, | |
2520 | unlike \fIoct()\fR. | |
2521 | .IP "import" 8 | |
2522 | .IX Item "import" | |
2523 | There is no builtin \f(CW\*(C`import\*(C'\fR function. It is just an ordinary | |
2524 | method (subroutine) defined (or inherited) by modules that wish to export | |
2525 | names to another module. The \f(CW\*(C`use\*(C'\fR function calls the \f(CW\*(C`import\*(C'\fR method | |
2526 | for 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 | |
2533 | The index function searches for one string within another, but without | |
2534 | the wildcard-like behavior of a full regular-expression pattern match. | |
2535 | It returns the position of the first occurrence of \s-1SUBSTR\s0 in \s-1STR\s0 at | |
2536 | or after \s-1POSITION\s0. If \s-1POSITION\s0 is omitted, starts searching from the | |
2537 | beginning of the string. The return value is based at \f(CW0\fR (or whatever | |
2538 | you've set the \f(CW$[\fR variable to\*(--but don't do that). If the substring | |
2539 | is 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 | |
2546 | Returns the integer portion of \s-1EXPR\s0. If \s-1EXPR\s0 is omitted, uses \f(CW$_\fR. | |
2547 | You should not use this function for rounding: one because it truncates | |
2548 | towards \f(CW0\fR, and two because machine representations of floating point | |
2549 | numbers 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 | |
2551 | because it's really more like \-268.99999999999994315658 instead. Usually, | |
2552 | the \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 | |
2553 | functions will serve you better than will \fIint()\fR. | |
2554 | .IP "ioctl \s-1FILEHANDLE\s0,FUNCTION,SCALAR" 8 | |
2555 | .IX Item "ioctl FILEHANDLE,FUNCTION,SCALAR" | |
2556 | Implements 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 | |
2562 | to get the correct function definitions. If \fIioctl.ph\fR doesn't | |
2563 | exist or doesn't have the correct definitions you'll have to roll your | |
2564 | own, 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 | |
2566 | may help you in this, but it's nontrivial.) \s-1SCALAR\s0 will be read and/or | |
2567 | written depending on the FUNCTION\*(--a pointer to the string value of \s-1SCALAR\s0 | |
2568 | will be passed as the third argument of the actual \f(CW\*(C`ioctl\*(C'\fR call. (If \s-1SCALAR\s0 | |
2569 | has no string value but does have a numeric value, that value will be | |
2570 | passed rather than a pointer to the string value. To guarantee this to be | |
2571 | true, 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 | |
2572 | functions may be needed to manipulate the values of structures used by | |
2573 | \&\f(CW\*(C`ioctl\*(C'\fR. | |
2574 | .Sp | |
2575 | The 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 | |
2584 | Thus Perl returns true on success and false on failure, yet you can | |
2585 | still easily determine the actual value returned by the operating | |
2586 | system: | |
2587 | .Sp | |
2588 | .Vb 2 | |
2589 | \& $retval = ioctl(...) || -1; | |
2590 | \& printf "System returned %d\en", $retval; | |
2591 | .Ve | |
2592 | .Sp | |
2593 | The special string "\f(CW0\fR but true" is exempt from \fB\-w\fR complaints | |
2594 | about improper numeric conversions. | |
2595 | .Sp | |
2596 | Here's an example of setting a filehandle named \f(CW\*(C`REMOTE\*(C'\fR to be | |
2597 | non-blocking at the system level. You'll have to negotiate \f(CW$|\fR | |
2598 | on 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" | |
2615 | Joins the separate strings of \s-1LIST\s0 into a single string with fields | |
2616 | separated 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 | |
2622 | Beware that unlike \f(CW\*(C`split\*(C'\fR, \f(CW\*(C`join\*(C'\fR doesn't take a pattern as its | |
2623 | first argument. Compare \*(L"split\*(R". | |
2624 | .IP "keys \s-1HASH\s0" 8 | |
2625 | .IX Item "keys HASH" | |
2626 | Returns a list consisting of all the keys of the named hash. (In | |
2627 | scalar context, returns the number of keys.) The keys are returned in | |
2628 | an apparently random order. The actual random order is subject to | |
2629 | change in future versions of perl, but it is guaranteed to be the same | |
2630 | order as either the \f(CW\*(C`values\*(C'\fR or \f(CW\*(C`each\*(C'\fR function produces (given | |
2631 | that the hash has not been modified). As a side effect, it resets | |
2632 | \&\s-1HASH\s0's iterator. | |
2633 | .Sp | |
2634 | Here 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 | |
2644 | or 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 | |
2652 | The returned values are copies of the original keys in the hash, so | |
2653 | modifying them will not affect the original hash. Compare \*(L"values\*(R". | |
2654 | .Sp | |
2655 | To sort a hash by value, you'll need to use a \f(CW\*(C`sort\*(C'\fR function. | |
2656 | Here'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 | |
2664 | As an lvalue \f(CW\*(C`keys\*(C'\fR allows you to increase the number of hash buckets | |
2665 | allocated for the given hash. This can gain you a measure of efficiency if | |
2666 | you know the hash is going to get big. (This is similar to pre-extending | |
2667 | an array by assigning a larger number to $#array.) If you say | |
2668 | .Sp | |
2669 | .Vb 1 | |
2670 | \& keys %hash = 200; | |
2671 | .Ve | |
2672 | .Sp | |
2673 | then \f(CW%hash\fR will have at least 200 buckets allocated for it\-\-256 of them, | |
2674 | in fact, since it rounds up to the next power of two. These | |
2675 | buckets 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. | |
2677 | You 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, | |
2679 | as trying has no effect). | |
2680 | .Sp | |
2681 | See 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" | |
2684 | Sends a signal to a list of processes. Returns the number of | |
2685 | processes successfully signaled (which is not necessarily the | |
2686 | same as the number actually killed). | |
2687 | .Sp | |
2688 | .Vb 2 | |
2689 | \& $cnt = kill 1, $child1, $child2; | |
2690 | \& kill 9, @goners; | |
2691 | .Ve | |
2692 | .Sp | |
2693 | If \s-1SIGNAL\s0 is zero, no signal is sent to the process. This is a | |
2694 | useful way to check that the process is alive and hasn't changed | |
2695 | its \s-1UID\s0. See perlport for notes on the portability of this | |
2696 | construct. | |
2697 | .Sp | |
2698 | Unlike in the shell, if \s-1SIGNAL\s0 is negative, it kills | |
2699 | process groups instead of processes. (On System V, a negative \fI\s-1PROCESS\s0\fR | |
2700 | number will also kill process groups, but that's not portable.) That | |
2701 | means you usually want to use positive not negative signals. You may also | |
2702 | use 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 | |
2709 | The \f(CW\*(C`last\*(C'\fR command is like the \f(CW\*(C`break\*(C'\fR statement in C (as used in | |
2710 | loops); it immediately exits the loop in question. If the \s-1LABEL\s0 is | |
2711 | omitted, 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 | |
2723 | a \fIgrep()\fR or \fImap()\fR operation. | |
2724 | .Sp | |
2725 | Note that a block by itself is semantically identical to a loop | |
2726 | that executes once. Thus \f(CW\*(C`last\*(C'\fR can be used to effect an early | |
2727 | exit out of such a block. | |
2728 | .Sp | |
2729 | See 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 | |
2737 | Returns a lowercased version of \s-1EXPR\s0. This is the internal function | |
2738 | implementing the \f(CW\*(C`\eL\*(C'\fR escape in double-quoted strings. Respects | |
2739 | current \s-1LC_CTYPE\s0 locale if \f(CW\*(C`use locale\*(C'\fR in force. See perllocale | |
2740 | and perlunicode for more details about locale and Unicode support. | |
2741 | .Sp | |
2742 | If \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 | |
2749 | Returns the value of \s-1EXPR\s0 with the first character lowercased. This | |
2750 | is the internal function implementing the \f(CW\*(C`\el\*(C'\fR escape in | |
2751 | double-quoted strings. Respects current \s-1LC_CTYPE\s0 locale if \f(CW\*(C`use | |
2752 | locale\*(C'\fR in force. See perllocale and perlunicode for more | |
2753 | details about locale and Unicode support. | |
2754 | .Sp | |
2755 | If \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 | |
2762 | Returns the length in characters of the value of \s-1EXPR\s0. If \s-1EXPR\s0 is | |
2763 | omitted, returns length of \f(CW$_\fR. Note that this cannot be used on | |
2764 | an entire array or hash to find out how many elements these have. | |
2765 | For 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" | |
2768 | Creates a new filename linked to the old filename. Returns true for | |
2769 | success, false otherwise. | |
2770 | .IP "listen \s-1SOCKET\s0,QUEUESIZE" 8 | |
2771 | .IX Item "listen SOCKET,QUEUESIZE" | |
2772 | Does the same thing that the listen system call does. Returns true if | |
2773 | it 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" | |
2777 | You really probably want to be using \f(CW\*(C`my\*(C'\fR instead, because \f(CW\*(C`local\*(C'\fR isn't | |
2778 | what most people think of as \*(L"local\*(R". See | |
2779 | \&\*(L"Private Variables via \fImy()\fR\*(R" in perlsub for details. | |
2780 | .Sp | |
2781 | A local modifies the listed variables to be local to the enclosing | |
2782 | block, file, or eval. If more than one value is listed, the list must | |
2783 | be placed in parentheses. See \*(L"Temporary Values via \fIlocal()\fR\*(R" in perlsub | |
2784 | for details, including issues with tied arrays and hashes. | |
2785 | .IP "localtime \s-1EXPR\s0" 8 | |
2786 | .IX Item "localtime EXPR" | |
2787 | Converts a time as returned by the time function to a 9\-element list | |
2788 | with the time analyzed for the local time zone. Typically used as | |
2789 | follows: | |
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 | |
2797 | All list elements are numeric, and come straight out of the C `struct | |
2798 | tm'. \f(CW$sec\fR, \f(CW$min\fR, and \f(CW$hour\fR are the seconds, minutes, and hours of the | |
2799 | specified time. \f(CW$mday\fR is the day of the month, and \f(CW$mon\fR is the month | |
2800 | itself, in the range \f(CW0..11\fR with 0 indicating January and 11 | |
2801 | indicating December. \f(CW$year\fR is the number of years since 1900. That | |
2802 | is, \f(CW$year\fR is \f(CW123\fR in year 2023. \f(CW$wday\fR is the day of the week, with | |
2803 | 0 indicating Sunday and 3 indicating Wednesday. \f(CW$yday\fR is the day of | |
2804 | the year, in the range \f(CW0..364\fR (or \f(CW0..365\fR in leap years.) \f(CW$isdst\fR | |
2805 | is true if the specified time occurs during daylight savings time, | |
2806 | false otherwise. | |
2807 | .Sp | |
2808 | Note that the \f(CW$year\fR element is \fInot\fR simply the last two digits of | |
2809 | the year. If you assume it is, then you create non\-Y2K\-compliant | |
2810 | programs\*(--and you wouldn't want to do that, would you? | |
2811 | .Sp | |
2812 | The proper way to get a complete 4\-digit year is simply: | |
2813 | .Sp | |
2814 | .Vb 1 | |
2815 | \& $year += 1900; | |
2816 | .Ve | |
2817 | .Sp | |
2818 | And 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 | |
2824 | If \s-1EXPR\s0 is omitted, \f(CW\*(C`localtime()\*(C'\fR uses the current time (\f(CW\*(C`localtime(time)\*(C'\fR). | |
2825 | .Sp | |
2826 | In 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 | |
2832 | This scalar value is \fBnot\fR locale dependent, see perllocale, but | |
2833 | instead 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 | |
2835 | stroke 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 | |
2838 | strings, 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 | |
2846 | Note that the \f(CW%a\fR and \f(CW%b\fR, the short forms of the day of the week | |
2847 | and the month of the year, may not necessarily be three characters wide. | |
2848 | .IP "lock \s-1THING\s0" 8 | |
2849 | .IX Item "lock THING" | |
2850 | This function places an advisory lock on a shared variable, or referenced | |
2851 | object 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 | |
2854 | by this name (before any calls to it), that function will be called | |
2855 | instead. (However, if you've said \f(CW\*(C`use threads\*(C'\fR, \fIlock()\fR is always a | |
2856 | keyword.) 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 | |
2863 | Returns the natural logarithm (base \fIe\fR) of \s-1EXPR\s0. If \s-1EXPR\s0 is omitted, | |
2864 | returns log of \f(CW$_\fR. To get the log of another base, use basic algebra: | |
2865 | The base-N log of a number is equal to the natural log of that number | |
2866 | divided 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 | |
2875 | See 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 | |
2882 | Does the same thing as the \f(CW\*(C`stat\*(C'\fR function (including setting the | |
2883 | special \f(CW\*(C`_\*(C'\fR filehandle) but stats a symbolic link instead of the file | |
2884 | the symbolic link points to. If symbolic links are unimplemented on | |
2885 | your system, a normal \f(CW\*(C`stat\*(C'\fR is done. | |
2886 | .Sp | |
2887 | If \s-1EXPR\s0 is omitted, stats \f(CW$_\fR. | |
2888 | .IP "m//" 8 | |
2889 | .IX Item "m//" | |
2890 | The 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 | |
2897 | Evaluates 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 | |
2899 | results of each such evaluation. In scalar context, returns the | |
2900 | total number of elements so generated. Evaluates \s-1BLOCK\s0 or \s-1EXPR\s0 in | |
2901 | list context, so each element of \s-1LIST\s0 may produce zero, one, or | |
2902 | more elements in the returned value. | |
2903 | .Sp | |
2904 | .Vb 1 | |
2905 | \& @chars = map(chr, @nums); | |
2906 | .Ve | |
2907 | .Sp | |
2908 | translates a list of numbers to the corresponding characters. And | |
2909 | .Sp | |
2910 | .Vb 1 | |
2911 | \& %hash = map { getkey($_) => $_ } @array; | |
2912 | .Ve | |
2913 | .Sp | |
2914 | is just a funny way to write | |
2915 | .Sp | |
2916 | .Vb 4 | |
2917 | \& %hash = (); | |
2918 | \& foreach $_ (@array) { | |
2919 | \& $hash{getkey($_)} = $_; | |
2920 | \& } | |
2921 | .Ve | |
2922 | .Sp | |
2923 | Note that \f(CW$_\fR is an alias to the list value, so it can be used to | |
2924 | modify the elements of the \s-1LIST\s0. While this is useful and supported, | |
2925 | it can cause bizarre results if the elements of \s-1LIST\s0 are not variables. | |
2926 | Using a regular \f(CW\*(C`foreach\*(C'\fR loop for this purpose would be clearer in | |
2927 | most cases. See also \*(L"grep\*(R" for an array composed of those items of | |
2928 | the 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 | |
2931 | the start of map \s-1BLOCK\s0 \s-1LIST\s0 or map \s-1EXPR\s0, \s-1LIST\s0. Because perl doesn't look | |
2932 | ahead for the closing \f(CW\*(C`}\*(C'\fR it has to take a guess at which its dealing with | |
2933 | based what it finds just after the \f(CW\*(C`{\*(C'\fR. Usually it gets it right, but if it | |
2934 | doesn't it won't realize something is wrong until it gets to the \f(CW\*(C`}\*(C'\fR and | |
2935 | encounters the missing (or unexpected) comma. The syntax error will be | |
2936 | reported close to the \f(CW\*(C`}\*(C'\fR but you'll need to change something near the \f(CW\*(C`{\*(C'\fR | |
2937 | such 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 | |
2951 | or 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 | |
2957 | and 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 | |
2964 | Creates the directory specified by \s-1FILENAME\s0, with permissions | |
2965 | specified by \s-1MASK\s0 (as modified by \f(CW\*(C`umask\*(C'\fR). If it succeeds it | |
2966 | returns true, otherwise it returns false and sets \f(CW$!\fR (errno). | |
2967 | If omitted, \s-1MASK\s0 defaults to 0777. | |
2968 | .Sp | |
2969 | In general, it is better to create directories with permissive \s-1MASK\s0, | |
2970 | and let the user modify that with their \f(CW\*(C`umask\*(C'\fR, than it is to supply | |
2971 | a restrictive \s-1MASK\s0 and give the user no way to be more permissive. | |
2972 | The exceptions to this rule are when the file or directory should be | |
2973 | kept 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 | |
2976 | Note that according to the \s-1POSIX\s0 1003.1\-1996 the \s-1FILENAME\s0 may have any | |
2977 | number of trailing slashes. Some operating and filesystems do not get | |
2978 | this right, so Perl automatically removes all trailing slashes to keep | |
2979 | everyone happy. | |
2980 | .IP "msgctl \s-1ID\s0,CMD,ARG" 8 | |
2981 | .IX Item "msgctl ID,CMD,ARG" | |
2982 | Calls 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 | |
2988 | first to get the correct constant definitions. If \s-1CMD\s0 is \f(CW\*(C`IPC_STAT\*(C'\fR, | |
2989 | then \s-1ARG\s0 must be a variable which will hold the returned \f(CW\*(C`msqid_ds\*(C'\fR | |
2990 | structure. 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" | |
2995 | Calls the System V \s-1IPC\s0 function \fImsgget\fR\|(2). Returns the message queue | |
2996 | id, 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" | |
3000 | Calls the System V \s-1IPC\s0 function msgrcv to receive a message from | |
3001 | message 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 | |
3003 | native long integer will be the first thing in \s-1VAR\s0, followed by the | |
3004 | actual message. This packing may be opened with \f(CW\*(C`unpack("l! a*")\*(C'\fR. | |
3005 | Taints the variable. Returns true if successful, or false if there is | |
3006 | an 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" | |
3010 | Calls the System V \s-1IPC\s0 function msgsnd to send the message \s-1MSG\s0 to the | |
3011 | message queue \s-1ID\s0. \s-1MSG\s0 must begin with the native long integer message | |
3012 | type, and be followed by the length of the actual message, and finally | |
3013 | the 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, | |
3015 | or false if there is an error. See also \f(CW\*(C`IPC::SysV\*(C'\fR | |
3016 | and \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 | |
3027 | A \f(CW\*(C`my\*(C'\fR declares the listed variables to be local (lexically) to the | |
3028 | enclosing block, file, or \f(CW\*(C`eval\*(C'\fR. If more than one value is listed, | |
3029 | the list must be placed in parentheses. | |
3030 | .Sp | |
3031 | The exact semantics and interface of \s-1TYPE\s0 and \s-1ATTRS\s0 are still | |
3032 | evolving. \s-1TYPE\s0 is currently bound to the use of \f(CW\*(C`fields\*(C'\fR pragma, | |
3033 | and attributes are handled using the \f(CW\*(C`attributes\*(C'\fR pragma, or starting | |
3034 | from 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, | |
3036 | attributes, 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 | |
3043 | The \f(CW\*(C`next\*(C'\fR command is like the \f(CW\*(C`continue\*(C'\fR statement in C; it starts | |
3044 | the 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 | |
3053 | Note that if there were a \f(CW\*(C`continue\*(C'\fR block on the above, it would get | |
3054 | executed even on discarded lines. If the \s-1LABEL\s0 is omitted, the command | |
3055 | refers 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 | |
3059 | a \fIgrep()\fR or \fImap()\fR operation. | |
3060 | .Sp | |
3061 | Note that a block by itself is semantically identical to a loop | |
3062 | that executes once. Thus \f(CW\*(C`next\*(C'\fR will exit such a block early. | |
3063 | .Sp | |
3064 | See 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 | |
3076 | See 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 | |
3083 | Interprets \s-1EXPR\s0 as an octal string and returns the corresponding | |
3084 | value. (If \s-1EXPR\s0 happens to start off with \f(CW\*(C`0x\*(C'\fR, interprets it as a | |
3085 | hex string. If \s-1EXPR\s0 starts off with \f(CW\*(C`0b\*(C'\fR, it is interpreted as a | |
3086 | binary string. Leading whitespace is ignored in all three cases.) | |
3087 | The following will handle decimal, binary, octal, and hex in the standard | |
3088 | Perl or C notation: | |
3089 | .Sp | |
3090 | .Vb 1 | |
3091 | \& $val = oct($val) if $val =~ /^0/; | |
3092 | .Ve | |
3093 | .Sp | |
3094 | If \s-1EXPR\s0 is omitted, uses \f(CW$_\fR. To go the other way (produce a number | |
3095 | in 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 | |
3102 | The \fIoct()\fR function is commonly used when a string such as \f(CW644\fR needs | |
3103 | to be converted into a file mode, for example. (Although perl will | |
3104 | automatically convert strings into numbers as needed, this automatic | |
3105 | conversion 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 | |
3118 | Opens 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 | |
3122 | introduction you may consider perlopentut.) | |
3123 | .Sp | |
3124 | If \s-1FILEHANDLE\s0 is an undefined lexical (\f(CW\*(C`my\*(C'\fR) variable the variable is | |
3125 | assigned 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 | |
3127 | filehandle wanted. (This is considered a symbolic reference, so \f(CW\*(C`use | |
3128 | strict 'refs'\*(C'\fR should \fInot\fR be in effect.) | |
3129 | .Sp | |
3130 | If \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 | |
3132 | declared with \f(CW\*(C`my\*(C'\fR\-\-will not work for this purpose; so if you're | |
3133 | using \f(CW\*(C`my\*(C'\fR, specify \s-1EXPR\s0 in your call to open.) | |
3134 | .Sp | |
3135 | If three or more arguments are specified then the mode of opening and | |
3136 | the file name are separate. If \s-1MODE\s0 is \f(CW'<'\fR or nothing, the file | |
3137 | is opened for input. If \s-1MODE\s0 is \f(CW'>'\fR, the file is truncated and | |
3138 | opened for output, being created if necessary. If \s-1MODE\s0 is \f(CW'>>'\fR, | |
3139 | the file is opened for appending, again being created if necessary. | |
3140 | .Sp | |
3141 | You can put a \f(CW'+'\fR in front of the \f(CW'>'\fR or \f(CW'<'\fR to | |
3142 | indicate 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 | |
3144 | either read-write mode for updating textfiles, since they have | |
3145 | variable length records. See the \fB\-i\fR switch in perlrun for a | |
3146 | better approach. The file is created with permissions of \f(CW0666\fR | |
3147 | modified by the process' \f(CW\*(C`umask\*(C'\fR value. | |
3148 | .Sp | |
3149 | These 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 | |
3152 | In the 2\-arguments (and 1\-argument) form of the call the mode and | |
3153 | filename should be concatenated (in this order), possibly separated by | |
3154 | spaces. It is possible to omit the mode in these forms if the mode is | |
3155 | \&\f(CW'<'\fR. | |
3156 | .Sp | |
3157 | If the filename begins with \f(CW'|'\fR, the filename is interpreted as a | |
3158 | command 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 | |
3160 | us. See \*(L"Using \fIopen()\fR for \s-1IPC\s0\*(R" in perlipc | |
3161 | for more examples of this. (You are not allowed to \f(CW\*(C`open\*(C'\fR to a command | |
3162 | that pipes both in \fIand\fR out, but see IPC::Open2, IPC::Open3, | |
3163 | and \*(L"Bidirectional Communication with Another Process\*(R" in perlipc | |
3164 | for alternatives.) | |
3165 | .Sp | |
3166 | For three or more arguments if \s-1MODE\s0 is \f(CW'|\-'\fR, the filename is | |
3167 | interpreted as a command to which output is to be piped, and if \s-1MODE\s0 | |
3168 | is \f(CW'\-|'\fR, the filename is interpreted as a command which pipes | |
3169 | output to us. In the 2\-arguments (and 1\-argument) form one should | |
3170 | replace dash (\f(CW'\-'\fR) with the command. | |
3171 | See \*(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 | |
3173 | out, but see IPC::Open2, IPC::Open3, and | |
3174 | \&\*(L"Bidirectional Communication\*(R" in perlipc for alternatives.) | |
3175 | .Sp | |
3176 | In 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 | |
3178 | to 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 | |
3180 | specified. Experimental \*(L"layers\*(R" may give extra \s-1LIST\s0 arguments | |
3181 | meaning. | |
3182 | .Sp | |
3183 | In the 2\-arguments (and 1\-argument) form opening \f(CW'\-'\fR opens \s-1STDIN\s0 | |
3184 | and opening \f(CW'>\-'\fR opens \s-1STDOUT\s0. | |
3185 | .Sp | |
3186 | You 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 | |
3188 | that affect how the input and output are processed (see open and | |
3189 | PerlIO for more details). For example | |
3190 | .Sp | |
3191 | .Vb 1 | |
3192 | \& open(FH, "<:utf8", "file") | |
3193 | .Ve | |
3194 | .Sp | |
3195 | will open the \s-1UTF\-8\s0 encoded file containing Unicode characters, | |
3196 | see perluniintro. (Note that if layers are specified in the | |
3197 | three-arg form then default layers set by the \f(CW\*(C`open\*(C'\fR pragma are | |
3198 | ignored.) | |
3199 | .Sp | |
3200 | Open returns nonzero upon success, the undefined value otherwise. If | |
3201 | the \f(CW\*(C`open\*(C'\fR involved a pipe, the return value happens to be the pid of | |
3202 | the subprocess. | |
3203 | .Sp | |
3204 | If you're running Perl on a system that distinguishes between text | |
3205 | files and binary files, then you should check out \*(L"binmode\*(R" for tips | |
3206 | for 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 | |
3208 | like Unix, Mac \s-1OS\s0, and Plan 9, which delimit lines with a single | |
3209 | character, and which encode that character in C as \f(CW"\en"\fR, do not | |
3210 | need \f(CW\*(C`binmode\*(C'\fR. The rest need it. | |
3211 | .Sp | |
3212 | When opening a file, it's usually a bad idea to continue normal execution | |
3213 | if 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, | |
3215 | where you want to make a nicely formatted error message (but there are | |
3216 | modules that can help with that problem)) you should always check | |
3217 | the return value from opening a file. The infrequent exception is when | |
3218 | working with an unopened filehandle is actually what you want to do. | |
3219 | .Sp | |
3220 | As a special case the 3 arg form with a read/write mode and the third | |
3221 | argument being \f(CW\*(C`undef\*(C'\fR: | |
3222 | .Sp | |
3223 | .Vb 1 | |
3224 | \& open(TMP, "+>", undef) or die ... | |
3225 | .Ve | |
3226 | .Sp | |
3227 | opens a filehandle to an anonymous temporary file. | |
3228 | .Sp | |
3229 | File 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 | |
3235 | Though 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" | |
3236 | file, 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 | |
3243 | Examples: | |
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 | |
3320 | You may also, in the Bourne shell tradition, specify an \s-1EXPR\s0 beginning | |
3321 | with \f(CW'>&'\fR, in which case the rest of the string is interpreted as the | |
3322 | name of a filehandle (or file descriptor, if numeric) to be | |
3323 | duped 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 | |
3325 | mode 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, | |
3328 | the name of a filehandle or the normal \*(L"reference to a glob\*(R". | |
3329 | .Sp | |
3330 | Here 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 | |
3369 | If you specify \f(CW'<&=N'\fR, where \f(CW\*(C`N\*(C'\fR is a number, then Perl will | |
3370 | do an equivalent of C's \f(CW\*(C`fdopen\*(C'\fR of that file descriptor; this is | |
3371 | more parsimonious of file descriptors. For example: | |
3372 | .Sp | |
3373 | .Vb 1 | |
3374 | \& open(FILEHANDLE, "<&=$fd") | |
3375 | .Ve | |
3376 | .Sp | |
3377 | or | |
3378 | .Sp | |
3379 | .Vb 1 | |
3380 | \& open(FILEHANDLE, "<&=", $fd) | |
3381 | .Ve | |
3382 | .Sp | |
3383 | Note that if Perl is using the standard C libraries' \fIfdopen()\fR then on | |
3384 | many \s-1UNIX\s0 systems, \fIfdopen()\fR is known to fail when file descriptors | |
3385 | exceed a certain value, typically 255. If you need more file | |
3386 | descriptors than that, consider rebuilding Perl to use the \f(CW\*(C`PerlIO\*(C'\fR. | |
3387 | .Sp | |
3388 | You can see whether Perl has been compiled with PerlIO or not by | |
3389 | running \f(CW\*(C`perl \-V\*(C'\fR and looking for \f(CW\*(C`useperlio=\*(C'\fR line. If \f(CW\*(C`useperlio\*(C'\fR | |
3390 | is \f(CW\*(C`define\*(C'\fR, you have PerlIO, otherwise you don't. | |
3391 | .Sp | |
3392 | If you open a pipe on the command \f(CW'\-'\fR, i.e., either \f(CW'|\-'\fR or \f(CW'\-|'\fR | |
3393 | with 2\-arguments (or 1\-argument) form of \fIopen()\fR, then | |
3394 | there is an implicit fork done, and the return value of open is the pid | |
3395 | of the child within the parent process, and \f(CW0\fR within the child | |
3396 | process. (Use \f(CW\*(C`defined($pid)\*(C'\fR to determine whether the open was successful.) | |
3397 | The filehandle behaves normally for the parent, but i/o to that | |
3398 | filehandle is piped from/to the \s-1STDOUT/STDIN\s0 of the child process. | |
3399 | In the child process the filehandle isn't opened\*(--i/o happens from/to | |
3400 | the new \s-1STDOUT\s0 or \s-1STDIN\s0. Typically this is used like the normal | |
3401 | piped open when you want to exercise more control over just how the | |
3402 | pipe command gets executed, such as when you are running setuid, and | |
3403 | don't want to have to scan shell commands for metacharacters. | |
3404 | The 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 | |
3420 | The last example in each block shows the pipe as \*(L"list form\*(R", which is | |
3421 | not yet supported on all platforms. A good rule of thumb is that if | |
3422 | your 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 | |
3425 | See \*(L"Safe Pipe Opens\*(R" in perlipc for more examples of this. | |
3426 | .Sp | |
3427 | Beginning with v5.6.0, Perl will attempt to flush all files opened for | |
3428 | output before any operation that may do a fork, but this may not be | |
3429 | supported on some platforms (see perlport). To be safe, you may need | |
3430 | to set \f(CW$|\fR ($AUTOFLUSH in English) or call the \f(CW\*(C`autoflush()\*(C'\fR method | |
3431 | of \f(CW\*(C`IO::Handle\*(C'\fR on any open handles. | |
3432 | .Sp | |
3433 | On systems that support a close-on-exec flag on files, the flag will | |
3434 | be set for the newly opened file descriptor as determined by the value | |
3435 | of $^F. See \*(L"$^F\*(R" in perlvar. | |
3436 | .Sp | |
3437 | Closing any piped filehandle causes the parent process to wait for the | |
3438 | child to finish, and returns the status value in \f(CW$?\fR. | |
3439 | .Sp | |
3440 | The filename passed to 2\-argument (or 1\-argument) form of \fIopen()\fR will | |
3441 | have leading and trailing whitespace deleted, and the normal | |
3442 | redirection characters honored. This property, known as \*(L"magic open\*(R", | |
3443 | can 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 | |
3451 | Use 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 | |
3457 | otherwise 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 | |
3465 | conscientiously choose between the \fImagic\fR and 3\-arguments form | |
3466 | of \fIopen()\fR: | |
3467 | .Sp | |
3468 | .Vb 1 | |
3469 | \& open IN, $ARGV[0]; | |
3470 | .Ve | |
3471 | .Sp | |
3472 | will allow the user to specify an argument of the form \f(CW"rsh cat file |"\fR, | |
3473 | but 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 | |
3479 | will have exactly the opposite restrictions. | |
3480 | .Sp | |
3481 | If you want a \*(L"real\*(R" C \f(CW\*(C`open\*(C'\fR (see \fIopen\fR\|(2) on your system), then you | |
3482 | should use the \f(CW\*(C`sysopen\*(C'\fR function, which involves no such magic (but | |
3483 | may use subtly different filemodes than Perl \fIopen()\fR, which is mapped | |
3484 | to C \fIfopen()\fR). This is | |
3485 | another 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 | |
3497 | Using the constructor from the \f(CW\*(C`IO::Handle\*(C'\fR package (or one of its | |
3498 | subclasses, such as \f(CW\*(C`IO::File\*(C'\fR or \f(CW\*(C`IO::Socket\*(C'\fR), you can generate anonymous | |
3499 | filehandles that have the scope of whatever variables hold references to | |
3500 | them, 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 | |
3517 | See \*(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" | |
3520 | Opens 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. | |
3522 | DIRHANDLEs 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 | |
3529 | Returns the numeric (the native 8\-bit encoding, like \s-1ASCII\s0 or \s-1EBCDIC\s0, | |
3530 | or Unicode) value of the first character of \s-1EXPR\s0. If \s-1EXPR\s0 is omitted, | |
3531 | uses \f(CW$_\fR. | |
3532 | .Sp | |
3533 | For the reverse, see \*(L"chr\*(R". | |
3534 | See 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 | |
3545 | An \f(CW\*(C`our\*(C'\fR declares the listed variables to be valid globals within | |
3546 | the enclosing block, file, or \f(CW\*(C`eval\*(C'\fR. That is, it has the same | |
3547 | scoping rules as a \*(L"my\*(R" declaration, but does not create a local | |
3548 | variable. If more than one value is listed, the list must be placed | |
3549 | in 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 | |
3551 | declared 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 | |
3553 | it differs from \*(L"use vars\*(R", which is package scoped.) | |
3554 | .Sp | |
3555 | An \f(CW\*(C`our\*(C'\fR declaration declares a global variable that will be visible | |
3556 | across its entire lexical scope, even across package boundaries. The | |
3557 | package in which the variable is entered is determined at the point | |
3558 | of the declaration, not at the point of use. This means the following | |
3559 | behavior 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 | |
3572 | Multiple \f(CW\*(C`our\*(C'\fR declarations in the same lexical scope are allowed | |
3573 | if they are in different packages. If they happened to be in the same | |
3574 | package, 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 | |
3593 | An \f(CW\*(C`our\*(C'\fR declaration may also have a list of attributes associated | |
3594 | with it. | |
3595 | .Sp | |
3596 | The exact semantics and interface of \s-1TYPE\s0 and \s-1ATTRS\s0 are still | |
3597 | evolving. \s-1TYPE\s0 is currently bound to the use of \f(CW\*(C`fields\*(C'\fR pragma, | |
3598 | and attributes are handled using the \f(CW\*(C`attributes\*(C'\fR pragma, or starting | |
3599 | from 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, | |
3601 | attributes, and Attribute::Handlers. | |
3602 | .Sp | |
3603 | The only currently recognized \f(CW\*(C`our()\*(C'\fR attribute is \f(CW\*(C`unique\*(C'\fR which | |
3604 | indicates that a single copy of the global is to be used by all | |
3605 | interpreters should the program happen to be running in a | |
3606 | multi-interpreter environment. (The default behaviour would be for | |
3607 | each 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 | |
3615 | Note that this attribute also has the effect of making the global | |
3616 | readonly when the first new interpreter is cloned (for example, | |
3617 | when the first new thread is created). | |
3618 | .Sp | |
3619 | Multi-interpreter environments can come to being either through the | |
3620 | \&\fIfork()\fR emulation on Windows platforms, or by embedding perl in a | |
3621 | multi-threaded application. The \f(CW\*(C`unique\*(C'\fR attribute does nothing in | |
3622 | all other environments. | |
3623 | .IP "pack \s-1TEMPLATE\s0,LIST" 8 | |
3624 | .IX Item "pack TEMPLATE,LIST" | |
3625 | Takes a \s-1LIST\s0 of values and converts it into a string using the rules | |
3626 | given by the \s-1TEMPLATE\s0. The resulting string is the concatenation of | |
3627 | the converted values. Typically, each converted value looks | |
3628 | like its machine-level representation. For example, on 32\-bit machines | |
3629 | a converted integer may be represented by a sequence of 4 bytes. | |
3630 | .Sp | |
3631 | The \s-1TEMPLATE\s0 is a sequence of characters that give the order and type | |
3632 | of 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 | |
3738 | The following rules apply: | |
3739 | .RS 8 | |
3740 | .IP "\(bu" 8 | |
3741 | Each letter may optionally be followed by a number giving a repeat | |
3742 | count. 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 | |
3744 | many values from the \s-1LIST\s0. A \f(CW\*(C`*\*(C'\fR for the repeat count means to use | |
3745 | however 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 | |
3746 | equivalent to \f(CW0\fR, and \f(CW\*(C`u\*(C'\fR, where it is equivalent to 1 (or 45, what | |
3747 | is the same). A numeric repeat count may optionally be enclosed in | |
3748 | brackets, as in \f(CW\*(C`pack 'C[80]', @arr\*(C'\fR. | |
3749 | .Sp | |
3750 | One can replace the numeric repeat count by a template enclosed in brackets; | |
3751 | then the packed length of this template in bytes is used as a count. | |
3752 | For example, \f(CW\*(C`x[L]\*(C'\fR skips a long (it skips the number of bytes in a long); | |
3753 | the template \f(CW\*(C`$t X[$t] $t\*(C'\fR \fIunpack()\fRs twice what \f(CW$t\fR unpacks. | |
3754 | If the template in brackets contains alignment commands (such as \f(CW\*(C`x![d]\*(C'\fR), | |
3755 | its packed length is calculated as if the start of the template has the maximal | |
3756 | possible alignment. | |
3757 | .Sp | |
3758 | When used with \f(CW\*(C`Z\*(C'\fR, \f(CW\*(C`*\*(C'\fR results in the addition of a trailing null | |
3759 | byte (so the packed result will be one longer than the byte \f(CW\*(C`length\*(C'\fR | |
3760 | of the item). | |
3761 | .Sp | |
3762 | The repeat count for \f(CW\*(C`u\*(C'\fR is interpreted as the maximal number of bytes | |
3763 | to encode per line of output, with 0 and 1 replaced by 45. | |
3764 | .IP "\(bu" 8 | |
3765 | The \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 | |
3766 | string of length count, padding with nulls or spaces as necessary. When | |
3767 | unpacking, \f(CW\*(C`A\*(C'\fR strips trailing spaces and nulls, \f(CW\*(C`Z\*(C'\fR strips everything | |
3768 | after 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 | |
3771 | If the value-to-pack is too long, it is truncated. If too long and an | |
3772 | explicit count is provided, \f(CW\*(C`Z\*(C'\fR packs only \f(CW\*(C`$count\-1\*(C'\fR bytes, followed | |
3773 | by a null byte. Thus \f(CW\*(C`Z\*(C'\fR always packs a trailing null byte under | |
3774 | all circumstances. | |
3775 | .IP "\(bu" 8 | |
3776 | Likewise, the \f(CW\*(C`b\*(C'\fR and \f(CW\*(C`B\*(C'\fR fields pack a string that many bits long. | |
3777 | Each byte of the input field of \fIpack()\fR generates 1 bit of the result. | |
3778 | Each result bit is based on the least-significant bit of the corresponding | |
3779 | input 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 | |
3782 | Starting from the beginning of the input string of \fIpack()\fR, each 8\-tuple | |
3783 | of bytes is converted to 1 byte of output. With format \f(CW\*(C`b\*(C'\fR | |
3784 | the first byte of the 8\-tuple determines the least-significant bit of a | |
3785 | byte, and with format \f(CW\*(C`B\*(C'\fR it determines the most-significant bit of | |
3786 | a byte. | |
3787 | .Sp | |
3788 | If the length of the input string is not exactly divisible by 8, the | |
3789 | remainder is packed as if the input string were padded by null bytes | |
3790 | at the end. Similarly, during \fIunpack()\fRing the \*(L"extra\*(R" bits are ignored. | |
3791 | .Sp | |
3792 | If the input string of \fIpack()\fR is longer than needed, extra bytes are ignored. | |
3793 | A \f(CW\*(C`*\*(C'\fR for the repeat count of \fIpack()\fR means to use all the bytes of | |
3794 | the input field. On \fIunpack()\fRing the bits are converted to a string | |
3795 | of \f(CW"0"\fRs and \f(CW"1"\fRs. | |
3796 | .IP "\(bu" 8 | |
3797 | The \f(CW\*(C`h\*(C'\fR and \f(CW\*(C`H\*(C'\fR fields pack a string that many nybbles (4\-bit groups, | |
3798 | representable as hexadecimal digits, 0\-9a\-f) long. | |
3799 | .Sp | |
3800 | Each byte of the input field of \fIpack()\fR generates 4 bits of the result. | |
3801 | For non-alphabetical bytes the result is based on the 4 least-significant | |
3802 | bits of the input byte, i.e., on \f(CW\*(C`ord($byte)%16\*(C'\fR. In particular, | |
3803 | bytes \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 | |
3805 | is 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 | |
3809 | Starting from the beginning of the input string of \fIpack()\fR, each pair | |
3810 | of bytes is converted to 1 byte of output. With format \f(CW\*(C`h\*(C'\fR the | |
3811 | first byte of the pair determines the least-significant nybble of the | |
3812 | output byte, and with format \f(CW\*(C`H\*(C'\fR it determines the most-significant | |
3813 | nybble. | |
3814 | .Sp | |
3815 | If the length of the input string is not even, it behaves as if padded | |
3816 | by a null byte at the end. Similarly, during \fIunpack()\fRing the \*(L"extra\*(R" | |
3817 | nybbles are ignored. | |
3818 | .Sp | |
3819 | If the input string of \fIpack()\fR is longer than needed, extra bytes are ignored. | |
3820 | A \f(CW\*(C`*\*(C'\fR for the repeat count of \fIpack()\fR means to use all the bytes of | |
3821 | the input field. On \fIunpack()\fRing the bits are converted to a string | |
3822 | of hexadecimal digits. | |
3823 | .IP "\(bu" 8 | |
3824 | The \f(CW\*(C`p\*(C'\fR type packs a pointer to a null-terminated string. You are | |
3825 | responsible for ensuring the string is not a temporary value (which can | |
3826 | potentially get deallocated before you get around to using the packed result). | |
3827 | The \f(CW\*(C`P\*(C'\fR type packs a pointer to a structure of the size indicated by the | |
3828 | length. 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 | |
3831 | The \f(CW\*(C`/\*(C'\fR template character allows packing and unpacking of strings where | |
3832 | the packed structure contains a byte count followed by the string itself. | |
3833 | You write \fIlength-item\fR\f(CW\*(C`/\*(C'\fR\fIstring-item\fR. | |
3834 | .Sp | |
3835 | The \fIlength-item\fR can be any \f(CW\*(C`pack\*(C'\fR template letter, and describes | |
3836 | how the length value is packed. The ones likely to be of most use are | |
3837 | integer-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 | |
3840 | The \fIstring-item\fR must, at present, be \f(CW"A*"\fR, \f(CW"a*"\fR or \f(CW"Z*"\fR. | |
3841 | For \f(CW\*(C`unpack\*(C'\fR the length of the string is obtained from the \fIlength-item\fR, | |
3842 | but 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 | |
3850 | The \fIlength-item\fR is not returned explicitly from \f(CW\*(C`unpack\*(C'\fR. | |
3851 | .Sp | |
3852 | Adding a count to the \fIlength-item\fR letter is unlikely to do anything | |
3853 | useful, 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, | |
3855 | which Perl does not regard as legal in numeric strings. | |
3856 | .IP "\(bu" 8 | |
3857 | The 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 | |
3858 | immediately followed by a \f(CW\*(C`!\*(C'\fR suffix to signify native shorts or | |
3859 | longs\*(--as you can see from above for example a bare \f(CW\*(C`l\*(C'\fR does mean | |
3860 | exactly 32 bits, the native \f(CW\*(C`long\*(C'\fR (as seen by the local C compiler) | |
3861 | may be larger. This is an issue mainly in 64\-bit platforms. You can | |
3862 | see 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; | |
3870 | they are identical to \f(CW\*(C`i\*(C'\fR and \f(CW\*(C`I\*(C'\fR. | |
3871 | .Sp | |
3872 | The actual sizes (in bytes) of native shorts, ints, longs, and long | |
3873 | longs on the platform where Perl was built are also available via | |
3874 | Config: | |
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 | |
3885 | not support long longs.) | |
3886 | .IP "\(bu" 8 | |
3887 | The 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 | |
3888 | are inherently non-portable between processors and operating systems | |
3889 | because they obey the native byteorder and endianness. For example a | |
3890 | 4\-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 | |
3898 | Basically, the Intel and \s-1VAX\s0 CPUs are little\-endian, while everybody | |
3899 | else, for example Motorola m68k/88k, \s-1PPC\s0, Sparc, \s-1HP\s0 \s-1PA\s0, Power, and | |
3900 | Cray are big\-endian. Alpha and \s-1MIPS\s0 can be either: Digital/Compaq | |
3901 | used/uses them in little-endian mode; SGI/Cray uses them in big-endian | |
3902 | mode. | |
3903 | .Sp | |
3904 | The names `big\-endian' and `little\-endian' are comic references to | |
3905 | the classic \*(L"Gulliver's Travels\*(R" (via the paper \*(L"On Holy Wars and a | |
3906 | Plea for Peace\*(R" by Danny Cohen, \s-1USC/ISI\s0 \s-1IEN\s0 137, April 1, 1980) and | |
3907 | the egg-eating habits of the Lilliputians. | |
3908 | .Sp | |
3909 | Some 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 | |
3916 | You 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 | |
3923 | The byteorder on the platform where Perl was built is also available | |
3924 | via Config: | |
3925 | .Sp | |
3926 | .Vb 2 | |
3927 | \& use Config; | |
3928 | \& print $Config{byteorder}, "\en"; | |
3929 | .Ve | |
3930 | .Sp | |
3931 | Byteorders \f(CW'1234'\fR and \f(CW'12345678'\fR are little\-endian, \f(CW'4321'\fR | |
3932 | and \f(CW'87654321'\fR are big\-endian. | |
3933 | .Sp | |
3934 | If 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. | |
3936 | See also perlport. | |
3937 | .IP "\(bu" 8 | |
3938 | Real numbers (floats and doubles) are in the native machine format only; | |
3939 | due to the multiplicity of floating formats around, and the lack of a | |
3940 | standard \*(L"network\*(R" representation, no facility for interchange has been | |
3941 | made. This means that packed floating point data written on one machine | |
3942 | may not be readable on another \- even if both use \s-1IEEE\s0 floating point | |
3943 | arithmetic (as the endian-ness of the memory representation is not part | |
3944 | of the \s-1IEEE\s0 spec). See also perlport. | |
3945 | .Sp | |
3946 | Note that Perl uses doubles internally for all numeric calculation, and | |
3947 | converting from double into float and thence back to double again will | |
3948 | lose precision (i.e., \f(CW\*(C`unpack("f", pack("f", $foo)\*(C'\fR) will not in general | |
3949 | equal \f(CW$foo\fR). | |
3950 | .IP "\(bu" 8 | |
3951 | If the pattern begins with a \f(CW\*(C`U\*(C'\fR, the resulting string will be treated | |
3952 | as Unicode\-encoded. You can force \s-1UTF8\s0 encoding on in a string with an | |
3953 | initial \f(CW\*(C`U0\*(C'\fR, and the bytes that follow will be interpreted as Unicode | |
3954 | characters. If you don't want this to happen, you can begin your pattern | |
3955 | with \f(CW\*(C`C0\*(C'\fR (or anything else) to force Perl not to \s-1UTF8\s0 encode your | |
3956 | string, and then follow this with a \f(CW\*(C`U*\*(C'\fR somewhere in your pattern. | |
3957 | .IP "\(bu" 8 | |
3958 | You must yourself do any alignment or padding by inserting for example | |
3959 | enough \f(CW'x'\fRes while packing. There is no way to \fIpack()\fR and \fIunpack()\fR | |
3960 | could 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 | |
3962 | sequences of bytes. | |
3963 | .IP "\(bu" 8 | |
3964 | A ()\-group is a sub-TEMPLATE enclosed in parentheses. A group may | |
3965 | take a repeat count, both as postfix, and via the \f(CW\*(C`/\*(C'\fR template | |
3966 | character. | |
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 | |
3969 | alignment commands: they jump forward/back to the closest position | |
3970 | aligned 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 | |
3972 | use the template \f(CW\*(C`C x![d] d C[2]\*(C'\fR; this assumes that doubles must be | |
3973 | aligned on the double's size. | |
3974 | .Sp | |
3975 | For alignment commands \f(CW\*(C`count\*(C'\fR of 0 is equivalent to \f(CW\*(C`count\*(C'\fR of 1; | |
3976 | both result in no\-ops. | |
3977 | .IP "\(bu" 8 | |
3978 | A comment in a \s-1TEMPLATE\s0 starts with \f(CW\*(C`#\*(C'\fR and goes to the end of line. | |
3979 | .IP "\(bu" 8 | |
3980 | If \s-1TEMPLATE\s0 requires more arguments to \fIpack()\fR than actually given, \fIpack()\fR | |
3981 | assumes additional \f(CW""\fR arguments. If \s-1TEMPLATE\s0 requires less arguments | |
3982 | to \fIpack()\fR than actually given, extra arguments are ignored. | |
3983 | .RE | |
3984 | .RS 8 | |
3985 | .Sp | |
3986 | Examples: | |
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 | |
4060 | The 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 | |
4068 | Declares the compilation unit as being in the given namespace. The scope | |
4069 | of the package declaration is from the declaration itself through the end | |
4070 | of the enclosing block, file, or eval (the same as the \f(CW\*(C`my\*(C'\fR operator). | |
4071 | All further unqualified dynamic identifiers will be in this namespace. | |
4072 | A package statement affects only dynamic variables\*(--including those | |
4073 | you've used \f(CW\*(C`local\*(C'\fR on\*(--but \fInot\fR lexical variables, which are created | |
4074 | with \f(CW\*(C`my\*(C'\fR. Typically it would be the first declaration in a file to | |
4075 | be included by the \f(CW\*(C`require\*(C'\fR or \f(CW\*(C`use\*(C'\fR operator. You can switch into a | |
4076 | package in more than one place; it merely influences which symbol table | |
4077 | is used by the compiler for the rest of that block. You can refer to | |
4078 | variables and filehandles in other packages by prefixing the identifier | |
4079 | with the package name and a double colon: \f(CW$Package::Variable\fR. | |
4080 | If 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, | |
4082 | still seen in older code). | |
4083 | .Sp | |
4084 | If \s-1NAMESPACE\s0 is omitted, then there is no current package, and all | |
4085 | identifiers must be fully qualified or lexicals. However, you are | |
4086 | strongly advised not to make use of this feature. Its use can cause | |
4087 | unexpected behaviour, even crashing some versions of Perl. It is | |
4088 | deprecated, and will be removed from a future release. | |
4089 | .Sp | |
4090 | See \*(L"Packages\*(R" in perlmod for more information about packages, modules, | |
4091 | and classes. See perlsub for other scoping issues. | |
4092 | .IP "pipe \s-1READHANDLE\s0,WRITEHANDLE" 8 | |
4093 | .IX Item "pipe READHANDLE,WRITEHANDLE" | |
4094 | Opens a pair of connected pipes like the corresponding system call. | |
4095 | Note that if you set up a loop of piped processes, deadlock can occur | |
4096 | unless 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 | |
4098 | after each command, depending on the application. | |
4099 | .Sp | |
4100 | See IPC::Open2, IPC::Open3, and \*(L"Bidirectional Communication\*(R" in perlipc | |
4101 | for examples of such things. | |
4102 | .Sp | |
4103 | On systems that support a close-on-exec flag on files, the flag will be set | |
4104 | for the newly opened file descriptors as determined by the value of $^F. | |
4105 | See \*(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 | |
4112 | Pops and returns the last value of the array, shortening the array by | |
4113 | one element. Has an effect similar to | |
4114 | .Sp | |
4115 | .Vb 1 | |
4116 | \& $ARRAY[$#ARRAY--] | |
4117 | .Ve | |
4118 | .Sp | |
4119 | If 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 | |
4121 | omitted, pops the \f(CW@ARGV\fR array in the main program, and the \f(CW@_\fR | |
4122 | array 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 | |
4129 | Returns the offset of where the last \f(CW\*(C`m//g\*(C'\fR search left off for the variable | |
4130 | in question (\f(CW$_\fR is used when the variable is not specified). May be | |
4131 | modified to change that offset. Such modification will also influence | |
4132 | the \f(CW\*(C`\eG\*(C'\fR zero-width assertion in regular expressions. See perlre and | |
4133 | perlop. | |
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 | |
4142 | Prints 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 | |
4144 | contains the name of or a reference to the filehandle, thus introducing | |
4145 | one level of indirection. (\s-1NOTE:\s0 If \s-1FILEHANDLE\s0 is a variable and | |
4146 | the next token is a term, it may be misinterpreted as an operator | |
4147 | unless you interpose a \f(CW\*(C`+\*(C'\fR or put parentheses around the arguments.) | |
4148 | If \s-1FILEHANDLE\s0 is omitted, prints by default to standard output (or | |
4149 | to the last selected output channel\*(--see \*(L"select\*(R"). If \s-1LIST\s0 is | |
4150 | also omitted, prints \f(CW$_\fR to the currently selected output channel. | |
4151 | To set the default output channel to something other than \s-1STDOUT\s0 | |
4152 | use the select operation. The current value of \f(CW$,\fR (if any) is | |
4153 | printed between each \s-1LIST\s0 item. The current value of \f(CW\*(C`$\e\*(C'\fR (if | |
4154 | any) is printed after the entire \s-1LIST\s0 has been printed. Because | |
4155 | print takes a \s-1LIST\s0, anything in the \s-1LIST\s0 is evaluated in list | |
4156 | context, and any subroutine that you call will have one or more of | |
4157 | its expressions evaluated in list context. Also be careful not to | |
4158 | follow the print keyword with a left parenthesis unless you want | |
4159 | the corresponding right parenthesis to terminate the arguments to | |
4160 | the print\*(--interpose a \f(CW\*(C`+\*(C'\fR or put parentheses around all the | |
4161 | arguments. | |
4162 | .Sp | |
4163 | Note that if you're storing \s-1FILEHANDLES\s0 in an array or other expression, | |
4164 | you 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 | |
4176 | Equivalent 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 | |
4178 | of the list will be interpreted as the \f(CW\*(C`printf\*(C'\fR format. See \f(CW\*(C`sprintf\*(C'\fR | |
4179 | for an explanation of the format argument. If \f(CW\*(C`use locale\*(C'\fR is in effect, | |
4180 | the character used for the decimal point in formatted real numbers is | |
4181 | affected by the \s-1LC_NUMERIC\s0 locale. See perllocale. | |
4182 | .Sp | |
4183 | Don'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 | |
4185 | error prone. | |
4186 | .IP "prototype \s-1FUNCTION\s0" 8 | |
4187 | .IX Item "prototype FUNCTION" | |
4188 | Returns the prototype of a function as a string (or \f(CW\*(C`undef\*(C'\fR if the | |
4189 | function has no prototype). \s-1FUNCTION\s0 is a reference to, or the name of, | |
4190 | the function whose prototype you want to retrieve. | |
4191 | .Sp | |
4192 | If \s-1FUNCTION\s0 is a string starting with \f(CW\*(C`CORE::\*(C'\fR, the rest is taken as a | |
4193 | name 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 | |
4196 | like a Perl function. Otherwise, the string describing the equivalent | |
4197 | prototype is returned. | |
4198 | .IP "push \s-1ARRAY\s0,LIST" 8 | |
4199 | .IX Item "push ARRAY,LIST" | |
4200 | Treats \s-1ARRAY\s0 as a stack, and pushes the values of \s-1LIST\s0 | |
4201 | onto 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 | |
4210 | but 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 | |
4223 | Generalized 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 | |
4230 | Returns the value of \s-1EXPR\s0 with all non\-\*(L"word\*(R" | |
4231 | characters 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 | |
4233 | returned string, regardless of any locale settings.) | |
4234 | This is the internal function implementing | |
4235 | the \f(CW\*(C`\eQ\*(C'\fR escape in double-quoted strings. | |
4236 | .Sp | |
4237 | If \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 | |
4244 | Returns a random fractional number greater than or equal to \f(CW0\fR and less | |
4245 | than the value of \s-1EXPR\s0. (\s-1EXPR\s0 should be positive.) If \s-1EXPR\s0 is | |
4246 | omitted, the value \f(CW1\fR is used. Currently \s-1EXPR\s0 with the value \f(CW0\fR is | |
4247 | also special-cased as \f(CW1\fR \- this has not been documented before perl 5.8.0 | |
4248 | and 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 | |
4251 | Apply \f(CW\*(C`int()\*(C'\fR to the value returned by \f(CW\*(C`rand()\*(C'\fR if you want random | |
4252 | integers instead of random fractional numbers. For example, | |
4253 | .Sp | |
4254 | .Vb 1 | |
4255 | \& int(rand(10)) | |
4256 | .Ve | |
4257 | .Sp | |
4258 | returns 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 | |
4261 | large or too small, then your version of Perl was probably compiled | |
4262 | with 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 | |
4269 | Attempts to read \s-1LENGTH\s0 \fIcharacters\fR of data into variable \s-1SCALAR\s0 | |
4270 | from the specified \s-1FILEHANDLE\s0. Returns the number of characters | |
4271 | actually 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 | |
4273 | needs growing, the new bytes will be zero bytes. An \s-1OFFSET\s0 may be | |
4274 | specified to place the read data into some other place in \s-1SCALAR\s0 than | |
4275 | the beginning. The call is actually implemented in terms of either | |
4276 | Perl's or system's \fIfread()\fR call. To get a true \fIread\fR\|(2) system call, | |
4277 | see \f(CW\*(C`sysread\*(C'\fR. | |
4278 | .Sp | |
4279 | Note the \fIcharacters\fR: depending on the status of the filehandle, | |
4280 | either (8\-bit) bytes or characters are read. By default all | |
4281 | filehandles operate on bytes, but for example if the filehandle has | |
4282 | been opened with the \f(CW\*(C`:utf8\*(C'\fR I/O layer (see \*(L"open\*(R", and the \f(CW\*(C`open\*(C'\fR | |
4283 | pragma, open), the I/O will operate on characters, not bytes. | |
4284 | .IP "readdir \s-1DIRHANDLE\s0" 8 | |
4285 | .IX Item "readdir DIRHANDLE" | |
4286 | Returns the next directory entry for a directory opened by \f(CW\*(C`opendir\*(C'\fR. | |
4287 | If used in list context, returns all the rest of the entries in the | |
4288 | directory. If there are no more entries, returns an undefined value in | |
4289 | scalar context or a null list in list context. | |
4290 | .Sp | |
4291 | If you're planning to filetest the return values out of a \f(CW\*(C`readdir\*(C'\fR, you'd | |
4292 | better 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" | |
4302 | Reads from the filehandle whose typeglob is contained in \s-1EXPR\s0. In scalar | |
4303 | context, each call reads and returns the next line, until end-of-file is | |
4304 | reached, whereupon the subsequent call returns undef. In list context, | |
4305 | reads until end-of-file is reached and returns a list of lines. Note that | |
4306 | the notion of \*(L"line\*(R" used here is however you may have defined it | |
4307 | with \f(CW$/\fR or \f(CW$INPUT_RECORD_SEPARATOR\fR). See \*(L"$/\*(R" in perlvar. | |
4308 | .Sp | |
4309 | When \f(CW$/\fR is set to \f(CW\*(C`undef\*(C'\fR, when \fIreadline()\fR is in scalar | |
4310 | context (i.e. file slurp mode), and when an empty file is read, it | |
4311 | returns \f(CW''\fR the first time, followed by \f(CW\*(C`undef\*(C'\fR subsequently. | |
4312 | .Sp | |
4313 | This is the internal function implementing the \f(CW\*(C`<EXPR>\*(C'\fR | |
4314 | operator, but you can use it directly. The \f(CW\*(C`<EXPR>\*(C'\fR | |
4315 | operator 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 | |
4327 | Returns the value of a symbolic link, if symbolic links are | |
4328 | implemented. If not, gives a fatal error. If there is some system | |
4329 | error, returns the undefined value and sets \f(CW$!\fR (errno). If \s-1EXPR\s0 is | |
4330 | omitted, 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. | |
4334 | The collected standard output of the command is returned. | |
4335 | In scalar context, it comes back as a single (potentially | |
4336 | multi\-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). | |
4338 | This is the internal function implementing the \f(CW\*(C`qx/EXPR/\*(C'\fR | |
4339 | operator, but you can use it directly. The \f(CW\*(C`qx/EXPR/\*(C'\fR | |
4340 | operator 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" | |
4343 | Receives a message on a socket. Attempts to receive \s-1LENGTH\s0 characters | |
4344 | of 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 | |
4346 | same flags as the system call of the same name. Returns the address | |
4347 | of the sender if \s-1SOCKET\s0's protocol supports this; returns an empty | |
4348 | string otherwise. If there's an error, returns the undefined value. | |
4349 | This call is actually implemented in terms of \fIrecvfrom\fR\|(2) system call. | |
4350 | See \*(L"\s-1UDP:\s0 Message Passing\*(R" in perlipc for examples. | |
4351 | .Sp | |
4352 | Note the \fIcharacters\fR: depending on the status of the socket, either | |
4353 | (8\-bit) bytes or characters are received. By default all sockets | |
4354 | operate 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 | |
4356 | pragma, 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 | |
4363 | The \f(CW\*(C`redo\*(C'\fR command restarts the loop block without evaluating the | |
4364 | conditional again. The \f(CW\*(C`continue\*(C'\fR block, if any, is not executed. If | |
4365 | the \s-1LABEL\s0 is omitted, the command refers to the innermost enclosing | |
4366 | loop. This command is normally used by programs that want to lie to | |
4367 | themselves 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 | |
4390 | a \fIgrep()\fR or \fImap()\fR operation. | |
4391 | .Sp | |
4392 | Note that a block by itself is semantically identical to a loop | |
4393 | that executes once. Thus \f(CW\*(C`redo\*(C'\fR inside such a block will effectively | |
4394 | turn it into a looping construct. | |
4395 | .Sp | |
4396 | See 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 | |
4404 | Returns a true value if \s-1EXPR\s0 is a reference, false otherwise. If \s-1EXPR\s0 | |
4405 | is not specified, \f(CW$_\fR will be used. The value returned depends on the | |
4406 | type of thing the reference is a reference to. | |
4407 | Builtin 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 | |
4419 | If the referenced object has been blessed into a package, then that package | |
4420 | name 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 | |
4434 | See also perlref. | |
4435 | .IP "rename \s-1OLDNAME\s0,NEWNAME" 8 | |
4436 | .IX Item "rename OLDNAME,NEWNAME" | |
4437 | Changes the name of a file; an existing file \s-1NEWNAME\s0 will be | |
4438 | clobbered. Returns true for success, false otherwise. | |
4439 | .Sp | |
4440 | Behavior of this function varies wildly depending on your system | |
4441 | implementation. For example, it will usually not work across file system | |
4442 | boundaries, even though the system \fImv\fR command sometimes compensates | |
4443 | for this. Other restrictions include whether it works on directories, | |
4444 | open 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 | |
4454 | Demands a version of Perl specified by \s-1VERSION\s0, or demands some semantics | |
4455 | specified 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 | |
4458 | compared to \f(CW$]\fR, or a literal of the form v5.6.1, which will be compared | |
4459 | to \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. | |
4461 | Compare with \*(L"use\*(R", which can do a similar check at compile time. | |
4462 | .Sp | |
4463 | Specifying \s-1VERSION\s0 as a literal of the form v5.6.1 should generally be | |
4464 | avoided, because it leads to misleading error messages under earlier | |
4465 | versions of Perl which do not support this syntax. The equivalent numeric | |
4466 | version 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 | |
4474 | Otherwise, demands that a library file be included if it hasn't already | |
4475 | been included. The file is included via the do-FILE mechanism, which is | |
4476 | essentially just a variety of \f(CW\*(C`eval\*(C'\fR. Has semantics similar to the following | |
4477 | subroutine: | |
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 | |
4502 | Note that the file will not be included twice under the same specified | |
4503 | name. The file must return true as the last statement to indicate | |
4504 | successful execution of any initialization code, so it's customary to | |
4505 | end such a file with \f(CW\*(C`1;\*(C'\fR unless you're sure it'll return true | |
4506 | otherwise. But it's better just to put the \f(CW\*(C`1;\*(C'\fR, in case you add more | |
4507 | statements. | |
4508 | .Sp | |
4509 | If \s-1EXPR\s0 is a bareword, the require assumes a "\fI.pm\fR\*(L" extension and | |
4510 | replaces \*(R"\fI::\fR\*(L" with \*(R"\fI/\fR" in the filename for you, | |
4511 | to make it easy to load standard modules. This form of loading of | |
4512 | modules does not risk altering your namespace. | |
4513 | .Sp | |
4514 | In other words, if you try this: | |
4515 | .Sp | |
4516 | .Vb 1 | |
4517 | \& require Foo::Bar; # a splendid bareword | |
4518 | .Ve | |
4519 | .Sp | |
4520 | The require function will actually look for the "\fIFoo/Bar.pm\fR" file in the | |
4521 | directories specified in the \f(CW@INC\fR array. | |
4522 | .Sp | |
4523 | But 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 | |
4532 | The require function will look for the "\fIFoo::Bar\fR\*(L" file in the \f(CW@INC\fR array and | |
4533 | will 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 | |
4539 | You can also insert hooks into the import facility, by putting directly | |
4540 | Perl code into the \f(CW@INC\fR array. There are three forms of hooks: subroutine | |
4541 | references, array references and blessed objects. | |
4542 | .Sp | |
4543 | Subroutine references are the simplest case. When the inclusion system | |
4544 | walks through \f(CW@INC\fR and encounters a subroutine, this subroutine gets | |
4545 | called with two parameters, the first being a reference to itself, and the | |
4546 | second the name of the file to be included (e.g. "\fIFoo/Bar.pm\fR"). The | |
4547 | subroutine should return \f(CW\*(C`undef\*(C'\fR or a filehandle, from which the file to | |
4548 | include will be read. If \f(CW\*(C`undef\*(C'\fR is returned, \f(CW\*(C`require\*(C'\fR will look at | |
4549 | the remaining elements of \f(CW@INC\fR. | |
4550 | .Sp | |
4551 | If the hook is an array reference, its first element must be a subroutine | |
4552 | reference. This subroutine is called as above, but the first parameter is | |
4553 | the array reference. This enables to pass indirectly some arguments to | |
4554 | the subroutine. | |
4555 | .Sp | |
4556 | In 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 | |
4566 | or: | |
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 | |
4578 | If the hook is an object, it must provide an \s-1INC\s0 method, that will be | |
4579 | called as above, the first parameter being the object itself. (Note that | |
4580 | you 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 | |
4598 | Note that these hooks are also permitted to set the \f(CW%INC\fR entry | |
4599 | corresponding to the files they have loaded. See \*(L"%INC\*(R" in perlvar. | |
4600 | .Sp | |
4601 | For 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 | |
4608 | Generally used in a \f(CW\*(C`continue\*(C'\fR block at the end of a loop to clear | |
4609 | variables and reset \f(CW\*(C`??\*(C'\fR searches so that they work again. The | |
4610 | expression is interpreted as a list of single characters (hyphens | |
4611 | allowed for ranges). All variables and arrays beginning with one of | |
4612 | those letters are reset to their pristine state. If the expression is | |
4613 | omitted, one-match searches (\f(CW\*(C`?pattern?\*(C'\fR) are reset to match again. Resets | |
4614 | only variables or searches in the current package. Always returns | |
4615 | 1. 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 | |
4623 | Resetting \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 | |
4625 | variables\*(--lexical variables are unaffected, but they clean themselves | |
4626 | up on scope exit anyway, so you'll probably want to use them instead. | |
4627 | See \*(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 | |
4634 | Returns from a subroutine, \f(CW\*(C`eval\*(C'\fR, or \f(CW\*(C`do FILE\*(C'\fR with the value | |
4635 | given in \s-1EXPR\s0. Evaluation of \s-1EXPR\s0 may be in list, scalar, or void | |
4636 | context, depending on how the return value will be used, and the context | |
4637 | may vary from one execution to the next (see \f(CW\*(C`wantarray\*(C'\fR). If no \s-1EXPR\s0 | |
4638 | is given, returns an empty list in list context, the undefined value in | |
4639 | scalar 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, | |
4642 | or do \s-1FILE\s0 will automatically return the value of the last expression | |
4643 | evaluated.) | |
4644 | .IP "reverse \s-1LIST\s0" 8 | |
4645 | .IX Item "reverse LIST" | |
4646 | In list context, returns a list value consisting of the elements | |
4647 | of \s-1LIST\s0 in the opposite order. In scalar context, concatenates the | |
4648 | elements of \s-1LIST\s0 and returns a string value with all characters | |
4649 | in 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 | |
4660 | This operator is also handy for inverting a hash, although there are some | |
4661 | caveats. If a value is duplicated in the original hash, only one of those | |
4662 | can be represented as a key in the inverted hash. Also, this has to | |
4663 | unwind one hash and build a whole new one, which may take some time | |
4664 | on 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" | |
4671 | Sets 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 | |
4679 | Works just like \fIindex()\fR except that it returns the position of the \s-1LAST\s0 | |
4680 | occurrence of \s-1SUBSTR\s0 in \s-1STR\s0. If \s-1POSITION\s0 is specified, returns the | |
4681 | last 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 | |
4688 | Deletes the directory specified by \s-1FILENAME\s0 if that directory is empty. If it | |
4689 | succeeds 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///" | |
4693 | The substitution operator. See perlop. | |
4694 | .IP "scalar \s-1EXPR\s0" 8 | |
4695 | .IX Item "scalar EXPR" | |
4696 | Forces \s-1EXPR\s0 to be interpreted in scalar context and returns the value | |
4697 | of \s-1EXPR\s0. | |
4698 | .Sp | |
4699 | .Vb 1 | |
4700 | \& @counts = ( scalar @a, scalar @b, scalar @c ); | |
4701 | .Ve | |
4702 | .Sp | |
4703 | There is no equivalent operator to force an expression to | |
4704 | be interpolated in list context because in practice, this is never | |
4705 | needed. If you really wanted to do so, however, you could use | |
4706 | the construction \f(CW\*(C`@{[ (some expression) ]}\*(C'\fR, but usually a simple | |
4707 | \&\f(CW\*(C`(some expression)\*(C'\fR suffices. | |
4708 | .Sp | |
4709 | Because \f(CW\*(C`scalar\*(C'\fR is unary operator, if you accidentally use for \s-1EXPR\s0 a | |
4710 | parenthesized list, this behaves as a scalar comma expression, evaluating | |
4711 | all but the last element in void context and returning the final element | |
4712 | evaluated in scalar context. This is seldom what you want. | |
4713 | .Sp | |
4714 | The following single statement: | |
4715 | .Sp | |
4716 | .Vb 1 | |
4717 | \& print uc(scalar(&foo,$bar)),$baz; | |
4718 | .Ve | |
4719 | .Sp | |
4720 | is the moral equivalent of these two: | |
4721 | .Sp | |
4722 | .Vb 2 | |
4723 | \& &foo; | |
4724 | \& print(uc($bar),$baz); | |
4725 | .Ve | |
4726 | .Sp | |
4727 | See 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" | |
4730 | Sets \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 | |
4732 | filehandle. 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 | |
4735 | negative). 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 | |
4737 | of the file) from the Fcntl module. Returns \f(CW1\fR upon success, \f(CW0\fR | |
4738 | otherwise. | |
4739 | .Sp | |
4740 | Note the \fIin bytes\fR: even if the filehandle has been set to | |
4741 | operate on characters (for example by using the \f(CW\*(C`:utf8\*(C'\fR open | |
4742 | layer), \fItell()\fR will return byte offsets, not character offsets | |
4743 | (because implementing that would render \fIseek()\fR and \fItell()\fR rather slow). | |
4744 | .Sp | |
4745 | If 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 | |
4747 | unpredictable and non\-portable. Use \f(CW\*(C`sysseek\*(C'\fR instead. | |
4748 | .Sp | |
4749 | Due to the rules and rigors of \s-1ANSI\s0 C, on some systems you have to do a | |
4750 | seek whenever you switch between reading and writing. Amongst other | |
4751 | things, this may have the effect of calling stdio's \fIclearerr\fR\|(3). | |
4752 | A \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 | |
4758 | This 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, | |
4761 | but it \fIdoes\fR clear the end-of-file condition on the handle, so that the | |
4762 | next \f(CW\*(C`<FILE>\*(C'\fR makes Perl try again to read something. We hope. | |
4763 | .Sp | |
4764 | If that doesn't work (some \s-1IO\s0 implementations are particularly | |
4765 | cantankerous), 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" | |
4779 | Sets the current position for the \f(CW\*(C`readdir\*(C'\fR routine on \s-1DIRHANDLE\s0. \s-1POS\s0 | |
4780 | must be a value returned by \f(CW\*(C`telldir\*(C'\fR. Has the same caveats about | |
4781 | possible directory compaction as the corresponding system library | |
4782 | routine. | |
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 | |
4789 | Returns the currently selected filehandle. Sets the current default | |
4790 | filehandle for output, if \s-1FILEHANDLE\s0 is supplied. This has two | |
4791 | effects: first, a \f(CW\*(C`write\*(C'\fR or a \f(CW\*(C`print\*(C'\fR without a filehandle will | |
4792 | default to this \s-1FILEHANDLE\s0. Second, references to variables related to | |
4793 | output will refer to this output channel. For example, if you have to | |
4794 | set the top of form format for more than one output channel, you might | |
4795 | do 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 | |
4805 | actual filehandle. Thus: | |
4806 | .Sp | |
4807 | .Vb 1 | |
4808 | \& $oldfh = select(STDERR); $| = 1; select($oldfh); | |
4809 | .Ve | |
4810 | .Sp | |
4811 | Some programmers may prefer to think of filehandles as objects with | |
4812 | methods, 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" | |
4820 | This calls the \fIselect\fR\|(2) system call with the bit masks specified, which | |
4821 | can 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 | |
4830 | If you want to select on many filehandles you might wish to write a | |
4831 | subroutine: | |
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 | |
4845 | The usual idiom is: | |
4846 | .Sp | |
4847 | .Vb 2 | |
4848 | \& ($nfound,$timeleft) = | |
4849 | \& select($rout=$rin, $wout=$win, $eout=$ein, $timeout); | |
4850 | .Ve | |
4851 | .Sp | |
4852 | or 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 | |
4858 | Most systems do not bother to return anything useful in \f(CW$timeleft\fR, so | |
4859 | calling \fIselect()\fR in scalar context just returns \f(CW$nfound\fR. | |
4860 | .Sp | |
4861 | Any of the bit masks can also be undef. The timeout, if specified, is | |
4862 | in seconds, which may be fractional. Note: not all implementations are | |
4863 | capable 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 | |
4866 | You 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 | |
4872 | Note that whether \f(CW\*(C`select\*(C'\fR gets restarted after signals (say, \s-1SIGALRM\s0) | |
4873 | is 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 | |
4876 | or <\s-1FH\s0>) with \f(CW\*(C`select\*(C'\fR, except as permitted by \s-1POSIX\s0, and even | |
4877 | then 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" | |
4880 | Calls 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 | |
4886 | first 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 | |
4888 | semid_ds structure or semaphore value array. Returns like \f(CW\*(C`ioctl\*(C'\fR: | |
4889 | the undefined value for error, "\f(CW\*(C`0 but true\*(C'\fR" for zero, or the actual | |
4890 | return value otherwise. The \s-1ARG\s0 must consist of a vector of native | |
4891 | short integers, which may be created with \f(CW\*(C`pack("s!",(0)x$nsem)\*(C'\fR. | |
4892 | See also \*(L"SysV \s-1IPC\s0\*(R" in perlipc, \f(CW\*(C`IPC::SysV\*(C'\fR, \f(CW\*(C`IPC::Semaphore\*(C'\fR | |
4893 | documentation. | |
4894 | .IP "semget \s-1KEY\s0,NSEMS,FLAGS" 8 | |
4895 | .IX Item "semget KEY,NSEMS,FLAGS" | |
4896 | Calls the System V \s-1IPC\s0 function semget. Returns the semaphore id, or | |
4897 | the 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 | |
4899 | documentation. | |
4900 | .IP "semop \s-1KEY\s0,OPSTRING" 8 | |
4901 | .IX Item "semop KEY,OPSTRING" | |
4902 | Calls the System V \s-1IPC\s0 function semop to perform semaphore operations | |
4903 | such as signalling and waiting. \s-1OPSTRING\s0 must be a packed array of | |
4904 | semop structures. Each semop structure can be generated with | |
4905 | \&\f(CW\*(C`pack("s!3", $semnum, $semop, $semflag)\*(C'\fR. The number of semaphore | |
4906 | operations is implied by the length of \s-1OPSTRING\s0. Returns true if | |
4907 | successful, or false if there is an error. As an example, the | |
4908 | following 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 | |
4915 | To 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 | |
4917 | documentation. | |
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 | |
4924 | Sends 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 | |
4926 | same name. On unconnected sockets you must specify a destination to | |
4927 | send \s-1TO\s0, in which case it does a C \f(CW\*(C`sendto\*(C'\fR. Returns the number of | |
4928 | characters sent, or the undefined value if there is an error. The C | |
4929 | system call \fIsendmsg\fR\|(2) is currently unimplemented. See | |
4930 | \&\*(L"\s-1UDP:\s0 Message Passing\*(R" in perlipc for examples. | |
4931 | .Sp | |
4932 | Note the \fIcharacters\fR: depending on the status of the socket, either | |
4933 | (8\-bit) bytes or characters are sent. By default all sockets operate | |
4934 | on 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 | |
4936 | the \f(CW\*(C`open\*(C'\fR pragma, open), the I/O will operate on characters, not | |
4937 | bytes. | |
4938 | .IP "setpgrp \s-1PID\s0,PGRP" 8 | |
4939 | .IX Item "setpgrp PID,PGRP" | |
4940 | Sets the current process group for the specified \s-1PID\s0, \f(CW0\fR for the current | |
4941 | process. Will produce a fatal error if used on a machine that doesn't | |
4942 | implement \s-1POSIX\s0 \fIsetpgid\fR\|(2) or \s-1BSD\s0 \fIsetpgrp\fR\|(2). If the arguments are omitted, | |
4943 | it 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 | |
4944 | accept 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" | |
4948 | Sets 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 | |
4950 | that doesn't implement \fIsetpriority\fR\|(2). | |
4951 | .IP "setsockopt \s-1SOCKET\s0,LEVEL,OPTNAME,OPTVAL" 8 | |
4952 | .IX Item "setsockopt SOCKET,LEVEL,OPTNAME,OPTVAL" | |
4953 | Sets the socket option requested. Returns undefined if there is an | |
4954 | error. \s-1OPTVAL\s0 may be specified as \f(CW\*(C`undef\*(C'\fR if you don't want to pass an | |
4955 | argument. | |
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 | |
4962 | Shifts the first value of the array off and returns it, shortening the | |
4963 | array by 1 and moving everything down. If there are no elements in the | |
4964 | array, 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 | |
4967 | the \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 | |
4968 | constructs. | |
4969 | .Sp | |
4970 | See 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 | |
4971 | same 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 | |
4972 | right end. | |
4973 | .IP "shmctl \s-1ID\s0,CMD,ARG" 8 | |
4974 | .IX Item "shmctl ID,CMD,ARG" | |
4975 | Calls 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 | |
4981 | first to get the correct constant definitions. If \s-1CMD\s0 is \f(CW\*(C`IPC_STAT\*(C'\fR, | |
4982 | then \s-1ARG\s0 must be a variable which will hold the returned \f(CW\*(C`shmid_ds\*(C'\fR | |
4983 | structure. Returns like ioctl: the undefined value for error, "\f(CW0\fR but | |
4984 | true" for zero, or the actual return value otherwise. | |
4985 | See 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" | |
4988 | Calls the System V \s-1IPC\s0 function shmget. Returns the shared memory | |
4989 | segment id, or the undefined value if there is an error. | |
4990 | See 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 | |
4997 | Reads or writes the System V shared memory segment \s-1ID\s0 starting at | |
4998 | position \s-1POS\s0 for size \s-1SIZE\s0 by attaching to it, copying in/out, and | |
4999 | detaching from it. When reading, \s-1VAR\s0 must be a variable that will | |
5000 | hold the data read. When writing, if \s-1STRING\s0 is too long, only \s-1SIZE\s0 | |
5001 | bytes 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" | |
5007 | Shuts down a socket connection in the manner indicated by \s-1HOW\s0, which | |
5008 | has 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 | |
5016 | This is useful with sockets when you want to tell the other | |
5017 | side you're done writing but not done reading, or vice versa. | |
5018 | It's also a more insistent form of close because it also | |
5019 | disables the file descriptor in any forked copies in other | |
5020 | processes. | |
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 | |
5027 | Returns the sine of \s-1EXPR\s0 (expressed in radians). If \s-1EXPR\s0 is omitted, | |
5028 | returns sine of \f(CW$_\fR. | |
5029 | .Sp | |
5030 | For the inverse sine operation, you may use the \f(CW\*(C`Math::Trig::asin\*(C'\fR | |
5031 | function, 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 | |
5042 | Causes the script to sleep for \s-1EXPR\s0 seconds, or forever if no \s-1EXPR\s0. | |
5043 | May be interrupted if the process receives a signal such as \f(CW\*(C`SIGALRM\*(C'\fR. | |
5044 | Returns the number of seconds actually slept. You probably cannot | |
5045 | mix \f(CW\*(C`alarm\*(C'\fR and \f(CW\*(C`sleep\*(C'\fR calls, because \f(CW\*(C`sleep\*(C'\fR is often implemented | |
5046 | using \f(CW\*(C`alarm\*(C'\fR. | |
5047 | .Sp | |
5048 | On some older systems, it may sleep up to a full second less than what | |
5049 | you requested, depending on how it counts seconds. Most modern systems | |
5050 | always sleep the full amount. They may appear to sleep longer than that, | |
5051 | however, because your process might not be scheduled right away in a | |
5052 | busy multitasking system. | |
5053 | .Sp | |
5054 | For 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 | |
5056 | it, or else see \*(L"select\*(R" above. The Time::HiRes module (from \s-1CPAN\s0, | |
5057 | and starting from Perl 5.8 part of the standard distribution) may also | |
5058 | help. | |
5059 | .Sp | |
5060 | See 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" | |
5063 | Opens 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 | |
5065 | the system call of the same name. You should \f(CW\*(C`use Socket\*(C'\fR first | |
5066 | to get the proper definitions imported. See the examples in | |
5067 | \&\*(L"Sockets: Client/Server Communication\*(R" in perlipc. | |
5068 | .Sp | |
5069 | On systems that support a close-on-exec flag on files, the flag will | |
5070 | be set for the newly opened file descriptor, as determined by the | |
5071 | value 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" | |
5074 | Creates an unnamed pair of sockets in the specified domain, of the | |
5075 | specified type. \s-1DOMAIN\s0, \s-1TYPE\s0, and \s-1PROTOCOL\s0 are specified the same as | |
5076 | for the system call of the same name. If unimplemented, yields a fatal | |
5077 | error. Returns true if successful. | |
5078 | .Sp | |
5079 | On systems that support a close-on-exec flag on files, the flag will | |
5080 | be set for the newly opened file descriptors, as determined by the value | |
5081 | of $^F. See \*(L"$^F\*(R" in perlvar. | |
5082 | .Sp | |
5083 | Some systems defined \f(CW\*(C`pipe\*(C'\fR in terms of \f(CW\*(C`socketpair\*(C'\fR, in which a call | |
5084 | to \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 | |
5093 | See perlipc for an example of socketpair use. Perl 5.8 and later will | |
5094 | emulate socketpair using \s-1IP\s0 sockets to localhost if your system implements | |
5095 | sockets 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 | |
5104 | In list context, this sorts the \s-1LIST\s0 and returns the sorted list value. | |
5105 | In scalar context, the behaviour of \f(CW\*(C`sort()\*(C'\fR is undefined. | |
5106 | .Sp | |
5107 | If \s-1SUBNAME\s0 or \s-1BLOCK\s0 is omitted, \f(CW\*(C`sort\*(C'\fRs in standard string comparison | |
5108 | order. If \s-1SUBNAME\s0 is specified, it gives the name of a subroutine | |
5109 | that returns an integer less than, equal to, or greater than \f(CW0\fR, | |
5110 | depending 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 | |
5112 | the value provides the name of (or a reference to) the actual | |
5113 | subroutine to use. In place of a \s-1SUBNAME\s0, you can provide a \s-1BLOCK\s0 as | |
5114 | an anonymous, in-line sort subroutine. | |
5115 | .Sp | |
5116 | If the subroutine's prototype is \f(CW\*(C`($$)\*(C'\fR, the elements to be compared | |
5117 | are passed by reference in \f(CW@_\fR, as for a normal subroutine. This is | |
5118 | slower than unprototyped subroutines, where the elements to be | |
5119 | compared are passed into the subroutine | |
5120 | as the package global variables \f(CW$a\fR and \f(CW$b\fR (see example below). Note that | |
5121 | in the latter case, it is usually counter-productive to declare \f(CW$a\fR and | |
5122 | \&\f(CW$b\fR as lexicals. | |
5123 | .Sp | |
5124 | In either case, the subroutine may not be recursive. The values to be | |
5125 | compared are always passed by reference, so don't modify them. | |
5126 | .Sp | |
5127 | You also cannot exit out of the sort block or subroutine using any of the | |
5128 | loop control operators described in perlsyn or with \f(CW\*(C`goto\*(C'\fR. | |
5129 | .Sp | |
5130 | When \f(CW\*(C`use locale\*(C'\fR is in effect, \f(CW\*(C`sort LIST\*(C'\fR sorts \s-1LIST\s0 according to the | |
5131 | current collation locale. See perllocale. | |
5132 | .Sp | |
5133 | Perl 5.6 and earlier used a quicksort algorithm to implement sort. | |
5134 | That algorithm was not stable, and \fIcould\fR go quadratic. (A \fIstable\fR sort | |
5135 | preserves the input order of elements that compare equal. Although | |
5136 | quicksort's run time is O(NlogN) when averaged over all arrays of | |
5137 | length N, the time can be O(N**2), \fIquadratic\fR behavior, for some | |
5138 | inputs.) In 5.7, the quicksort implementation was replaced with | |
5139 | a stable mergesort algorithm whose worst case behavior is O(NlogN). | |
5140 | But benchmarks indicated that for some inputs, on some platforms, | |
5141 | the original quicksort was faster. 5.8 has a sort pragma for | |
5142 | limited control of the sort. Its rather blunt control of the | |
5143 | underlying algorithm may not persist into future perls, but the | |
5144 | ability to characterize the input or output in implementation | |
5145 | independent ways quite probably will. See \*(L"use\*(R". | |
5146 | .Sp | |
5147 | Examples: | |
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 | |
5272 | If you're using strict, you \fImust not\fR declare \f(CW$a\fR | |
5273 | and \f(CW$b\fR as lexicals. They are package globals. That means | |
5274 | if 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 | |
5280 | then \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), | |
5281 | but 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 | |
5287 | The comparison function is required to behave. If it returns | |
5288 | inconsistent results (sometimes saying \f(CW$x[1]\fR is less than \f(CW$x[2]\fR and | |
5289 | sometimes saying the opposite, for example) the results are not | |
5290 | well\-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 | |
5301 | Removes the elements designated by \s-1OFFSET\s0 and \s-1LENGTH\s0 from an array, and | |
5302 | replaces them with the elements of \s-1LIST\s0, if any. In list context, | |
5303 | returns the elements removed from the array. In scalar context, | |
5304 | returns the last element removed, or \f(CW\*(C`undef\*(C'\fR if no elements are | |
5305 | removed. The array grows or shrinks as necessary. | |
5306 | If \s-1OFFSET\s0 is negative then it starts that far from the end of the array. | |
5307 | If \s-1LENGTH\s0 is omitted, removes everything from \s-1OFFSET\s0 onward. | |
5308 | If \s-1LENGTH\s0 is negative, removes the elements from \s-1OFFSET\s0 onward | |
5309 | except for \-LENGTH elements at the end of the array. | |
5310 | If both \s-1OFFSET\s0 and \s-1LENGTH\s0 are omitted, removes everything. If \s-1OFFSET\s0 is | |
5311 | past the end of the array, perl issues a warning, and splices at the | |
5312 | end of the array. | |
5313 | .Sp | |
5314 | The 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 | |
5324 | Example, 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 | |
5348 | Splits a string into a list of strings and returns that list. By default, | |
5349 | empty leading fields are preserved, and empty trailing ones are deleted. | |
5350 | .Sp | |
5351 | In scalar context, returns the number of fields found and splits into | |
5352 | the \f(CW@_\fR array. Use of split in scalar context is deprecated, however, | |
5353 | because it clobbers your subroutine arguments. | |
5354 | .Sp | |
5355 | If \s-1EXPR\s0 is omitted, splits the \f(CW$_\fR string. If \s-1PATTERN\s0 is also omitted, | |
5356 | splits on whitespace (after skipping any leading whitespace). Anything | |
5357 | matching \s-1PATTERN\s0 is taken to be a delimiter separating the fields. (Note | |
5358 | that the delimiter may be longer than one character.) | |
5359 | .Sp | |
5360 | If \s-1LIMIT\s0 is specified and positive, it represents the maximum number | |
5361 | of fields the \s-1EXPR\s0 will be split into, though the actual number of | |
5362 | fields 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 | |
5364 | stripped (which potential users of \f(CW\*(C`pop\*(C'\fR would do well to remember). | |
5365 | If \s-1LIMIT\s0 is negative, it is treated as if an arbitrarily large \s-1LIMIT\s0 | |
5366 | had been specified. Note that splitting an \s-1EXPR\s0 that evaluates to the | |
5367 | empty string always returns the empty list, regardless of the \s-1LIMIT\s0 | |
5368 | specified. | |
5369 | .Sp | |
5370 | A pattern matching the null string (not to be confused with | |
5371 | a null pattern \f(CW\*(C`//\*(C'\fR, which is just one member of the set of patterns | |
5372 | matching a null string) will split the value of \s-1EXPR\s0 into separate | |
5373 | characters at each point it matches that way. For example: | |
5374 | .Sp | |
5375 | .Vb 1 | |
5376 | \& print join(':', split(/ */, 'hi there')); | |
5377 | .Ve | |
5378 | .Sp | |
5379 | produces the output 'h:i:t:h:e:r:e'. | |
5380 | .Sp | |
5381 | Using the empty pattern \f(CW\*(C`//\*(C'\fR specifically matches the null string, and is | |
5382 | not be confused with the use of \f(CW\*(C`//\*(C'\fR to mean \*(L"the last successful pattern | |
5383 | match\*(R". | |
5384 | .Sp | |
5385 | Empty leading (or trailing) fields are produced when there are positive width | |
5386 | matches at the beginning (or end) of the string; a zero-width match at the | |
5387 | beginning (or end) of the string does not produce an empty field. For | |
5388 | example: | |
5389 | .Sp | |
5390 | .Vb 1 | |
5391 | \& print join(':', split(/(?=\ew)/, 'hi there!')); | |
5392 | .Ve | |
5393 | .Sp | |
5394 | produces the output 'h:i :t:h:e:r:e!'. | |
5395 | .Sp | |
5396 | The \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 | |
5402 | When assigning to a list, if \s-1LIMIT\s0 is omitted, Perl supplies a \s-1LIMIT\s0 | |
5403 | one larger than the number of variables in the list, to avoid | |
5404 | unnecessary work. For the list above \s-1LIMIT\s0 would have been 4 by | |
5405 | default. In time critical applications it behooves you not to split | |
5406 | into more fields than you really need. | |
5407 | .Sp | |
5408 | If the \s-1PATTERN\s0 contains parentheses, additional list elements are | |
5409 | created from each matching substring in the delimiter. | |
5410 | .Sp | |
5411 | .Vb 1 | |
5412 | \& split(/([,-])/, "1-10,20", 3); | |
5413 | .Ve | |
5414 | .Sp | |
5415 | produces the list value | |
5416 | .Sp | |
5417 | .Vb 1 | |
5418 | \& (1, '-', 10, ',', 20) | |
5419 | .Ve | |
5420 | .Sp | |
5421 | If you had the entire header of a normal Unix email message in \f(CW$header\fR, | |
5422 | you 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 | |
5429 | The pattern \f(CW\*(C`/PATTERN/\*(C'\fR may be replaced with an expression to specify | |
5430 | patterns that vary at runtime. (To do runtime compilation only once, | |
5431 | use \f(CW\*(C`/$variable/o\*(C'\fR.) | |
5432 | .Sp | |
5433 | As a special case, specifying a \s-1PATTERN\s0 of space (\f(CW' '\fR) will split on | |
5434 | white space just as \f(CW\*(C`split\*(C'\fR with no arguments does. Thus, \f(CW\*(C`split(' ')\*(C'\fR can | |
5435 | be used to emulate \fBawk\fR's default behavior, whereas \f(CW\*(C`split(/ /)\*(C'\fR | |
5436 | will give you as many null initial fields as there are leading spaces. | |
5437 | A \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 | |
5438 | whitespace produces a null first field. A \f(CW\*(C`split\*(C'\fR with no arguments | |
5439 | really does a \f(CW\*(C`split(' ', $_)\*(C'\fR internally. | |
5440 | .Sp | |
5441 | A \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 | |
5442 | much use otherwise. | |
5443 | .Sp | |
5444 | Example: | |
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 | |
5456 | As with regular pattern matching, any capturing parentheses that are not | |
5457 | matched 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" | |
5465 | Returns a string formatted by the usual \f(CW\*(C`printf\*(C'\fR conventions of the C | |
5466 | library function \f(CW\*(C`sprintf\*(C'\fR. See below for more details | |
5467 | and see \fIsprintf\fR\|(3) or \fIprintf\fR\|(3) on your system for an explanation of | |
5468 | the general principles. | |
5469 | .Sp | |
5470 | For 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 | |
5482 | Perl does its own \f(CW\*(C`sprintf\*(C'\fR formatting\*(--it emulates the C | |
5483 | function \f(CW\*(C`sprintf\*(C'\fR, but it doesn't use it (except for floating-point | |
5484 | numbers, and even then only the standard modifiers are allowed). As a | |
5485 | result, any non-standard extensions in your local \f(CW\*(C`sprintf\*(C'\fR are not | |
5486 | available from Perl. | |
5487 | .Sp | |
5488 | Unlike \f(CW\*(C`printf\*(C'\fR, \f(CW\*(C`sprintf\*(C'\fR does not do what you probably mean when you | |
5489 | pass it an array as your first argument. The array is given scalar context, | |
5490 | and instead of using the 0th element of the array as the format, Perl will | |
5491 | use the count of elements in the array as the format, which is almost never | |
5492 | useful. | |
5493 | .Sp | |
5494 | Perl'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 | |
5509 | In 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 | |
5521 | Finally, for backward (and we do mean \*(L"backward\*(R") compatibility, Perl | |
5522 | permits 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 | |
5532 | Note that the number of exponent digits in the scientific notation produced | |
5533 | by \f(CW%e\fR, \f(CW%E\fR, \f(CW%g\fR and \f(CW%G\fR for numbers with the modulus of the | |
5534 | exponent 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 | |
5536 | 99th may be either \*(L"1.23e99\*(R" or \*(L"1.23e099\*(R". | |
5537 | .Sp | |
5538 | Between the \f(CW\*(C`%\*(C'\fR and the format letter, you may specify a number of | |
5539 | additional attributes controlling the interpretation of the format. | |
5540 | In order, these are: | |
5541 | .RS 8 | |
5542 | .IP "format parameter index" 4 | |
5543 | .IX Item "format parameter index" | |
5544 | An explicit format parameter index, such as \f(CW\*(C`2$\*(C'\fR. By default sprintf | |
5545 | will format the next unused argument in the list, but this allows you | |
5546 | to 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" | |
5554 | one 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 | |
5562 | For 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" | |
5574 | The vector flag \f(CW\*(C`v\*(C'\fR, optionally specifying the join string to use. | |
5575 | This flag tells perl to interpret the supplied string as a vector | |
5576 | of integers, one for each character in the string, separated by | |
5577 | a given string (a dot \f(CW\*(C`.\*(C'\fR by default). This can be useful for | |
5578 | displaying 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 | |
5584 | Put an asterisk \f(CW\*(C`*\*(C'\fR before the \f(CW\*(C`v\*(C'\fR to override the string to | |
5585 | use 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 | |
5592 | You can also explicitly specify the argument number to use for | |
5593 | the 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" | |
5600 | Arguments are usually formatted to be only as wide as required to | |
5601 | display the given value. You can override the width by putting | |
5602 | a number here, or get the width from the next argument (with \f(CW\*(C`*\*(C'\fR) | |
5603 | or 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 | |
5613 | If a field width obtained through \f(CW\*(C`*\*(C'\fR is negative, it has the same | |
5614 | effect as the \f(CW\*(C`\-\*(C'\fR flag: left\-justification. | |
5615 | .IP "precision, or maximum width" 4 | |
5616 | .IX Item "precision, or maximum width" | |
5617 | You can specify a precision (for numeric conversions) or a maximum | |
5618 | width (for string conversions) by specifying a \f(CW\*(C`.\*(C'\fR followed by a number. | |
5619 | For floating point formats, this specifies the number of decimal places | |
5620 | to 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 | |
5631 | For integer conversions, specifying a precision implies that the | |
5632 | output 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 | |
5640 | For string conversions, specifying a precision truncates the string | |
5641 | to 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 | |
5648 | You 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 | |
5655 | You cannot currently get the precision from a specified number, | |
5656 | but it is intended that this will be possible in the future using | |
5657 | eg \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" | |
5664 | For numeric conversions, you can specify the size to interpret the | |
5665 | number 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 | |
5666 | conversions, numbers are usually assumed to be whatever the default | |
5667 | integer size is on your platform (usually 32 or 64 bits), but you | |
5668 | can override this to use instead one of the standard C types, as | |
5669 | supported 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 | |
5678 | For floating point conversions, numbers are usually assumed to be | |
5679 | the default floating point size on your platform (double or long double), | |
5680 | but 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 | |
5681 | platform supports them. | |
5682 | .Sp | |
5683 | The size specifier 'V' has no effect for Perl code, but it supported | |
5684 | for compatibility with \s-1XS\s0 code; it means 'use the standard size for | |
5685 | a Perl integer (or floating-point number)', which is already the | |
5686 | default for Perl code. | |
5687 | .IP "order of arguments" 4 | |
5688 | .IX Item "order of arguments" | |
5689 | Normally, sprintf takes the next unused argument as the value to | |
5690 | format for each format specification. If the format specification | |
5691 | uses \f(CW\*(C`*\*(C'\fR to require additional arguments, these are consumed from | |
5692 | the argument list in the order in which they appear in the format | |
5693 | specification \fIbefore\fR the value to format. Where an argument is | |
5694 | specified using an explicit index, this does not affect the normal | |
5695 | order for the arguments (even when the explicitly specified index | |
5696 | would have been the next argument in any case). | |
5697 | .Sp | |
5698 | So: | |
5699 | .Sp | |
5700 | .Vb 1 | |
5701 | \& printf '<%*.*s>', $a, $b, $c; | |
5702 | .Ve | |
5703 | .Sp | |
5704 | would use \f(CW$a\fR for the width, \f(CW$b\fR for the precision and \f(CW$c\fR | |
5705 | as the value to format, while: | |
5706 | .Sp | |
5707 | .Vb 1 | |
5708 | \& print '<%*1$.*s>', $a, $b; | |
5709 | .Ve | |
5710 | .Sp | |
5711 | would use \f(CW$a\fR for the width and the precision, and \f(CW$b\fR as the | |
5712 | value to format. | |
5713 | .Sp | |
5714 | Here are some more examples \- beware that when using an explicit | |
5715 | index, 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 | |
5726 | If \f(CW\*(C`use locale\*(C'\fR is in effect, the character used for the decimal | |
5727 | point in formatted real numbers is affected by the \s-1LC_NUMERIC\s0 locale. | |
5728 | See perllocale. | |
5729 | .Sp | |
5730 | If Perl understands \*(L"quads\*(R" (64\-bit integers) (this requires | |
5731 | either that the platform natively support quads or that Perl | |
5732 | be 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 | |
5738 | print quads, and they may optionally be preceded by | |
5739 | .Sp | |
5740 | .Vb 1 | |
5741 | \& ll L q | |
5742 | .Ve | |
5743 | .Sp | |
5744 | For example | |
5745 | .Sp | |
5746 | .Vb 1 | |
5747 | \& %lld %16LX %qo | |
5748 | .Ve | |
5749 | .Sp | |
5750 | You 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 | |
5758 | If Perl understands \*(L"long doubles\*(R" (this requires that the platform | |
5759 | support long doubles), the flags | |
5760 | .Sp | |
5761 | .Vb 1 | |
5762 | \& e f g E F G | |
5763 | .Ve | |
5764 | .Sp | |
5765 | may optionally be preceded by | |
5766 | .Sp | |
5767 | .Vb 1 | |
5768 | \& ll L | |
5769 | .Ve | |
5770 | .Sp | |
5771 | For example | |
5772 | .Sp | |
5773 | .Vb 1 | |
5774 | \& %llf %Lg | |
5775 | .Ve | |
5776 | .Sp | |
5777 | You 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 | |
5790 | Return the square root of \s-1EXPR\s0. If \s-1EXPR\s0 is omitted, returns square | |
5791 | root of \f(CW$_\fR. Only works on non-negative operands, unless you've | |
5792 | loaded 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 | |
5804 | Sets the random number seed for the \f(CW\*(C`rand\*(C'\fR operator. | |
5805 | .Sp | |
5806 | The 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 | |
5808 | program. | |
5809 | .Sp | |
5810 | If \fIsrand()\fR is not called explicitly, it is called implicitly at the | |
5811 | first use of the \f(CW\*(C`rand\*(C'\fR operator. However, this was not the case in | |
5812 | versions of Perl before 5.004, so if your script will run under older | |
5813 | Perl versions, it should call \f(CW\*(C`srand\*(C'\fR. | |
5814 | .Sp | |
5815 | Most programs won't even call \fIsrand()\fR at all, except those that | |
5816 | need a cryptographically-strong starting point rather than the | |
5817 | generally acceptable default, which is based on time of day, | |
5818 | process \s-1ID\s0, and memory allocation, or the \fI/dev/urandom\fR device, | |
5819 | if available. | |
5820 | .Sp | |
5821 | You 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 | |
5823 | generating predictable results for testing or debugging. | |
5824 | Otherwise, don't call \fIsrand()\fR more than once in your program. | |
5825 | .Sp | |
5826 | Do \fBnot\fR call \fIsrand()\fR (i.e. without an argument) more than once in | |
5827 | a script. The internal state of the random number generator should | |
5828 | contain more entropy than can be provided by any seed, so calling | |
5829 | \&\fIsrand()\fR again actually \fIloses\fR randomness. | |
5830 | .Sp | |
5831 | Most implementations of \f(CW\*(C`srand\*(C'\fR take an integer and will silently | |
5832 | truncate decimal numbers. This means \f(CW\*(C`srand(42)\*(C'\fR will usually | |
5833 | produce 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 | |
5836 | In versions of Perl prior to 5.004 the default seed was just the | |
5837 | current \f(CW\*(C`time\*(C'\fR. This isn't a particularly good seed, so many old | |
5838 | programs 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 | |
5841 | Note that you need something much more random than the default seed for | |
5842 | cryptographic purposes. Checksumming the compressed output of one or more | |
5843 | rapidly changing operating system status programs is the usual method. For | |
5844 | example: | |
5845 | .Sp | |
5846 | .Vb 1 | |
5847 | \& srand (time ^ $$ ^ unpack "%L*", `ps axww | gzip`); | |
5848 | .Ve | |
5849 | .Sp | |
5850 | If you're particularly concerned with this, see the \f(CW\*(C`Math::TrulyRandom\*(C'\fR | |
5851 | module in \s-1CPAN\s0. | |
5852 | .Sp | |
5853 | Frequently called programs (like \s-1CGI\s0 scripts) that simply use | |
5854 | .Sp | |
5855 | .Vb 1 | |
5856 | \& time ^ $$ | |
5857 | .Ve | |
5858 | .Sp | |
5859 | for 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 | |
5865 | one-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 | |
5874 | Returns a 13\-element list giving the status info for a file, either | |
5875 | the file opened via \s-1FILEHANDLE\s0, or named by \s-1EXPR\s0. If \s-1EXPR\s0 is omitted, | |
5876 | it stats \f(CW$_\fR. Returns a null list if the stat fails. Typically used | |
5877 | as 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 | |
5885 | Not all fields are supported on all filesystem types. Here are the | |
5886 | meaning 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 | |
5907 | it to be a \*(L"creation time\*(R", see \*(L"Files and Filesystems\*(R" in perlport | |
5908 | for details. | |
5909 | .Sp | |
5910 | If stat is passed the special filehandle consisting of an underline, no | |
5911 | stat is done, but the current contents of the stat structure from the | |
5912 | last 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 | |
5921 | under \s-1NFS\s0.) | |
5922 | .Sp | |
5923 | Because the mode contains both the file type and its permissions, you | |
5924 | should mask off the file type portion and (s)printf using a \f(CW"%o"\fR | |
5925 | if 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 | |
5932 | In scalar context, \f(CW\*(C`stat\*(C'\fR returns a boolean value indicating success | |
5933 | or failure, and, if successful, sets the information associated with | |
5934 | the special filehandle \f(CW\*(C`_\*(C'\fR. | |
5935 | .Sp | |
5936 | The 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 | |
5946 | You 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 | |
5972 | You could write the last two using the \f(CW\*(C`\-u\*(C'\fR and \f(CW\*(C`\-d\*(C'\fR operators. | |
5973 | The 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 | |
6009 | and 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 | |
6041 | See your native \fIchmod\fR\|(2) and \fIstat\fR\|(2) documentation for more details | |
6042 | about 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 | |
6049 | Takes extra time to study \s-1SCALAR\s0 (\f(CW$_\fR if unspecified) in anticipation of | |
6050 | doing many pattern matches on the string before it is next modified. | |
6051 | This may or may not save time, depending on the nature and number of | |
6052 | patterns you are searching on, and on the distribution of character | |
6053 | frequencies in the string to be searched\*(--you probably want to compare | |
6054 | run times with and without it to see which runs faster. Those loops | |
6055 | which scan for many short constant strings (including the constant | |
6056 | parts of more complex patterns) will benefit most. You may have only | |
6057 | one \f(CW\*(C`study\*(C'\fR active at a time\*(--if you study a different scalar the first | |
6058 | is \*(L"unstudied\*(R". (The way \f(CW\*(C`study\*(C'\fR works is this: a linked list of every | |
6059 | character in the string to be searched is made, so we know, for | |
6060 | example, where all the \f(CW'k'\fR characters are. From each search string, | |
6061 | the rarest character is selected, based on some static frequency tables | |
6062 | constructed from some C programs and English text. Only those places | |
6063 | that contain this \*(L"rarest\*(R" character are examined.) | |
6064 | .Sp | |
6065 | For example, here is a loop that inserts index producing entries | |
6066 | before 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 | |
6079 | In searching for \f(CW\*(C`/\ebfoo\eb/\*(C'\fR, only those locations in \f(CW$_\fR that contain \f(CW\*(C`f\*(C'\fR | |
6080 | will be looked at, because \f(CW\*(C`f\*(C'\fR is rarer than \f(CW\*(C`o\*(C'\fR. In general, this is | |
6081 | a big win except in pathological cases. The only question is whether | |
6082 | it saves you more time than it took to build the linked list in the | |
6083 | first place. | |
6084 | .Sp | |
6085 | Note that if you have to look for strings that you don't know till | |
6086 | runtime, you can build an entire loop as a string and \f(CW\*(C`eval\*(C'\fR that to | |
6087 | avoid recompiling all your patterns all the time. Together with | |
6088 | undefining \f(CW$/\fR to input entire files as one record, this can be very | |
6089 | fast, often faster than specialized programs like \fIfgrep\fR\|(1). The following | |
6090 | scans a list of files (\f(CW@files\fR) for a list of words (\f(CW@words\fR), and prints | |
6091 | out 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 | |
6117 | This is subroutine definition, not a real function \fIper se\fR. | |
6118 | Without a \s-1BLOCK\s0 it's just a forward declaration. Without a \s-1NAME\s0, | |
6119 | it's an anonymous function declaration, and does actually return | |
6120 | a value: the \s-1CODE\s0 ref of the closure you just created. | |
6121 | .Sp | |
6122 | See perlsub and perlref for details about subroutines and | |
6123 | references, and attributes and Attribute::Handlers for more | |
6124 | information 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 | |
6133 | Extracts a substring out of \s-1EXPR\s0 and returns it. First character is at | |
6134 | offset \f(CW0\fR, or whatever you've set \f(CW$[\fR to (but don't do that). | |
6135 | If \s-1OFFSET\s0 is negative (or more precisely, less than \f(CW$[\fR), starts | |
6136 | that far from the end of the string. If \s-1LENGTH\s0 is omitted, returns | |
6137 | everything to the end of the string. If \s-1LENGTH\s0 is negative, leaves that | |
6138 | many characters off the end of the string. | |
6139 | .Sp | |
6140 | You can use the \fIsubstr()\fR function as an lvalue, in which case \s-1EXPR\s0 | |
6141 | must itself be an lvalue. If you assign something shorter than \s-1LENGTH\s0, | |
6142 | the string will shrink, and if you assign something longer than \s-1LENGTH\s0, | |
6143 | the string will grow to accommodate it. To keep the string the same | |
6144 | length you may need to pad or chop your value using \f(CW\*(C`sprintf\*(C'\fR. | |
6145 | .Sp | |
6146 | If \s-1OFFSET\s0 and \s-1LENGTH\s0 specify a substring that is partly outside the | |
6147 | string, only the part within the string is returned. If the substring | |
6148 | is beyond either end of the string, \fIsubstr()\fR returns the undefined | |
6149 | value and produces a warning. When used as an lvalue, specifying a | |
6150 | substring that is entirely outside the string is a fatal error. | |
6151 | Here'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 | |
6161 | An alternative to using \fIsubstr()\fR as an lvalue is to specify the | |
6162 | replacement string as the 4th argument. This allows you to replace | |
6163 | parts of the \s-1EXPR\s0 and return what was there before in one operation, | |
6164 | just as you can with \fIsplice()\fR. | |
6165 | .IP "symlink \s-1OLDFILE\s0,NEWFILE" 8 | |
6166 | .IX Item "symlink OLDFILE,NEWFILE" | |
6167 | Creates a new filename symbolically linked to the old filename. | |
6168 | Returns \f(CW1\fR for success, \f(CW0\fR otherwise. On systems that don't support | |
6169 | symbolic links, produces a fatal error at run time. To check for that, | |
6170 | use 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" | |
6177 | Calls the system call specified as the first element of the list, | |
6178 | passing the remaining elements as arguments to the system call. If | |
6179 | unimplemented, produces a fatal error. The arguments are interpreted | |
6180 | as follows: if a given argument is numeric, the argument is passed as | |
6181 | an int. If not, the pointer to the string value is passed. You are | |
6182 | responsible to make sure a string is pre-extended long enough to | |
6183 | receive any result that might be written into a string. You can't use a | |
6184 | string literal (or other read-only string) as an argument to \f(CW\*(C`syscall\*(C'\fR | |
6185 | because Perl has to assume that any string pointer might be written | |
6186 | through. If your | |
6187 | integer arguments are not literals and have never been interpreted in a | |
6188 | numeric context, you may need to add \f(CW0\fR to them to force them to look | |
6189 | like 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 | |
6197 | Note that Perl supports passing of up to only 14 arguments to your system call, | |
6198 | which in practice should usually suffice. | |
6199 | .Sp | |
6200 | Syscall returns whatever value returned by the system call it calls. | |
6201 | If the system call fails, \f(CW\*(C`syscall\*(C'\fR returns \f(CW\*(C`\-1\*(C'\fR and sets \f(CW$!\fR (errno). | |
6202 | Note that some system calls can legitimately return \f(CW\*(C`\-1\*(C'\fR. The proper | |
6203 | way to handle such calls is to assign \f(CW\*(C`$!=0;\*(C'\fR before the call and | |
6204 | check the value of \f(CW$!\fR if syscall returns \f(CW\*(C`\-1\*(C'\fR. | |
6205 | .Sp | |
6206 | There's a problem with \f(CW\*(C`syscall(&SYS_pipe)\*(C'\fR: it returns the file | |
6207 | number of the read end of the pipe it creates. There is no way | |
6208 | to retrieve the file number of the other end. You can avoid this | |
6209 | problem 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 | |
6216 | Opens the file whose filename is given by \s-1FILENAME\s0, and associates it | |
6217 | with \s-1FILEHANDLE\s0. If \s-1FILEHANDLE\s0 is an expression, its value is used as | |
6218 | the name of the real filehandle wanted. This function calls the | |
6219 | underlying 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 | |
6222 | The possible values and flag bits of the \s-1MODE\s0 parameter are | |
6223 | system\-dependent; they are available via the standard module \f(CW\*(C`Fcntl\*(C'\fR. | |
6224 | See the documentation of your operating system's \f(CW\*(C`open\*(C'\fR to see which | |
6225 | values and flag bits are available. You may combine several flags | |
6226 | using the \f(CW\*(C`|\*(C'\fR\-operator. | |
6227 | .Sp | |
6228 | Some of the most common values are \f(CW\*(C`O_RDONLY\*(C'\fR for opening the file in | |
6229 | read-only mode, \f(CW\*(C`O_WRONLY\*(C'\fR for opening the file in write-only mode, | |
6230 | and \f(CW\*(C`O_RDWR\*(C'\fR for opening the file in read-write mode, and. | |
6231 | .Sp | |
6232 | For historical reasons, some values work on almost every system | |
6233 | supported by perl: zero means read\-only, one means write\-only, and two | |
6234 | means 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 | |
6236 | use them in new code. | |
6237 | .Sp | |
6238 | If the file named by \s-1FILENAME\s0 does not exist and the \f(CW\*(C`open\*(C'\fR call creates | |
6239 | it (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 | |
6241 | the \s-1PERMS\s0 argument to \f(CW\*(C`sysopen\*(C'\fR, Perl uses the octal value \f(CW0666\fR. | |
6242 | These permission values need to be in octal, and are modified by your | |
6243 | process's current \f(CW\*(C`umask\*(C'\fR. | |
6244 | .Sp | |
6245 | In many systems the \f(CW\*(C`O_EXCL\*(C'\fR flag is available for opening files in | |
6246 | exclusive mode. This is \fBnot\fR locking: exclusiveness means here that | |
6247 | if 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 | |
6250 | Sometimes you may want to truncate an already-existing file: \f(CW\*(C`O_TRUNC\*(C'\fR. | |
6251 | .Sp | |
6252 | You should seldom if ever use \f(CW0644\fR as argument to \f(CW\*(C`sysopen\*(C'\fR, because | |
6253 | that takes away the user's option to have a more permissive umask. | |
6254 | Better to omit it. See the \fIperlfunc\fR\|(1) entry on \f(CW\*(C`umask\*(C'\fR for more | |
6255 | on this. | |
6256 | .Sp | |
6257 | Note that \f(CW\*(C`sysopen\*(C'\fR depends on the \fIfdopen()\fR C library function. | |
6258 | On many \s-1UNIX\s0 systems, \fIfdopen()\fR is known to fail when file descriptors | |
6259 | exceed a certain value, typically 255. If you need more file | |
6260 | descriptors than that, consider rebuilding Perl to use the \f(CW\*(C`sfio\*(C'\fR | |
6261 | library, or perhaps using the \fIPOSIX::open()\fR function. | |
6262 | .Sp | |
6263 | See 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 | |
6270 | Attempts to read \s-1LENGTH\s0 \fIcharacters\fR of data into variable \s-1SCALAR\s0 from | |
6271 | the specified \s-1FILEHANDLE\s0, using the system call \fIread\fR\|(2). It bypasses | |
6272 | buffered \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 | |
6274 | stdio usually buffers data. Returns the number of characters actually | |
6275 | read, \f(CW0\fR at end of file, or undef if there was an error. \s-1SCALAR\s0 | |
6276 | will be grown or shrunk so that the last byte actually read is the | |
6277 | last byte of the scalar after the read. | |
6278 | .Sp | |
6279 | Note the \fIcharacters\fR: depending on the status of the filehandle, | |
6280 | either (8\-bit) bytes or characters are read. By default all | |
6281 | filehandles operate on bytes, but for example if the filehandle has | |
6282 | been opened with the \f(CW\*(C`:utf8\*(C'\fR I/O layer (see \*(L"open\*(R", and the \f(CW\*(C`open\*(C'\fR | |
6283 | pragma, open), the I/O will operate on characters, not bytes. | |
6284 | .Sp | |
6285 | An \s-1OFFSET\s0 may be specified to place the read data at some place in the | |
6286 | string other than the beginning. A negative \s-1OFFSET\s0 specifies | |
6287 | placement at that many characters counting backwards from the end of | |
6288 | the string. A positive \s-1OFFSET\s0 greater than the length of \s-1SCALAR\s0 | |
6289 | results in the string being padded to the required size with \f(CW"\e0"\fR | |
6290 | bytes before the result of the read is appended. | |
6291 | .Sp | |
6292 | There is no \fIsyseof()\fR function, which is ok, since \fIeof()\fR doesn't work | |
6293 | very well on device files (like ttys) anyway. Use \fIsysread()\fR and check | |
6294 | for 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" | |
6297 | Sets \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 | |
6299 | of the filehandle. The values for \s-1WHENCE\s0 are \f(CW0\fR to set the new | |
6300 | position 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 | |
6302 | negative). | |
6303 | .Sp | |
6304 | Note the \fIin bytes\fR: even if the filehandle has been set to operate | |
6305 | on characters (for example by using the \f(CW\*(C`:utf8\*(C'\fR I/O layer), \fItell()\fR | |
6306 | will return byte offsets, not character offsets (because implementing | |
6307 | that would render \fIsysseek()\fR very slow). | |
6308 | .Sp | |
6309 | \&\fIsysseek()\fR bypasses normal buffered io, so mixing this with reads (other | |
6310 | than \f(CW\*(C`sysread\*(C'\fR, for example >< 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 | |
6313 | For \s-1WHENCE\s0, you may also use the constants \f(CW\*(C`SEEK_SET\*(C'\fR, \f(CW\*(C`SEEK_CUR\*(C'\fR, | |
6314 | and \f(CW\*(C`SEEK_END\*(C'\fR (start of the file, current position, end of the file) | |
6315 | from the Fcntl module. Use of the constants is also more portable | |
6316 | than 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 | |
6323 | Returns the new position, or the undefined value on failure. A position | |
6324 | of zero is returned as the string \f(CW"0 but true"\fR; thus \f(CW\*(C`sysseek\*(C'\fR returns | |
6325 | true on success and false on failure, yet you can still easily determine | |
6326 | the 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 | |
6333 | Does exactly the same thing as \f(CW\*(C`exec LIST\*(C'\fR, except that a fork is | |
6334 | done first, and the parent process waits for the child process to | |
6335 | complete. Note that argument processing varies depending on the | |
6336 | number of arguments. If there is more than one argument in \s-1LIST\s0, | |
6337 | or if \s-1LIST\s0 is an array with more than one value, starts the program | |
6338 | given by the first element of the list with arguments given by the | |
6339 | rest of the list. If there is only one scalar argument, the argument | |
6340 | is checked for shell metacharacters, and if there are any, the | |
6341 | entire 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 | |
6343 | platforms). If there are no shell metacharacters in the argument, | |
6344 | it is split into words and passed directly to \f(CW\*(C`execvp\*(C'\fR, which is | |
6345 | more efficient. | |
6346 | .Sp | |
6347 | Beginning with v5.6.0, Perl will attempt to flush all files opened for | |
6348 | output before any operation that may do a fork, but this may not be | |
6349 | supported on some platforms (see perlport). To be safe, you may need | |
6350 | to set \f(CW$|\fR ($AUTOFLUSH in English) or call the \f(CW\*(C`autoflush()\*(C'\fR method | |
6351 | of \f(CW\*(C`IO::Handle\*(C'\fR on any open handles. | |
6352 | .Sp | |
6353 | The 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). | |
6355 | See also \*(L"exec\*(R". This is \fInot\fR what you want to use to capture | |
6356 | the 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 | |
6358 | indicates a failure to start the program (inspect $! for the reason). | |
6359 | .Sp | |
6360 | Like \f(CW\*(C`exec\*(C'\fR, \f(CW\*(C`system\*(C'\fR allows you to lie to a program about its name if | |
6361 | you use the \f(CW\*(C`system PROGRAM LIST\*(C'\fR syntax. Again, see \*(L"exec\*(R". | |
6362 | .Sp | |
6363 | Because \f(CW\*(C`system\*(C'\fR and backticks block \f(CW\*(C`SIGINT\*(C'\fR and \f(CW\*(C`SIGQUIT\*(C'\fR, | |
6364 | killing the program they're running doesn't actually interrupt | |
6365 | your 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 | |
6373 | You 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 | |
6382 | or more portably by using the W*() calls of the \s-1POSIX\s0 extension; | |
6383 | see perlport for more information. | |
6384 | .Sp | |
6385 | When the arguments get executed via the system shell, results | |
6386 | and return codes will be subject to its quirks and capabilities. | |
6387 | See \*(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 | |
6396 | Attempts to write \s-1LENGTH\s0 characters of data from variable \s-1SCALAR\s0 to | |
6397 | the specified \s-1FILEHANDLE\s0, using the system call \fIwrite\fR\|(2). If \s-1LENGTH\s0 | |
6398 | is not specified, writes whole \s-1SCALAR\s0. It bypasses buffered \s-1IO\s0, so | |
6399 | mixing 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 | |
6401 | buffers 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 | |
6403 | available data in the \s-1SCALAR\s0 after the \s-1OFFSET\s0, only as much data as is | |
6404 | available will be written. | |
6405 | .Sp | |
6406 | An \s-1OFFSET\s0 may be specified to write the data from some part of the | |
6407 | string other than the beginning. A negative \s-1OFFSET\s0 specifies writing | |
6408 | that many characters counting backwards from the end of the string. | |
6409 | In the case the \s-1SCALAR\s0 is empty you can use \s-1OFFSET\s0 but only zero offset. | |
6410 | .Sp | |
6411 | Note the \fIcharacters\fR: depending on the status of the filehandle, | |
6412 | either (8\-bit) bytes or characters are written. By default all | |
6413 | filehandles operate on bytes, but for example if the filehandle has | |
6414 | been opened with the \f(CW\*(C`:utf8\*(C'\fR I/O layer (see \*(L"open\*(R", and the open | |
6415 | pragma, 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 | |
6422 | Returns the current position \fIin bytes\fR for \s-1FILEHANDLE\s0, or \-1 on | |
6423 | error. \s-1FILEHANDLE\s0 may be an expression whose value gives the name of | |
6424 | the actual filehandle. If \s-1FILEHANDLE\s0 is omitted, assumes the file | |
6425 | last read. | |
6426 | .Sp | |
6427 | Note the \fIin bytes\fR: even if the filehandle has been set to | |
6428 | operate on characters (for example by using the \f(CW\*(C`:utf8\*(C'\fR open | |
6429 | layer), \fItell()\fR will return byte offsets, not character offsets | |
6430 | (because that would render \fIseek()\fR and \fItell()\fR rather slow). | |
6431 | .Sp | |
6432 | The return value of \fItell()\fR for the standard streams like the \s-1STDIN\s0 | |
6433 | depends on the operating system: it may return \-1 or something else. | |
6434 | \&\fItell()\fR on pipes, fifos, and sockets usually returns \-1. | |
6435 | .Sp | |
6436 | There is no \f(CW\*(C`systell\*(C'\fR function. Use \f(CW\*(C`sysseek(FH, 0, 1)\*(C'\fR for that. | |
6437 | .Sp | |
6438 | Do 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 | |
6441 | buffered 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" | |
6445 | Returns the current position of the \f(CW\*(C`readdir\*(C'\fR routines on \s-1DIRHANDLE\s0. | |
6446 | Value may be given to \f(CW\*(C`seekdir\*(C'\fR to access a particular location in a | |
6447 | directory. Has the same caveats about possible directory compaction as | |
6448 | the corresponding system library routine. | |
6449 | .IP "tie \s-1VARIABLE\s0,CLASSNAME,LIST" 8 | |
6450 | .IX Item "tie VARIABLE,CLASSNAME,LIST" | |
6451 | This function binds a variable to a package class that will provide the | |
6452 | implementation for the variable. \s-1VARIABLE\s0 is the name of the variable | |
6453 | to be enchanted. \s-1CLASSNAME\s0 is the name of a class implementing objects | |
6454 | of correct type. Any additional arguments are passed to the \f(CW\*(C`new\*(C'\fR | |
6455 | method of the class (meaning \f(CW\*(C`TIESCALAR\*(C'\fR, \f(CW\*(C`TIEHANDLE\*(C'\fR, \f(CW\*(C`TIEARRAY\*(C'\fR, | |
6456 | or \f(CW\*(C`TIEHASH\*(C'\fR). Typically these are arguments such as might be passed | |
6457 | to the \f(CW\*(C`dbm_open()\*(C'\fR function of C. The object returned by the \f(CW\*(C`new\*(C'\fR | |
6458 | method is also returned by the \f(CW\*(C`tie\*(C'\fR function, which would be useful | |
6459 | if you want to access other methods in \s-1CLASSNAME\s0. | |
6460 | .Sp | |
6461 | Note that functions such as \f(CW\*(C`keys\*(C'\fR and \f(CW\*(C`values\*(C'\fR may return huge lists | |
6462 | when 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 | |
6475 | A 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 | |
6490 | A 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 | |
6509 | A 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 | |
6530 | A 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 | |
6540 | Not all methods indicated above need be implemented. See perltie, | |
6541 | Tie::Hash, Tie::Array, Tie::Scalar, and Tie::Handle. | |
6542 | .Sp | |
6543 | Unlike \f(CW\*(C`dbmopen\*(C'\fR, the \f(CW\*(C`tie\*(C'\fR function will not use or require a module | |
6544 | for you\*(--you need to do that explicitly yourself. See DB_File | |
6545 | or the \fIConfig\fR module for interesting \f(CW\*(C`tie\*(C'\fR implementations. | |
6546 | .Sp | |
6547 | For further details see perltie, \*(L"tied \s-1VARIABLE\s0\*(R". | |
6548 | .IP "tied \s-1VARIABLE\s0" 8 | |
6549 | .IX Item "tied VARIABLE" | |
6550 | Returns a reference to the object underlying \s-1VARIABLE\s0 (the same value | |
6551 | that was originally returned by the \f(CW\*(C`tie\*(C'\fR call that bound the variable | |
6552 | to a package.) Returns the undefined value if \s-1VARIABLE\s0 isn't tied to a | |
6553 | package. | |
6554 | .IP "time" 8 | |
6555 | .IX Item "time" | |
6556 | Returns the number of non-leap seconds since whatever time the system | |
6557 | considers to be the epoch (that's 00:00:00, January 1, 1904 for Mac \s-1OS\s0, | |
6558 | and 00:00:00 \s-1UTC\s0, January 1, 1970 for most other systems). | |
6559 | Suitable for feeding to \f(CW\*(C`gmtime\*(C'\fR and \f(CW\*(C`localtime\*(C'\fR. | |
6560 | .Sp | |
6561 | For measuring time in better granularity than one second, | |
6562 | you may use either the Time::HiRes module from \s-1CPAN\s0, or | |
6563 | if 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" | |
6567 | Returns a four-element list giving the user and system times, in | |
6568 | seconds, for this process and the children of this process. | |
6569 | .Sp | |
6570 | .Vb 1 | |
6571 | \& ($user,$system,$cuser,$csystem) = times; | |
6572 | .Ve | |
6573 | .Sp | |
6574 | In scalar context, \f(CW\*(C`times\*(C'\fR returns \f(CW$user\fR. | |
6575 | .IP "tr///" 8 | |
6576 | .IX Item "tr///" | |
6577 | The 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 | |
6584 | Truncates the file opened on \s-1FILEHANDLE\s0, or named by \s-1EXPR\s0, to the | |
6585 | specified length. Produces a fatal error if truncate isn't implemented | |
6586 | on your system. Returns true if successful, the undefined value | |
6587 | otherwise. | |
6588 | .Sp | |
6589 | The behavior is undefined if \s-1LENGTH\s0 is greater than the length of the | |
6590 | file. | |
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 | |
6597 | Returns an uppercased version of \s-1EXPR\s0. This is the internal function | |
6598 | implementing the \f(CW\*(C`\eU\*(C'\fR escape in double-quoted strings. Respects | |
6599 | current \s-1LC_CTYPE\s0 locale if \f(CW\*(C`use locale\*(C'\fR in force. See perllocale | |
6600 | and perlunicode for more details about locale and Unicode support. | |
6601 | It does not attempt to do titlecase mapping on initial letters. See | |
6602 | \&\f(CW\*(C`ucfirst\*(C'\fR for that. | |
6603 | .Sp | |
6604 | If \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 | |
6611 | Returns the value of \s-1EXPR\s0 with the first character in uppercase | |
6612 | (titlecase in Unicode). This is the internal function implementing | |
6613 | the \f(CW\*(C`\eu\*(C'\fR escape in double-quoted strings. Respects current \s-1LC_CTYPE\s0 | |
6614 | locale if \f(CW\*(C`use locale\*(C'\fR in force. See perllocale and perlunicode | |
6615 | for more details about locale and Unicode support. | |
6616 | .Sp | |
6617 | If \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 | |
6624 | Sets the umask for the process to \s-1EXPR\s0 and returns the previous value. | |
6625 | If \s-1EXPR\s0 is omitted, merely returns the current umask. | |
6626 | .Sp | |
6627 | The Unix permission \f(CW\*(C`rwxr\-x\-\-\-\*(C'\fR is represented as three sets of three | |
6628 | bits, or three octal digits: \f(CW0750\fR (the leading 0 indicates octal | |
6629 | and isn't one of the digits). The \f(CW\*(C`umask\*(C'\fR value is such a number | |
6630 | representing disabled permissions bits. The permission (or \*(L"mode\*(R") | |
6631 | values you pass \f(CW\*(C`mkdir\*(C'\fR or \f(CW\*(C`sysopen\*(C'\fR are modified by your umask, so | |
6632 | even if you tell \f(CW\*(C`sysopen\*(C'\fR to create a file with permissions \f(CW0777\fR, | |
6633 | if your umask is \f(CW0022\fR then the file will actually be created with | |
6634 | permissions \f(CW0755\fR. If your \f(CW\*(C`umask\*(C'\fR were \f(CW0027\fR (group can't | |
6635 | write; 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 &~ | |
6637 | 027\*(C'\fR is \f(CW0640\fR). | |
6638 | .Sp | |
6639 | Here's some advice: supply a creation mode of \f(CW0666\fR for regular | |
6640 | files (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 | |
6642 | choice: if they want protected files, they might choose process umasks | |
6643 | of \f(CW022\fR, \f(CW027\fR, or even the particularly antisocial mask of \f(CW077\fR. | |
6644 | Programs should rarely if ever make policy decisions better left to | |
6645 | the user. The exception to this is when writing files that should be | |
6646 | kept private: mail files, web browser cookies, \fI.rhosts\fR files, and | |
6647 | so on. | |
6648 | .Sp | |
6649 | If \fIumask\fR\|(2) is not implemented on your system and you are trying to | |
6650 | restrict access for \fIyourself\fR (i.e., (\s-1EXPR\s0 & 0700) > 0), produces a | |
6651 | fatal error at run time. If \fIumask\fR\|(2) is not implemented and you are | |
6652 | not trying to restrict access for yourself, returns \f(CW\*(C`undef\*(C'\fR. | |
6653 | .Sp | |
6654 | Remember that a umask is a number, usually given in octal; it is \fInot\fR a | |
6655 | string 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 | |
6662 | Undefines the value of \s-1EXPR\s0, which must be an lvalue. Use only on a | |
6663 | scalar 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 | |
6665 | will 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 | |
6667 | undefined value. You can omit the \s-1EXPR\s0, in which case nothing is | |
6668 | undefined, but you still get an undefined value that you could, for | |
6669 | instance, return from a subroutine, assign to a variable or pass as a | |
6670 | parameter. 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 | |
6684 | Note 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 | |
6691 | Deletes a list of files. Returns the number of files successfully | |
6692 | deleted. | |
6693 | .Sp | |
6694 | .Vb 3 | |
6695 | \& $cnt = unlink 'a', 'b', 'c'; | |
6696 | \& unlink @goners; | |
6697 | \& unlink <*.bak>; | |
6698 | .Ve | |
6699 | .Sp | |
6700 | Note: \f(CW\*(C`unlink\*(C'\fR will not delete directories unless you are superuser and | |
6701 | the \fB\-U\fR flag is supplied to Perl. Even if these conditions are | |
6702 | met, be warned that unlinking a directory can inflict damage on your | |
6703 | filesystem. Use \f(CW\*(C`rmdir\*(C'\fR instead. | |
6704 | .Sp | |
6705 | If \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 | |
6709 | and expands it out into a list of values. | |
6710 | (In scalar context, it returns merely the first value produced.) | |
6711 | .Sp | |
6712 | The string is broken into chunks described by the \s-1TEMPLATE\s0. Each chunk | |
6713 | is converted separately to a value. Typically, either the string is a result | |
6714 | of \f(CW\*(C`pack\*(C'\fR, or the bytes of the string represent a C structure of some | |
6715 | kind. | |
6716 | .Sp | |
6717 | The \s-1TEMPLATE\s0 has the same format as in the \f(CW\*(C`pack\*(C'\fR function. | |
6718 | Here'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 | |
6727 | and then there's | |
6728 | .Sp | |
6729 | .Vb 1 | |
6730 | \& sub ordinal { unpack("c",$_[0]); } # same as ord() | |
6731 | .Ve | |
6732 | .Sp | |
6733 | In addition to fields allowed in \fIpack()\fR, you may prefix a field with | |
6734 | a %<number> to indicate that | |
6735 | you want a <number>\-bit checksum of the items instead of the items | |
6736 | themselves. Default is a 16\-bit checksum. Checksum is calculated by | |
6737 | summing 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 | |
6740 | For example, the following | |
6741 | computes 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 | |
6750 | The 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 | |
6756 | The \f(CW\*(C`p\*(C'\fR and \f(CW\*(C`P\*(C'\fR formats should be used with care. Since Perl | |
6757 | has no way of checking whether the value passed to \f(CW\*(C`unpack()\*(C'\fR | |
6758 | corresponds to a valid memory location, passing a pointer value that's | |
6759 | not known to be valid is likely to have disastrous consequences. | |
6760 | .Sp | |
6761 | If the repeat count of a field is larger than what the remainder of | |
6762 | the input string allows, repeat count is decreased. If the input string | |
6763 | is longer than one described by the \s-1TEMPLATE\s0, the rest is ignored. | |
6764 | .Sp | |
6765 | See \*(L"pack\*(R" for more examples and notes. | |
6766 | .IP "untie \s-1VARIABLE\s0" 8 | |
6767 | .IX Item "untie VARIABLE" | |
6768 | Breaks the binding between a variable and a package. (See \f(CW\*(C`tie\*(C'\fR.) | |
6769 | Has no effect if the variable is not tied. | |
6770 | .IP "unshift \s-1ARRAY\s0,LIST" 8 | |
6771 | .IX Item "unshift ARRAY,LIST" | |
6772 | Does the opposite of a \f(CW\*(C`shift\*(C'\fR. Or the opposite of a \f(CW\*(C`push\*(C'\fR, | |
6773 | depending on how you look at it. Prepends list to the front of the | |
6774 | array, 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 | |
6780 | Note the \s-1LIST\s0 is prepended whole, not one element at a time, so the | |
6781 | prepended elements stay in the same order. Use \f(CW\*(C`reverse\*(C'\fR to do the | |
6782 | reverse. | |
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 | |
6795 | Imports some semantics into the current package from the named module, | |
6796 | generally by aliasing certain subroutine or variable names into your | |
6797 | package. It is exactly equivalent to | |
6798 | .Sp | |
6799 | .Vb 1 | |
6800 | \& BEGIN { require Module; import Module LIST; } | |
6801 | .Ve | |
6802 | .Sp | |
6803 | except 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 | |
6806 | compared to \f(CW$]\fR, or a literal of the form v5.6.1, which will be compared | |
6807 | to \f(CW$^V\fR (aka \f(CW$PERL_VERSION\fR. A fatal error is produced if \s-1VERSION\s0 is | |
6808 | greater than the version of the current Perl interpreter; Perl will not | |
6809 | attempt to parse the rest of the file. Compare with \*(L"require\*(R", which can | |
6810 | do a similar check at run time. | |
6811 | .Sp | |
6812 | Specifying \s-1VERSION\s0 as a literal of the form v5.6.1 should generally be | |
6813 | avoided, because it leads to misleading error messages under earlier | |
6814 | versions of Perl which do not support this syntax. The equivalent numeric | |
6815 | version 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 | |
6823 | This 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 | |
6825 | older versions of Perl. (We try not to do this more than we have to.) | |
6826 | .Sp | |
6827 | The \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 | |
6829 | yet. The \f(CW\*(C`import\*(C'\fR is not a builtin\*(--it's just an ordinary static method | |
6830 | call into the \f(CW\*(C`Module\*(C'\fR package to tell the module to import the list of | |
6831 | features 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 | |
6833 | derive their \f(CW\*(C`import\*(C'\fR method via inheritance from the \f(CW\*(C`Exporter\*(C'\fR class that | |
6834 | is defined in the \f(CW\*(C`Exporter\*(C'\fR module. See Exporter. If no \f(CW\*(C`import\*(C'\fR | |
6835 | method can be found then the call is skipped. | |
6836 | .Sp | |
6837 | If you do not want to call the package's \f(CW\*(C`import\*(C'\fR method (for instance, | |
6838 | to stop your namespace from being altered), explicitly supply the empty list: | |
6839 | .Sp | |
6840 | .Vb 1 | |
6841 | \& use Module (); | |
6842 | .Ve | |
6843 | .Sp | |
6844 | That is exactly equivalent to | |
6845 | .Sp | |
6846 | .Vb 1 | |
6847 | \& BEGIN { require Module } | |
6848 | .Ve | |
6849 | .Sp | |
6850 | If 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 | |
6852 | version as an argument. The default \s-1VERSION\s0 method, inherited from | |
6853 | the \s-1UNIVERSAL\s0 class, croaks if the given version is larger than the | |
6854 | value of the variable \f(CW$Module::VERSION\fR. | |
6855 | .Sp | |
6856 | Again, there is a distinction between omitting \s-1LIST\s0 (\f(CW\*(C`import\*(C'\fR called | |
6857 | with no arguments) and an explicit empty \s-1LIST\s0 \f(CW\*(C`()\*(C'\fR (\f(CW\*(C`import\*(C'\fR not | |
6858 | called). Note that there is no comma after \s-1VERSION\s0! | |
6859 | .Sp | |
6860 | Because this is a wide-open interface, pragmas (compiler directives) | |
6861 | are 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 | |
6874 | Some of these pseudo-modules import semantics into the current | |
6875 | block scope (like \f(CW\*(C`strict\*(C'\fR or \f(CW\*(C`integer\*(C'\fR, unlike ordinary modules, | |
6876 | which import symbols into the current package (which are effective | |
6877 | through the end of the file). | |
6878 | .Sp | |
6879 | There's a corresponding \f(CW\*(C`no\*(C'\fR command that unimports meanings imported | |
6880 | by \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 | |
6888 | See perlmodlib for a list of standard modules and pragmas. See perlrun | |
6889 | for 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 | |
6890 | functionality from the command\-line. | |
6891 | .IP "utime \s-1LIST\s0" 8 | |
6892 | .IX Item "utime LIST" | |
6893 | Changes the access and modification times on each file of a list of | |
6894 | files. The first two elements of the list must be the \s-1NUMERICAL\s0 access | |
6895 | and modification times, in that order. Returns the number of files | |
6896 | successfully changed. The inode change time of each file is set | |
6897 | to the current time. This code has the same effect as the \f(CW\*(C`touch\*(C'\fR | |
6898 | command 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 | |
6906 | If the first two elements of the list are \f(CW\*(C`undef\*(C'\fR, then the \fIutime\fR\|(2) | |
6907 | function in the C library will be called with a null second argument. | |
6908 | On most systems, this will set the file's access and modification | |
6909 | times 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" | |
6916 | Returns a list consisting of all the values of the named hash. (In a | |
6917 | scalar context, returns the number of values.) The values are | |
6918 | returned in an apparently random order. The actual random order is | |
6919 | subject to change in future versions of perl, but it is guaranteed to | |
6920 | be the same order as either the \f(CW\*(C`keys\*(C'\fR or \f(CW\*(C`each\*(C'\fR function would | |
6921 | produce on the same (unmodified) hash. | |
6922 | .Sp | |
6923 | Note that the values are not copied, which means modifying them will | |
6924 | modify 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 | |
6931 | As a side effect, calling \fIvalues()\fR resets the \s-1HASH\s0's internal iterator. | |
6932 | See 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" | |
6935 | Treats the string in \s-1EXPR\s0 as a bit vector made up of elements of | |
6936 | width \s-1BITS\s0, and returns the value of the element specified by \s-1OFFSET\s0 | |
6937 | as an unsigned integer. \s-1BITS\s0 therefore specifies the number of bits | |
6938 | that are reserved for each element in the bit vector. This must | |
6939 | be a power of two from 1 to 32 (or 64, if your platform supports | |
6940 | that). | |
6941 | .Sp | |
6942 | If \s-1BITS\s0 is 8, \*(L"elements\*(R" coincide with bytes of the input string. | |
6943 | .Sp | |
6944 | If \s-1BITS\s0 is 16 or more, bytes of the input string are grouped into chunks | |
6945 | of 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 | |
6947 | for BITS==64). See \*(L"pack\*(R" for details. | |
6948 | .Sp | |
6949 | If bits is 4 or less, the string is broken into bytes, then the bits | |
6950 | of each byte are broken into 8/BITS groups. Bits of a byte are | |
6951 | numbered 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, | |
6953 | breaking 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 | |
6957 | to 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 | |
6963 | If the selected element is outside the string, the value 0 is returned. | |
6964 | If an element off the end of the string is written to, Perl will first | |
6965 | extend the string with sufficiently many zero bytes. It is an error | |
6966 | to try to write off the beginning of the string (i.e. negative \s-1OFFSET\s0). | |
6967 | .Sp | |
6968 | The string should not contain any character with the value > 255 (which | |
6969 | can only happen if you're using \s-1UTF8\s0 encoding). If it does, it will be | |
6970 | treated as something which is not \s-1UTF8\s0 encoded. When the \f(CW\*(C`vec\*(C'\fR was | |
6971 | assigned to, other parts of your program will also no longer consider the | |
6972 | string to be \s-1UTF8\s0 encoded. In other words, if you do have such characters | |
6973 | in your string, \fIvec()\fR will operate on the actual byte string, and not the | |
6974 | conceptual character string. | |
6975 | .Sp | |
6976 | Strings created with \f(CW\*(C`vec\*(C'\fR can also be manipulated with the logical | |
6977 | operators \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 | |
6978 | vector operation is desired when both operands are strings. | |
6979 | See \*(L"Bitwise String Operators\*(R" in perlop. | |
6980 | .Sp | |
6981 | The following code will build up an \s-1ASCII\s0 string saying \f(CW'PerlPerlPerl'\fR. | |
6982 | The comments show the string after each step. Note that this code works | |
6983 | in 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 | |
7009 | To 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 | |
7016 | If you know the exact length in bits, it can be used in place of the \f(CW\*(C`*\*(C'\fR. | |
7017 | .Sp | |
7018 | Here 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 | |
7056 | Regardless of the machine architecture on which it is run, the above | |
7057 | example 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" | |
7194 | Behaves like the \fIwait\fR\|(2) system call on your system: it waits for a child | |
7195 | process 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. | |
7197 | Note that a return value of \f(CW\*(C`\-1\*(C'\fR could mean that child processes are | |
7198 | being automatically reaped, as described in perlipc. | |
7199 | .IP "waitpid \s-1PID\s0,FLAGS" 8 | |
7200 | .IX Item "waitpid PID,FLAGS" | |
7201 | Waits for a particular child process to terminate and returns the pid of | |
7202 | the deceased process, or \f(CW\*(C`\-1\*(C'\fR if there is no such child process. On some | |
7203 | systems, a value of 0 indicates that there are processes still running. | |
7204 | The 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 | |
7214 | then you can do a non-blocking wait for all pending zombie processes. | |
7215 | Non-blocking wait is available on machines supporting either the | |
7216 | \&\fIwaitpid\fR\|(2) or \fIwait4\fR\|(2) system calls. However, waiting for a particular | |
7217 | pid with \s-1FLAGS\s0 of \f(CW0\fR is implemented everywhere. (Perl emulates the | |
7218 | system call by remembering the status values of processes that have | |
7219 | exited but have not been harvested by the Perl script yet.) | |
7220 | .Sp | |
7221 | Note that on some systems, a return value of \f(CW\*(C`\-1\*(C'\fR could mean that child | |
7222 | processes are being automatically reaped. See perlipc for details, | |
7223 | and for other examples. | |
7224 | .IP "wantarray" 8 | |
7225 | .IX Item "wantarray" | |
7226 | Returns true if the context of the currently executing subroutine is | |
7227 | looking for a list value. Returns false if the context is looking | |
7228 | for a scalar. Returns the undefined value if the context is looking | |
7229 | for 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 | |
7237 | This function should have been named \fIwantlist()\fR instead. | |
7238 | .IP "warn \s-1LIST\s0" 8 | |
7239 | .IX Item "warn LIST" | |
7240 | Produces a message on \s-1STDERR\s0 just like \f(CW\*(C`die\*(C'\fR, but doesn't exit or throw | |
7241 | an exception. | |
7242 | .Sp | |
7243 | If \s-1LIST\s0 is empty and \f(CW$@\fR already contains a value (typically from a | |
7244 | previous eval) that value is used after appending \f(CW"\et...caught"\fR | |
7245 | to \f(CW$@\fR. This is useful for staying almost, but not entirely similar to | |
7246 | \&\f(CW\*(C`die\*(C'\fR. | |
7247 | .Sp | |
7248 | If \f(CW$@\fR is empty then the string \f(CW"Warning: Something's wrong"\fR is used. | |
7249 | .Sp | |
7250 | No message is printed if there is a \f(CW$SIG{_\|_WARN_\|_}\fR handler | |
7251 | installed. It is the handler's responsibility to deal with the message | |
7252 | as it sees fit (like, for instance, converting it into a \f(CW\*(C`die\*(C'\fR). Most | |
7253 | handlers must therefore make arrangements to actually display the | |
7254 | warnings that they are not prepared to deal with, by calling \f(CW\*(C`warn\*(C'\fR | |
7255 | again in the handler. Note that this is quite safe and will not | |
7256 | produce an endless loop, since \f(CW\*(C`_\|_WARN_\|_\*(C'\fR hooks are not called from | |
7257 | inside one. | |
7258 | .Sp | |
7259 | You 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 | |
7261 | instead call \f(CW\*(C`die\*(C'\fR again to change it). | |
7262 | .Sp | |
7263 | Using a \f(CW\*(C`_\|_WARN_\|_\*(C'\fR handler provides a powerful way to silence all | |
7264 | warnings (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 | |
7281 | See perlvar for details on setting \f(CW%SIG\fR entries, and for more | |
7282 | examples. 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 | |
7292 | Writes a formatted record (possibly multi\-line) to the specified \s-1FILEHANDLE\s0, | |
7293 | using the format associated with that file. By default the format for | |
7294 | a file is the one having the same name as the filehandle, but the | |
7295 | format for the current output channel (see the \f(CW\*(C`select\*(C'\fR function) may be set | |
7296 | explicitly by assigning the name of the format to the \f(CW$~\fR variable. | |
7297 | .Sp | |
7298 | Top of form processing is handled automatically: if there is | |
7299 | insufficient room on the current page for the formatted record, the | |
7300 | page is advanced by writing a form feed, a special top-of-page format | |
7301 | is used to format the new page header, and then the record is written. | |
7302 | By 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 | |
7304 | choice by assigning the name to the \f(CW$^\fR variable while the filehandle is | |
7305 | selected. The number of lines remaining on the current page is in | |
7306 | variable \f(CW\*(C`$\-\*(C'\fR, which can be set to \f(CW0\fR to force a new page. | |
7307 | .Sp | |
7308 | If \s-1FILEHANDLE\s0 is unspecified, output goes to the current default output | |
7309 | channel, 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 | |
7311 | is evaluated and the resulting string is used to look up the name of | |
7312 | the \s-1FILEHANDLE\s0 at run time. For more on formats, see perlform. | |
7313 | .Sp | |
7314 | Note that write is \fInot\fR the opposite of \f(CW\*(C`read\*(C'\fR. Unfortunately. | |
7315 | .IP "y///" 8 | |
7316 | .IX Item "y///" | |
7317 | The transliteration operator. Same as \f(CW\*(C`tr///\*(C'\fR. See perlop. |