perlfunc >
5.10.1
との差分
perlfunc 5.10.1 と 5.10.0 の差分
1 | 1 | |
2 | 2 | =encoding euc-jp |
3 | 3 | |
4 | 4 | =head1 NAME |
5 | 5 | X<function> |
6 | 6 | |
7 | 7 | =begin original |
8 | 8 | |
9 | 9 | perlfunc - Perl builtin functions |
10 | 10 | |
11 | 11 | =end original |
12 | 12 | |
13 | 13 | perlfunc - Perl 組み込み関数 |
14 | 14 | |
15 | 15 | =head1 DESCRIPTION |
16 | 16 | |
17 | 17 | =begin original |
18 | 18 | |
19 | 19 | The functions in this section can serve as terms in an expression. |
20 | 20 | They fall into two major categories: list operators and named unary |
21 | 21 | operators. These differ in their precedence relationship with a |
22 | 22 | following comma. (See the precedence table in L<perlop>.) List |
23 | 23 | operators take more than one argument, while unary operators can never |
24 | 24 | take more than one argument. Thus, a comma terminates the argument of |
25 | 25 | a unary operator, but merely separates the arguments of a list |
26 | 26 | operator. A unary operator generally provides a scalar context to its |
27 | 27 | argument, while a list operator may provide either scalar or list |
28 | 28 | contexts for its arguments. If it does both, the scalar arguments will |
29 | 29 | be first, and the list argument will follow. (Note that there can ever |
30 | 30 | be only one such list argument.) For instance, splice() has three scalar |
31 | 31 | arguments followed by a list, whereas gethostbyname() has four scalar |
32 | 32 | arguments. |
33 | 33 | |
34 | 34 | =end original |
35 | 35 | |
36 | 36 | この節の関数は、式の中で項として使うことができます。 |
37 | 37 | これらは、大きく 2 つに分けられます: |
38 | 38 | リスト演算子と名前付き単項演算子です。 |
39 | 39 | これらの違いは、その後に出て来るコンマとの優先順位の関係にあります。 |
40 | 40 | (L<perlop> の優先順位の表を参照してください。) |
41 | 41 | リスト演算子は 2 個以上の引数をとるのに対して、 |
42 | 42 | 単項演算子が複数の引数をとることはありません。 |
43 | 43 | つまり、コンマは単項演算子の引数の終わりとなりますが、 |
44 | 44 | リスト演算子の場合には、引数の区切りでしかありません。 |
45 | 45 | 単項演算子は一般に、 |
46 | 46 | 引数に対してスカラコンテキストを与えるのに対して、 |
47 | 47 | スカラ演算子の場合には、引数に対してスカラコンテキストを与える場合も、 |
48 | 48 | リストコンテキストを与える場合もあります。 |
49 | 49 | 1 つのリスト演算子が |
50 | 50 | 両方のコンテキストを与える場合には、スカラ引数がいくつか並び、 |
51 | 51 | 最後にリスト引数が 1 つ続きます。 |
52 | 52 | (リスト引数は 1 つだけです。) |
53 | 53 | たとえば、splice() は 3 つのスカラ引数に 1 つのリスト引数が続きます。 |
54 | 54 | 一方 gethostbyname() は 4 つのスカラ引数を持ちます。 |
55 | 55 | |
56 | 56 | =begin original |
57 | 57 | |
58 | 58 | In the syntax descriptions that follow, list operators that expect a |
59 | 59 | list (and provide list context for the elements of the list) are shown |
60 | 60 | with LIST as an argument. Such a list may consist of any combination |
61 | 61 | of scalar arguments or list values; the list values will be included |
62 | 62 | in the list as if each individual element were interpolated at that |
63 | 63 | point in the list, forming a longer single-dimensional list value. |
64 | 64 | Commas should separate elements of the LIST. |
65 | 65 | |
66 | 66 | =end original |
67 | 67 | |
68 | 68 | 後に載せる構文記述では、リストをとり |
69 | 69 | (そのリストの要素にリストコンテキストを与える) |
70 | 70 | リスト演算子は、引数として LIST をとるように書いています。 |
71 | 71 | そのようなリストには、任意のスカラ引数の組み合わせやリスト値を |
72 | 72 | 含めることができ、リスト値はリストの中に、 |
73 | 73 | 個々の要素が展開されたように埋め込まれます。 |
74 | 74 | 1 次元の長いリスト値が形成されることになります。 |
75 | 75 | LIST の要素は、コンマで区切られます。 |
76 | 76 | |
77 | 77 | =begin original |
78 | 78 | |
79 | 79 | Any function in the list below may be used either with or without |
80 | 80 | parentheses around its arguments. (The syntax descriptions omit the |
81 | 81 | parentheses.) If you use the parentheses, the simple (but occasionally |
82 | 82 | surprising) rule is this: It I<looks> like a function, therefore it I<is> a |
83 | 83 | function, and precedence doesn't matter. Otherwise it's a list |
84 | 84 | operator or unary operator, and precedence does matter. And whitespace |
85 | 85 | between the function and left parenthesis doesn't count--so you need to |
86 | 86 | be careful sometimes: |
87 | 87 | |
88 | 88 | =end original |
89 | 89 | |
90 | 90 | 以下のリストの関数はすべて、引数の前後の括弧は省略可能と |
91 | 91 | なっています。 |
92 | 92 | (構文記述では省略しています。) |
93 | 93 | 括弧を使うときには、 |
94 | 94 | 単純な (しかし、ときには驚く結果となる) 規則が適用できます: |
95 | 95 | I<関数に見える>ならば、I<それは関数>で、優先順位は関係ありません。 |
96 | 96 | そう見えなければ、それはリスト演算子か単項演算子で、優先順位が |
97 | 97 | 関係します。 |
98 | 98 | また、関数と開き括弧の間の空白は関係ありませんので、 |
99 | 99 | ときに気を付けなければなりません: |
100 | 100 | |
101 | 101 | print 1+2+4; # Prints 7. |
102 | 102 | print(1+2) + 4; # Prints 3. |
103 | 103 | print (1+2)+4; # Also prints 3! |
104 | 104 | print +(1+2)+4; # Prints 7. |
105 | 105 | print ((1+2)+4); # Prints 7. |
106 | 106 | |
107 | 107 | =begin original |
108 | 108 | |
109 | 109 | If you run Perl with the B<-w> switch it can warn you about this. For |
110 | 110 | example, the third line above produces: |
111 | 111 | |
112 | 112 | =end original |
113 | 113 | |
114 | 114 | Perl に B<-w> スイッチを付けて実行すれば、こういったものには |
115 | 115 | 警告を出してくれます。 |
116 | 116 | たとえば、上記の 3 つめは、以下のような警告が出ます: |
117 | 117 | |
118 | 118 | print (...) interpreted as function at - line 1. |
119 | 119 | Useless use of integer addition in void context at - line 1. |
120 | 120 | |
121 | 121 | =begin original |
122 | 122 | |
123 | 123 | A few functions take no arguments at all, and therefore work as neither |
124 | 124 | unary nor list operators. These include such functions as C<time> |
125 | 125 | and C<endpwent>. For example, C<time+86_400> always means |
126 | 126 | C<time() + 86_400>. |
127 | 127 | |
128 | 128 | =end original |
129 | 129 | |
130 | 130 | いくつかの関数は引数を全くとらないので、単項演算子としても |
131 | 131 | リスト演算子としても動作しません。 |
132 | 132 | このような関数としては C<time> や C<endpwent> があります。 |
133 | 133 | 例えば、C<time+86_400> は常に C<time() + 86_400> として扱われます。 |
134 | 134 | |
135 | 135 | =begin original |
136 | 136 | |
137 | 137 | For functions that can be used in either a scalar or list context, |
138 | 138 | nonabortive failure is generally indicated in a scalar context by |
139 | 139 | returning the undefined value, and in a list context by returning the |
140 | 140 | null list. |
141 | 141 | |
142 | 142 | =end original |
143 | 143 | |
144 | 144 | スカラコンテキストでも、リストコンテキストでも使える関数は、 |
145 | 145 | 致命的でないエラーを示すために、スカラコンテキストでは |
146 | 146 | 未定義値を返し、リストコンテキストでは空リストを返します。 |
147 | 147 | |
148 | 148 | =begin original |
149 | 149 | |
150 | 150 | Remember the following important rule: There is B<no rule> that relates |
151 | 151 | the behavior of an expression in list context to its behavior in scalar |
152 | 152 | context, or vice versa. It might do two totally different things. |
153 | 153 | Each operator and function decides which sort of value it would be most |
154 | 154 | appropriate to return in scalar context. Some operators return the |
155 | 155 | length of the list that would have been returned in list context. Some |
156 | 156 | operators return the first value in the list. Some operators return the |
157 | 157 | last value in the list. Some operators return a count of successful |
158 | 158 | operations. In general, they do what you want, unless you want |
159 | 159 | consistency. |
160 | 160 | X<context> |
161 | 161 | |
162 | 162 | =end original |
163 | 163 | |
164 | 164 | 以下に述べる重要なルールを忘れないで下さい: リストコンテキストでの |
165 | 165 | 振る舞いとスカラコンテキストでの振る舞いの関係、あるいはその逆に |
166 | 166 | B<ルールはありません>。 |
167 | 167 | 2 つの全く異なったことがあります。 |
168 | 168 | それぞれの演算子と関数は、スカラコンテキストでは、もっとも適切と |
169 | 169 | 思われる値を返します。 |
170 | 170 | リストコンテキストで返す時のリストの長さを返す演算子もあります。 |
171 | 171 | リストの最初の値を返す演算子もあります。 |
172 | 172 | リストの最後の値を返す演算子もあります。 |
173 | 173 | 成功した操作の数を返す演算子もあります。 |
174 | 174 | 一般的には、一貫性を求めない限り、こちらが求めることをします。 |
175 | 175 | X<context> |
176 | 176 | |
177 | 177 | =begin original |
178 | 178 | |
179 | 179 | A named array in scalar context is quite different from what would at |
180 | 180 | first glance appear to be a list in scalar context. You can't get a list |
181 | 181 | like C<(1,2,3)> into being in scalar context, because the compiler knows |
182 | 182 | the context at compile time. It would generate the scalar comma operator |
183 | 183 | there, not the list construction version of the comma. That means it |
184 | 184 | was never a list to start with. |
185 | 185 | |
186 | 186 | =end original |
187 | 187 | |
188 | 188 | スカラコンテキストでの名前付き配列は、スカラコンテキストでのリストを |
189 | 189 | 一目見たものとは全く違います。 |
190 | 190 | コンパイラはコンパイル時にコンテキストを知っているので、 |
191 | 191 | C<(1,2,3)> のようなリストをスカラコンテキストで得ることはできません。 |
192 | 192 | これはスカラコンマ演算子を生成し、コンマのリスト作成版ではありません。 |
193 | 193 | これは初めからリストであることはないことを意味します。 |
194 | 194 | |
195 | 195 | =begin original |
196 | 196 | |
197 | 197 | In general, functions in Perl that serve as wrappers for system calls |
198 | 198 | of the same name (like chown(2), fork(2), closedir(2), etc.) all return |
199 | 199 | true when they succeed and C<undef> otherwise, as is usually mentioned |
200 | 200 | in the descriptions below. This is different from the C interfaces, |
201 | 201 | which return C<-1> on failure. Exceptions to this rule are C<wait>, |
202 | 202 | C<waitpid>, and C<syscall>. System calls also set the special C<$!> |
203 | 203 | variable on failure. Other functions do not, except accidentally. |
204 | 204 | |
205 | 205 | =end original |
206 | 206 | |
207 | 207 | 一般的に、同じ名前のシステムコールのラッパーとして動作する Perl の関数 |
208 | 208 | (chown(2), fork(2), closedir(2) など)は、以下に述べるように、 |
209 | 209 | 成功時に真を返し、そうでなければ C<undef> を返します。 |
210 | 210 | これは失敗時に C<-1> を返す C のインターフェースとは違います。 |
211 | 211 | このルールの例外は C<wait>, C<waitpid>, C<syscall> です。 |
212 | 212 | システムコールは失敗時に特殊変数 C<$!> をセットします。 |
213 | 213 | その他の関数は、事故を除いて、セットしません。 |
214 | 214 | |
215 | 215 | =head2 Perl Functions by Category |
216 | 216 | X<function> |
217 | 217 | |
218 | 218 | (カテゴリ別の Perl 関数) |
219 | 219 | |
220 | 220 | =begin original |
221 | 221 | |
222 | 222 | Here are Perl's functions (including things that look like |
223 | 223 | functions, like some keywords and named operators) |
224 | 224 | arranged by category. Some functions appear in more |
225 | 225 | than one place. |
226 | 226 | |
227 | 227 | =end original |
228 | 228 | |
229 | 229 | 以下に、カテゴリ別の関数(キーワードや名前付き演算子のような、 |
230 | 230 | 関数のように見えるものも含みます)を示します。 |
231 | 231 | 複数の場所に現れる関数もあります。 |
232 | 232 | |
233 | 233 | =over 4 |
234 | 234 | |
235 | 235 | =item Functions for SCALARs or strings |
236 | 236 | X<scalar> X<string> X<character> |
237 | 237 | |
238 | 238 | (スカラや文字列のための関数) |
239 | 239 | |
240 | 240 | C<chomp>, C<chop>, C<chr>, C<crypt>, C<hex>, C<index>, C<lc>, C<lcfirst>, |
241 | 241 | C<length>, C<oct>, C<ord>, C<pack>, C<q//>, C<qq//>, C<reverse>, |
242 | 242 | C<rindex>, C<sprintf>, C<substr>, C<tr///>, C<uc>, C<ucfirst>, C<y///> |
243 | 243 | |
244 | 244 | =item Regular expressions and pattern matching |
245 | 245 | X<regular expression> X<regex> X<regexp> |
246 | 246 | |
247 | 247 | (正規表現とパターンマッチング) |
248 | 248 | |
249 | 249 | C<m//>, C<pos>, C<quotemeta>, C<s///>, C<split>, C<study>, C<qr//> |
250 | 250 | |
251 | 251 | =item Numeric functions |
252 | 252 | X<numeric> X<number> X<trigonometric> X<trigonometry> |
253 | 253 | |
254 | 254 | (数値関数) |
255 | 255 | |
256 | 256 | C<abs>, C<atan2>, C<cos>, C<exp>, C<hex>, C<int>, C<log>, C<oct>, C<rand>, |
257 | 257 | C<sin>, C<sqrt>, C<srand> |
258 | 258 | |
259 | 259 | =item Functions for real @ARRAYs |
260 | 260 | X<array> |
261 | 261 | |
262 | 262 | (実配列のための関数) |
263 | 263 | |
264 | 264 | C<pop>, C<push>, C<shift>, C<splice>, C<unshift> |
265 | 265 | |
266 | 266 | =item Functions for list data |
267 | 267 | X<list> |
268 | 268 | |
269 | 269 | (リストデータのための関数) |
270 | 270 | |
271 | 271 | C<grep>, C<join>, C<map>, C<qw//>, C<reverse>, C<sort>, C<unpack> |
272 | 272 | |
273 | 273 | =item Functions for real %HASHes |
274 | 274 | X<hash> |
275 | 275 | |
276 | 276 | (実ハッシュのための関数) |
277 | 277 | |
278 | 278 | C<delete>, C<each>, C<exists>, C<keys>, C<values> |
279 | 279 | |
280 | 280 | =item Input and output functions |
281 | 281 | X<I/O> X<input> X<output> X<dbm> |
282 | 282 | |
283 | 283 | (入出力関数) |
284 | 284 | |
285 | 285 | C<binmode>, C<close>, C<closedir>, C<dbmclose>, C<dbmopen>, C<die>, C<eof>, |
286 | 286 | C<fileno>, C<flock>, C<format>, C<getc>, C<print>, C<printf>, C<read>, |
287 | 287 | C<readdir>, C<rewinddir>, C<say>, C<seek>, C<seekdir>, C<select>, C<syscall>, |
288 | 288 | C<sysread>, C<sysseek>, C<syswrite>, C<tell>, C<telldir>, C<truncate>, |
289 | 289 | C<warn>, C<write> |
290 | 290 | |
291 | 291 | =item Functions for fixed length data or records |
292 | 292 | |
293 | 293 | (固定長データやレコードのための関数) |
294 | 294 | |
295 | 295 | C<pack>, C<read>, C<syscall>, C<sysread>, C<syswrite>, C<unpack>, C<vec> |
296 | 296 | |
297 | 297 | =item Functions for filehandles, files, or directories |
298 | 298 | X<file> X<filehandle> X<directory> X<pipe> X<link> X<symlink> |
299 | 299 | |
300 | 300 | (ファイルハンドル、ファイル、ディレクトリのための関数) |
301 | 301 | |
302 | 302 | C<-I<X>>, C<chdir>, C<chmod>, C<chown>, C<chroot>, C<fcntl>, C<glob>, |
303 | 303 | C<ioctl>, C<link>, C<lstat>, C<mkdir>, C<open>, C<opendir>, |
304 | 304 | C<readlink>, C<rename>, C<rmdir>, C<stat>, C<symlink>, C<sysopen>, |
305 | 305 | C<umask>, C<unlink>, C<utime> |
306 | 306 | |
307 | 307 | =item Keywords related to the control flow of your Perl program |
308 | 308 | X<control flow> |
309 | 309 | |
310 | 310 | (プログラムの流れを制御することに関連するキーワード) |
311 | 311 | |
312 | 312 | C<caller>, C<continue>, C<die>, C<do>, C<dump>, C<eval>, C<exit>, |
313 | 313 | C<goto>, C<last>, C<next>, C<redo>, C<return>, C<sub>, C<wantarray> |
314 | 314 | |
315 | 315 | =item Keywords related to switch |
316 | 316 | |
317 | 317 | (switch に関連するキーワード) |
318 | 318 | |
319 | 319 | C<break>, C<continue>, C<given>, C<when>, C<default> |
320 | 320 | |
321 | 321 | =begin original |
322 | 322 | |
323 | 323 | (These are only available if you enable the "switch" feature. |
324 | 324 | See L<feature> and L<perlsyn/"Switch statements">.) |
325 | 325 | |
326 | 326 | =end original |
327 | 327 | |
328 | 328 | (これらは "switch" 機能が有効の場合にのみ利用可能です。 |
329 | 329 | L<feature> と L<perlsyn/"Switch statements"> を参照してください。) |
330 | 330 | |
331 | 331 | =item Keywords related to scoping |
332 | 332 | |
333 | 333 | (スコープに関するキーワード) |
334 | 334 | |
335 | 335 | C<caller>, C<import>, C<local>, C<my>, C<our>, C<state>, C<package>, |
336 | 336 | C<use> |
337 | 337 | |
338 | 338 | =begin original |
339 | 339 | |
340 | 340 | (C<state> is only available if the "state" feature is enabled. See |
341 | 341 | L<feature>.) |
342 | 342 | |
343 | 343 | =end original |
344 | 344 | |
345 | 345 | (C<state> は "state" 機能が有効の場合にのみ利用可能です。 |
346 | 346 | L<feature> を参照してください。) |
347 | 347 | |
348 | 348 | =item Miscellaneous functions |
349 | 349 | |
350 | 350 | (さまざまな関数) |
351 | 351 | |
352 | 352 | C<defined>, C<dump>, C<eval>, C<formline>, C<local>, C<my>, C<our>, |
353 | 353 | C<reset>, C<scalar>, C<state>, C<undef>, C<wantarray> |
354 | 354 | |
355 | 355 | =item Functions for processes and process groups |
356 | 356 | X<process> X<pid> X<process id> |
357 | 357 | |
358 | 358 | (プロセスとプロセスグループのための関数) |
359 | 359 | |
360 | 360 | C<alarm>, C<exec>, C<fork>, C<getpgrp>, C<getppid>, C<getpriority>, C<kill>, |
361 | 361 | C<pipe>, C<qx//>, C<setpgrp>, C<setpriority>, C<sleep>, C<system>, |
362 | 362 | C<times>, C<wait>, C<waitpid> |
363 | 363 | |
364 | 364 | =item Keywords related to perl modules |
365 | 365 | X<module> |
366 | 366 | |
367 | 367 | (perl モジュールに関するキーワード) |
368 | 368 | |
369 | 369 | C<do>, C<import>, C<no>, C<package>, C<require>, C<use> |
370 | 370 | |
371 | 371 | =item Keywords related to classes and object-orientation |
372 | 372 | X<object> X<class> X<package> |
373 | 373 | |
374 | 374 | (クラスとオブジェクト指向に関するキーワード) |
375 | 375 | |
376 | 376 | C<bless>, C<dbmclose>, C<dbmopen>, C<package>, C<ref>, C<tie>, C<tied>, |
377 | 377 | C<untie>, C<use> |
378 | 378 | |
379 | 379 | =item Low-level socket functions |
380 | 380 | X<socket> X<sock> |
381 | 381 | |
382 | 382 | (低レベルソケット関数) |
383 | 383 | |
384 | 384 | C<accept>, C<bind>, C<connect>, C<getpeername>, C<getsockname>, |
385 | 385 | C<getsockopt>, C<listen>, C<recv>, C<send>, C<setsockopt>, C<shutdown>, |
386 | 386 | C<socket>, C<socketpair> |
387 | 387 | |
388 | 388 | =item System V interprocess communication functions |
389 | 389 | X<IPC> X<System V> X<semaphore> X<shared memory> X<memory> X<message> |
390 | 390 | |
391 | 391 | (System V プロセス間通信関数) |
392 | 392 | |
393 | 393 | C<msgctl>, C<msgget>, C<msgrcv>, C<msgsnd>, C<semctl>, C<semget>, C<semop>, |
394 | 394 | C<shmctl>, C<shmget>, C<shmread>, C<shmwrite> |
395 | 395 | |
396 | 396 | =item Fetching user and group info |
397 | 397 | X<user> X<group> X<password> X<uid> X<gid> X<passwd> X</etc/passwd> |
398 | 398 | |
399 | 399 | (ユーザーとグループの情報取得) |
400 | 400 | |
401 | 401 | C<endgrent>, C<endhostent>, C<endnetent>, C<endpwent>, C<getgrent>, |
402 | 402 | C<getgrgid>, C<getgrnam>, C<getlogin>, C<getpwent>, C<getpwnam>, |
403 | 403 | C<getpwuid>, C<setgrent>, C<setpwent> |
404 | 404 | |
405 | 405 | =item Fetching network info |
406 | 406 | X<network> X<protocol> X<host> X<hostname> X<IP> X<address> X<service> |
407 | 407 | |
408 | 408 | (ネットワーク情報取得) |
409 | 409 | |
410 | 410 | C<endprotoent>, C<endservent>, C<gethostbyaddr>, C<gethostbyname>, |
411 | 411 | C<gethostent>, C<getnetbyaddr>, C<getnetbyname>, C<getnetent>, |
412 | 412 | C<getprotobyname>, C<getprotobynumber>, C<getprotoent>, |
413 | 413 | C<getservbyname>, C<getservbyport>, C<getservent>, C<sethostent>, |
414 | 414 | C<setnetent>, C<setprotoent>, C<setservent> |
415 | 415 | |
416 | 416 | =item Time-related functions |
417 | 417 | X<time> X<date> |
418 | 418 | |
419 | 419 | (時刻に関する関数) |
420 | 420 | |
421 | 421 | C<gmtime>, C<localtime>, C<time>, C<times> |
422 | 422 | |
423 | 423 | =item Functions new in perl5 |
424 | 424 | X<perl5> |
425 | 425 | |
426 | 426 | (perl5 で新設された関数) |
427 | 427 | |
428 | 428 | C<abs>, C<bless>, C<break>, C<chomp>, C<chr>, C<continue>, C<default>, |
429 | 429 | C<exists>, C<formline>, C<given>, C<glob>, C<import>, C<lc>, C<lcfirst>, |
430 | 430 | C<lock>, C<map>, C<my>, C<no>, C<our>, C<prototype>, C<qr//>, C<qw//>, C<qx//>, |
431 | 431 | C<readline>, C<readpipe>, C<ref>, C<sub>*, C<sysopen>, C<tie>, C<tied>, C<uc>, |
432 | 432 | C<ucfirst>, C<untie>, C<use>, C<when> |
433 | 433 | |
434 | 434 | =begin original |
435 | 435 | |
436 | 436 | * - C<sub> was a keyword in perl4, but in perl5 it is an |
437 | 437 | operator, which can be used in expressions. |
438 | 438 | |
439 | 439 | =end original |
440 | 440 | |
441 | 441 | * - C<sub> は perl4 ではキーワードですが、perl5 では演算子なので、 |
442 | 442 | 式で使えます。 |
443 | 443 | |
444 | 444 | =item Functions obsoleted in perl5 |
445 | 445 | |
446 | 446 | (perl5 では古いものとなった関数) |
447 | 447 | |
448 | 448 | C<dbmclose>, C<dbmopen> |
449 | 449 | |
450 | 450 | =back |
451 | 451 | |
452 | 452 | =head2 Portability |
453 | 453 | X<portability> X<Unix> X<portable> |
454 | 454 | |
455 | 455 | (移植性) |
456 | 456 | |
457 | 457 | =begin original |
458 | 458 | |
459 | 459 | Perl was born in Unix and can therefore access all common Unix |
460 | 460 | system calls. In non-Unix environments, the functionality of some |
461 | 461 | Unix system calls may not be available, or details of the available |
462 | 462 | functionality may differ slightly. The Perl functions affected |
463 | 463 | by this are: |
464 | 464 | |
465 | 465 | =end original |
466 | 466 | |
467 | 467 | Perl は Unix 環境で生まれたので、全ての共通する Unix システムコールに |
468 | 468 | アクセスします。非 Unix 環境では、いくつかの Unix システムコールの |
469 | 469 | 機能が使えなかったり、使える機能の詳細が多少異なったりします。 |
470 | 470 | これによる影響を受ける Perl 関数は以下のものです: |
471 | 471 | |
472 | 472 | C<-X>, C<binmode>, C<chmod>, C<chown>, C<chroot>, C<crypt>, |
473 | 473 | C<dbmclose>, C<dbmopen>, C<dump>, C<endgrent>, C<endhostent>, |
474 | 474 | C<endnetent>, C<endprotoent>, C<endpwent>, C<endservent>, C<exec>, |
475 | 475 | C<fcntl>, C<flock>, C<fork>, C<getgrent>, C<getgrgid>, C<gethostbyname>, |
476 | 476 | C<gethostent>, C<getlogin>, C<getnetbyaddr>, C<getnetbyname>, C<getnetent>, |
477 | 477 | C<getppid>, C<getpgrp>, C<getpriority>, C<getprotobynumber>, |
478 | 478 | C<getprotoent>, C<getpwent>, C<getpwnam>, C<getpwuid>, |
479 | 479 | C<getservbyport>, C<getservent>, C<getsockopt>, C<glob>, C<ioctl>, |
480 | 480 | C<kill>, C<link>, C<lstat>, C<msgctl>, C<msgget>, C<msgrcv>, |
481 | 481 | C<msgsnd>, C<open>, C<pipe>, C<readlink>, C<rename>, C<select>, C<semctl>, |
482 | 482 | C<semget>, C<semop>, C<setgrent>, C<sethostent>, C<setnetent>, |
483 | 483 | C<setpgrp>, C<setpriority>, C<setprotoent>, C<setpwent>, |
484 | 484 | C<setservent>, C<setsockopt>, C<shmctl>, C<shmget>, C<shmread>, |
485 | 485 | C<shmwrite>, C<socket>, C<socketpair>, |
486 | 486 | C<stat>, C<symlink>, C<syscall>, C<sysopen>, C<system>, |
487 | 487 | C<times>, C<truncate>, C<umask>, C<unlink>, |
488 | 488 | C<utime>, C<wait>, C<waitpid> |
489 | 489 | |
490 | 490 | =begin original |
491 | 491 | |
492 | 492 | For more information about the portability of these functions, see |
493 | 493 | L<perlport> and other available platform-specific documentation. |
494 | 494 | |
495 | 495 | =end original |
496 | 496 | |
497 | 497 | これらの関数の移植性に関するさらなる情報については、 |
498 | 498 | L<perlport> とその他のプラットホーム固有のドキュメントを参照して下さい。 |
499 | 499 | |
500 | 500 | =head2 Alphabetical Listing of Perl Functions |
501 | 501 | |
502 | 502 | =over 8 |
503 | 503 | |
504 | 504 | =item -X FILEHANDLE |
505 | 505 | X<-r>X<-w>X<-x>X<-o>X<-R>X<-W>X<-X>X<-O>X<-e>X<-z>X<-s>X<-f>X<-d>X<-l>X<-p> |
506 | 506 | X<-S>X<-b>X<-c>X<-t>X<-u>X<-g>X<-k>X<-T>X<-B>X<-M>X<-A>X<-C> |
507 | 507 | |
508 | 508 | =item -X EXPR |
509 | 509 | |
510 | 510 | =item -X DIRHANDLE |
511 | 511 | |
512 | 512 | =item -X |
513 | 513 | |
514 | 514 | =begin original |
515 | 515 | |
516 | 516 | A file test, where X is one of the letters listed below. This unary |
517 | 517 | operator takes one argument, either a filename, a filehandle, or a dirhandle, |
518 | 518 | and tests the associated file to see if something is true about it. If the |
519 | 519 | argument is omitted, tests C<$_>, except for C<-t>, which tests STDIN. |
520 | 520 | Unless otherwise documented, it returns C<1> for true and C<''> for false, or |
521 | 521 | the undefined value if the file doesn't exist. Despite the funny |
522 | 522 | names, precedence is the same as any other named unary operator. The |
523 | 523 | operator may be any of: |
524 | 524 | |
525 | 525 | =end original |
526 | 526 | |
527 | 527 | X は以下にあげる文字で、ファイルテストを行ないます。 |
528 | 528 | この単項演算子は、ファイル名かファイルハンドルを唯一の |
529 | 529 | 引数として動作し、「あること」について真であるか否かを |
530 | 530 | 判定した結果を返します。 |
531 | 531 | 引数が省略されると、C<-t> では STDIN を調べますが、その他は C<$_> を調べます。 |
532 | 532 | 特に記述されていなければ、真として C<1> を返し、偽として |
533 | 533 | C<''> を返し、ファイルが存在しなければ、未定義値を返します。 |
534 | 534 | みかけは変わっていますが、優先順位は名前付き単項演算子と同じで、 |
535 | 535 | 他の単項演算子と同じく、引数を括弧で括ることもできます。 |
536 | 536 | 演算子には以下のものがあります: |
537 | 537 | |
538 | 538 | =begin original |
539 | 539 | |
540 | 540 | -r File is readable by effective uid/gid. |
541 | 541 | -w File is writable by effective uid/gid. |
542 | 542 | -x File is executable by effective uid/gid. |
543 | 543 | -o File is owned by effective uid. |
544 | 544 | |
545 | 545 | =end original |
546 | 546 | |
547 | 547 | -r ファイルが実効 uid/gid で読み出し可 |
548 | 548 | -w ファイルが実効 uid/gid で書き込み可 |
549 | 549 | -x ファイルが実効 uid/gid で実行可 |
550 | 550 | -o ファイルが実効 uid の所有物 |
551 | 551 | |
552 | 552 | =begin original |
553 | 553 | |
554 | 554 | -R File is readable by real uid/gid. |
555 | 555 | -W File is writable by real uid/gid. |
556 | 556 | -X File is executable by real uid/gid. |
557 | 557 | -O File is owned by real uid. |
558 | 558 | |
559 | 559 | =end original |
560 | 560 | |
561 | 561 | -R ファイルが実 uid/gid で読み出し可 |
562 | 562 | -W ファイルが実 uid/gid で書き込み可 |
563 | 563 | -X ファイルが実 uid/gid で実行可 |
564 | 564 | -O ファイルが実 uid の所有物 |
565 | 565 | |
566 | 566 | =begin original |
567 | 567 | |
568 | 568 | -e File exists. |
569 | 569 | -z File has zero size (is empty). |
570 | 570 | -s File has nonzero size (returns size in bytes). |
571 | 571 | |
572 | 572 | =end original |
573 | 573 | |
574 | 574 | -e ファイルが存在する |
575 | 575 | -z ファイルの大きさがゼロ(空) |
576 | 576 | -s ファイルの大きさがゼロ以外 (バイト単位での大きさを返す) |
577 | 577 | |
578 | 578 | =begin original |
579 | 579 | |
580 | 580 | -f File is a plain file. |
581 | 581 | -d File is a directory. |
582 | 582 | -l File is a symbolic link. |
583 | 583 | -p File is a named pipe (FIFO), or Filehandle is a pipe. |
584 | 584 | -S File is a socket. |
585 | 585 | -b File is a block special file. |
586 | 586 | -c File is a character special file. |
587 | 587 | -t Filehandle is opened to a tty. |
588 | 588 | |
589 | 589 | =end original |
590 | 590 | |
591 | 591 | -f ファイルは通常ファイル |
592 | 592 | -d ファイルはディレクトリ |
593 | 593 | -l ファイルはシンボリックリンク |
594 | 594 | -p ファイルは名前付きパイプ (FIFO) またはファイルハンドルはパイプ |
595 | 595 | -S ファイルはソケット |
596 | 596 | -b ファイルはブロック特殊ファイル |
597 | 597 | -c ファイルはキャラクタ特殊ファイル |
598 | 598 | -t ファイルハンドルは tty にオープンされている |
599 | 599 | |
600 | 600 | =begin original |
601 | 601 | |
602 | 602 | -u File has setuid bit set. |
603 | 603 | -g File has setgid bit set. |
604 | 604 | -k File has sticky bit set. |
605 | 605 | |
606 | 606 | =end original |
607 | 607 | |
608 | 608 | -u ファイルの setuid ビットがセットされている |
609 | 609 | -g ファイルの setgid ビットがセットされている |
610 | 610 | -k ファイルの sticky ビットがセットされている |
611 | 611 | |
612 | 612 | =begin original |
613 | 613 | |
614 | 614 | -T File is an ASCII text file (heuristic guess). |
615 | 615 | -B File is a "binary" file (opposite of -T). |
616 | 616 | |
617 | 617 | =end original |
618 | 618 | |
619 | 619 | -T ファイルは ASCII テキストファイル (発見的に推測します) |
620 | 620 | -B ファイルは「バイナリ」ファイル (-T の反対) |
621 | 621 | |
622 | 622 | =begin original |
623 | 623 | |
624 | 624 | -M Script start time minus file modification time, in days. |
625 | 625 | -A Same for access time. |
626 | 626 | -C Same for inode change time (Unix, may differ for other platforms) |
627 | 627 | |
628 | 628 | =end original |
629 | 629 | |
630 | 630 | -M スクリプト実行開始時刻からファイル修正時刻を引いたもの(日単位) |
631 | 631 | -A 同様にアクセスがあってからの日数 |
632 | 632 | -C 同様に(Unix では) inode が変更されてからの日数(それ以外のプラットフォームでは違うかもしれません) |
633 | 633 | |
634 | 634 | =begin original |
635 | 635 | |
636 | 636 | Example: |
637 | 637 | |
638 | 638 | =end original |
639 | 639 | |
640 | 640 | 例: |
641 | 641 | |
642 | 642 | while (<>) { |
643 | 643 | chomp; |
644 | 644 | next unless -f $_; # ignore specials |
645 | 645 | #... |
646 | 646 | } |
647 | 647 | |
648 | 648 | =begin original |
649 | 649 | |
650 | 650 | The interpretation of the file permission operators C<-r>, C<-R>, |
651 | 651 | C<-w>, C<-W>, C<-x>, and C<-X> is by default based solely on the mode |
652 | 652 | of the file and the uids and gids of the user. There may be other |
653 | 653 | reasons you can't actually read, write, or execute the file: for |
654 | 654 | example network filesystem access controls, ACLs (access control lists), |
655 | 655 | read-only filesystems, and unrecognized executable formats. Note |
656 | 656 | that the use of these six specific operators to verify if some operation |
657 | 657 | is possible is usually a mistake, because it may be open to race |
658 | 658 | conditions. |
659 | 659 | |
660 | 660 | =end original |
661 | 661 | |
662 | 662 | ファイルのパーミッション演算子 C<-r>, C<-R>, C<-w>, C<-W>, C<-x>, |
663 | 663 | C<-X> の解釈は、ファイルのモードとユーザの実効/実 uid と |
664 | 664 | 実効/実 gid のみから判断されます。 |
665 | 665 | 実際にファイルが読めたり、書けたり、実行できたりするためには、 |
666 | 666 | 別の条件が必要かもしれません: |
667 | 667 | 例えば、ネットワークファイルシステムアクセスコントロール、 |
668 | 668 | ACL(アクセスコントロールリスト)、読み込み専用ファイルシステム、 |
669 | 669 | 認識できない実行ファイルフォーマット、などです。 |
670 | 670 | これらの 6 つの演算子を、特定の操作が可能かどうかを確認するために使うのは |
671 | 671 | 通常は誤りであることに注意してください; なぜなら、これらは競合条件を |
672 | 672 | 招きやすいからです。 |
673 | 673 | |
674 | 674 | =begin original |
675 | 675 | |
676 | 676 | Also note that, for the superuser on the local filesystems, the C<-r>, |
677 | 677 | C<-R>, C<-w>, and C<-W> tests always return 1, and C<-x> and C<-X> return 1 |
678 | 678 | if any execute bit is set in the mode. Scripts run by the superuser |
679 | 679 | may thus need to do a stat() to determine the actual mode of the file, |
680 | 680 | or temporarily set their effective uid to something else. |
681 | 681 | |
682 | 682 | =end original |
683 | 683 | |
684 | 684 | ローカルファイルシステムのスーパーユーザには、 |
685 | 685 | C<-r>, C<-R>, C<-w>, C<-W> に対して、常に 1 が返り、モード中の |
686 | 686 | いずれかの実行許可ビットが立っていれば、C<-x>, C<-X> にも 1 が |
687 | 687 | 返ることにも注意してください。 |
688 | 688 | スーパーユーザが実行するスクリプトでは、ファイルのモードを調べるためには、 |
689 | 689 | stat() を行なうか、実効 uid を一時的に別のものにする |
690 | 690 | 必要があるでしょう。 |
691 | 691 | |
692 | 692 | =begin original |
693 | 693 | |
694 | 694 | If you are using ACLs, there is a pragma called C<filetest> that may |
695 | 695 | produce more accurate results than the bare stat() mode bits. |
696 | 696 | When under the C<use filetest 'access'> the above-mentioned filetests |
697 | 697 | will test whether the permission can (not) be granted using the |
698 | 698 | access() family of system calls. Also note that the C<-x> and C<-X> may |
699 | 699 | under this pragma return true even if there are no execute permission |
700 | 700 | bits set (nor any extra execute permission ACLs). This strangeness is |
701 | 701 | due to the underlying system calls' definitions. Note also that, due to |
702 | 702 | the implementation of C<use filetest 'access'>, the C<_> special |
703 | 703 | filehandle won't cache the results of the file tests when this pragma is |
704 | 704 | in effect. Read the documentation for the C<filetest> pragma for more |
705 | 705 | information. |
706 | 706 | |
707 | 707 | =end original |
708 | 708 | |
709 | 709 | ACL を使っている場合は、生の stat() モードビットより |
710 | 710 | 精度の高い結果を作成する C<filetest> プラグマがあります。 |
711 | 711 | C<use filetest 'access'> とした場合、上述したファイルテストは |
712 | 712 | システムコールの access() ファミリーを使って権限が与えられているか |
713 | 713 | どうかをテストします。 |
714 | 714 | また、このプラグマが指定されている場合、C<-x> と C<-X> は |
715 | 715 | たとえ実行許可ビット(または追加の実行許可 ACL)がセットされていない |
716 | 716 | 場合でも真を返すことに注意してください。 |
717 | 717 | この挙動は使用するシステムコールの定義によるものです。 |
718 | 718 | C<use filetest 'access'> の実装により、このプラグマが有効の場合は |
719 | 719 | C<_> 特殊ファイルハンドルはファイルテストの結果をキャッシュしないことに |
720 | 720 | 注意してください。 |
721 | 721 | さらなる情報については C<filetest> プラグマのドキュメントを |
722 | 722 | 参照してください。 |
723 | 723 | |
724 | 724 | =begin original |
725 | 725 | |
726 | 726 | Note that C<-s/a/b/> does not do a negated substitution. Saying |
727 | 727 | C<-exp($foo)> still works as expected, however--only single letters |
728 | 728 | following a minus are interpreted as file tests. |
729 | 729 | |
730 | 730 | =end original |
731 | 731 | |
732 | 732 | C<-s/a/b> は、置換演算 (s///) の符号反転ではありません。 |
733 | 733 | しかし、C<-exp($foo)> は期待どおりに動作します。 |
734 | 734 | マイナス記号の後に英字が 1 字続くときにのみ、ファイルテストと |
735 | 735 | 解釈されます。 |
736 | 736 | |
737 | 737 | =begin original |
738 | 738 | |
739 | 739 | The C<-T> and C<-B> switches work as follows. The first block or so of the |
740 | 740 | file is examined for odd characters such as strange control codes or |
741 | 741 | characters with the high bit set. If too many strange characters (>30%) |
742 | 742 | are found, it's a C<-B> file; otherwise it's a C<-T> file. Also, any file |
743 | 743 | containing null in the first block is considered a binary file. If C<-T> |
744 | 744 | or C<-B> is used on a filehandle, the current IO buffer is examined |
745 | 745 | rather than the first block. Both C<-T> and C<-B> return true on a null |
746 | 746 | file, or a file at EOF when testing a filehandle. Because you have to |
747 | 747 | read a file to do the C<-T> test, on most occasions you want to use a C<-f> |
748 | 748 | against the file first, as in C<next unless -f $file && -T $file>. |
749 | 749 | |
750 | 750 | =end original |
751 | 751 | |
752 | 752 | ファイルテスト C<-T> と C<-B> の動作原理は、次のようになっています。 |
753 | 753 | ファイルの最初の数ブロックを調べて、変わった制御コードや |
754 | 754 | 上位ビットがセットされているような、通常のテキストには現れない文字を探します。 |
755 | 755 | そのような文字が、たくさん (>30%) 見つかるようであれば、 |
756 | 756 | そのファイルは C<-B> ファイルであると判断されます; |
757 | 757 | さもなければ C<-T> ファイルとなります。 |
758 | 758 | 最初のブロックにヌル文字が含まれるファイルも、 |
759 | 759 | バイナリファイルとみなされます。 |
760 | 760 | C<-T> や C<-B> をファイルハンドルに対して用いると、 |
761 | 761 | 最初のブロックを調べる代わりに、IO バッファを調べます。 |
762 | 762 | 調べたファイルの中身が何もないときや、 |
763 | 763 | ファイルハンドルを調べたときに EOF に達して |
764 | 764 | いたときには、C<-T> も C<-B> も「真」を返します。 |
765 | 765 | C<-T> テストをするためにはファイルを読み込まないといけないので、 |
766 | 766 | たいていは C<next unless -f $file && -T $file> というような形で |
767 | 767 | まず調べたいファイルに対して C<-f> を使いたいはずです。 |
768 | 768 | |
769 | 769 | =begin original |
770 | 770 | |
771 | 771 | If any of the file tests (or either the C<stat> or C<lstat> operators) are given |
772 | 772 | the special filehandle consisting of a solitary underline, then the stat |
773 | 773 | structure of the previous file test (or stat operator) is used, saving |
774 | 774 | a system call. (This doesn't work with C<-t>, and you need to remember |
775 | 775 | that lstat() and C<-l> will leave values in the stat structure for the |
776 | 776 | symbolic link, not the real file.) (Also, if the stat buffer was filled by |
777 | 777 | an C<lstat> call, C<-T> and C<-B> will reset it with the results of C<stat _>). |
778 | 778 | Example: |
779 | 779 | |
780 | 780 | =end original |
781 | 781 | |
782 | 782 | どのファイルテスト (あるいは、C<stat> や C<lstat>) 演算子にも、 |
783 | 783 | 下線だけから成る特別なファイルハンドルを与えると、 |
784 | 784 | 前回のファイルテスト (や stat) の stat 構造体が使われ、 |
785 | 785 | システムコールを省きます。 |
786 | 786 | (C<-t> には使えませんし、lstat() や C<-l> は実ファイルではなく、 |
787 | 787 | シンボリックリンクの情報を stat 構造体に残すことを |
788 | 788 | 覚えておく必要があります。) |
789 | 789 | (また、stat バッファが C<lstat> 呼び出しで埋まった場合、 |
790 | 790 | C<-T> と C<-B> の結果は C<stat _> の結果でリセットされます。 |
791 | 791 | 例: |
792 | 792 | |
793 | 793 | print "Can do.\n" if -r $a || -w _ || -x _; |
794 | 794 | |
795 | 795 | stat($filename); |
796 | 796 | print "Readable\n" if -r _; |
797 | 797 | print "Writable\n" if -w _; |
798 | 798 | print "Executable\n" if -x _; |
799 | 799 | print "Setuid\n" if -u _; |
800 | 800 | print "Setgid\n" if -g _; |
801 | 801 | print "Sticky\n" if -k _; |
802 | 802 | print "Text\n" if -T _; |
803 | 803 | print "Binary\n" if -B _; |
804 | 804 | |
805 | 805 | =begin original |
806 | 806 | |
807 | 807 | As of Perl 5.9.1, as a form of purely syntactic sugar, you can stack file |
808 | 808 | test operators, in a way that C<-f -w -x $file> is equivalent to |
809 | 809 | C<-x $file && -w _ && -f _>. (This is only syntax fancy: if you use |
810 | 810 | the return value of C<-f $file> as an argument to another filetest |
811 | 811 | operator, no special magic will happen.) |
812 | 812 | |
813 | 813 | =end original |
814 | 814 | |
815 | 815 | Perl 5.9.1 から、純粋にシンタックスシュガーとして、ファイルテスト演算子を |
816 | 816 | スタックさせることができるので、C<-f -w -x $file> は |
817 | 817 | C<-x $file && -w _ && -f _> と等価です。 |
818 | 818 | (これば文法上だけの話です; もし C<-f $file> の返り値を他のファイルテスト |
819 | 819 | 演算子の引数として使う場合は、何の特別なことも起きません。) |
820 | 820 | |
821 | 821 | =item abs VALUE |
822 | 822 | X<abs> X<absolute> |
823 | 823 | |
824 | 824 | =item abs |
825 | 825 | |
826 | 826 | =begin original |
827 | 827 | |
828 | 828 | Returns the absolute value of its argument. |
829 | 829 | If VALUE is omitted, uses C<$_>. |
830 | 830 | |
831 | 831 | =end original |
832 | 832 | |
833 | 833 | 引数の絶対値を返します。 |
834 | 834 | VALUE が省略された場合は、C<$_> を使います。 |
835 | 835 | |
836 | 836 | =item accept NEWSOCKET,GENERICSOCKET |
837 | 837 | X<accept> |
838 | 838 | |
839 | 839 | =begin original |
840 | 840 | |
841 | 841 | Accepts an incoming socket connect, just as the accept(2) system call |
842 | 842 | does. Returns the packed address if it succeeded, false otherwise. |
843 | 843 | See the example in L<perlipc/"Sockets: Client/Server Communication">. |
844 | 844 | |
845 | 845 | =end original |
846 | 846 | |
847 | 847 | accept(2) システムコールと同様に、着信するソケットの接続を受け付けます。 |
848 | 848 | 成功時にはパックされたアドレスを返し、失敗すれば偽を返します。 |
849 | 849 | L<perlipc/"Sockets: Client/Server Communication"> の |
850 | 850 | 例を参照してください。 |
851 | 851 | |
852 | 852 | =begin original |
853 | 853 | |
854 | 854 | On systems that support a close-on-exec flag on files, the flag will |
855 | 855 | be set for the newly opened file descriptor, as determined by the |
856 | 856 | value of $^F. See L<perlvar/$^F>. |
857 | 857 | |
858 | 858 | =end original |
859 | 859 | |
860 | 860 | ファイルに対する close-on-exec フラグをサポートしているシステムでは、 |
861 | 861 | フラグは $^F の値で決定される、新しくオープンされたファイル記述子に対して |
862 | セットされます。 | |
862 | セットされます。L<perlvar/$^F> を参照してください。 | |
863 | L<perlvar/$^F> を参照してください。 | |
864 | 863 | |
865 | 864 | =item alarm SECONDS |
866 | 865 | X<alarm> |
867 | 866 | X<SIGALRM> |
868 | 867 | X<timer> |
869 | 868 | |
870 | 869 | =item alarm |
871 | 870 | |
872 | 871 | =begin original |
873 | 872 | |
874 | 873 | Arranges to have a SIGALRM delivered to this process after the |
875 | 874 | specified number of wallclock seconds has elapsed. If SECONDS is not |
876 | 875 | specified, the value stored in C<$_> is used. (On some machines, |
877 | 876 | unfortunately, the elapsed time may be up to one second less or more |
878 | 877 | than you specified because of how seconds are counted, and process |
879 | 878 | scheduling may delay the delivery of the signal even further.) |
880 | 879 | |
881 | 880 | =end original |
882 | 881 | |
883 | 882 | 指定した壁時計秒数が経過した後に、自プロセスに SIGALRM が |
884 | 883 | 送られてくるようにします。SECONDS が指定されていない場合は、 |
885 | 884 | C<$_>に格納されている値を使います。 |
886 | 885 | (マシンによっては、秒の数え方が異なるため、指定した秒数よりも |
887 | 886 | 最大で 1 秒ずれます。) |
888 | 887 | |
889 | 888 | =begin original |
890 | 889 | |
891 | 890 | Only one timer may be counting at once. Each call disables the |
892 | 891 | previous timer, and an argument of C<0> may be supplied to cancel the |
893 | 892 | previous timer without starting a new one. The returned value is the |
894 | 893 | amount of time remaining on the previous timer. |
895 | 894 | |
896 | 895 | =end original |
897 | 896 | |
898 | 897 | 一度には 1 つのタイマだけが設定可能です。 |
899 | 898 | 呼び出しを行なう度に、以前のタイマを無効にしますし、 |
900 | 899 | 新しくタイマを起動しないで以前のタイマをキャンセルするために |
901 | 900 | 引数に C<0> を指定して呼び出すことができます。 |
902 | 901 | 以前のタイマの残り時間が、返り値となります。 |
903 | 902 | |
904 | 903 | =begin original |
905 | 904 | |
906 | 905 | For delays of finer granularity than one second, the Time::HiRes module |
907 | 906 | (from CPAN, and starting from Perl 5.8 part of the standard |
908 | 907 | distribution) provides ualarm(). You may also use Perl's four-argument |
909 | 908 | version of select() leaving the first three arguments undefined, or you |
910 | 909 | might be able to use the C<syscall> interface to access setitimer(2) if |
911 | 910 | your system supports it. See L<perlfaq8> for details. |
912 | 911 | |
913 | 912 | =end original |
914 | 913 | |
915 | 914 | 1 秒より精度の高いスリープを行なうには、 |
916 | 915 | Time::HiRes モジュール(CPAN から、また Perl 5.8 からは |
917 | 916 | 標準配布されています) が ualarm() を提供します。 |
918 | 917 | Perl の 4 引数版 select() を最初の 3 引数を未定義にして使うか、 |
919 | 918 | setitimer(2) をサポートしているシステムでは、Perl の |
920 | 919 | C<syscall> インタフェースを使ってアクセスすることもできます。 |
921 | 920 | 詳しくは L<perlfaq8> を参照してください。 |
922 | 921 | |
923 | 922 | =begin original |
924 | 923 | |
925 | 924 | It is usually a mistake to intermix C<alarm> and C<sleep> calls. |
926 | 925 | (C<sleep> may be internally implemented in your system with C<alarm>) |
927 | 926 | |
928 | 927 | =end original |
929 | 928 | |
930 | 929 | C<alarm> と C<sleep> を混ぜて使うのは普通は間違いです。 |
931 | 930 | (C<sleep> は内部的に C<alarm> を使って内部的に実装されているかもしれません) |
932 | 931 | |
933 | 932 | =begin original |
934 | 933 | |
935 | 934 | If you want to use C<alarm> to time out a system call you need to use an |
936 | 935 | C<eval>/C<die> pair. You can't rely on the alarm causing the system call to |
937 | 936 | fail with C<$!> set to C<EINTR> because Perl sets up signal handlers to |
938 | 937 | restart system calls on some systems. Using C<eval>/C<die> always works, |
939 | 938 | modulo the caveats given in L<perlipc/"Signals">. |
940 | 939 | |
941 | 940 | =end original |
942 | 941 | |
943 | 942 | C<alarm> をシステムコールの時間切れのために使いたいなら、 |
944 | 943 | C<eval>/C<die> のペアで使う必要があります。 |
945 | 944 | システムコールが失敗したときに C<$!> に C<EINTR> がセットされることに |
946 | 945 | 頼ってはいけません。なぜならシステムによっては Perl は |
947 | 946 | システムコールを再開するためにシグナルハンドラを設定するからです。 |
948 | 947 | C<eval>/C<die> は常にうまく動きます。 |
949 | 948 | 注意点については L<perlipc/"Signals"> を参照して下さい。 |
950 | 949 | |
951 | 950 | eval { |
952 | 951 | local $SIG{ALRM} = sub { die "alarm\n" }; # NB: \n required |
953 | 952 | alarm $timeout; |
954 | 953 | $nread = sysread SOCKET, $buffer, $size; |
955 | 954 | alarm 0; |
956 | 955 | }; |
957 | 956 | if ($@) { |
958 | 957 | die unless $@ eq "alarm\n"; # propagate unexpected errors |
959 | 958 | # timed out |
960 | 959 | } |
961 | 960 | else { |
962 | 961 | # didn't |
963 | 962 | } |
964 | 963 | |
965 | 964 | =begin original |
966 | 965 | |
967 | 966 | For more information see L<perlipc>. |
968 | 967 | |
969 | 968 | =end original |
970 | 969 | |
971 | 970 | さらなる情報については L<perlipc> を参照してください。 |
972 | 971 | |
973 | 972 | =item atan2 Y,X |
974 | 973 | X<atan2> X<arctangent> X<tan> X<tangent> |
975 | 974 | |
976 | 975 | =begin original |
977 | 976 | |
978 | 977 | Returns the arctangent of Y/X in the range -PI to PI. |
979 | 978 | |
980 | 979 | =end original |
981 | 980 | |
982 | 981 | -πからπの範囲で Y/X の逆正接を返します。 |
983 | 982 | |
984 | 983 | =begin original |
985 | 984 | |
986 | 985 | For the tangent operation, you may use the C<Math::Trig::tan> |
987 | 986 | function, or use the familiar relation: |
988 | 987 | |
989 | 988 | =end original |
990 | 989 | |
991 | 990 | 正接を求めたいときは、C<Math::Trig::tan> を使うか、 |
992 | 991 | 以下のよく知られた関係を使ってください。 |
993 | 992 | |
994 | 993 | sub tan { sin($_[0]) / cos($_[0]) } |
995 | 994 | |
996 | 995 | =begin original |
997 | 996 | |
998 | ||
997 | Note that atan2(0, 0) is not well-defined. | |
999 | your atan2(3) manpage for more information. | |
1000 | 998 | |
1001 | 999 | =end original |
1002 | 1000 | |
1003 | ||
1001 | atan2(0, 0) は未定義であることに注意してください。 | |
1004 | atan2(3) man ページを参照してください。 | |
1005 | 1002 | |
1006 | 1003 | =item bind SOCKET,NAME |
1007 | 1004 | X<bind> |
1008 | 1005 | |
1009 | 1006 | =begin original |
1010 | 1007 | |
1011 | 1008 | Binds a network address to a socket, just as the bind system call |
1012 | 1009 | does. Returns true if it succeeded, false otherwise. NAME should be a |
1013 | 1010 | packed address of the appropriate type for the socket. See the examples in |
1014 | 1011 | L<perlipc/"Sockets: Client/Server Communication">. |
1015 | 1012 | |
1016 | 1013 | =end original |
1017 | 1014 | |
1018 | 1015 | bind(2) システムコールと同様に、ネットワークアドレスをソケットに結び付けます。 |
1019 | 1016 | 成功時には真を返し、失敗時には偽を返します。 |
1020 | 1017 | NAME は、ソケットに対する、適切な型のパックされたアドレスでなければなりません。 |
1021 | 1018 | L<perlipc/"Sockets: Client/Server Communication"> の例を参照してください。 |
1022 | 1019 | |
1023 | 1020 | =item binmode FILEHANDLE, LAYER |
1024 | 1021 | X<binmode> X<binary> X<text> X<DOS> X<Windows> |
1025 | 1022 | |
1026 | 1023 | =item binmode FILEHANDLE |
1027 | 1024 | |
1028 | 1025 | =begin original |
1029 | 1026 | |
1030 | 1027 | Arranges for FILEHANDLE to be read or written in "binary" or "text" |
1031 | 1028 | mode on systems where the run-time libraries distinguish between |
1032 | 1029 | binary and text files. If FILEHANDLE is an expression, the value is |
1033 | 1030 | taken as the name of the filehandle. Returns true on success, |
1034 | 1031 | otherwise it returns C<undef> and sets C<$!> (errno). |
1035 | 1032 | |
1036 | 1033 | =end original |
1037 | 1034 | |
1038 | 1035 | バイナリファイルとテキストファイルを区別する OS において、 |
1039 | 1036 | FILEHANDLE を「バイナリ」または「テキスト」で読み書きするように |
1040 | 1037 | 指定します。 |
1041 | 1038 | FILEHANDLE が式である場合には、その式の値がファイルハンドルの |
1042 | 1039 | 名前として使われます。 |
1043 | 1040 | 成功時には真を返し、失敗時には C<undef> を返して C<$!> (errno) を設定します。 |
1044 | 1041 | |
1045 | 1042 | =begin original |
1046 | 1043 | |
1047 | 1044 | On some systems (in general, DOS and Windows-based systems) binmode() |
1048 | 1045 | is necessary when you're not working with a text file. For the sake |
1049 | 1046 | of portability it is a good idea to always use it when appropriate, |
1050 | 1047 | and to never use it when it isn't appropriate. Also, people can |
1051 | 1048 | set their I/O to be by default UTF-8 encoded Unicode, not bytes. |
1052 | 1049 | |
1053 | 1050 | =end original |
1054 | 1051 | |
1055 | 1052 | テキストファイルでないものを扱う場合に binmode() が必要な |
1056 | 1053 | システムもあります(一般的には DOS と Windows ベースのシステムです)。 |
1057 | 1054 | 移植性のために、適切なときには常にこれを使い、適切でないときには |
1058 | 1055 | 決して使わないというのは良い考えです。 |
1059 | 1056 | また、デフォルトとして I/O を bytes ではなく UTF-8 エンコードされた |
1060 | 1057 | Unicode にセットすることも出来ます。 |
1061 | 1058 | |
1062 | 1059 | =begin original |
1063 | 1060 | |
1064 | 1061 | In other words: regardless of platform, use binmode() on binary data, |
1065 | 1062 | like for example images. |
1066 | 1063 | |
1067 | 1064 | =end original |
1068 | 1065 | |
1069 | 1066 | 言い換えると: プラットフォームに関わらず、 |
1070 | 1067 | 例のイメージのようなバイナリファイルに対しては binmode() を使ってください。 |
1071 | 1068 | |
1072 | 1069 | =begin original |
1073 | 1070 | |
1074 | 1071 | If LAYER is present it is a single string, but may contain multiple |
1075 | 1072 | directives. The directives alter the behaviour of the file handle. |
1076 | When LAYER is present using binmode on | |
1073 | When LAYER is present using binmode on text file makes sense. | |
1077 | 1074 | |
1078 | 1075 | =end original |
1079 | 1076 | |
1080 | 1077 | LAYER が存在すると、それは単一の文字列ですが、複数の指示子を |
1081 | 1078 | 含むことができます。 |
1082 | 1079 | 指示子はファイルハンドルの振る舞いを変更します。 |
1083 | 1080 | LAYER が存在すると、テキストファイルでの binmode が意味を持ちます。 |
1084 | 1081 | |
1085 | 1082 | =begin original |
1086 | 1083 | |
1087 | 1084 | If LAYER is omitted or specified as C<:raw> the filehandle is made |
1088 | 1085 | suitable for passing binary data. This includes turning off possible CRLF |
1089 | 1086 | translation and marking it as bytes (as opposed to Unicode characters). |
1090 | 1087 | Note that, despite what may be implied in I<"Programming Perl"> (the |
1091 | 1088 | Camel) or elsewhere, C<:raw> is I<not> simply the inverse of C<:crlf> |
1092 | 1089 | -- other layers which would affect the binary nature of the stream are |
1093 | 1090 | I<also> disabled. See L<PerlIO>, L<perlrun> and the discussion about the |
1094 | 1091 | PERLIO environment variable. |
1095 | 1092 | |
1096 | 1093 | =end original |
1097 | 1094 | |
1098 | 1095 | LAYER が省略されたり、C<:raw> が指定されると、ファイルハンドルはバイナリ |
1099 | 1096 | データの通過に適するように設定されます。 |
1100 | 1097 | これには CRLF 変換をオフにしたり、それぞれを(Unicode 文字ではなく) |
1101 | 1098 | バイトであるとマークしたりすることを含みます。 |
1102 | 1099 | I<"プログラミング Perl">(ラクダ本) やその他で暗示されているにも関わらず、 |
1103 | 1100 | C<:raw> は単なる C<:crlf> の I<逆ではありません> -- ストリームの |
1104 | 1101 | バイナリとしての性質に影響を与える I<その他の層も無効にされます>。 |
1105 | 1102 | L<PerlIO>, L<perlrun> およびPERLIO 環境変数に関する議論を参照してください。 |
1106 | 1103 | |
1107 | 1104 | =begin original |
1108 | 1105 | |
1109 | 1106 | The C<:bytes>, C<:crlf>, and C<:utf8>, and any other directives of the |
1110 | 1107 | form C<:...>, are called I/O I<layers>. The C<open> pragma can be used to |
1111 | 1108 | establish default I/O layers. See L<open>. |
1112 | 1109 | |
1113 | 1110 | =end original |
1114 | 1111 | |
1115 | 1112 | C<:bytes>, C<:crlf>, and C<:utf8>, 及びその他の C<:...> 形式の指示子は |
1116 | 1113 | I/O I<層> が呼び出されます。 |
1117 | 1114 | C<open> プラグマはデフォルト I/O 層を指定するために使われます。 |
1118 | 1115 | L<open> を参照してください。 |
1119 | 1116 | |
1120 | 1117 | =begin original |
1121 | 1118 | |
1122 | 1119 | I<The LAYER parameter of the binmode() function is described as "DISCIPLINE" |
1123 | 1120 | in "Programming Perl, 3rd Edition". However, since the publishing of this |
1124 | 1121 | book, by many known as "Camel III", the consensus of the naming of this |
1125 | 1122 | functionality has moved from "discipline" to "layer". All documentation |
1126 | 1123 | of this version of Perl therefore refers to "layers" rather than to |
1127 | 1124 | "disciplines". Now back to the regularly scheduled documentation...> |
1128 | 1125 | |
1129 | 1126 | =end original |
1130 | 1127 | |
1131 | 1128 | I<binmode() 関数の LAYER パラメータは 「プログラミングPerl 第3版」では |
1132 | 1129 | 「ディシプリン(DISCIPLINE)」と表現されていました。 |
1133 | 1130 | しかし、「ラクダ本第3版」として知られているこの本の出版後、この機能の名前は |
1134 | 1131 | 「ディシプリン」から「層」に変更することで合意されました。 |
1135 | 1132 | 従って、このバージョンの Perl の全ての文書では「ディシプリン」ではなく |
1136 | 1133 | 「層」と記述されています。では通常の解説に戻ります…。> |
1137 | 1134 | |
1138 | 1135 | =begin original |
1139 | 1136 | |
1140 | 1137 | To mark FILEHANDLE as UTF-8, use C<:utf8> or C<:encoding(utf8)>. |
1141 | 1138 | C<:utf8> just marks the data as UTF-8 without further checking, |
1142 | 1139 | while C<:encoding(utf8)> checks the data for actually being valid |
1143 | 1140 | UTF-8. More details can be found in L<PerlIO::encoding>. |
1144 | 1141 | |
1145 | 1142 | =end original |
1146 | 1143 | |
1147 | 1144 | FILEHANDLE が UTF-8 であるというマークをつけるには、C<:utf8> か |
1148 | 1145 | C<:encoding(utf8)> を使ってください。 |
1149 | 1146 | C<:utf8> は、さらなるチェックなしにデータが UTF-8 としてマークしますが、 |
1150 | 1147 | C<:encoding(utf8)> はデータが実際に有効な UTF-8 かどうかをチェックします。 |
1151 | 1148 | さらなる詳細は L<PerlIO::encoding> にあります。 |
1152 | 1149 | |
1153 | 1150 | =begin original |
1154 | 1151 | |
1155 | 1152 | In general, binmode() should be called after open() but before any I/O |
1156 | 1153 | is done on the filehandle. Calling binmode() will normally flush any |
1157 | 1154 | pending buffered output data (and perhaps pending input data) on the |
1158 | 1155 | handle. An exception to this is the C<:encoding> layer that |
1159 | 1156 | changes the default character encoding of the handle, see L<open>. |
1160 | 1157 | The C<:encoding> layer sometimes needs to be called in |
1161 | 1158 | mid-stream, and it doesn't flush the stream. The C<:encoding> |
1162 | 1159 | also implicitly pushes on top of itself the C<:utf8> layer because |
1163 | 1160 | internally Perl will operate on UTF-8 encoded Unicode characters. |
1164 | 1161 | |
1165 | 1162 | =end original |
1166 | 1163 | |
1167 | 1164 | 一般的に binmode() は open() を呼び出した後、このファイルハンドルに対する |
1168 | 1165 | I/O 操作をする前に呼び出すべきです。 |
1169 | 1166 | binmode() を呼び出すと、普通はこのファイルハンドルに対して |
1170 | 1167 | バッファリングされている全ての出力データ |
1171 | 1168 | (およびおそらくは入力データ)をフラッシュします。 |
1172 | 1169 | 例外は、このハンドルに対するデフォルト文字エンコーディングを変更する |
1173 | 1170 | C<:encoding> 層です; L<open> を参照してください。 |
1174 | 1171 | C<:encoding> 層はストリームの途中で呼び出す必要があることがあり、 |
1175 | 1172 | それによってストリームはフラッシュされません。 |
1176 | 1173 | Perl は内部で UTF-8 エンコードされた Unicode 文字を操作しているので、 |
1177 | 1174 | C<:encoding> は暗黙のうちに自身を C<:utf8> 層の上に押し上げます。 |
1178 | 1175 | |
1179 | 1176 | =begin original |
1180 | 1177 | |
1181 | 1178 | The operating system, device drivers, C libraries, and Perl run-time |
1182 | 1179 | system all work together to let the programmer treat a single |
1183 | 1180 | character (C<\n>) as the line terminator, irrespective of the external |
1184 | 1181 | representation. On many operating systems, the native text file |
1185 | 1182 | representation matches the internal representation, but on some |
1186 | 1183 | platforms the external representation of C<\n> is made up of more than |
1187 | 1184 | one character. |
1188 | 1185 | |
1189 | 1186 | =end original |
1190 | 1187 | |
1191 | 1188 | オペレーティングシステム、デバイスドライバ、C ライブラリ、 |
1192 | 1189 | Perl ランタイムシステムは全て、プログラマが外部表現に関わらず |
1193 | 1190 | 1 文字 (C<\n>) を行終端として扱えるように協調作業します。 |
1194 | 1191 | 多くのオペレーティングシステムでは、ネイティブテキストファイル表現は |
1195 | 1192 | 内部表現と同じですが、C<\n> の外部表現が複数文字になる |
1196 | 1193 | プラットフォームもあります。 |
1197 | 1194 | |
1198 | 1195 | =begin original |
1199 | 1196 | |
1200 | 1197 | Mac OS, all variants of Unix, and Stream_LF files on VMS use a single |
1201 | 1198 | character to end each line in the external representation of text (even |
1202 | 1199 | though that single character is CARRIAGE RETURN on Mac OS and LINE FEED |
1203 | 1200 | on Unix and most VMS files). In other systems like OS/2, DOS and the |
1204 | 1201 | various flavors of MS-Windows your program sees a C<\n> as a simple C<\cJ>, |
1205 | 1202 | but what's stored in text files are the two characters C<\cM\cJ>. That |
1206 | 1203 | means that, if you don't use binmode() on these systems, C<\cM\cJ> |
1207 | 1204 | sequences on disk will be converted to C<\n> on input, and any C<\n> in |
1208 | 1205 | your program will be converted back to C<\cM\cJ> on output. This is what |
1209 | 1206 | you want for text files, but it can be disastrous for binary files. |
1210 | 1207 | |
1211 | 1208 | =end original |
1212 | 1209 | |
1213 | 1210 | Mac OS、全ての Unix 系、VMS の Stream_LF ファイルは |
1214 | 1211 | テキストの外部表現として各行の末尾に 1 つの文字を |
1215 | 1212 | 使っています(その文字は Mac OS では復帰で、 |
1216 | 1213 | Unix とほとんどのVMS のファイルでは改行です)。 |
1217 | 1214 | VMS, MS-DOS, MS-Windows 系といったその他のシステムでは、 |
1218 | 1215 | プログラムからは C<\n> は単純に C<\cJ> に見えますが、 |
1219 | 1216 | テキストファイルとして保存される場合は C<\cM\cJ> の 2 文字になります。 |
1220 | 1217 | つまり、もしこれらのシステムで binmode() を使わないと、 |
1221 | 1218 | ディスク上の C<\cM\cJ> という並びは入力時に C<\n> に変換され、 |
1222 | 1219 | プログラムが出力した全ての C<\n> は C<\cM\cJ> に逆変換されます。 |
1223 | 1220 | これはテキストファイルの場合は思い通りの結果でしょうが、 |
1224 | 1221 | バイナリファイルの場合は悲惨です。 |
1225 | 1222 | |
1226 | 1223 | =begin original |
1227 | 1224 | |
1228 | 1225 | Another consequence of using binmode() (on some systems) is that |
1229 | 1226 | special end-of-file markers will be seen as part of the data stream. |
1230 | 1227 | For systems from the Microsoft family this means that if your binary |
1231 | 1228 | data contains C<\cZ>, the I/O subsystem will regard it as the end of |
1232 | 1229 | the file, unless you use binmode(). |
1233 | 1230 | |
1234 | 1231 | =end original |
1235 | 1232 | |
1236 | 1233 | binmode() を(いくつかのシステムで)使うことによるその他の作用としては、 |
1237 | 1234 | 特別なファイル終端マーカーがデータストリームの一部として |
1238 | 1235 | 見られることです。 |
1239 | 1236 | Microsoft ファミリーのシステムでは、binmode() を使っていないと |
1240 | 1237 | もしバイナリデータに C<\cZ> が含まれていたときに、I/O サブシステムが |
1241 | 1238 | これをファイル終端とみなすことを意味します。 |
1242 | 1239 | |
1243 | 1240 | =begin original |
1244 | 1241 | |
1245 | 1242 | binmode() is not only important for readline() and print() operations, |
1246 | 1243 | but also when using read(), seek(), sysread(), syswrite() and tell() |
1247 | 1244 | (see L<perlport> for more details). See the C<$/> and C<$\> variables |
1248 | 1245 | in L<perlvar> for how to manually set your input and output |
1249 | 1246 | line-termination sequences. |
1250 | 1247 | |
1251 | 1248 | =end original |
1252 | 1249 | |
1253 | 1250 | binmode() は readline() と print() 操作にだけではなく、 |
1254 | 1251 | read(), seek(), sysread(), syswrite(), tell() を使うときにも重要です |
1255 | 1252 | (詳細は L<perlport> を参照してください)。 |
1256 | 1253 | 入出力の行端末シーケンスを手動でセットする方法については |
1257 | 1254 | L<perlvar> の C<$/> 変数と C<$\> 変数を参照してください。 |
1258 | 1255 | |
1259 | 1256 | =item bless REF,CLASSNAME |
1260 | 1257 | X<bless> |
1261 | 1258 | |
1262 | 1259 | =item bless REF |
1263 | 1260 | |
1264 | 1261 | =begin original |
1265 | 1262 | |
1266 | 1263 | This function tells the thingy referenced by REF that it is now an object |
1267 | 1264 | in the CLASSNAME package. If CLASSNAME is omitted, the current package |
1268 | 1265 | is used. Because a C<bless> is often the last thing in a constructor, |
1269 | 1266 | it returns the reference for convenience. Always use the two-argument |
1270 | 1267 | version if a derived class might inherit the function doing the blessing. |
1271 | 1268 | See L<perltoot> and L<perlobj> for more about the blessing (and blessings) |
1272 | 1269 | of objects. |
1273 | 1270 | |
1274 | 1271 | =end original |
1275 | 1272 | |
1276 | 1273 | この関数は、REF で渡された オブジェクトに対し、 |
1277 | 1274 | CLASSNAME 内のオブジェクトとなったことを伝えます。 |
1278 | 1275 | CLASSNAME が省略された場合には、その時点のパッケージとなります。 |
1279 | 1276 | C<bless> は通常、コンストラクタの最後に置かれますので、 |
1280 | 1277 | 簡便のためにそのリファレンスを返します。 |
1281 | 1278 | 派生クラスが bless される関数を継承する場合は、 |
1282 | 1279 | 常に 2 引数版を使ってください。 |
1283 | 1280 | オブジェクトの bless (や再 bless) について、 |
1284 | 1281 | 詳しくは L<perltoot> と L<perlobj> を参照してください。 |
1285 | 1282 | |
1286 | 1283 | =begin original |
1287 | 1284 | |
1288 | 1285 | Consider always blessing objects in CLASSNAMEs that are mixed case. |
1289 | 1286 | Namespaces with all lowercase names are considered reserved for |
1290 | 1287 | Perl pragmata. Builtin types have all uppercase names. To prevent |
1291 | 1288 | confusion, you may wish to avoid such package names as well. Make sure |
1292 | 1289 | that CLASSNAME is a true value. |
1293 | 1290 | |
1294 | 1291 | =end original |
1295 | 1292 | |
1296 | 1293 | 大文字小文字が混じっている CLASSNAME のオブジェクトは常に bless することを |
1297 | 1294 | 考慮してください。 |
1298 | 1295 | 全て小文字の名前を持つ名前空間は Perl プラグマのために予約されています。 |
1299 | 1296 | 組み込みの型は全て大文字の名前を持ちます。 |
1300 | 1297 | 混乱を避けるために、 |
1301 | 1298 | パッケージ名としてこのような名前は避けるべきです。 |
1302 | 1299 | CLASSNAME は真の値を持つようにしてください。 |
1303 | 1300 | |
1304 | 1301 | =begin original |
1305 | 1302 | |
1306 | 1303 | See L<perlmod/"Perl Modules">. |
1307 | 1304 | |
1308 | 1305 | =end original |
1309 | 1306 | |
1310 | 1307 | L<perlmod/"Perl Modules"> を参照して下さい。 |
1311 | 1308 | |
1312 | 1309 | =item break |
1313 | 1310 | |
1314 | 1311 | =begin original |
1315 | 1312 | |
1316 | 1313 | Break out of a C<given()> block. |
1317 | 1314 | |
1318 | 1315 | =end original |
1319 | 1316 | |
1320 | 1317 | C<given()> ブロックから脱出します。 |
1321 | 1318 | |
1322 | 1319 | =begin original |
1323 | 1320 | |
1324 | 1321 | This keyword is enabled by the "switch" feature: see L<feature> |
1325 | 1322 | for more information. |
1326 | 1323 | |
1327 | 1324 | =end original |
1328 | 1325 | |
1329 | 1326 | このキーワードは "switch" 機能によって有効になります: |
1330 | 1327 | さらなる情報については L<feature> を参照してください。 |
1331 | 1328 | |
1332 | 1329 | =item caller EXPR |
1333 | 1330 | X<caller> X<call stack> X<stack> X<stack trace> |
1334 | 1331 | |
1335 | 1332 | =item caller |
1336 | 1333 | |
1337 | 1334 | =begin original |
1338 | 1335 | |
1339 | 1336 | Returns the context of the current subroutine call. In scalar context, |
1340 | 1337 | returns the caller's package name if there is a caller, that is, if |
1341 | 1338 | we're in a subroutine or C<eval> or C<require>, and the undefined value |
1342 | 1339 | otherwise. In list context, returns |
1343 | 1340 | |
1344 | 1341 | =end original |
1345 | 1342 | |
1346 | 1343 | その時点のサブルーチン呼び出しのコンテキストを返します。 |
1347 | 1344 | スカラコンテキストでは、呼び元がある場合 |
1348 | 1345 | (サブルーチン、C<eval>、C<require> の中にいるとき) には |
1349 | 1346 | 呼び出し元のパッケージ名を返し、その他のときには未定義値を返します。 |
1350 | 1347 | リストコンテキストでは、以下を返します: |
1351 | 1348 | |
1352 | 1349 | # 0 1 2 |
1353 | 1350 | ($package, $filename, $line) = caller; |
1354 | 1351 | |
1355 | 1352 | =begin original |
1356 | 1353 | |
1357 | 1354 | With EXPR, it returns some extra information that the debugger uses to |
1358 | 1355 | print a stack trace. The value of EXPR indicates how many call frames |
1359 | 1356 | to go back before the current one. |
1360 | 1357 | |
1361 | 1358 | =end original |
1362 | 1359 | |
1363 | 1360 | EXPR を付けると、デバッガがスタックトレースを表示するために使う情報を返します。 |
1364 | 1361 | EXPR の値は、現状から数えて、 |
1365 | 1362 | いくつ前のコールフレームまで戻るかを示します。 |
1366 | 1363 | |
1367 | 1364 | # 0 1 2 3 4 |
1368 | 1365 | ($package, $filename, $line, $subroutine, $hasargs, |
1369 | 1366 | |
1370 | 1367 | # 5 6 7 8 9 10 |
1371 | 1368 | $wantarray, $evaltext, $is_require, $hints, $bitmask, $hinthash) |
1372 | 1369 | = caller($i); |
1373 | 1370 | |
1374 | 1371 | =begin original |
1375 | 1372 | |
1376 | 1373 | Here $subroutine may be C<(eval)> if the frame is not a subroutine |
1377 | 1374 | call, but an C<eval>. In such a case additional elements $evaltext and |
1378 | 1375 | C<$is_require> are set: C<$is_require> is true if the frame is created by a |
1379 | 1376 | C<require> or C<use> statement, $evaltext contains the text of the |
1380 | 1377 | C<eval EXPR> statement. In particular, for an C<eval BLOCK> statement, |
1381 | 1378 | $subroutine is C<(eval)>, but $evaltext is undefined. (Note also that |
1382 | 1379 | each C<use> statement creates a C<require> frame inside an C<eval EXPR> |
1383 | 1380 | frame.) $subroutine may also be C<(unknown)> if this particular |
1384 | 1381 | subroutine happens to have been deleted from the symbol table. |
1385 | 1382 | C<$hasargs> is true if a new instance of C<@_> was set up for the frame. |
1386 | 1383 | C<$hints> and C<$bitmask> contain pragmatic hints that the caller was |
1387 | 1384 | compiled with. The C<$hints> and C<$bitmask> values are subject to change |
1388 | 1385 | between versions of Perl, and are not meant for external use. |
1389 | 1386 | |
1390 | 1387 | =end original |
1391 | 1388 | |
1392 | 1389 | もしフレームがサブルーチン呼び出しではなく C<eval> だった場合、この |
1393 | 1390 | $subroutine は C<(eval)> になります。 |
1394 | 1391 | この場合、追加の要素である $evaltext と C<$is_require> がセットされます: |
1395 | 1392 | C<$is_require> はフレームが C<require> または C<use> で作られた場合に |
1396 | 1393 | 真になり、$evaltext は C<eval EXPR> のテキストが入ります。 |
1397 | 1394 | 特に、C<eval BLOCK> の場合、$subroutine は C<(eval)> になりますが、 |
1398 | 1395 | $evaltext は未定義値になります。 |
1399 | 1396 | (それぞれの C<use> は C<eval EXPR> の中で C<require> フレームを作ることに |
1400 | 1397 | 注意してください。) |
1401 | 1398 | $subroutine は、そのサブルーチンがシンボルテーブルから削除された場合は |
1402 | 1399 | C<(unknown)> になります。 |
1403 | 1400 | C<$hasargs> はこのフレーム用に C<@_> の新しい実体が設定された場合に真となります。 |
1404 | 1401 | C<$hints> と C<$bitmask> は caller がコンパイルされたときの |
1405 | 1402 | 実際的なヒントを含みます。 |
1406 | 1403 | C<$hints> は C<$bitmask> は Perl のバージョンによって変更される |
1407 | 1404 | 可能性があるので、外部での使用を想定していません。 |
1408 | 1405 | |
1409 | 1406 | =begin original |
1410 | 1407 | |
1411 | 1408 | C<$hinthash> is a reference to a hash containing the value of C<%^H> when the |
1412 | 1409 | caller was compiled, or C<undef> if C<%^H> was empty. Do not modify the values |
1413 | 1410 | of this hash, as they are the actual values stored in the optree. |
1414 | 1411 | |
1415 | 1412 | =end original |
1416 | 1413 | |
1417 | 1414 | C<$hinthash> は、caller がコンパイルされた時の C<%^H> の値を含む |
1418 | 1415 | ハッシュへのリファレンスか、あるいは C<%^H> が空の場合は C<undef> です。 |
1419 | 1416 | このハッシュの値は構文木に保管されている実際の値なので、変更しないで下さい。 |
1420 | 1417 | |
1421 | 1418 | =begin original |
1422 | 1419 | |
1423 | 1420 | Furthermore, when called from within the DB package, caller returns more |
1424 | 1421 | detailed information: it sets the list variable C<@DB::args> to be the |
1425 | 1422 | arguments with which the subroutine was invoked. |
1426 | 1423 | |
1427 | 1424 | =end original |
1428 | 1425 | |
1429 | 1426 | さらに、DB パッケージの中から呼ばれた場合は、caller は |
1430 | 1427 | より詳細な情報を返します。 |
1431 | 1428 | サブルーチンが起動されたときの引数を変数 C<@DB::args> に設定します。 |
1432 | 1429 | |
1433 | 1430 | =begin original |
1434 | 1431 | |
1435 | 1432 | Be aware that the optimizer might have optimized call frames away before |
1436 | 1433 | C<caller> had a chance to get the information. That means that C<caller(N)> |
1437 | 1434 | might not return information about the call frame you expect it do, for |
1438 | 1435 | C<< N > 1 >>. In particular, C<@DB::args> might have information from the |
1439 | 1436 | previous time C<caller> was called. |
1440 | 1437 | |
1441 | 1438 | =end original |
1442 | 1439 | |
1443 | 1440 | C<caller> が情報を得る前にオプティマイザが呼び出しフレームを最適化して |
1444 | 1441 | しまうかもしれないことに注意してください。 |
1445 | 1442 | これは、C<caller(N)> が C<< N > 1 >> のとき、 |
1446 | 1443 | あなたが予測した呼び出しフレームの情報を返さないかもしれないことを意味します。 |
1447 | 1444 | 特に、C<@DB::args> は C<caller> が前回呼び出された時の情報を |
1448 | 1445 | 持っているかもしれません。 |
1449 | 1446 | |
1450 | 1447 | =item chdir EXPR |
1451 | 1448 | X<chdir> |
1452 | 1449 | X<cd> |
1453 | 1450 | X<directory, change> |
1454 | 1451 | |
1455 | 1452 | =item chdir FILEHANDLE |
1456 | 1453 | |
1457 | 1454 | =item chdir DIRHANDLE |
1458 | 1455 | |
1459 | 1456 | =item chdir |
1460 | 1457 | |
1461 | 1458 | =begin original |
1462 | 1459 | |
1463 | 1460 | Changes the working directory to EXPR, if possible. If EXPR is omitted, |
1464 | 1461 | changes to the directory specified by C<$ENV{HOME}>, if set; if not, |
1465 | 1462 | changes to the directory specified by C<$ENV{LOGDIR}>. (Under VMS, the |
1466 | 1463 | variable C<$ENV{SYS$LOGIN}> is also checked, and used if it is set.) If |
1467 | 1464 | neither is set, C<chdir> does nothing. It returns true upon success, |
1468 | 1465 | false otherwise. See the example under C<die>. |
1469 | 1466 | |
1470 | 1467 | =end original |
1471 | 1468 | |
1472 | 1469 | (可能であれば、) カレントディレクトリを EXPR に移します。 |
1473 | 1470 | EXPR を指定しないと、C<$ENV{HOME}> が設定されていれば、 |
1474 | 1471 | そのディレクトリに移ります。 |
1475 | 1472 | そうでなく、C<$ENV{LOGDIR}>が設定されていれば、そのディレクトリに移ります。 |
1476 | 1473 | (VMS では C<$ENV{SYS$LOGIN}> もチェックされ、もしセットされていれば使われます。) |
1477 | 1474 | どちらも設定されていなければ、C<chdir> は何もしません。 |
1478 | 1475 | 成功時には真を返し、そうでなければ偽を返します。 |
1479 | 1476 | C<die> の項の例を参照してください。 |
1480 | 1477 | |
1481 | 1478 | =begin original |
1482 | 1479 | |
1483 | 1480 | On systems that support fchdir, you might pass a file handle or |
1484 | 1481 | directory handle as argument. On systems that don't support fchdir, |
1485 | 1482 | passing handles produces a fatal error at run time. |
1486 | 1483 | |
1487 | 1484 | =end original |
1488 | 1485 | |
1489 | 1486 | fchdir に対応しているシステムでは、ファイルハンドルやディレクトリハンドルを |
1490 | 1487 | 引数として渡せます。 |
1491 | 1488 | fchdir に対応していないシステムでは、ハンドルを渡すと実行時に |
1492 | 1489 | 致命的エラーになります。 |
1493 | 1490 | |
1494 | 1491 | =item chmod LIST |
1495 | 1492 | X<chmod> X<permission> X<mode> |
1496 | 1493 | |
1497 | 1494 | =begin original |
1498 | 1495 | |
1499 | 1496 | Changes the permissions of a list of files. The first element of the |
1500 | 1497 | list must be the numerical mode, which should probably be an octal |
1501 | 1498 | number, and which definitely should I<not> be a string of octal digits: |
1502 | 1499 | C<0644> is okay, C<'0644'> is not. Returns the number of files |
1503 | 1500 | successfully changed. See also L</oct>, if all you have is a string. |
1504 | 1501 | |
1505 | 1502 | =end original |
1506 | 1503 | |
1507 | 1504 | LIST に含まれるファイルの、パーミッションを変更します。 |
1508 | 1505 | LIST の最初の要素は、数値表現のモードでなければなりません。 |
1509 | 1506 | 恐らく 8 進表記の数であるべきでしょう。しかし、8 進表記のC<文字列ではいけません>。 |
1510 | 1507 | C<0644> は OK ですが、 C<'0644'> はだめ、ということです。 |
1511 | 1508 | 変更に成功したファイルの数を返します。 |
1512 | 1509 | 文字列を使いたい場合は、L</oct> を参照してください。 |
1513 | 1510 | |
1514 | 1511 | $cnt = chmod 0755, 'foo', 'bar'; |
1515 | 1512 | chmod 0755, @executables; |
1516 | 1513 | $mode = '0644'; chmod $mode, 'foo'; # !!! sets mode to |
1517 | 1514 | # --w----r-T |
1518 | 1515 | $mode = '0644'; chmod oct($mode), 'foo'; # this is better |
1519 | 1516 | $mode = 0644; chmod $mode, 'foo'; # this is best |
1520 | 1517 | |
1521 | 1518 | =begin original |
1522 | 1519 | |
1523 | 1520 | On systems that support fchmod, you might pass file handles among the |
1524 | 1521 | files. On systems that don't support fchmod, passing file handles |
1525 | 1522 | produces a fatal error at run time. The file handles must be passed |
1526 | 1523 | as globs or references to be recognized. Barewords are considered |
1527 | 1524 | file names. |
1528 | 1525 | |
1529 | 1526 | =end original |
1530 | 1527 | |
1531 | 1528 | fchmod に対応しているシステムでは、ファイルハンドルを引数として渡せます。 |
1532 | 1529 | fchmod に対応していないシステムでは、ファイルハンドルを渡すと実行時に |
1533 | 1530 | 致命的エラーになります。 |
1534 | 1531 | ファイルハンドルを認識させるためには、グロブまたはリファレンスとして |
1535 | 1532 | 渡されなければなりません。 |
1536 | 1533 | 裸の単語はファイル名として扱われます。 |
1537 | 1534 | |
1538 | 1535 | open(my $fh, "<", "foo"); |
1539 | 1536 | my $perm = (stat $fh)[2] & 07777; |
1540 | 1537 | chmod($perm | 0600, $fh); |
1541 | 1538 | |
1542 | 1539 | =begin original |
1543 | 1540 | |
1544 | 1541 | You can also import the symbolic C<S_I*> constants from the Fcntl |
1545 | 1542 | module: |
1546 | 1543 | |
1547 | 1544 | =end original |
1548 | 1545 | |
1549 | 1546 | シンボリックな C<S_I*> 定数を Fcntl モジュールからインポートすることもできます。 |
1550 | 1547 | |
1551 | 1548 | use Fcntl ':mode'; |
1552 | 1549 | |
1553 | 1550 | chmod S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH, @executables; |
1554 | 1551 | # This is identical to the chmod 0755 of the above example. |
1555 | 1552 | |
1556 | 1553 | =item chomp VARIABLE |
1557 | 1554 | X<chomp> X<INPUT_RECORD_SEPARATOR> X<$/> X<newline> X<eol> |
1558 | 1555 | |
1559 | 1556 | =item chomp( LIST ) |
1560 | 1557 | |
1561 | 1558 | =item chomp |
1562 | 1559 | |
1563 | 1560 | =begin original |
1564 | 1561 | |
1565 | 1562 | This safer version of L</chop> removes any trailing string |
1566 | 1563 | that corresponds to the current value of C<$/> (also known as |
1567 | 1564 | $INPUT_RECORD_SEPARATOR in the C<English> module). It returns the total |
1568 | 1565 | number of characters removed from all its arguments. It's often used to |
1569 | 1566 | remove the newline from the end of an input record when you're worried |
1570 | 1567 | that the final record may be missing its newline. When in paragraph |
1571 | 1568 | mode (C<$/ = "">), it removes all trailing newlines from the string. |
1572 | 1569 | When in slurp mode (C<$/ = undef>) or fixed-length record mode (C<$/> is |
1573 | 1570 | a reference to an integer or the like, see L<perlvar>) chomp() won't |
1574 | 1571 | remove anything. |
1575 | 1572 | If VARIABLE is omitted, it chomps C<$_>. Example: |
1576 | 1573 | |
1577 | 1574 | =end original |
1578 | 1575 | |
1579 | 1576 | より安全な C<chop> (以下を参照してください) です。 |
1580 | 1577 | C<$/> (C<English> モジュールでは、$INPUT_RECORD_SEPARATOR |
1581 | 1578 | とも言う) のその時点の値に対応する行末文字を削除します。 |
1582 | 1579 | 全ての引数から削除した文字数の合計を返します。 |
1583 | 1580 | 入力レコードから、改行を削除したいのだけれど、最後のレコードには改行が |
1584 | 1581 | 入っているのかわからないような場合に、使用できます。 |
1585 | 1582 | 段落モード (C<$/ = "">) では、レコードの最後の改行をすべて取り除きます。 |
1586 | 1583 | 吸い込みモード (C<$/ = undef>) や 固定長レコードモード |
1587 | 1584 | (C<$/> が整数へのリファレンスや類似のものの場合。L<perlvar>を参照してください) |
1588 | 1585 | では、chomp() は何も取り除きません。 |
1589 | 1586 | VARIABLE が省略されると、$_ を対象として chomp します。 |
1590 | 1587 | 例: |
1591 | 1588 | |
1592 | 1589 | while (<>) { |
1593 | 1590 | chomp; # avoid \n on last field |
1594 | 1591 | @array = split(/:/); |
1595 | 1592 | # ... |
1596 | 1593 | } |
1597 | 1594 | |
1598 | 1595 | =begin original |
1599 | 1596 | |
1600 | 1597 | If VARIABLE is a hash, it chomps the hash's values, but not its keys. |
1601 | 1598 | |
1602 | 1599 | =end original |
1603 | 1600 | |
1604 | 1601 | VARIABLE がハッシュなら、ハッシュのキーではなく値について chomp します。 |
1605 | 1602 | |
1606 | 1603 | =begin original |
1607 | 1604 | |
1608 | 1605 | You can actually chomp anything that's an lvalue, including an assignment: |
1609 | 1606 | |
1610 | 1607 | =end original |
1611 | 1608 | |
1612 | 1609 | 左辺値であれば、代入を含めて、任意のものを chomp できます: |
1613 | 1610 | |
1614 | 1611 | chomp($cwd = `pwd`); |
1615 | 1612 | chomp($answer = <STDIN>); |
1616 | 1613 | |
1617 | 1614 | =begin original |
1618 | 1615 | |
1619 | 1616 | If you chomp a list, each element is chomped, and the total number of |
1620 | 1617 | characters removed is returned. |
1621 | 1618 | |
1622 | 1619 | =end original |
1623 | 1620 | |
1624 | 1621 | リストを chomp すると、個々の要素が chomp され、 |
1625 | 1622 | 削除された文字数の合計が返されます。 |
1626 | 1623 | |
1627 | 1624 | =begin original |
1628 | 1625 | |
1629 | 1626 | Note that parentheses are necessary when you're chomping anything |
1630 | 1627 | that is not a simple variable. This is because C<chomp $cwd = `pwd`;> |
1631 | 1628 | is interpreted as C<(chomp $cwd) = `pwd`;>, rather than as |
1632 | 1629 | C<chomp( $cwd = `pwd` )> which you might expect. Similarly, |
1633 | 1630 | C<chomp $a, $b> is interpreted as C<chomp($a), $b> rather than |
1634 | 1631 | as C<chomp($a, $b)>. |
1635 | 1632 | |
1636 | 1633 | =end original |
1637 | 1634 | |
1638 | 1635 | 単純な変数以外のものを chomp する場合はかっこが必要であることに |
1639 | 1636 | 注意してください。 |
1640 | 1637 | これは、C<chomp $cwd = `pwd`;> は、予測している |
1641 | 1638 | C<chomp( $cwd = `pwd` )> ではなく、C<(chomp $cwd) = `pwd`;> と |
1642 | 1639 | 解釈されるからです。 |
1643 | 1640 | 同様に、C<chomp $a, $b> は C<chomp($a, $b)> ではなく C<chomp($a), $b> |
1644 | 1641 | と解釈されます。 |
1645 | 1642 | |
1646 | 1643 | =item chop VARIABLE |
1647 | 1644 | X<chop> |
1648 | 1645 | |
1649 | 1646 | =item chop( LIST ) |
1650 | 1647 | |
1651 | 1648 | =item chop |
1652 | 1649 | |
1653 | 1650 | =begin original |
1654 | 1651 | |
1655 | 1652 | Chops off the last character of a string and returns the character |
1656 | 1653 | chopped. It is much more efficient than C<s/.$//s> because it neither |
1657 | 1654 | scans nor copies the string. If VARIABLE is omitted, chops C<$_>. |
1658 | 1655 | If VARIABLE is a hash, it chops the hash's values, but not its keys. |
1659 | 1656 | |
1660 | 1657 | =end original |
1661 | 1658 | |
1662 | 1659 | 文字列の最後の文字を切り捨てて、その切り取った文字を返します。 |
1663 | 1660 | 文字列の検索もコピーも行ないませんので |
1664 | 1661 | C<s/.$//s> よりも、ずっと効率的です。 |
1665 | 1662 | VARIABLE が省略されると、C<$_> を対象として chop します。 |
1666 | 1663 | VARIABLE がハッシュの場合、ハッシュの value を chop しますが、 |
1667 | 1664 | key は chop しません。 |
1668 | 1665 | |
1669 | 1666 | =begin original |
1670 | 1667 | |
1671 | 1668 | You can actually chop anything that's an lvalue, including an assignment. |
1672 | 1669 | |
1673 | 1670 | =end original |
1674 | 1671 | |
1675 | 1672 | 実際のところ、代入を含む左辺値となりうるなんでも chop できます。 |
1676 | 1673 | |
1677 | 1674 | =begin original |
1678 | 1675 | |
1679 | 1676 | If you chop a list, each element is chopped. Only the value of the |
1680 | 1677 | last C<chop> is returned. |
1681 | 1678 | |
1682 | 1679 | =end original |
1683 | 1680 | |
1684 | 1681 | リストを chop すると、個々の要素が chop されます。 |
1685 | 1682 | 最後の C<chop> の値だけが返されます。 |
1686 | 1683 | |
1687 | 1684 | =begin original |
1688 | 1685 | |
1689 | 1686 | Note that C<chop> returns the last character. To return all but the last |
1690 | 1687 | character, use C<substr($string, 0, -1)>. |
1691 | 1688 | |
1692 | 1689 | =end original |
1693 | 1690 | |
1694 | 1691 | C<chop> は最後の文字を返すことに注意してください。 |
1695 | 1692 | 最後以外の全ての文字を返すためには、C<substr($string, 0, -1)> を |
1696 | 1693 | 使ってください。 |
1697 | 1694 | |
1698 | 1695 | =begin original |
1699 | 1696 | |
1700 | 1697 | See also L</chomp>. |
1701 | 1698 | |
1702 | 1699 | =end original |
1703 | 1700 | |
1704 | 1701 | L</chomp> も参照してください。 |
1705 | 1702 | |
1706 | 1703 | =item chown LIST |
1707 | 1704 | X<chown> X<owner> X<user> X<group> |
1708 | 1705 | |
1709 | 1706 | =begin original |
1710 | 1707 | |
1711 | 1708 | Changes the owner (and group) of a list of files. The first two |
1712 | 1709 | elements of the list must be the I<numeric> uid and gid, in that |
1713 | 1710 | order. A value of -1 in either position is interpreted by most |
1714 | 1711 | systems to leave that value unchanged. Returns the number of files |
1715 | 1712 | successfully changed. |
1716 | 1713 | |
1717 | 1714 | =end original |
1718 | 1715 | |
1719 | 1716 | LIST に含まれるファイルの所有者 (とグループ) を変更します。 |
1720 | 1717 | LIST の最初の 2 つの要素には、I<数値表現> の uid と gid を |
1721 | 1718 | この順序で与えなければなりません。 |
1722 | 1719 | どちらかの値を -1 にすると、ほとんどのシステムではその値は |
1723 | 1720 | 変更しないと解釈します。 |
1724 | 1721 | 変更に成功したファイルの数が返されます。 |
1725 | 1722 | |
1726 | 1723 | $cnt = chown $uid, $gid, 'foo', 'bar'; |
1727 | 1724 | chown $uid, $gid, @filenames; |
1728 | 1725 | |
1729 | 1726 | =begin original |
1730 | 1727 | |
1731 | 1728 | On systems that support fchown, you might pass file handles among the |
1732 | 1729 | files. On systems that don't support fchown, passing file handles |
1733 | 1730 | produces a fatal error at run time. The file handles must be passed |
1734 | 1731 | as globs or references to be recognized. Barewords are considered |
1735 | 1732 | file names. |
1736 | 1733 | |
1737 | 1734 | =end original |
1738 | 1735 | |
1739 | 1736 | fchown に対応しているシステムでは、ファイルハンドルを引数として渡せます。 |
1740 | 1737 | fchown に対応していないシステムでは、ファイルハンドルを渡すと実行時に |
1741 | 1738 | 致命的エラーになります。 |
1742 | 1739 | ファイルハンドルを認識させるためには、グロブまたはリファレンスとして |
1743 | 1740 | 渡されなければなりません。 |
1744 | 1741 | 裸の単語はファイル名として扱われます。 |
1745 | 1742 | |
1746 | 1743 | =begin original |
1747 | 1744 | |
1748 | 1745 | Here's an example that looks up nonnumeric uids in the passwd file: |
1749 | 1746 | |
1750 | 1747 | =end original |
1751 | 1748 | |
1752 | 1749 | passwd ファイルから数値表現でない uid を検索する例を |
1753 | 1750 | 示します: |
1754 | 1751 | |
1755 | 1752 | print "User: "; |
1756 | 1753 | chomp($user = <STDIN>); |
1757 | 1754 | print "Files: "; |
1758 | 1755 | chomp($pattern = <STDIN>); |
1759 | 1756 | |
1760 | 1757 | ($login,$pass,$uid,$gid) = getpwnam($user) |
1761 | 1758 | or die "$user not in passwd file"; |
1762 | 1759 | |
1763 | 1760 | @ary = glob($pattern); # expand filenames |
1764 | 1761 | chown $uid, $gid, @ary; |
1765 | 1762 | |
1766 | 1763 | =begin original |
1767 | 1764 | |
1768 | 1765 | On most systems, you are not allowed to change the ownership of the |
1769 | 1766 | file unless you're the superuser, although you should be able to change |
1770 | 1767 | the group to any of your secondary groups. On insecure systems, these |
1771 | 1768 | restrictions may be relaxed, but this is not a portable assumption. |
1772 | 1769 | On POSIX systems, you can detect this condition this way: |
1773 | 1770 | |
1774 | 1771 | =end original |
1775 | 1772 | |
1776 | 1773 | ほとんどのシステムでは、スーパーユーザーだけがファイルの所有者を |
1777 | 1774 | 変更できますが、グループは実行者の副グループに変更できるべきです。 |
1778 | 1775 | 安全でないシステムでは、この制限はゆるめられています。 |
1779 | 1776 | しかしこれは移植性のある仮定ではありません。 |
1780 | 1777 | POSIX システムでは、以下のようにしてこの条件を検出できます: |
1781 | 1778 | |
1782 | 1779 | use POSIX qw(sysconf _PC_CHOWN_RESTRICTED); |
1783 | 1780 | $can_chown_giveaway = not sysconf(_PC_CHOWN_RESTRICTED); |
1784 | 1781 | |
1785 | 1782 | =item chr NUMBER |
1786 | 1783 | X<chr> X<character> X<ASCII> X<Unicode> |
1787 | 1784 | |
1788 | 1785 | =item chr |
1789 | 1786 | |
1790 | 1787 | =begin original |
1791 | 1788 | |
1792 | 1789 | Returns the character represented by that NUMBER in the character set. |
1793 | 1790 | For example, C<chr(65)> is C<"A"> in either ASCII or Unicode, and |
1794 | 1791 | chr(0x263a) is a Unicode smiley face. |
1795 | 1792 | |
1796 | 1793 | =end original |
1797 | 1794 | |
1798 | 1795 | 特定の文字セットでの NUMBER で表わされる文字を返します。 |
1799 | 1796 | たとえば、C<chr(65)> は ASCII と Unicode の両方で C<"A"> となります。 |
1800 | 1797 | chr(0x263a) は Unicode のスマイリーフェイスです。 |
1801 | 1798 | |
1802 | 1799 | =begin original |
1803 | 1800 | |
1804 | 1801 | Negative values give the Unicode replacement character (chr(0xfffd)), |
1805 | 1802 | except under the L<bytes> pragma, where low eight bits of the value |
1806 | 1803 | (truncated to an integer) are used. |
1807 | 1804 | |
1808 | 1805 | =end original |
1809 | 1806 | |
1810 | 1807 | 負の数は Unicode の置換文字 (chr(0xfffd)) を与えますが、 |
1811 | 1808 | L<bytes> プラグマの影響下では、(integer に切り詰められた)値の下位 8 ビットが |
1812 | 1809 | 使われます。 |
1813 | 1810 | |
1814 | 1811 | =begin original |
1815 | 1812 | |
1816 | 1813 | If NUMBER is omitted, uses C<$_>. |
1817 | 1814 | |
1818 | 1815 | =end original |
1819 | 1816 | |
1820 | 1817 | NUMBER が省略された場合、C<$_> を使います。 |
1821 | 1818 | |
1822 | 1819 | =begin original |
1823 | 1820 | |
1824 | 1821 | For the reverse, use L</ord>. |
1825 | 1822 | |
1826 | 1823 | =end original |
1827 | 1824 | |
1828 | 1825 | 逆を行うためには、L</ord> を参照してください。 |
1829 | 1826 | |
1830 | 1827 | =begin original |
1831 | 1828 | |
1832 | 1829 | Note that characters from 128 to 255 (inclusive) are by default |
1833 | 1830 | internally not encoded as UTF-8 for backward compatibility reasons. |
1834 | 1831 | |
1835 | 1832 | =end original |
1836 | 1833 | |
1837 | 1834 | 128 から 255 までの文字は過去との互換性のために |
1838 | 1835 | デフォルトでは UTF-8 Unicode にエンコードされません。 |
1839 | 1836 | |
1840 | 1837 | =begin original |
1841 | 1838 | |
1842 | 1839 | See L<perlunicode> for more about Unicode. |
1843 | 1840 | |
1844 | 1841 | =end original |
1845 | 1842 | |
1846 | 1843 | Unicode についてもっと知りたいなら、L<perlunicode> を |
1847 | 1844 | 参照してください。 |
1848 | 1845 | |
1849 | 1846 | =item chroot FILENAME |
1850 | 1847 | X<chroot> X<root> |
1851 | 1848 | |
1852 | 1849 | =item chroot |
1853 | 1850 | |
1854 | 1851 | =begin original |
1855 | 1852 | |
1856 | 1853 | This function works like the system call by the same name: it makes the |
1857 | 1854 | named directory the new root directory for all further pathnames that |
1858 | 1855 | begin with a C</> by your process and all its children. (It doesn't |
1859 | 1856 | change your current working directory, which is unaffected.) For security |
1860 | 1857 | reasons, this call is restricted to the superuser. If FILENAME is |
1861 | 1858 | omitted, does a C<chroot> to C<$_>. |
1862 | 1859 | |
1863 | 1860 | =end original |
1864 | 1861 | |
1865 | 1862 | 同じ名前のシステムコールと同じことをします。 |
1866 | 1863 | 現在のプロセス及び子プロセスに対して、C</>で始まるパス名に関して |
1867 | 1864 | 指定されたディレクトリを新しいルートディレクトリとして扱います。 |
1868 | 1865 | (これはカレントディレクトリを変更しません。カレントディレクトリはそのままです)。 |
1869 | 1866 | セキュリティ上の理由により、この呼び出しはスーパーユーザーしか行えません。 |
1870 | 1867 | FILENAME を省略すると、C<$_> へ C<chroot> します。 |
1871 | 1868 | |
1872 | 1869 | =item close FILEHANDLE |
1873 | 1870 | X<close> |
1874 | 1871 | |
1875 | 1872 | =item close |
1876 | 1873 | |
1877 | 1874 | =begin original |
1878 | 1875 | |
1879 | 1876 | Closes the file or pipe associated with the file handle, flushes the IO |
1880 | 1877 | buffers, and closes the system file descriptor. Returns true if those |
1881 | 1878 | operations have succeeded and if no error was reported by any PerlIO |
1882 | 1879 | layer. Closes the currently selected filehandle if the argument is |
1883 | 1880 | omitted. |
1884 | 1881 | |
1885 | 1882 | =end original |
1886 | 1883 | |
1887 | 1884 | FILEHANDLE に対応したファイルまたはパイプをクローズして、 |
1888 | 1885 | IO バッファをフラッシュし、システムファイル記述子をクローズします。 |
1889 | 1886 | 操作が成功し、PerlIO 層からエラーが報告されなかった場合に真を返します。 |
1890 | 1887 | 引数が省略された場合、現在選択されているファイルハンドルをクローズします。 |
1891 | 1888 | |
1892 | 1889 | =begin original |
1893 | 1890 | |
1894 | 1891 | You don't have to close FILEHANDLE if you are immediately going to do |
1895 | 1892 | another C<open> on it, because C<open> will close it for you. (See |
1896 | 1893 | C<open>.) However, an explicit C<close> on an input file resets the line |
1897 | 1894 | counter (C<$.>), while the implicit close done by C<open> does not. |
1898 | 1895 | |
1899 | 1896 | =end original |
1900 | 1897 | |
1901 | 1898 | クローズしてすぐにまた、同じファイルハンドルに |
1902 | 1899 | 対してオープンを行なう場合には、C<open> が自動的に C<close> |
1903 | 1900 | を行ないますので、close FILEHANDLE する必要はありません |
1904 | 1901 | (C<open> を参照してください)。 |
1905 | 1902 | ただし、明示的にクローズを行なったときにのみ入力ファイルの |
1906 | 1903 | 行番号 (C<$.>) のリセットが行なわれ、C<open> によって行なわれる |
1907 | 1904 | 暗黙の C<close>では行なわれません。 |
1908 | 1905 | |
1909 | 1906 | =begin original |
1910 | 1907 | |
1911 | 1908 | If the file handle came from a piped open, C<close> will additionally |
1912 | 1909 | return false if one of the other system calls involved fails, or if the |
1913 | 1910 | program exits with non-zero status. (If the only problem was that the |
1914 | 1911 | program exited non-zero, C<$!> will be set to C<0>.) Closing a pipe |
1915 | 1912 | also waits for the process executing on the pipe to complete, in case you |
1916 | 1913 | want to look at the output of the pipe afterwards, and |
1917 | 1914 | implicitly puts the exit status value of that command into C<$?> and |
1918 | 1915 | C<${^CHILD_ERROR_NATIVE}>. |
1919 | 1916 | |
1920 | 1917 | =end original |
1921 | 1918 | |
1922 | 1919 | ファイルハンドルがパイプつきオープンなら、 |
1923 | 1920 | C<close> はその他のシステムコールが失敗したり |
1924 | 1921 | プログラムが非ゼロのステータスで終了した場合にも偽を返します |
1925 | 1922 | (プログラムが非ゼロで終了しただけの場合は、C<$!>がC<0>にセットされます)。 |
1926 | 1923 | 後でパイプの出力を見たい場合のために、 |
1927 | 1924 | パイプのクローズでは、パイプ上で実行されている |
1928 | 1925 | プロセスの完了を待ちます。 |
1929 | 1926 | また自動的にコマンドのステータス値を C<$?> と |
1930 | 1927 | C<${^CHILD_ERROR_NATIVE}> に設定します。 |
1931 | 1928 | |
1932 | 1929 | =begin original |
1933 | 1930 | |
1934 | 1931 | Prematurely closing the read end of a pipe (i.e. before the process |
1935 | 1932 | writing to it at the other end has closed it) will result in a |
1936 | 1933 | SIGPIPE being delivered to the writer. If the other end can't |
1937 | 1934 | handle that, be sure to read all the data before closing the pipe. |
1938 | 1935 | |
1939 | 1936 | =end original |
1940 | 1937 | |
1941 | 1938 | 途中で(つまり、書き込み側が閉じる前に) |
1942 | 1939 | パイプの読み込み側が閉じた場合、 |
1943 | 1940 | 書き込み側に SIGPIPE が配送されます。 |
1944 | 1941 | 書き込み側がこれを扱えない場合、パイプを閉じる前に |
1945 | 1942 | 確実に全てのデータが読み込まれるようにする必要があります。 |
1946 | 1943 | |
1947 | 1944 | =begin original |
1948 | 1945 | |
1949 | 1946 | Example: |
1950 | 1947 | |
1951 | 1948 | =end original |
1952 | 1949 | |
1953 | 1950 | 例: |
1954 | 1951 | |
1955 | 1952 | open(OUTPUT, '|sort >foo') # pipe to sort |
1956 | 1953 | or die "Can't start sort: $!"; |
1957 | 1954 | #... # print stuff to output |
1958 | 1955 | close OUTPUT # wait for sort to finish |
1959 | 1956 | or warn $! ? "Error closing sort pipe: $!" |
1960 | 1957 | : "Exit status $? from sort"; |
1961 | 1958 | open(INPUT, 'foo') # get sort's results |
1962 | 1959 | or die "Can't open 'foo' for input: $!"; |
1963 | 1960 | |
1964 | 1961 | =begin original |
1965 | 1962 | |
1966 | 1963 | FILEHANDLE may be an expression whose value can be used as an indirect |
1967 | 1964 | filehandle, usually the real filehandle name. |
1968 | 1965 | |
1969 | 1966 | =end original |
1970 | 1967 | |
1971 | 1968 | FILEHANDLE は式でもかまいません。この場合、値は間接ファイルハンドルと |
1972 | 1969 | して扱われ、普通は実際のファイルハンドル名です。 |
1973 | 1970 | |
1974 | 1971 | =item closedir DIRHANDLE |
1975 | 1972 | X<closedir> |
1976 | 1973 | |
1977 | 1974 | =begin original |
1978 | 1975 | |
1979 | 1976 | Closes a directory opened by C<opendir> and returns the success of that |
1980 | 1977 | system call. |
1981 | 1978 | |
1982 | 1979 | =end original |
1983 | 1980 | |
1984 | 1981 | C<opendir> でオープンしたディレクトリをクローズし、 |
1985 | 1982 | システムコールの返り値を返します。 |
1986 | 1983 | |
1987 | 1984 | =item connect SOCKET,NAME |
1988 | 1985 | X<connect> |
1989 | 1986 | |
1990 | 1987 | =begin original |
1991 | 1988 | |
1992 | 1989 | Attempts to connect to a remote socket, just as the connect system call |
1993 | 1990 | does. Returns true if it succeeded, false otherwise. NAME should be a |
1994 | 1991 | packed address of the appropriate type for the socket. See the examples in |
1995 | 1992 | L<perlipc/"Sockets: Client/Server Communication">. |
1996 | 1993 | |
1997 | 1994 | =end original |
1998 | 1995 | |
1999 | 1996 | connect(2) システムコールと同様に、リモートソケットへの接続を試みます。 |
2000 | 1997 | 成功時には真を返し、失敗時には偽を返します。 |
2001 | 1998 | NAME は、ソケットに対する、適切な型のパックされた |
2002 | 1999 | アドレスでなければなりません。 |
2003 | 2000 | L<perlipc/"Sockets: Client/Server Communication"> の例を参照してください。 |
2004 | 2001 | |
2005 | 2002 | =item continue BLOCK |
2006 | 2003 | X<continue> |
2007 | 2004 | |
2008 | 2005 | =item continue |
2009 | 2006 | |
2010 | 2007 | =begin original |
2011 | 2008 | |
2012 | 2009 | C<continue> is actually a flow control statement rather than a function. If |
2013 | 2010 | there is a C<continue> BLOCK attached to a BLOCK (typically in a C<while> or |
2014 | 2011 | C<foreach>), it is always executed just before the conditional is about to |
2015 | 2012 | be evaluated again, just like the third part of a C<for> loop in C. Thus |
2016 | 2013 | it can be used to increment a loop variable, even when the loop has been |
2017 | 2014 | continued via the C<next> statement (which is similar to the C C<continue> |
2018 | 2015 | statement). |
2019 | 2016 | |
2020 | 2017 | =end original |
2021 | 2018 | |
2022 | 2019 | C<continue> は実際には関数ではなく、実行制御文です。 |
2023 | 2020 | C<continue> BLOCK が BLOCK (典型的には C<while> または C<foreach> の中)にあると、 |
2024 | 2021 | これは条件文が再評価される直前に常に実行されます。 |
2025 | 2022 | これは C における C<for> ループの 3 番目の部分と同様です。 |
2026 | 2023 | 従って、これは C<next> 文 |
2027 | 2024 | (これは C の C<continue> 文と似ています)を使ってループが繰り返されるときでも |
2028 | 2025 | ループ変数を増やしたいときに使えます。 |
2029 | 2026 | |
2030 | 2027 | =begin original |
2031 | 2028 | |
2032 | 2029 | C<last>, C<next>, or C<redo> may appear within a C<continue> |
2033 | 2030 | block. C<last> and C<redo> will behave as if they had been executed within |
2034 | 2031 | the main block. So will C<next>, but since it will execute a C<continue> |
2035 | 2032 | block, it may be more entertaining. |
2036 | 2033 | |
2037 | 2034 | =end original |
2038 | 2035 | |
2039 | 2036 | C<last>, C<next>, C<redo> が C<continue> ブロック内に現れる可能性があります。 |
2040 | 2037 | C<last> と C<redo> はメインブロックの中で実行されたのと同じように振舞います。 |
2041 | 2038 | C<next> の場合は、C<continue> ブロックを実行することになるので、 |
2042 | 2039 | より面白いことになります。 |
2043 | 2040 | |
2044 | 2041 | while (EXPR) { |
2045 | 2042 | ### redo always comes here |
2046 | 2043 | do_something; |
2047 | 2044 | } continue { |
2048 | 2045 | ### next always comes here |
2049 | 2046 | do_something_else; |
2050 | 2047 | # then back the top to re-check EXPR |
2051 | 2048 | } |
2052 | 2049 | ### last always comes here |
2053 | 2050 | |
2054 | 2051 | =begin original |
2055 | 2052 | |
2056 | 2053 | Omitting the C<continue> section is semantically equivalent to using an |
2057 | 2054 | empty one, logically enough. In that case, C<next> goes directly back |
2058 | 2055 | to check the condition at the top of the loop. |
2059 | 2056 | |
2060 | 2057 | =end original |
2061 | 2058 | |
2062 | 2059 | C<continue> 節を省略するのは、文法的には空の節を指定したのと同じで、 |
2063 | 2060 | 論理的には十分です。 |
2064 | 2061 | この場合、C<next> は直接ループ先頭の条件チェックに戻ります。 |
2065 | 2062 | |
2066 | 2063 | =begin original |
2067 | 2064 | |
2068 | 2065 | If the "switch" feature is enabled, C<continue> is also a |
2069 | 2066 | function that will break out of the current C<when> or C<default> |
2070 | 2067 | block, and fall through to the next case. See L<feature> and |
2071 | 2068 | L<perlsyn/"Switch statements"> for more information. |
2072 | 2069 | |
2073 | 2070 | =end original |
2074 | 2071 | |
2075 | 2072 | "switch" 機能が有効なら、C<continue> は現在の C<when> や C<default> の |
2076 | 2073 | ブロックから飛び出して、次の場合に移動するための文となります。 |
2077 | 2074 | さらなる情報については L<feature> と L<perlsyn/"Switch statements"> を |
2078 | 2075 | 参照してください。 |
2079 | 2076 | |
2080 | 2077 | =item cos EXPR |
2081 | 2078 | X<cos> X<cosine> X<acos> X<arccosine> |
2082 | 2079 | |
2083 | 2080 | =item cos |
2084 | 2081 | |
2085 | 2082 | =begin original |
2086 | 2083 | |
2087 | 2084 | Returns the cosine of EXPR (expressed in radians). If EXPR is omitted, |
2088 | 2085 | takes cosine of C<$_>. |
2089 | 2086 | |
2090 | 2087 | =end original |
2091 | 2088 | |
2092 | 2089 | (ラジアンで示した) EXPR の余弦を返します。 |
2093 | 2090 | EXPR が省略されたときには、C<$_> の余弦を取ります。 |
2094 | 2091 | |
2095 | 2092 | =begin original |
2096 | 2093 | |
2097 | 2094 | For the inverse cosine operation, you may use the C<Math::Trig::acos()> |
2098 | 2095 | function, or use this relation: |
2099 | 2096 | |
2100 | 2097 | =end original |
2101 | 2098 | |
2102 | 2099 | 逆余弦を求めるためには、C<Math::Trig::acos()> 関数を使うか、 |
2103 | 2100 | 以下の関係を使ってください。 |
2104 | 2101 | |
2105 | 2102 | sub acos { atan2( sqrt(1 - $_[0] * $_[0]), $_[0] ) } |
2106 | 2103 | |
2107 | 2104 | =item crypt PLAINTEXT,SALT |
2108 | 2105 | X<crypt> X<digest> X<hash> X<salt> X<plaintext> X<password> |
2109 | 2106 | X<decrypt> X<cryptography> X<passwd> X<encrypt> |
2110 | 2107 | |
2111 | 2108 | =begin original |
2112 | 2109 | |
2113 | 2110 | Creates a digest string exactly like the crypt(3) function in the C |
2114 | 2111 | library (assuming that you actually have a version there that has not |
2115 | been extirpated as a potential munition). | |
2112 | been extirpated as a potential munitions). | |
2116 | 2113 | |
2117 | 2114 | =end original |
2118 | 2115 | |
2119 | 2116 | C ライブラリの crypt(3) 関数と全く同じように、ダイジェスト文字列を |
2120 | 2117 | 作成します(一時的な必需品として、まだ絶滅していないバージョンを |
2121 | 2118 | 持っていると仮定しています)。 |
2122 | 2119 | |
2123 | 2120 | =begin original |
2124 | 2121 | |
2125 | 2122 | crypt() is a one-way hash function. The PLAINTEXT and SALT is turned |
2126 | 2123 | into a short string, called a digest, which is returned. The same |
2127 | 2124 | PLAINTEXT and SALT will always return the same string, but there is no |
2128 | 2125 | (known) way to get the original PLAINTEXT from the hash. Small |
2129 | 2126 | changes in the PLAINTEXT or SALT will result in large changes in the |
2130 | 2127 | digest. |
2131 | 2128 | |
2132 | 2129 | =end original |
2133 | 2130 | |
2134 | 2131 | crypt() は一方向ハッシュ関数です。 |
2135 | 2132 | PLAINTEXT と SALT はダイジェストと呼ばれる短い文字列に変えられて、 |
2136 | 2133 | それが返されます。 |
2137 | 2134 | PLAINTEXT と SALT が同じ場合は常に同じ文字列を返しますが、ハッシュから |
2138 | 2135 | 元の PLAINTEXT を得る(既知の)方法はありません。 |
2139 | 2136 | PLAINTEXT や SALT を少し変更してもダイジェストは大きく変更されます。 |
2140 | 2137 | |
2141 | 2138 | =begin original |
2142 | 2139 | |
2143 | 2140 | There is no decrypt function. This function isn't all that useful for |
2144 | 2141 | cryptography (for that, look for F<Crypt> modules on your nearby CPAN |
2145 | 2142 | mirror) and the name "crypt" is a bit of a misnomer. Instead it is |
2146 | 2143 | primarily used to check if two pieces of text are the same without |
2147 | 2144 | having to transmit or store the text itself. An example is checking |
2148 | 2145 | if a correct password is given. The digest of the password is stored, |
2149 | 2146 | not the password itself. The user types in a password that is |
2150 | 2147 | crypt()'d with the same salt as the stored digest. If the two digests |
2151 | 2148 | match the password is correct. |
2152 | 2149 | |
2153 | 2150 | =end original |
2154 | 2151 | |
2155 | 2152 | 復号化関数はありません。 |
2156 | 2153 | この関数は暗号化のためにはまったく役に立ちません(このためには、 |
2157 | 2154 | お近くの CPAN ミラーで F<Crypt> モジュールを探してください)ので、 |
2158 | 2155 | "crypt" という名前は少し間違った名前です。 |
2159 | 2156 | その代わりに、一般的には二つのテキスト片が同じかどうかをテキストそのものを |
2160 | 2157 | 転送したり保管したりせずにチェックするために使います。 |
2161 | 2158 | 例としては、正しいパスワードが与えられたかどうかをチェックがあります。 |
2162 | 2159 | パスワード自身ではなく、パスワードのダイジェストが保管されます。 |
2163 | 2160 | ユーザーがパスワードを入力すると、保管されているダイジェストと同じ |
2164 | 2161 | salt で crypt() します。 |
2165 | 2162 | 二つのダイジェストが同じなら、パスワードは正しいです。 |
2166 | 2163 | |
2167 | 2164 | =begin original |
2168 | 2165 | |
2169 | 2166 | When verifying an existing digest string you should use the digest as |
2170 | 2167 | the salt (like C<crypt($plain, $digest) eq $digest>). The SALT used |
2171 | 2168 | to create the digest is visible as part of the digest. This ensures |
2172 | 2169 | crypt() will hash the new string with the same salt as the digest. |
2173 | 2170 | This allows your code to work with the standard L<crypt|/crypt> and |
2174 | 2171 | with more exotic implementations. In other words, do not assume |
2175 | 2172 | anything about the returned string itself, or how many bytes in the |
2176 | 2173 | digest matter. |
2177 | 2174 | |
2178 | 2175 | =end original |
2179 | 2176 | |
2180 | 2177 | すでにあるダイジェスト文字列を検証するには、ダイジェストを |
2181 | 2178 | (C<crypt($plain, $digest) eq $digest> のようにして)salt として使います。 |
2182 | 2179 | ダイジェストを作るのに使われた SALT はダイジェストの一部として見えます。 |
2183 | 2180 | これにより、crypt() は同じ salt で新しい文字列をダイジェストとして |
2184 | 2181 | ハッシュ化できるようにします。 |
2185 | 2182 | これによって標準的な C<crypt|/crypt> や、より風変わりな実装でも動作します。 |
2186 | 2183 | 言い換えると、返される文字列そのものや、ダイジェスト文字列が |
2187 | 2184 | 何バイトあるかといったことに対して、どのような仮定もしてはいけません。 |
2188 | 2185 | |
2189 | 2186 | =begin original |
2190 | 2187 | |
2191 | 2188 | Traditionally the result is a string of 13 bytes: two first bytes of |
2192 | 2189 | the salt, followed by 11 bytes from the set C<[./0-9A-Za-z]>, and only |
2193 | the first eight bytes of | |
2190 | the first eight bytes of the digest string mattered, but alternative | |
2194 | 2191 | hashing schemes (like MD5), higher level security schemes (like C2), |
2195 | 2192 | and implementations on non-UNIX platforms may produce different |
2196 | 2193 | strings. |
2197 | 2194 | |
2198 | 2195 | =end original |
2199 | 2196 | |
2200 | 2197 | 伝統的には結果は 13 バイトの文字列です: 最初の 2 バイトは salt、引き続いて |
2201 | 集合 C<[./0-9A-Za-z]> からの 11 バイトで、 | |
2198 | 集合 C<[./0-9A-Za-z]> からの 11 バイトで、ダイジェスト文字列の最初の | |
2202 | 8 バイトだけが意味があります | |
2199 | 8 バイトだけが意味がありますが、(MD5 のように) 異なったハッシュ手法、 | |
2203 | しかし、(MD5 のように) 異なったハッシュ手法、 | |
2204 | 2200 | (C2 のような) 高レベルセキュリティ手法、非 UNIX プラットフォームでの |
2205 | 2201 | 実装などでは異なった文字列が生成されることがあります。 |
2206 | 2202 | |
2207 | 2203 | =begin original |
2208 | 2204 | |
2209 | 2205 | When choosing a new salt create a random two character string whose |
2210 | 2206 | characters come from the set C<[./0-9A-Za-z]> (like C<join '', ('.', |
2211 | 2207 | '/', 0..9, 'A'..'Z', 'a'..'z')[rand 64, rand 64]>). This set of |
2212 | 2208 | characters is just a recommendation; the characters allowed in |
2213 | 2209 | the salt depend solely on your system's crypt library, and Perl can't |
2214 | 2210 | restrict what salts C<crypt()> accepts. |
2215 | 2211 | |
2216 | 2212 | =end original |
2217 | 2213 | |
2218 | 2214 | 新しい salt を選択する場合は、集合 C<[./0-9A-Za-z]> から |
2219 | 2215 | (C<join '', ('.', '/', 0..9, 'A'..'Z', 'a'..'z')[rand 64, rand 64]> のようにして) |
2220 | 2216 | ランダムに2 つの文字を選びます。 |
2221 | 2217 | この文字集合は単なる推薦です; salt として許される文字はシステムの暗号化 |
2222 | 2218 | ライブラリだけに依存し、Perl は C<crypt()> がどのような salt を受け付けるかに |
2223 | 2219 | ついて制限しません。 |
2224 | 2220 | |
2225 | 2221 | =begin original |
2226 | 2222 | |
2227 | 2223 | Here's an example that makes sure that whoever runs this program knows |
2228 | 2224 | their password: |
2229 | 2225 | |
2230 | 2226 | =end original |
2231 | 2227 | |
2232 | 2228 | プログラムを実行する人が、 |
2233 | 2229 | 自分のパスワードを知っていることを確認する例です: |
2234 | 2230 | |
2235 | 2231 | $pwd = (getpwuid($<))[1]; |
2236 | 2232 | |
2237 | 2233 | system "stty -echo"; |
2238 | 2234 | print "Password: "; |
2239 | 2235 | chomp($word = <STDIN>); |
2240 | 2236 | print "\n"; |
2241 | 2237 | system "stty echo"; |
2242 | 2238 | |
2243 | 2239 | if (crypt($word, $pwd) ne $pwd) { |
2244 | 2240 | die "Sorry...\n"; |
2245 | 2241 | } else { |
2246 | 2242 | print "ok\n"; |
2247 | 2243 | } |
2248 | 2244 | |
2249 | 2245 | =begin original |
2250 | 2246 | |
2251 | 2247 | Of course, typing in your own password to whoever asks you |
2252 | 2248 | for it is unwise. |
2253 | 2249 | |
2254 | 2250 | =end original |
2255 | 2251 | |
2256 | 2252 | もちろん、自分自身のパスワードを誰にでも入力するのは賢明ではありません。 |
2257 | 2253 | |
2258 | 2254 | =begin original |
2259 | 2255 | |
2260 | 2256 | The L<crypt|/crypt> function is unsuitable for hashing large quantities |
2261 | 2257 | of data, not least of all because you can't get the information |
2262 | 2258 | back. Look at the L<Digest> module for more robust algorithms. |
2263 | 2259 | |
2264 | 2260 | =end original |
2265 | 2261 | |
2266 | 2262 | L<crypt|/crypt> 関数は大量のデータのハッシュ化には向いていません。 |
2267 | 2263 | これは情報を戻せないという理由だけではありません。 |
2268 | 2264 | より頑強なアルゴリズムについては L<Digest> モジュールを参照してください。 |
2269 | 2265 | |
2270 | 2266 | =begin original |
2271 | 2267 | |
2272 | 2268 | If using crypt() on a Unicode string (which I<potentially> has |
2273 | 2269 | characters with codepoints above 255), Perl tries to make sense |
2274 | 2270 | of the situation by trying to downgrade (a copy of the string) |
2275 | 2271 | the string back to an eight-bit byte string before calling crypt() |
2276 | 2272 | (on that copy). If that works, good. If not, crypt() dies with |
2277 | 2273 | C<Wide character in crypt>. |
2278 | 2274 | |
2279 | 2275 | =end original |
2280 | 2276 | |
2281 | 2277 | Unicode 文字列(I<潜在的には> 255 を越えるコードポイントを持つ文字を |
2282 | 2278 | 含みます)に crypt() を使った場合、Perl は crypt() を呼び出す前に与えられた |
2283 | 2279 | 文字列を8 ビットバイト文字列にダウングレードする(文字列のコピーを作る) |
2284 | 2280 | ことで状況のつじつまを合わせようとします。 |
2285 | 2281 | うまく動けば、それでよし。動かなければ、crypt() は |
2286 | 2282 | C<Wide character in crypt> というメッセージと共に die します。 |
2287 | 2283 | |
2288 | 2284 | =item dbmclose HASH |
2289 | 2285 | X<dbmclose> |
2290 | 2286 | |
2291 | 2287 | =begin original |
2292 | 2288 | |
2293 | 2289 | [This function has been largely superseded by the C<untie> function.] |
2294 | 2290 | |
2295 | 2291 | =end original |
2296 | 2292 | |
2297 | 2293 | [この関数は、C<untie> 関数に大きくとって代わられました。] |
2298 | 2294 | |
2299 | 2295 | =begin original |
2300 | 2296 | |
2301 | 2297 | Breaks the binding between a DBM file and a hash. |
2302 | 2298 | |
2303 | 2299 | =end original |
2304 | 2300 | |
2305 | 2301 | DBM ファイルとハッシュの連結をはずします。 |
2306 | 2302 | |
2307 | 2303 | =item dbmopen HASH,DBNAME,MASK |
2308 | 2304 | X<dbmopen> X<dbm> X<ndbm> X<sdbm> X<gdbm> |
2309 | 2305 | |
2310 | 2306 | =begin original |
2311 | 2307 | |
2312 | 2308 | [This function has been largely superseded by the C<tie> function.] |
2313 | 2309 | |
2314 | 2310 | =end original |
2315 | 2311 | |
2316 | 2312 | [この関数は、C<tie> 関数に大きくとって代わられました。] |
2317 | 2313 | |
2318 | 2314 | =begin original |
2319 | 2315 | |
2320 | 2316 | This binds a dbm(3), ndbm(3), sdbm(3), gdbm(3), or Berkeley DB file to a |
2321 | 2317 | hash. HASH is the name of the hash. (Unlike normal C<open>, the first |
2322 | 2318 | argument is I<not> a filehandle, even though it looks like one). DBNAME |
2323 | 2319 | is the name of the database (without the F<.dir> or F<.pag> extension if |
2324 | 2320 | any). If the database does not exist, it is created with protection |
2325 | 2321 | specified by MASK (as modified by the C<umask>). If your system supports |
2326 | 2322 | only the older DBM functions, you may perform only one C<dbmopen> in your |
2327 | 2323 | program. In older versions of Perl, if your system had neither DBM nor |
2328 | 2324 | ndbm, calling C<dbmopen> produced a fatal error; it now falls back to |
2329 | 2325 | sdbm(3). |
2330 | 2326 | |
2331 | 2327 | =end original |
2332 | 2328 | |
2333 | 2329 | dbm(3), ndbm(3), sdbm(3), gdbm(3) ファイルまたは Berkeley DB |
2334 | 2330 | ファイルを連想配列に結び付けます。 |
2335 | 2331 | HASH は、その連想配列の名前です。 |
2336 | 2332 | (普通の C<open> とは違って、最初の引数はファイルハンドル |
2337 | 2333 | I<ではありません>。まあ、似たようなものですが。) |
2338 | 2334 | DBNAME は、データベースの名前です (拡張子の .dir や |
2339 | 2335 | .pag はもしあってもつけません)。 |
2340 | 2336 | データベースが存在しなければ、MODE MASK (を C<umask> で修正したもの) で |
2341 | 2337 | 指定されたモードで作られます。 |
2342 | 2338 | 古い DBM 関数のみをサポートしているシステムでは、プログラム中で 1 度だけ |
2343 | 2339 | dbmopen() を実行することができます。 |
2344 | 2340 | 昔のバージョンの Perl では、DBM も ndbm も持っていないシステムでは、 |
2345 | 2341 | dbmopen() を呼び出すと致命的エラーになります。 |
2346 | 2342 | 現在では sdbm(3) にフォールバックします。 |
2347 | 2343 | |
2348 | 2344 | =begin original |
2349 | 2345 | |
2350 | 2346 | If you don't have write access to the DBM file, you can only read hash |
2351 | 2347 | variables, not set them. If you want to test whether you can write, |
2352 | 2348 | either use file tests or try setting a dummy hash entry inside an C<eval>, |
2353 | 2349 | which will trap the error. |
2354 | 2350 | |
2355 | 2351 | =end original |
2356 | 2352 | |
2357 | 2353 | DBM ファイルに対して、書き込み権が無いときには、ハッシュ |
2358 | 2354 | 配列を読みだすことだけができ、設定することはできません。 |
2359 | 2355 | 書けるか否かを調べたい場合には、ファイルテスト |
2360 | 2356 | 演算子を使うか、エラーをトラップしてくれる、C<eval> |
2361 | 2357 | の中で、ダミーのハッシュエントリを設定してみることになります。 |
2362 | 2358 | |
2363 | 2359 | =begin original |
2364 | 2360 | |
2365 | 2361 | Note that functions such as C<keys> and C<values> may return huge lists |
2366 | 2362 | when used on large DBM files. You may prefer to use the C<each> |
2367 | 2363 | function to iterate over large DBM files. Example: |
2368 | 2364 | |
2369 | 2365 | =end original |
2370 | 2366 | |
2371 | 2367 | 大きな DBM ファイルを扱うときには、C<keys> や C<values> のような関数は、 |
2372 | 2368 | 巨大なリストを返します。 |
2373 | 2369 | 大きな DBM ファイルでは、C<each> 関数を使って繰り返しを行なった方が |
2374 | 2370 | 良いかもしれません。 |
2375 | 2371 | 例: |
2376 | 2372 | |
2377 | 2373 | # print out history file offsets |
2378 | 2374 | dbmopen(%HIST,'/usr/lib/news/history',0666); |
2379 | 2375 | while (($key,$val) = each %HIST) { |
2380 | 2376 | print $key, ' = ', unpack('L',$val), "\n"; |
2381 | 2377 | } |
2382 | 2378 | dbmclose(%HIST); |
2383 | 2379 | |
2384 | 2380 | =begin original |
2385 | 2381 | |
2386 | 2382 | See also L<AnyDBM_File> for a more general description of the pros and |
2387 | 2383 | cons of the various dbm approaches, as well as L<DB_File> for a particularly |
2388 | 2384 | rich implementation. |
2389 | 2385 | |
2390 | 2386 | =end original |
2391 | 2387 | |
2392 | 2388 | 様々な dbm 手法に対する利点欠点に関するより一般的な記述および |
2393 | 2389 | 特にリッチな実装である L<DB_File> に関しては |
2394 | 2390 | L<AnyDBM_File> も参照してください。 |
2395 | 2391 | |
2396 | 2392 | =begin original |
2397 | 2393 | |
2398 | 2394 | You can control which DBM library you use by loading that library |
2399 | 2395 | before you call dbmopen(): |
2400 | 2396 | |
2401 | 2397 | =end original |
2402 | 2398 | |
2403 | 2399 | dbmopen() を呼び出す前にライブラリを読み込むことで、 |
2404 | 2400 | どの DBM ライブラリを使うかを制御できます: |
2405 | 2401 | |
2406 | 2402 | use DB_File; |
2407 | 2403 | dbmopen(%NS_Hist, "$ENV{HOME}/.netscape/history.db") |
2408 | 2404 | or die "Can't open netscape history file: $!"; |
2409 | 2405 | |
2410 | 2406 | =item defined EXPR |
2411 | 2407 | X<defined> X<undef> X<undefined> |
2412 | 2408 | |
2413 | 2409 | =item defined |
2414 | 2410 | |
2415 | 2411 | =begin original |
2416 | 2412 | |
2417 | 2413 | Returns a Boolean value telling whether EXPR has a value other than |
2418 | 2414 | the undefined value C<undef>. If EXPR is not present, C<$_> will be |
2419 | 2415 | checked. |
2420 | 2416 | |
2421 | 2417 | =end original |
2422 | 2418 | |
2423 | 2419 | 左辺値 EXPR が未定義値 C<undef> 以外の値を持つか否かを示す、ブール値を |
2424 | 2420 | 返します。 |
2425 | 2421 | EXPR がない場合は、C<$_> がチェックされます。 |
2426 | 2422 | |
2427 | 2423 | =begin original |
2428 | 2424 | |
2429 | 2425 | Many operations return C<undef> to indicate failure, end of file, |
2430 | 2426 | system error, uninitialized variable, and other exceptional |
2431 | 2427 | conditions. This function allows you to distinguish C<undef> from |
2432 | 2428 | other values. (A simple Boolean test will not distinguish among |
2433 | 2429 | C<undef>, zero, the empty string, and C<"0">, which are all equally |
2434 | 2430 | false.) Note that since C<undef> is a valid scalar, its presence |
2435 | 2431 | doesn't I<necessarily> indicate an exceptional condition: C<pop> |
2436 | 2432 | returns C<undef> when its argument is an empty array, I<or> when the |
2437 | 2433 | element to return happens to be C<undef>. |
2438 | 2434 | |
2439 | 2435 | =end original |
2440 | 2436 | |
2441 | 2437 | 多くの演算子が、EOF や未初期化変数、システムエラーといった、 |
2442 | 2438 | 例外的な条件で C<undef> を返すようになっています。 |
2443 | 2439 | この関数は、他の値と C<undef> とを区別するために使えます。 |
2444 | 2440 | (単純な真偽値テストでは、C<undef>、0、空文字、C<"0"> のいずれも偽を返すので、 |
2445 | 2441 | 区別することができません。) |
2446 | 2442 | C<undef> は有効なスカラ値なので、その存在が I<必ずしも> |
2447 | 2443 | 例外的な状況を表すとは限らないということに注意してください: |
2448 | 2444 | C<pop> は引数が空の配列だったときに C<undef> を返しますが、 |
2449 | 2445 | I<あるいは> 返すべき要素がたまたま C<undef> だったのかもしれません。 |
2450 | 2446 | |
2451 | 2447 | =begin original |
2452 | 2448 | |
2453 | 2449 | You may also use C<defined(&func)> to check whether subroutine C<&func> |
2454 | 2450 | has ever been defined. The return value is unaffected by any forward |
2455 | 2451 | declarations of C<&func>. Note that a subroutine which is not defined |
2456 | 2452 | may still be callable: its package may have an C<AUTOLOAD> method that |
2457 | 2453 | makes it spring into existence the first time that it is called -- see |
2458 | 2454 | L<perlsub>. |
2459 | 2455 | |
2460 | 2456 | =end original |
2461 | 2457 | |
2462 | 2458 | C<defined(&func)> とすることでサブルーチン C<&func> の存在を、 |
2463 | 2459 | 確かめることもできます。 |
2464 | 2460 | 返り値は C<&func> の前方定義には影響されません。 |
2465 | 2461 | 定義されていないサブルーチンも呼び出し可能であることに注意してください。 |
2466 | 2462 | 最初に呼び出されたときに存在するようにするための |
2467 | 2463 | C<AUTOLOAD> メソッドを持ったパッケージかもしれません-- |
2468 | 2464 | L<perlsub> を参照して下さい。 |
2469 | 2465 | |
2470 | 2466 | =begin original |
2471 | 2467 | |
2472 | 2468 | Use of C<defined> on aggregates (hashes and arrays) is deprecated. It |
2473 | 2469 | used to report whether memory for that aggregate has ever been |
2474 | 2470 | allocated. This behavior may disappear in future versions of Perl. |
2475 | 2471 | You should instead use a simple test for size: |
2476 | 2472 | |
2477 | 2473 | =end original |
2478 | 2474 | |
2479 | 2475 | 集合(ハッシュや配列)への C<defined> の使用は非推奨です。 |
2480 | 2476 | これはその集合にメモリが割り当てられたかを報告するのに |
2481 | 2477 | 用いられていました。 |
2482 | 2478 | この振る舞いは将来のバージョンの Perl では消滅するかもしれません。 |
2483 | 2479 | 代わりにサイズに対する簡単なテストを使うべきです。 |
2484 | 2480 | |
2485 | 2481 | if (@an_array) { print "has array elements\n" } |
2486 | 2482 | if (%a_hash) { print "has hash members\n" } |
2487 | 2483 | |
2488 | 2484 | =begin original |
2489 | 2485 | |
2490 | 2486 | When used on a hash element, it tells you whether the value is defined, |
2491 | 2487 | not whether the key exists in the hash. Use L</exists> for the latter |
2492 | 2488 | purpose. |
2493 | 2489 | |
2494 | 2490 | =end original |
2495 | 2491 | |
2496 | 2492 | ハッシュの要素に対して用いると、value が定義されているか否かを |
2497 | 2493 | 返すものであって、ハッシュに key が存在するか否かを返すのではありません。 |
2498 | 2494 | この用途には、L</exists> を使ってください。 |
2499 | 2495 | |
2500 | 2496 | =begin original |
2501 | 2497 | |
2502 | 2498 | Examples: |
2503 | 2499 | |
2504 | 2500 | =end original |
2505 | 2501 | |
2506 | 2502 | 例: |
2507 | 2503 | |
2508 | 2504 | print if defined $switch{'D'}; |
2509 | 2505 | print "$val\n" while defined($val = pop(@ary)); |
2510 | 2506 | die "Can't readlink $sym: $!" |
2511 | 2507 | unless defined($value = readlink $sym); |
2512 | 2508 | sub foo { defined &$bar ? &$bar(@_) : die "No bar"; } |
2513 | 2509 | $debugging = 0 unless defined $debugging; |
2514 | 2510 | |
2515 | 2511 | =begin original |
2516 | 2512 | |
2517 | 2513 | Note: Many folks tend to overuse C<defined>, and then are surprised to |
2518 | 2514 | discover that the number C<0> and C<""> (the zero-length string) are, in fact, |
2519 | 2515 | defined values. For example, if you say |
2520 | 2516 | |
2521 | 2517 | =end original |
2522 | 2518 | |
2523 | 2519 | 注意: 多くの人々が C<defined> を使いすぎて、C<0> と C<"">(空文字列) が |
2524 | 2520 | 実際のところ定義された値であることに驚くようです。 |
2525 | 2521 | 例えば、以下のように書くと: |
2526 | 2522 | |
2527 | 2523 | "ab" =~ /a(.*)b/; |
2528 | 2524 | |
2529 | 2525 | =begin original |
2530 | 2526 | |
2531 | 2527 | The pattern match succeeds, and C<$1> is defined, despite the fact that it |
2532 | 2528 | matched "nothing". It didn't really fail to match anything. Rather, it |
2533 | 2529 | matched something that happened to be zero characters long. This is all |
2534 | 2530 | very above-board and honest. When a function returns an undefined value, |
2535 | 2531 | it's an admission that it couldn't give you an honest answer. So you |
2536 | 2532 | should use C<defined> only when you're questioning the integrity of what |
2537 | 2533 | you're trying to do. At other times, a simple comparison to C<0> or C<""> is |
2538 | 2534 | what you want. |
2539 | 2535 | |
2540 | 2536 | =end original |
2541 | 2537 | |
2542 | 2538 | パターンマッチングが成功し、C<$1> が定義されても、実際には |
2543 | 2539 | 「なし」にマッチしています。 |
2544 | 2540 | しかしこれは何にもマッチしていないわけではありません。 |
2545 | 2541 | 何かにはマッチしているのですが、たまたまそれが長さ 0 だっただけです。 |
2546 | 2542 | これは非常に率直で正直なことです。 |
2547 | 2543 | 関数が未定義値を返すとき、正直な答えを返すことができないことを |
2548 | 2544 | 告白しています。 |
2549 | 2545 | ですので、あなたが自分がしようとしていることの完全性を確認するときにだけ |
2550 | 2546 | C<defined> を使うべきです。 |
2551 | 2547 | その他の場合では、単に C<0> または C<""> と比較するというのがあなたの |
2552 | 2548 | 求めているものです。 |
2553 | 2549 | |
2554 | 2550 | =begin original |
2555 | 2551 | |
2556 | 2552 | See also L</undef>, L</exists>, L</ref>. |
2557 | 2553 | |
2558 | 2554 | =end original |
2559 | 2555 | |
2560 | 2556 | L</undef>, L</exists>, L</ref> も参照してください。 |
2561 | 2557 | |
2562 | 2558 | =item delete EXPR |
2563 | 2559 | X<delete> |
2564 | 2560 | |
2565 | 2561 | =begin original |
2566 | 2562 | |
2567 | 2563 | Given an expression that specifies a hash element, array element, hash slice, |
2568 | 2564 | or array slice, deletes the specified element(s) from the hash or array. |
2569 | 2565 | In the case of an array, if the array elements happen to be at the end, |
2570 | 2566 | the size of the array will shrink to the highest element that tests |
2571 | 2567 | true for exists() (or 0 if no such element exists). |
2572 | 2568 | |
2573 | 2569 | =end original |
2574 | 2570 | |
2575 | 2571 | ハッシュ要素、配列要素、ハッシュスライス、配列スライスを指定する式を取り、 |
2576 | 2572 | 指定された要素をハッシュや配列からを削除します。 |
2577 | 2573 | 配列の場合、配列要素が最後にあった場合は、 |
2578 | 2574 | 配列の大きさは exists() が真を返す最後尾の要素に縮みます |
2579 | 2575 | (そのような要素がない場合は 0 になります)。 |
2580 | 2576 | |
2581 | 2577 | =begin original |
2582 | 2578 | |
2583 | 2579 | Returns a list with the same number of elements as the number of elements |
2584 | 2580 | for which deletion was attempted. Each element of that list consists of |
2585 | 2581 | either the value of the element deleted, or the undefined value. In scalar |
2586 | 2582 | context, this means that you get the value of the last element deleted (or |
2587 | 2583 | the undefined value if that element did not exist). |
2588 | 2584 | |
2589 | 2585 | =end original |
2590 | 2586 | |
2591 | 2587 | 削除をしようとしたようその数と同じ数の要素からなるリストを返します。 |
2592 | 2588 | このリストの各要素は、削除された値か未定義値のどちらかです。 |
2593 | 2589 | スカラコンテキストでは、これは削除された最後の要素(または削除された要素が |
2594 | 2590 | ない場合は未定義値)を得ることを意味します。 |
2595 | 2591 | |
2596 | 2592 | %hash = (foo => 11, bar => 22, baz => 33); |
2597 | 2593 | $scalar = delete $hash{foo}; # $scalar is 11 |
2598 | 2594 | $scalar = delete @hash{qw(foo bar)}; # $scalar is 22 |
2599 | 2595 | @array = delete @hash{qw(foo bar baz)}; # @array is (undef,undef,33) |
2600 | 2596 | |
2601 | 2597 | =begin original |
2602 | 2598 | |
2603 | 2599 | Deleting from C<%ENV> modifies the environment. Deleting from |
2604 | 2600 | a hash tied to a DBM file deletes the entry from the DBM file. Deleting |
2605 | 2601 | from a C<tie>d hash or array may not necessarily return anything. |
2606 | 2602 | |
2607 | 2603 | =end original |
2608 | 2604 | |
2609 | 2605 | C<%ENV> から削除を行なうと、実際に環境変数を変更します。 |
2610 | 2606 | DBM ファイルに tie された配列からの削除は、その DBM ファイルからエントリを |
2611 | 2607 | 削除します。 |
2612 | 2608 | しかし、C<tie> されたハッシュや配列からの削除は、 |
2613 | 2609 | 値を返すとは限りません。 |
2614 | 2610 | |
2615 | 2611 | =begin original |
2616 | 2612 | |
2617 | 2613 | Deleting an array element effectively returns that position of the array |
2618 | 2614 | to its initial, uninitialized state. Subsequently testing for the same |
2619 | 2615 | element with exists() will return false. Also, deleting array elements |
2620 | 2616 | in the middle of an array will not shift the index of the elements |
2621 | 2617 | after them down. Use splice() for that. See L</exists>. |
2622 | 2618 | |
2623 | 2619 | =end original |
2624 | 2620 | |
2625 | 2621 | 配列要素を削除した場合、配列の位置は初期の、初期化されていない状態になります。 |
2626 | 2622 | 引き続いて同じ要素に対して exists() でテストすると偽を返します。 |
2627 | 2623 | また、配列の途中の配列要素を削除してもインデックスはシフトしません。 |
2628 | 2624 | このためには splice() を使ってください。 |
2629 | 2625 | L</exists> を参照してください。 |
2630 | 2626 | |
2631 | 2627 | =begin original |
2632 | 2628 | |
2633 | 2629 | The following (inefficiently) deletes all the values of %HASH and @ARRAY: |
2634 | 2630 | |
2635 | 2631 | =end original |
2636 | 2632 | |
2637 | 2633 | 以下は、%HASH と @ARRAY のすべての値を(非効率的に)削除します: |
2638 | 2634 | |
2639 | 2635 | foreach $key (keys %HASH) { |
2640 | 2636 | delete $HASH{$key}; |
2641 | 2637 | } |
2642 | 2638 | |
2643 | 2639 | foreach $index (0 .. $#ARRAY) { |
2644 | 2640 | delete $ARRAY[$index]; |
2645 | 2641 | } |
2646 | 2642 | |
2647 | 2643 | =begin original |
2648 | 2644 | |
2649 | 2645 | And so do these: |
2650 | 2646 | |
2651 | 2647 | =end original |
2652 | 2648 | |
2653 | 2649 | そして以下のようにもできます: |
2654 | 2650 | |
2655 | 2651 | delete @HASH{keys %HASH}; |
2656 | 2652 | |
2657 | 2653 | delete @ARRAY[0 .. $#ARRAY]; |
2658 | 2654 | |
2659 | 2655 | =begin original |
2660 | 2656 | |
2661 | 2657 | But both of these are slower than just assigning the empty list |
2662 | 2658 | or undefining %HASH or @ARRAY: |
2663 | 2659 | |
2664 | 2660 | =end original |
2665 | 2661 | |
2666 | 2662 | しかし、これら二つは単に空リストを代入するか、%HASH や @ARRAY を |
2667 | 2663 | undef するより遅いです: |
2668 | 2664 | |
2669 | 2665 | %HASH = (); # completely empty %HASH |
2670 | 2666 | undef %HASH; # forget %HASH ever existed |
2671 | 2667 | |
2672 | 2668 | @ARRAY = (); # completely empty @ARRAY |
2673 | 2669 | undef @ARRAY; # forget @ARRAY ever existed |
2674 | 2670 | |
2675 | 2671 | =begin original |
2676 | 2672 | |
2677 | 2673 | Note that the EXPR can be arbitrarily complicated as long as the final |
2678 | 2674 | operation is a hash element, array element, hash slice, or array slice |
2679 | 2675 | lookup: |
2680 | 2676 | |
2681 | 2677 | =end original |
2682 | 2678 | |
2683 | 2679 | 最終的な操作がハッシュ要素、配列要素、ハッシュスライス、配列スライスの |
2684 | 2680 | いずれかである限りは、EXPR には任意の複雑な式を置くことができることに |
2685 | 2681 | 注意してください: |
2686 | 2682 | |
2687 | 2683 | delete $ref->[$x][$y]{$key}; |
2688 | 2684 | delete @{$ref->[$x][$y]}{$key1, $key2, @morekeys}; |
2689 | 2685 | |
2690 | 2686 | delete $ref->[$x][$y][$index]; |
2691 | 2687 | delete @{$ref->[$x][$y]}[$index1, $index2, @moreindices]; |
2692 | 2688 | |
2693 | 2689 | =item die LIST |
2694 | 2690 | X<die> X<throw> X<exception> X<raise> X<$@> X<abort> |
2695 | 2691 | |
2696 | 2692 | =begin original |
2697 | 2693 | |
2698 | 2694 | Outside an C<eval>, prints the value of LIST to C<STDERR> and |
2699 | 2695 | exits with the current value of C<$!> (errno). If C<$!> is C<0>, |
2700 | 2696 | exits with the value of C<<< ($? >> 8) >>> (backtick `command` |
2701 | 2697 | status). If C<<< ($? >> 8) >>> is C<0>, exits with C<255>. Inside |
2702 | 2698 | an C<eval(),> the error message is stuffed into C<$@> and the |
2703 | 2699 | C<eval> is terminated with the undefined value. This makes |
2704 | 2700 | C<die> the way to raise an exception. |
2705 | 2701 | |
2706 | 2702 | =end original |
2707 | 2703 | |
2708 | 2704 | C<eval> の外では、LIST の値を C<STDERR> に出力し、その時点の |
2709 | 2705 | C<$!> (errno) の値で exit します。 |
2710 | 2706 | C<$!> の値が C<0> ならば、 |
2711 | 2707 | C<<< ($? >> 8) >>> (backtick `command` のステータス) の値で exitします。 |
2712 | 2708 | C<<< ($? >> 8) >>> も C<0> であれば、C<255> で exit することになります。 |
2713 | 2709 | C<eval> の中で使用すると、エラーメッセージが、 |
2714 | 2710 | C<$@> に入れられます。 |
2715 | 2711 | C<eval> は中断され、未定義値を返します。 |
2716 | 2712 | これが C<die> が例外を発生させる方法です。 |
2717 | 2713 | |
2718 | 2714 | =begin original |
2719 | 2715 | |
2720 | 2716 | Equivalent examples: |
2721 | 2717 | |
2722 | 2718 | =end original |
2723 | 2719 | |
2724 | 2720 | 等価な例: |
2725 | 2721 | |
2726 | 2722 | die "Can't cd to spool: $!\n" unless chdir '/usr/spool/news'; |
2727 | 2723 | chdir '/usr/spool/news' or die "Can't cd to spool: $!\n" |
2728 | 2724 | |
2729 | 2725 | =begin original |
2730 | 2726 | |
2731 | 2727 | If the last element of LIST does not end in a newline, the current |
2732 | 2728 | script line number and input line number (if any) are also printed, |
2733 | 2729 | and a newline is supplied. Note that the "input line number" (also |
2734 | 2730 | known as "chunk") is subject to whatever notion of "line" happens to |
2735 | 2731 | be currently in effect, and is also available as the special variable |
2736 | 2732 | C<$.>. See L<perlvar/"$/"> and L<perlvar/"$.">. |
2737 | 2733 | |
2738 | 2734 | =end original |
2739 | 2735 | |
2740 | 2736 | LIST の最後の要素が改行で終わっていなければ、その時点の |
2741 | 2737 | スクリプト名とスクリプトの行番号、(もしあれば) |
2742 | 2738 | 入力ファイルの行番号と改行文字が、続けて表示されます。 |
2743 | 2739 | 「入力行番号」("chunk" とも呼ばれます)は |
2744 | 2740 | 「行」という概念が現在有効であると仮定しています。 |
2745 | 2741 | また特殊変数 C<$.> でも利用可能です。 |
2746 | 2742 | L<perlvar/"$/"> と L<perlvar/"$."> も参照してください。 |
2747 | 2743 | |
2748 | 2744 | =begin original |
2749 | 2745 | |
2750 | 2746 | Hint: sometimes appending C<", stopped"> to your message will cause it |
2751 | 2747 | to make better sense when the string C<"at foo line 123"> is appended. |
2752 | 2748 | Suppose you are running script "canasta". |
2753 | 2749 | |
2754 | 2750 | =end original |
2755 | 2751 | |
2756 | 2752 | ヒント: メッセージの最後を C<", stopped"> のようなもので |
2757 | 2753 | 終わるようにしておけば、C<"at foo line 123"> のように |
2758 | 2754 | 追加されて、わかりやすくなります。 |
2759 | 2755 | "canasta" というスクリプトを実行しているとします。 |
2760 | 2756 | |
2761 | 2757 | die "/etc/games is no good"; |
2762 | 2758 | die "/etc/games is no good, stopped"; |
2763 | 2759 | |
2764 | 2760 | =begin original |
2765 | 2761 | |
2766 | 2762 | produce, respectively |
2767 | 2763 | |
2768 | 2764 | =end original |
2769 | 2765 | |
2770 | 2766 | これは、それぞれ以下のように表示します。 |
2771 | 2767 | |
2772 | 2768 | /etc/games is no good at canasta line 123. |
2773 | 2769 | /etc/games is no good, stopped at canasta line 123. |
2774 | 2770 | |
2775 | 2771 | =begin original |
2776 | 2772 | |
2777 | 2773 | See also exit(), warn(), and the Carp module. |
2778 | 2774 | |
2779 | 2775 | =end original |
2780 | 2776 | |
2781 | 2777 | exit() と warn() と Carp モジュールも参照してください。 |
2782 | 2778 | |
2783 | 2779 | =begin original |
2784 | 2780 | |
2785 | 2781 | If LIST is empty and C<$@> already contains a value (typically from a |
2786 | 2782 | previous eval) that value is reused after appending C<"\t...propagated">. |
2787 | 2783 | This is useful for propagating exceptions: |
2788 | 2784 | |
2789 | 2785 | =end original |
2790 | 2786 | |
2791 | 2787 | LIST が空で C<$@> が(典型的には前回の eval で)既に値を持っている場合、 |
2792 | 2788 | 値は C<"\t...propagated"> を追加した後再利用されます。 |
2793 | 2789 | これは例外を伝播させる場合に有効です: |
2794 | 2790 | |
2795 | 2791 | eval { ... }; |
2796 | 2792 | die unless $@ =~ /Expected exception/; |
2797 | 2793 | |
2798 | 2794 | =begin original |
2799 | 2795 | |
2800 | 2796 | If LIST is empty and C<$@> contains an object reference that has a |
2801 | 2797 | C<PROPAGATE> method, that method will be called with additional file |
2802 | 2798 | and line number parameters. The return value replaces the value in |
2803 | 2799 | C<$@>. i.e. as if C<< $@ = eval { $@->PROPAGATE(__FILE__, __LINE__) }; >> |
2804 | 2800 | were called. |
2805 | 2801 | |
2806 | 2802 | =end original |
2807 | 2803 | |
2808 | 2804 | LIST が空で、C<$@> が C<PROPAGATE> メソッドを含むオブジェクトへの |
2809 | 2805 | リファレンスを含む場合、このメソッドが追加ファイルと行番号を引数として |
2810 | 2806 | 呼び出されます。 |
2811 | 2807 | 返り値は C<$@> の値を置き換えます。 |
2812 | 2808 | つまり、C<< $@ = eval { $@->PROPAGATE(__FILE__, __LINE__) }; >> が |
2813 | 2809 | 呼び出されたかのようになります。 |
2814 | 2810 | |
2815 | 2811 | =begin original |
2816 | 2812 | |
2817 | 2813 | If C<$@> is empty then the string C<"Died"> is used. |
2818 | 2814 | |
2819 | 2815 | =end original |
2820 | 2816 | |
2821 | 2817 | C<$@> が空の場合、C<"Died"> が使われます。 |
2822 | 2818 | |
2823 | 2819 | =begin original |
2824 | 2820 | |
2825 | 2821 | die() can also be called with a reference argument. If this happens to be |
2826 | 2822 | trapped within an eval(), $@ contains the reference. This behavior permits |
2827 | 2823 | a more elaborate exception handling implementation using objects that |
2828 | 2824 | maintain arbitrary state about the nature of the exception. Such a scheme |
2829 | 2825 | is sometimes preferable to matching particular string values of $@ using |
2830 | 2826 | regular expressions. Because $@ is a global variable, and eval() may be |
2831 | 2827 | used within object implementations, care must be taken that analyzing the |
2832 | 2828 | error object doesn't replace the reference in the global variable. The |
2833 | 2829 | easiest solution is to make a local copy of the reference before doing |
2834 | 2830 | other manipulations. Here's an example: |
2835 | 2831 | |
2836 | 2832 | =end original |
2837 | 2833 | |
2838 | 2834 | die() はリファレンス引数と共に呼び出すこともできます。 |
2839 | 2835 | eval() 内部でこのように呼び出された場合、$@ はリファレンスを持ちます。 |
2840 | 2836 | この振る舞いは、例外の性質にすいて任意の状態を管理するオブジェクトを使った |
2841 | 2837 | より複雑な例外処理の実装を可能にします。 |
2842 | 2838 | このようなスキーマは $@ の特定の文字列値を正規表現を使って |
2843 | 2839 | マッチングするときに時々好まれます。 |
2844 | 2840 | $@ はグローバル変数で、eval() はオブジェクト実装の内部で |
2845 | 2841 | 使われることがあるので、エラーオブジェクトの解析はグローバル変数の |
2846 | 2842 | リファレンスを置き換えないことに注意を払わなければなりません。 |
2847 | 2843 | 最も簡単な解決方法は、他の操作をする前にリファレンスのローカルコピーを |
2848 | 2844 | 作ることです。 |
2849 | 2845 | 以下に例を示します: |
2850 | 2846 | |
2851 | 2847 | use Scalar::Util 'blessed'; |
2852 | 2848 | |
2853 | 2849 | eval { ... ; die Some::Module::Exception->new( FOO => "bar" ) }; |
2854 | 2850 | if (my $ev_err = $@) { |
2855 | 2851 | if (blessed($ev_err) && $ev_err->isa("Some::Module::Exception")) { |
2856 | 2852 | # handle Some::Module::Exception |
2857 | 2853 | } |
2858 | 2854 | else { |
2859 | 2855 | # handle all other possible exceptions |
2860 | 2856 | } |
2861 | 2857 | } |
2862 | 2858 | |
2863 | 2859 | =begin original |
2864 | 2860 | |
2865 | 2861 | Because perl will stringify uncaught exception messages before displaying |
2866 | 2862 | them, you may want to overload stringification operations on such custom |
2867 | 2863 | exception objects. See L<overload> for details about that. |
2868 | 2864 | |
2869 | 2865 | =end original |
2870 | 2866 | |
2871 | 2867 | perl は捕らえられなかった例外のメッセージを表示する前に文字列化するので、 |
2872 | 2868 | このようなカスタム例外オブジェクトの文字列化をオーバーロードしたいと |
2873 | 2869 | 思うかもしれません。 |
2874 | 2870 | これに関する詳細は L<overload> を参照してください。 |
2875 | 2871 | |
2876 | 2872 | =begin original |
2877 | 2873 | |
2878 | 2874 | You can arrange for a callback to be run just before the C<die> |
2879 | 2875 | does its deed, by setting the C<$SIG{__DIE__}> hook. The associated |
2880 | 2876 | handler will be called with the error text and can change the error |
2881 | 2877 | message, if it sees fit, by calling C<die> again. See |
2882 | 2878 | L<perlvar/$SIG{expr}> for details on setting C<%SIG> entries, and |
2883 | 2879 | L<"eval BLOCK"> for some examples. Although this feature was |
2884 | 2880 | to be run only right before your program was to exit, this is not |
2885 | 2881 | currently the case--the C<$SIG{__DIE__}> hook is currently called |
2886 | 2882 | even inside eval()ed blocks/strings! If one wants the hook to do |
2887 | 2883 | nothing in such situations, put |
2888 | 2884 | |
2889 | 2885 | =end original |
2890 | 2886 | |
2891 | 2887 | C<$SIG{__DIE__}> フックをセットすることで、C<die> がその行動を行う |
2892 | 2888 | 直前に実行されるコールバックを設定できます。 |
2893 | 2889 | 結び付けられたハンドラはエラーテキストと共に呼び出され、 |
2894 | 2890 | 必要なら再び C<die> を呼び出すことでエラーテキストを変更できアス。 |
2895 | 2891 | C<%SIG> のエントリをセットする詳細については、L<perlvar/$SIG{expr}> を、 |
2896 | 2892 | 例については L<"eval BLOCK"> を参照してください。 |
2897 | 2893 | この機能はプログラムが終了しようとする前に 1 回だけ実行していましたが、 |
2898 | 2894 | 現在ではそうではありません -- |
2899 | 2895 | C<$SIG{__DIE__}> フックは eval() されたブロック/文字列の中でも |
2900 | 2896 | 呼ばれるのです! |
2901 | 2897 | もしそのような状況で何もしなくない時は: |
2902 | 2898 | |
2903 | 2899 | die @_ if $^S; |
2904 | 2900 | |
2905 | 2901 | =begin original |
2906 | 2902 | |
2907 | 2903 | as the first line of the handler (see L<perlvar/$^S>). Because |
2908 | 2904 | this promotes strange action at a distance, this counterintuitive |
2909 | 2905 | behavior may be fixed in a future release. |
2910 | 2906 | |
2911 | 2907 | =end original |
2912 | 2908 | |
2913 | 2909 | をハンドラの最初の行に置いてください(L<perlvar/$^S> を参照してください)。 |
2914 | 2910 | これは離れたところで不思議な行動を引き起こすので、 |
2915 | 2911 | この直感的でない振る舞いは将来のリリースで修正されるかもしれません。 |
2916 | 2912 | |
2917 | 2913 | =item do BLOCK |
2918 | 2914 | X<do> X<block> |
2919 | 2915 | |
2920 | 2916 | =begin original |
2921 | 2917 | |
2922 | 2918 | Not really a function. Returns the value of the last command in the |
2923 | 2919 | sequence of commands indicated by BLOCK. When modified by the C<while> or |
2924 | 2920 | C<until> loop modifier, executes the BLOCK once before testing the loop |
2925 | 2921 | condition. (On other statements the loop modifiers test the conditional |
2926 | 2922 | first.) |
2927 | 2923 | |
2928 | 2924 | =end original |
2929 | 2925 | |
2930 | 2926 | 実際は関数ではありません。 |
2931 | 2927 | BLOCK で示されるコマンド列の最後の値を返します。 |
2932 | 2928 | C<while> や C<until> ループ修飾子で修飾すると、 |
2933 | 2929 | ループ条件を調べる前に 1 度、BLOCK を実行します。 |
2934 | 2930 | (これ以外の実行文は、ループ修飾子により、条件が最初に |
2935 | 2931 | 調べられます。) |
2936 | 2932 | |
2937 | 2933 | =begin original |
2938 | 2934 | |
2939 | 2935 | C<do BLOCK> does I<not> count as a loop, so the loop control statements |
2940 | 2936 | C<next>, C<last>, or C<redo> cannot be used to leave or restart the block. |
2941 | 2937 | See L<perlsyn> for alternative strategies. |
2942 | 2938 | |
2943 | 2939 | =end original |
2944 | 2940 | |
2945 | 2941 | C<do BLOCK> はループとしては I<扱われません>。 |
2946 | 2942 | 従って、C<next>, C<last>, C<redo> といったループ制御文は |
2947 | 2943 | ブロックから抜けたり再開することはできません。 |
2948 | 2944 | その他の戦略については L<perlsyn> を参照して下さい。 |
2949 | 2945 | |
2950 | 2946 | =item do SUBROUTINE(LIST) |
2951 | 2947 | X<do> |
2952 | 2948 | |
2953 | 2949 | =begin original |
2954 | 2950 | |
2955 | 2951 | This form of subroutine call is deprecated. See L<perlsub>. |
2956 | 2952 | |
2957 | 2953 | =end original |
2958 | 2954 | |
2959 | 2955 | この形のサブルーチン呼び出しは非推奨です。 |
2960 | 2956 | L<perlsub> を参照してください。 |
2961 | 2957 | |
2962 | 2958 | =item do EXPR |
2963 | 2959 | X<do> |
2964 | 2960 | |
2965 | 2961 | =begin original |
2966 | 2962 | |
2967 | 2963 | Uses the value of EXPR as a filename and executes the contents of the |
2968 | 2964 | file as a Perl script. |
2969 | 2965 | |
2970 | 2966 | =end original |
2971 | 2967 | |
2972 | 2968 | EXPR の値をファイル名として用い、そのファイルの中身を |
2973 | 2969 | Perl のスクリプトとして実行します。 |
2974 | 2970 | |
2975 | 2971 | do 'stat.pl'; |
2976 | 2972 | |
2977 | 2973 | =begin original |
2978 | 2974 | |
2979 | 2975 | is just like |
2980 | 2976 | |
2981 | 2977 | =end original |
2982 | 2978 | |
2983 | 2979 | は以下のものと同じようなものですが、 |
2984 | 2980 | |
2985 | 2981 | eval `cat stat.pl`; |
2986 | 2982 | |
2987 | 2983 | =begin original |
2988 | 2984 | |
2989 | 2985 | except that it's more efficient and concise, keeps track of the current |
2990 | 2986 | filename for error messages, searches the @INC directories, and updates |
2991 | 2987 | C<%INC> if the file is found. See L<perlvar/Predefined Names> for these |
2992 | 2988 | variables. It also differs in that code evaluated with C<do FILENAME> |
2993 | 2989 | cannot see lexicals in the enclosing scope; C<eval STRING> does. It's the |
2994 | 2990 | same, however, in that it does reparse the file every time you call it, |
2995 | 2991 | so you probably don't want to do this inside a loop. |
2996 | 2992 | |
2997 | 2993 | =end original |
2998 | 2994 | |
2999 | 2995 | より効率的で、簡潔であり、エラーメッセージでファイル名がわかる、 |
3000 | 2996 | カレントディレクトリでファイルが見つからなかったときに |
3001 | 2997 | @INC ディレクトリを検索する、ファイルがあったときに C<%INC> を更新する、 |
3002 | 2998 | といったことがあります。 |
3003 | 2999 | L<perlvar/Predefined Names> も参照してください。 |
3004 | 3000 | C<do FILENAME> で評価されたコードは、入れ子のスコープにある |
3005 | 3001 | レキシカル変数を見ることができないのに対し、C<eval STRING>ではできる、 |
3006 | 3002 | という違いがあります。 |
3007 | 3003 | しかし、呼び出すたびにファイルを解析し直すという点では同じですから、 |
3008 | 3004 | ループ内でこれを使おうなどとは、間違っても思ったりしないように。 |
3009 | 3005 | |
3010 | 3006 | =begin original |
3011 | 3007 | |
3012 | 3008 | If C<do> cannot read the file, it returns undef and sets C<$!> to the |
3013 | 3009 | error. If C<do> can read the file but cannot compile it, it |
3014 | 3010 | returns undef and sets an error message in C<$@>. If the file is |
3015 | 3011 | successfully compiled, C<do> returns the value of the last expression |
3016 | 3012 | evaluated. |
3017 | 3013 | |
3018 | 3014 | =end original |
3019 | 3015 | |
3020 | 3016 | C<do>がファイルを読み込めなかった場合、undef を返して C<$!> に |
3021 | 3017 | エラーを設定します。 |
3022 | 3018 | C<do> がファイルを読み込めたがコンパイルできなかった場合、 |
3023 | 3019 | undef を返して C<$@> にエラーメッセージを設定します。 |
3024 | 3020 | ファイルのコンパイルに成功した場合、C<do> は最後に評価した表現の値を返します。 |
3025 | 3021 | |
3026 | 3022 | =begin original |
3027 | 3023 | |
3028 | 3024 | Note that inclusion of library modules is better done with the |
3029 | 3025 | C<use> and C<require> operators, which also do automatic error checking |
3030 | 3026 | and raise an exception if there's a problem. |
3031 | 3027 | |
3032 | 3028 | =end original |
3033 | 3029 | |
3034 | 3030 | ライブラリモジュールのインクルードには、C<use> 演算子や |
3035 | 3031 | C<require> 演算子を使った方がよいことに注意してください。 |
3036 | 3032 | これらは自動的にエラーをチェックして、問題があれば例外を発生させます。 |
3037 | 3033 | |
3038 | 3034 | =begin original |
3039 | 3035 | |
3040 | 3036 | You might like to use C<do> to read in a program configuration |
3041 | 3037 | file. Manual error checking can be done this way: |
3042 | 3038 | |
3043 | 3039 | =end original |
3044 | 3040 | |
3045 | 3041 | C<do> をプログラム設定ファイルを読み込むのに使いたいかもしれません。 |
3046 | 3042 | 手動のエラーチェックは以下のようにして行えます: |
3047 | 3043 | |
3048 | 3044 | # read in config files: system first, then user |
3049 | 3045 | for $file ("/share/prog/defaults.rc", |
3050 | 3046 | "$ENV{HOME}/.someprogrc") |
3051 | 3047 | { |
3052 | 3048 | unless ($return = do $file) { |
3053 | 3049 | warn "couldn't parse $file: $@" if $@; |
3054 | 3050 | warn "couldn't do $file: $!" unless defined $return; |
3055 | 3051 | warn "couldn't run $file" unless $return; |
3056 | 3052 | } |
3057 | 3053 | } |
3058 | 3054 | |
3059 | 3055 | =item dump LABEL |
3060 | 3056 | X<dump> X<core> X<undump> |
3061 | 3057 | |
3062 | 3058 | =item dump |
3063 | 3059 | |
3064 | 3060 | =begin original |
3065 | 3061 | |
3066 | 3062 | This function causes an immediate core dump. See also the B<-u> |
3067 | 3063 | command-line switch in L<perlrun>, which does the same thing. |
3068 | 3064 | Primarily this is so that you can use the B<undump> program (not |
3069 | 3065 | supplied) to turn your core dump into an executable binary after |
3070 | 3066 | having initialized all your variables at the beginning of the |
3071 | 3067 | program. When the new binary is executed it will begin by executing |
3072 | 3068 | a C<goto LABEL> (with all the restrictions that C<goto> suffers). |
3073 | 3069 | Think of it as a goto with an intervening core dump and reincarnation. |
3074 | 3070 | If C<LABEL> is omitted, restarts the program from the top. |
3075 | 3071 | |
3076 | 3072 | =end original |
3077 | 3073 | |
3078 | 3074 | この関数は即座にコアダンプを行ないます。 |
3079 | 3075 | 同様のことを行う L<perlrun> の B<-u> オプションも参照してください。 |
3080 | 3076 | プログラムの先頭で、 |
3081 | 3077 | すべての変数を初期化したあとのコアダンプを B<undump> |
3082 | 3078 | プログラム(提供していません)を使って実行ファイルに返ることができます。 |
3083 | 3079 | この新しいバイナリが実行されると、C<goto LABEL> から始めます |
3084 | 3080 | (C<goto> に関する制限はすべて適用されます)。 |
3085 | 3081 | コアダンプをはさんで再生する goto と考えてください。 |
3086 | 3082 | C<LABEL> が省略されると、プログラムを先頭から再開します。 |
3087 | 3083 | |
3088 | 3084 | =begin original |
3089 | 3085 | |
3090 | 3086 | B<WARNING>: Any files opened at the time of the dump will I<not> |
3091 | 3087 | be open any more when the program is reincarnated, with possible |
3092 | 3088 | resulting confusion on the part of Perl. |
3093 | 3089 | |
3094 | 3090 | =end original |
3095 | 3091 | |
3096 | 3092 | B<警告>: dump 時点でオープンされていたファイルは、 |
3097 | 3093 | プログラムが再生されたときには、もはやオープンされていません。 |
3098 | 3094 | Perl を部分的に混乱させる可能性があります。 |
3099 | 3095 | |
3100 | 3096 | =begin original |
3101 | 3097 | |
3102 | 3098 | This function is now largely obsolete, mostly because it's very hard to |
3103 | 3099 | convert a core file into an executable. That's why you should now invoke |
3104 | 3100 | it as C<CORE::dump()>, if you don't want to be warned against a possible |
3105 | 3101 | typo. |
3106 | 3102 | |
3107 | 3103 | =end original |
3108 | 3104 | |
3109 | 3105 | この関数は大幅に時代遅れのものです; 主な理由としては、コアファイルを |
3110 | 3106 | 実行形式に変換するのが非常に困難であることです。 |
3111 | 3107 | これが、今ではタイプミスの可能性を警告されたくないなら |
3112 | 3108 | C<CORE::dump()> として起動するべき理由です。 |
3113 | 3109 | |
3114 | 3110 | =item each HASH |
3115 | 3111 | X<each> X<hash, iterator> |
3116 | 3112 | |
3117 | 3113 | =begin original |
3118 | 3114 | |
3119 | 3115 | When called in list context, returns a 2-element list consisting of the |
3120 | 3116 | key and value for the next element of a hash, so that you can iterate over |
3121 | 3117 | it. When called in scalar context, returns only the key for the next |
3122 | 3118 | element in the hash. |
3123 | 3119 | |
3124 | 3120 | =end original |
3125 | 3121 | |
3126 | 3122 | リストコンテキストで呼び出した場合は、 |
3127 | 3123 | ハッシュの次の 要素 に対する、key と 要素 からなる |
3128 | 3124 | 2 要素のリストを返しますので、ハッシュ上での繰り返しを |
3129 | 3125 | 行なうことができます。 |
3130 | 3126 | スカラコンテキストで呼び出した場合は、 |
3131 | 3127 | ハッシュの次の要素のための key を返します。 |
3132 | 3128 | |
3133 | 3129 | =begin original |
3134 | 3130 | |
3135 | 3131 | Entries are returned in an apparently random order. The actual random |
3136 | 3132 | order is subject to change in future versions of perl, but it is |
3137 | 3133 | guaranteed to be in the same order as either the C<keys> or C<values> |
3138 | 3134 | function would produce on the same (unmodified) hash. Since Perl |
3139 | 5.8. | |
3135 | 5.8.1 the ordering is different even between different runs of Perl | |
3140 | 3136 | for security reasons (see L<perlsec/"Algorithmic Complexity Attacks">). |
3141 | 3137 | |
3142 | 3138 | =end original |
3143 | 3139 | |
3144 | 3140 | エントリは見かけ上、ランダムな順序で返されます。 |
3145 | 3141 | 実際のランダムな順番は perl の将来のバージョンでは変わるかもしれませんが、 |
3146 | 3142 | C<keys> や C<values> 関数が同じ(変更されていない)ハッシュに対して |
3147 | 3143 | 生成するのと同じ順番であることは保証されます。 |
3148 | Perl 5.8. | |
3144 | Perl 5.8.1 以降ではセキュリティ上の理由により、 | |
3149 | 実行される毎に順番は変わ | |
3145 | 実行される毎に順番は変わります | |
3150 | 3146 | (L<perlsec/"Algorithmic Complexity Attacks"> を参照してください)。 |
3151 | 3147 | |
3152 | 3148 | =begin original |
3153 | 3149 | |
3154 | 3150 | When the hash is entirely read, a null array is returned in list context |
3155 | 3151 | (which when assigned produces a false (C<0>) value), and C<undef> in |
3156 | 3152 | scalar context. The next call to C<each> after that will start iterating |
3157 | 3153 | again. There is a single iterator for each hash, shared by all C<each>, |
3158 | 3154 | C<keys>, and C<values> function calls in the program; it can be reset by |
3159 | 3155 | reading all the elements from the hash, or by evaluating C<keys HASH> or |
3160 | 3156 | C<values HASH>. If you add or delete elements of a hash while you're |
3161 | 3157 | iterating over it, you may get entries skipped or duplicated, so |
3162 | 3158 | don't. Exception: It is always safe to delete the item most recently |
3163 | 3159 | returned by C<each()>, which means that the following code will work: |
3164 | 3160 | |
3165 | 3161 | =end original |
3166 | 3162 | |
3167 | 3163 | ハッシュをすべて読み込んでしまうと、リストコンテキストでは空配列が |
3168 | 3164 | 返されます(これは代入されると、偽 (C<0>) となります)。 |
3169 | 3165 | スカラコンテキストでは C<undef> が返されます。 |
3170 | 3166 | そのあと、もう一度 C<each> を呼び出すと、 |
3171 | 3167 | 再び繰り返しを始めます。 |
3172 | 3168 | ハッシュごとに反復子が 1 つあり、プログラム中のすべての |
3173 | 3169 | C<each> 関数、C<keys> 関数、C<values> 関数で共用されます。 |
3174 | 3170 | 反復子は、配列の要素をすべて読むことによって、またはC<keys HASH>, |
3175 | 3171 | C<values HASH>を評価することでリセットすることができます。 |
3176 | 3172 | 繰り返しを行なっている間に、ハッシュに要素を追加したり削除したりすると、 |
3177 | 3173 | 要素が飛ばされたり重複したりするので、してはいけません。 |
3178 | 3174 | 例外: 一番最近に C<each()> から返されたものを削除するのは常に安全です。 |
3179 | 3175 | これは以下のようなコードが正しく動くことを意味します: |
3180 | 3176 | |
3181 | 3177 | while (($key, $value) = each %hash) { |
3182 | 3178 | print $key, "\n"; |
3183 | 3179 | delete $hash{$key}; # This is safe |
3184 | 3180 | } |
3185 | 3181 | |
3186 | 3182 | =begin original |
3187 | 3183 | |
3188 | 3184 | The following prints out your environment like the printenv(1) program, |
3189 | 3185 | only in a different order: |
3190 | 3186 | |
3191 | 3187 | =end original |
3192 | 3188 | |
3193 | 3189 | 以下のプログラムは、順番が異なるものの、 |
3194 | 3190 | printenv(1) プログラムのように環境変数を表示します: |
3195 | 3191 | |
3196 | 3192 | while (($key,$value) = each %ENV) { |
3197 | 3193 | print "$key=$value\n"; |
3198 | 3194 | } |
3199 | 3195 | |
3200 | 3196 | =begin original |
3201 | 3197 | |
3202 | 3198 | See also C<keys>, C<values> and C<sort>. |
3203 | 3199 | |
3204 | 3200 | =end original |
3205 | 3201 | |
3206 | 3202 | C<keys> や C<values> や C<sort> も参照してください。 |
3207 | 3203 | |
3208 | 3204 | =item eof FILEHANDLE |
3209 | 3205 | X<eof> |
3210 | 3206 | X<end of file> |
3211 | 3207 | X<end-of-file> |
3212 | 3208 | |
3213 | 3209 | =item eof () |
3214 | 3210 | |
3215 | 3211 | =item eof |
3216 | 3212 | |
3217 | 3213 | =begin original |
3218 | 3214 | |
3219 | 3215 | Returns 1 if the next read on FILEHANDLE will return end of file, or if |
3220 | 3216 | FILEHANDLE is not open. FILEHANDLE may be an expression whose value |
3221 | 3217 | gives the real filehandle. (Note that this function actually |
3222 | 3218 | reads a character and then C<ungetc>s it, so isn't very useful in an |
3223 | 3219 | interactive context.) Do not read from a terminal file (or call |
3224 | 3220 | C<eof(FILEHANDLE)> on it) after end-of-file is reached. File types such |
3225 | 3221 | as terminals may lose the end-of-file condition if you do. |
3226 | 3222 | |
3227 | 3223 | =end original |
3228 | 3224 | |
3229 | 3225 | 次に FILEHANDLE 上で読み込みを行なったときに、 EOF が返されるときか、 |
3230 | 3226 | FILEHANDLE がオープンされていないと、1 を返します。 |
3231 | 3227 | FILEHANDLE は、値が実際のファイルハンドルを示す式であってもかまいません。 |
3232 | 3228 | (この関数は、実際に文字を読み、C<ungetc> を行ないますので、 |
3233 | 3229 | 対話型の場合には、それ程有用ではありません。) |
3234 | 3230 | 端末ファイルは EOF に達した後にさらに読み込んだり C<eof(FILEHANDLE)> を |
3235 | 3231 | 呼び出したりしてはいけません。 |
3236 | 3232 | そのようなことをすると、端末のようなファイルタイプは |
3237 | 3233 | EOF 状態を失ってしまうかもしれません。 |
3238 | 3234 | |
3239 | 3235 | =begin original |
3240 | 3236 | |
3241 | 3237 | An C<eof> without an argument uses the last file read. Using C<eof()> |
3242 | 3238 | with empty parentheses is very different. It refers to the pseudo file |
3243 | 3239 | formed from the files listed on the command line and accessed via the |
3244 | 3240 | C<< <> >> operator. Since C<< <> >> isn't explicitly opened, |
3245 | 3241 | as a normal filehandle is, an C<eof()> before C<< <> >> has been |
3246 | 3242 | used will cause C<@ARGV> to be examined to determine if input is |
3247 | 3243 | available. Similarly, an C<eof()> after C<< <> >> has returned |
3248 | 3244 | end-of-file will assume you are processing another C<@ARGV> list, |
3249 | 3245 | and if you haven't set C<@ARGV>, will read input from C<STDIN>; |
3250 | 3246 | see L<perlop/"I/O Operators">. |
3251 | 3247 | |
3252 | 3248 | =end original |
3253 | 3249 | |
3254 | 3250 | 引数を省略した C<eof> は、最後に読み込みを行なったファイルを使います。 |
3255 | 3251 | 空の括弧をつけた C<eof()> は大きく異なります。 |
3256 | 3252 | これはコマンドラインのファイルリストで構成され、C<< <> >> 演算子経由で |
3257 | 3253 | アクセスされる擬似ファイルを示すために用いられます。 |
3258 | 3254 | 通常のファイルハンドルと違って C<< <> >> は明示的にオープンされないので、 |
3259 | 3255 | C<< <> >> を使う前に C<eof()> を使うと、 |
3260 | 3256 | 入力が正常か確認するために C<@ARGV> がテストされます。 |
3261 | 3257 | 同様に、C<< <> >> が EOF を返した後の C<eof()> は、 |
3262 | 3258 | 他の C<@ARGV> リストを処理していると仮定し、もし C<@ARGV> を |
3263 | 3259 | セットしていないときは C<STDIN> から読み込みます; |
3264 | 3260 | L<perlop/"I/O Operators"> を参照してください。 |
3265 | 3261 | |
3266 | 3262 | =begin original |
3267 | 3263 | |
3268 | 3264 | In a C<< while (<>) >> loop, C<eof> or C<eof(ARGV)> can be used to |
3269 | 3265 | detect the end of each file, C<eof()> will only detect the end of the |
3270 | 3266 | last file. Examples: |
3271 | 3267 | |
3272 | 3268 | =end original |
3273 | 3269 | |
3274 | 3270 | C<< while (<>) >> ループの中では、個々のファイルの終わりを調べるには、 |
3275 | 3271 | C<eof> か C<eof(ARGV)> を用います。 |
3276 | 3272 | C<eof()> は最後のファイルの終わりのみを調べます。 |
3277 | 3273 | 例: |
3278 | 3274 | |
3279 | 3275 | # reset line numbering on each input file |
3280 | 3276 | while (<>) { |
3281 | 3277 | next if /^\s*#/; # skip comments |
3282 | 3278 | print "$.\t$_"; |
3283 | 3279 | } continue { |
3284 | 3280 | close ARGV if eof; # Not eof()! |
3285 | 3281 | } |
3286 | 3282 | |
3287 | 3283 | # insert dashes just before last line of last file |
3288 | 3284 | while (<>) { |
3289 | 3285 | if (eof()) { # check for end of last file |
3290 | 3286 | print "--------------\n"; |
3291 | 3287 | } |
3292 | 3288 | print; |
3293 | 3289 | last if eof(); # needed if we're reading from a terminal |
3294 | 3290 | } |
3295 | 3291 | |
3296 | 3292 | =begin original |
3297 | 3293 | |
3298 | 3294 | Practical hint: you almost never need to use C<eof> in Perl, because the |
3299 | 3295 | input operators typically return C<undef> when they run out of data, or if |
3300 | 3296 | there was an error. |
3301 | 3297 | |
3302 | 3298 | =end original |
3303 | 3299 | |
3304 | 3300 | 現実的なヒント: Perl で C<eof> が必要となることは、ほとんどありません。 |
3305 | 3301 | 基本的には、 |
3306 | 3302 | データがなくなったときやエラーがあったときに、入力演算子が |
3307 | 3303 | C<undef> を返してくれるからです。 |
3308 | 3304 | |
3309 | 3305 | =item eval EXPR |
3310 | 3306 | X<eval> X<try> X<catch> X<evaluate> X<parse> X<execute> |
3311 | 3307 | X<error, handling> X<exception, handling> |
3312 | 3308 | |
3313 | 3309 | =item eval BLOCK |
3314 | 3310 | |
3315 | 3311 | =item eval |
3316 | 3312 | |
3317 | 3313 | =begin original |
3318 | 3314 | |
3319 | 3315 | In the first form, the return value of EXPR is parsed and executed as if it |
3320 | 3316 | were a little Perl program. The value of the expression (which is itself |
3321 | 3317 | determined within scalar context) is first parsed, and if there weren't any |
3322 | 3318 | errors, executed in the lexical context of the current Perl program, so |
3323 | 3319 | that any variable settings or subroutine and format definitions remain |
3324 | 3320 | afterwards. Note that the value is parsed every time the C<eval> executes. |
3325 | 3321 | If EXPR is omitted, evaluates C<$_>. This form is typically used to |
3326 | 3322 | delay parsing and subsequent execution of the text of EXPR until run time. |
3327 | 3323 | |
3328 | 3324 | =end original |
3329 | 3325 | |
3330 | 3326 | 第一の形式では、EXPR の返り値が Perl のプログラムであるかのように |
3331 | 3327 | 解析され、実行されます。 |
3332 | 3328 | 式の値(それ自身スカラコンテキストの中で決定されます)はまずパースされ、 |
3333 | 3329 | エラーがなければ |
3334 | 3330 | Perl プログラムのレキシカルコンテキストの中で実行されますので、変数の設定、 |
3335 | 3331 | サブルーチンやフォーマットの定義は、その後も残っています。 |
3336 | 3332 | 返される値は C<eval> が実行されるごとにパースされることに注意してください。 |
3337 | 3333 | EXPR を省略すると、C<$_> を評価します。 |
3338 | 3334 | この形は主に EXPR のテキストのパースと実行を実行時にまで |
3339 | 3335 | 遅延させるのに用います。 |
3340 | 3336 | |
3341 | 3337 | =begin original |
3342 | 3338 | |
3343 | 3339 | In the second form, the code within the BLOCK is parsed only once--at the |
3344 | 3340 | same time the code surrounding the C<eval> itself was parsed--and executed |
3345 | 3341 | within the context of the current Perl program. This form is typically |
3346 | 3342 | used to trap exceptions more efficiently than the first (see below), while |
3347 | 3343 | also providing the benefit of checking the code within BLOCK at compile |
3348 | 3344 | time. |
3349 | 3345 | |
3350 | 3346 | =end original |
3351 | 3347 | |
3352 | 3348 | 第二の形式では、BLOCK 内部のコードは一度だけパースされ -- コードを |
3353 | 3349 | 囲む C<eval> 自身がパースされるのと同じ時点です -- 現在の Perl プログラムの |
3354 | 3350 | コンテキストで実行されます。 |
3355 | 3351 | この形式は典型的には第一の形式より効率的に例外をトラップします(後述)。 |
3356 | 3352 | また BLOCK 内部のコードはコンパイル時にチェックされるという利点を提供します。 |
3357 | 3353 | |
3358 | 3354 | =begin original |
3359 | 3355 | |
3360 | 3356 | The final semicolon, if any, may be omitted from the value of EXPR or within |
3361 | 3357 | the BLOCK. |
3362 | 3358 | |
3363 | 3359 | =end original |
3364 | 3360 | |
3365 | 3361 | 最後のセミコロンは、もしあれば、EXPR の値や BLOCK の中身から省くことができます。 |
3366 | 3362 | |
3367 | 3363 | =begin original |
3368 | 3364 | |
3369 | 3365 | In both forms, the value returned is the value of the last expression |
3370 | 3366 | evaluated inside the mini-program; a return statement may be also used, just |
3371 | 3367 | as with subroutines. The expression providing the return value is evaluated |
3372 | 3368 | in void, scalar, or list context, depending on the context of the C<eval> |
3373 | 3369 | itself. See L</wantarray> for more on how the evaluation context can be |
3374 | 3370 | determined. |
3375 | 3371 | |
3376 | 3372 | =end original |
3377 | 3373 | |
3378 | 3374 | どちらの形式でも、返される値はミニプログラムの内部で最後に評価された |
3379 | 3375 | 表現の値です; サブルーチンと同様、return 文も使えます。 |
3380 | 3376 | 返り値として提供される表現は、C<eval> 自身のコンテキストに依存して |
3381 | 3377 | 無効・スカラ・リストのいずれかのコンテキストで評価されます。 |
3382 | 3378 | 評価コンテキストの決定方法についての詳細は L</wantarray> を参照してください。 |
3383 | 3379 | |
3384 | 3380 | =begin original |
3385 | 3381 | |
3386 | 3382 | If there is a syntax error or runtime error, or a C<die> statement is |
3387 | executed, | |
3383 | executed, an undefined value is returned by C<eval>, and C<$@> is set to the | |
3388 | or an empty list in list context, and C<$@> is set to the | |
3389 | 3384 | error message. If there was no error, C<$@> is guaranteed to be a null |
3390 | 3385 | string. Beware that using C<eval> neither silences perl from printing |
3391 | 3386 | warnings to STDERR, nor does it stuff the text of warning messages into C<$@>. |
3392 | 3387 | To do either of those, you have to use the C<$SIG{__WARN__}> facility, or |
3393 | 3388 | turn off warnings inside the BLOCK or EXPR using S<C<no warnings 'all'>>. |
3394 | 3389 | See L</warn>, L<perlvar>, L<warnings> and L<perllexwarn>. |
3395 | 3390 | |
3396 | 3391 | =end original |
3397 | 3392 | |
3398 | 3393 | 構文エラーや実行エラーが発生するか、C<die> 文が実行されると、 |
3399 | C<eval> | |
3394 | C<eval> の値として未定義値が返され、C<$@> にエラーメッセージが設定されます。 | |
3400 | 空リストが返され、C<$@> にエラーメッセージが設定されます。 | |
3401 | 3395 | エラーがなければ、C<$@> は、空文字列であることが保証されます。 |
3402 | 3396 | C<eval> を、STDERR に警告メッセージを表示させない目的や、 |
3403 | 3397 | 警告メッセージを C<$@> に格納する目的では使わないでください。 |
3404 | 3398 | そのような用途では、C<$SIG{__WARN__}> 機能を使うか、 |
3405 | 3399 | S<C<no warnings 'all'>> を使って BLOCK か EXPR の内部での警告を |
3406 | 3400 | オフにする必要があります。 |
3407 | 3401 | L</warn>, L<perlvar>, L<warnings>, L<perllexwarn> を参照してください。 |
3408 | 3402 | |
3409 | 3403 | =begin original |
3410 | 3404 | |
3411 | 3405 | Note that, because C<eval> traps otherwise-fatal errors, it is useful for |
3412 | 3406 | determining whether a particular feature (such as C<socket> or C<symlink>) |
3413 | 3407 | is implemented. It is also Perl's exception trapping mechanism, where |
3414 | 3408 | the die operator is used to raise exceptions. |
3415 | 3409 | |
3416 | 3410 | =end original |
3417 | 3411 | |
3418 | 3412 | C<eval> は、致命的エラーとなるようなものをトラップすることができますから、 |
3419 | 3413 | (C<socket> や C<symlink> といった) 特定の機能が実装されているかを、 |
3420 | 3414 | 調べるために使うことができることに注意してください。 |
3421 | 3415 | die 演算子が例外を発生させるものとすれば、 |
3422 | 3416 | これはまた、Perl の例外捕捉機能と捉えることもできます。 |
3423 | 3417 | |
3424 | 3418 | =begin original |
3425 | 3419 | |
3426 | If you want to trap errors when loading an XS module, some problems with | |
3427 | the binary interface (such as Perl version skew) may be fatal even with | |
3428 | C<eval> unless C<$ENV{PERL_DL_NONLAZY}> is set. See L<perlrun>. | |
3429 | ||
3430 | =end original | |
3431 | ||
3432 | XS モジュールのロード中のエラーをトラップしたいなら、 | |
3433 | (Perl バージョンの違いのような) バイナリインターフェースに関する問題に | |
3434 | ついては C<$ENV{PERL_DL_NONLAZY}> がセットされていない C<eval> でも | |
3435 | 致命的エラーになるかもしれません。 | |
3436 | L<perlrun> を参照してください。 | |
3437 | ||
3438 | =begin original | |
3439 | ||
3440 | 3420 | If the code to be executed doesn't vary, you may use the eval-BLOCK |
3441 | 3421 | form to trap run-time errors without incurring the penalty of |
3442 | 3422 | recompiling each time. The error, if any, is still returned in C<$@>. |
3443 | 3423 | Examples: |
3444 | 3424 | |
3445 | 3425 | =end original |
3446 | 3426 | |
3447 | 3427 | 実行するコードが変わらないのであれば、毎回多量の再コンパイルすることなしに、 |
3448 | 3428 | 実行時エラーのトラップを行なうために、 |
3449 | 3429 | eval-BLOCK 形式を使うことができます。 |
3450 | 3430 | エラーがあれば、やはり $@ に返されます。 |
3451 | 3431 | 例: |
3452 | 3432 | |
3453 | 3433 | # make divide-by-zero nonfatal |
3454 | 3434 | eval { $answer = $a / $b; }; warn $@ if $@; |
3455 | 3435 | |
3456 | 3436 | # same thing, but less efficient |
3457 | 3437 | eval '$answer = $a / $b'; warn $@ if $@; |
3458 | 3438 | |
3459 | 3439 | # a compile-time error |
3460 | 3440 | eval { $answer = }; # WRONG |
3461 | 3441 | |
3462 | 3442 | # a run-time error |
3463 | 3443 | eval '$answer ='; # sets $@ |
3464 | 3444 | |
3465 | 3445 | =begin original |
3466 | 3446 | |
3467 | 3447 | Using the C<eval{}> form as an exception trap in libraries does have some |
3468 | 3448 | issues. Due to the current arguably broken state of C<__DIE__> hooks, you |
3469 | 3449 | may wish not to trigger any C<__DIE__> hooks that user code may have installed. |
3470 | 3450 | You can use the C<local $SIG{__DIE__}> construct for this purpose, |
3471 | 3451 | as shown in this example: |
3472 | 3452 | |
3473 | 3453 | =end original |
3474 | 3454 | |
3475 | 3455 | C<eval{}> 形式をライブラリの例外を捕捉するために使うときには |
3476 | 3456 | 問題があります。 |
3477 | 3457 | 現在の C<__DIE__> フックの状態はほぼ確実に壊れているという理由で、 |
3478 | 3458 | ユーザーのコードが設定した C<__DIE__> フックを実行したくないかもしれません。 |
3479 | 3459 | この目的には以下の例のように、C<local $SIG{__DIE__}> 構造が使えます。 |
3480 | 3460 | |
3481 | 3461 | # a very private exception trap for divide-by-zero |
3482 | 3462 | eval { local $SIG{'__DIE__'}; $answer = $a / $b; }; |
3483 | 3463 | warn $@ if $@; |
3484 | 3464 | |
3485 | 3465 | =begin original |
3486 | 3466 | |
3487 | 3467 | This is especially significant, given that C<__DIE__> hooks can call |
3488 | 3468 | C<die> again, which has the effect of changing their error messages: |
3489 | 3469 | |
3490 | 3470 | =end original |
3491 | 3471 | |
3492 | 3472 | これは特に顕著です。与えられた C<__DIE__> フックは C<die> をもう一度 |
3493 | 3473 | 呼び出すことができ、これによってエラーメッセージを変える効果があります: |
3494 | 3474 | |
3495 | 3475 | # __DIE__ hooks may modify error messages |
3496 | 3476 | { |
3497 | 3477 | local $SIG{'__DIE__'} = |
3498 | 3478 | sub { (my $x = $_[0]) =~ s/foo/bar/g; die $x }; |
3499 | 3479 | eval { die "foo lives here" }; |
3500 | 3480 | print $@ if $@; # prints "bar lives here" |
3501 | 3481 | } |
3502 | 3482 | |
3503 | 3483 | =begin original |
3504 | 3484 | |
3505 | 3485 | Because this promotes action at a distance, this counterintuitive behavior |
3506 | 3486 | may be fixed in a future release. |
3507 | 3487 | |
3508 | 3488 | =end original |
3509 | 3489 | |
3510 | 3490 | これは距離の離れた行動であるため、この直感的でない振る舞いは |
3511 | 3491 | 将来のリリースでは修正されるかもしれません。 |
3512 | 3492 | |
3513 | 3493 | =begin original |
3514 | 3494 | |
3515 | 3495 | With an C<eval>, you should be especially careful to remember what's |
3516 | 3496 | being looked at when: |
3517 | 3497 | |
3518 | 3498 | =end original |
3519 | 3499 | |
3520 | 3500 | C<eval> では、以下のような場合に、 |
3521 | 3501 | 何が調べられるかに特に注意しておくことが必要です: |
3522 | 3502 | |
3523 | 3503 | eval $x; # CASE 1 |
3524 | 3504 | eval "$x"; # CASE 2 |
3525 | 3505 | |
3526 | 3506 | eval '$x'; # CASE 3 |
3527 | 3507 | eval { $x }; # CASE 4 |
3528 | 3508 | |
3529 | 3509 | eval "\$$x++"; # CASE 5 |
3530 | 3510 | $$x++; # CASE 6 |
3531 | 3511 | |
3532 | 3512 | =begin original |
3533 | 3513 | |
3534 | 3514 | Cases 1 and 2 above behave identically: they run the code contained in |
3535 | 3515 | the variable $x. (Although case 2 has misleading double quotes making |
3536 | 3516 | the reader wonder what else might be happening (nothing is).) Cases 3 |
3537 | 3517 | and 4 likewise behave in the same way: they run the code C<'$x'>, which |
3538 | 3518 | does nothing but return the value of $x. (Case 4 is preferred for |
3539 | 3519 | purely visual reasons, but it also has the advantage of compiling at |
3540 | 3520 | compile-time instead of at run-time.) Case 5 is a place where |
3541 | 3521 | normally you I<would> like to use double quotes, except that in this |
3542 | 3522 | particular situation, you can just use symbolic references instead, as |
3543 | 3523 | in case 6. |
3544 | 3524 | |
3545 | 3525 | =end original |
3546 | 3526 | |
3547 | 3527 | 上記の CASE 1 と CASE 2 の動作は同一で、変数 $x 内の |
3548 | 3528 | コードを実行します。 |
3549 | 3529 | (ただし、CASE 2 では、必要のないダブルクォートによって、 |
3550 | 3530 | 読む人が何が起こるか混乱することでしょう (何も起こりませんが)。) |
3551 | 3531 | 同様に CASE 3 と CASE 4 の動作も等しく、$x の値を返す以外に |
3552 | 3532 | 何もしない C<$x> というコードを実行します |
3553 | 3533 | (純粋に見た目の問題で、CASE 4 が好まれますが、 |
3554 | 3534 | 実行時でなくコンパイル時にコンパイルされるという利点もあります)。 |
3555 | 3535 | CASE 5 の場合は、通常ダブルクォートを使用します。 |
3556 | 3536 | この状況を除けば、CASE 6 のように、単に |
3557 | 3537 | シンボリックリファレンスを使えば良いでしょう。 |
3558 | 3538 | |
3559 | 3539 | =begin original |
3560 | 3540 | |
3561 | The assignment to C<$@> occurs before restoration of localised variables, | |
3562 | which means a temporary is required if you want to mask some but not all | |
3563 | errors: | |
3564 | ||
3565 | =end original | |
3566 | ||
3567 | C<$@> への代入はローカル化された変数の復帰の前に起きるので、 | |
3568 | 全てではなく一部だけののエラーをマスクしたい場合には一時変数が必要です: | |
3569 | ||
3570 | # alter $@ on nefarious repugnancy only | |
3571 | { | |
3572 | my $e; | |
3573 | { | |
3574 | local $@; # protect existing $@ | |
3575 | eval { test_repugnancy() }; | |
3576 | # $@ =~ /nefarious/ and die $@; # DOES NOT WORK | |
3577 | $@ =~ /nefarious/ and $e = $@; | |
3578 | } | |
3579 | die $e if defined $e | |
3580 | } | |
3581 | ||
3582 | =begin original | |
3583 | ||
3584 | 3541 | C<eval BLOCK> does I<not> count as a loop, so the loop control statements |
3585 | 3542 | C<next>, C<last>, or C<redo> cannot be used to leave or restart the block. |
3586 | 3543 | |
3587 | 3544 | =end original |
3588 | 3545 | |
3589 | 3546 | C<eval BLOCK> はループとして I<扱われません>。 |
3590 | 3547 | 従って、C<next>, C<last>, C<redo> といったループ制御文でブロックから離れたり |
3591 | 3548 | 再実行したりはできません。 |
3592 | 3549 | |
3593 | 3550 | =begin original |
3594 | 3551 | |
3595 | 3552 | Note that as a very special case, an C<eval ''> executed within the C<DB> |
3596 | 3553 | package doesn't see the usual surrounding lexical scope, but rather the |
3597 | 3554 | scope of the first non-DB piece of code that called it. You don't normally |
3598 | 3555 | need to worry about this unless you are writing a Perl debugger. |
3599 | 3556 | |
3600 | 3557 | =end original |
3601 | 3558 | |
3602 | 3559 | とても特殊な場合として、C<DB> パッケージ内で C<eval ''> を実行すると、 |
3603 | 3560 | 通常のレキシカルスコープではなく、これを呼び出した最初の非 DB コード片の |
3604 | 3561 | スコープになります。 |
3605 | 3562 | Perl デバッガを書いているのでない限り、普通はこれについて心配する必要は |
3606 | 3563 | ありません。 |
3607 | 3564 | |
3608 | 3565 | =item exec LIST |
3609 | 3566 | X<exec> X<execute> |
3610 | 3567 | |
3611 | 3568 | =item exec PROGRAM LIST |
3612 | 3569 | |
3613 | 3570 | =begin original |
3614 | 3571 | |
3615 | 3572 | The C<exec> function executes a system command I<and never returns>-- |
3616 | 3573 | use C<system> instead of C<exec> if you want it to return. It fails and |
3617 | 3574 | returns false only if the command does not exist I<and> it is executed |
3618 | 3575 | directly instead of via your system's command shell (see below). |
3619 | 3576 | |
3620 | 3577 | =end original |
3621 | 3578 | |
3622 | 3579 | C<exec> 関数は、システムのコマンドを実行し、I<戻ってはきません>。 |
3623 | 3580 | 戻って欲しい場合には、C<exec>ではなく C<system> 関数を使ってください。 |
3624 | 3581 | コマンドが存在せず、I<しかも> システムのコマンドシェル経由でなく |
3625 | 3582 | 直接コマンドを実行しようとした場合にのみこの関数は失敗して偽を返します。 |
3626 | 3583 | |
3627 | 3584 | =begin original |
3628 | 3585 | |
3629 | 3586 | Since it's a common mistake to use C<exec> instead of C<system>, Perl |
3630 | 3587 | warns you if there is a following statement which isn't C<die>, C<warn>, |
3631 | 3588 | or C<exit> (if C<-w> is set - but you always do that). If you |
3632 | 3589 | I<really> want to follow an C<exec> with some other statement, you |
3633 | 3590 | can use one of these styles to avoid the warning: |
3634 | 3591 | |
3635 | 3592 | =end original |
3636 | 3593 | |
3637 | 3594 | C<system> の代わりに C<exec> を使うというよくある間違いを防ぐために、 |
3638 | 3595 | 引き続く文が C<die>, C<warn>, C<exit>(C<-w> がセットされている場合 - |
3639 | 3596 | でもいつもセットしてますよね) 以外の場合、Perl は警告を出します。 |
3640 | 3597 | もし I<本当に> C<exec> の後に他の文を書きたい場合、 |
3641 | 3598 | 以下のどちらかのスタイルを使うことで警告を回避できます: |
3642 | 3599 | |
3643 | 3600 | exec ('foo') or print STDERR "couldn't exec foo: $!"; |
3644 | 3601 | { exec ('foo') }; print STDERR "couldn't exec foo: $!"; |
3645 | 3602 | |
3646 | 3603 | =begin original |
3647 | 3604 | |
3648 | 3605 | If there is more than one argument in LIST, or if LIST is an array |
3649 | 3606 | with more than one value, calls execvp(3) with the arguments in LIST. |
3650 | 3607 | If there is only one scalar argument or an array with one element in it, |
3651 | 3608 | the argument is checked for shell metacharacters, and if there are any, |
3652 | 3609 | the entire argument is passed to the system's command shell for parsing |
3653 | 3610 | (this is C</bin/sh -c> on Unix platforms, but varies on other platforms). |
3654 | 3611 | If there are no shell metacharacters in the argument, it is split into |
3655 | 3612 | words and passed directly to C<execvp>, which is more efficient. |
3656 | 3613 | Examples: |
3657 | 3614 | |
3658 | 3615 | =end original |
3659 | 3616 | |
3660 | 3617 | LIST に複数の引数がある場合か、LIST が複数の値を持つ |
3661 | 3618 | 配列の場合には、LIST の引数を使って、execvp(3) を呼び出します。 |
3662 | 3619 | 1 つのスカラ引数のみまたは要素が 1 つの配列の場合には、その引数から |
3663 | 3620 | シェルのメタ文字をチェックし、もし、メタ文字があれば、 |
3664 | 3621 | 引数全体をシステムのコマンドシェル(これはUnix では |
3665 | 3622 | C</bin/sh -c> ですが、システムによって異なります)に渡して解析させます。 |
3666 | 3623 | もし、メタキャラクタがなければ、その引数を単語に分け、 |
3667 | 3624 | より効率的な C<execvp> に直接渡します。 |
3668 | 3625 | 例: |
3669 | 3626 | |
3670 | 3627 | exec '/bin/echo', 'Your arguments are: ', @ARGV; |
3671 | 3628 | exec "sort $outfile | uniq"; |
3672 | 3629 | |
3673 | 3630 | =begin original |
3674 | 3631 | |
3675 | 3632 | If you don't really want to execute the first argument, but want to lie |
3676 | 3633 | to the program you are executing about its own name, you can specify |
3677 | 3634 | the program you actually want to run as an "indirect object" (without a |
3678 | 3635 | comma) in front of the LIST. (This always forces interpretation of the |
3679 | 3636 | LIST as a multivalued list, even if there is only a single scalar in |
3680 | 3637 | the list.) Example: |
3681 | 3638 | |
3682 | 3639 | =end original |
3683 | 3640 | |
3684 | 3641 | 第一引数に指定するものを本当に実行したいが、実行する |
3685 | 3642 | プログラムに対して別の名前を教えたい場合には、LISTの前に、 |
3686 | 3643 | 「間接オブジェクト」(コンマなし) として、実際に |
3687 | 3644 | 実行したいプログラムを指定することができます。 |
3688 | 3645 | (これによって、LIST に単一のスカラしかなくても、複数 |
3689 | 3646 | 値のリストであるように、LIST の解釈を行ないます。) |
3690 | 3647 | 例: |
3691 | 3648 | |
3692 | 3649 | $shell = '/bin/csh'; |
3693 | 3650 | exec $shell '-sh'; # pretend it's a login shell |
3694 | 3651 | |
3695 | 3652 | =begin original |
3696 | 3653 | |
3697 | 3654 | or, more directly, |
3698 | 3655 | |
3699 | 3656 | =end original |
3700 | 3657 | |
3701 | 3658 | あるいは、より直接的に、 |
3702 | 3659 | |
3703 | 3660 | exec {'/bin/csh'} '-sh'; # pretend it's a login shell |
3704 | 3661 | |
3705 | 3662 | =begin original |
3706 | 3663 | |
3707 | 3664 | When the arguments get executed via the system shell, results will |
3708 | 3665 | be subject to its quirks and capabilities. See L<perlop/"`STRING`"> |
3709 | 3666 | for details. |
3710 | 3667 | |
3711 | 3668 | =end original |
3712 | 3669 | |
3713 | 3670 | 引数がシステムシェルで実行されるとき、結果はシェルの奇癖と能力によって |
3714 | 3671 | 変わります。 |
3715 | 3672 | 詳細については L<perlop/"`STRING`"> を参照してください。 |
3716 | 3673 | |
3717 | 3674 | =begin original |
3718 | 3675 | |
3719 | 3676 | Using an indirect object with C<exec> or C<system> is also more |
3720 | 3677 | secure. This usage (which also works fine with system()) forces |
3721 | 3678 | interpretation of the arguments as a multivalued list, even if the |
3722 | 3679 | list had just one argument. That way you're safe from the shell |
3723 | 3680 | expanding wildcards or splitting up words with whitespace in them. |
3724 | 3681 | |
3725 | 3682 | =end original |
3726 | 3683 | |
3727 | 3684 | C<exec> や C<system> で間接オブジェクトを使うのもより安全です。 |
3728 | 3685 | この使い方(system() でも同様にうまく動きます)は、たとえ引数が一つだけの |
3729 | 3686 | 場合も、複数の値を持つリストとして引数を解釈することを強制します。 |
3730 | 3687 | この方法で、シェルによるワイルドカード展開や、空白による単語の分割から |
3731 | 3688 | 守られます。 |
3732 | 3689 | |
3733 | 3690 | @args = ( "echo surprise" ); |
3734 | 3691 | |
3735 | 3692 | exec @args; # subject to shell escapes |
3736 | 3693 | # if @args == 1 |
3737 | 3694 | exec { $args[0] } @args; # safe even with one-arg list |
3738 | 3695 | |
3739 | 3696 | =begin original |
3740 | 3697 | |
3741 | 3698 | The first version, the one without the indirect object, ran the I<echo> |
3742 | 3699 | program, passing it C<"surprise"> an argument. The second version |
3743 | 3700 | didn't--it tried to run a program literally called I<"echo surprise">, |
3744 | 3701 | didn't find it, and set C<$?> to a non-zero value indicating failure. |
3745 | 3702 | |
3746 | 3703 | =end original |
3747 | 3704 | |
3748 | 3705 | 間接オブジェクトなしの一つ目のバージョンでは、I<echo> プログラムが実行され、 |
3749 | 3706 | C<"surprise"> が引数として渡されます。 |
3750 | 3707 | 二つ目のバージョンでは違います -- 文字通り I<"echo surprise"> という名前の |
3751 | 3708 | プログラムを実行しようとして、見つからないので、失敗したことを示すために |
3752 | 3709 | C<$?> に非 0 がセットされます。 |
3753 | 3710 | |
3754 | 3711 | =begin original |
3755 | 3712 | |
3756 | 3713 | Beginning with v5.6.0, Perl will attempt to flush all files opened for |
3757 | 3714 | output before the exec, but this may not be supported on some platforms |
3758 | 3715 | (see L<perlport>). To be safe, you may need to set C<$|> ($AUTOFLUSH |
3759 | 3716 | in English) or call the C<autoflush()> method of C<IO::Handle> on any |
3760 | 3717 | open handles in order to avoid lost output. |
3761 | 3718 | |
3762 | 3719 | =end original |
3763 | 3720 | |
3764 | 3721 | v5.6.0 から、Perl は exec の前に出力用に開かれている全てのファイルを |
3765 | 3722 | フラッシュしようとしますが、これに対応していないプラットフォームもあります |
3766 | 3723 | (L<perlport> を参照してください)。 |
3767 | 3724 | 安全のためには、出力が重複するのを避けるために、全てのオープンしている |
3768 | 3725 | ハンドルに対して C<$|> (English モジュールでは $AUTOFLUSH) を設定するか、 |
3769 | 3726 | C<IO::Handle> モジュールの C<autoflush()> メソッドをを呼ぶ必要が |
3770 | 3727 | あるかもしれません。 |
3771 | 3728 | |
3772 | 3729 | =begin original |
3773 | 3730 | |
3774 | 3731 | Note that C<exec> will not call your C<END> blocks, nor will it call |
3775 | 3732 | any C<DESTROY> methods in your objects. |
3776 | 3733 | |
3777 | 3734 | =end original |
3778 | 3735 | |
3779 | 3736 | C<exec> は C<END> ブロックや、オブジェクトの C<DESTROY> メソッドを |
3780 | 3737 | 呼び出さないことに注意してください。 |
3781 | 3738 | |
3782 | 3739 | =item exists EXPR |
3783 | 3740 | X<exists> X<autovivification> |
3784 | 3741 | |
3785 | 3742 | =begin original |
3786 | 3743 | |
3787 | 3744 | Given an expression that specifies a hash element or array element, |
3788 | 3745 | returns true if the specified element in the hash or array has ever |
3789 | been initialized, even if the corresponding value is undefined. | |
3746 | been initialized, even if the corresponding value is undefined. The | |
3747 | element is not autovivified if it doesn't exist. | |
3790 | 3748 | |
3791 | 3749 | =end original |
3792 | 3750 | |
3793 | 3751 | ハッシュ要素か配列要素を示す表現が与えられ、 |
3794 | 3752 | 指定された要素が、ハッシュか配列に存在すれば、 |
3795 | 3753 | たとえ対応する value が未定義でも真を返します。 |
3754 | 要素が存在しなかった場合は自動活性化されません。 | |
3796 | 3755 | |
3797 | 3756 | print "Exists\n" if exists $hash{$key}; |
3798 | 3757 | print "Defined\n" if defined $hash{$key}; |
3799 | 3758 | print "True\n" if $hash{$key}; |
3800 | 3759 | |
3801 | 3760 | print "Exists\n" if exists $array[$index]; |
3802 | 3761 | print "Defined\n" if defined $array[$index]; |
3803 | 3762 | print "True\n" if $array[$index]; |
3804 | 3763 | |
3805 | 3764 | =begin original |
3806 | 3765 | |
3807 | 3766 | A hash or array element can be true only if it's defined, and defined if |
3808 | 3767 | it exists, but the reverse doesn't necessarily hold true. |
3809 | 3768 | |
3810 | 3769 | =end original |
3811 | 3770 | |
3812 | 3771 | ハッシュまたは配列要素は、定義されているときにのみ真となり、 |
3813 | 3772 | 存在しているときにのみ定義されますが、逆は必ずしも真ではありません。 |
3814 | 3773 | |
3815 | 3774 | =begin original |
3816 | 3775 | |
3817 | 3776 | Given an expression that specifies the name of a subroutine, |
3818 | 3777 | returns true if the specified subroutine has ever been declared, even |
3819 | 3778 | if it is undefined. Mentioning a subroutine name for exists or defined |
3820 | 3779 | does not count as declaring it. Note that a subroutine which does not |
3821 | 3780 | exist may still be callable: its package may have an C<AUTOLOAD> |
3822 | 3781 | method that makes it spring into existence the first time that it is |
3823 | 3782 | called -- see L<perlsub>. |
3824 | 3783 | |
3825 | 3784 | =end original |
3826 | 3785 | |
3827 | 3786 | 引数としてサブルーチンの名前が指定された場合、 |
3828 | 3787 | 指定されたサブルーチンが宣言されていれば(たとえ未定義でも) |
3829 | 3788 | 真を返します。 |
3830 | 3789 | exists や defined のために言及されているサブルーチン名は |
3831 | 3790 | 宣言としてのカウントに入りません。 |
3832 | 3791 | 存在しないサブルーチンでも呼び出し可能かもしれないことに注意してください: |
3833 | 3792 | パッケージが C<AUTOLOAD> メソッドを持っていて、最初に呼び出された時に |
3834 | 3793 | 存在を作り出すかもしれません -- L<perlsub> を参照してください。 |
3835 | 3794 | |
3836 | 3795 | print "Exists\n" if exists &subroutine; |
3837 | 3796 | print "Defined\n" if defined &subroutine; |
3838 | 3797 | |
3839 | 3798 | =begin original |
3840 | 3799 | |
3841 | 3800 | Note that the EXPR can be arbitrarily complicated as long as the final |
3842 | 3801 | operation is a hash or array key lookup or subroutine name: |
3843 | 3802 | |
3844 | 3803 | =end original |
3845 | 3804 | |
3846 | 3805 | 最終的な操作がハッシュや配列の key による検索またはサブルーチン名である限りは、 |
3847 | 3806 | EXPR には任意の複雑な式を置くことができます: |
3848 | 3807 | |
3849 | 3808 | if (exists $ref->{A}->{B}->{$key}) { } |
3850 | 3809 | if (exists $hash{A}{B}{$key}) { } |
3851 | 3810 | |
3852 | 3811 | if (exists $ref->{A}->{B}->[$ix]) { } |
3853 | 3812 | if (exists $hash{A}{B}[$ix]) { } |
3854 | 3813 | |
3855 | 3814 | if (exists &{$ref->{A}{B}{$key}}) { } |
3856 | 3815 | |
3857 | 3816 | =begin original |
3858 | 3817 | |
3859 | 3818 | Although the deepest nested array or hash will not spring into existence |
3860 | 3819 | just because its existence was tested, any intervening ones will. |
3861 | 3820 | Thus C<< $ref->{"A"} >> and C<< $ref->{"A"}->{"B"} >> will spring |
3862 | 3821 | into existence due to the existence test for the $key element above. |
3863 | 3822 | This happens anywhere the arrow operator is used, including even: |
3864 | 3823 | |
3865 | 3824 | =end original |
3866 | 3825 | |
3867 | 3826 | ネストした配列やハッシュの一番深い部分は、その存在をテストしただけでは |
3868 | 3827 | 存在するようにはなりませんが、途中のものは存在するようになります。 |
3869 | 3828 | 従って C<< $ref->{"A"} >> と C<< $ref->{"A"}->{"B"} >> は上記の $key の |
3870 | 3829 | 存在をテストしたことによって存在するようになります。 |
3871 | 3830 | これは、矢印演算子が使われるところでは、以下のようなものを含むどこででも |
3872 | 3831 | 起こります。 |
3873 | 3832 | |
3874 | 3833 | undef $ref; |
3875 | 3834 | if (exists $ref->{"Some key"}) { } |
3876 | 3835 | print $ref; # prints HASH(0x80d3d5c) |
3877 | 3836 | |
3878 | 3837 | =begin original |
3879 | 3838 | |
3880 | 3839 | This surprising autovivification in what does not at first--or even |
3881 | 3840 | second--glance appear to be an lvalue context may be fixed in a future |
3882 | 3841 | release. |
3883 | 3842 | |
3884 | 3843 | =end original |
3885 | 3844 | |
3886 | 3845 | 一目見ただけでは -- あるいは二目見ても -- 驚かされる、左辺値コンテキストでの |
3887 | 3846 | 自動有効化は将来のリリースでは修正されるでしょう。 |
3888 | 3847 | |
3889 | 3848 | =begin original |
3890 | 3849 | |
3891 | 3850 | Use of a subroutine call, rather than a subroutine name, as an argument |
3892 | 3851 | to exists() is an error. |
3893 | 3852 | |
3894 | 3853 | =end original |
3895 | 3854 | |
3896 | 3855 | exists() の引数としてサブルーチン名でなくサブルーチン呼び出しを使うと、 |
3897 | 3856 | エラーになります。 |
3898 | 3857 | |
3899 | 3858 | exists ⊂ # OK |
3900 | 3859 | exists &sub(); # Error |
3901 | 3860 | |
3902 | 3861 | =item exit EXPR |
3903 | 3862 | X<exit> X<terminate> X<abort> |
3904 | 3863 | |
3905 | 3864 | =item exit |
3906 | 3865 | |
3907 | 3866 | =begin original |
3908 | 3867 | |
3909 | 3868 | Evaluates EXPR and exits immediately with that value. Example: |
3910 | 3869 | |
3911 | 3870 | =end original |
3912 | 3871 | |
3913 | 3872 | EXPR を評価し、即座にその値を持って終了します。 |
3914 | 3873 | 例: |
3915 | 3874 | |
3916 | 3875 | $ans = <STDIN>; |
3917 | 3876 | exit 0 if $ans =~ /^[Xx]/; |
3918 | 3877 | |
3919 | 3878 | =begin original |
3920 | 3879 | |
3921 | 3880 | See also C<die>. If EXPR is omitted, exits with C<0> status. The only |
3922 | 3881 | universally recognized values for EXPR are C<0> for success and C<1> |
3923 | 3882 | for error; other values are subject to interpretation depending on the |
3924 | 3883 | environment in which the Perl program is running. For example, exiting |
3925 | 3884 | 69 (EX_UNAVAILABLE) from a I<sendmail> incoming-mail filter will cause |
3926 | 3885 | the mailer to return the item undelivered, but that's not true everywhere. |
3927 | 3886 | |
3928 | 3887 | =end original |
3929 | 3888 | |
3930 | 3889 | C<die> も参照してください。 |
3931 | 3890 | EXPR が省略された場合には、ステータスを C<0> として終了します。 |
3932 | 3891 | EXPR の値として広く利用可能なのは C<0> が成功で C<1> がエラーということだけです。 |
3933 | 3892 | その他の値は、 Perl が実行される環境によって異なる解釈がされる |
3934 | 3893 | 可能性があります。 |
3935 | 3894 | 例えば、I<sendmail> 到着メールフィルタから 69 (EX_UNAVAILABLE) で終了すると |
3936 | 3895 | メーラーはアイテムを配達せずに差し戻しますが、 |
3937 | 3896 | これはいつでも真ではありません。 |
3938 | 3897 | |
3939 | 3898 | =begin original |
3940 | 3899 | |
3941 | 3900 | Don't use C<exit> to abort a subroutine if there's any chance that |
3942 | 3901 | someone might want to trap whatever error happened. Use C<die> instead, |
3943 | 3902 | which can be trapped by an C<eval>. |
3944 | 3903 | |
3945 | 3904 | =end original |
3946 | 3905 | |
3947 | 3906 | 誰かが発生したエラーをトラップしようと考えている可能性がある場合は、 |
3948 | 3907 | サブルーチンの中断に C<exit> を使わないでください。 |
3949 | 3908 | 代わりに C<eval> でトラップできる C<die> を使ってください。 |
3950 | 3909 | |
3951 | 3910 | =begin original |
3952 | 3911 | |
3953 | 3912 | The exit() function does not always exit immediately. It calls any |
3954 | 3913 | defined C<END> routines first, but these C<END> routines may not |
3955 | 3914 | themselves abort the exit. Likewise any object destructors that need to |
3956 | 3915 | be called are called before the real exit. If this is a problem, you |
3957 | 3916 | can call C<POSIX:_exit($status)> to avoid END and destructor processing. |
3958 | 3917 | See L<perlmod> for details. |
3959 | 3918 | |
3960 | 3919 | =end original |
3961 | 3920 | |
3962 | 3921 | exit() 関数は常に直ちに終了するわけではありません。 |
3963 | 3922 | まず、定義されている END ルーチンを呼び出しますが、 |
3964 | 3923 | C<END> ルーチン自身は exit を止められません。 |
3965 | 3924 | 同様に、呼び出す必要のあるオブジェクトデストラクタは |
3966 | 3925 | すべて、実際の終了前に呼び出されます。 |
3967 | 3926 | これが問題になる場合は、END やデストラクタが実行されることを |
3968 | 3927 | 防ぐために C<POSIX:_exit($status)> を呼び出してください。 |
3969 | 3928 | 詳しくは L<perlmod> を参照してください。 |
3970 | 3929 | |
3971 | 3930 | =item exp EXPR |
3972 | 3931 | X<exp> X<exponential> X<antilog> X<antilogarithm> X<e> |
3973 | 3932 | |
3974 | 3933 | =item exp |
3975 | 3934 | |
3976 | 3935 | =begin original |
3977 | 3936 | |
3978 | 3937 | Returns I<e> (the natural logarithm base) to the power of EXPR. |
3979 | 3938 | If EXPR is omitted, gives C<exp($_)>. |
3980 | 3939 | |
3981 | 3940 | =end original |
3982 | 3941 | |
3983 | 3942 | I<e> (自然対数の底) の EXPR 乗を返します。 |
3984 | 3943 | EXPR を省略した場合には、C<exp($_)> を返します。 |
3985 | 3944 | |
3986 | 3945 | =item fcntl FILEHANDLE,FUNCTION,SCALAR |
3987 | 3946 | X<fcntl> |
3988 | 3947 | |
3989 | 3948 | =begin original |
3990 | 3949 | |
3991 | 3950 | Implements the fcntl(2) function. You'll probably have to say |
3992 | 3951 | |
3993 | 3952 | =end original |
3994 | 3953 | |
3995 | 3954 | fcntl(2) 関数を実装します。 |
3996 | 3955 | 正しい定数定義を得るために、まず、 |
3997 | 3956 | |
3998 | 3957 | use Fcntl; |
3999 | 3958 | |
4000 | 3959 | =begin original |
4001 | 3960 | |
4002 | 3961 | first to get the correct constant definitions. Argument processing and |
4003 | 3962 | value return works just like C<ioctl> below. |
4004 | 3963 | For example: |
4005 | 3964 | |
4006 | 3965 | =end original |
4007 | 3966 | |
4008 | 3967 | と書くことが必要でしょう。 |
4009 | 3968 | 引数の処理と返り値については、下記の C<ioctl> と同様に動作します。 |
4010 | 3969 | 例: |
4011 | 3970 | |
4012 | 3971 | use Fcntl; |
4013 | 3972 | fcntl($filehandle, F_GETFL, $packed_return_buffer) |
4014 | 3973 | or die "can't fcntl F_GETFL: $!"; |
4015 | 3974 | |
4016 | 3975 | =begin original |
4017 | 3976 | |
4018 | 3977 | You don't have to check for C<defined> on the return from C<fcntl>. |
4019 | 3978 | Like C<ioctl>, it maps a C<0> return from the system call into |
4020 | 3979 | C<"0 but true"> in Perl. This string is true in boolean context and C<0> |
4021 | 3980 | in numeric context. It is also exempt from the normal B<-w> warnings |
4022 | 3981 | on improper numeric conversions. |
4023 | 3982 | |
4024 | 3983 | =end original |
4025 | 3984 | |
4026 | 3985 | C<fcntl> からの返り値のチェックに C<defined> を使う必要はありません。 |
4027 | 3986 | C<ioctl> と違って、C<fnctl> はシステムコールの結果が C<0> だった場合は |
4028 | 3987 | C<"0 だが真">を返します。 |
4029 | 3988 | この文字列は真偽値コンテキストでは真となり、 |
4030 | 3989 | 数値コンテキストでは C<0> になります。 |
4031 | 3990 | これはまた、不適切な数値変換に関する通常の B<-w> 警告を回避します。 |
4032 | 3991 | |
4033 | 3992 | =begin original |
4034 | 3993 | |
4035 | 3994 | Note that C<fcntl> will produce a fatal error if used on a machine that |
4036 | 3995 | doesn't implement fcntl(2). See the Fcntl module or your fcntl(2) |
4037 | 3996 | manpage to learn what functions are available on your system. |
4038 | 3997 | |
4039 | 3998 | =end original |
4040 | 3999 | |
4041 | 4000 | fcntl(2) が実装されていないマシンでは、C<fcntl>は致命的エラーを |
4042 | 4001 | 引き起こすことに注意してください。 |
4043 | 4002 | システムでどの関数が利用可能かについては Fcntl モジュールや |
4044 | 4003 | fcntl(2) man ページを参照してください。 |
4045 | 4004 | |
4046 | 4005 | =begin original |
4047 | 4006 | |
4048 | 4007 | Here's an example of setting a filehandle named C<REMOTE> to be |
4049 | 4008 | non-blocking at the system level. You'll have to negotiate C<$|> |
4050 | 4009 | on your own, though. |
4051 | 4010 | |
4052 | 4011 | =end original |
4053 | 4012 | |
4054 | 4013 | これは C<REMOTE> というファイルハンドルをシステムレベルで |
4055 | 4014 | 非ブロックモードにセットする例です。 |
4056 | 4015 | ただし、 C<$|> を自分で管理しなければなりません。 |
4057 | 4016 | |
4058 | 4017 | use Fcntl qw(F_GETFL F_SETFL O_NONBLOCK); |
4059 | 4018 | |
4060 | 4019 | $flags = fcntl(REMOTE, F_GETFL, 0) |
4061 | 4020 | or die "Can't get flags for the socket: $!\n"; |
4062 | 4021 | |
4063 | 4022 | $flags = fcntl(REMOTE, F_SETFL, $flags | O_NONBLOCK) |
4064 | 4023 | or die "Can't set flags for the socket: $!\n"; |
4065 | 4024 | |
4066 | 4025 | =item fileno FILEHANDLE |
4067 | 4026 | X<fileno> |
4068 | 4027 | |
4069 | 4028 | =begin original |
4070 | 4029 | |
4071 | 4030 | Returns the file descriptor for a filehandle, or undefined if the |
4072 | 4031 | filehandle is not open. This is mainly useful for constructing |
4073 | 4032 | bitmaps for C<select> and low-level POSIX tty-handling operations. |
4074 | 4033 | If FILEHANDLE is an expression, the value is taken as an indirect |
4075 | 4034 | filehandle, generally its name. |
4076 | 4035 | |
4077 | 4036 | =end original |
4078 | 4037 | |
4079 | 4038 | ファイルハンドルに対するファイル記述子を返します。 |
4080 | 4039 | ファイルハンドルがオープンしていない場合は未定義値を返します。 |
4081 | 4040 | これは主に C<select> や低レベル POSIX tty 操作に対する、ビットマップを |
4082 | 4041 | 構成するときに便利です。 |
4083 | 4042 | FILEHANDLE が式であれば、 |
4084 | 4043 | その値が間接ファイルハンドル(普通は名前)として使われます。 |
4085 | 4044 | |
4086 | 4045 | =begin original |
4087 | 4046 | |
4088 | 4047 | You can use this to find out whether two handles refer to the |
4089 | 4048 | same underlying descriptor: |
4090 | 4049 | |
4091 | 4050 | =end original |
4092 | 4051 | |
4093 | 4052 | これを、二つのハンドルが同じ識別子を参照しているかどうかを見つけるのに |
4094 | 4053 | 使えます: |
4095 | 4054 | |
4096 | 4055 | if (fileno(THIS) == fileno(THAT)) { |
4097 | 4056 | print "THIS and THAT are dups\n"; |
4098 | 4057 | } |
4099 | 4058 | |
4100 | 4059 | =begin original |
4101 | 4060 | |
4102 | 4061 | (Filehandles connected to memory objects via new features of C<open> may |
4103 | 4062 | return undefined even though they are open.) |
4104 | 4063 | |
4105 | 4064 | =end original |
4106 | 4065 | |
4107 | 4066 | (C<open> の新機能によってメモリに接続されたファイルハンドルは、 |
4108 | 4067 | 開いている時でも未定義値を返します。) |
4109 | 4068 | |
4110 | 4069 | |
4111 | 4070 | =item flock FILEHANDLE,OPERATION |
4112 | 4071 | X<flock> X<lock> X<locking> |
4113 | 4072 | |
4114 | 4073 | =begin original |
4115 | 4074 | |
4116 | 4075 | Calls flock(2), or an emulation of it, on FILEHANDLE. Returns true |
4117 | 4076 | for success, false on failure. Produces a fatal error if used on a |
4118 | 4077 | machine that doesn't implement flock(2), fcntl(2) locking, or lockf(3). |
4119 | 4078 | C<flock> is Perl's portable file locking interface, although it locks |
4120 | 4079 | only entire files, not records. |
4121 | 4080 | |
4122 | 4081 | =end original |
4123 | 4082 | |
4124 | 4083 | FILEHANDLE に対して flock(2)、またはそのエミュレーションを呼び出します。 |
4125 | 4084 | 成功時には真を、失敗時には偽を返します。 |
4126 | 4085 | flock(2), fcntl(2) ロック, lockf(3) のいずれかを実装していない |
4127 | 4086 | マシンで使うと、致命的エラーが発生します。 |
4128 | 4087 | C<flock> は Perl の移植性のあるファイルロックインターフェースです。 |
4129 | 4088 | しかしレコードではなく、ファイル全体のみをロックします。 |
4130 | 4089 | |
4131 | 4090 | =begin original |
4132 | 4091 | |
4133 | 4092 | Two potentially non-obvious but traditional C<flock> semantics are |
4134 | 4093 | that it waits indefinitely until the lock is granted, and that its locks |
4135 | 4094 | B<merely advisory>. Such discretionary locks are more flexible, but offer |
4136 | 4095 | fewer guarantees. This means that programs that do not also use C<flock> |
4137 | 4096 | may modify files locked with C<flock>. See L<perlport>, |
4138 | 4097 | your port's specific documentation, or your system-specific local manpages |
4139 | 4098 | for details. It's best to assume traditional behavior if you're writing |
4140 | 4099 | portable programs. (But if you're not, you should as always feel perfectly |
4141 | 4100 | free to write for your own system's idiosyncrasies (sometimes called |
4142 | 4101 | "features"). Slavish adherence to portability concerns shouldn't get |
4143 | 4102 | in the way of your getting your job done.) |
4144 | 4103 | |
4145 | 4104 | =end original |
4146 | 4105 | |
4147 | 4106 | 明白ではないものの、伝統的な C<flock> の動作としては、ロックが得られるまで |
4148 | 4107 | 無限に待ち続けるものと、B<単に勧告的に> ロックするものの二つがあります。 |
4149 | 4108 | このような自由裁量のロックはより柔軟ですが、保障されるものはより少ないです。 |
4150 | 4109 | これは、C<flock> を使わないプログラムが C<flock> でロックされたファイルを |
4151 | 4110 | 書き換えるかもしれないことを意味します。 |
4152 | 4111 | 詳細については、L<perlport>、システム固有のドキュメント、システム固有の |
4153 | 4112 | ローカルの man ページを参照してください。 |
4154 | 4113 | 移植性のあるプログラムを書く場合は、伝統的な振る舞いを仮定するのが |
4155 | 4114 | ベストです。 |
4156 | 4115 | (しかし移植性のないプログラムを書く場合は、自身のシステムの性癖(しばしば |
4157 | 4116 | 「仕様」と呼ばれます)に合わせて書くことも完全に自由です。 |
4158 | 4117 | 盲目的に移植性に固執することで、あなたの作業を仕上げるのを邪魔するべきでは |
4159 | 4118 | ありません。) |
4160 | 4119 | |
4161 | 4120 | =begin original |
4162 | 4121 | |
4163 | 4122 | OPERATION is one of LOCK_SH, LOCK_EX, or LOCK_UN, possibly combined with |
4164 | 4123 | LOCK_NB. These constants are traditionally valued 1, 2, 8 and 4, but |
4165 | 4124 | you can use the symbolic names if you import them from the Fcntl module, |
4166 | 4125 | either individually, or as a group using the ':flock' tag. LOCK_SH |
4167 | 4126 | requests a shared lock, LOCK_EX requests an exclusive lock, and LOCK_UN |
4168 | 4127 | releases a previously requested lock. If LOCK_NB is bitwise-or'ed with |
4169 | 4128 | LOCK_SH or LOCK_EX then C<flock> will return immediately rather than blocking |
4170 | 4129 | waiting for the lock (check the return status to see if you got it). |
4171 | 4130 | |
4172 | 4131 | =end original |
4173 | 4132 | |
4174 | 4133 | OPERATION は LOCK_SH, LOCK_EX, LOCK_UN のいずれかで、LOCK_NB と |
4175 | 4134 | 組み合わされることもあります。 |
4176 | 4135 | これらの定数は伝統的には 1, 2, 8, 4 の値を持ちますが、Fcntl モジュールから |
4177 | 4136 | シンボル名を独立してインポートするか、 ':flock' タグを使うグループとして、 |
4178 | 4137 | シンボル名をを使うことができます。 |
4179 | 4138 | LOCK_SH は共有ロックを要求し、LOCK_EX は排他ロックを要求し、LOCK_UN は |
4180 | 4139 | 前回要求したロックを開放します。 |
4181 | 4140 | LOCK_NB と LOCK_SH か LOCK_EX がビット単位の論理和されると、C<flock> は |
4182 | 4141 | ロックを取得するまで待つのではなく、すぐに返ります(ロックが取得できたか |
4183 | 4142 | どうかは返り値を調べます)。 |
4184 | 4143 | |
4185 | 4144 | =begin original |
4186 | 4145 | |
4187 | 4146 | To avoid the possibility of miscoordination, Perl now flushes FILEHANDLE |
4188 | 4147 | before locking or unlocking it. |
4189 | 4148 | |
4190 | 4149 | =end original |
4191 | 4150 | |
4192 | 4151 | 不一致の可能性を避けるために、Perl はファイルをロック、アンロックする前に |
4193 | 4152 | FILEHANDLE をフラッシュします。 |
4194 | 4153 | |
4195 | 4154 | =begin original |
4196 | 4155 | |
4197 | 4156 | Note that the emulation built with lockf(3) doesn't provide shared |
4198 | 4157 | locks, and it requires that FILEHANDLE be open with write intent. These |
4199 | 4158 | are the semantics that lockf(3) implements. Most if not all systems |
4200 | 4159 | implement lockf(3) in terms of fcntl(2) locking, though, so the |
4201 | 4160 | differing semantics shouldn't bite too many people. |
4202 | 4161 | |
4203 | 4162 | =end original |
4204 | 4163 | |
4205 | 4164 | lockf(3) で作成されたエミュレーションは共有ロックを提供せず、 |
4206 | 4165 | FILEHANDLE が書き込みモードで開いていることを必要とすることに |
4207 | 4166 | 注意してください。 |
4208 | 4167 | これは lockf(3) が実装している動作です。 |
4209 | 4168 | しかし、全てではないにしてもほとんどのシステムでは fcntl(2) を使って |
4210 | 4169 | lockf(3) を実装しているので、異なった動作で多くの人々を混乱させることは |
4211 | 4170 | ないはずです。 |
4212 | 4171 | |
4213 | 4172 | =begin original |
4214 | 4173 | |
4215 | 4174 | Note that the fcntl(2) emulation of flock(3) requires that FILEHANDLE |
4216 | 4175 | be open with read intent to use LOCK_SH and requires that it be open |
4217 | 4176 | with write intent to use LOCK_EX. |
4218 | 4177 | |
4219 | 4178 | =end original |
4220 | 4179 | |
4221 | 4180 | flock(3) の fcntl(2) エミュレーションは、 LOCK_SH を使うためには |
4222 | 4181 | FILEHANDLE を読み込みで開いている必要があり、LOCK_EX を使うためには |
4223 | 4182 | 書き込みで開いている必要があることに注意してください。 |
4224 | 4183 | |
4225 | 4184 | =begin original |
4226 | 4185 | |
4227 | 4186 | Note also that some versions of C<flock> cannot lock things over the |
4228 | 4187 | network; you would need to use the more system-specific C<fcntl> for |
4229 | 4188 | that. If you like you can force Perl to ignore your system's flock(2) |
4230 | 4189 | function, and so provide its own fcntl(2)-based emulation, by passing |
4231 | 4190 | the switch C<-Ud_flock> to the F<Configure> program when you configure |
4232 | 4191 | perl. |
4233 | 4192 | |
4234 | 4193 | =end original |
4235 | 4194 | |
4236 | 4195 | ネットワーク越しにはロックできない C<flock> もあることに注意してください; |
4237 | 4196 | このためには、よりシステム依存な C<fcntl> を使う必要があります。 |
4238 | 4197 | Perl にシステムの flock(2) 関数を無視させ、自身の fcntl(2) ベースの |
4239 | 4198 | エミュレーションを使う場合は、perl を設定するときに F<Configure> |
4240 | 4199 | プログラムに C<-Ud_flock> オプションを渡してください。 |
4241 | 4200 | |
4242 | 4201 | =begin original |
4243 | 4202 | |
4244 | 4203 | Here's a mailbox appender for BSD systems. |
4245 | 4204 | |
4246 | 4205 | =end original |
4247 | 4206 | |
4248 | 4207 | BSD システムでのメールボックスへの追加処理の例を示します。 |
4249 | 4208 | |
4250 | use Fcntl | |
4209 | use Fcntl ':flock'; # import LOCK_* constants | |
4251 | 4210 | |
4252 | 4211 | sub lock { |
4253 | | |
4212 | flock(MBOX,LOCK_EX); | |
4254 | | |
4213 | # and, in case someone appended | |
4214 | # while we were waiting... | |
4256 | | |
4215 | seek(MBOX, 0, 2); | |
4257 | seek($fh, 0, SEEK_END) or die "Cannot seek - $!\n"; | |
4258 | 4216 | } |
4259 | 4217 | |
4260 | 4218 | sub unlock { |
4261 | | |
4219 | flock(MBOX,LOCK_UN); | |
4262 | flock($fh, LOCK_UN) or die "Cannot unlock mailbox - $!\n"; | |
4263 | 4220 | } |
4264 | 4221 | |
4265 | open( | |
4222 | open(MBOX, ">>/usr/spool/mail/$ENV{'USER'}") | |
4266 | 4223 | or die "Can't open mailbox: $!"; |
4267 | 4224 | |
4268 | lock( | |
4225 | lock(); | |
4269 | print | |
4226 | print MBOX $msg,"\n\n"; | |
4270 | unlock( | |
4227 | unlock(); | |
4271 | 4228 | |
4272 | 4229 | =begin original |
4273 | 4230 | |
4274 | 4231 | On systems that support a real flock(), locks are inherited across fork() |
4275 | 4232 | calls, whereas those that must resort to the more capricious fcntl() |
4276 | 4233 | function lose the locks, making it harder to write servers. |
4277 | 4234 | |
4278 | 4235 | =end original |
4279 | 4236 | |
4280 | 4237 | 真の flock() に対応しているシステムではロックは fork() を通して |
4281 | 4238 | 継承されるのに対して、より不安定な fcntl() に頼らなければならない場合、 |
4282 | 4239 | サーバを書くのはより難しくなります。 |
4283 | 4240 | |
4284 | 4241 | =begin original |
4285 | 4242 | |
4286 | 4243 | See also L<DB_File> for other flock() examples. |
4287 | 4244 | |
4288 | 4245 | =end original |
4289 | 4246 | |
4290 | 4247 | その他の flock() の例としては L<DB_File> も参照してください。 |
4291 | 4248 | |
4292 | 4249 | =item fork |
4293 | 4250 | X<fork> X<child> X<parent> |
4294 | 4251 | |
4295 | 4252 | =begin original |
4296 | 4253 | |
4297 | 4254 | Does a fork(2) system call to create a new process running the |
4298 | 4255 | same program at the same point. It returns the child pid to the |
4299 | 4256 | parent process, C<0> to the child process, or C<undef> if the fork is |
4300 | 4257 | unsuccessful. File descriptors (and sometimes locks on those descriptors) |
4301 | 4258 | are shared, while everything else is copied. On most systems supporting |
4302 | 4259 | fork(), great care has gone into making it extremely efficient (for |
4303 | 4260 | example, using copy-on-write technology on data pages), making it the |
4304 | 4261 | dominant paradigm for multitasking over the last few decades. |
4305 | 4262 | |
4306 | 4263 | =end original |
4307 | 4264 | |
4308 | 4265 | 同じプログラムの同じ地点から開始する新しいプロセスを作成するために |
4309 | 4266 | システムコール fork(2) を行ないます。 |
4310 | 4267 | 親プロセスには、チャイルドプロセスの pid を、 |
4311 | 4268 | チャイルドプロセスに C<0> を返しますが、 |
4312 | 4269 | fork に失敗したときには、C<undef>を返します。 |
4313 | 4270 | ファイル記述子(および記述子に関連するロック)は共有され、 |
4314 | 4271 | その他の全てはコピーされます。 |
4315 | 4272 | fork() に対応するほとんどのシステムでは、 |
4316 | 4273 | これを極めて効率的にするために多大な努力が払われてきました |
4317 | 4274 | (例えば、データページへの copy-on-write テクノロジーなどです)。 |
4318 | 4275 | これはここ 20 年にわたるマルチタスクに関する主要なパラダイムとなっています。 |
4319 | 4276 | |
4320 | 4277 | =begin original |
4321 | 4278 | |
4322 | 4279 | Beginning with v5.6.0, Perl will attempt to flush all files opened for |
4323 | 4280 | output before forking the child process, but this may not be supported |
4324 | 4281 | on some platforms (see L<perlport>). To be safe, you may need to set |
4325 | 4282 | C<$|> ($AUTOFLUSH in English) or call the C<autoflush()> method of |
4326 | 4283 | C<IO::Handle> on any open handles in order to avoid duplicate output. |
4327 | 4284 | |
4328 | 4285 | =end original |
4329 | 4286 | |
4330 | 4287 | v5.6.0 から、Perl は子プロセスを fork する前に出力用にオープンしている全ての |
4331 | 4288 | ファイルをフラッシュしようとしますが、これに対応していないプラットフォームも |
4332 | 4289 | あります(L<perlport> を参照してください)。 |
4333 | 4290 | 安全のためには、出力が重複するのを避けるために、 |
4334 | 4291 | 全てのオープンしているハンドルに対して C<$|> (English モジュールでは |
4335 | 4292 | $AUTOFLUSH) を設定するか、 |
4336 | 4293 | C<IO::Handle> モジュールの C<autoflush()>メソッドをを呼ぶ必要が |
4337 | 4294 | あるかもしれません。 |
4338 | 4295 | |
4339 | 4296 | =begin original |
4340 | 4297 | |
4341 | 4298 | If you C<fork> without ever waiting on your children, you will |
4342 | 4299 | accumulate zombies. On some systems, you can avoid this by setting |
4343 | 4300 | C<$SIG{CHLD}> to C<"IGNORE">. See also L<perlipc> for more examples of |
4344 | 4301 | forking and reaping moribund children. |
4345 | 4302 | |
4346 | 4303 | =end original |
4347 | 4304 | |
4348 | 4305 | チャイルドプロセスの終了を待たずに、C<fork> を繰り返せば、 |
4349 | 4306 | ゾンビをためこむことになります。 |
4350 | 4307 | C<$SIG{CHLD}> に C<"IGNORE"> を指定することでこれを回避できるシステムもあります。 |
4351 | 4308 | fork と消滅しかけている子プロセスを回収するための更なる例については |
4352 | 4309 | L<perlipc> も参照してください。 |
4353 | 4310 | |
4354 | 4311 | =begin original |
4355 | 4312 | |
4356 | 4313 | Note that if your forked child inherits system file descriptors like |
4357 | 4314 | STDIN and STDOUT that are actually connected by a pipe or socket, even |
4358 | 4315 | if you exit, then the remote server (such as, say, a CGI script or a |
4359 | 4316 | backgrounded job launched from a remote shell) won't think you're done. |
4360 | 4317 | You should reopen those to F</dev/null> if it's any issue. |
4361 | 4318 | |
4362 | 4319 | =end original |
4363 | 4320 | |
4364 | 4321 | fork した子プロセスが STDIN や STDOUT といったシステムファイル記述子を |
4365 | 4322 | 継承する場合、(CGI スクリプトやリモートシェルといった |
4366 | 4323 | バックグラウンドジョブのような)リモートサーバは考え通りに |
4367 | 4324 | 動かないであろうことに注意してください。 |
4368 | 4325 | このような場合ではこれらを F</dev/null> として再オープンするべきです。 |
4369 | 4326 | |
4370 | 4327 | =item format |
4371 | 4328 | X<format> |
4372 | 4329 | |
4373 | 4330 | =begin original |
4374 | 4331 | |
4375 | 4332 | Declare a picture format for use by the C<write> function. For |
4376 | 4333 | example: |
4377 | 4334 | |
4378 | 4335 | =end original |
4379 | 4336 | |
4380 | 4337 | C<write> 関数で使うピクチャーフォーマットを宣言します。 |
4381 | 4338 | 例: |
4382 | 4339 | |
4383 | 4340 | format Something = |
4384 | 4341 | Test: @<<<<<<<< @||||| @>>>>> |
4385 | 4342 | $str, $%, '$' . int($num) |
4386 | 4343 | . |
4387 | 4344 | |
4388 | 4345 | $str = "widget"; |
4389 | 4346 | $num = $cost/$quantity; |
4390 | 4347 | $~ = 'Something'; |
4391 | 4348 | write; |
4392 | 4349 | |
4393 | 4350 | =begin original |
4394 | 4351 | |
4395 | 4352 | See L<perlform> for many details and examples. |
4396 | 4353 | |
4397 | 4354 | =end original |
4398 | 4355 | |
4399 | 4356 | 詳細と例については L<perlform> を参照して下さい。 |
4400 | 4357 | |
4401 | 4358 | =item formline PICTURE,LIST |
4402 | 4359 | X<formline> |
4403 | 4360 | |
4404 | 4361 | =begin original |
4405 | 4362 | |
4406 | 4363 | This is an internal function used by C<format>s, though you may call it, |
4407 | 4364 | too. It formats (see L<perlform>) a list of values according to the |
4408 | 4365 | contents of PICTURE, placing the output into the format output |
4409 | 4366 | accumulator, C<$^A> (or C<$ACCUMULATOR> in English). |
4410 | 4367 | Eventually, when a C<write> is done, the contents of |
4411 | 4368 | C<$^A> are written to some filehandle. You could also read C<$^A> |
4412 | 4369 | and then set C<$^A> back to C<"">. Note that a format typically |
4413 | 4370 | does one C<formline> per line of form, but the C<formline> function itself |
4414 | 4371 | doesn't care how many newlines are embedded in the PICTURE. This means |
4415 | 4372 | that the C<~> and C<~~> tokens will treat the entire PICTURE as a single line. |
4416 | 4373 | You may therefore need to use multiple formlines to implement a single |
4417 | 4374 | record format, just like the format compiler. |
4418 | 4375 | |
4419 | 4376 | =end original |
4420 | 4377 | |
4421 | 4378 | これは、C<format> が使用する内部関数ですが、直接呼び出すこともできます。 |
4422 | 4379 | これは、PICTURE の内容にしたがって、LIST の値を整形し (L<perlform> を |
4423 | 4380 | 参照してください)、結果をフォーマット出力アキュムレータC<$^A> |
4424 | 4381 | (English モジュールでは C<$ACCUMULATOR>) に納めます。 |
4425 | 4382 | 最終的に、C<write> が実行されると、C<$^A> の中身が、 |
4426 | 4383 | 何らかのファイルハンドルに書き出されます。 |
4427 | 4384 | また、自分で C<$^A> を読んで、C<$^A> の内容を C<""> に戻してもかまいません。 |
4428 | 4385 | format は通常、1 行ごとに C<formline> を行ないますが、 |
4429 | 4386 | C<formline> 関数自身は、PICTURE の中にいくつの改行が入っているかは、 |
4430 | 4387 | 関係がありません。 |
4431 | 4388 | これは、C<~> と C<~~>トークンは PICTURE 全体を一行として扱うことを意味します。 |
4432 | 4389 | 従って、1 レコードフォーマットを実装するためには |
4433 | 4390 | フォーマットコンパイラのような複数 formline を使う必要があります。 |
4434 | 4391 | |
4435 | 4392 | =begin original |
4436 | 4393 | |
4437 | 4394 | Be careful if you put double quotes around the picture, because an C<@> |
4438 | 4395 | character may be taken to mean the beginning of an array name. |
4439 | 4396 | C<formline> always returns true. See L<perlform> for other examples. |
4440 | 4397 | |
4441 | 4398 | =end original |
4442 | 4399 | |
4443 | 4400 | ダブルクォートで PICTURE を囲む場合には、C<@> という文字が |
4444 | 4401 | 配列名の始まりと解釈されますので、注意してください。 |
4445 | 4402 | C<formline> は常に真を返します。 |
4446 | 4403 | その他の例については L<perlform> を参照してください。 |
4447 | 4404 | |
4448 | 4405 | =item getc FILEHANDLE |
4449 | 4406 | X<getc> X<getchar> X<character> X<file, read> |
4450 | 4407 | |
4451 | 4408 | =item getc |
4452 | 4409 | |
4453 | 4410 | =begin original |
4454 | 4411 | |
4455 | 4412 | Returns the next character from the input file attached to FILEHANDLE, |
4456 | 4413 | or the undefined value at end of file, or if there was an error (in |
4457 | 4414 | the latter case C<$!> is set). If FILEHANDLE is omitted, reads from |
4458 | 4415 | STDIN. This is not particularly efficient. However, it cannot be |
4459 | 4416 | used by itself to fetch single characters without waiting for the user |
4460 | 4417 | to hit enter. For that, try something more like: |
4461 | 4418 | |
4462 | 4419 | =end original |
4463 | 4420 | |
4464 | 4421 | FILEHANDLE につながれている入力ファイルから、次の一文字を返します。 |
4465 | 4422 | ファイルの最後、またはエラーが発生した場合は、未定義値を返します |
4466 | 4423 | (後者の場合は C<$!> がセットされます)。 |
4467 | 4424 | FILEHANDLE が省略された場合には、STDIN から読み込みを行ないます。 |
4468 | 4425 | これは特に効率的ではありません。 |
4469 | 4426 | しかし、これはユーザーがリターンキーを押すのを待つことなく |
4470 | 4427 | 一文字を読み込む用途には使えません。 |
4471 | 4428 | そのような場合には、以下のようなものを試して見てください: |
4472 | 4429 | |
4473 | 4430 | if ($BSD_STYLE) { |
4474 | 4431 | system "stty cbreak </dev/tty >/dev/tty 2>&1"; |
4475 | 4432 | } |
4476 | 4433 | else { |
4477 | 4434 | system "stty", '-icanon', 'eol', "\001"; |
4478 | 4435 | } |
4479 | 4436 | |
4480 | 4437 | $key = getc(STDIN); |
4481 | 4438 | |
4482 | 4439 | if ($BSD_STYLE) { |
4483 | 4440 | system "stty -cbreak </dev/tty >/dev/tty 2>&1"; |
4484 | 4441 | } |
4485 | 4442 | else { |
4486 | 4443 | system "stty", 'icanon', 'eol', '^@'; # ASCII null |
4487 | 4444 | } |
4488 | 4445 | print "\n"; |
4489 | 4446 | |
4490 | 4447 | =begin original |
4491 | 4448 | |
4492 | 4449 | Determination of whether $BSD_STYLE should be set |
4493 | 4450 | is left as an exercise to the reader. |
4494 | 4451 | |
4495 | 4452 | =end original |
4496 | 4453 | |
4497 | 4454 | $BSD_STYLE をセットするべきかどうかを決定する方法については |
4498 | 4455 | 読者への宿題として残しておきます。 |
4499 | 4456 | |
4500 | 4457 | =begin original |
4501 | 4458 | |
4502 | 4459 | The C<POSIX::getattr> function can do this more portably on |
4503 | 4460 | systems purporting POSIX compliance. See also the C<Term::ReadKey> |
4504 | 4461 | module from your nearest CPAN site; details on CPAN can be found on |
4505 | 4462 | L<perlmodlib/CPAN>. |
4506 | 4463 | |
4507 | 4464 | =end original |
4508 | 4465 | |
4509 | 4466 | C<POSIX::getattr> 関数は POSIX 準拠を主張するシステムでこれを |
4510 | 4467 | より移植性のある形で行います。 |
4511 | 4468 | お近くの CPAN サイトから C<Term::ReadKey> モジュールも参照して下さい; |
4512 | 4469 | CPAN に関する詳細は L<perlmodlib/CPAN> にあります。 |
4513 | 4470 | |
4514 | 4471 | =item getlogin |
4515 | 4472 | X<getlogin> X<login> |
4516 | 4473 | |
4517 | 4474 | =begin original |
4518 | 4475 | |
4519 | 4476 | This implements the C library function of the same name, which on most |
4520 | 4477 | systems returns the current login from F</etc/utmp>, if any. If null, |
4521 | 4478 | use C<getpwuid>. |
4522 | 4479 | |
4523 | 4480 | =end original |
4524 | 4481 | |
4525 | 4482 | これは同じ名前の C ライブラリ関数を実装していて、 |
4526 | 4483 | 多くのシステムでは、もしあれば、/etc/utmp から現在のログイン名を返します。 |
4527 | 4484 | ヌルであれば、getpwuid() を使ってください。 |
4528 | 4485 | |
4529 | 4486 | $login = getlogin || getpwuid($<) || "Kilroy"; |
4530 | 4487 | |
4531 | 4488 | =begin original |
4532 | 4489 | |
4533 | 4490 | Do not consider C<getlogin> for authentication: it is not as |
4534 | 4491 | secure as C<getpwuid>. |
4535 | 4492 | |
4536 | 4493 | =end original |
4537 | 4494 | |
4538 | 4495 | C<getlogin> を認証に使ってはいけません。 |
4539 | 4496 | これは C<getpwuid> のように安全ではありません。 |
4540 | 4497 | |
4541 | 4498 | =item getpeername SOCKET |
4542 | 4499 | X<getpeername> X<peer> |
4543 | 4500 | |
4544 | 4501 | =begin original |
4545 | 4502 | |
4546 | 4503 | Returns the packed sockaddr address of other end of the SOCKET connection. |
4547 | 4504 | |
4548 | 4505 | =end original |
4549 | 4506 | |
4550 | 4507 | SOCKET コネクションの向こう側のパックされた aockaddr アドレスを返します。 |
4551 | 4508 | |
4552 | 4509 | use Socket; |
4553 | 4510 | $hersockaddr = getpeername(SOCK); |
4554 | 4511 | ($port, $iaddr) = sockaddr_in($hersockaddr); |
4555 | 4512 | $herhostname = gethostbyaddr($iaddr, AF_INET); |
4556 | 4513 | $herstraddr = inet_ntoa($iaddr); |
4557 | 4514 | |
4558 | 4515 | =item getpgrp PID |
4559 | 4516 | X<getpgrp> X<group> |
4560 | 4517 | |
4561 | 4518 | =begin original |
4562 | 4519 | |
4563 | 4520 | Returns the current process group for the specified PID. Use |
4564 | 4521 | a PID of C<0> to get the current process group for the |
4565 | 4522 | current process. Will raise an exception if used on a machine that |
4566 | 4523 | doesn't implement getpgrp(2). If PID is omitted, returns process |
4567 | 4524 | group of current process. Note that the POSIX version of C<getpgrp> |
4568 | 4525 | does not accept a PID argument, so only C<PID==0> is truly portable. |
4569 | 4526 | |
4570 | 4527 | =end original |
4571 | 4528 | |
4572 | 4529 | 指定された PID の現在のプロセスグループを返します。 |
4573 | 4530 | PID に C<0> を与えるとカレントプロセスの指定となります。 |
4574 | 4531 | getpgrp(2) を実装していないマシンで実行した場合には、例外が発生します。 |
4575 | 4532 | PID を省略するとカレントプロセスのプロセスグループを返します。 |
4576 | 4533 | POSIX 版の C<getpgrp> は PID 引数を受け付けないので、 |
4577 | 4534 | C<PID==0> のみが完全に移植性があります。 |
4578 | 4535 | |
4579 | 4536 | =item getppid |
4580 | 4537 | X<getppid> X<parent> X<pid> |
4581 | 4538 | |
4582 | 4539 | =begin original |
4583 | 4540 | |
4584 | 4541 | Returns the process id of the parent process. |
4585 | 4542 | |
4586 | 4543 | =end original |
4587 | 4544 | |
4588 | 4545 | 親プロセスのプロセス id を返します。 |
4589 | 4546 | |
4590 | 4547 | =begin original |
4591 | 4548 | |
4592 | 4549 | Note for Linux users: on Linux, the C functions C<getpid()> and |
4593 | 4550 | C<getppid()> return different values from different threads. In order to |
4594 | 4551 | be portable, this behavior is not reflected by the perl-level function |
4595 | 4552 | C<getppid()>, that returns a consistent value across threads. If you want |
4596 | 4553 | to call the underlying C<getppid()>, you may use the CPAN module |
4597 | 4554 | C<Linux::Pid>. |
4598 | 4555 | |
4599 | 4556 | =end original |
4600 | 4557 | |
4601 | 4558 | Linux ユーザーへの注意: Linux では C<getpid()> と C<getppid()> の C 関数は |
4602 | 4559 | スレッドが異なると異なった値を返します。 |
4603 | 4560 | 移植性のために、この振る舞いは perl レベルの関数 C<getppid()> には |
4604 | 4561 | 反映されず、スレッドをまたいで一貫性のある値を返します。 |
4605 | 4562 | 基礎となる C<getppid()> を呼び出したい場合は、CPAN モジュールである |
4606 | 4563 | C<Linux::Pid> を使ってください。 |
4607 | 4564 | |
4608 | 4565 | =item getpriority WHICH,WHO |
4609 | 4566 | X<getpriority> X<priority> X<nice> |
4610 | 4567 | |
4611 | 4568 | =begin original |
4612 | 4569 | |
4613 | 4570 | Returns the current priority for a process, a process group, or a user. |
4614 | (See | |
4571 | (See L<getpriority(2)>.) Will raise a fatal exception if used on a | |
4615 | 4572 | machine that doesn't implement getpriority(2). |
4616 | 4573 | |
4617 | 4574 | =end original |
4618 | 4575 | |
4619 | 4576 | プロセス、プロセスグループ、ユーザに対する現在の優先度を返します。 |
4620 | ( | |
4577 | (L<getpriority(2)> を参照してください。) | |
4621 | 4578 | getpriority(2) を実装していない |
4622 | 4579 | マシンで実行した場合には、致命的例外が発生します。 |
4623 | 4580 | |
4624 | 4581 | =item getpwnam NAME |
4625 | 4582 | X<getpwnam> X<getgrnam> X<gethostbyname> X<getnetbyname> X<getprotobyname> |
4626 | 4583 | X<getpwuid> X<getgrgid> X<getservbyname> X<gethostbyaddr> X<getnetbyaddr> |
4627 | 4584 | X<getprotobynumber> X<getservbyport> X<getpwent> X<getgrent> X<gethostent> |
4628 | 4585 | X<getnetent> X<getprotoent> X<getservent> X<setpwent> X<setgrent> X<sethostent> |
4629 | 4586 | X<setnetent> X<setprotoent> X<setservent> X<endpwent> X<endgrent> X<endhostent> |
4630 | 4587 | X<endnetent> X<endprotoent> X<endservent> |
4631 | 4588 | |
4632 | 4589 | =item getgrnam NAME |
4633 | 4590 | |
4634 | 4591 | =item gethostbyname NAME |
4635 | 4592 | |
4636 | 4593 | =item getnetbyname NAME |
4637 | 4594 | |
4638 | 4595 | =item getprotobyname NAME |
4639 | 4596 | |
4640 | 4597 | =item getpwuid UID |
4641 | 4598 | |
4642 | 4599 | =item getgrgid GID |
4643 | 4600 | |
4644 | 4601 | =item getservbyname NAME,PROTO |
4645 | 4602 | |
4646 | 4603 | =item gethostbyaddr ADDR,ADDRTYPE |
4647 | 4604 | |
4648 | 4605 | =item getnetbyaddr ADDR,ADDRTYPE |
4649 | 4606 | |
4650 | 4607 | =item getprotobynumber NUMBER |
4651 | 4608 | |
4652 | 4609 | =item getservbyport PORT,PROTO |
4653 | 4610 | |
4654 | 4611 | =item getpwent |
4655 | 4612 | |
4656 | 4613 | =item getgrent |
4657 | 4614 | |
4658 | 4615 | =item gethostent |
4659 | 4616 | |
4660 | 4617 | =item getnetent |
4661 | 4618 | |
4662 | 4619 | =item getprotoent |
4663 | 4620 | |
4664 | 4621 | =item getservent |
4665 | 4622 | |
4666 | 4623 | =item setpwent |
4667 | 4624 | |
4668 | 4625 | =item setgrent |
4669 | 4626 | |
4670 | 4627 | =item sethostent STAYOPEN |
4671 | 4628 | |
4672 | 4629 | =item setnetent STAYOPEN |
4673 | 4630 | |
4674 | 4631 | =item setprotoent STAYOPEN |
4675 | 4632 | |
4676 | 4633 | =item setservent STAYOPEN |
4677 | 4634 | |
4678 | 4635 | =item endpwent |
4679 | 4636 | |
4680 | 4637 | =item endgrent |
4681 | 4638 | |
4682 | 4639 | =item endhostent |
4683 | 4640 | |
4684 | 4641 | =item endnetent |
4685 | 4642 | |
4686 | 4643 | =item endprotoent |
4687 | 4644 | |
4688 | 4645 | =item endservent |
4689 | 4646 | |
4690 | 4647 | =begin original |
4691 | 4648 | |
4692 | 4649 | These routines perform the same functions as their counterparts in the |
4693 | 4650 | system library. In list context, the return values from the |
4694 | 4651 | various get routines are as follows: |
4695 | 4652 | |
4696 | 4653 | =end original |
4697 | 4654 | |
4698 | 4655 | これらのルーチンは、システムライブラリの同名の関数を実行します。 |
4699 | 4656 | リストコンテキストでは、さまざまな |
4700 | 4657 | get ルーチンからの返り値は、次のようになります: |
4701 | 4658 | |
4702 | 4659 | ($name,$passwd,$uid,$gid, |
4703 | 4660 | $quota,$comment,$gcos,$dir,$shell,$expire) = getpw* |
4704 | 4661 | ($name,$passwd,$gid,$members) = getgr* |
4705 | 4662 | ($name,$aliases,$addrtype,$length,@addrs) = gethost* |
4706 | 4663 | ($name,$aliases,$addrtype,$net) = getnet* |
4707 | 4664 | ($name,$aliases,$proto) = getproto* |
4708 | 4665 | ($name,$aliases,$port,$proto) = getserv* |
4709 | 4666 | |
4710 | 4667 | =begin original |
4711 | 4668 | |
4712 | 4669 | (If the entry doesn't exist you get a null list.) |
4713 | 4670 | |
4714 | 4671 | =end original |
4715 | 4672 | |
4716 | 4673 | (エントリが存在しなければ、空リストが返されます。) |
4717 | 4674 | |
4718 | 4675 | =begin original |
4719 | 4676 | |
4720 | 4677 | The exact meaning of the $gcos field varies but it usually contains |
4721 | 4678 | the real name of the user (as opposed to the login name) and other |
4722 | 4679 | information pertaining to the user. Beware, however, that in many |
4723 | 4680 | system users are able to change this information and therefore it |
4724 | 4681 | cannot be trusted and therefore the $gcos is tainted (see |
4725 | 4682 | L<perlsec>). The $passwd and $shell, user's encrypted password and |
4726 | 4683 | login shell, are also tainted, because of the same reason. |
4727 | 4684 | |
4728 | 4685 | =end original |
4729 | 4686 | |
4730 | 4687 | $gcos フィールドの正確な意味はさまざまですが、通常は(ログイン名ではなく) |
4731 | 4688 | ユーザーの実際の名前とユーザーに付随する情報を含みます。 |
4732 | 4689 | 但し、多くのシステムではユーザーがこの情報を変更できるので、この情報は |
4733 | 4690 | 信頼できず、従って $gcos は汚染されます(L<perlsec> を参照してください)。 |
4734 | 4691 | ユーザーの暗号化されたパスワードとログインシェルである $passwd と |
4735 | 4692 | $shell も、同様の理由で汚染されます。 |
4736 | 4693 | |
4737 | 4694 | =begin original |
4738 | 4695 | |
4739 | 4696 | In scalar context, you get the name, unless the function was a |
4740 | 4697 | lookup by name, in which case you get the other thing, whatever it is. |
4741 | 4698 | (If the entry doesn't exist you get the undefined value.) For example: |
4742 | 4699 | |
4743 | 4700 | =end original |
4744 | 4701 | |
4745 | 4702 | スカラコンテキストでは、*nam、*byname といった NAME で検索するもの以外は、 |
4746 | 4703 | name を返し、NAME で検索するものは、何か別のものを返します。 |
4747 | 4704 | (エントリが存在しなければ、未定義値が返ります。) |
4748 | 4705 | 例: |
4749 | 4706 | |
4750 | 4707 | $uid = getpwnam($name); |
4751 | 4708 | $name = getpwuid($num); |
4752 | 4709 | $name = getpwent(); |
4753 | 4710 | $gid = getgrnam($name); |
4754 | 4711 | $name = getgrgid($num); |
4755 | 4712 | $name = getgrent(); |
4756 | 4713 | #etc. |
4757 | 4714 | |
4758 | 4715 | =begin original |
4759 | 4716 | |
4760 | 4717 | In I<getpw*()> the fields $quota, $comment, and $expire are special |
4761 | 4718 | cases in the sense that in many systems they are unsupported. If the |
4762 | 4719 | $quota is unsupported, it is an empty scalar. If it is supported, it |
4763 | 4720 | usually encodes the disk quota. If the $comment field is unsupported, |
4764 | 4721 | it is an empty scalar. If it is supported it usually encodes some |
4765 | 4722 | administrative comment about the user. In some systems the $quota |
4766 | 4723 | field may be $change or $age, fields that have to do with password |
4767 | 4724 | aging. In some systems the $comment field may be $class. The $expire |
4768 | 4725 | field, if present, encodes the expiration period of the account or the |
4769 | 4726 | password. For the availability and the exact meaning of these fields |
4770 | 4727 | in your system, please consult your getpwnam(3) documentation and your |
4771 | 4728 | F<pwd.h> file. You can also find out from within Perl what your |
4772 | 4729 | $quota and $comment fields mean and whether you have the $expire field |
4773 | 4730 | by using the C<Config> module and the values C<d_pwquota>, C<d_pwage>, |
4774 | 4731 | C<d_pwchange>, C<d_pwcomment>, and C<d_pwexpire>. Shadow password |
4775 | 4732 | files are only supported if your vendor has implemented them in the |
4776 | 4733 | intuitive fashion that calling the regular C library routines gets the |
4777 | 4734 | shadow versions if you're running under privilege or if there exists |
4778 | 4735 | the shadow(3) functions as found in System V (this includes Solaris |
4779 | 4736 | and Linux.) Those systems that implement a proprietary shadow password |
4780 | 4737 | facility are unlikely to be supported. |
4781 | 4738 | |
4782 | 4739 | =end original |
4783 | 4740 | |
4784 | 4741 | I<getpw*()> では、$quota, $comment, $expire フィールドは、 |
4785 | 4742 | 多くのシステムでは対応していないので特別な処理がされます。 |
4786 | 4743 | $quota が非対応の場合、空のスカラになります。 |
4787 | 4744 | 対応している場合、通常はディスククォータの値が入ります。 |
4788 | 4745 | $comment フィールドが非対応の場合、空のスカラになります。 |
4789 | 4746 | 対応している場合、通常はユーザーに関する管理上のコメントが入ります。 |
4790 | 4747 | $quota フィールドはパスワードの寿命を示す $change や $age である |
4791 | 4748 | システムもあります。 |
4792 | 4749 | $comment フィールドは $class であるシステムもあります。 |
4793 | 4750 | $expire フィールドがある場合は、アカウントやパスワードが時間切れになる |
4794 | 4751 | 期間が入ります。 |
4795 | 4752 | 動作させるシステムでのこれらのフィールドの有効性と正確な意味については、 |
4796 | 4753 | getpwnam(3) のドキュメントと F<pwd.h> ファイルを参照してください。 |
4797 | 4754 | $quota と $comment フィールドが何を意味しているかと、$expire フィールドが |
4798 | 4755 | あるかどうかは、C<Config> モジュールを使って、C<d_pwquota>, C<d_pwage>, |
4799 | 4756 | C<d_pwchange>, C<d_pwcomment>, C<d_pwexpire> の値を調べることによって |
4800 | 4757 | Perl 自身で調べることも出来ます。 |
4801 | 4758 | シャドウパスワードは、通常の C ライブラリルーチンを権限がある状態で |
4802 | 4759 | 呼び出すことでシャドウ版が取得できるか、System V にあるような |
4803 | 4760 | (Solaris と Linux を含みます) shadow(3) 関数があるといった、 |
4804 | 4761 | 直感的な方法で実装されている場合にのみ対応されます。 |
4805 | 4762 | 独占的なシャドウパスワード機能を実装しているシステムでは、 |
4806 | 4763 | それに対応されることはないでしょう。 |
4807 | 4764 | |
4808 | 4765 | =begin original |
4809 | 4766 | |
4810 | 4767 | The $members value returned by I<getgr*()> is a space separated list of |
4811 | 4768 | the login names of the members of the group. |
4812 | 4769 | |
4813 | 4770 | =end original |
4814 | 4771 | |
4815 | 4772 | I<getgr*()> によって返る値 $members は、グループのメンバの |
4816 | 4773 | ログイン名をスペースで区切ったものです。 |
4817 | 4774 | |
4818 | 4775 | =begin original |
4819 | 4776 | |
4820 | 4777 | For the I<gethost*()> functions, if the C<h_errno> variable is supported in |
4821 | 4778 | C, it will be returned to you via C<$?> if the function call fails. The |
4822 | 4779 | C<@addrs> value returned by a successful call is a list of the raw |
4823 | 4780 | addresses returned by the corresponding system library call. In the |
4824 | 4781 | Internet domain, each address is four bytes long and you can unpack it |
4825 | 4782 | by saying something like: |
4826 | 4783 | |
4827 | 4784 | =end original |
4828 | 4785 | |
4829 | 4786 | I<gethost*()> 関数では、C で C<h_errno> 変数がサポートされていれば、 |
4830 | 4787 | 関数呼出が失敗したときに、C<$?> を通して、その値が返されます。 |
4831 | 4788 | 成功時に返される C<@addrs> 値は、対応するシステムコールが返す、 |
4832 | 4789 | 生のアドレスのリストです。 |
4833 | 4790 | インターネットドメインでは、個々のアドレスは、4 バイト長で、 |
4834 | 4791 | 以下のようにして unpack することができます。 |
4835 | 4792 | |
4836 | 4793 | ($a,$b,$c,$d) = unpack('W4',$addr[0]); |
4837 | 4794 | |
4838 | 4795 | =begin original |
4839 | 4796 | |
4840 | 4797 | The Socket library makes this slightly easier: |
4841 | 4798 | |
4842 | 4799 | =end original |
4843 | 4800 | |
4844 | 4801 | Socket ライブラリを使うともう少し簡単になります。 |
4845 | 4802 | |
4846 | 4803 | use Socket; |
4847 | 4804 | $iaddr = inet_aton("127.1"); # or whatever address |
4848 | 4805 | $name = gethostbyaddr($iaddr, AF_INET); |
4849 | 4806 | |
4850 | 4807 | # or going the other way |
4851 | 4808 | $straddr = inet_ntoa($iaddr); |
4852 | 4809 | |
4853 | 4810 | =begin original |
4854 | 4811 | |
4855 | 4812 | In the opposite way, to resolve a hostname to the IP address |
4856 | 4813 | you can write this: |
4857 | 4814 | |
4858 | 4815 | =end original |
4859 | 4816 | |
4860 | 4817 | 逆方向に、ホスト名から IP アドレスを解決するには以下のように書けます: |
4861 | 4818 | |
4862 | 4819 | use Socket; |
4863 | 4820 | $packed_ip = gethostbyname("www.perl.org"); |
4864 | 4821 | if (defined $packed_ip) { |
4865 | 4822 | $ip_address = inet_ntoa($packed_ip); |
4866 | 4823 | } |
4867 | 4824 | |
4868 | 4825 | =begin original |
4869 | 4826 | |
4870 | 4827 | Make sure <gethostbyname()> is called in SCALAR context and that |
4871 | 4828 | its return value is checked for definedness. |
4872 | 4829 | |
4873 | 4830 | =end original |
4874 | 4831 | |
4875 | 4832 | C<gethostbyname()> はスカラコンテキストで呼び出すようにして、返り値が |
4876 | 4833 | 定義されているかを必ずチェックしてください。 |
4877 | 4834 | |
4878 | 4835 | =begin original |
4879 | 4836 | |
4880 | 4837 | If you get tired of remembering which element of the return list |
4881 | 4838 | contains which return value, by-name interfaces are provided |
4882 | 4839 | in standard modules: C<File::stat>, C<Net::hostent>, C<Net::netent>, |
4883 | 4840 | C<Net::protoent>, C<Net::servent>, C<Time::gmtime>, C<Time::localtime>, |
4884 | 4841 | and C<User::grent>. These override the normal built-ins, supplying |
4885 | 4842 | versions that return objects with the appropriate names |
4886 | 4843 | for each field. For example: |
4887 | 4844 | |
4888 | 4845 | =end original |
4889 | 4846 | |
4890 | 4847 | 返り値のリストの何番目がどの要素かを覚えるのに疲れたなら、 |
4891 | 4848 | 名前ベースのインターフェースが標準モジュールで提供されています: |
4892 | 4849 | C<File::stat>, C<Net::hostent>, C<Net::netent>, |
4893 | 4850 | C<Net::protoent>, C<Net::servent>, C<Time::gmtime>, C<Time::localtime>, |
4894 | 4851 | C<User::grent> です。 |
4895 | 4852 | これらは通常の組み込みを上書きし、 |
4896 | 4853 | それぞれのフィールドに適切な名前をつけたオブジェクトを返します。 |
4897 | 4854 | 例: |
4898 | 4855 | |
4899 | 4856 | use File::stat; |
4900 | 4857 | use User::pwent; |
4901 | 4858 | $is_his = (stat($filename)->uid == pwent($whoever)->uid); |
4902 | 4859 | |
4903 | 4860 | =begin original |
4904 | 4861 | |
4905 | 4862 | Even though it looks like they're the same method calls (uid), |
4906 | 4863 | they aren't, because a C<File::stat> object is different from |
4907 | 4864 | a C<User::pwent> object. |
4908 | 4865 | |
4909 | 4866 | =end original |
4910 | 4867 | |
4911 | 4868 | 同じメソッド(uid)を呼び出しているように見えますが、違います。 |
4912 | 4869 | なぜなら C<File::stat> オブジェクトは C<User::pwent> オブジェクトとは |
4913 | 4870 | 異なるからです。 |
4914 | 4871 | |
4915 | 4872 | =item getsockname SOCKET |
4916 | 4873 | X<getsockname> |
4917 | 4874 | |
4918 | 4875 | =begin original |
4919 | 4876 | |
4920 | 4877 | Returns the packed sockaddr address of this end of the SOCKET connection, |
4921 | 4878 | in case you don't know the address because you have several different |
4922 | 4879 | IPs that the connection might have come in on. |
4923 | 4880 | |
4924 | 4881 | =end original |
4925 | 4882 | |
4926 | 4883 | SOCKET 接続のこちら側の pack された sockaddr アドレスを返します。 |
4927 | 4884 | 複数の異なる IP から接続されるためにアドレスがわからない場合に使います。 |
4928 | 4885 | |
4929 | 4886 | use Socket; |
4930 | 4887 | $mysockaddr = getsockname(SOCK); |
4931 | 4888 | ($port, $myaddr) = sockaddr_in($mysockaddr); |
4932 | 4889 | printf "Connect to %s [%s]\n", |
4933 | 4890 | scalar gethostbyaddr($myaddr, AF_INET), |
4934 | 4891 | inet_ntoa($myaddr); |
4935 | 4892 | |
4936 | 4893 | =item getsockopt SOCKET,LEVEL,OPTNAME |
4937 | 4894 | X<getsockopt> |
4938 | 4895 | |
4939 | 4896 | =begin original |
4940 | 4897 | |
4941 | 4898 | Queries the option named OPTNAME associated with SOCKET at a given LEVEL. |
4942 | 4899 | Options may exist at multiple protocol levels depending on the socket |
4943 | 4900 | type, but at least the uppermost socket level SOL_SOCKET (defined in the |
4944 | 4901 | C<Socket> module) will exist. To query options at another level the |
4945 | 4902 | protocol number of the appropriate protocol controlling the option |
4946 | 4903 | should be supplied. For example, to indicate that an option is to be |
4947 | 4904 | interpreted by the TCP protocol, LEVEL should be set to the protocol |
4948 | 4905 | number of TCP, which you can get using getprotobyname. |
4949 | 4906 | |
4950 | 4907 | =end original |
4951 | 4908 | |
4952 | 4909 | 与えられた LEVEL で SOCKET に関連付けられた OPTNAME と言う名前のオプションを |
4953 | 4910 | 問い合わせます。 |
4954 | 4911 | オプションはソケットの種類に依存しした複数のプロトコルレベルに存在することも |
4955 | 4912 | ありますが、少なくとも最上位ソケットレベル SOL_SOCKET (C<Socket> モジュールで |
4956 | 4913 | 定義されています)は存在します。 |
4957 | 4914 | その他のレベルのオプションを問い合わせるには、そのオプションを制御する |
4958 | 4915 | 適切なプロトコルのプロトコル番号を指定します。 |
4959 | 4916 | 例えば、オプションが TCP プロトコルで解釈されるべきであることを示すためには、 |
4960 | 4917 | LEVEL は getprotobyname で得られる TCP のプロトコル番号を設定します。 |
4961 | 4918 | |
4962 | 4919 | =begin original |
4963 | 4920 | |
4964 | 4921 | The call returns a packed string representing the requested socket option, |
4965 | 4922 | or C<undef> if there is an error (the error reason will be in $!). What |
4966 | 4923 | exactly is in the packed string depends in the LEVEL and OPTNAME, consult |
4967 | 4924 | your system documentation for details. A very common case however is that |
4968 | 4925 | the option is an integer, in which case the result will be a packed |
4969 | 4926 | integer which you can decode using unpack with the C<i> (or C<I>) format. |
4970 | 4927 | |
4971 | 4928 | =end original |
4972 | 4929 | |
4973 | 4930 | この呼び出しは、要求されたソケットオプションの pack された文字列表現か、 |
4974 | 4931 | あるいはエラーがある場合は C<undef> を返します(エラーの理由は $! にあります)。 |
4975 | 4932 | pack された文字列の正確な中身は LEVEL と OPTNAME に依存するので、 |
4976 | 4933 | 詳細についてはシステムドキュメントを確認してください。 |
4977 | 4934 | しかし、とても一般的な場合というのはオプションが整数の場合で、この場合 |
4978 | 4935 | 結果は unpack の C<i> (あるいは C<I>)フォーマットでデコードできる pack された |
4979 | 4936 | 整数です。 |
4980 | 4937 | |
4981 | 4938 | =begin original |
4982 | 4939 | |
4983 | 4940 | An example testing if Nagle's algorithm is turned on on a socket: |
4984 | 4941 | |
4985 | 4942 | =end original |
4986 | 4943 | |
4987 | 4944 | あるソケットで Nagle のアルゴリズム有効かどうかを調べる例です: |
4988 | 4945 | |
4989 | 4946 | use Socket qw(:all); |
4990 | 4947 | |
4991 | 4948 | defined(my $tcp = getprotobyname("tcp")) |
4992 | 4949 | or die "Could not determine the protocol number for tcp"; |
4993 | 4950 | # my $tcp = IPPROTO_TCP; # Alternative |
4994 | 4951 | my $packed = getsockopt($socket, $tcp, TCP_NODELAY) |
4995 | 4952 | or die "Could not query TCP_NODELAY socket option: $!"; |
4996 | 4953 | my $nodelay = unpack("I", $packed); |
4997 | 4954 | print "Nagle's algorithm is turned ", $nodelay ? "off\n" : "on\n"; |
4998 | 4955 | |
4999 | 4956 | |
5000 | 4957 | =item glob EXPR |
5001 | 4958 | X<glob> X<wildcard> X<filename, expansion> X<expand> |
5002 | 4959 | |
5003 | 4960 | =item glob |
5004 | 4961 | |
5005 | 4962 | =begin original |
5006 | 4963 | |
5007 | 4964 | In list context, returns a (possibly empty) list of filename expansions on |
5008 | 4965 | the value of EXPR such as the standard Unix shell F</bin/csh> would do. In |
5009 | 4966 | scalar context, glob iterates through such filename expansions, returning |
5010 | 4967 | undef when the list is exhausted. This is the internal function |
5011 | 4968 | implementing the C<< <*.c> >> operator, but you can use it directly. If |
5012 | 4969 | EXPR is omitted, C<$_> is used. The C<< <*.c> >> operator is discussed in |
5013 | 4970 | more detail in L<perlop/"I/O Operators">. |
5014 | 4971 | |
5015 | 4972 | =end original |
5016 | 4973 | |
5017 | 4974 | リストコンテキストでは、 |
5018 | 4975 | EXPR の値を、標準 Unix シェル F</bin/csh> が行なうように |
5019 | 4976 | ファイル名の展開を行なった結果のリスト(空かもしれません)を返します。 |
5020 | 4977 | スカラコンテキストでは、glob はこのようなファイル名展開を繰り返し、 |
5021 | 4978 | リストがなくなったら undef を返します。 |
5022 | 4979 | これは、C<< <*.c> >> 演算子を実装する内部関数ですが、 |
5023 | 4980 | 直接使用することもできます。 |
5024 | 4981 | EXPR を省略すると、C<$_>が使われます。 |
5025 | 4982 | C<< <*.c> >>演算子については |
5026 | 4983 | L<perlop/"I/O Operators"> でより詳細に議論しています。 |
5027 | 4984 | |
5028 | 4985 | =begin original |
5029 | 4986 | |
5030 | Note that C<glob> will split its arguments on whitespace, treating | |
5031 | each segment as separate pattern. As such, C<glob('*.c *.h')> would | |
5032 | match all files with a F<.c> or F<.h> extension. The expression | |
5033 | C<glob('.* *')> would match all files in the current working directory. | |
5034 | ||
5035 | =end original | |
5036 | ||
5037 | Note that | |
5038 | C<glob> は引数を空白で分割して、それぞれの部分を別々のパターンとして | |
5039 | 扱うことに注意してください。 | |
5040 | それにより、C<glob('*.c *.h')> は F<.c> と F<.h> の拡張子を持つ全ての | |
5041 | ファイルにマッチングします。 | |
5042 | C<glob('.* *')> という式はカレントワーキングディレクトリの | |
5043 | 全てのファイルにマッチングします。 | |
5044 | ||
5045 | =begin original | |
5046 | ||
5047 | 4987 | Beginning with v5.6.0, this operator is implemented using the standard |
5048 | C<File::Glob> extension. See L<File::Glob> for details | |
4988 | C<File::Glob> extension. See L<File::Glob> for details. | |
5049 | C<bsd_glob> which does not treat whitespace as a pattern separator. | |
5050 | 4989 | |
5051 | 4990 | =end original |
5052 | 4991 | |
5053 | 4992 | v5.6.0 から、この演算子は標準の C<File::Glob> 拡張を使って |
5054 | 4993 | 実装されています。 |
5055 | 空白をパターンのセパレータとして扱わない C<bsd_glob> を含めた | |
5056 | 4994 | 詳細は L<File::Glob> を参照して下さい。 |
5057 | 4995 | |
5058 | 4996 | =item gmtime EXPR |
5059 | 4997 | X<gmtime> X<UTC> X<Greenwich> |
5060 | 4998 | |
5061 | 4999 | =item gmtime |
5062 | 5000 | |
5063 | 5001 | =begin original |
5064 | 5002 | |
5065 | 5003 | Works just like L<localtime> but the returned values are |
5066 | 5004 | localized for the standard Greenwich time zone. |
5067 | 5005 | |
5068 | 5006 | =end original |
5069 | 5007 | |
5070 | 5008 | L<localtime> と同様に働きますが、返り値はグリニッジ標準時に |
5071 | 5009 | ローカライズされています。 |
5072 | 5010 | |
5073 | 5011 | =begin original |
5074 | 5012 | |
5075 | 5013 | Note: when called in list context, $isdst, the last value |
5076 | 5014 | returned by gmtime is always C<0>. There is no |
5077 | 5015 | Daylight Saving Time in GMT. |
5078 | 5016 | |
5079 | 5017 | =end original |
5080 | 5018 | |
5081 | 5019 | 注意: リストコンテキストで呼び出した時、gmtime が返す末尾の値である |
5082 | 5020 | $isdst は常に C<0> です。 |
5083 | 5021 | GMT には夏時間はありません。 |
5084 | 5022 | |
5085 | 5023 | =begin original |
5086 | 5024 | |
5087 | 5025 | See L<perlport/gmtime> for portability concerns. |
5088 | 5026 | |
5089 | 5027 | =end original |
5090 | 5028 | |
5091 | 5029 | 移植性の問題については L<perlport/gmtime> を参照してください。 |
5092 | 5030 | |
5093 | 5031 | =item goto LABEL |
5094 | 5032 | X<goto> X<jump> X<jmp> |
5095 | 5033 | |
5096 | 5034 | =item goto EXPR |
5097 | 5035 | |
5098 | 5036 | =item goto &NAME |
5099 | 5037 | |
5100 | 5038 | =begin original |
5101 | 5039 | |
5102 | 5040 | The C<goto-LABEL> form finds the statement labeled with LABEL and resumes |
5103 | 5041 | execution there. It may not be used to go into any construct that |
5104 | 5042 | requires initialization, such as a subroutine or a C<foreach> loop. It |
5105 | 5043 | also can't be used to go into a construct that is optimized away, |
5106 | 5044 | or to get out of a block or subroutine given to C<sort>. |
5107 | 5045 | It can be used to go almost anywhere else within the dynamic scope, |
5108 | 5046 | including out of subroutines, but it's usually better to use some other |
5109 | 5047 | construct such as C<last> or C<die>. The author of Perl has never felt the |
5110 | 5048 | need to use this form of C<goto> (in Perl, that is--C is another matter). |
5111 | 5049 | (The difference being that C does not offer named loops combined with |
5112 | 5050 | loop control. Perl does, and this replaces most structured uses of C<goto> |
5113 | 5051 | in other languages.) |
5114 | 5052 | |
5115 | 5053 | =end original |
5116 | 5054 | |
5117 | 5055 | C<goto-LABEL> の形式は、LABEL というラベルの付いた文を |
5118 | 5056 | 探して、そこへ実行を移すものです。 サブルーチンや |
5119 | 5057 | C<foreach> ループなど、何らかの初期化が必要な構造の中に |
5120 | 5058 | 入り込むことは許されません。 最適化によってなくなってしまう構造の中にも |
5121 | 5059 | goto することはできません。 |
5122 | 5060 | また、C<sort>で与えられたブロックやサブルーチンから外へ出ることもできません。 |
5123 | 5061 | これ以外は、サブルーチンの外を含む、動的スコープ内の |
5124 | 5062 | ほとんどすべての場所へ行くために使用できますが、普通は、 |
5125 | 5063 | C<last> や C<die> といった別の構造を使った方が良いでしょう。 |
5126 | 5064 | Perl の作者はこの形式の C<goto> を使う必要を感じたことは、 |
5127 | 5065 | 1 度もありません (Perl では。C は別のお話です)。 |
5128 | 5066 | (違いは、C にはループ制御と結びついた名前つきのループがないことです。 |
5129 | 5067 | Perl にはあり、これが他の言語でのほとんどの構造的な C<goto> の使用法を |
5130 | 5068 | 置き換えます。) |
5131 | 5069 | |
5132 | 5070 | =begin original |
5133 | 5071 | |
5134 | 5072 | The C<goto-EXPR> form expects a label name, whose scope will be resolved |
5135 | 5073 | dynamically. This allows for computed C<goto>s per FORTRAN, but isn't |
5136 | 5074 | necessarily recommended if you're optimizing for maintainability: |
5137 | 5075 | |
5138 | 5076 | =end original |
5139 | 5077 | |
5140 | 5078 | C<goto-EXPR> の形式はラベル名を予測し、このスコープは動的に解決されます。 |
5141 | 5079 | これにより FORTRAN のような算術 C<goto> が可能になりますが、 |
5142 | 5080 | 保守性を重視するならお勧めしません。 |
5143 | 5081 | |
5144 | 5082 | goto ("FOO", "BAR", "GLARCH")[$i]; |
5145 | 5083 | |
5146 | 5084 | =begin original |
5147 | 5085 | |
5148 | 5086 | The C<goto-&NAME> form is quite different from the other forms of |
5149 | 5087 | C<goto>. In fact, it isn't a goto in the normal sense at all, and |
5150 | 5088 | doesn't have the stigma associated with other gotos. Instead, it |
5151 | 5089 | exits the current subroutine (losing any changes set by local()) and |
5152 | 5090 | immediately calls in its place the named subroutine using the current |
5153 | 5091 | value of @_. This is used by C<AUTOLOAD> subroutines that wish to |
5154 | 5092 | load another subroutine and then pretend that the other subroutine had |
5155 | 5093 | been called in the first place (except that any modifications to C<@_> |
5156 | 5094 | in the current subroutine are propagated to the other subroutine.) |
5157 | 5095 | After the C<goto>, not even C<caller> will be able to tell that this |
5158 | 5096 | routine was called first. |
5159 | 5097 | |
5160 | 5098 | =end original |
5161 | 5099 | |
5162 | 5100 | C<goto-&NAME> の形式は、その他の C<goto> の形式とはかなり |
5163 | 5101 | 異なったものです。 |
5164 | 5102 | 実際、これは普通の感覚でいうところのどこかへ行くものでは全くなく、 |
5165 | 5103 | 他の goto が持つ不名誉を持っていません。 |
5166 | 5104 | 現在のサブルーチンを終了し (local() による変更は失われます)、 |
5167 | 5105 | 直ちに現在の @_ の値を使って指定された名前のサブルーチンを呼び出します。 |
5168 | 5106 | これは、C<AUTOLOAD> サブルーチンが別のサブルーチンをロードして、 |
5169 | 5107 | その別のサブルーチンが最初に呼ばれたようにするために使われます |
5170 | 5108 | (ただし、現在のサブルーチンで C<@_> を修正した場合には、 |
5171 | 5109 | その別のサブルーチンに伝えられます)。 |
5172 | 5110 | C<goto> のあとは、C<caller> でさえも、現在のサブルーチンが |
5173 | 5111 | 最初に呼び出されたと言うことができません。 |
5174 | 5112 | |
5175 | 5113 | =begin original |
5176 | 5114 | |
5177 | 5115 | NAME needn't be the name of a subroutine; it can be a scalar variable |
5178 | 5116 | containing a code reference, or a block that evaluates to a code |
5179 | 5117 | reference. |
5180 | 5118 | |
5181 | 5119 | =end original |
5182 | 5120 | |
5183 | 5121 | NAME はサブルーチンの名前である必要はありません; コードリファレンスを |
5184 | 5122 | 含むスカラ値や、コードリファレンスと評価されるブロックでも構いません。 |
5185 | 5123 | |
5186 | 5124 | =item grep BLOCK LIST |
5187 | 5125 | X<grep> |
5188 | 5126 | |
5189 | 5127 | =item grep EXPR,LIST |
5190 | 5128 | |
5191 | 5129 | =begin original |
5192 | 5130 | |
5193 | 5131 | This is similar in spirit to, but not the same as, grep(1) and its |
5194 | 5132 | relatives. In particular, it is not limited to using regular expressions. |
5195 | 5133 | |
5196 | 5134 | =end original |
5197 | 5135 | |
5198 | 5136 | これは grep(1) とその親類と同じようなものですが、同じではありません。 |
5199 | 5137 | 特に、正規表現の使用に制限されません。 |
5200 | 5138 | |
5201 | 5139 | =begin original |
5202 | 5140 | |
5203 | 5141 | Evaluates the BLOCK or EXPR for each element of LIST (locally setting |
5204 | 5142 | C<$_> to each element) and returns the list value consisting of those |
5205 | 5143 | elements for which the expression evaluated to true. In scalar |
5206 | 5144 | context, returns the number of times the expression was true. |
5207 | 5145 | |
5208 | 5146 | =end original |
5209 | 5147 | |
5210 | 5148 | LIST の個々の要素に対して、BLOCK か EXPR を評価し |
5211 | 5149 | (C<$_> は、ローカルに個々の要素が設定されます) 、 |
5212 | 5150 | その要素のうち、評価した式が真となったものからなるリスト値が返されます。 |
5213 | 5151 | スカラコンテキストでは、式が真となった回数を返します。 例: |
5214 | 5152 | |
5215 | 5153 | @foo = grep(!/^#/, @bar); # weed out comments |
5216 | 5154 | |
5217 | 5155 | =begin original |
5218 | 5156 | |
5219 | 5157 | or equivalently, |
5220 | 5158 | |
5221 | 5159 | =end original |
5222 | 5160 | |
5223 | 5161 | あるいは等価な例として: |
5224 | 5162 | |
5225 | 5163 | @foo = grep {!/^#/} @bar; # weed out comments |
5226 | 5164 | |
5227 | 5165 | =begin original |
5228 | 5166 | |
5229 | 5167 | Note that C<$_> is an alias to the list value, so it can be used to |
5230 | 5168 | modify the elements of the LIST. While this is useful and supported, |
5231 | 5169 | it can cause bizarre results if the elements of LIST are not variables. |
5232 | 5170 | Similarly, grep returns aliases into the original list, much as a for |
5233 | 5171 | loop's index variable aliases the list elements. That is, modifying an |
5234 | 5172 | element of a list returned by grep (for example, in a C<foreach>, C<map> |
5235 | 5173 | or another C<grep>) actually modifies the element in the original list. |
5236 | 5174 | This is usually something to be avoided when writing clear code. |
5237 | 5175 | |
5238 | 5176 | =end original |
5239 | 5177 | |
5240 | 5178 | C<$_> は、LIST の値へのエイリアスですので、LIST の要素を |
5241 | 5179 | 変更するために使うことができます。 |
5242 | 5180 | これは、便利でサポートされていますが、 |
5243 | 5181 | LIST の要素が変数でないと、おかしな結果になります。 |
5244 | 5182 | 同様に、grep は元のリストへのエイリアスを返します。 |
5245 | 5183 | for ループのインデックス変数がリスト要素のエイリアスであるのと |
5246 | 5184 | 同様です。 |
5247 | 5185 | つまり、grep で返されたリストの要素を |
5248 | 5186 | (C<foreach>, C<map>, または他の C<grep> で)修正すると |
5249 | 5187 | 元のリストの要素が変更されます。 |
5250 | 5188 | これはきれいなコードを書こうとする邪魔になることが多いです。 |
5251 | 5189 | |
5252 | 5190 | =begin original |
5253 | 5191 | |
5254 | 5192 | If C<$_> is lexical in the scope where the C<grep> appears (because it has |
5255 | 5193 | been declared with C<my $_>) then, in addition to being locally aliased to |
5256 | 5194 | the list elements, C<$_> keeps being lexical inside the block; i.e. it |
5257 | 5195 | can't be seen from the outside, avoiding any potential side-effects. |
5258 | 5196 | |
5259 | 5197 | =end original |
5260 | 5198 | |
5261 | 5199 | (C<my $_> として宣言されることによって) C<$_> が C<grep> が現れるスコープ内で |
5262 | 5200 | レキシカルな場合は、ローカルではリスト要素へのエイリアスであることに加えて、 |
5263 | 5201 | C<$_> はブロック内でレキシカルでありつづけます; つまり、外側からは見えず、 |
5264 | 5202 | 起こりうる副作用を回避します。 |
5265 | 5203 | |
5266 | 5204 | =begin original |
5267 | 5205 | |
5268 | 5206 | See also L</map> for a list composed of the results of the BLOCK or EXPR. |
5269 | 5207 | |
5270 | 5208 | =end original |
5271 | 5209 | |
5272 | 5210 | BLOCK や EXPR の結果をリストの形にしたい場合は L</map> を参照してください。 |
5273 | 5211 | |
5274 | 5212 | =item hex EXPR |
5275 | 5213 | X<hex> X<hexadecimal> |
5276 | 5214 | |
5277 | 5215 | =item hex |
5278 | 5216 | |
5279 | 5217 | =begin original |
5280 | 5218 | |
5281 | 5219 | Interprets EXPR as a hex string and returns the corresponding value. |
5282 | 5220 | (To convert strings that might start with either C<0>, C<0x>, or C<0b>, see |
5283 | 5221 | L</oct>.) If EXPR is omitted, uses C<$_>. |
5284 | 5222 | |
5285 | 5223 | =end original |
5286 | 5224 | |
5287 | 5225 | EXPR を 16 進数の文字列と解釈して、対応する値を返します。 |
5288 | 5226 | (C<0>, C<0x>, C<0b> で始まる文字列の変換には、L</oct> を |
5289 | 5227 | 参照してください。) |
5290 | 5228 | EXPR が省略されると、C<$_> を使用します。 |
5291 | 5229 | |
5292 | 5230 | print hex '0xAf'; # prints '175' |
5293 | 5231 | print hex 'aF'; # same |
5294 | 5232 | |
5295 | 5233 | =begin original |
5296 | 5234 | |
5297 | 5235 | Hex strings may only represent integers. Strings that would cause |
5298 | 5236 | integer overflow trigger a warning. Leading whitespace is not stripped, |
5299 | 5237 | unlike oct(). To present something as hex, look into L</printf>, |
5300 | 5238 | L</sprintf>, or L</unpack>. |
5301 | 5239 | |
5302 | 5240 | =end original |
5303 | 5241 | |
5304 | 5242 | 16 進文字列は整数のみを表現します。 |
5305 | 5243 | 整数オーバーフローを起こすような文字列は警告を引き起こします。 |
5306 | 5244 | oct() とは違って、先頭の空白は除去されません。 |
5307 | 5245 | 何かを 16 進で表現したい場合は、L</printf>, L</sprintf>, L</unpack> を |
5308 | 5246 | 参照してください。 |
5309 | 5247 | |
5310 | 5248 | =item import LIST |
5311 | 5249 | X<import> |
5312 | 5250 | |
5313 | 5251 | =begin original |
5314 | 5252 | |
5315 | 5253 | There is no builtin C<import> function. It is just an ordinary |
5316 | 5254 | method (subroutine) defined (or inherited) by modules that wish to export |
5317 | 5255 | names to another module. The C<use> function calls the C<import> method |
5318 | 5256 | for the package used. See also L</use>, L<perlmod>, and L<Exporter>. |
5319 | 5257 | |
5320 | 5258 | =end original |
5321 | 5259 | |
5322 | 5260 | 組み込みの C<import> 関数というものはありません。 |
5323 | 5261 | これは単に、別のモジュールに名前をエクスポートしたいモジュールが |
5324 | 5262 | 定義した(または継承した)、通常のメソッド(サブルーチン)です。 |
5325 | 5263 | C<use> 関数はパッケージを使う時に C<import> メソッドを呼び出します。 |
5326 | 5264 | L</use>, L<perlmod>, L<Exporter> も参照してください。 |
5327 | 5265 | |
5328 | 5266 | =item index STR,SUBSTR,POSITION |
5329 | 5267 | X<index> X<indexOf> X<InStr> |
5330 | 5268 | |
5331 | 5269 | =item index STR,SUBSTR |
5332 | 5270 | |
5333 | 5271 | =begin original |
5334 | 5272 | |
5335 | 5273 | The index function searches for one string within another, but without |
5336 | 5274 | the wildcard-like behavior of a full regular-expression pattern match. |
5337 | 5275 | It returns the position of the first occurrence of SUBSTR in STR at |
5338 | 5276 | or after POSITION. If POSITION is omitted, starts searching from the |
5339 | 5277 | beginning of the string. POSITION before the beginning of the string |
5340 | 5278 | or after its end is treated as if it were the beginning or the end, |
5341 | 5279 | respectively. POSITION and the return value are based at C<0> (or whatever |
5342 | 5280 | you've set the C<$[> variable to--but don't do that). If the substring |
5343 | 5281 | is not found, C<index> returns one less than the base, ordinarily C<-1>. |
5344 | 5282 | |
5345 | 5283 | =end original |
5346 | 5284 | |
5347 | 5285 | index 関数は ある文字列をもうひとつの文字列から検索しますが、 |
5348 | 5286 | 完全正規表現パターンマッチのワイルドカード的な振る舞いはしません。 |
5349 | 5287 | STR の中の POSITION の位置以降で、最初に SUBSTR が見つかった位置を返します。 |
5350 | 5288 | POSITION が省略された場合には、STR の最初から探し始めます。 |
5351 | 5289 | POSITION が文字列の先頭より前、あるいは末尾より後ろを指定した場合は、 |
5352 | 5290 | それぞれ先頭と末尾を指定されたものとして扱われます。 |
5353 | 5291 | POSITION と返り値のベースは、C<0> (もしくは、変数 C<$[> に設定した値です -- |
5354 | 5292 | しかし、これは使ってはいけません)。 |
5355 | 5293 | SUBSTR が見つからなかった場合には、C<index> はベースよりも 1 小さい値、 |
5356 | 5294 | 通常は C<-1> が返されます。 |
5357 | 5295 | |
5358 | 5296 | =item int EXPR |
5359 | 5297 | X<int> X<integer> X<truncate> X<trunc> X<floor> |
5360 | 5298 | |
5361 | 5299 | =item int |
5362 | 5300 | |
5363 | 5301 | =begin original |
5364 | 5302 | |
5365 | 5303 | Returns the integer portion of EXPR. If EXPR is omitted, uses C<$_>. |
5366 | 5304 | You should not use this function for rounding: one because it truncates |
5367 | 5305 | towards C<0>, and two because machine representations of floating point |
5368 | 5306 | numbers can sometimes produce counterintuitive results. For example, |
5369 | 5307 | C<int(-6.725/0.025)> produces -268 rather than the correct -269; that's |
5370 | 5308 | because it's really more like -268.99999999999994315658 instead. Usually, |
5371 | 5309 | the C<sprintf>, C<printf>, or the C<POSIX::floor> and C<POSIX::ceil> |
5372 | 5310 | functions will serve you better than will int(). |
5373 | 5311 | |
5374 | 5312 | =end original |
5375 | 5313 | |
5376 | 5314 | EXPR の整数部を返します。 |
5377 | 5315 | EXPR を省略すると、C<$_> を使います。 |
5378 | 5316 | この関数を丸めのために使うべきではありません。 |
5379 | 5317 | 第一の理由として C<0> の方向への切捨てを行うから、第二の理由として |
5380 | 5318 | 浮動小数点数の機械表現は時々直感に反した結果を生み出すからです。 |
5381 | 5319 | たとえば、C<int(-6.725/0.025)> は正しい結果である -269 ではなく |
5382 | 5320 | -268 を返します。 |
5383 | 5321 | これは実際には -268.99999999999994315658 というような値になっているからです。 |
5384 | 5322 | 通常、C<sprintf>, C<printf>, C<POSIX::floor>, C<POSIX::ceil> の方が |
5385 | 5323 | int() より便利です。 |
5386 | 5324 | |
5387 | 5325 | =item ioctl FILEHANDLE,FUNCTION,SCALAR |
5388 | 5326 | X<ioctl> |
5389 | 5327 | |
5390 | 5328 | =begin original |
5391 | 5329 | |
5392 | 5330 | Implements the ioctl(2) function. You'll probably first have to say |
5393 | 5331 | |
5394 | 5332 | =end original |
5395 | 5333 | |
5396 | 5334 | ioctl(2) 関数を実装します。 |
5397 | 5335 | 正しい関数の定義を得るために、おそらく最初に |
5398 | 5336 | |
5399 | 5337 | require "sys/ioctl.ph"; # probably in $Config{archlib}/sys/ioctl.ph |
5400 | 5338 | |
5401 | 5339 | =begin original |
5402 | 5340 | |
5403 | 5341 | to get the correct function definitions. If F<sys/ioctl.ph> doesn't |
5404 | 5342 | exist or doesn't have the correct definitions you'll have to roll your |
5405 | 5343 | own, based on your C header files such as F<< <sys/ioctl.h> >>. |
5406 | 5344 | (There is a Perl script called B<h2ph> that comes with the Perl kit that |
5407 | 5345 | may help you in this, but it's nontrivial.) SCALAR will be read and/or |
5408 | 5346 | written depending on the FUNCTION--a pointer to the string value of SCALAR |
5409 | 5347 | will be passed as the third argument of the actual C<ioctl> call. (If SCALAR |
5410 | 5348 | has no string value but does have a numeric value, that value will be |
5411 | 5349 | passed rather than a pointer to the string value. To guarantee this to be |
5412 | 5350 | true, add a C<0> to the scalar before using it.) The C<pack> and C<unpack> |
5413 | 5351 | functions may be needed to manipulate the values of structures used by |
5414 | 5352 | C<ioctl>. |
5415 | 5353 | |
5416 | 5354 | =end original |
5417 | 5355 | |
5418 | 5356 | としなくてはならないでしょう。 |
5419 | 5357 | F<sys/ioctl.ph> がないか、間違った定義をしている場合には、 |
5420 | 5358 | F<< <sys/ioctl.ph> >>のような C のヘッダファイルをもとに、 |
5421 | 5359 | 自分で作らなければなりません。 |
5422 | 5360 | (Perl の配布キットに入っている B<h2ph> という |
5423 | 5361 | Perl スクリプトがこれを手助けしてくれるでしょうが、これは重要です。) |
5424 | 5362 | FOUNCTION に応じて SCALAR が読み書きされます。 |
5425 | 5363 | SCALAR の文字列値へのポインタが、実際の C<ioctl> コールの |
5426 | 5364 | 3 番目の引数として渡されます。 |
5427 | 5365 | (SCALAR が文字列値を持っておらず、数値を持っている場合には、 |
5428 | 5366 | 文字列値へのポインタの代わりに、その値が渡されます。 |
5429 | 5367 | このことを保証するためには、使用する前に SCALAR にC<0> を足してください。) |
5430 | 5368 | C<ioctl> で使われる構造体の値を操作するには、 |
5431 | 5369 | C<pack> 関数と C<unpack> 関数が必要となるでしょう。 |
5432 | 5370 | |
5433 | 5371 | =begin original |
5434 | 5372 | |
5435 | 5373 | The return value of C<ioctl> (and C<fcntl>) is as follows: |
5436 | 5374 | |
5437 | 5375 | =end original |
5438 | 5376 | |
5439 | 5377 | C<ioctl> (と C<fcntl>) の返り値は、以下のようになります: |
5440 | 5378 | |
5441 | 5379 | =begin original |
5442 | 5380 | |
5443 | 5381 | if OS returns: then Perl returns: |
5444 | 5382 | -1 undefined value |
5445 | 5383 | 0 string "0 but true" |
5446 | 5384 | anything else that number |
5447 | 5385 | |
5448 | 5386 | =end original |
5449 | 5387 | |
5450 | 5388 | OS が返した値: Perl が返す値: |
5451 | 5389 | -1 未定義値 |
5452 | 5390 | 0 「0 だが真」の文字列 |
5453 | 5391 | その他 その値そのもの |
5454 | 5392 | |
5455 | 5393 | =begin original |
5456 | 5394 | |
5457 | 5395 | Thus Perl returns true on success and false on failure, yet you can |
5458 | 5396 | still easily determine the actual value returned by the operating |
5459 | 5397 | system: |
5460 | 5398 | |
5461 | 5399 | =end original |
5462 | 5400 | |
5463 | 5401 | つまり Perl は、成功時に「真」、失敗時に「偽」を返す |
5464 | 5402 | ことになり、OS が実際に返した値も、以下のように簡単に知ることができます。 |
5465 | 5403 | |
5466 | 5404 | $retval = ioctl(...) || -1; |
5467 | 5405 | printf "System returned %d\n", $retval; |
5468 | 5406 | |
5469 | 5407 | =begin original |
5470 | 5408 | |
5471 | 5409 | The special string C<"0 but true"> is exempt from B<-w> complaints |
5472 | 5410 | about improper numeric conversions. |
5473 | 5411 | |
5474 | 5412 | =end original |
5475 | 5413 | |
5476 | 5414 | 特別な文字列 C<"0 だが真"> は、不適切な数値変換に関する |
5477 | 5415 | B<-w> 警告を回避します。 |
5478 | 5416 | |
5479 | 5417 | =item join EXPR,LIST |
5480 | 5418 | X<join> |
5481 | 5419 | |
5482 | 5420 | =begin original |
5483 | 5421 | |
5484 | 5422 | Joins the separate strings of LIST into a single string with fields |
5485 | 5423 | separated by the value of EXPR, and returns that new string. Example: |
5486 | 5424 | |
5487 | 5425 | =end original |
5488 | 5426 | |
5489 | 5427 | LIST の個別の文字列を、EXPR の値で区切って |
5490 | 5428 | 1 つの文字列につなげ、その文字列を返します。 |
5491 | 5429 | 例: |
5492 | 5430 | |
5493 | 5431 | $rec = join(':', $login,$passwd,$uid,$gid,$gcos,$home,$shell); |
5494 | 5432 | |
5495 | 5433 | =begin original |
5496 | 5434 | |
5497 | 5435 | Beware that unlike C<split>, C<join> doesn't take a pattern as its |
5498 | 5436 | first argument. Compare L</split>. |
5499 | 5437 | |
5500 | 5438 | =end original |
5501 | 5439 | |
5502 | 5440 | C<split> と違って、C<join> は最初の引数にパターンは取れないことに |
5503 | 5441 | 注意してください。 |
5504 | 5442 | L</split> と比較してください。 |
5505 | 5443 | |
5506 | 5444 | =item keys HASH |
5507 | 5445 | X<keys> X<key> |
5508 | 5446 | |
5509 | 5447 | =begin original |
5510 | 5448 | |
5511 | 5449 | Returns a list consisting of all the keys of the named hash. |
5512 | 5450 | (In scalar context, returns the number of keys.) |
5513 | 5451 | |
5514 | 5452 | =end original |
5515 | 5453 | |
5516 | 5454 | 指定したハッシュのすべての key からなる、リストを返します。 |
5517 | 5455 | (スカラコンテキストでは、key の数を返します。) |
5518 | 5456 | |
5519 | 5457 | =begin original |
5520 | 5458 | |
5521 | 5459 | The keys are returned in an apparently random order. The actual |
5522 | 5460 | random order is subject to change in future versions of perl, but it |
5523 | 5461 | is guaranteed to be the same order as either the C<values> or C<each> |
5524 | 5462 | function produces (given that the hash has not been modified). Since |
5525 | 5463 | Perl 5.8.1 the ordering is different even between different runs of |
5526 | 5464 | Perl for security reasons (see L<perlsec/"Algorithmic Complexity |
5527 | 5465 | Attacks">). |
5528 | 5466 | |
5529 | 5467 | =end original |
5530 | 5468 | |
5531 | 5469 | キーは見たところではランダムな順番に返されます。 |
5532 | 5470 | 実際のランダムな順番は perl の将来のバージョンでは変わるかもしれませんが、 |
5533 | 5471 | C<values> や C<each> 関数が同じ(変更されていない)ハッシュに対して |
5534 | 5472 | 生成するのと同じ順番であることは保証されます。 |
5535 | 5473 | Perl 5.8.1 以降ではセキュリティ上の理由により、 |
5536 | 5474 | 実行される毎に順番は変わります |
5537 | 5475 | (L<perlsec/"Algorithmic Complexity Attacks"> を参照してください)。 |
5538 | 5476 | |
5539 | 5477 | =begin original |
5540 | 5478 | |
5541 | 5479 | As a side effect, calling keys() resets the HASH's internal iterator |
5542 | 5480 | (see L</each>). In particular, calling keys() in void context resets |
5543 | 5481 | the iterator with no other overhead. |
5544 | 5482 | |
5545 | 5483 | =end original |
5546 | 5484 | |
5547 | 5485 | 副作用として、HASH の反復子を初期化します |
5548 | 5486 | (L</each> を参照してください)。 |
5549 | 5487 | 特に、無効コンテキストで keys() を呼び出すと |
5550 | 5488 | オーバーヘッドなしで反復子を初期化します。 |
5551 | 5489 | |
5552 | 5490 | =begin original |
5553 | 5491 | |
5554 | 5492 | Here is yet another way to print your environment: |
5555 | 5493 | |
5556 | 5494 | =end original |
5557 | 5495 | |
5558 | 5496 | 環境変数を表示する別の例です: |
5559 | 5497 | |
5560 | 5498 | @keys = keys %ENV; |
5561 | 5499 | @values = values %ENV; |
5562 | 5500 | while (@keys) { |
5563 | 5501 | print pop(@keys), '=', pop(@values), "\n"; |
5564 | 5502 | } |
5565 | 5503 | |
5566 | 5504 | =begin original |
5567 | 5505 | |
5568 | 5506 | or how about sorted by key: |
5569 | 5507 | |
5570 | 5508 | =end original |
5571 | 5509 | |
5572 | 5510 | key でソートしてもいいでしょう: |
5573 | 5511 | |
5574 | 5512 | foreach $key (sort(keys %ENV)) { |
5575 | 5513 | print $key, '=', $ENV{$key}, "\n"; |
5576 | 5514 | } |
5577 | 5515 | |
5578 | 5516 | =begin original |
5579 | 5517 | |
5580 | 5518 | The returned values are copies of the original keys in the hash, so |
5581 | 5519 | modifying them will not affect the original hash. Compare L</values>. |
5582 | 5520 | |
5583 | 5521 | =end original |
5584 | 5522 | |
5585 | 5523 | 返される値はハッシュにある元のキーのコピーなので、 |
5586 | 5524 | これを変更しても元のハッシュには影響を与えません。 |
5587 | 5525 | L</values> と比較してください。 |
5588 | 5526 | |
5589 | 5527 | =begin original |
5590 | 5528 | |
5591 | 5529 | To sort a hash by value, you'll need to use a C<sort> function. |
5592 | 5530 | Here's a descending numeric sort of a hash by its values: |
5593 | 5531 | |
5594 | 5532 | =end original |
5595 | 5533 | |
5596 | 5534 | ハッシュを値でソートするためには、C<sort> 関数を使う必要があります。 |
5597 | 5535 | 以下ではハッシュの値を数値の降順でソートしています: |
5598 | 5536 | |
5599 | 5537 | foreach $key (sort { $hash{$b} <=> $hash{$a} } keys %hash) { |
5600 | 5538 | printf "%4d %s\n", $hash{$key}, $key; |
5601 | 5539 | } |
5602 | 5540 | |
5603 | 5541 | =begin original |
5604 | 5542 | |
5605 | 5543 | As an lvalue C<keys> allows you to increase the number of hash buckets |
5606 | 5544 | allocated for the given hash. This can gain you a measure of efficiency if |
5607 | 5545 | you know the hash is going to get big. (This is similar to pre-extending |
5608 | 5546 | an array by assigning a larger number to $#array.) If you say |
5609 | 5547 | |
5610 | 5548 | =end original |
5611 | 5549 | |
5612 | 5550 | 左辺値としては、C<keys> を使うことで与えられたハッシュに割り当てられた |
5613 | 5551 | ハッシュ表の大きさを増やすことができます。 |
5614 | 5552 | これによって、ハッシュが大きくなっていくなっていくときの |
5615 | 5553 | 効率の測定ができます。以下のようにすると: |
5616 | 5554 | |
5617 | 5555 | keys %hash = 200; |
5618 | 5556 | |
5619 | 5557 | =begin original |
5620 | 5558 | |
5621 | 5559 | then C<%hash> will have at least 200 buckets allocated for it--256 of them, |
5622 | 5560 | in fact, since it rounds up to the next power of two. These |
5623 | 5561 | buckets will be retained even if you do C<%hash = ()>, use C<undef |
5624 | 5562 | %hash> if you want to free the storage while C<%hash> is still in scope. |
5625 | 5563 | You can't shrink the number of buckets allocated for the hash using |
5626 | 5564 | C<keys> in this way (but you needn't worry about doing this by accident, |
5627 | 5565 | as trying has no effect). |
5628 | 5566 | |
5629 | 5567 | =end original |
5630 | 5568 | |
5631 | 5569 | C<%hash> は少なくとも 200 の大きさの表が割り当てられます -- |
5632 | 5570 | 実際には 2 のべき乗に切り上げられるので、256 が割り当てられます。 |
5633 | 5571 | この表はたとえ C<%hash = ()> としても残るので、 |
5634 | 5572 | もし C<%hash> がスコープにいるうちにこの領域を開放したい場合は |
5635 | 5573 | C<undef %hash> を使います。 |
5636 | 5574 | この方法で C<keys> を使うことで、表の大きさを小さくすることはできません |
5637 | 5575 | (間違えてそのようなことをしても何も起きないので気にすることはありません)。 |
5638 | 5576 | |
5639 | 5577 | =begin original |
5640 | 5578 | |
5641 | 5579 | See also C<each>, C<values> and C<sort>. |
5642 | 5580 | |
5643 | 5581 | =end original |
5644 | 5582 | |
5645 | 5583 | C<each>, C<values>, C<sort> も参照してください。 |
5646 | 5584 | |
5647 | 5585 | =item kill SIGNAL, LIST |
5648 | 5586 | X<kill> X<signal> |
5649 | 5587 | |
5650 | 5588 | =begin original |
5651 | 5589 | |
5652 | 5590 | Sends a signal to a list of processes. Returns the number of |
5653 | 5591 | processes successfully signaled (which is not necessarily the |
5654 | 5592 | same as the number actually killed). |
5655 | 5593 | |
5656 | 5594 | =end original |
5657 | 5595 | |
5658 | 5596 | プロセスのリストにシグナルを送ります。シグナル送信に成功したプロセスの |
5659 | 5597 | 数を返します |
5660 | 5598 | (実際に kill に成功したプロセスと同じとは限りません)。 |
5661 | 5599 | |
5662 | 5600 | $cnt = kill 1, $child1, $child2; |
5663 | 5601 | kill 9, @goners; |
5664 | 5602 | |
5665 | 5603 | =begin original |
5666 | 5604 | |
5667 | 5605 | If SIGNAL is zero, no signal is sent to the process, but the kill(2) |
5668 | 5606 | system call will check whether it's possible to send a signal to it (that |
5669 | 5607 | means, to be brief, that the process is owned by the same user, or we are |
5670 | 5608 | the super-user). This is a useful way to check that a child process is |
5671 | 5609 | alive (even if only as a zombie) and hasn't changed its UID. See |
5672 | 5610 | L<perlport> for notes on the portability of this construct. |
5673 | 5611 | |
5674 | 5612 | =end original |
5675 | 5613 | |
5676 | 5614 | SIGNAL がゼロの場合、プロセスにシグナルは送られませんが、 |
5677 | 5615 | but the kill(2) |
5678 | 5616 | system call will check whether it's possible to send a signal to it (that |
5679 | 5617 | means, to be brief, that the process is owned by the same user, or we are |
5680 | 5618 | the super-user) |
5681 | 5619 | これは子プロセスが(ゾンビとしてだけでも)生きていて、 UID が |
5682 | 5620 | 変わっていないことを調べる時に有用です。 |
5683 | 5621 | この構成の移植性に関する注意については L<perlport> を参照して下さい。 |
5684 | 5622 | |
5685 | 5623 | =begin original |
5686 | 5624 | |
5687 | 5625 | Unlike in the shell, if SIGNAL is negative, it kills |
5688 | 5626 | process groups instead of processes. (On System V, a negative I<PROCESS> |
5689 | 5627 | number will also kill process groups, but that's not portable.) That |
5690 | 5628 | means you usually want to use positive not negative signals. You may also |
5691 | 5629 | use a signal name in quotes. |
5692 | 5630 | |
5693 | 5631 | =end original |
5694 | 5632 | |
5695 | 5633 | シェルとは異なり、シグナルに負の数を与えると、 |
5696 | 5634 | プロセスではなくプロセスグループに対して kill を行ないます。 |
5697 | 5635 | (Syetem V では、プロセス番号として負の値を与えても、 |
5698 | 5636 | プロセスグループの kill を行ないますが、 |
5699 | 5637 | 移植性がありません。) |
5700 | 5638 | すなわち、通常は、負のシグナルは用いず、正のシグナルを使うことになります。 |
5701 | 5639 | シグナル名をクォートして使うこともできます。 |
5702 | 5640 | |
5703 | 5641 | =begin original |
5704 | 5642 | |
5705 | 5643 | See L<perlipc/"Signals"> for more details. |
5706 | 5644 | |
5707 | 5645 | =end original |
5708 | 5646 | |
5709 | 5647 | 詳細はL<perlipc/"Signals">を参照してください。 |
5710 | 5648 | |
5711 | 5649 | =item last LABEL |
5712 | 5650 | X<last> X<break> |
5713 | 5651 | |
5714 | 5652 | =item last |
5715 | 5653 | |
5716 | 5654 | =begin original |
5717 | 5655 | |
5718 | 5656 | The C<last> command is like the C<break> statement in C (as used in |
5719 | 5657 | loops); it immediately exits the loop in question. If the LABEL is |
5720 | 5658 | omitted, the command refers to the innermost enclosing loop. The |
5721 | 5659 | C<continue> block, if any, is not executed: |
5722 | 5660 | |
5723 | 5661 | =end original |
5724 | 5662 | |
5725 | 5663 | C<last> コマンドは、(ループ内で使った) C の C<break> 文と |
5726 | 5664 | 同じようなもので、LABEL で指定されるループを即座に抜けます。 |
5727 | 5665 | LABEL が省略されると、一番内側のループが対象となります。 |
5728 | 5666 | C<continue> ブロックがあっても実行されません: |
5729 | 5667 | |
5730 | 5668 | LINE: while (<STDIN>) { |
5731 | 5669 | last LINE if /^$/; # exit when done with header |
5732 | 5670 | #... |
5733 | 5671 | } |
5734 | 5672 | |
5735 | 5673 | =begin original |
5736 | 5674 | |
5737 | 5675 | C<last> cannot be used to exit a block which returns a value such as |
5738 | 5676 | C<eval {}>, C<sub {}> or C<do {}>, and should not be used to exit |
5739 | 5677 | a grep() or map() operation. |
5740 | 5678 | |
5741 | 5679 | =end original |
5742 | 5680 | |
5743 | 5681 | C<last> は C<eval {}>, C<sub {}>, C<do {}> といった |
5744 | 5682 | 値を返すブロックを終了するのには使えませんし、 |
5745 | 5683 | grep() や map() 操作を終了するのに使うべきではありません。 |
5746 | 5684 | |
5747 | 5685 | =begin original |
5748 | 5686 | |
5749 | 5687 | Note that a block by itself is semantically identical to a loop |
5750 | 5688 | that executes once. Thus C<last> can be used to effect an early |
5751 | 5689 | exit out of such a block. |
5752 | 5690 | |
5753 | 5691 | =end original |
5754 | 5692 | |
5755 | 5693 | ブロックはそれ自体文法的には一度だけ実行されるループと同等であることに |
5756 | 5694 | 注意してください。従って、C<last> でそのようなブロックを |
5757 | 5695 | 途中で抜け出すことができます。 |
5758 | 5696 | |
5759 | 5697 | =begin original |
5760 | 5698 | |
5761 | 5699 | See also L</continue> for an illustration of how C<last>, C<next>, and |
5762 | 5700 | C<redo> work. |
5763 | 5701 | |
5764 | 5702 | =end original |
5765 | 5703 | |
5766 | 5704 | C<last>, C<next>, C<redo> がどのように働くかについては |
5767 | L</continue> | |
5705 | L</continue> を参照して下さい。 | |
5768 | 5706 | |
5769 | 5707 | =item lc EXPR |
5770 | 5708 | X<lc> X<lowercase> |
5771 | 5709 | |
5772 | 5710 | =item lc |
5773 | 5711 | |
5774 | 5712 | =begin original |
5775 | 5713 | |
5776 | 5714 | Returns a lowercased version of EXPR. This is the internal function |
5777 | 5715 | implementing the C<\L> escape in double-quoted strings. Respects |
5778 | 5716 | current LC_CTYPE locale if C<use locale> in force. See L<perllocale> |
5779 | 5717 | and L<perlunicode> for more details about locale and Unicode support. |
5780 | 5718 | |
5781 | 5719 | =end original |
5782 | 5720 | |
5783 | 5721 | EXPR を小文字に変換したものを返します。 |
5784 | 5722 | これは、ダブルクォート文字列における、 |
5785 | 5723 | C<\L> エスケープを実装する内部関数です。 |
5786 | 5724 | C<use locale> が有効な場合は、現在の LC_CTYPE ロケールを参照します。 |
5787 | 5725 | ロケールと Unicode のサポートに関するさらなる詳細については |
5788 | 5726 | L<perllocale> と L<perlunicode> を参照してください。 |
5789 | 5727 | |
5790 | 5728 | =begin original |
5791 | 5729 | |
5792 | 5730 | If EXPR is omitted, uses C<$_>. |
5793 | 5731 | |
5794 | 5732 | =end original |
5795 | 5733 | |
5796 | EXPR が省略されると、C<$_> | |
5734 | EXPR が省略されると、C<$_>を使います。 | |
5797 | 5735 | |
5798 | 5736 | =item lcfirst EXPR |
5799 | 5737 | X<lcfirst> X<lowercase> |
5800 | 5738 | |
5801 | 5739 | =item lcfirst |
5802 | 5740 | |
5803 | 5741 | =begin original |
5804 | 5742 | |
5805 | 5743 | Returns the value of EXPR with the first character lowercased. This |
5806 | 5744 | is the internal function implementing the C<\l> escape in |
5807 | 5745 | double-quoted strings. Respects current LC_CTYPE locale if C<use |
5808 | 5746 | locale> in force. See L<perllocale> and L<perlunicode> for more |
5809 | 5747 | details about locale and Unicode support. |
5810 | 5748 | |
5811 | 5749 | =end original |
5812 | 5750 | |
5813 | 5751 | 最初の文字だけを小文字にした、EXPR を返します。 |
5814 | 5752 | これは、ダブルクォート文字列における、C<\l> エスケープを |
5815 | 5753 | 実装する内部関数です。 |
5816 | 5754 | C<use locale> が有効な場合は、現在の LC_CTYPE ロケールを参照します。 |
5817 | 5755 | ロケールと Unicode のサポートに関するさらなる詳細については |
5818 | 5756 | L<perllocale> と L<perlunicode> を参照してください。 |
5819 | 5757 | |
5820 | 5758 | =begin original |
5821 | 5759 | |
5822 | 5760 | If EXPR is omitted, uses C<$_>. |
5823 | 5761 | |
5824 | 5762 | =end original |
5825 | 5763 | |
5826 | EXPR が省略されると、C<$_> | |
5764 | EXPR が省略されると、C<$_>を使います。 | |
5827 | 5765 | |
5828 | 5766 | =item length EXPR |
5829 | 5767 | X<length> X<size> |
5830 | 5768 | |
5831 | 5769 | =item length |
5832 | 5770 | |
5833 | 5771 | =begin original |
5834 | 5772 | |
5835 | 5773 | Returns the length in I<characters> of the value of EXPR. If EXPR is |
5836 | 5774 | omitted, returns length of C<$_>. Note that this cannot be used on |
5837 | 5775 | an entire array or hash to find out how many elements these have. |
5838 | 5776 | For that, use C<scalar @array> and C<scalar keys %hash> respectively. |
5839 | 5777 | |
5840 | 5778 | =end original |
5841 | 5779 | |
5842 | 5780 | EXPR の値の I<文字> の長さを返します。 |
5843 | 5781 | EXPR が省略されたときには、C<$_> の長さを返します。 |
5844 | 5782 | これは配列やハッシュ全体に対してどれだけの要素を含んでいるかを |
5845 | 5783 | 調べるためには使えません。 |
5846 | 5784 | そのような用途には、それぞれ C<scalar @array> と C<scalar keys %hash> を |
5847 | 5785 | 利用してください。 |
5848 | 5786 | |
5849 | 5787 | =begin original |
5850 | 5788 | |
5851 | 5789 | Note the I<characters>: if the EXPR is in Unicode, you will get the |
5852 | 5790 | number of characters, not the number of bytes. To get the length |
5853 | 5791 | of the internal string in bytes, use C<bytes::length(EXPR)>, see |
5854 | 5792 | L<bytes>. Note that the internal encoding is variable, and the number |
5855 | 5793 | of bytes usually meaningless. To get the number of bytes that the |
5856 | 5794 | string would have when encoded as UTF-8, use |
5857 | 5795 | C<length(Encoding::encode_utf8(EXPR))>. |
5858 | 5796 | |
5859 | 5797 | =end original |
5860 | 5798 | |
5861 | 5799 | I<文字> 関する注意: |
5862 | 5800 | EXPR が Unicode の場合、バイト数ではなく、文字の数が返ります。 |
5863 | 5801 | 内部文字列のバイト数が必要な場合は L<bytes> を参照して、 |
5864 | 5802 | C<do { use bytes; length(EXPR) }> を使ってください。 |
5865 | 5803 | 内部エンコーディングは様々なので、バイト数は普通は無意味です。 |
5866 | 5804 | UTF-8 でエンコードされている場合の文字列のバイト数を得たい場合は、 |
5867 | 5805 | C<length(Encoding::encode_utf8(EXPR))> を使ってください。 |
5868 | 5806 | |
5869 | 5807 | =item link OLDFILE,NEWFILE |
5870 | 5808 | X<link> |
5871 | 5809 | |
5872 | 5810 | =begin original |
5873 | 5811 | |
5874 | 5812 | Creates a new filename linked to the old filename. Returns true for |
5875 | 5813 | success, false otherwise. |
5876 | 5814 | |
5877 | 5815 | =end original |
5878 | 5816 | |
5879 | 5817 | OLDFILE にリンクされた、新しいファイル NEWFILE を作ります。 |
5880 | 5818 | 成功時には true を、失敗時には false を返します。 |
5881 | 5819 | |
5882 | 5820 | =item listen SOCKET,QUEUESIZE |
5883 | 5821 | X<listen> |
5884 | 5822 | |
5885 | 5823 | =begin original |
5886 | 5824 | |
5887 | 5825 | Does the same thing that the listen system call does. Returns true if |
5888 | 5826 | it succeeded, false otherwise. See the example in |
5889 | 5827 | L<perlipc/"Sockets: Client/Server Communication">. |
5890 | 5828 | |
5891 | 5829 | =end original |
5892 | 5830 | |
5893 | 5831 | listen システムコールと同じことをします。成功時には真を返し、 |
5894 | 5832 | 失敗時には偽を返します。 |
5895 | 5833 | L<perlipc/"Sockets: Client/Server Communication">の例を参照してください。 |
5896 | 5834 | |
5897 | 5835 | =item local EXPR |
5898 | 5836 | X<local> |
5899 | 5837 | |
5900 | 5838 | =begin original |
5901 | 5839 | |
5902 | 5840 | You really probably want to be using C<my> instead, because C<local> isn't |
5903 | 5841 | what most people think of as "local". See |
5904 | 5842 | L<perlsub/"Private Variables via my()"> for details. |
5905 | 5843 | |
5906 | 5844 | =end original |
5907 | 5845 | |
5908 | 5846 | あなたはが本当に望んでいるのは C<my> の方でしょう。 |
5909 | 5847 | C<local> はほとんどの人々が「ローカル」と考えるものと違うからです。 |
5910 | 5848 | 詳細は L<perlsub/"Private Variables via my()"> を参照してください。 |
5911 | 5849 | |
5912 | 5850 | =begin original |
5913 | 5851 | |
5914 | 5852 | A local modifies the listed variables to be local to the enclosing |
5915 | 5853 | block, file, or eval. If more than one value is listed, the list must |
5916 | 5854 | be placed in parentheses. See L<perlsub/"Temporary Values via local()"> |
5917 | 5855 | for details, including issues with tied arrays and hashes. |
5918 | 5856 | |
5919 | 5857 | =end original |
5920 | 5858 | |
5921 | 5859 | "local" はリストアップされた変数を、囲っているブロック、 |
5922 | 5860 | ファイル、eval の中で、ローカルなものにします。 |
5923 | 5861 | 複数の値を指定する場合は、リストは括弧でくくらなければなりません。 |
5924 | 5862 | tie した配列とハッシュに関する事項を含む詳細については |
5925 | 5863 | L<perlsub/"Temporary Values via local()"> を参照してください。 |
5926 | 5864 | |
5927 | 5865 | =item localtime EXPR |
5928 | 5866 | X<localtime> X<ctime> |
5929 | 5867 | |
5930 | 5868 | =item localtime |
5931 | 5869 | |
5932 | 5870 | =begin original |
5933 | 5871 | |
5934 | 5872 | Converts a time as returned by the time function to a 9-element list |
5935 | 5873 | with the time analyzed for the local time zone. Typically used as |
5936 | 5874 | follows: |
5937 | 5875 | |
5938 | 5876 | =end original |
5939 | 5877 | |
5940 | 5878 | time 関数が返す時刻を、ローカルなタイムゾーンで測った時刻として、 |
5941 | 5879 | 9 要素の配列に変換します。 |
5942 | 5880 | 通常は、以下のようにして使用します。 |
5943 | 5881 | |
5944 | 5882 | # 0 1 2 3 4 5 6 7 8 |
5945 | 5883 | ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = |
5946 | 5884 | localtime(time); |
5947 | 5885 | |
5948 | 5886 | =begin original |
5949 | 5887 | |
5950 | 5888 | All list elements are numeric, and come straight out of the C `struct |
5951 | 5889 | tm'. C<$sec>, C<$min>, and C<$hour> are the seconds, minutes, and hours |
5952 | 5890 | of the specified time. |
5953 | 5891 | |
5954 | 5892 | =end original |
5955 | 5893 | |
5956 | 5894 | すべてのリスト要素は数値で、C の `struct tm' 構造体から |
5957 | 5895 | 直接持ってきます。 |
5958 | 5896 | C<$sec>, C<$min>, C<$hour> は指定された時刻の秒、分、時です。 |
5959 | 5897 | |
5960 | 5898 | =begin original |
5961 | 5899 | |
5962 | 5900 | C<$mday> is the day of the month, and C<$mon> is the month itself, in |
5963 | 5901 | the range C<0..11> with 0 indicating January and 11 indicating December. |
5964 | 5902 | This makes it easy to get a month name from a list: |
5965 | 5903 | |
5966 | 5904 | =end original |
5967 | 5905 | |
5968 | 5906 | C<$mday> は月の何日目か、C<$mon> は月の値です。 |
5969 | 5907 | 月の値は C<0..11> で、0 が 1 月、11 が 12 月です。 |
5970 | 5908 | これにより、リストから月の名前を得るのが簡単になります: |
5971 | 5909 | |
5972 | 5910 | my @abbr = qw( Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec ); |
5973 | 5911 | print "$abbr[$mon] $mday"; |
5974 | 5912 | # $mon=9, $mday=18 gives "Oct 18" |
5975 | 5913 | |
5976 | 5914 | =begin original |
5977 | 5915 | |
5978 | 5916 | C<$year> is the number of years since 1900, not just the last two digits |
5979 | 5917 | of the year. That is, C<$year> is C<123> in year 2023. The proper way |
5980 | 5918 | to get a complete 4-digit year is simply: |
5981 | 5919 | |
5982 | 5920 | =end original |
5983 | 5921 | |
5984 | 5922 | C<$year> は 1900 年からの年数であり、単に西暦の下 2 桁を表しているのでは |
5985 | 5923 | ありません。 |
5986 | 5924 | つまり、$year が C<123> なら 2023 年です。 |
5987 | 5925 | 完全な 4 桁の西暦を得るには単に以下のようにしてください: |
5988 | 5926 | |
5989 | 5927 | $year += 1900; |
5990 | 5928 | |
5991 | 5929 | =begin original |
5992 | 5930 | |
5993 | 5931 | Otherwise you create non-Y2K-compliant programs--and you wouldn't want |
5994 | 5932 | to do that, would you? |
5995 | 5933 | |
5996 | 5934 | =end original |
5997 | 5935 | |
5998 | 5936 | さもなければ、Y2K 問題を含んだプログラムを作ることになります -- |
5999 | 5937 | それはお望みじゃないでしょう? |
6000 | 5938 | |
6001 | 5939 | =begin original |
6002 | 5940 | |
6003 | 5941 | To get the last two digits of the year (e.g., '01' in 2001) do: |
6004 | 5942 | |
6005 | 5943 | =end original |
6006 | 5944 | |
6007 | 5945 | 西暦の下 2 桁(2001 年では '01')がほしい場合は以下のようにします: |
6008 | 5946 | |
6009 | 5947 | $year = sprintf("%02d", $year % 100); |
6010 | 5948 | |
6011 | 5949 | =begin original |
6012 | 5950 | |
6013 | 5951 | C<$wday> is the day of the week, with 0 indicating Sunday and 3 indicating |
6014 | 5952 | Wednesday. C<$yday> is the day of the year, in the range C<0..364> |
6015 | 5953 | (or C<0..365> in leap years.) |
6016 | 5954 | |
6017 | 5955 | =end original |
6018 | 5956 | |
6019 | 5957 | C<$wday> は曜日で、0 が日曜日、3 が水曜日です。 |
6020 | 5958 | C<$yday> はその年の何日目かで、C<0..364> の値を取ります |
6021 | 5959 | (うるう年は C<0..365> です)。 |
6022 | 5960 | |
6023 | 5961 | =begin original |
6024 | 5962 | |
6025 | 5963 | C<$isdst> is true if the specified time occurs during Daylight Saving |
6026 | 5964 | Time, false otherwise. |
6027 | 5965 | |
6028 | 5966 | =end original |
6029 | 5967 | |
6030 | 5968 | C<$isdst> は指定された時刻が夏時間の場合は真、そうでなければ偽です。 |
6031 | 5969 | |
6032 | 5970 | =begin original |
6033 | 5971 | |
6034 | If EXPR is omitted, C<localtime()> uses the current time (a | |
5972 | If EXPR is omitted, C<localtime()> uses the current time (C<localtime(time)>). | |
6035 | by time(3)). | |
6036 | 5973 | |
6037 | 5974 | =end original |
6038 | 5975 | |
6039 | EXPR が省略されると、C<localtime()> は | |
5976 | EXPR が省略されると、C<localtime()> は現在時刻を使います | |
6040 | ||
5977 | (C<localtime(time())>。 | |
6041 | 5978 | |
6042 | 5979 | =begin original |
6043 | 5980 | |
6044 | 5981 | In scalar context, C<localtime()> returns the ctime(3) value: |
6045 | 5982 | |
6046 | 5983 | =end original |
6047 | 5984 | |
6048 | 5985 | スカラコンテキストでは、C<localtime()> は ctime(3) の値を返します: |
6049 | 5986 | |
6050 | 5987 | $now_string = localtime; # e.g., "Thu Oct 13 04:54:34 1994" |
6051 | 5988 | |
6052 | 5989 | =begin original |
6053 | 5990 | |
6054 | 5991 | This scalar value is B<not> locale dependent but is a Perl builtin. For GMT |
6055 | 5992 | instead of local time use the L</gmtime> builtin. See also the |
6056 | 5993 | C<Time::Local> module (to convert the second, minutes, hours, ... back to |
6057 | 5994 | the integer value returned by time()), and the L<POSIX> module's strftime(3) |
6058 | 5995 | and mktime(3) functions. |
6059 | 5996 | |
6060 | 5997 | =end original |
6061 | 5998 | |
6062 | 5999 | スカラ値はロケール依存 B<ではなく>、Perl の組み込みの値です。 |
6063 | 6000 | ローカル時刻ではなく GMT がほしい場合は L</gmtime> 組み込み関数を |
6064 | 6001 | 使ってください。 |
6065 | 6002 | また、(秒、分、時…の形から、time() が返す値である |
6066 | 6003 | 1970 年 1 月 1 日の真夜中からの秒数に変換する) C<Time::Local> モジュール |
6067 | 6004 | 及び POSIX モジュールで提供される strftime(3) と mktime(3) 関数も |
6068 | 6005 | 参照してください。 |
6069 | 6006 | |
6070 | 6007 | =begin original |
6071 | 6008 | |
6072 | 6009 | To get somewhat similar but locale dependent date strings, set up your |
6073 | 6010 | locale environment variables appropriately (please see L<perllocale>) and |
6074 | 6011 | try for example: |
6075 | 6012 | |
6076 | 6013 | =end original |
6077 | 6014 | |
6078 | 6015 | 似たような、しかしロケール依存の日付文字列がほしい場合は、 |
6079 | 6016 | ロケール環境変数を適切に設定して(L<perllocale> を参照してください)、 |
6080 | 6017 | 以下の例を試してください: |
6081 | 6018 | |
6082 | 6019 | use POSIX qw(strftime); |
6083 | 6020 | $now_string = strftime "%a %b %e %H:%M:%S %Y", localtime; |
6084 | 6021 | # or for GMT formatted appropriately for your locale: |
6085 | 6022 | $now_string = strftime "%a %b %e %H:%M:%S %Y", gmtime; |
6086 | 6023 | |
6087 | 6024 | =begin original |
6088 | 6025 | |
6089 | 6026 | Note that the C<%a> and C<%b>, the short forms of the day of the week |
6090 | 6027 | and the month of the year, may not necessarily be three characters wide. |
6091 | 6028 | |
6092 | 6029 | =end original |
6093 | 6030 | |
6094 | 6031 | 曜日と月の短い表現である C<%a> と C<%b> は、3 文字とは限らないことに |
6095 | 6032 | 注意してください。 |
6096 | 6033 | |
6097 | 6034 | =begin original |
6098 | 6035 | |
6099 | 6036 | See L<perlport/localtime> for portability concerns. |
6100 | 6037 | |
6101 | 6038 | =end original |
6102 | 6039 | |
6103 | 6040 | 移植性については L<perlport/localtime> を参照してください。 |
6104 | 6041 | |
6105 | 6042 | =begin original |
6106 | 6043 | |
6107 | 6044 | The L<Time::gmtime> and L<Time::localtime> modules provides a convenient, |
6108 | 6045 | by-name access mechanism to the gmtime() and localtime() functions, |
6109 | 6046 | respectively. |
6110 | 6047 | |
6111 | 6048 | =end original |
6112 | 6049 | |
6113 | 6050 | L<Time::gmtime> モジュールと L<Time::localtime> モジュールは、それぞれ |
6114 | 6051 | gmtime() 関数と localtime() 関数に、名前でアクセスする機構を提供する |
6115 | 6052 | 便利なモジュールです。 |
6116 | 6053 | |
6117 | 6054 | =begin original |
6118 | 6055 | |
6119 | 6056 | For a comprehensive date and time representation look at the |
6120 | 6057 | L<DateTime> module on CPAN. |
6121 | 6058 | |
6122 | 6059 | =end original |
6123 | 6060 | |
6124 | 6061 | 包括的な日付と時刻の表現については、CPAN の L<DateTime> モジュールを |
6125 | 6062 | 参照してください。 |
6126 | 6063 | |
6127 | 6064 | =item lock THING |
6128 | 6065 | X<lock> |
6129 | 6066 | |
6130 | 6067 | =begin original |
6131 | 6068 | |
6132 | 6069 | This function places an advisory lock on a shared variable, or referenced |
6133 | 6070 | object contained in I<THING> until the lock goes out of scope. |
6134 | 6071 | |
6135 | 6072 | =end original |
6136 | 6073 | |
6137 | 6074 | この関数は I<THING> が含む共有変数またはリファレンスされたオブジェクトに、 |
6138 | 6075 | スコープから出るまでアドバイサリロックを掛けます. |
6139 | 6076 | |
6140 | 6077 | =begin original |
6141 | 6078 | |
6142 | 6079 | lock() is a "weak keyword" : this means that if you've defined a function |
6143 | 6080 | by this name (before any calls to it), that function will be called |
6144 | 6081 | instead. (However, if you've said C<use threads>, lock() is always a |
6145 | 6082 | keyword.) See L<threads>. |
6146 | 6083 | |
6147 | 6084 | =end original |
6148 | 6085 | |
6149 | 6086 | lock() は「弱いキーワード」です: もしユーザーが(呼び出し前に) |
6150 | 6087 | この名前で関数を定義すると、定義された関数の方が呼び出されます |
6151 | 6088 | (ただし、C<use threads> とすると、lock() は常にキーワードです)。 |
6152 | 6089 | L<threads> を参照してください。 |
6153 | 6090 | |
6154 | 6091 | =item log EXPR |
6155 | 6092 | X<log> X<logarithm> X<e> X<ln> X<base> |
6156 | 6093 | |
6157 | 6094 | =item log |
6158 | 6095 | |
6159 | 6096 | =begin original |
6160 | 6097 | |
6161 | 6098 | Returns the natural logarithm (base I<e>) of EXPR. If EXPR is omitted, |
6162 | 6099 | returns log of C<$_>. To get the log of another base, use basic algebra: |
6163 | 6100 | The base-N log of a number is equal to the natural log of that number |
6164 | 6101 | divided by the natural log of N. For example: |
6165 | 6102 | |
6166 | 6103 | =end original |
6167 | 6104 | |
6168 | 6105 | EXPR の (I<e> を底とする) 自然対数を返します。 |
6169 | 6106 | EXPR が省略されると、C<$_> の対数を返します。 |
6170 | 6107 | 底の異なる対数を求めるためには、基礎代数を利用してください: |
6171 | 6108 | ある数の N を底とする対数は、その数の自然対数を N の自然対数で割ったものです。 |
6172 | 6109 | 例: |
6173 | 6110 | |
6174 | 6111 | sub log10 { |
6175 | 6112 | my $n = shift; |
6176 | 6113 | return log($n)/log(10); |
6177 | 6114 | } |
6178 | 6115 | |
6179 | 6116 | =begin original |
6180 | 6117 | |
6181 | 6118 | See also L</exp> for the inverse operation. |
6182 | 6119 | |
6183 | 6120 | =end original |
6184 | 6121 | |
6185 | 6122 | 逆操作については L</exp> を参照して下さい。 |
6186 | 6123 | |
6187 | 6124 | =item lstat EXPR |
6188 | 6125 | X<lstat> |
6189 | 6126 | |
6190 | 6127 | =item lstat |
6191 | 6128 | |
6192 | 6129 | =begin original |
6193 | 6130 | |
6194 | 6131 | Does the same thing as the C<stat> function (including setting the |
6195 | 6132 | special C<_> filehandle) but stats a symbolic link instead of the file |
6196 | 6133 | the symbolic link points to. If symbolic links are unimplemented on |
6197 | 6134 | your system, a normal C<stat> is done. For much more detailed |
6198 | 6135 | information, please see the documentation for C<stat>. |
6199 | 6136 | |
6200 | 6137 | =end original |
6201 | 6138 | |
6202 | 6139 | (特別なファイルハンドルである C<_> の設定を含めて) |
6203 | 6140 | C<stat> 関数と同じことをしますが、シンボリックリンクが |
6204 | 6141 | 指しているファイルではなく、シンボリックリンク自体の stat をとります。 |
6205 | 6142 | シンボリックリンクがシステムに実装されていないと、通常の C<stat> が行なわれます。 |
6206 | 6143 | さらにより詳細な情報については、L<stat> の文書を参照してください。 |
6207 | 6144 | |
6208 | 6145 | =begin original |
6209 | 6146 | |
6210 | 6147 | If EXPR is omitted, stats C<$_>. |
6211 | 6148 | |
6212 | 6149 | =end original |
6213 | 6150 | |
6214 | 6151 | EXPR が省略されると、C<$_> の stat をとります。 |
6215 | 6152 | |
6216 | 6153 | =item m// |
6217 | 6154 | |
6218 | 6155 | =begin original |
6219 | 6156 | |
6220 | The match operator. See L<perlop | |
6157 | The match operator. See L<perlop>. | |
6221 | 6158 | |
6222 | 6159 | =end original |
6223 | 6160 | |
6224 | 6161 | マッチ演算子です。 |
6225 | L<perlop | |
6162 | L<perlop> を参照してください。 | |
6226 | 6163 | |
6227 | 6164 | =item map BLOCK LIST |
6228 | 6165 | X<map> |
6229 | 6166 | |
6230 | 6167 | =item map EXPR,LIST |
6231 | 6168 | |
6232 | 6169 | =begin original |
6233 | 6170 | |
6234 | 6171 | Evaluates the BLOCK or EXPR for each element of LIST (locally setting |
6235 | 6172 | C<$_> to each element) and returns the list value composed of the |
6236 | 6173 | results of each such evaluation. In scalar context, returns the |
6237 | 6174 | total number of elements so generated. Evaluates BLOCK or EXPR in |
6238 | 6175 | list context, so each element of LIST may produce zero, one, or |
6239 | 6176 | more elements in the returned value. |
6240 | 6177 | |
6241 | 6178 | =end original |
6242 | 6179 | |
6243 | 6180 | LIST の個々の要素に対して、BLOCK か EXPR を評価し |
6244 | 6181 | (C<$_> は、ローカルに個々の要素が設定されます) 、 |
6245 | 6182 | それぞれの評価結果からなるリスト値が返されます。 |
6246 | 6183 | スカラコンテキストでは、生成された要素の数を返します。 |
6247 | 6184 | BLOCK や EXPR をリストコンテキストで評価しますので、LIST の |
6248 | 6185 | 個々の要素によって作られる、返り値であるリストの要素数は、 |
6249 | 6186 | 0 個の場合もあれば、複数の場合もあります。 |
6250 | 6187 | |
6251 | 6188 | @chars = map(chr, @nums); |
6252 | 6189 | |
6253 | 6190 | =begin original |
6254 | 6191 | |
6255 | 6192 | translates a list of numbers to the corresponding characters. And |
6256 | 6193 | |
6257 | 6194 | =end original |
6258 | 6195 | |
6259 | 6196 | は、数のリストを対応する文字に変換します。 |
6260 | 6197 | また: |
6261 | 6198 | |
6262 | 6199 | %hash = map { get_a_key_for($_) => $_ } @array; |
6263 | 6200 | |
6264 | 6201 | =begin original |
6265 | 6202 | |
6266 | 6203 | is just a funny way to write |
6267 | 6204 | |
6268 | 6205 | =end original |
6269 | 6206 | |
6270 | 6207 | は以下のものをちょっと変わった書き方で書いたものです。 |
6271 | 6208 | |
6272 | 6209 | %hash = (); |
6273 | 6210 | foreach (@array) { |
6274 | 6211 | $hash{get_a_key_for($_)} = $_; |
6275 | 6212 | } |
6276 | 6213 | |
6277 | 6214 | =begin original |
6278 | 6215 | |
6279 | 6216 | Note that C<$_> is an alias to the list value, so it can be used to |
6280 | 6217 | modify the elements of the LIST. While this is useful and supported, |
6281 | 6218 | it can cause bizarre results if the elements of LIST are not variables. |
6282 | 6219 | Using a regular C<foreach> loop for this purpose would be clearer in |
6283 | 6220 | most cases. See also L</grep> for an array composed of those items of |
6284 | 6221 | the original list for which the BLOCK or EXPR evaluates to true. |
6285 | 6222 | |
6286 | 6223 | =end original |
6287 | 6224 | |
6288 | 6225 | C<$_> は、LIST の値へのエイリアスですので、LIST の要素を |
6289 | 6226 | 変更するために使うことができます。 |
6290 | 6227 | これは、便利でサポートされていますが、 |
6291 | 6228 | LIST の要素が変数でないと、おかしな結果になります。 |
6292 | 6229 | この目的には通常の C<foreach> ループを使うことで、ほとんどの場合は |
6293 | 6230 | より明確になります。 |
6294 | 6231 | BLOCK や EXPR が真になる元のリストの要素からなる配列については、 |
6295 | 6232 | L</grep> も参照してください。 |
6296 | 6233 | |
6297 | 6234 | =begin original |
6298 | 6235 | |
6299 | 6236 | If C<$_> is lexical in the scope where the C<map> appears (because it has |
6300 | 6237 | been declared with C<my $_>), then, in addition to being locally aliased to |
6301 | 6238 | the list elements, C<$_> keeps being lexical inside the block; that is, it |
6302 | 6239 | can't be seen from the outside, avoiding any potential side-effects. |
6303 | 6240 | |
6304 | 6241 | =end original |
6305 | 6242 | |
6306 | 6243 | (C<my $_> として宣言されることによって) C<$_> が C<map> が現れるスコープ内で |
6307 | 6244 | レキシカルな場合は、ローカルではリスト要素へのエイリアスであることに加えて、 |
6308 | 6245 | C<$_> はブロック内でレキシカルでありつづけます; つまり、外側からは見えず、 |
6309 | 6246 | 起こりうる副作用を回避します。 |
6310 | 6247 | |
6311 | 6248 | =begin original |
6312 | 6249 | |
6313 | 6250 | C<{> starts both hash references and blocks, so C<map { ...> could be either |
6314 | 6251 | the start of map BLOCK LIST or map EXPR, LIST. Because perl doesn't look |
6315 | 6252 | ahead for the closing C<}> it has to take a guess at which its dealing with |
6316 | 6253 | based what it finds just after the C<{>. Usually it gets it right, but if it |
6317 | 6254 | doesn't it won't realize something is wrong until it gets to the C<}> and |
6318 | 6255 | encounters the missing (or unexpected) comma. The syntax error will be |
6319 | 6256 | reported close to the C<}> but you'll need to change something near the C<{> |
6320 | 6257 | such as using a unary C<+> to give perl some help: |
6321 | 6258 | |
6322 | 6259 | =end original |
6323 | 6260 | |
6324 | 6261 | C<{> はハッシュリファレンスとブロックの両方の開始文字なので、 |
6325 | 6262 | C<map { ...> は map BLOCK LIST の場合と map EXPR, LIST の場合があります。 |
6326 | 6263 | perl は終了文字の C<}> を先読みしないので、C<{> の直後の文字を見て |
6327 | 6264 | どちらとして扱うかを推測します。 |
6328 | 6265 | 通常この推測は正しいですが、もし間違った場合は、C<}> まで読み込んで |
6329 | 6266 | カンマが足りない(または多い)ことがわかるまで、何かがおかしいことに |
6330 | 6267 | 気付きません。 |
6331 | 6268 | C<}> の近くで文法エラーが出ますが、perl を助けるために単項の C<+> を |
6332 | 6269 | 使うというように、C<{> の近くの何かを変更する必要があります。 |
6333 | 6270 | |
6334 | 6271 | %hash = map { "\L$_", 1 } @array # perl guesses EXPR. wrong |
6335 | 6272 | %hash = map { +"\L$_", 1 } @array # perl guesses BLOCK. right |
6336 | 6273 | %hash = map { ("\L$_", 1) } @array # this also works |
6337 | 6274 | %hash = map { lc($_), 1 } @array # as does this. |
6338 | 6275 | %hash = map +( lc($_), 1 ), @array # this is EXPR and works! |
6339 | 6276 | |
6340 | 6277 | %hash = map ( lc($_), 1 ), @array # evaluates to (1, @array) |
6341 | 6278 | |
6342 | 6279 | =begin original |
6343 | 6280 | |
6344 | 6281 | or to force an anon hash constructor use C<+{>: |
6345 | 6282 | |
6346 | 6283 | =end original |
6347 | 6284 | |
6348 | 6285 | または C<+{> を使って無名ハッシュコンストラクタを強制します: |
6349 | 6286 | |
6350 | 6287 | @hashes = map +{ lc($_), 1 }, @array # EXPR, so needs , at end |
6351 | 6288 | |
6352 | 6289 | =begin original |
6353 | 6290 | |
6354 | 6291 | and you get list of anonymous hashes each with only 1 entry. |
6355 | 6292 | |
6356 | 6293 | =end original |
6357 | 6294 | |
6358 | 6295 | こうするとそれぞれ 1 要素だけの無名ハッシュのリストを得られます。 |
6359 | 6296 | |
6360 | 6297 | =item mkdir FILENAME,MASK |
6361 | 6298 | X<mkdir> X<md> X<directory, create> |
6362 | 6299 | |
6363 | 6300 | =item mkdir FILENAME |
6364 | 6301 | |
6365 | 6302 | =item mkdir |
6366 | 6303 | |
6367 | 6304 | =begin original |
6368 | 6305 | |
6369 | 6306 | Creates the directory specified by FILENAME, with permissions |
6370 | 6307 | specified by MASK (as modified by C<umask>). If it succeeds it |
6371 | 6308 | returns true, otherwise it returns false and sets C<$!> (errno). |
6372 | 6309 | If omitted, MASK defaults to 0777. If omitted, FILENAME defaults |
6373 | 6310 | to C<$_>. |
6374 | 6311 | |
6375 | 6312 | =end original |
6376 | 6313 | |
6377 | 6314 | FILENAME で指定したディレクトリを、MASK で指定した許可モード(を |
6378 | 6315 | C<umask> で修正したもの) で作成します。 |
6379 | 6316 | 成功時には真を返し、失敗時には偽を返して C<$!> (errno) を設定します。 |
6380 | 6317 | MASK を省略すると、0777 とみなします。 |
6381 | 6318 | FILENAME を省略すると、C<$_> を使います。 |
6382 | 6319 | |
6383 | 6320 | =begin original |
6384 | 6321 | |
6385 | 6322 | In general, it is better to create directories with permissive MASK, |
6386 | 6323 | and let the user modify that with their C<umask>, than it is to supply |
6387 | 6324 | a restrictive MASK and give the user no way to be more permissive. |
6388 | 6325 | The exceptions to this rule are when the file or directory should be |
6389 | 6326 | kept private (mail files, for instance). The perlfunc(1) entry on |
6390 | 6327 | C<umask> discusses the choice of MASK in more detail. |
6391 | 6328 | |
6392 | 6329 | =end original |
6393 | 6330 | |
6394 | 6331 | 一般的に、制限された MASK を使ってユーザーがより寛容にする方法を |
6395 | 6332 | 与えないより、寛容な MASK でディレクトリを作り、ユーザーが自身の C<umask> で |
6396 | 6333 | 修正するようにした方がよいです。 |
6397 | 6334 | 例外は、(例えばメールファイルのような)プライベートに保つべきファイルや |
6398 | 6335 | ディレクトリを書く場合です。 |
6399 | 6336 | perlfunc(1) の C<umask> で、MASK の選択に関して詳細に議論しています。 |
6400 | 6337 | |
6401 | 6338 | =begin original |
6402 | 6339 | |
6403 | 6340 | Note that according to the POSIX 1003.1-1996 the FILENAME may have any |
6404 | 6341 | number of trailing slashes. Some operating and filesystems do not get |
6405 | 6342 | this right, so Perl automatically removes all trailing slashes to keep |
6406 | 6343 | everyone happy. |
6407 | 6344 | |
6408 | 6345 | =end original |
6409 | 6346 | |
6410 | 6347 | POSIX 1003.1-1996 によれば、FILENAME には末尾に任意の数のスラッシュを |
6411 | 6348 | つけることができます。 |
6412 | 6349 | このようには動かない OS やファイルシステムもあるので、Perl はみんなが |
6413 | 6350 | 幸せになれるように、自動的に末尾のスラッシュを削除します。 |
6414 | 6351 | |
6415 | 6352 | =begin original |
6416 | 6353 | |
6417 | 6354 | In order to recursively create a directory structure look at |
6418 | 6355 | the C<mkpath> function of the L<File::Path> module. |
6419 | 6356 | |
6420 | 6357 | =end original |
6421 | 6358 | |
6422 | 6359 | ディレクトリ構造を再帰的に作成するには、L<File::Path> モジュールの |
6423 | 6360 | C<makepath> 関数を参照してください。 |
6424 | 6361 | |
6425 | 6362 | =item msgctl ID,CMD,ARG |
6426 | 6363 | X<msgctl> |
6427 | 6364 | |
6428 | 6365 | =begin original |
6429 | 6366 | |
6430 | 6367 | Calls the System V IPC function msgctl(2). You'll probably have to say |
6431 | 6368 | |
6432 | 6369 | =end original |
6433 | 6370 | |
6434 | 6371 | System V IPC 関数 msgctl を呼び出します。正しい定数定義を得るために、まず |
6435 | 6372 | |
6436 | 6373 | use IPC::SysV; |
6437 | 6374 | |
6438 | 6375 | =begin original |
6439 | 6376 | |
6440 | 6377 | first to get the correct constant definitions. If CMD is C<IPC_STAT>, |
6441 | 6378 | then ARG must be a variable that will hold the returned C<msqid_ds> |
6442 | 6379 | structure. Returns like C<ioctl>: the undefined value for error, |
6443 | 6380 | C<"0 but true"> for zero, or the actual return value otherwise. See also |
6444 | 6381 | L<perlipc/"SysV IPC">, C<IPC::SysV>, and C<IPC::Semaphore> documentation. |
6445 | 6382 | |
6446 | 6383 | =end original |
6447 | 6384 | |
6448 | 6385 | と宣言する必要があるでしょう。 |
6449 | 6386 | CMD が C<IPC_STAT> であれば、ARG は返される C<msqid_ds> 構造体を |
6450 | 6387 | 納める変数でなければなりません。 |
6451 | 6388 | C<ioctl> と同じように、エラー時には未定義値、 |
6452 | 6389 | ゼロのときは C<"0 but true">、それ以外なら、その値そのものを返します。 |
6453 | 6390 | L<perlipc/"SysV IPC">, C<IPC::SysV>, C<IPC::Semaphore> も参照してください。 |
6454 | 6391 | |
6455 | 6392 | =item msgget KEY,FLAGS |
6456 | 6393 | X<msgget> |
6457 | 6394 | |
6458 | 6395 | =begin original |
6459 | 6396 | |
6460 | 6397 | Calls the System V IPC function msgget(2). Returns the message queue |
6461 | 6398 | id, or the undefined value if there is an error. See also |
6462 | 6399 | L<perlipc/"SysV IPC"> and C<IPC::SysV> and C<IPC::Msg> documentation. |
6463 | 6400 | |
6464 | 6401 | =end original |
6465 | 6402 | |
6466 | 6403 | System V IPC 関数 msgget を呼び出します。 |
6467 | 6404 | メッセージキューの ID か、エラー時には未定義値を返します。 |
6468 | 6405 | L<perlipc/"SysV IPC">, C<IPC::SysV>, C<IPC::Msg> も参照してください。 |
6469 | 6406 | |
6470 | 6407 | =item msgrcv ID,VAR,SIZE,TYPE,FLAGS |
6471 | 6408 | X<msgrcv> |
6472 | 6409 | |
6473 | 6410 | =begin original |
6474 | 6411 | |
6475 | 6412 | Calls the System V IPC function msgrcv to receive a message from |
6476 | 6413 | message queue ID into variable VAR with a maximum message size of |
6477 | 6414 | SIZE. Note that when a message is received, the message type as a |
6478 | 6415 | native long integer will be the first thing in VAR, followed by the |
6479 | 6416 | actual message. This packing may be opened with C<unpack("l! a*")>. |
6480 | 6417 | Taints the variable. Returns true if successful, or false if there is |
6481 | 6418 | an error. See also L<perlipc/"SysV IPC">, C<IPC::SysV>, and |
6482 | 6419 | C<IPC::SysV::Msg> documentation. |
6483 | 6420 | |
6484 | 6421 | =end original |
6485 | 6422 | |
6486 | 6423 | System V IPC 関数 msgrcv を呼び出し、メッセージキュー ID から、 |
6487 | 6424 | 変数 VAR に最大メッセージ長 SIZE のメッセージを受信します。 |
6488 | 6425 | メッセージが受信された時、ネイティブな long 整数のメッセージタイプが |
6489 | 6426 | VAR の先頭となり、実際のメッセージが続きます。 |
6490 | 6427 | このパッキングは C<unpack("l! a*")> で展開できます。 |
6491 | 6428 | 変数は汚染されます。 |
6492 | 6429 | 成功時には真を返し、エラー時には偽を返します。 |
6493 | 6430 | L<perlipc/"SysV IPC">, C<IPC::SysV>, C<IPC::SysV::Msg> も参照してください。 |
6494 | 6431 | |
6495 | 6432 | =item msgsnd ID,MSG,FLAGS |
6496 | 6433 | X<msgsnd> |
6497 | 6434 | |
6498 | 6435 | =begin original |
6499 | 6436 | |
6500 | 6437 | Calls the System V IPC function msgsnd to send the message MSG to the |
6501 | 6438 | message queue ID. MSG must begin with the native long integer message |
6502 | 6439 | type, and be followed by the length of the actual message, and finally |
6503 | 6440 | the message itself. This kind of packing can be achieved with |
6504 | 6441 | C<pack("l! a*", $type, $message)>. Returns true if successful, |
6505 | 6442 | or false if there is an error. See also C<IPC::SysV> |
6506 | 6443 | and C<IPC::SysV::Msg> documentation. |
6507 | 6444 | |
6508 | 6445 | =end original |
6509 | 6446 | |
6510 | 6447 | System V IPC 関数 msgsnd を呼び出し、メッセージキュー ID に |
6511 | 6448 | メッセージ MSG を送信します。 |
6512 | 6449 | MSG の先頭は、ネイティブなlong 整数のメッセージタイプでなければならず、 |
6513 | 6450 | メッセージの長さ、メッセージ本体と続きます。 |
6514 | 6451 | これは、C<pack("l! a*", $type, $message)> として生成できます。 |
6515 | 6452 | 成功時には真を、エラー時には偽を返します。 |
6516 | 6453 | C<IPC::SysV> と C<IPC::SysV::Msg> も参照してください。 |
6517 | 6454 | |
6518 | 6455 | =item my EXPR |
6519 | 6456 | X<my> |
6520 | 6457 | |
6521 | 6458 | =item my TYPE EXPR |
6522 | 6459 | |
6523 | 6460 | =item my EXPR : ATTRS |
6524 | 6461 | |
6525 | 6462 | =item my TYPE EXPR : ATTRS |
6526 | 6463 | |
6527 | 6464 | =begin original |
6528 | 6465 | |
6529 | 6466 | A C<my> declares the listed variables to be local (lexically) to the |
6530 | 6467 | enclosing block, file, or C<eval>. If more than one value is listed, |
6531 | 6468 | the list must be placed in parentheses. |
6532 | 6469 | |
6533 | 6470 | =end original |
6534 | 6471 | |
6535 | 6472 | C<my> はリストアップされた変数を、囲っているブロック、ファイル、 |
6536 | 6473 | C<eval> の中でローカルな (レキシカルな) ものにします。 |
6537 | 6474 | 複数の値を並べる場合には、括弧で括る必要があります。 |
6538 | 6475 | |
6539 | 6476 | =begin original |
6540 | 6477 | |
6541 | 6478 | The exact semantics and interface of TYPE and ATTRS are still |
6542 | 6479 | evolving. TYPE is currently bound to the use of C<fields> pragma, |
6543 | 6480 | and attributes are handled using the C<attributes> pragma, or starting |
6544 | 6481 | from Perl 5.8.0 also via the C<Attribute::Handlers> module. See |
6545 | 6482 | L<perlsub/"Private Variables via my()"> for details, and L<fields>, |
6546 | 6483 | L<attributes>, and L<Attribute::Handlers>. |
6547 | 6484 | |
6548 | 6485 | =end original |
6549 | 6486 | |
6550 | 6487 | TYPE と ATTRS の正確な文法とインターフェースは今でも進化しています。 |
6551 | 6488 | 現在のところ、TYPE は C<fields> プラグマの使用と結び付けられていて、 |
6552 | 6489 | 属性は C<attributes> プラグマか、Perl 5.8.0 からは |
6553 | 6490 | C<Attribute::Handlers> モジュールと結び付けられています。 |
6554 | 6491 | 詳しくはL<perlsub/"Private Variables via my()">, L<fields>, |
6555 | 6492 | L<attributes>, L<Attribute::Handlers> を参照してください。 |
6556 | 6493 | |
6557 | 6494 | =item next LABEL |
6558 | 6495 | X<next> X<continue> |
6559 | 6496 | |
6560 | 6497 | =item next |
6561 | 6498 | |
6562 | 6499 | =begin original |
6563 | 6500 | |
6564 | 6501 | The C<next> command is like the C<continue> statement in C; it starts |
6565 | 6502 | the next iteration of the loop: |
6566 | 6503 | |
6567 | 6504 | =end original |
6568 | 6505 | |
6569 | 6506 | C<next> コマンドは、C での C<continue> 文のようなもので、 |
6570 | 6507 | ループの次の繰り返しを開始します: |
6571 | 6508 | |
6572 | 6509 | LINE: while (<STDIN>) { |
6573 | 6510 | next LINE if /^#/; # discard comments |
6574 | 6511 | #... |
6575 | 6512 | } |
6576 | 6513 | |
6577 | 6514 | =begin original |
6578 | 6515 | |
6579 | 6516 | Note that if there were a C<continue> block on the above, it would get |
6580 | 6517 | executed even on discarded lines. If the LABEL is omitted, the command |
6581 | 6518 | refers to the innermost enclosing loop. |
6582 | 6519 | |
6583 | 6520 | =end original |
6584 | 6521 | |
6585 | 6522 | C<continue> ブロックが存在すれば、たとえ捨てられる行に |
6586 | 6523 | あっても、それが実行されます。 |
6587 | 6524 | LABEL が省略されると、このコマンドはもっとも内側のループを参照します。 |
6588 | 6525 | |
6589 | 6526 | =begin original |
6590 | 6527 | |
6591 | 6528 | C<next> cannot be used to exit a block which returns a value such as |
6592 | 6529 | C<eval {}>, C<sub {}> or C<do {}>, and should not be used to exit |
6593 | 6530 | a grep() or map() operation. |
6594 | 6531 | |
6595 | 6532 | =end original |
6596 | 6533 | |
6597 | 6534 | C<next> は C<eval {}>, C<sub {}>, C<do {}> のように値を返す |
6598 | 6535 | ブロックから抜けるのには使えません。 |
6599 | 6536 | また、grep() や map() 操作から抜けるのに使うべきではありません。 |
6600 | 6537 | |
6601 | 6538 | =begin original |
6602 | 6539 | |
6603 | 6540 | Note that a block by itself is semantically identical to a loop |
6604 | 6541 | that executes once. Thus C<next> will exit such a block early. |
6605 | 6542 | |
6606 | 6543 | =end original |
6607 | 6544 | |
6608 | 6545 | ブロック自身は一回だけ実行されるループと文法的に同一であることに |
6609 | 6546 | 注意してください。 |
6610 | 6547 | 従って、C<next> はそのようなブロックから早く抜けるのに使えます。 |
6611 | 6548 | |
6612 | 6549 | =begin original |
6613 | 6550 | |
6614 | 6551 | See also L</continue> for an illustration of how C<last>, C<next>, and |
6615 | 6552 | C<redo> work. |
6616 | 6553 | |
6617 | 6554 | =end original |
6618 | 6555 | |
6619 | 6556 | C<last>, C<next>, C<redo> がどのように働くかについては |
6620 | 6557 | L</continue> も参照して下さい。 |
6621 | 6558 | |
6622 | 6559 | =item no Module VERSION LIST |
6623 | 6560 | X<no> |
6624 | 6561 | |
6625 | 6562 | =item no Module VERSION |
6626 | 6563 | |
6627 | 6564 | =item no Module LIST |
6628 | 6565 | |
6629 | 6566 | =item no Module |
6630 | 6567 | |
6631 | 6568 | =item no VERSION |
6632 | 6569 | |
6633 | 6570 | =begin original |
6634 | 6571 | |
6635 | 6572 | See the C<use> function, of which C<no> is the opposite. |
6636 | 6573 | |
6637 | 6574 | =end original |
6638 | 6575 | |
6639 | 6576 | L<use> 関数を参照してください。C<no> は、その逆を行なうものです。 |
6640 | 6577 | |
6641 | 6578 | =item oct EXPR |
6642 | 6579 | X<oct> X<octal> X<hex> X<hexadecimal> X<binary> X<bin> |
6643 | 6580 | |
6644 | 6581 | =item oct |
6645 | 6582 | |
6646 | 6583 | =begin original |
6647 | 6584 | |
6648 | 6585 | Interprets EXPR as an octal string and returns the corresponding |
6649 | 6586 | value. (If EXPR happens to start off with C<0x>, interprets it as a |
6650 | 6587 | hex string. If EXPR starts off with C<0b>, it is interpreted as a |
6651 | 6588 | binary string. Leading whitespace is ignored in all three cases.) |
6652 | 6589 | The following will handle decimal, binary, octal, and hex in the standard |
6653 | 6590 | Perl or C notation: |
6654 | 6591 | |
6655 | 6592 | =end original |
6656 | 6593 | |
6657 | 6594 | EXPR を 8 進数文字列と解釈して、対応する値を返します。 |
6658 | 6595 | (EXPR が C<0x> で始まるときには、16 進数文字列と解釈します。 |
6659 | 6596 | EXPR が C<0b>で始まるときは、2 進数文字列と解釈します。 |
6660 | 6597 | どの場合でも、先頭の空白は無視されます。) |
6661 | 6598 | 以下の例は、標準的な Perl や C の記法での |
6662 | 6599 | 10 進数、2 進数、8 進数、16 進数を扱います: |
6663 | 6600 | |
6664 | 6601 | $val = oct($val) if $val =~ /^0/; |
6665 | 6602 | |
6666 | 6603 | =begin original |
6667 | 6604 | |
6668 | 6605 | If EXPR is omitted, uses C<$_>. To go the other way (produce a number |
6669 | 6606 | in octal), use sprintf() or printf(): |
6670 | 6607 | |
6671 | 6608 | =end original |
6672 | 6609 | |
6673 | 6610 | EXPR を省略すると、C<$_> を使用します。 |
6674 | 6611 | (8 進数を扱う)その他の方法としては sprintf() または printf()があります。 |
6675 | 6612 | |
6676 | 6613 | $perms = (stat("filename"))[2] & 07777; |
6677 | 6614 | $oct_perms = sprintf "%lo", $perms; |
6678 | 6615 | |
6679 | 6616 | =begin original |
6680 | 6617 | |
6681 | 6618 | The oct() function is commonly used when a string such as C<644> needs |
6682 | 6619 | to be converted into a file mode, for example. (Although perl will |
6683 | 6620 | automatically convert strings into numbers as needed, this automatic |
6684 | 6621 | conversion assumes base 10.) |
6685 | 6622 | |
6686 | 6623 | =end original |
6687 | 6624 | |
6688 | 6625 | oct() 関数は例えば、 C<644> といった文字列をファイルモードに変換する時に |
6689 | 6626 | よく使います。 |
6690 | 6627 | (Perl は必要に応じて自動的に文字列を数値に変換しますが、 |
6691 | 6628 | この自動変換は十進数を仮定します。) |
6692 | 6629 | |
6693 | 6630 | =item open FILEHANDLE,EXPR |
6694 | 6631 | X<open> X<pipe> X<file, open> X<fopen> |
6695 | 6632 | |
6696 | 6633 | =item open FILEHANDLE,MODE,EXPR |
6697 | 6634 | |
6698 | 6635 | =item open FILEHANDLE,MODE,EXPR,LIST |
6699 | 6636 | |
6700 | 6637 | =item open FILEHANDLE,MODE,REFERENCE |
6701 | 6638 | |
6702 | 6639 | =item open FILEHANDLE |
6703 | 6640 | |
6704 | 6641 | =begin original |
6705 | 6642 | |
6706 | 6643 | Opens the file whose filename is given by EXPR, and associates it with |
6707 | 6644 | FILEHANDLE. |
6708 | 6645 | |
6709 | 6646 | =end original |
6710 | 6647 | |
6711 | 6648 | EXPR で与えられたファイル名のファイルを開き、FILEHANDLE と結び付けます。 |
6712 | 6649 | |
6713 | 6650 | =begin original |
6714 | 6651 | |
6715 | Simple examples to open a file for reading: | |
6716 | ||
6717 | =end original | |
6718 | ||
6719 | 読み込みのためにファイルを開くための簡単な例は以下のもので: | |
6720 | ||
6721 | open(my $fh, '<', "input.txt") or die $!; | |
6722 | ||
6723 | =begin original | |
6724 | ||
6725 | and for writing: | |
6726 | ||
6727 | =end original | |
6728 | ||
6729 | 書き込み用は以下のものです: | |
6730 | ||
6731 | open(my $fh, '>', "output.txt") or die $!; | |
6732 | ||
6733 | =begin original | |
6734 | ||
6735 | 6652 | (The following is a comprehensive reference to open(): for a gentler |
6736 | 6653 | introduction you may consider L<perlopentut>.) |
6737 | 6654 | |
6738 | 6655 | =end original |
6739 | 6656 | |
6740 | 6657 | (以下は総合的な open() のリファレンスです: より親切な説明については |
6741 | 6658 | L<perlopentut> を参照してください。) |
6742 | 6659 | |
6743 | 6660 | =begin original |
6744 | 6661 | |
6745 | 6662 | If FILEHANDLE is an undefined scalar variable (or array or hash element) |
6746 | 6663 | the variable is assigned a reference to a new anonymous filehandle, |
6747 | 6664 | otherwise if FILEHANDLE is an expression, its value is used as the name of |
6748 | 6665 | the real filehandle wanted. (This is considered a symbolic reference, so |
6749 | 6666 | C<use strict 'refs'> should I<not> be in effect.) |
6750 | 6667 | |
6751 | 6668 | =end original |
6752 | 6669 | |
6753 | 6670 | FILEHANDLE が未定義のスカラ変数(または配列かハッシュの要素)の場合、その |
6754 | 6671 | 変数は新しい無名ファイルハンドルへのリファレンスが代入され、 |
6755 | 6672 | さもなければ、もし FILEHANDLE が式なら、その値を求めている実際の |
6756 | 6673 | ファイルハンドルの名前として使います。 |
6757 | 6674 | (これはシンボリックリファレンスとして扱われるので、 |
6758 | 6675 | C<use strict 'refs'> の影響を I<受けません>。) |
6759 | 6676 | |
6760 | 6677 | =begin original |
6761 | 6678 | |
6762 | 6679 | If EXPR is omitted, the scalar variable of the same name as the |
6763 | 6680 | FILEHANDLE contains the filename. (Note that lexical variables--those |
6764 | 6681 | declared with C<my>--will not work for this purpose; so if you're |
6765 | 6682 | using C<my>, specify EXPR in your call to open.) |
6766 | 6683 | |
6767 | 6684 | =end original |
6768 | 6685 | |
6769 | 6686 | EXPR が省略された場合、FILEHANDLE と同じ名前のスカラ変数にファイル名が |
6770 | 6687 | 入っています。 |
6771 | 6688 | (レキシカル変数 -- C<my> で宣言されたもの -- はこの用途には使えないことに |
6772 | 6689 | 注意してください; 従って、C<my> を使っている場合は、open を呼び出すときに |
6773 | 6690 | EXPR を指定してください。) |
6774 | 6691 | |
6775 | 6692 | =begin original |
6776 | 6693 | |
6777 | 6694 | If three or more arguments are specified then the mode of opening and |
6778 | 6695 | the file name are separate. If MODE is C<< '<' >> or nothing, the file |
6779 | 6696 | is opened for input. If MODE is C<< '>' >>, the file is truncated and |
6780 | 6697 | opened for output, being created if necessary. If MODE is C<<< '>>' >>>, |
6781 | 6698 | the file is opened for appending, again being created if necessary. |
6782 | 6699 | |
6783 | 6700 | =end original |
6784 | 6701 | |
6785 | 6702 | 3 以上の引数が指定された場合、開く時のモードとファイル名は分離されます。 |
6786 | 6703 | MODE が C<< '<' >> か空の場合、ファイルは入力用に開かれます。 |
6787 | 6704 | MODE が C<< '>' >> の場合、ファイルは切り詰められて出力用に開かれ、 |
6788 | 6705 | 必要なら作成されます。 |
6789 | 6706 | MODE が C<<< '>>' >>> の場合、ファイルは追加用に開かれ、やはり必要なら |
6790 | 6707 | 作成されます。 |
6791 | 6708 | |
6792 | 6709 | =begin original |
6793 | 6710 | |
6794 | 6711 | You can put a C<'+'> in front of the C<< '>' >> or C<< '<' >> to |
6795 | 6712 | indicate that you want both read and write access to the file; thus |
6796 | 6713 | C<< '+<' >> is almost always preferred for read/write updates--the C<< |
6797 | 6714 | '+>' >> mode would clobber the file first. You can't usually use |
6798 | 6715 | either read-write mode for updating textfiles, since they have |
6799 | 6716 | variable length records. See the B<-i> switch in L<perlrun> for a |
6800 | 6717 | better approach. The file is created with permissions of C<0666> |
6801 | 6718 | modified by the process' C<umask> value. |
6802 | 6719 | |
6803 | 6720 | =end original |
6804 | 6721 | |
6805 | 6722 | ファイルに読み込みアクセスと書き込みアクセスの両方をしたいことを示すために、 |
6806 | 6723 | C<< '>' >> や C<< '<' >> の前に C<'+'> を付けることができます: |
6807 | 6724 | 従って、ほとんど常に C<< '+<' >> が読み書き更新のために使われます -- |
6808 | 6725 | C<< '+>' >> モードはまずファイルを上書きします。 |
6809 | 6726 | 普通はこれらの読み書きモードをテキストファイルの更新のためには使えません; |
6810 | 6727 | なぜなら可変長のレコードで構成されているからです。 |
6811 | 6728 | よりよい手法については L<perlrun> の B<-i> オプションを参照してください。 |
6812 | 6729 | ファイルは C<0666> をプロセスの C<umask> 値で修正したパーミッションで |
6813 | 6730 | 作成されます。 |
6814 | 6731 | |
6815 | 6732 | =begin original |
6816 | 6733 | |
6817 | 6734 | These various prefixes correspond to the fopen(3) modes of C<'r'>, |
6818 | 6735 | C<'r+'>, C<'w'>, C<'w+'>, C<'a'>, and C<'a+'>. |
6819 | 6736 | |
6820 | 6737 | =end original |
6821 | 6738 | |
6822 | 6739 | これらの様々な前置詞は fopen(3) の C<'r'>, C<'r+'>, |
6823 | 6740 | C<'w'>, C<'w+'>, C<'a'>, C<'a+'> のモードに対応します。 |
6824 | 6741 | |
6825 | 6742 | =begin original |
6826 | 6743 | |
6827 | 6744 | In the 2-arguments (and 1-argument) form of the call the mode and |
6828 | 6745 | filename should be concatenated (in this order), possibly separated by |
6829 | 6746 | spaces. It is possible to omit the mode in these forms if the mode is |
6830 | 6747 | C<< '<' >>. |
6831 | 6748 | |
6832 | 6749 | =end original |
6833 | 6750 | |
6834 | 6751 | 2 引数(と 1 引数) の形式ではモードとファイル名は(この順番で) |
6835 | 6752 | 結合されます(空白によって分割されているかもしれません)。 |
6836 | 6753 | この形式で、モードが C<< '<' >> の場合はモードを省略できます。 |
6837 | 6754 | |
6838 | 6755 | =begin original |
6839 | 6756 | |
6840 | 6757 | If the filename begins with C<'|'>, the filename is interpreted as a |
6841 | 6758 | command to which output is to be piped, and if the filename ends with a |
6842 | 6759 | C<'|'>, the filename is interpreted as a command which pipes output to |
6843 | 6760 | us. See L<perlipc/"Using open() for IPC"> |
6844 | 6761 | for more examples of this. (You are not allowed to C<open> to a command |
6845 | 6762 | that pipes both in I<and> out, but see L<IPC::Open2>, L<IPC::Open3>, |
6846 | 6763 | and L<perlipc/"Bidirectional Communication with Another Process"> |
6847 | 6764 | for alternatives.) |
6848 | 6765 | |
6849 | 6766 | =end original |
6850 | 6767 | |
6851 | 6768 | ファイル名の先頭に C<'|'> を付けると、 |
6852 | 6769 | そのファイル名をコマンドとして解釈し、ファイルハンドルへの |
6853 | 6770 | 出力がパイプを通じて、そのコマンドへ入力されます。 |
6854 | 6771 | 逆にファイル名の最後に C<'|'> を付けた場合には、 |
6855 | 6772 | 同様にファイル名をコマンドと解釈し、そのコマンドの出力が |
6856 | 6773 | パイプを通じて、ファイルハンドルから入力として |
6857 | 6774 | 読み込むことができるようになります。 |
6858 | 6775 | これに関する例については L<perlipc/"Using open() for IPC"> を参照してください。 |
6859 | 6776 | (C<open> を入出力両用にパイプすることは出来ませんが |
6860 | 6777 | 代替案としては L<IPC::Open2>, L<IPC::Open3>, |
6861 | 6778 | L<perlipc/"Bidirectional Communication with Another Process"> |
6862 | 6779 | を参照してください。) |
6863 | 6780 | |
6864 | 6781 | =begin original |
6865 | 6782 | |
6866 | 6783 | For three or more arguments if MODE is C<'|-'>, the filename is |
6867 | 6784 | interpreted as a command to which output is to be piped, and if MODE |
6868 | 6785 | is C<'-|'>, the filename is interpreted as a command which pipes |
6869 | 6786 | output to us. In the 2-arguments (and 1-argument) form one should |
6870 | 6787 | replace dash (C<'-'>) with the command. |
6871 | 6788 | See L<perlipc/"Using open() for IPC"> for more examples of this. |
6872 | 6789 | (You are not allowed to C<open> to a command that pipes both in I<and> |
6873 | 6790 | out, but see L<IPC::Open2>, L<IPC::Open3>, and |
6874 | 6791 | L<perlipc/"Bidirectional Communication"> for alternatives.) |
6875 | 6792 | |
6876 | 6793 | =end original |
6877 | 6794 | |
6878 | 6795 | 3 引数以上の形式で |
6879 | 6796 | MODE が C<'|-'> の場合、ファイル名は出力がパイプされるコマンドとして |
6880 | 6797 | 解釈され、MODE が C<'-|'> の場合、ファイル名は出力がこちらに |
6881 | 6798 | パイプされるコマンドとして解釈されます。 |
6882 | 6799 | 2 引数(と 1 引数) の形式ではハイフン(C<'-'>)をコマンドの代わりに |
6883 | 6800 | 使えます。 |
6884 | 6801 | これに関するさらなる例については L<perlipc/"Using open() for IPC"> を |
6885 | 6802 | 参照してください。 |
6886 | 6803 | (C<open> を入出力 I<両用> にパイプすることは出来ませんが |
6887 | 6804 | 代替案としては L<IPC::Open2>, L<IPC::Open3>, |
6888 | 6805 | L<perlipc/"Bidirectional Communication"> を参照してください。) |
6889 | 6806 | |
6890 | 6807 | =begin original |
6891 | 6808 | |
6892 | 6809 | In the three-or-more argument form of pipe opens, if LIST is specified |
6893 | 6810 | (extra arguments after the command name) then LIST becomes arguments |
6894 | 6811 | to the command invoked if the platform supports it. The meaning of |
6895 | 6812 | C<open> with more than three arguments for non-pipe modes is not yet |
6896 | 6813 | specified. Experimental "layers" may give extra LIST arguments |
6897 | 6814 | meaning. |
6898 | 6815 | |
6899 | 6816 | =end original |
6900 | 6817 | |
6901 | 6818 | パイプでの 3 つ以上の引数の形式では、LIST (コマンド名の後の追加の引数) が |
6902 | 6819 | 指定されると、プラットフォームが対応していれば、LIST は起動される |
6903 | 6820 | コマンドへの引数となります。 |
6904 | 6821 | パイプモードではない C<open> での 3 つ以上の引数の意味はまだ未定義です。 |
6905 | 6822 | 実験的な「層」は追加の LIST 引数の意味を与えます。 |
6906 | 6823 | |
6907 | 6824 | =begin original |
6908 | 6825 | |
6909 | 6826 | In the 2-arguments (and 1-argument) form opening C<'-'> opens STDIN |
6910 | 6827 | and opening C<< '>-' >> opens STDOUT. |
6911 | 6828 | |
6912 | 6829 | =end original |
6913 | 6830 | |
6914 | 6831 | 2 引数(と 1 引数)で C<'-'> を open すると STDIN がオープンされ、 |
6915 | 6832 | C<< '>-' >> を open すると STDOUT がオープンされます。 |
6916 | 6833 | |
6917 | 6834 | =begin original |
6918 | 6835 | |
6919 | 6836 | You may use the three-argument form of open to specify IO "layers" |
6920 | 6837 | (sometimes also referred to as "disciplines") to be applied to the handle |
6921 | 6838 | that affect how the input and output are processed (see L<open> and |
6922 | 6839 | L<PerlIO> for more details). For example |
6923 | 6840 | |
6924 | 6841 | =end original |
6925 | 6842 | |
6926 | 6843 | open の 3 引数形式では、どのように入出力が処理されるかに影響を与える |
6927 | 6844 | IO 「層」(「ディシプリン」とも呼ばれます)を指定できます |
6928 | 6845 | (詳細については L<open> と L<PerlIO> を参照してください)。 |
6929 | 6846 | 例えば: |
6930 | 6847 | |
6931 | open( | |
6848 | open(FH, "<:encoding(UTF-8)", "file") | |
6932 | 6849 | |
6933 | 6850 | =begin original |
6934 | 6851 | |
6935 | 6852 | will open the UTF-8 encoded file containing Unicode characters, |
6936 | 6853 | see L<perluniintro>. Note that if layers are specified in the |
6937 | 6854 | three-arg form then default layers stored in ${^OPEN} (see L<perlvar>; |
6938 | 6855 | usually set by the B<open> pragma or the switch B<-CioD>) are ignored. |
6939 | 6856 | |
6940 | 6857 | =end original |
6941 | 6858 | |
6942 | 6859 | は、Unicode 文字を含む UTF-8 エンコードされたファイルを開きます; |
6943 | 6860 | L<perluniintro> を参照してください。 |
6944 | 6861 | 3 引数形式で層を指定すると、${^OPEN} (L<perlvar> を参照してください; |
6945 | 6862 | 通常はC<open> プラグマか B<-CioD> オプションでセットされます) |
6946 | 6863 | に保存されたデフォルト層は無視されることに注意してください。 |
6947 | 6864 | |
6948 | 6865 | =begin original |
6949 | 6866 | |
6950 | 6867 | Open returns nonzero upon success, the undefined value otherwise. If |
6951 | 6868 | the C<open> involved a pipe, the return value happens to be the pid of |
6952 | 6869 | the subprocess. |
6953 | 6870 | |
6954 | 6871 | =end original |
6955 | 6872 | |
6956 | 6873 | open は、成功時にはゼロ以外を返し、失敗時には未定義値を返します。 |
6957 | 6874 | パイプに関る C<open> のときには、返り値はサブプロセスの pid となります。 |
6958 | 6875 | |
6959 | 6876 | =begin original |
6960 | 6877 | |
6961 | 6878 | If you're running Perl on a system that distinguishes between text |
6962 | 6879 | files and binary files, then you should check out L</binmode> for tips |
6963 | 6880 | for dealing with this. The key distinction between systems that need |
6964 | 6881 | C<binmode> and those that don't is their text file formats. Systems |
6965 | 6882 | like Unix, Mac OS, and Plan 9, which delimit lines with a single |
6966 | 6883 | character, and which encode that character in C as C<"\n">, do not |
6967 | 6884 | need C<binmode>. The rest need it. |
6968 | 6885 | |
6969 | 6886 | =end original |
6970 | 6887 | |
6971 | 6888 | テキストファイルとバイナリファイルを区別するシステムで Perl を実行している |
6972 | 6889 | 場合、これを扱うための小技のために L</binmode> をチェックするべきです。 |
6973 | 6890 | 動作させているシステムで C<binmode> が必要か不要化を区別する鍵は、テキスト |
6974 | 6891 | ファイルの形式です。 |
6975 | 6892 | Unix, Mac OS, Plan 9 といった、行の境界を 1 文字で表現し、それが C では |
6976 | 6893 | C<"\n"> でエンコードされる場合、C<binmode> は不要です。 |
6977 | 6894 | それ以外では必要です。 |
6978 | 6895 | |
6979 | 6896 | =begin original |
6980 | 6897 | |
6981 | 6898 | When opening a file, it's usually a bad idea to continue normal execution |
6982 | 6899 | if the request failed, so C<open> is frequently used in connection with |
6983 | 6900 | C<die>. Even if C<die> won't do what you want (say, in a CGI script, |
6984 | 6901 | where you want to make a nicely formatted error message (but there are |
6985 | 6902 | modules that can help with that problem)) you should always check |
6986 | 6903 | the return value from opening a file. The infrequent exception is when |
6987 | 6904 | working with an unopened filehandle is actually what you want to do. |
6988 | 6905 | |
6989 | 6906 | =end original |
6990 | 6907 | |
6991 | 6908 | ファイルを開く時、開くのに失敗した時に通常の処理を続けるのは |
6992 | 6909 | 普通は悪い考えですので、C<open> はしばしば C<die> と結び付けられて |
6993 | 6910 | 使われます。 |
6994 | 6911 | 望むものが C<die> でない場合(例えば、CGI スクリプト のように |
6995 | 6912 | きれいにフォーマットされたエラーメッセージを作りたい場合 |
6996 | 6913 | (但しこの問題を助けるモジュールがあります))でも、 |
6997 | 6914 | ファイルを開いた時の帰り値を常にチェックするべきです。 |
6998 | 6915 | めったにない例外は、開いていないファイルハンドルを使うのが |
6999 | 6916 | 本当にやりたいことの場合です。 |
7000 | 6917 | |
7001 | 6918 | =begin original |
7002 | 6919 | |
7003 | 6920 | As a special case the 3-arg form with a read/write mode and the third |
7004 | 6921 | argument being C<undef>: |
7005 | 6922 | |
7006 | 6923 | =end original |
7007 | 6924 | |
7008 | 6925 | 特別な場合として、3 引数の形で読み書きモードで 3 番目の引数が |
7009 | 6926 | C<undef> の場合: |
7010 | 6927 | |
7011 | open( | |
6928 | open(TMP, "+>", undef) or die ... | |
7012 | 6929 | |
7013 | 6930 | =begin original |
7014 | 6931 | |
7015 | 6932 | opens a filehandle to an anonymous temporary file. Also using "+<" |
7016 | 6933 | works for symmetry, but you really should consider writing something |
7017 | 6934 | to the temporary file first. You will need to seek() to do the |
7018 | 6935 | reading. |
7019 | 6936 | |
7020 | 6937 | =end original |
7021 | 6938 | |
7022 | 6939 | 無名一時ファイルとしてファイルハンドルを開きます。 |
7023 | 6940 | また "+<" も対称性のために動作しますが、 |
7024 | 6941 | 一時ファイルにはまず何かを書き込みたいはずです。 |
7025 | 6942 | 読み込みを行うためには seek() が必要です。 |
7026 | 6943 | |
7027 | 6944 | =begin original |
7028 | 6945 | |
7029 | 6946 | Since v5.8.0, perl has built using PerlIO by default. Unless you've |
7030 | 6947 | changed this (i.e. Configure -Uuseperlio), you can open file handles to |
7031 | 6948 | "in memory" files held in Perl scalars via: |
7032 | 6949 | |
7033 | 6950 | =end original |
7034 | 6951 | |
7035 | 6952 | v5.8.0 から、perl はデフォルトで PerlIO を使ってビルドされています。 |
7036 | 6953 | これ(Configure -Uuseperlio)を変更していない限り、 |
7037 | 6954 | 以下のようにして、Perl スカラに保持されている「オンメモリの」 |
7038 | 6955 | ファイルをファイルハンドルを開くことができます: |
7039 | 6956 | |
7040 | 6957 | open($fh, '>', \$variable) || .. |
7041 | 6958 | |
7042 | 6959 | =begin original |
7043 | 6960 | |
7044 | 6961 | Though if you try to re-open C<STDOUT> or C<STDERR> as an "in memory" |
7045 | 6962 | file, you have to close it first: |
7046 | 6963 | |
7047 | 6964 | =end original |
7048 | 6965 | |
7049 | 6966 | しかし、もし C<STDOUT> や C<STDERR> を「オンメモリの」ファイルとして |
7050 | 6967 | 再び開きたい場合は、先にそれを閉じる必要があります: |
7051 | 6968 | |
7052 | 6969 | close STDOUT; |
7053 | 6970 | open STDOUT, '>', \$variable or die "Can't open STDOUT: $!"; |
7054 | 6971 | |
7055 | 6972 | =begin original |
7056 | 6973 | |
7057 | 6974 | Examples: |
7058 | 6975 | |
7059 | 6976 | =end original |
7060 | 6977 | |
7061 | 6978 | 例: |
7062 | 6979 | |
7063 | 6980 | $ARTICLE = 100; |
7064 | 6981 | open ARTICLE or die "Can't find article $ARTICLE: $!\n"; |
7065 | 6982 | while (<ARTICLE>) {... |
7066 | 6983 | |
7067 | 6984 | open(LOG, '>>/usr/spool/news/twitlog'); # (log is reserved) |
7068 | 6985 | # if the open fails, output is discarded |
7069 | 6986 | |
7070 | open( | |
6987 | open(DBASE, '+<', 'dbase.mine') # open for update | |
7071 | 6988 | or die "Can't open 'dbase.mine' for update: $!"; |
7072 | 6989 | |
7073 | open( | |
6990 | open(DBASE, '+<dbase.mine') # ditto | |
7074 | 6991 | or die "Can't open 'dbase.mine' for update: $!"; |
7075 | 6992 | |
7076 | 6993 | open(ARTICLE, '-|', "caesar <$article") # decrypt article |
7077 | 6994 | or die "Can't start caesar: $!"; |
7078 | 6995 | |
7079 | 6996 | open(ARTICLE, "caesar <$article |") # ditto |
7080 | 6997 | or die "Can't start caesar: $!"; |
7081 | 6998 | |
7082 | 6999 | open(EXTRACT, "|sort >Tmp$$") # $$ is our process id |
7083 | 7000 | or die "Can't start sort: $!"; |
7084 | 7001 | |
7085 | 7002 | # in memory files |
7086 | 7003 | open(MEMORY,'>', \$var) |
7087 | 7004 | or die "Can't open memory file: $!"; |
7088 | 7005 | print MEMORY "foo!\n"; # output will end up in $var |
7089 | 7006 | |
7090 | 7007 | # process argument list of files along with any includes |
7091 | 7008 | |
7092 | 7009 | foreach $file (@ARGV) { |
7093 | 7010 | process($file, 'fh00'); |
7094 | 7011 | } |
7095 | 7012 | |
7096 | 7013 | sub process { |
7097 | 7014 | my($filename, $input) = @_; |
7098 | 7015 | $input++; # this is a string increment |
7099 | 7016 | unless (open($input, $filename)) { |
7100 | 7017 | print STDERR "Can't open $filename: $!\n"; |
7101 | 7018 | return; |
7102 | 7019 | } |
7103 | 7020 | |
7104 | 7021 | local $_; |
7105 | 7022 | while (<$input>) { # note use of indirection |
7106 | 7023 | if (/^#include "(.*)"/) { |
7107 | 7024 | process($1, $input); |
7108 | 7025 | next; |
7109 | 7026 | } |
7110 | 7027 | #... # whatever |
7111 | 7028 | } |
7112 | 7029 | } |
7113 | 7030 | |
7114 | 7031 | =begin original |
7115 | 7032 | |
7116 | 7033 | See L<perliol> for detailed info on PerlIO. |
7117 | 7034 | |
7118 | 7035 | =end original |
7119 | 7036 | |
7120 | 7037 | PerlIO に関する詳しい情報については L<perliol> を参照してください。 |
7121 | 7038 | |
7122 | 7039 | =begin original |
7123 | 7040 | |
7124 | 7041 | You may also, in the Bourne shell tradition, specify an EXPR beginning |
7125 | 7042 | with C<< '>&' >>, in which case the rest of the string is interpreted |
7126 | 7043 | as the name of a filehandle (or file descriptor, if numeric) to be |
7127 | duped (as | |
7044 | duped (as L<dup(2)>) and opened. You may use C<&> after C<< > >>, | |
7128 | 7045 | C<<< >> >>>, C<< < >>, C<< +> >>, C<<< +>> >>>, and C<< +< >>. |
7129 | 7046 | The mode you specify should match the mode of the original filehandle. |
7130 | 7047 | (Duping a filehandle does not take into account any existing contents |
7131 | 7048 | of IO buffers.) If you use the 3-arg form then you can pass either a |
7132 | 7049 | number, the name of a filehandle or the normal "reference to a glob". |
7133 | 7050 | |
7134 | 7051 | =end original |
7135 | 7052 | |
7136 | 7053 | Bourne シェルの慣例にしたがって、EXPR の先頭に C<< '>&' >> |
7137 | 7054 | を付けると、EXPR の残りの文字列をファイルハンドル名 |
7138 | (数字であれば、ファイル記述子) と解釈して、それを ( | |
7055 | (数字であれば、ファイル記述子) と解釈して、それを (L<dup(2)> によって) | |
7139 | 7056 | 複製してオープンします。 |
7140 | 7057 | C<&> は、C<< > >>, C<<< >> >>>, C<< < >>, C<< +> >>, C<<< +>> >>>, |
7141 | 7058 | C<< +< >>というモード指定に付けることができます。 |
7142 | 7059 | 指定するモード指定は、もとのファイルハンドルのモードと |
7143 | 7060 | 合っていないといけません。 |
7144 | 7061 | (ファイルハンドルの複製は既に存在する IO バッファの内容に含めません。) |
7145 | 7062 | 3 引数形式を使う場合は、数値を渡すか、ファイルハンドルの名前を渡すか、 |
7146 | 7063 | 通常の「グロブへのリファレンス」を渡します。 |
7147 | 7064 | |
7148 | 7065 | =begin original |
7149 | 7066 | |
7150 | 7067 | Here is a script that saves, redirects, and restores C<STDOUT> and |
7151 | 7068 | C<STDERR> using various methods: |
7152 | 7069 | |
7153 | 7070 | =end original |
7154 | 7071 | |
7155 | 7072 | C<STDOUT> と C<STDERR> 保存し、リダイレクトし、元に戻すスクリプトを示します: |
7156 | 7073 | |
7157 | 7074 | #!/usr/bin/perl |
7158 | 7075 | open my $oldout, ">&STDOUT" or die "Can't dup STDOUT: $!"; |
7159 | 7076 | open OLDERR, ">&", \*STDERR or die "Can't dup STDERR: $!"; |
7160 | 7077 | |
7161 | 7078 | open STDOUT, '>', "foo.out" or die "Can't redirect STDOUT: $!"; |
7162 | 7079 | open STDERR, ">&STDOUT" or die "Can't dup STDOUT: $!"; |
7163 | 7080 | |
7164 | 7081 | select STDERR; $| = 1; # make unbuffered |
7165 | 7082 | select STDOUT; $| = 1; # make unbuffered |
7166 | 7083 | |
7167 | 7084 | print STDOUT "stdout 1\n"; # this works for |
7168 | 7085 | print STDERR "stderr 1\n"; # subprocesses too |
7169 | 7086 | |
7170 | 7087 | open STDOUT, ">&", $oldout or die "Can't dup \$oldout: $!"; |
7171 | 7088 | open STDERR, ">&OLDERR" or die "Can't dup OLDERR: $!"; |
7172 | 7089 | |
7173 | 7090 | print STDOUT "stdout 2\n"; |
7174 | 7091 | print STDERR "stderr 2\n"; |
7175 | 7092 | |
7176 | 7093 | =begin original |
7177 | 7094 | |
7178 | 7095 | If you specify C<< '<&=X' >>, where C<X> is a file descriptor number |
7179 | 7096 | or a filehandle, then Perl will do an equivalent of C's C<fdopen> of |
7180 | that file descriptor (and not call | |
7097 | that file descriptor (and not call L<dup(2)>); this is more | |
7181 | 7098 | parsimonious of file descriptors. For example: |
7182 | 7099 | |
7183 | 7100 | =end original |
7184 | 7101 | |
7185 | 7102 | C<X> をファイル記述子の番号かファイルハンドルとして、 |
7186 | 7103 | C<< '<&=X' >> と指定すると、Perl はそのファイル記述子に対する |
7187 | C の C<fdopen> と同じことを行ないます(そして | |
7104 | C の C<fdopen> と同じことを行ないます(そして L<dup(2)> は呼び出しません); | |
7188 | 7105 | これはファイル記述子をより節約します。 |
7189 | 7106 | 例: |
7190 | 7107 | |
7191 | 7108 | # open for input, reusing the fileno of $fd |
7192 | 7109 | open(FILEHANDLE, "<&=$fd") |
7193 | 7110 | |
7194 | 7111 | =begin original |
7195 | 7112 | |
7196 | 7113 | or |
7197 | 7114 | |
7198 | 7115 | =end original |
7199 | 7116 | |
7200 | 7117 | または |
7201 | 7118 | |
7202 | 7119 | open(FILEHANDLE, "<&=", $fd) |
7203 | 7120 | |
7204 | 7121 | =begin original |
7205 | 7122 | |
7206 | 7123 | or |
7207 | 7124 | |
7208 | 7125 | =end original |
7209 | 7126 | |
7210 | 7127 | または |
7211 | 7128 | |
7212 | 7129 | # open for append, using the fileno of OLDFH |
7213 | 7130 | open(FH, ">>&=", OLDFH) |
7214 | 7131 | |
7215 | 7132 | =begin original |
7216 | 7133 | |
7217 | 7134 | or |
7218 | 7135 | |
7219 | 7136 | =end original |
7220 | 7137 | |
7221 | 7138 | または |
7222 | 7139 | |
7223 | 7140 | open(FH, ">>&=OLDFH") |
7224 | 7141 | |
7225 | 7142 | =begin original |
7226 | 7143 | |
7227 | 7144 | Being parsimonious on filehandles is also useful (besides being |
7228 | 7145 | parsimonious) for example when something is dependent on file |
7229 | 7146 | descriptors, like for example locking using flock(). If you do just |
7230 | 7147 | C<< open(A, '>>&B') >>, the filehandle A will not have the same file |
7231 | 7148 | descriptor as B, and therefore flock(A) will not flock(B), and vice |
7232 | 7149 | versa. But with C<< open(A, '>>&=B') >> the filehandles will share |
7233 | 7150 | the same file descriptor. |
7234 | 7151 | |
7235 | 7152 | =end original |
7236 | 7153 | |
7237 | 7154 | ファイルハンドルを倹約することは、何かがファイル記述子に依存している場合、 |
7238 | 7155 | 例えば flock() を使ったファイルロックといった場合に有用です |
7239 | 7156 | (しかも倹約できます)。 |
7240 | 7157 | C<< open(A, '>>&B') >> とすると、ファイルハンドル A は B と同じ |
7241 | 7158 | ファイル記述子にはならないので、flock(A) と flock(B) は別々になります。 |
7242 | 7159 | しかし C<< open(A, '>>&=B') >> ではファイルハンドルは同じファイル記述子を |
7243 | 7160 | 共有します。 |
7244 | 7161 | |
7245 | 7162 | =begin original |
7246 | 7163 | |
7247 | 7164 | Note that if you are using Perls older than 5.8.0, Perl will be using |
7248 | 7165 | the standard C libraries' fdopen() to implement the "=" functionality. |
7249 | 7166 | On many UNIX systems fdopen() fails when file descriptors exceed a |
7250 | 7167 | certain value, typically 255. For Perls 5.8.0 and later, PerlIO is |
7251 | 7168 | most often the default. |
7252 | 7169 | |
7253 | 7170 | =end original |
7254 | 7171 | |
7255 | 7172 | 5.8.0 より前の Perl を使っている場合、"=" 機能の実装は |
7256 | 7173 | 標準 C ライブラリの fdopen() を使っています。 |
7257 | 7174 | 多くの UNIX システムでは、fdopen() はファイル記述子がある値 |
7258 | 7175 | (典型的には 255)を超えた場合に失敗することが知られています。 |
7259 | 7176 | 5.8.0 以降の Perl では、ほとんどの場合 PerlIO がデフォルトです。 |
7260 | 7177 | |
7261 | 7178 | =begin original |
7262 | 7179 | |
7263 | 7180 | You can see whether Perl has been compiled with PerlIO or not by |
7264 | 7181 | running C<perl -V> and looking for C<useperlio=> line. If C<useperlio> |
7265 | 7182 | is C<define>, you have PerlIO, otherwise you don't. |
7266 | 7183 | |
7267 | 7184 | =end original |
7268 | 7185 | |
7269 | 7186 | Perl が PerlIO つきでコンパイルされているかどうかを確認するには、 |
7270 | 7187 | C<perl -V> として C<useperlio=> の行を見ます。 |
7271 | 7188 | C<useperlio> が C<define> なら PerlIO を使っています。 |
7272 | 7189 | そうでなければ使っていません。 |
7273 | 7190 | |
7274 | 7191 | =begin original |
7275 | 7192 | |
7276 | 7193 | If you open a pipe on the command C<'-'>, i.e., either C<'|-'> or C<'-|'> |
7277 | 7194 | with 2-arguments (or 1-argument) form of open(), then |
7278 | 7195 | there is an implicit fork done, and the return value of open is the pid |
7279 | 7196 | of the child within the parent process, and C<0> within the child |
7280 | 7197 | process. (Use C<defined($pid)> to determine whether the open was successful.) |
7281 | 7198 | The filehandle behaves normally for the parent, but i/o to that |
7282 | 7199 | filehandle is piped from/to the STDOUT/STDIN of the child process. |
7283 | 7200 | In the child process the filehandle isn't opened--i/o happens from/to |
7284 | 7201 | the new STDOUT or STDIN. Typically this is used like the normal |
7285 | 7202 | piped open when you want to exercise more control over just how the |
7286 | 7203 | pipe command gets executed, such as when you are running setuid, and |
7287 | 7204 | don't want to have to scan shell commands for metacharacters. |
7288 | 7205 | The following triples are more or less equivalent: |
7289 | 7206 | |
7290 | 7207 | =end original |
7291 | 7208 | |
7292 | 7209 | 2 引数(または 1 引数)の形の open() で |
7293 | 7210 | C<'-|'> や C<'|-'> というふうに、C<'-'> というコマンドに |
7294 | 7211 | パイプをオープンすると、fork が行なわれ、open の返り値として、 |
7295 | 7212 | 親プロセスにはチャイルドプロセスの pid が、チャイルドプロセスには |
7296 | 7213 | C<0> が返されます。 |
7297 | 7214 | (open が成功したかどうかは、 |
7298 | 7215 | C<defined($pid)> のようにして調べることができます。) |
7299 | 7216 | 親プロセスでは、このファイルハンドルは |
7300 | 7217 | 通常通りに動作しますが、行なわれる入出力は、 |
7301 | 7218 | チャイルドプロセスの STDIN/STDOUT にパイプされます。 |
7302 | 7219 | チャイルドプロセス側では、そのファイルハンドルは |
7303 | 7220 | オープンされず、入出力は新しい STDOUT か STDIN に対して行なわれます。 |
7304 | 7221 | これは、setuid で実行して、シェルコマンドのメタ文字を |
7305 | 7222 | 検索させたくないような場合に、パイプコマンドの起動の仕方を |
7306 | 7223 | 制御したいとき、普通のパイプの open と同じように使います。 |
7307 | 7224 | 以下の組み合わせは、だいたい同じものです: |
7308 | 7225 | |
7309 | 7226 | open(FOO, "|tr '[a-z]' '[A-Z]'"); |
7310 | 7227 | open(FOO, '|-', "tr '[a-z]' '[A-Z]'"); |
7311 | 7228 | open(FOO, '|-') || exec 'tr', '[a-z]', '[A-Z]'; |
7312 | 7229 | open(FOO, '|-', "tr", '[a-z]', '[A-Z]'); |
7313 | 7230 | |
7314 | 7231 | open(FOO, "cat -n '$file'|"); |
7315 | 7232 | open(FOO, '-|', "cat -n '$file'"); |
7316 | 7233 | open(FOO, '-|') || exec 'cat', '-n', $file; |
7317 | 7234 | open(FOO, '-|', "cat", '-n', $file); |
7318 | 7235 | |
7319 | 7236 | =begin original |
7320 | 7237 | |
7321 | 7238 | The last example in each block shows the pipe as "list form", which is |
7322 | 7239 | not yet supported on all platforms. A good rule of thumb is that if |
7323 | 7240 | your platform has true C<fork()> (in other words, if your platform is |
7324 | 7241 | UNIX) you can use the list form. |
7325 | 7242 | |
7326 | 7243 | =end original |
7327 | 7244 | |
7328 | 7245 | それぞれのブロックの最後の例ではパイプを「リスト形式」にしていますが、 |
7329 | 7246 | これはまだ全てのプラットフォームで対応しているわけではなりません。 |
7330 | 7247 | よい経験則としては、もし実行しているプラットフォームで真の C<fork()> が |
7331 | 7248 | あれば(言い換えると、プラットフォームが UNIX なら) リスト形式が使えます。 |
7332 | 7249 | |
7333 | 7250 | =begin original |
7334 | 7251 | |
7335 | 7252 | See L<perlipc/"Safe Pipe Opens"> for more examples of this. |
7336 | 7253 | |
7337 | 7254 | =end original |
7338 | 7255 | |
7339 | 7256 | これに関する更なる例については L<perlipc/"Safe Pipe Opens"> を参照して下さい。 |
7340 | 7257 | |
7341 | 7258 | =begin original |
7342 | 7259 | |
7343 | 7260 | Beginning with v5.6.0, Perl will attempt to flush all files opened for |
7344 | 7261 | output before any operation that may do a fork, but this may not be |
7345 | 7262 | supported on some platforms (see L<perlport>). To be safe, you may need |
7346 | 7263 | to set C<$|> ($AUTOFLUSH in English) or call the C<autoflush()> method |
7347 | 7264 | of C<IO::Handle> on any open handles. |
7348 | 7265 | |
7349 | 7266 | =end original |
7350 | 7267 | |
7351 | 7268 | v5.6.0 から、Perl は書き込み用に開いている全てのファイルに対して |
7352 | 7269 | fork を行う前にフラッシュしようとしますが、これに対応していない |
7353 | 7270 | プラットフォームもあります(L<perlport> を参照してください)。 |
7354 | 7271 | 安全のために、C<$|> (English モジュールでは $AUTOFLUSH) をセットするか、 |
7355 | 7272 | 全ての開いているハンドルに対して C<IO::Handle> の C<autoflush()> メソッドを |
7356 | 呼び出す | |
7273 | 呼び出すようにしてください。 | |
7357 | 7274 | |
7358 | 7275 | =begin original |
7359 | 7276 | |
7360 | 7277 | On systems that support a close-on-exec flag on files, the flag will |
7361 | 7278 | be set for the newly opened file descriptor as determined by the value |
7362 | 7279 | of $^F. See L<perlvar/$^F>. |
7363 | 7280 | |
7364 | 7281 | =end original |
7365 | 7282 | |
7366 | 7283 | ファイルに対する close-on-exec フラグをサポートしているシステムでは、 |
7367 | 7284 | フラグは $^F の値で決定される、新しくオープンされたファイル記述子に対して |
7368 | 7285 | セットされます。 |
7369 | 7286 | L<perlvar/$^F> を参照してください。 |
7370 | 7287 | |
7371 | 7288 | =begin original |
7372 | 7289 | |
7373 | 7290 | Closing any piped filehandle causes the parent process to wait for the |
7374 | 7291 | child to finish, and returns the status value in C<$?> and |
7375 | 7292 | C<${^CHILD_ERROR_NATIVE}>. |
7376 | 7293 | |
7377 | 7294 | =end original |
7378 | 7295 | |
7379 | 7296 | パイプのファイルハンドルを close することで、 |
7380 | 7297 | 親プロセスは、チャイルドプロセスの終了を待ち、C<$?> と |
7381 | 7298 | C<${^CHILD_ERROR_NATIVE}> にステータス値を返します。 |
7382 | 7299 | |
7383 | 7300 | =begin original |
7384 | 7301 | |
7385 | 7302 | The filename passed to 2-argument (or 1-argument) form of open() will |
7386 | 7303 | have leading and trailing whitespace deleted, and the normal |
7387 | 7304 | redirection characters honored. This property, known as "magic open", |
7388 | 7305 | can often be used to good effect. A user could specify a filename of |
7389 | 7306 | F<"rsh cat file |">, or you could change certain filenames as needed: |
7390 | 7307 | |
7391 | 7308 | =end original |
7392 | 7309 | |
7393 | 7310 | 2 引数(と 1 引数)の形の open() に渡されたファイル名は、 |
7394 | 7311 | はじめと終わりの空白が取り除かれ、 |
7395 | 7312 | 通常のリダイレクト文字列を受け付けます。 |
7396 | 7313 | この機能は "magic open" として知られていますが、 |
7397 | 7314 | 普通いい効果をもたらします。 |
7398 | 7315 | ユーザーは F<"rsh cat file |"> といったファイル名を指定できますし、 |
7399 | 7316 | 特定のファイル名を必要に応じて変更できます。 |
7400 | 7317 | |
7401 | 7318 | $filename =~ s/(.*\.gz)\s*$/gzip -dc < $1|/; |
7402 | 7319 | open(FH, $filename) or die "Can't open $filename: $!"; |
7403 | 7320 | |
7404 | 7321 | =begin original |
7405 | 7322 | |
7406 | 7323 | Use 3-argument form to open a file with arbitrary weird characters in it, |
7407 | 7324 | |
7408 | 7325 | =end original |
7409 | 7326 | |
7410 | 7327 | 妙な文字が含まれているようなファイル名をオープンするには、 |
7411 | 7328 | 3 引数の形を使います。 |
7412 | 7329 | |
7413 | 7330 | open(FOO, '<', $file); |
7414 | 7331 | |
7415 | 7332 | =begin original |
7416 | 7333 | |
7417 | 7334 | otherwise it's necessary to protect any leading and trailing whitespace: |
7418 | 7335 | |
7419 | 7336 | =end original |
7420 | 7337 | |
7421 | 7338 | あるいは、次のようにして、最初と最後の空白を保護します: |
7422 | 7339 | |
7423 | 7340 | $file =~ s#^(\s)#./$1#; |
7424 | 7341 | open(FOO, "< $file\0"); |
7425 | 7342 | |
7426 | 7343 | =begin original |
7427 | 7344 | |
7428 | 7345 | (this may not work on some bizarre filesystems). One should |
7429 | 7346 | conscientiously choose between the I<magic> and 3-arguments form |
7430 | 7347 | of open(): |
7431 | 7348 | |
7432 | 7349 | =end original |
7433 | 7350 | |
7434 | 7351 | (これは奇妙なファイルシステムでは動作しないかもしれません)。 |
7435 | 7352 | open() の I<magic> と 3 引数形式を誠実に選択するべきです。 |
7436 | 7353 | |
7437 | 7354 | open IN, $ARGV[0]; |
7438 | 7355 | |
7439 | 7356 | =begin original |
7440 | 7357 | |
7441 | 7358 | will allow the user to specify an argument of the form C<"rsh cat file |">, |
7442 | 7359 | but will not work on a filename which happens to have a trailing space, while |
7443 | 7360 | |
7444 | 7361 | =end original |
7445 | 7362 | |
7446 | 7363 | とするとユーザーは C<"rsh cat file |"> という形の引数を指定できますが、 |
7447 | 7364 | 末尾にスペースがついてしまったファイル名では動作しません。一方: |
7448 | 7365 | |
7449 | 7366 | open IN, '<', $ARGV[0]; |
7450 | 7367 | |
7451 | 7368 | =begin original |
7452 | 7369 | |
7453 | 7370 | will have exactly the opposite restrictions. |
7454 | 7371 | |
7455 | 7372 | =end original |
7456 | 7373 | |
7457 | 7374 | はまったく逆の制限があります。 |
7458 | 7375 | |
7459 | 7376 | =begin original |
7460 | 7377 | |
7461 | If you want a "real" C C<open> (see | |
7378 | If you want a "real" C C<open> (see L<open(2)> on your system), then you | |
7462 | 7379 | should use the C<sysopen> function, which involves no such magic (but |
7463 | 7380 | may use subtly different filemodes than Perl open(), which is mapped |
7464 | 7381 | to C fopen()). This is |
7465 | 7382 | another way to protect your filenames from interpretation. For example: |
7466 | 7383 | |
7467 | 7384 | =end original |
7468 | 7385 | |
7469 | もし「本当の」C 言語の C<open> (システムの | |
7386 | もし「本当の」C 言語の C<open> (システムの L<open(2)> を参照してください)が | |
7470 | 7387 | 必要なら、このような副作用のない C<sysopen> 関数を使うべきです |
7471 | 7388 | (ただし、C の fopen() に割り付けられる Perl の open() とは |
7472 | 7389 | かすかに違うファイルモードを持ちます)。 |
7473 | 7390 | これはファイル名を解釈から守るもう一つの方法です。 |
7474 | 7391 | 例えば: |
7475 | 7392 | |
7476 | 7393 | use IO::Handle; |
7477 | 7394 | sysopen(HANDLE, $path, O_RDWR|O_CREAT|O_EXCL) |
7478 | 7395 | or die "sysopen $path: $!"; |
7479 | 7396 | $oldfh = select(HANDLE); $| = 1; select($oldfh); |
7480 | 7397 | print HANDLE "stuff $$\n"; |
7481 | 7398 | seek(HANDLE, 0, 0); |
7482 | 7399 | print "File contains: ", <HANDLE>; |
7483 | 7400 | |
7484 | 7401 | =begin original |
7485 | 7402 | |
7486 | 7403 | Using the constructor from the C<IO::Handle> package (or one of its |
7487 | 7404 | subclasses, such as C<IO::File> or C<IO::Socket>), you can generate anonymous |
7488 | 7405 | filehandles that have the scope of whatever variables hold references to |
7489 | 7406 | them, and automatically close whenever and however you leave that scope: |
7490 | 7407 | |
7491 | 7408 | =end original |
7492 | 7409 | |
7493 | 7410 | C<IO::Handle> パッケージ(または C<IO::File> や C<IO::Socket> といった |
7494 | 7411 | サブパッケージ)のコンストラクタを使うことで、 |
7495 | 7412 | これらへのリファレンスを保持している変数のスコープを持ち、 |
7496 | 7413 | スコープから離れると自動的に閉じる無名ファイルハンドルを作成できます: |
7497 | 7414 | |
7498 | 7415 | use IO::File; |
7499 | 7416 | #... |
7500 | 7417 | sub read_myfile_munged { |
7501 | 7418 | my $ALL = shift; |
7502 | my $handle = IO::File | |
7419 | my $handle = new IO::File; | |
7503 | 7420 | open($handle, "myfile") or die "myfile: $!"; |
7504 | 7421 | $first = <$handle> |
7505 | 7422 | or return (); # Automatically closed here. |
7506 | 7423 | mung $first or die "mung failed"; # Or here. |
7507 | 7424 | return $first, <$handle> if $ALL; # Or here. |
7508 | 7425 | $first; # Or here. |
7509 | 7426 | } |
7510 | 7427 | |
7511 | 7428 | =begin original |
7512 | 7429 | |
7513 | 7430 | See L</seek> for some details about mixing reading and writing. |
7514 | 7431 | |
7515 | 7432 | =end original |
7516 | 7433 | |
7517 | 7434 | 読み書きを混ぜる場合の詳細については L</seek> を参照して下さい。 |
7518 | 7435 | |
7519 | 7436 | =item opendir DIRHANDLE,EXPR |
7520 | 7437 | X<opendir> |
7521 | 7438 | |
7522 | 7439 | =begin original |
7523 | 7440 | |
7524 | 7441 | Opens a directory named EXPR for processing by C<readdir>, C<telldir>, |
7525 | 7442 | C<seekdir>, C<rewinddir>, and C<closedir>. Returns true if successful. |
7526 | 7443 | DIRHANDLE may be an expression whose value can be used as an indirect |
7527 | 7444 | dirhandle, usually the real dirhandle name. If DIRHANDLE is an undefined |
7528 | 7445 | scalar variable (or array or hash element), the variable is assigned a |
7529 | 7446 | reference to a new anonymous dirhandle. |
7530 | 7447 | DIRHANDLEs have their own namespace separate from FILEHANDLEs. |
7531 | 7448 | |
7532 | 7449 | =end original |
7533 | 7450 | |
7534 | 7451 | C<readdir>、C<telldir>、C<seekdir>、C<rewinddir>、C<closedir> で |
7535 | 7452 | 処理するために、EXPR で指定された名前のディレクトリをオープンします。 |
7536 | 7453 | 成功時には真を返します。 |
7537 | 7454 | DIRHANDLE は間接ディレクトリハンドルとして使える値(普通は実際のディレクトリ |
7538 | 7455 | ハンドルの名前)となる式でも構いません。 |
7539 | 7456 | DIRHANDLE が未定義のスカラ値(または配列かハッシュの要素)の場合、その変数は |
7540 | 7457 | 新しい無名ディレクトリハンドルへのリファレンスが代入されます。 |
7541 | 7458 | DIRHANDLE は、FILEHANDLE とは別に名前空間を持っています。 |
7542 | 7459 | |
7543 | =begin original | |
7544 | ||
7545 | See example at C<readdir>. | |
7546 | ||
7547 | =end original | |
7548 | ||
7549 | C<readdir> の例を参照してください。 | |
7550 | ||
7551 | 7460 | =item ord EXPR |
7552 | 7461 | X<ord> X<encoding> |
7553 | 7462 | |
7554 | 7463 | =item ord |
7555 | 7464 | |
7556 | 7465 | =begin original |
7557 | 7466 | |
7558 | 7467 | Returns the numeric (the native 8-bit encoding, like ASCII or EBCDIC, |
7559 | 7468 | or Unicode) value of the first character of EXPR. If EXPR is omitted, |
7560 | 7469 | uses C<$_>. |
7561 | 7470 | |
7562 | 7471 | =end original |
7563 | 7472 | |
7564 | 7473 | EXPR の最初の文字の数値としての(ASCII, EBCDIC, Unicode のような 8-bit |
7565 | 7474 | ネイティブエンコーディングの)値を返します。 |
7566 | 7475 | EXPR を省略した場合には、C<$_> を使用します。 |
7567 | 7476 | |
7568 | 7477 | =begin original |
7569 | 7478 | |
7570 | 7479 | For the reverse, see L</chr>. |
7571 | 7480 | See L<perlunicode> for more about Unicode. |
7572 | 7481 | |
7573 | 7482 | =end original |
7574 | 7483 | |
7575 | 7484 | 逆のことをするには L</chr> を参照してください。 |
7576 | 7485 | Unicode については L<perlunicode> を参照してください。 |
7577 | 7486 | |
7578 | 7487 | =item our EXPR |
7579 | 7488 | X<our> X<global> |
7580 | 7489 | |
7581 | 7490 | =item our TYPE EXPR |
7582 | 7491 | |
7583 | 7492 | =item our EXPR : ATTRS |
7584 | 7493 | |
7585 | 7494 | =item our TYPE EXPR : ATTRS |
7586 | 7495 | |
7587 | 7496 | =begin original |
7588 | 7497 | |
7589 | 7498 | C<our> associates a simple name with a package variable in the current |
7590 | 7499 | package for use within the current scope. When C<use strict 'vars'> is in |
7591 | 7500 | effect, C<our> lets you use declared global variables without qualifying |
7592 | 7501 | them with package names, within the lexical scope of the C<our> declaration. |
7593 | 7502 | In this way C<our> differs from C<use vars>, which is package scoped. |
7594 | 7503 | |
7595 | 7504 | =end original |
7596 | 7505 | |
7597 | 7506 | C<our> は単純名を、現在のスコープ内で使うために、現在のパッケージの |
7598 | 7507 | パッケージ変数と結び付けます。 |
7599 | 7508 | C<use strict 'vars'> が有効の場合は、C<our> を使うことで、C<our> 宣言の |
7600 | 7509 | レキシカルスコープ内で、宣言されたグローバル変数をパッケージ名で |
7601 | 7510 | 修飾することなく使うことができます。 |
7602 | 7511 | この意味では、C<use vars> はパッケージスコープなので、C<our> とは異なります。 |
7603 | 7512 | |
7604 | 7513 | =begin original |
7605 | 7514 | |
7606 | 7515 | Unlike C<my>, which both allocates storage for a variable and associates |
7607 | 7516 | a simple name with that storage for use within the current scope, C<our> |
7608 | 7517 | associates a simple name with a package variable in the current package, |
7609 | 7518 | for use within the current scope. In other words, C<our> has the same |
7610 | 7519 | scoping rules as C<my>, but does not necessarily create a |
7611 | 7520 | variable. |
7612 | 7521 | |
7613 | 7522 | =end original |
7614 | 7523 | |
7615 | 7524 | 記憶領域を変数に割り当て、単純名を現在のスコープ内で使うためにその記憶領域に |
7616 | 7525 | 割り当てる C<my> と違って、C<our> は単純名を、現在のスコープ内で使うために、 |
7617 | 7526 | 現在のパッケージのパッケージ変数と結び付けます。 |
7618 | 7527 | 言い換えると、C<our> は C<my> と同じスコープルールを持ちますが、 |
7619 | 7528 | 変数を作る必要はありません。 |
7620 | 7529 | |
7621 | 7530 | =begin original |
7622 | 7531 | |
7623 | 7532 | If more than one value is listed, the list must be placed |
7624 | 7533 | in parentheses. |
7625 | 7534 | |
7626 | 7535 | =end original |
7627 | 7536 | |
7628 | 7537 | 二つ以上の値をリストする場合は、リストはかっこでくくる必要があります。 |
7629 | 7538 | |
7630 | 7539 | our $foo; |
7631 | 7540 | our($bar, $baz); |
7632 | 7541 | |
7633 | 7542 | =begin original |
7634 | 7543 | |
7635 | 7544 | An C<our> declaration declares a global variable that will be visible |
7636 | 7545 | across its entire lexical scope, even across package boundaries. The |
7637 | 7546 | package in which the variable is entered is determined at the point |
7638 | 7547 | of the declaration, not at the point of use. This means the following |
7639 | 7548 | behavior holds: |
7640 | 7549 | |
7641 | 7550 | =end original |
7642 | 7551 | |
7643 | 7552 | C<our> 宣言はレキシカルスコープ全体に対して(たとえパッケージ境界を |
7644 | 7553 | 越えていても)見えるグローバル変数を宣言します。 |
7645 | 7554 | この変数が入るパッケージは宣言した時点で定義され、 |
7646 | 7555 | 使用した時点ではありません。 |
7647 | 7556 | これにより、以下のような振る舞いになります: |
7648 | 7557 | |
7649 | 7558 | package Foo; |
7650 | 7559 | our $bar; # declares $Foo::bar for rest of lexical scope |
7651 | 7560 | $bar = 20; |
7652 | 7561 | |
7653 | 7562 | package Bar; |
7654 | 7563 | print $bar; # prints 20, as it refers to $Foo::bar |
7655 | 7564 | |
7656 | 7565 | =begin original |
7657 | 7566 | |
7658 | 7567 | Multiple C<our> declarations with the same name in the same lexical |
7659 | 7568 | scope are allowed if they are in different packages. If they happen |
7660 | 7569 | to be in the same package, Perl will emit warnings if you have asked |
7661 | 7570 | for them, just like multiple C<my> declarations. Unlike a second |
7662 | 7571 | C<my> declaration, which will bind the name to a fresh variable, a |
7663 | 7572 | second C<our> declaration in the same package, in the same scope, is |
7664 | 7573 | merely redundant. |
7665 | 7574 | |
7666 | 7575 | =end original |
7667 | 7576 | |
7668 | 7577 | 同じレキシカルスコープでも、パッケージが異なっていれば、同じ名前で複数の |
7669 | 7578 | C<our> 宣言ができます。 |
7670 | 7579 | 同じパッケージになっていると、警告が出力されるようになっていれば |
7671 | 7580 | 複数の C<my> 宣言がある場合と同じように警告が出力されます。 |
7672 | 7581 | 新しい変数を名前に割り当てることになる 2 回目の C<my> 宣言と違って、 |
7673 | 7582 | 同じパッケージの同じスコープで 2 回 C<our> 宣言するのは単に冗長です。 |
7674 | 7583 | |
7675 | 7584 | use warnings; |
7676 | 7585 | package Foo; |
7677 | 7586 | our $bar; # declares $Foo::bar for rest of lexical scope |
7678 | 7587 | $bar = 20; |
7679 | 7588 | |
7680 | 7589 | package Bar; |
7681 | 7590 | our $bar = 30; # declares $Bar::bar for rest of lexical scope |
7682 | 7591 | print $bar; # prints 30 |
7683 | 7592 | |
7684 | 7593 | our $bar; # emits warning but has no other effect |
7685 | 7594 | print $bar; # still prints 30 |
7686 | 7595 | |
7687 | 7596 | =begin original |
7688 | 7597 | |
7689 | 7598 | An C<our> declaration may also have a list of attributes associated |
7690 | 7599 | with it. |
7691 | 7600 | |
7692 | 7601 | =end original |
7693 | 7602 | |
7694 | 7603 | C<our> 宣言には、それと結び付けられる属性のリストを持つこともあります。 |
7695 | 7604 | |
7696 | 7605 | =begin original |
7697 | 7606 | |
7698 | 7607 | The exact semantics and interface of TYPE and ATTRS are still |
7699 | 7608 | evolving. TYPE is currently bound to the use of C<fields> pragma, |
7700 | 7609 | and attributes are handled using the C<attributes> pragma, or starting |
7701 | 7610 | from Perl 5.8.0 also via the C<Attribute::Handlers> module. See |
7702 | 7611 | L<perlsub/"Private Variables via my()"> for details, and L<fields>, |
7703 | 7612 | L<attributes>, and L<Attribute::Handlers>. |
7704 | 7613 | |
7705 | 7614 | =end original |
7706 | 7615 | |
7707 | 7616 | TYPE と ATTRS の正確な文法とインターフェースは今でも進化しています。 |
7708 | 7617 | 現在のところ、TYPE は C<fields> プラグマの使用と結び付けられていて、 |
7709 | 7618 | 属性は C<attributes> プラグマか、Perl 5.8.0 からは |
7710 | 7619 | C<Attribute::Handlers> モジュールと結び付けられています。 |
7711 | 7620 | 詳しくはL<perlsub/"Private Variables via my()">, L<fields>, |
7712 | 7621 | L<attributes>, L<Attribute::Handlers> を参照してください。 |
7713 | 7622 | |
7714 | 7623 | =item pack TEMPLATE,LIST |
7715 | 7624 | X<pack> |
7716 | 7625 | |
7717 | 7626 | =begin original |
7718 | 7627 | |
7719 | 7628 | Takes a LIST of values and converts it into a string using the rules |
7720 | 7629 | given by the TEMPLATE. The resulting string is the concatenation of |
7721 | 7630 | the converted values. Typically, each converted value looks |
7722 | 7631 | like its machine-level representation. For example, on 32-bit machines |
7723 | 7632 | an integer may be represented by a sequence of 4 bytes that will be |
7724 | 7633 | converted to a sequence of 4 characters. |
7725 | 7634 | |
7726 | 7635 | =end original |
7727 | 7636 | |
7728 | 7637 | LIST の値を TEMPLATE で与えられたルールを用いて文字列に変換します。 |
7729 | 7638 | 結果の文字列は変換した値を連結したものです。 |
7730 | 7639 | 典型的には、それぞれの変換された値はマシンレベルの表現のように見えます。 |
7731 | 7640 | 例えば、32-bit マシンでは、4 バイトで表現される整数を変換すると |
7732 | 7641 | 4 文字の文字列に変換されます。 |
7733 | 7642 | |
7734 | 7643 | =begin original |
7735 | 7644 | |
7736 | 7645 | The TEMPLATE is a sequence of characters that give the order and type |
7737 | 7646 | of values, as follows: |
7738 | 7647 | |
7739 | 7648 | =end original |
7740 | 7649 | |
7741 | 7650 | TEMPLATE は、以下のような値の型と順番を指定する文字を並べたものです: |
7742 | 7651 | |
7743 | 7652 | =begin original |
7744 | 7653 | |
7745 | 7654 | a A string with arbitrary binary data, will be null padded. |
7746 | 7655 | A A text (ASCII) string, will be space padded. |
7747 | 7656 | Z A null terminated (ASCIZ) string, will be null padded. |
7748 | 7657 | |
7749 | 7658 | =end original |
7750 | 7659 | |
7751 | 7660 | a 任意のバイナリデータを含む文字列、ヌル文字で埋める |
7752 | 7661 | A テキスト (ASCII) 文字列、スペース文字で埋める |
7753 | 7662 | Z ヌル文字終端 (ASCIZ) 文字列、ヌル文字で埋める |
7754 | 7663 | |
7755 | 7664 | =begin original |
7756 | 7665 | |
7757 | 7666 | b A bit string (ascending bit order inside each byte, like vec()). |
7758 | 7667 | B A bit string (descending bit order inside each byte). |
7759 | 7668 | h A hex string (low nybble first). |
7760 | 7669 | H A hex string (high nybble first). |
7761 | 7670 | |
7762 | 7671 | =end original |
7763 | 7672 | |
7764 | 7673 | b ビット列 (バイトごとに昇ビット順、vec() と同じ) |
7765 | 7674 | B ビット列 (バイトごとに降ビット順) |
7766 | 7675 | h 16 進数文字列 (低位ニブルが先) |
7767 | 7676 | H 16 進数文字列 (高位ニブルが先) |
7768 | 7677 | |
7769 | 7678 | =begin original |
7770 | 7679 | |
7771 | 7680 | c A signed char (8-bit) value. |
7772 | 7681 | C An unsigned char (octet) value. |
7773 | 7682 | W An unsigned char value (can be greater than 255). |
7774 | 7683 | |
7775 | 7684 | =end original |
7776 | 7685 | |
7777 | 7686 | c signed char (8 ビット) 値 |
7778 | 7687 | C unsigned char (オクテット) 値 |
7779 | 7688 | W unsigned char 値 (255 より大きいかもしれません) |
7780 | 7689 | |
7781 | 7690 | =begin original |
7782 | 7691 | |
7783 | 7692 | s A signed short (16-bit) value. |
7784 | 7693 | S An unsigned short value. |
7785 | 7694 | |
7786 | 7695 | =end original |
7787 | 7696 | |
7788 | 7697 | s signed short (16 ビット) 値 |
7789 | 7698 | S unsigned short 値 |
7790 | 7699 | |
7791 | 7700 | =begin original |
7792 | 7701 | |
7793 | 7702 | l A signed long (32-bit) value. |
7794 | 7703 | L An unsigned long value. |
7795 | 7704 | |
7796 | 7705 | =end original |
7797 | 7706 | |
7798 | 7707 | l signed long (32 ビット) 値 |
7799 | 7708 | L unsigned long 値 |
7800 | 7709 | |
7801 | 7710 | =begin original |
7802 | 7711 | |
7803 | 7712 | q A signed quad (64-bit) value. |
7804 | 7713 | Q An unsigned quad value. |
7805 | 7714 | (Quads are available only if your system supports 64-bit |
7806 | 7715 | integer values _and_ if Perl has been compiled to support those. |
7807 | 7716 | Causes a fatal error otherwise.) |
7808 | 7717 | |
7809 | 7718 | =end original |
7810 | 7719 | |
7811 | 7720 | q 符号付き 64 ビット整数 |
7812 | 7721 | Q 符号なし 64 ビット整数 |
7813 | 7722 | (64 ビット整数は、システムが 64 ビット整数に対応していて、かつ Perl が |
7814 | 7723 | 64 ビット整数対応としてコンパイルされている場合にのみ使用可能です。 |
7815 | 7724 | それ以外の場合は致命的エラーが発生します。) |
7816 | 7725 | |
7817 | 7726 | =begin original |
7818 | 7727 | |
7819 | 7728 | i A signed integer value. |
7820 | 7729 | I A unsigned integer value. |
7821 | 7730 | (This 'integer' is _at_least_ 32 bits wide. Its exact |
7822 | 7731 | size depends on what a local C compiler calls 'int'.) |
7823 | 7732 | |
7824 | 7733 | =end original |
7825 | 7734 | |
7826 | 7735 | i signed int 値 |
7827 | 7736 | I unsigned int 値 |
7828 | 7737 | (ここでの 'integer' は 「最低」 32 bits 幅です。 |
7829 | 7738 | 正確なサイズはローカルの C コンパイラの |
7830 | 7739 | 'int'のサイズに依存します) |
7831 | 7740 | |
7832 | 7741 | =begin original |
7833 | 7742 | |
7834 | 7743 | n An unsigned short (16-bit) in "network" (big-endian) order. |
7835 | 7744 | N An unsigned long (32-bit) in "network" (big-endian) order. |
7836 | 7745 | v An unsigned short (16-bit) in "VAX" (little-endian) order. |
7837 | 7746 | V An unsigned long (32-bit) in "VAX" (little-endian) order. |
7838 | 7747 | |
7839 | 7748 | =end original |
7840 | 7749 | |
7841 | 7750 | n "network" 順序 (ビッグエンディアン) の unsigned short (16 ビット) |
7842 | 7751 | N "network" 順序 (ビッグエンディアン) の unsigned long (32 ビット) |
7843 | 7752 | v "VAX" 順序 (リトルエンディアン) の unsigned short (16 ビット) |
7844 | 7753 | V "VAX" 順序 (リトルエンディアン) の unsigned long (32 ビット) |
7845 | 7754 | |
7846 | 7755 | =begin original |
7847 | 7756 | |
7848 | 7757 | j A Perl internal signed integer value (IV). |
7849 | 7758 | J A Perl internal unsigned integer value (UV). |
7850 | 7759 | |
7851 | 7760 | =end original |
7852 | 7761 | |
7853 | 7762 | j Perl 内部符号付き整数 (IV) |
7854 | 7763 | J Perl 内部符号なし整数 (UV) |
7855 | 7764 | |
7856 | 7765 | =begin original |
7857 | 7766 | |
7858 | 7767 | f A single-precision float in the native format. |
7859 | 7768 | d A double-precision float in the native format. |
7860 | 7769 | |
7861 | 7770 | =end original |
7862 | 7771 | |
7863 | 7772 | f 機種依存の単精度浮動小数点数 |
7864 | 7773 | d 機種依存の倍精度浮動小数点数 |
7865 | 7774 | |
7866 | 7775 | =begin original |
7867 | 7776 | |
7868 | 7777 | F A Perl internal floating point value (NV) in the native format |
7869 | 7778 | D A long double-precision float in the native format. |
7870 | 7779 | (Long doubles are available only if your system supports long |
7871 | 7780 | double values _and_ if Perl has been compiled to support those. |
7872 | 7781 | Causes a fatal error otherwise.) |
7873 | 7782 | |
7874 | 7783 | =end original |
7875 | 7784 | |
7876 | 7785 | F ネイティブフォーマットの Perl 内部浮動小数点数 (NV) |
7877 | 7786 | D ネイティブフォーマットの長い倍精度浮動小数点数(long double) |
7878 | 7787 | (long double は、システムが long double に対応していて、かつ Perl が |
7879 | 7788 | long double 対応としてコンパイルされている場合にのみ使用可能です。 |
7880 | 7789 | それ以外の場合は致命的エラーが発生します。) |
7881 | 7790 | |
7882 | 7791 | =begin original |
7883 | 7792 | |
7884 | 7793 | p A pointer to a null-terminated string. |
7885 | 7794 | P A pointer to a structure (fixed-length string). |
7886 | 7795 | |
7887 | 7796 | =end original |
7888 | 7797 | |
7889 | 7798 | p ヌル文字で終端する文字列へのポインタ |
7890 | 7799 | P 構造体 (固定長文字列) へのポインタ |
7891 | 7800 | |
7892 | 7801 | =begin original |
7893 | 7802 | |
7894 | 7803 | u A uuencoded string. |
7895 | 7804 | U A Unicode character number. Encodes to a character in character mode |
7896 | 7805 | and UTF-8 (or UTF-EBCDIC in EBCDIC platforms) in byte mode. |
7897 | 7806 | |
7898 | 7807 | =end original |
7899 | 7808 | |
7900 | 7809 | u uuencode 文字列 |
7901 | 7810 | U Unicode 文字番号。文字モードでは文字に、バイトモードなら UTF-8 に |
7902 | 7811 | (EBCDIC システムでは UTF-EBCDIC に)エンコードされます |
7903 | 7812 | |
7904 | 7813 | =begin original |
7905 | 7814 | |
7906 | 7815 | w A BER compressed integer (not an ASN.1 BER, see perlpacktut for |
7907 | 7816 | details). Its bytes represent an unsigned integer in base 128, |
7908 | 7817 | most significant digit first, with as few digits as possible. Bit |
7909 | 7818 | eight (the high bit) is set on each byte except the last. |
7910 | 7819 | |
7911 | 7820 | =end original |
7912 | 7821 | |
7913 | 7822 | w A BER 圧縮変数(ASN.1 BER ではありません。詳細については perlpacktut を |
7914 | 7823 | 参照してください)。このバイト列はできるだけ少ない桁数で表現された |
7915 | 7824 | 128 を基とした符号なし整数で、最上位ビットから順に並びます。 |
7916 | 7825 | 最後のバイト以外の各バイトのビット 8 (上位ビット) がセットされます。 |
7917 | 7826 | |
7918 | 7827 | =begin original |
7919 | 7828 | |
7920 | 7829 | x A null byte. |
7921 | 7830 | X Back up a byte. |
7922 | 7831 | @ Null fill or truncate to absolute position, counted from the |
7923 | 7832 | start of the innermost ()-group. |
7924 | 7833 | . Null fill or truncate to absolute position specified by value. |
7925 | 7834 | ( Start of a ()-group. |
7926 | 7835 | |
7927 | 7836 | =end original |
7928 | 7837 | |
7929 | 7838 | x ヌル文字 |
7930 | 7839 | X 1 文字後退 |
7931 | 7840 | @ 一番内側の () の組の開始位置から数えて、絶対位置までヌル文字で |
7932 | 7841 | 埋めるか切り詰める |
7933 | 7842 | . 値で指定した絶対位置までヌル文字で埋めるか切り詰める |
7934 | 7843 | ( () の組の開始 |
7935 | 7844 | |
7936 | 7845 | =begin original |
7937 | 7846 | |
7938 | 7847 | One or more of the modifiers below may optionally follow some letters in the |
7939 | 7848 | TEMPLATE (the second column lists the letters for which the modifier is |
7940 | 7849 | valid): |
7941 | 7850 | |
7942 | 7851 | =end original |
7943 | 7852 | |
7944 | 7853 | 以下に示す一つまたは複数の修飾子を、TEMPLATE の文字のいくつかにオプションで |
7945 | 7854 | 付けることができます(表の 2 列目は、その修飾子が有効な文字です): |
7946 | 7855 | |
7947 | 7856 | =begin original |
7948 | 7857 | |
7949 | 7858 | ! sSlLiI Forces native (short, long, int) sizes instead |
7950 | 7859 | of fixed (16-/32-bit) sizes. |
7951 | 7860 | |
7952 | 7861 | =end original |
7953 | 7862 | |
7954 | 7863 | ! sSlLiI 固定の(16/32 ビット)サイズではなく、ネイティブな |
7955 | 7864 | (short, long, int)サイズを強制する。 |
7956 | 7865 | |
7957 | 7866 | =begin original |
7958 | 7867 | |
7959 | 7868 | xX Make x and X act as alignment commands. |
7960 | 7869 | |
7961 | 7870 | =end original |
7962 | 7871 | |
7963 | 7872 | xX x と X をアライメントコマンドとして振舞わせる。 |
7964 | 7873 | |
7965 | 7874 | =begin original |
7966 | 7875 | |
7967 | 7876 | nNvV Treat integers as signed instead of unsigned. |
7968 | 7877 | |
7969 | 7878 | =end original |
7970 | 7879 | |
7971 | 7880 | nNvV 整数を符号なしではなく符号付きとして扱わせる。 |
7972 | 7881 | |
7973 | 7882 | =begin original |
7974 | 7883 | |
7975 | 7884 | @. Specify position as byte offset in the internal |
7976 | 7885 | representation of the packed string. Efficient but |
7977 | 7886 | dangerous. |
7978 | 7887 | |
7979 | 7888 | =end original |
7980 | 7889 | |
7981 | 7890 | @. pack された内部表現のバイトオフセットとして位置を指定する。 |
7982 | 7891 | 効率的ですが危険です。 |
7983 | 7892 | |
7984 | 7893 | =begin original |
7985 | 7894 | |
7986 | 7895 | > sSiIlLqQ Force big-endian byte-order on the type. |
7987 | 7896 | jJfFdDpP (The "big end" touches the construct.) |
7988 | 7897 | |
7989 | 7898 | =end original |
7990 | 7899 | |
7991 | 7900 | > sSiIlLqQ これらの型のバイト順をビッグエンディアンに強制します |
7992 | 7901 | jJfFdDpP (「大きい端」が構造に触れています) |
7993 | 7902 | |
7994 | 7903 | =begin original |
7995 | 7904 | |
7996 | 7905 | < sSiIlLqQ Force little-endian byte-order on the type. |
7997 | 7906 | jJfFdDpP (The "little end" touches the construct.) |
7998 | 7907 | |
7999 | 7908 | =end original |
8000 | 7909 | |
8001 | 7910 | < sSiIlLqQ これらの型のバイト順をリトルエンディアンに強制します |
8002 | 7911 | jJfFdDpP (「小さい端」が構造に触れています) |
8003 | 7912 | |
8004 | 7913 | =begin original |
8005 | 7914 | |
8006 | 7915 | The C<E<gt>> and C<E<lt>> modifiers can also be used on C<()>-groups, |
8007 | 7916 | in which case they force a certain byte-order on all components of |
8008 | 7917 | that group, including subgroups. |
8009 | 7918 | |
8010 | 7919 | =end original |
8011 | 7920 | |
8012 | 7921 | C<E<gt>> と C<E<lt>> の修飾子は C<()>-グループでも使えます; |
8013 | 7922 | この場合はそのグループと副グループ内の全ての要素を特定のバイト順に |
8014 | 7923 | 強制します。 |
8015 | 7924 | |
8016 | 7925 | =begin original |
8017 | 7926 | |
8018 | 7927 | The following rules apply: |
8019 | 7928 | |
8020 | 7929 | =end original |
8021 | 7930 | |
8022 | 7931 | 以下の条件が適用されます: |
8023 | 7932 | |
8024 | 7933 | =over 8 |
8025 | 7934 | |
8026 | 7935 | =item * |
8027 | 7936 | |
8028 | 7937 | =begin original |
8029 | 7938 | |
8030 | 7939 | Each letter may optionally be followed by a number giving a repeat |
8031 | 7940 | count. With all types except C<a>, C<A>, C<Z>, C<b>, C<B>, C<h>, |
8032 | 7941 | C<H>, C<@>, C<.>, C<x>, C<X> and C<P> the pack function will gobble up |
8033 | 7942 | that many values from the LIST. A C<*> for the repeat count means to |
8034 | 7943 | use however many items are left, except for C<@>, C<x>, C<X>, where it |
8035 | 7944 | is equivalent to C<0>, for <.> where it means relative to string start |
8036 | 7945 | and C<u>, where it is equivalent to 1 (or 45, which is the same). |
8037 | 7946 | A numeric repeat count may optionally be enclosed in brackets, as in |
8038 | 7947 | C<pack 'C[80]', @arr>. |
8039 | 7948 | |
8040 | 7949 | =end original |
8041 | 7950 | |
8042 | 7951 | これらの文字の後には、繰り返し数を示す数字を付けることができます。 |
8043 | 7952 | C<a>, C<A>, C<Z>, C<b>, C<B>, C<h>, C<H>, C<@>, C<.>, C<x>, C<X>, C<P> |
8044 | 7953 | 以外の全ての型では、LIST からその数の値を取り出して使います。 |
8045 | 7954 | 繰り返し数に C<*> を指定すると、その時点で残っているすべての要素を意味します。 |
8046 | 7955 | ただし、C<@>, C<x>, C<X> では C<0> と等価で、 |
8047 | 7956 | C<.> では文字列の開始からの相対位置を意味し、 |
8048 | 7957 | C<u> では 1 (あるいは 45 でも同じ) と等価です。 |
8049 | 7958 | 数値の繰り返し数は C<pack 'C[80]', @arr> のように大かっこで囲むこともできます。 |
8050 | 7959 | |
8051 | 7960 | =begin original |
8052 | 7961 | |
8053 | 7962 | One can replace the numeric repeat count by a template enclosed in brackets; |
8054 | 7963 | then the packed length of this template in bytes is used as a count. |
8055 | 7964 | For example, C<x[L]> skips a long (it skips the number of bytes in a long); |
8056 | 7965 | the template C<$t X[$t] $t> unpack()s twice what $t unpacks. |
8057 | 7966 | If the template in brackets contains alignment commands (such as C<x![d]>), |
8058 | 7967 | its packed length is calculated as if the start of the template has the maximal |
8059 | 7968 | possible alignment. |
8060 | 7969 | |
8061 | 7970 | =end original |
8062 | 7971 | |
8063 | 7972 | 大かっこで囲まれたテンプレートで数値の繰り返し数を置き換えることができます; |
8064 | 7973 | このテンプレートでパックされたバイト長が繰り返し数として使われます。 |
8065 | 7974 | 例えば、C<x[L]> は long をスキップします(long のバイト数だけスキップします); |
8066 | 7975 | テンプレート C<$t X[$t] $t> は $t を unpack したものの 2 倍を |
8067 | 7976 | unpack() します。 |
8068 | 7977 | (C<x![d]> のように) 大かっこにアライメントコマンドが含まれている場合、 |
8069 | 7978 | パックされた長さは、テンプレートの先頭で最大限可能なアライメントを |
8070 | 7979 | 持っているものとして計算されます。 |
8071 | 7980 | |
8072 | 7981 | =begin original |
8073 | 7982 | |
8074 | 7983 | When used with C<Z>, C<*> results in the addition of a trailing null |
8075 | 7984 | byte (so the packed result will be one longer than the byte C<length> |
8076 | 7985 | of the item). |
8077 | 7986 | |
8078 | 7987 | =end original |
8079 | 7988 | |
8080 | 7989 | C<*> が C<Z> と共に使われた場合、末尾にヌルバイトをつけます |
8081 | 7990 | (従ってパックされた結果は要素の C<length> の値より 1 大きくなります)。 |
8082 | 7991 | |
8083 | 7992 | =begin original |
8084 | 7993 | |
8085 | 7994 | When used with C<@>, the repeat count represents an offset from the start |
8086 | 7995 | of the innermost () group. |
8087 | 7996 | |
8088 | 7997 | =end original |
8089 | 7998 | |
8090 | 7999 | C<@> を使うと、繰り返し数は一番内側の () グループの先頭からのオフセットを |
8091 | 8000 | 表現します。 |
8092 | 8001 | |
8093 | 8002 | =begin original |
8094 | 8003 | |
8095 | 8004 | When used with C<.>, the repeat count is used to determine the starting |
8096 | 8005 | position from where the value offset is calculated. If the repeat count |
8097 | 8006 | is 0, it's relative to the current position. If the repeat count is C<*>, |
8098 | 8007 | the offset is relative to the start of the packed string. And if its an |
8099 | 8008 | integer C<n> the offset is relative to the start of the n-th innermost |
8100 | 8009 | () group (or the start of the string if C<n> is bigger then the group |
8101 | 8010 | level). |
8102 | 8011 | |
8103 | 8012 | =end original |
8104 | 8013 | |
8105 | 8014 | C<.> で使われると、繰り返し数は値のオフセットを計算するための開始位置を |
8106 | 8015 | 決定するために使われます。 |
8107 | 8016 | 繰り返し数が 0 なら、現在位置からの相対位置となります。 |
8108 | 8017 | 繰り返し数が C<*> なら、オフセットは pack された文字列の先頭からの相対位置です。 |
8109 | 8018 | そして整数 C<n> なら、オフセットは一番内側から n 番目の () グループの先頭 |
8110 | 8019 | (または C<n> がグループレベルより大きい場合は文字列の先頭)からの |
8111 | 8020 | 相対位置です。 |
8112 | 8021 | |
8113 | 8022 | =begin original |
8114 | 8023 | |
8115 | 8024 | The repeat count for C<u> is interpreted as the maximal number of bytes |
8116 | 8025 | to encode per line of output, with 0, 1 and 2 replaced by 45. The repeat |
8117 | 8026 | count should not be more than 65. |
8118 | 8027 | |
8119 | 8028 | =end original |
8120 | 8029 | |
8121 | 8030 | C<u> での繰り返し回数は、出力行毎に最大何バイトまでをエンコードするかを |
8122 | 8031 | 示します。0, 1, 2 は 45 として扱われます。 |
8123 | 8032 | 繰り返し数は 65 を超えてはなりません。 |
8124 | 8033 | |
8125 | 8034 | =item * |
8126 | 8035 | |
8127 | 8036 | =begin original |
8128 | 8037 | |
8129 | 8038 | The C<a>, C<A>, and C<Z> types gobble just one value, but pack it as a |
8130 | 8039 | string of length count, padding with nulls or spaces as necessary. When |
8131 | 8040 | unpacking, C<A> strips trailing whitespace and nulls, C<Z> strips everything |
8132 | 8041 | after the first null, and C<a> returns data verbatim. |
8133 | 8042 | |
8134 | 8043 | =end original |
8135 | 8044 | |
8136 | 8045 | C<a>, C<A>, C<Z> という型を使うと、値を一つだけ取り出して使いますが、 |
8137 | 8046 | 繰り返し数で示す長さの文字列となるように、必要に応じてヌル文字か |
8138 | 8047 | スペース文字を付け足します。 |
8139 | 8048 | unpack するとき、C<A> は後続の空白やヌル文字を取り除きます。 |
8140 | 8049 | C<Z>は最初のヌル文字以降の全てを取り除きます。 |
8141 | 8050 | C<a>はデータをそのまま返します。 |
8142 | 8051 | |
8143 | 8052 | =begin original |
8144 | 8053 | |
8145 | 8054 | If the value-to-pack is too long, it is truncated. If too long and an |
8146 | 8055 | explicit count is provided, C<Z> packs only C<$count-1> bytes, followed |
8147 | 8056 | by a null byte. Thus C<Z> always packs a trailing null (except when the |
8148 | 8057 | count is 0). |
8149 | 8058 | |
8150 | 8059 | =end original |
8151 | 8060 | |
8152 | 8061 | pack する値が長すぎる場合、切り詰められます。 |
8153 | 8062 | 長すぎてかつ明示的に個数が指定されている場合、 |
8154 | 8063 | C<Z> は C<$count-1> バイトまで pack し、その後にヌルバイトがつきます。 |
8155 | 8064 | 従って、C<Z> は(繰り返し数が 0 の場合を除いて)常に末尾にヌルバイトが |
8156 | 8065 | つきます。 |
8157 | 8066 | |
8158 | 8067 | =item * |
8159 | 8068 | |
8160 | 8069 | =begin original |
8161 | 8070 | |
8162 | 8071 | Likewise, the C<b> and C<B> fields pack a string that many bits long. |
8163 | 8072 | Each character of the input field of pack() generates 1 bit of the result. |
8164 | 8073 | Each result bit is based on the least-significant bit of the corresponding |
8165 | 8074 | input character, i.e., on C<ord($char)%2>. In particular, characters C<"0"> |
8166 | 8075 | and C<"1"> generate bits 0 and 1, as do characters C<"\0"> and C<"\1">. |
8167 | 8076 | |
8168 | 8077 | =end original |
8169 | 8078 | |
8170 | 8079 | 同様に、C<b> や C<B> は、繰り返し数で示すビット長のビット列に pack します。 |
8171 | 8080 | pack() の入力フィールドの各文字は結果の 1 ビットを生成します。 |
8172 | 8081 | 結果ビットのそれぞれは対応する入力文字の最下位ビットを基にします |
8173 | 8082 | (つまり C<ord($char)%2>)。 |
8174 | 8083 | 特に、文字 C<"0"> と C<"1"> は文字 C<"\0"> と C<"\1"> と同様に、 |
8175 | 8084 | ビット 0 と 1 を生成します。 |
8176 | 8085 | |
8177 | 8086 | =begin original |
8178 | 8087 | |
8179 | 8088 | Starting from the beginning of the input string of pack(), each 8-tuple |
8180 | 8089 | of characters is converted to 1 character of output. With format C<b> |
8181 | 8090 | the first character of the 8-tuple determines the least-significant bit of a |
8182 | 8091 | character, and with format C<B> it determines the most-significant bit of |
8183 | 8092 | a character. |
8184 | 8093 | |
8185 | 8094 | =end original |
8186 | 8095 | |
8187 | 8096 | pack() の入力文字列の先頭から始めて、8 タプル毎に 1 文字の出力に |
8188 | 8097 | 変換されます。 |
8189 | 8098 | C<b> フォーマットでは 8 タプルの最初の文字が出力の最下位ビットとなり、 |
8190 | 8099 | C<B> フォーマットでは出力の最上位ビットとなります。 |
8191 | 8100 | |
8192 | 8101 | =begin original |
8193 | 8102 | |
8194 | 8103 | If the length of the input string is not exactly divisible by 8, the |
8195 | 8104 | remainder is packed as if the input string were padded by null characters |
8196 | 8105 | at the end. Similarly, during unpack()ing the "extra" bits are ignored. |
8197 | 8106 | |
8198 | 8107 | =end original |
8199 | 8108 | |
8200 | 8109 | もし入力文字列の長さが 8 で割り切れない場合、余りの部分は入力文字列の |
8201 | 8110 | 最後にヌル文字がパッディングされているものとしてパックされます。 |
8202 | 8111 | 同様に、unpack() 中は「余分な」ビットは無視されます。 |
8203 | 8112 | |
8204 | 8113 | =begin original |
8205 | 8114 | |
8206 | 8115 | If the input string of pack() is longer than needed, extra characters are |
8207 | 8116 | ignored. A C<*> for the repeat count of pack() means to use all the |
8208 | 8117 | characters of the input field. On unpack()ing the bits are converted to a |
8209 | 8118 | string of C<"0">s and C<"1">s. |
8210 | 8119 | |
8211 | 8120 | =end original |
8212 | 8121 | |
8213 | 8122 | pack() の入力文字列が必要な分よりも長い場合、余分な文字は無視されます。 |
8214 | 8123 | pack() の繰り返し数として C<*> が指定されると、入力フィールドの全ての |
8215 | 8124 | 文字が使われます。 |
8216 | 8125 | unpack() 時にはビット列は C<"0"> と C<"1"> の文字列に変換されます。 |
8217 | 8126 | |
8218 | 8127 | =item * |
8219 | 8128 | |
8220 | 8129 | =begin original |
8221 | 8130 | |
8222 | 8131 | The C<h> and C<H> fields pack a string that many nybbles (4-bit groups, |
8223 | 8132 | representable as hexadecimal digits, 0-9a-f) long. |
8224 | 8133 | |
8225 | 8134 | =end original |
8226 | 8135 | |
8227 | 8136 | C<h> や C<H> は、多ニブル長(16 進文字である 0-9a-f で表現可能な |
8228 | 8137 | 4 ビットグループ)のニブル列に pack します。 |
8229 | 8138 | |
8230 | 8139 | =begin original |
8231 | 8140 | |
8232 | 8141 | Each character of the input field of pack() generates 4 bits of the result. |
8233 | 8142 | For non-alphabetical characters the result is based on the 4 least-significant |
8234 | 8143 | bits of the input character, i.e., on C<ord($char)%16>. In particular, |
8235 | 8144 | characters C<"0"> and C<"1"> generate nybbles 0 and 1, as do bytes |
8236 | 8145 | C<"\0"> and C<"\1">. For characters C<"a".."f"> and C<"A".."F"> the result |
8237 | 8146 | is compatible with the usual hexadecimal digits, so that C<"a"> and |
8238 | 8147 | C<"A"> both generate the nybble C<0xa==10>. The result for characters |
8239 | 8148 | C<"g".."z"> and C<"G".."Z"> is not well-defined. |
8240 | 8149 | |
8241 | 8150 | =end original |
8242 | 8151 | |
8243 | 8152 | pack() の入力フィールドの各文字は結果の 4 ビットを生成します。 |
8244 | 8153 | 英字でない文字の場合、結果は入力文字の下位 4 ビットを |
8245 | 8154 | 基にします(つまり C<ord($char)%16>)。 |
8246 | 8155 | 特に、文字 C<"0"> と C<"1"> はバイト C<"\0"> と C<"\1"> と同様に |
8247 | 8156 | ニブル 0 と 1 を生成します。 |
8248 | 8157 | 文字 C<"a".."f"> と C<"A".."F"> の場合は結果は通常の |
8249 | 8158 | 16 進数と同じ結果になりますので、C<"a"> と C<"A"> はどちらも |
8250 | 8159 | ニブル C<0xa==10> を生成します。 |
8251 | 8160 | 文字 C<"g".."z"> と C<"G".."Z"> に対する結果は未定義です。 |
8252 | 8161 | |
8253 | 8162 | =begin original |
8254 | 8163 | |
8255 | 8164 | Starting from the beginning of the input string of pack(), each pair |
8256 | 8165 | of characters is converted to 1 character of output. With format C<h> the |
8257 | 8166 | first character of the pair determines the least-significant nybble of the |
8258 | 8167 | output character, and with format C<H> it determines the most-significant |
8259 | 8168 | nybble. |
8260 | 8169 | |
8261 | 8170 | =end original |
8262 | 8171 | |
8263 | 8172 | pack() の入力文字列の先頭から始めて、2 文字毎に 1 文字の出力に |
8264 | 8173 | 変換されます。 |
8265 | 8174 | C<h> フォーマットでは 1 文字目が出力の最下位ニブルとなり、 |
8266 | 8175 | C<H> フォーマットでは出力の最上位ニブルとなります。 |
8267 | 8176 | |
8268 | 8177 | =begin original |
8269 | 8178 | |
8270 | 8179 | If the length of the input string is not even, it behaves as if padded |
8271 | 8180 | by a null character at the end. Similarly, during unpack()ing the "extra" |
8272 | 8181 | nybbles are ignored. |
8273 | 8182 | |
8274 | 8183 | =end original |
8275 | 8184 | |
8276 | 8185 | 入力文字列の長さが偶数でない場合、最後にヌル文字でパッディングされて |
8277 | 8186 | いるかのように振る舞います。 |
8278 | 8187 | 同様に、unpack() 中は「余分な」ニブルは無視されます。 |
8279 | 8188 | |
8280 | 8189 | =begin original |
8281 | 8190 | |
8282 | 8191 | If the input string of pack() is longer than needed, extra characters are |
8283 | 8192 | ignored. |
8284 | 8193 | A C<*> for the repeat count of pack() means to use all the characters of |
8285 | 8194 | the input field. On unpack()ing the nybbles are converted to a string |
8286 | 8195 | of hexadecimal digits. |
8287 | 8196 | |
8288 | 8197 | =end original |
8289 | 8198 | |
8290 | 8199 | pack() の入力文字列が必要な分より長い場合、余分な部分は無視されます。 |
8291 | 8200 | pack() の繰り返し数として C<*> が指定されると、入力フィールドの全ての |
8292 | 8201 | 文字が使われます。 |
8293 | 8202 | unpack() 時にはニブルは 16 進数の文字列に変換されます。 |
8294 | 8203 | |
8295 | 8204 | =item * |
8296 | 8205 | |
8297 | 8206 | =begin original |
8298 | 8207 | |
8299 | 8208 | The C<p> type packs a pointer to a null-terminated string. You are |
8300 | 8209 | responsible for ensuring the string is not a temporary value (which can |
8301 | 8210 | potentially get deallocated before you get around to using the packed result). |
8302 | 8211 | The C<P> type packs a pointer to a structure of the size indicated by the |
8303 | 8212 | length. A NULL pointer is created if the corresponding value for C<p> or |
8304 | 8213 | C<P> is C<undef>, similarly for unpack(). |
8305 | 8214 | |
8306 | 8215 | =end original |
8307 | 8216 | |
8308 | 8217 | C<p>は、ヌル文字終端文字列へのポインタを pack します。 |
8309 | 8218 | 文字列が一時的な値でない(つまり pack された結果を使う前に文字列が |
8310 | 8219 | 解放されない) ことに責任を持つ必要があります |
8311 | 8220 | C<P> は、指定した長さの構造体へのポインタを pack します。 |
8312 | 8221 | C<p>またはC<P>に対応する値が C<undef> だった場合、 |
8313 | 8222 | unpack() と同様にヌルポインタが作成されます。 |
8314 | 8223 | |
8315 | 8224 | =begin original |
8316 | 8225 | |
8317 | 8226 | If your system has a strange pointer size (i.e. a pointer is neither as |
8318 | 8227 | big as an int nor as big as a long), it may not be possible to pack or |
8319 | 8228 | unpack pointers in big- or little-endian byte order. Attempting to do |
8320 | 8229 | so will result in a fatal error. |
8321 | 8230 | |
8322 | 8231 | =end original |
8323 | 8232 | |
8324 | 8233 | システムのポインタが変わったサイズの場合(つまり、int の大きさでも |
8325 | 8234 | long の大きさでもない場合) ポインタをビッグエンディアンやリトルエンディアンの |
8326 | 8235 | バイト順で pack や unpack することはできません。 |
8327 | 8236 | そうしようとすると致命的エラーになります。 |
8328 | 8237 | |
8329 | 8238 | =item * |
8330 | 8239 | |
8331 | 8240 | =begin original |
8332 | 8241 | |
8333 | 8242 | The C</> template character allows packing and unpacking of a sequence of |
8334 | 8243 | items where the packed structure contains a packed item count followed by |
8335 | 8244 | the packed items themselves. |
8336 | 8245 | |
8337 | 8246 | =end original |
8338 | 8247 | |
8339 | 8248 | C</> テンプレート文字は、アイテムの数の後にアイテムそのものが入っている形の |
8340 | 8249 | アイテム列を pack 及び unpack します。 |
8341 | 8250 | |
8342 | 8251 | =begin original |
8343 | 8252 | |
8344 | 8253 | For C<pack> you write I<length-item>C</>I<sequence-item> and the |
8345 | 8254 | I<length-item> describes how the length value is packed. The ones likely |
8346 | 8255 | to be of most use are integer-packing ones like C<n> (for Java strings), |
8347 | 8256 | C<w> (for ASN.1 or SNMP) and C<N> (for Sun XDR). |
8348 | 8257 | |
8349 | 8258 | =end original |
8350 | 8259 | |
8351 | 8260 | C<pack> では I<length-item>C</>I<string-item> の形になり、 |
8352 | 8261 | I<length-item> は長さの値がどのように pack されているかを指定します。 |
8353 | 8262 | もっともよく使われるのは C<n>(Java 文字列), C<w>(SNMP), C<N>(Sun XDR) と |
8354 | 8263 | いった整数型です。 |
8355 | 8264 | |
8356 | 8265 | =begin original |
8357 | 8266 | |
8358 | 8267 | For C<pack>, the I<sequence-item> may have a repeat count, in which case |
8359 | 8268 | the minimum of that and the number of available items is used as argument |
8360 | 8269 | for the I<length-item>. If it has no repeat count or uses a '*', the number |
8361 | 8270 | of available items is used. |
8362 | 8271 | |
8363 | 8272 | =end original |
8364 | 8273 | |
8365 | 8274 | C<pack> では、I<sequence-item> は繰り返し数を持つことがあり、その場合は |
8366 | 8275 | その最小値と利用可能なアイテムの数は I<length-item> のための引数として |
8367 | 8276 | 使われます。 |
8368 | 8277 | 繰り返し数がなかったり、'*' を使うと、利用可能なアイテムの数が使われます。 |
8369 | 8278 | |
8370 | 8279 | =begin original |
8371 | 8280 | |
8372 | 8281 | For C<unpack> an internal stack of integer arguments unpacked so far is |
8373 | 8282 | used. You write C</>I<sequence-item> and the repeat count is obtained by |
8374 | 8283 | popping off the last element from the stack. The I<sequence-item> must not |
8375 | 8284 | have a repeat count. |
8376 | 8285 | |
8377 | 8286 | =end original |
8378 | 8287 | |
8379 | 8288 | C<unpack> では、今まで unpack した数値引数の内部スタックが使われます。 |
8380 | 8289 | C</>I<sequence-item> と書いて、繰り返し数はスタックから最後の要素を |
8381 | 8290 | 取り出すことで得ます。 |
8382 | 8291 | I<sequence-item> は繰り返し数を持っていてはいけません。 |
8383 | 8292 | |
8384 | 8293 | =begin original |
8385 | 8294 | |
8386 | 8295 | If the I<sequence-item> refers to a string type (C<"A">, C<"a"> or C<"Z">), |
8387 | 8296 | the I<length-item> is a string length, not a number of strings. If there is |
8388 | 8297 | an explicit repeat count for pack, the packed string will be adjusted to that |
8389 | 8298 | given length. |
8390 | 8299 | |
8391 | 8300 | =end original |
8392 | 8301 | |
8393 | 8302 | I<sequence-item> が文字列型 (C<"A">, C<"a">, C<"Z">) を参照している場合、 |
8394 | 8303 | I<length-item> は文字列の数ではなく、文字列の長さです。 |
8395 | 8304 | pack で明示的な繰り返し数があると、pack された文字列は与えられた |
8396 | 8305 | 長さに調整されます。 |
8397 | 8306 | |
8398 | 8307 | unpack 'W/a', "\04Gurusamy"; gives ('Guru') |
8399 | 8308 | unpack 'a3/A A*', '007 Bond J '; gives (' Bond', 'J') |
8400 | 8309 | unpack 'a3 x2 /A A*', '007: Bond, J.'; gives ('Bond, J', '.') |
8401 | 8310 | pack 'n/a* w/a','hello,','world'; gives "\000\006hello,\005world" |
8402 | 8311 | pack 'a/W2', ord('a') .. ord('z'); gives '2ab' |
8403 | 8312 | |
8404 | 8313 | =begin original |
8405 | 8314 | |
8406 | 8315 | The I<length-item> is not returned explicitly from C<unpack>. |
8407 | 8316 | |
8408 | 8317 | =end original |
8409 | 8318 | |
8410 | 8319 | I<length-item> は C<unpack> から明示的には返されません。 |
8411 | 8320 | |
8412 | 8321 | =begin original |
8413 | 8322 | |
8414 | 8323 | Adding a count to the I<length-item> letter is unlikely to do anything |
8415 | 8324 | useful, unless that letter is C<A>, C<a> or C<Z>. Packing with a |
8416 | 8325 | I<length-item> of C<a> or C<Z> may introduce C<"\000"> characters, |
8417 | 8326 | which Perl does not regard as legal in numeric strings. |
8418 | 8327 | |
8419 | 8328 | =end original |
8420 | 8329 | |
8421 | 8330 | I<length-item> 文字に繰り返し数をつけるのは |
8422 | 8331 | 文字が C<A>, C<a>, C<Z> でない限りは有用ではありません。 |
8423 | 8332 | C<a> や C<Z> を I<length-item> としてパックすると C<"\000"> 文字が |
8424 | 8333 | 出力されることがあり、Perl はこれを有効な数値文字列として認識しません。 |
8425 | 8334 | |
8426 | 8335 | =item * |
8427 | 8336 | |
8428 | 8337 | =begin original |
8429 | 8338 | |
8430 | 8339 | The integer types C<s>, C<S>, C<l>, and C<L> may be |
8431 | 8340 | followed by a C<!> modifier to signify native shorts or |
8432 | 8341 | longs--as you can see from above for example a bare C<l> does mean |
8433 | 8342 | exactly 32 bits, the native C<long> (as seen by the local C compiler) |
8434 | 8343 | may be larger. This is an issue mainly in 64-bit platforms. You can |
8435 | 8344 | see whether using C<!> makes any difference by |
8436 | 8345 | |
8437 | 8346 | =end original |
8438 | 8347 | |
8439 | 8348 | C<s>, C<S>, C<l>, C<L> の整数タイプに引き続いて C<!> 修飾子を |
8440 | 8349 | つけることで、ネイティブの short や long を指定できます -- |
8441 | 8350 | 上述のように、例えば C<l> は正確に 32 ビットであり、ネイティブな |
8442 | 8351 | (ローカルな C コンパイラによる)C<long> はもっと大きいかもしれません。 |
8443 | 8352 | これは主に 64 ビットプラットフォームで意味があります。 |
8444 | 8353 | C<!> を使うことによって違いがあるかどうかは以下のようにして調べられます: |
8445 | 8354 | |
8446 | 8355 | print length(pack("s")), " ", length(pack("s!")), "\n"; |
8447 | 8356 | print length(pack("l")), " ", length(pack("l!")), "\n"; |
8448 | 8357 | |
8449 | 8358 | =begin original |
8450 | 8359 | |
8451 | 8360 | C<i!> and C<I!> also work but only because of completeness; |
8452 | 8361 | they are identical to C<i> and C<I>. |
8453 | 8362 | |
8454 | 8363 | =end original |
8455 | 8364 | |
8456 | 8365 | C<i!> と C<I!> も動作しますが、単に完全性のためだけです; |
8457 | 8366 | これは C<i> 及び C<I> と同じです。 |
8458 | 8367 | |
8459 | 8368 | =begin original |
8460 | 8369 | |
8461 | 8370 | The actual sizes (in bytes) of native shorts, ints, longs, and long |
8462 | 8371 | longs on the platform where Perl was built are also available via |
8463 | 8372 | L<Config>: |
8464 | 8373 | |
8465 | 8374 | =end original |
8466 | 8375 | |
8467 | 8376 | Perl がビルドされたプラットフォームでの short, int, long, long long の |
8468 | 8377 | 実際の(バイト数での)サイズは L<Config> でも得られます。 |
8469 | 8378 | |
8470 | 8379 | use Config; |
8471 | 8380 | print $Config{shortsize}, "\n"; |
8472 | 8381 | print $Config{intsize}, "\n"; |
8473 | 8382 | print $Config{longsize}, "\n"; |
8474 | 8383 | print $Config{longlongsize}, "\n"; |
8475 | 8384 | |
8476 | 8385 | =begin original |
8477 | 8386 | |
8478 | 8387 | (The C<$Config{longlongsize}> will be undefined if your system does |
8479 | 8388 | not support long longs.) |
8480 | 8389 | |
8481 | 8390 | =end original |
8482 | 8391 | |
8483 | 8392 | (システムが long long に対応していない場合は C<$Config{longlongsize}> は |
8484 | 8393 | 未定義値になります。) |
8485 | 8394 | |
8486 | 8395 | =item * |
8487 | 8396 | |
8488 | 8397 | =begin original |
8489 | 8398 | |
8490 | 8399 | The integer formats C<s>, C<S>, C<i>, C<I>, C<l>, C<L>, C<j>, and C<J> |
8491 | 8400 | are inherently non-portable between processors and operating systems |
8492 | 8401 | because they obey the native byteorder and endianness. For example a |
8493 | 8402 | 4-byte integer 0x12345678 (305419896 decimal) would be ordered natively |
8494 | 8403 | (arranged in and handled by the CPU registers) into bytes as |
8495 | 8404 | |
8496 | 8405 | =end original |
8497 | 8406 | |
8498 | 8407 | 整数フォーマット C<s>, C<S>, C<i>, C<I>, C<l>, C<L>, C<j>, C<J> は |
8499 | 8408 | ネイティブなバイト順序とエンディアンに従っているため、 |
8500 | 8409 | 本質的にプロセッサ間や OS 間で移植性がありません。 |
8501 | 8410 | 例えば 4 バイトの整数 0x12345678 (10 進数では 305419896) は |
8502 | 8411 | 内部では(CPU レジスタによって変換され扱われる形では) |
8503 | 8412 | 以下のようなバイト列に並べられます: |
8504 | 8413 | |
8505 | 8414 | 0x12 0x34 0x56 0x78 # big-endian |
8506 | 8415 | 0x78 0x56 0x34 0x12 # little-endian |
8507 | 8416 | |
8508 | 8417 | =begin original |
8509 | 8418 | |
8510 | 8419 | Basically, the Intel and VAX CPUs are little-endian, while everybody |
8511 | 8420 | else, for example Motorola m68k/88k, PPC, Sparc, HP PA, Power, and |
8512 | 8421 | Cray are big-endian. Alpha and MIPS can be either: Digital/Compaq |
8513 | 8422 | used/uses them in little-endian mode; SGI/Cray uses them in big-endian |
8514 | 8423 | mode. |
8515 | 8424 | |
8516 | 8425 | =end original |
8517 | 8426 | |
8518 | 8427 | 基本的に、Intel と VAX の CPU はリトルエンディアンです。 |
8519 | 8428 | 一方それ以外、例えば Motorola m68k/88k, PPC, Sparc, HP PA, Power, |
8520 | 8429 | Cray などはビッグエンディアンです。 |
8521 | 8430 | Alpha と MIPS は両方ともあります: |
8522 | 8431 | Digital/Compaq はリトルエンディアンモードで使っています(いました); |
8523 | 8432 | SGI/Cray はビッグエンディアンモードで使っています。 |
8524 | 8433 | |
8525 | 8434 | =begin original |
8526 | 8435 | |
8527 | 8436 | The names `big-endian' and `little-endian' are comic references to |
8528 | 8437 | the classic "Gulliver's Travels" (via the paper "On Holy Wars and a |
8529 | 8438 | Plea for Peace" by Danny Cohen, USC/ISI IEN 137, April 1, 1980) and |
8530 | 8439 | the egg-eating habits of the Lilliputians. |
8531 | 8440 | |
8532 | 8441 | =end original |
8533 | 8442 | |
8534 | 8443 | 「ビッグエンディアン」と「リトルエンディアン」の名前は |
8535 | 8444 | 古典である「ガリバー旅行記」とリリパット族の卵を食べる習慣から |
8536 | 8445 | 取られています。 |
8537 | 8446 | |
8538 | 8447 | =begin original |
8539 | 8448 | |
8540 | 8449 | Some systems may have even weirder byte orders such as |
8541 | 8450 | |
8542 | 8451 | =end original |
8543 | 8452 | |
8544 | 8453 | 以下のような、さらに変わったバイト順序を持つシステムもあるかもしれません: |
8545 | 8454 | |
8546 | 8455 | 0x56 0x78 0x12 0x34 |
8547 | 8456 | 0x34 0x12 0x78 0x56 |
8548 | 8457 | |
8549 | 8458 | =begin original |
8550 | 8459 | |
8551 | 8460 | You can see your system's preference with |
8552 | 8461 | |
8553 | 8462 | =end original |
8554 | 8463 | |
8555 | 8464 | システムの設定は以下のようにして見ることができます: |
8556 | 8465 | |
8557 | 8466 | print join(" ", map { sprintf "%#02x", $_ } |
8558 | 8467 | unpack("W*",pack("L",0x12345678))), "\n"; |
8559 | 8468 | |
8560 | 8469 | =begin original |
8561 | 8470 | |
8562 | 8471 | The byteorder on the platform where Perl was built is also available |
8563 | 8472 | via L<Config>: |
8564 | 8473 | |
8565 | 8474 | =end original |
8566 | 8475 | |
8567 | 8476 | Perl がビルドされたプラットフォームでのバイト順序は |
8568 | 8477 | L<Config> でも知ることができます: |
8569 | 8478 | |
8570 | 8479 | use Config; |
8571 | 8480 | print $Config{byteorder}, "\n"; |
8572 | 8481 | |
8573 | 8482 | =begin original |
8574 | 8483 | |
8575 | 8484 | Byteorders C<'1234'> and C<'12345678'> are little-endian, C<'4321'> |
8576 | 8485 | and C<'87654321'> are big-endian. |
8577 | 8486 | |
8578 | 8487 | =end original |
8579 | 8488 | |
8580 | 8489 | C<'1234'> と C<'12345678'> はリトルエンディアン、 |
8581 | 8490 | C<'4321'> と C<'87654321'> はビッグエンディアンです。 |
8582 | 8491 | |
8583 | 8492 | =begin original |
8584 | 8493 | |
8585 | 8494 | If you want portable packed integers you can either use the formats |
8586 | 8495 | C<n>, C<N>, C<v>, and C<V>, or you can use the C<E<gt>> and C<E<lt>> |
8587 | 8496 | modifiers. These modifiers are only available as of perl 5.9.2. |
8588 | 8497 | See also L<perlport>. |
8589 | 8498 | |
8590 | 8499 | =end original |
8591 | 8500 | |
8592 | 8501 | 移植性のあるパック化された整数がほしい場合は、 |
8593 | 8502 | C<n>, C<N>, C<v>, C<V> フォーマットを使うか、C<E<gt>> と C<E<lt>> の |
8594 | 8503 | 修飾子が使えます。 |
8595 | 8504 | これらの修飾子は perl 5.9.2 以降でのみ使えます。 |
8596 | 8505 | L<perlport> も参照して下さい。 |
8597 | 8506 | |
8598 | 8507 | =item * |
8599 | 8508 | |
8600 | 8509 | =begin original |
8601 | 8510 | |
8602 | 8511 | All integer and floating point formats as well as C<p> and C<P> and |
8603 | 8512 | C<()>-groups may be followed by the C<E<gt>> or C<E<lt>> modifiers |
8604 | 8513 | to force big- or little- endian byte-order, respectively. |
8605 | 8514 | This is especially useful, since C<n>, C<N>, C<v> and C<V> don't cover |
8606 | 8515 | signed integers, 64-bit integers and floating point values. However, |
8607 | 8516 | there are some things to keep in mind. |
8608 | 8517 | |
8609 | 8518 | =end original |
8610 | 8519 | |
8611 | 8520 | C<p> と C<P> と C<()> グループと同様、全ての整数と浮動小数点数の |
8612 | 8521 | フォーマットは C<E<gt>> や C<E<lt>> の修飾子をつけることで、それぞれ |
8613 | 8522 | ビッグエンディアンとリトルエンディアンに強制させることができます。 |
8614 | 8523 | C<n>, C<N>, C<v> and C<V> は符号付き整数、64 ビット整数、浮動小数点数に |
8615 | 8524 | 対応していないので、これは特に有用です。 |
8616 | 8525 | しかし、心に留めておくべきことがいくつかあります。 |
8617 | 8526 | |
8618 | 8527 | =begin original |
8619 | 8528 | |
8620 | 8529 | Exchanging signed integers between different platforms only works |
8621 | 8530 | if all platforms store them in the same format. Most platforms store |
8622 | 8531 | signed integers in two's complement, so usually this is not an issue. |
8623 | 8532 | |
8624 | 8533 | =end original |
8625 | 8534 | |
8626 | 8535 | 異なったプラットフォームで符号付き整数を交換することは、全ての |
8627 | 8536 | プラットフォームで同じフォーマットで保存されている場合にのみうまくいきます。 |
8628 | 8537 | ほとんどのプラットフォームでは符号付き整数は 2 の補数で保存するので、 |
8629 | 8538 | 普通はこれは問題になりません。 |
8630 | 8539 | |
8631 | 8540 | =begin original |
8632 | 8541 | |
8633 | 8542 | The C<E<gt>> or C<E<lt>> modifiers can only be used on floating point |
8634 | 8543 | formats on big- or little-endian machines. Otherwise, attempting to |
8635 | 8544 | do so will result in a fatal error. |
8636 | 8545 | |
8637 | 8546 | =end original |
8638 | 8547 | |
8639 | 8548 | C<E<gt>> や C<E<lt>> の修飾子はビッグエンディアンやリトルエンディアンの |
8640 | 8549 | マシンでの浮動小数点フォーマットでのみ使えます。 |
8641 | 8550 | それ以外では、そのような試みは致命的エラーとなります。 |
8642 | 8551 | |
8643 | 8552 | =begin original |
8644 | 8553 | |
8645 | 8554 | Forcing big- or little-endian byte-order on floating point values for |
8646 | 8555 | data exchange can only work if all platforms are using the same |
8647 | 8556 | binary representation (e.g. IEEE floating point format). Even if all |
8648 | 8557 | platforms are using IEEE, there may be subtle differences. Being able |
8649 | 8558 | to use C<E<gt>> or C<E<lt>> on floating point values can be very useful, |
8650 | 8559 | but also very dangerous if you don't know exactly what you're doing. |
8651 | 8560 | It is definitely not a general way to portably store floating point |
8652 | 8561 | values. |
8653 | 8562 | |
8654 | 8563 | =end original |
8655 | 8564 | |
8656 | 8565 | データ交換のために浮動小数点数のバイト順をビッグエンディアンかリトル |
8657 | 8566 | エンディアンに強制することは、全てのプラットフォームが同じバイナリ |
8658 | 8567 | 表現(例えば IEEE 浮動小数点フォーマット)の場合にのみうまくいきます。 |
8659 | 8568 | たとえ全てのプラットフォームが IEEE を使っていても、そこには微妙な違いが |
8660 | 8569 | あるかもしれません。 |
8661 | 8570 | 浮動小数点数に C<E<gt>> や C<E<lt>> が使えることはとても便利な場合が |
8662 | 8571 | ありますが、もし自分が何をしているかを正確に理解していなければ、 |
8663 | 8572 | とても危険です。 |
8664 | 8573 | 移植性のある浮動小数点数の保存のための一般的な方法がないことは確実です。 |
8665 | 8574 | |
8666 | 8575 | =begin original |
8667 | 8576 | |
8668 | 8577 | When using C<E<gt>> or C<E<lt>> on an C<()>-group, this will affect |
8669 | 8578 | all types inside the group that accept the byte-order modifiers, |
8670 | 8579 | including all subgroups. It will silently be ignored for all other |
8671 | 8580 | types. You are not allowed to override the byte-order within a group |
8672 | 8581 | that already has a byte-order modifier suffix. |
8673 | 8582 | |
8674 | 8583 | =end original |
8675 | 8584 | |
8676 | 8585 | C<()> グループで C<E<gt>> や C<E<lt>> を使うと、これは、副グループを |
8677 | 8586 | 含む全ての型のうち、バイト順修飾子を受け入れる全てのものに影響与えます。 |
8678 | 8587 | その他の型については沈黙のうちに無視されます。 |
8679 | 8588 | 既にバイト順接尾辞を持っているグループ内のバイト順を上書きすることは |
8680 | 8589 | できません。 |
8681 | 8590 | |
8682 | 8591 | =item * |
8683 | 8592 | |
8684 | 8593 | =begin original |
8685 | 8594 | |
8686 | 8595 | Real numbers (floats and doubles) are in the native machine format only; |
8687 | 8596 | due to the multiplicity of floating formats around, and the lack of a |
8688 | 8597 | standard "network" representation, no facility for interchange has been |
8689 | 8598 | made. This means that packed floating point data written on one machine |
8690 | 8599 | may not be readable on another - even if both use IEEE floating point |
8691 | 8600 | arithmetic (as the endian-ness of the memory representation is not part |
8692 | 8601 | of the IEEE spec). See also L<perlport>. |
8693 | 8602 | |
8694 | 8603 | =end original |
8695 | 8604 | |
8696 | 8605 | 実数 (float と double) は、機種依存のフォーマットしかありません。 |
8697 | 8606 | いろんな浮動小数点数のフォーマットが在り、標準的な |
8698 | 8607 | "network" 表現といったものがないため、データ交換のための機能は |
8699 | 8608 | 用意してありません。 |
8700 | 8609 | つまり、あるマシンで pack した浮動小数点数は、別のマシンでは |
8701 | 8610 | 読めないかもしれないということです。 |
8702 | 8611 | たとえ双方で IEEE フォーマットの浮動小数点数演算を行なっていてもです |
8703 | 8612 | (IEEE の仕様では、メモリ表現上のバイト順序までは、 |
8704 | 8613 | 規定されていないからです)。 |
8705 | 8614 | L<perlport> も参照してください。 |
8706 | 8615 | |
8707 | 8616 | =begin original |
8708 | 8617 | |
8709 | 8618 | If you know exactly what you're doing, you can use the C<E<gt>> or C<E<lt>> |
8710 | 8619 | modifiers to force big- or little-endian byte-order on floating point values. |
8711 | 8620 | |
8712 | 8621 | =end original |
8713 | 8622 | |
8714 | 8623 | もし何をしようとしているのかを正確に理解しているなら、浮動小数点数の |
8715 | 8624 | バイト順をビッグエンディアンやリトルエンディアンに強制するために、 |
8716 | 8625 | C<E<gt>> と C<E<lt>> の修飾子が使えます。 |
8717 | 8626 | |
8718 | 8627 | =begin original |
8719 | 8628 | |
8720 | 8629 | Note that Perl uses doubles (or long doubles, if configured) internally for |
8721 | 8630 | all numeric calculation, and converting from double into float and thence back |
8722 | 8631 | to double again will lose precision (i.e., C<unpack("f", pack("f", $foo)>) |
8723 | 8632 | will not in general equal $foo). |
8724 | 8633 | |
8725 | 8634 | =end original |
8726 | 8635 | |
8727 | 8636 | Perl では、すべての数値演算のために、内部的に double (または |
8728 | 8637 | 設定によっては long double) を使用しています。 |
8729 | 8638 | double から float へ変換し、それから再び double に戻すと |
8730 | 8639 | 精度が落ちることになります (つまり、C<unpack("f", pack("f", $foo)>) は、 |
8731 | 8640 | 一般には $foo と同じではないということです)。 |
8732 | 8641 | |
8733 | 8642 | =item * |
8734 | 8643 | |
8735 | 8644 | =begin original |
8736 | 8645 | |
8737 | 8646 | Pack and unpack can operate in two modes, character mode (C<C0> mode) where |
8738 | 8647 | the packed string is processed per character and UTF-8 mode (C<U0> mode) |
8739 | 8648 | where the packed string is processed in its UTF-8-encoded Unicode form on |
8740 | 8649 | a byte by byte basis. Character mode is the default unless the format string |
8741 | 8650 | starts with an C<U>. You can switch mode at any moment with an explicit |
8742 | 8651 | C<C0> or C<U0> in the format. A mode is in effect until the next mode switch |
8743 | 8652 | or until the end of the ()-group in which it was entered. |
8744 | 8653 | |
8745 | 8654 | =end original |
8746 | 8655 | |
8747 | 8656 | pack と unpack は二つのモードで操作します; pack された文字列を文字単位で |
8748 | 8657 | 処理する文字モード (C<C0> モード) と、pack された文字列を、バイト毎に、 |
8749 | 8658 | その UTF-8 エンコードされた形式で処理するUTF-8 モード (C<U0> モード) です。 |
8750 | 8659 | 文字モードはフォーマット文字列が C<U> で始まっていない限りはデフォルトです。 |
8751 | 8660 | モードはフォーマット中に明示的に C<C0> または C<U0> と書くことでいつでも |
8752 | 8661 | 切り替えられます。 |
8753 | 8662 | モードは次のモードに切り替えられるか、現在の () グループが終了するまで |
8754 | 8663 | 有効です。 |
8755 | 8664 | |
8756 | 8665 | =item * |
8757 | 8666 | |
8758 | 8667 | =begin original |
8759 | 8668 | |
8760 | 8669 | You must yourself do any alignment or padding by inserting for example |
8761 | 8670 | enough C<'x'>es while packing. There is no way to pack() and unpack() |
8762 | 8671 | could know where the characters are going to or coming from. Therefore |
8763 | 8672 | C<pack> (and C<unpack>) handle their output and input as flat |
8764 | 8673 | sequences of characters. |
8765 | 8674 | |
8766 | 8675 | =end original |
8767 | 8676 | |
8768 | 8677 | pack するときに、例えば十分な数の C<'x'> を挿入することによって |
8769 | 8678 | アライメントやパッディングを行うのは全て自分でしなければなりません。 |
8770 | 8679 | 文字列がどこへ行くか・どこから来たかを pack() や unpack() が |
8771 | 8680 | 知る方法はありません。 |
8772 | 8681 | 従って C<pack> (と C<unpack>) は出力と入力をフラットな文字列として |
8773 | 8682 | 扱います。 |
8774 | 8683 | |
8775 | 8684 | =item * |
8776 | 8685 | |
8777 | 8686 | =begin original |
8778 | 8687 | |
8779 | 8688 | A ()-group is a sub-TEMPLATE enclosed in parentheses. A group may |
8780 | 8689 | take a repeat count, both as postfix, and for unpack() also via the C</> |
8781 | 8690 | template character. Within each repetition of a group, positioning with |
8782 | 8691 | C<@> starts again at 0. Therefore, the result of |
8783 | 8692 | |
8784 | 8693 | =end original |
8785 | 8694 | |
8786 | 8695 | () のグループはかっこで囲われた副テンプレートです。 |
8787 | 8696 | グループは繰り返し数を取ることができます; 接尾辞によるか、unpack() の場合は |
8788 | 8697 | C</> テンプレート文字によります。 |
8789 | 8698 | グループの繰り返し毎に、C<@> の位置は 0 になります。 |
8790 | 8699 | 従って、以下の結果は: |
8791 | 8700 | |
8792 | 8701 | pack( '@1A((@2A)@3A)', 'a', 'b', 'c' ) |
8793 | 8702 | |
8794 | 8703 | =begin original |
8795 | 8704 | |
8796 | 8705 | is the string "\0a\0\0bc". |
8797 | 8706 | |
8798 | 8707 | =end original |
8799 | 8708 | |
8800 | 8709 | 文字列 "\0a\0\0bc" です。 |
8801 | 8710 | |
8802 | 8711 | =item * |
8803 | 8712 | |
8804 | 8713 | =begin original |
8805 | 8714 | |
8806 | 8715 | C<x> and C<X> accept C<!> modifier. In this case they act as |
8807 | 8716 | alignment commands: they jump forward/back to the closest position |
8808 | 8717 | aligned at a multiple of C<count> characters. For example, to pack() or |
8809 | 8718 | unpack() C's C<struct {char c; double d; char cc[2]}> one may need to |
8810 | 8719 | use the template C<W x![d] d W[2]>; this assumes that doubles must be |
8811 | 8720 | aligned on the double's size. |
8812 | 8721 | |
8813 | 8722 | =end original |
8814 | 8723 | |
8815 | 8724 | C<x> と C<X> には C<!> 修飾子を付けることができます。 |
8816 | 8725 | この場合、これらはアライメントコマンドとして働きます: |
8817 | 8726 | C<count> 文字の倍数のアライメントとなる、もっとも近い位置に移動します。 |
8818 | 8727 | 例えば、C の C<struct {char c; double d; char cc[2]}> を pack() または |
8819 | 8728 | unpack() するためには、C<W x![d] d W[2]> というテンプレートを使う必要が |
8820 | 8729 | あるかもしれません; |
8821 | 8730 | これは double が double のサイズでアライメントされていることを |
8822 | 8731 | 仮定しています。 |
8823 | 8732 | |
8824 | 8733 | =begin original |
8825 | 8734 | |
8826 | 8735 | For alignment commands C<count> of 0 is equivalent to C<count> of 1; |
8827 | 8736 | both result in no-ops. |
8828 | 8737 | |
8829 | 8738 | =end original |
8830 | 8739 | |
8831 | 8740 | アライメントコマンドに対しては、C<count> に 0 を指定するのは 1 を |
8832 | 8741 | 指定するのと等価です; どちらも何もしません。 |
8833 | 8742 | |
8834 | 8743 | =item * |
8835 | 8744 | |
8836 | 8745 | =begin original |
8837 | 8746 | |
8838 | 8747 | C<n>, C<N>, C<v> and C<V> accept the C<!> modifier. In this case they |
8839 | 8748 | will represent signed 16-/32-bit integers in big-/little-endian order. |
8840 | 8749 | This is only portable if all platforms sharing the packed data use the |
8841 | 8750 | same binary representation for signed integers (e.g. all platforms are |
8842 | 8751 | using two's complement representation). |
8843 | 8752 | |
8844 | 8753 | =end original |
8845 | 8754 | |
8846 | 8755 | C<n>, C<N>, C<v>, C<V> は C<!> 修飾子を受け入れます。 |
8847 | 8756 | この場合、これらはビッグ/リトルエンディアンの順序で符号付き 16 または |
8848 | 8757 | 32 ビット整数で表現されます。 |
8849 | 8758 | これは pack されたデータを共有する全てのプラットフォームが |
8850 | 8759 | 符号付き整数について同じバイナリ表現を使う(つまり、全ての |
8851 | 8760 | プラットフォームで 2 の補数表現を使う)場合にのみ移植性があります。 |
8852 | 8761 | |
8853 | 8762 | =item * |
8854 | 8763 | |
8855 | 8764 | =begin original |
8856 | 8765 | |
8857 | 8766 | A comment in a TEMPLATE starts with C<#> and goes to the end of line. |
8858 | 8767 | White space may be used to separate pack codes from each other, but |
8859 | 8768 | modifiers and a repeat count must follow immediately. |
8860 | 8769 | |
8861 | 8770 | =end original |
8862 | 8771 | |
8863 | 8772 | TEMPLATE の中の C<#> から行末まではコメントです。 |
8864 | 8773 | 空白は pack コードをそれぞれ分けるために使えますが、修飾子と |
8865 | 8774 | 繰り返し数は直後に置かなければなりません。 |
8866 | 8775 | |
8867 | 8776 | =item * |
8868 | 8777 | |
8869 | 8778 | =begin original |
8870 | 8779 | |
8871 | 8780 | If TEMPLATE requires more arguments to pack() than actually given, pack() |
8872 | 8781 | assumes additional C<""> arguments. If TEMPLATE requires fewer arguments |
8873 | 8782 | to pack() than actually given, extra arguments are ignored. |
8874 | 8783 | |
8875 | 8784 | =end original |
8876 | 8785 | |
8877 | 8786 | TEMPLATE が要求する引数の数が pack() が実際に与えている数より多い場合、 |
8878 | 8787 | 追加の C<""> 引数があるものと仮定します。 |
8879 | 8788 | TEMPLATE が要求する引数の数の方が少ない場合、余分の引数は無視されます。 |
8880 | 8789 | |
8881 | 8790 | =back |
8882 | 8791 | |
8883 | 8792 | =begin original |
8884 | 8793 | |
8885 | 8794 | Examples: |
8886 | 8795 | |
8887 | 8796 | =end original |
8888 | 8797 | |
8889 | 8798 | 例: |
8890 | 8799 | |
8891 | 8800 | $foo = pack("WWWW",65,66,67,68); |
8892 | 8801 | # foo eq "ABCD" |
8893 | 8802 | $foo = pack("W4",65,66,67,68); |
8894 | 8803 | # same thing |
8895 | 8804 | $foo = pack("W4",0x24b6,0x24b7,0x24b8,0x24b9); |
8896 | 8805 | # same thing with Unicode circled letters. |
8897 | 8806 | $foo = pack("U4",0x24b6,0x24b7,0x24b8,0x24b9); |
8898 | 8807 | # same thing with Unicode circled letters. You don't get the UTF-8 |
8899 | 8808 | # bytes because the U at the start of the format caused a switch to |
8900 | 8809 | # U0-mode, so the UTF-8 bytes get joined into characters |
8901 | 8810 | $foo = pack("C0U4",0x24b6,0x24b7,0x24b8,0x24b9); |
8902 | 8811 | # foo eq "\xe2\x92\xb6\xe2\x92\xb7\xe2\x92\xb8\xe2\x92\xb9" |
8903 | 8812 | # This is the UTF-8 encoding of the string in the previous example |
8904 | 8813 | |
8905 | 8814 | $foo = pack("ccxxcc",65,66,67,68); |
8906 | 8815 | # foo eq "AB\0\0CD" |
8907 | 8816 | |
8908 | 8817 | # note: the above examples featuring "W" and "c" are true |
8909 | 8818 | # only on ASCII and ASCII-derived systems such as ISO Latin 1 |
8910 | 8819 | # and UTF-8. In EBCDIC the first example would be |
8911 | 8820 | # $foo = pack("WWWW",193,194,195,196); |
8912 | 8821 | |
8913 | 8822 | $foo = pack("s2",1,2); |
8914 | 8823 | # "\1\0\2\0" on little-endian |
8915 | 8824 | # "\0\1\0\2" on big-endian |
8916 | 8825 | |
8917 | 8826 | $foo = pack("a4","abcd","x","y","z"); |
8918 | 8827 | # "abcd" |
8919 | 8828 | |
8920 | 8829 | $foo = pack("aaaa","abcd","x","y","z"); |
8921 | 8830 | # "axyz" |
8922 | 8831 | |
8923 | 8832 | $foo = pack("a14","abcdefg"); |
8924 | 8833 | # "abcdefg\0\0\0\0\0\0\0" |
8925 | 8834 | |
8926 | 8835 | $foo = pack("i9pl", gmtime); |
8927 | 8836 | # a real struct tm (on my system anyway) |
8928 | 8837 | |
8929 | 8838 | $utmp_template = "Z8 Z8 Z16 L"; |
8930 | 8839 | $utmp = pack($utmp_template, @utmp1); |
8931 | 8840 | # a struct utmp (BSDish) |
8932 | 8841 | |
8933 | 8842 | @utmp2 = unpack($utmp_template, $utmp); |
8934 | 8843 | # "@utmp1" eq "@utmp2" |
8935 | 8844 | |
8936 | 8845 | sub bintodec { |
8937 | 8846 | unpack("N", pack("B32", substr("0" x 32 . shift, -32))); |
8938 | 8847 | } |
8939 | 8848 | |
8940 | 8849 | $foo = pack('sx2l', 12, 34); |
8941 | 8850 | # short 12, two zero bytes padding, long 34 |
8942 | 8851 | $bar = pack('s@4l', 12, 34); |
8943 | 8852 | # short 12, zero fill to position 4, long 34 |
8944 | 8853 | # $foo eq $bar |
8945 | 8854 | $baz = pack('s.l', 12, 4, 34); |
8946 | 8855 | # short 12, zero fill to position 4, long 34 |
8947 | 8856 | |
8948 | 8857 | $foo = pack('nN', 42, 4711); |
8949 | 8858 | # pack big-endian 16- and 32-bit unsigned integers |
8950 | 8859 | $foo = pack('S>L>', 42, 4711); |
8951 | 8860 | # exactly the same |
8952 | 8861 | $foo = pack('s<l<', -42, 4711); |
8953 | 8862 | # pack little-endian 16- and 32-bit signed integers |
8954 | 8863 | $foo = pack('(sl)<', -42, 4711); |
8955 | 8864 | # exactly the same |
8956 | 8865 | |
8957 | 8866 | =begin original |
8958 | 8867 | |
8959 | 8868 | The same template may generally also be used in unpack(). |
8960 | 8869 | |
8961 | 8870 | =end original |
8962 | 8871 | |
8963 | 8872 | 一般には、pack で使用したものと同じテンプレートが、 |
8964 | 8873 | unpack() 関数でも使用できます。 |
8965 | 8874 | |
8966 | 8875 | =item package NAMESPACE |
8967 | 8876 | X<package> X<module> X<namespace> |
8968 | 8877 | |
8969 | 8878 | =item package |
8970 | 8879 | |
8971 | 8880 | =begin original |
8972 | 8881 | |
8973 | 8882 | Declares the compilation unit as being in the given namespace. The scope |
8974 | 8883 | of the package declaration is from the declaration itself through the end |
8975 | 8884 | of the enclosing block, file, or eval (the same as the C<my> operator). |
8976 | 8885 | All further unqualified dynamic identifiers will be in this namespace. |
8977 | 8886 | A package statement affects only dynamic variables--including those |
8978 | 8887 | you've used C<local> on--but I<not> lexical variables, which are created |
8979 | 8888 | with C<my>. Typically it would be the first declaration in a file to |
8980 | 8889 | be included by the C<require> or C<use> operator. You can switch into a |
8981 | 8890 | package in more than one place; it merely influences which symbol table |
8982 | 8891 | is used by the compiler for the rest of that block. You can refer to |
8983 | 8892 | variables and filehandles in other packages by prefixing the identifier |
8984 | 8893 | with the package name and a double colon: C<$Package::Variable>. |
8985 | 8894 | If the package name is null, the C<main> package as assumed. That is, |
8986 | 8895 | C<$::sail> is equivalent to C<$main::sail> (as well as to C<$main'sail>, |
8987 | 8896 | still seen in older code). |
8988 | 8897 | |
8989 | 8898 | =end original |
8990 | 8899 | |
8991 | 8900 | 与えられた名前空間でのコンパイル単位を宣言します。 |
8992 | 8901 | パッケージ宣言のスコープは、宣言自体から、閉じたブロック、ファイル、 |
8993 | 8902 | eval の終わりまでです(C<my> 演算子と同じです)。 |
8994 | 8903 | 全てのさらなる修飾されてない動的識別子はこの名前空間になります。 |
8995 | 8904 | package 文は動的変数にのみ影響します -- C<local> で使ったものも |
8996 | 8905 | 含みます -- が、C<my> で作成されたレキシカル変数には I<影響しません>。 |
8997 | 8906 | 典型的にはこれは C<require> や C<use> 演算子でインクルードされるファイルの |
8998 | 8907 | 最初に宣言されます。 |
8999 | 8908 | パッケージを複数の場所で切り替えることができます; |
9000 | 8909 | これは単にコンパイラがこのブロックの残りに対してどのシンボルテーブルを |
9001 | 8910 | 使うかにのみ影響します。 |
9002 | 8911 | 他のパッケージの変数やファイルハンドルは、識別子にパッケージ名と |
9003 | 8912 | コロン 2 つをつけることで参照できます(C<$Package::Variable>)。 |
9004 | 8913 | パッケージ名が空文字列の場合、C<main> パッケージが仮定されます。 |
9005 | 8914 | つまり、C<$::sail> は C<$main::sail> と等価です(C<$main'sail> も |
9006 | 8915 | 古いコードではまだ見られます)。 |
9007 | 8916 | |
9008 | 8917 | =begin original |
9009 | 8918 | |
8919 | If NAMESPACE is omitted, then there is no current package, and all | |
8920 | identifiers must be fully qualified or lexicals. However, you are | |
8921 | strongly advised not to make use of this feature. Its use can cause | |
8922 | unexpected behaviour, even crashing some versions of Perl. It is | |
8923 | deprecated, and will be removed from a future release. | |
8924 | ||
8925 | =end original | |
8926 | ||
8927 | NAMESPACE が省略されると、カレントパッケージはなくなるので、 | |
8928 | 全ての識別子は完全修飾されるかレキシカルでなければなりません。 | |
8929 | しかし、この機能を使わないことを強く勧めます。 | |
8930 | この使用は予想外の振る舞いを引きおこすことがあり、Perl のバージョンに | |
8931 | よってはクラッシュするかもしれません。 | |
8932 | これは非推奨であり、将来のリリースでは削除されます。 | |
8933 | ||
8934 | =begin original | |
8935 | ||
9010 | 8936 | See L<perlmod/"Packages"> for more information about packages, modules, |
9011 | 8937 | and classes. See L<perlsub> for other scoping issues. |
9012 | 8938 | |
9013 | 8939 | =end original |
9014 | 8940 | |
9015 | 8941 | パッケージ、モジュール、クラスに関するさらなる情報については |
9016 | 8942 | L<perlmod/"Packages"> を参照してください。 |
9017 | 8943 | その他のスコープに関する話題については L<perlsub> を参照してください。 |
9018 | 8944 | |
9019 | 8945 | =item pipe READHANDLE,WRITEHANDLE |
9020 | 8946 | X<pipe> |
9021 | 8947 | |
9022 | 8948 | =begin original |
9023 | 8949 | |
9024 | 8950 | Opens a pair of connected pipes like the corresponding system call. |
9025 | 8951 | Note that if you set up a loop of piped processes, deadlock can occur |
9026 | 8952 | unless you are very careful. In addition, note that Perl's pipes use |
9027 | 8953 | IO buffering, so you may need to set C<$|> to flush your WRITEHANDLE |
9028 | 8954 | after each command, depending on the application. |
9029 | 8955 | |
9030 | 8956 | =end original |
9031 | 8957 | |
9032 | 8958 | 対応するシステムコールと同じように、 |
9033 | 8959 | 接続されたパイプのペアをオープンします。 |
9034 | 8960 | パイプでプロセスをループにするときには、よほど気を付けないと、 |
9035 | 8961 | デッドロックが起こり得ます。 |
9036 | 8962 | さらに、Perl のパイプでは、IO のバッファリングを使いますから、 |
9037 | 8963 | アプリケーションによっては、コマンドごとに WRITEHANDLE を |
9038 | 8964 | フラッシュするように、C<$|> を設定することが必要になるかもしれません。 |
9039 | 8965 | |
9040 | 8966 | =begin original |
9041 | 8967 | |
9042 | 8968 | See L<IPC::Open2>, L<IPC::Open3>, and L<perlipc/"Bidirectional Communication"> |
9043 | 8969 | for examples of such things. |
9044 | 8970 | |
9045 | 8971 | =end original |
9046 | 8972 | |
9047 | 8973 | これらに関する例については、L<IPC::Open2>, L<IPC::Open3>, |
9048 | 8974 | L<perlipc/"Bidirectional Communication"> を参照して下さい。 |
9049 | 8975 | |
9050 | 8976 | =begin original |
9051 | 8977 | |
9052 | 8978 | On systems that support a close-on-exec flag on files, the flag will be set |
9053 | 8979 | for the newly opened file descriptors as determined by the value of $^F. |
9054 | 8980 | See L<perlvar/$^F>. |
9055 | 8981 | |
9056 | 8982 | =end original |
9057 | 8983 | |
9058 | 8984 | ファイルに対する close-on-exec フラグをサポートしているシステムでは、 |
9059 | 8985 | フラグは $^F の値で決定される、新しくオープンされたファイル記述子に対して |
9060 | 8986 | セットされます。 |
9061 | 8987 | L<perlvar/$^F> を参照してください。 |
9062 | 8988 | |
9063 | 8989 | =item pop ARRAY |
9064 | 8990 | X<pop> X<stack> |
9065 | 8991 | |
9066 | 8992 | =item pop |
9067 | 8993 | |
9068 | 8994 | =begin original |
9069 | 8995 | |
9070 | 8996 | Pops and returns the last value of the array, shortening the array by |
9071 | 8997 | one element. |
9072 | 8998 | |
9073 | 8999 | =end original |
9074 | 9000 | |
9075 | 9001 | 配列の最後の値をポップして返し、配列の大きさを 1 だけ小さくします。 |
9076 | 9002 | |
9077 | 9003 | =begin original |
9078 | 9004 | |
9079 | 9005 | If there are no elements in the array, returns the undefined value |
9080 | 9006 | (although this may happen at other times as well). If ARRAY is |
9081 | 9007 | omitted, pops the C<@ARGV> array in the main program, and the C<@_> |
9082 | 9008 | array in subroutines, just like C<shift>. |
9083 | 9009 | |
9084 | 9010 | =end original |
9085 | 9011 | |
9086 | 9012 | 指定された配列に要素がなければ、未定義値が返されます |
9087 | 9013 | (しかしこれは他の場合にも起こり得ます)。 |
9088 | 9014 | ARRAY が省略されると、C<shift> と同様に、メインプログラムでは C<@ARGV>が、 |
9089 | 9015 | サブルーチンでは C<@_> が使われます。 |
9090 | 9016 | |
9091 | 9017 | =item pos SCALAR |
9092 | 9018 | X<pos> X<match, position> |
9093 | 9019 | |
9094 | 9020 | =item pos |
9095 | 9021 | |
9096 | 9022 | =begin original |
9097 | 9023 | |
9098 | 9024 | Returns the offset of where the last C<m//g> search left off for the variable |
9099 | 9025 | in question (C<$_> is used when the variable is not specified). Note that |
9100 | 9026 | 0 is a valid match offset. C<undef> indicates that the search position |
9101 | 9027 | is reset (usually due to match failure, but can also be because no match has |
9102 | 9028 | yet been performed on the scalar). C<pos> directly accesses the location used |
9103 | 9029 | by the regexp engine to store the offset, so assigning to C<pos> will change |
9104 | 9030 | that offset, and so will also influence the C<\G> zero-width assertion in |
9105 | 9031 | regular expressions. Because a failed C<m//gc> match doesn't reset the offset, |
9106 | 9032 | the return from C<pos> won't change either in this case. See L<perlre> and |
9107 | 9033 | L<perlop>. |
9108 | 9034 | |
9109 | 9035 | =end original |
9110 | 9036 | |
9111 | 9037 | 対象の変数に対して、前回の C<m//g> が終了した場所の |
9112 | 9038 | オフセットを返します(変数が指定されなかった場合は C<$_> が使われます)。 |
9113 | 9039 | 0 は有効なマッチオフセットであることに注意してください。 |
9114 | 9040 | C<undef> は検索位置がリセットされることを意味します (通常はマッチ失敗が |
9115 | 9041 | 原因ですが、このスカラ値にまだマッチングが行われていないためかもしれません)。 |
9116 | 9042 | C<pos> は正規表現エンジンがオフセットを保存するために使う場所を直接 |
9117 | 9043 | アクセスするので、C<pos> への代入はオフセットを変更し、そのような変更は |
9118 | 9044 | 正規表現における C<\G> ゼロ幅アサートにも影響を与えます。 |
9119 | 9045 | C<m//gc> マッチに失敗してもオフセットはリセットしないので、 |
9120 | 9046 | C<pos> からの返り値はどちらの場合も変更されません。 |
9121 | 9047 | L<perlre> と L<perlop> を参照してください。 |
9122 | 9048 | |
9123 | 9049 | =item print FILEHANDLE LIST |
9124 | 9050 | X<print> |
9125 | 9051 | |
9126 | 9052 | =item print LIST |
9127 | 9053 | |
9128 | 9054 | =item print |
9129 | 9055 | |
9130 | 9056 | =begin original |
9131 | 9057 | |
9132 | 9058 | Prints a string or a list of strings. Returns true if successful. |
9133 | 9059 | FILEHANDLE may be a scalar variable name, in which case the variable |
9134 | 9060 | contains the name of or a reference to the filehandle, thus introducing |
9135 | 9061 | one level of indirection. (NOTE: If FILEHANDLE is a variable and |
9136 | 9062 | the next token is a term, it may be misinterpreted as an operator |
9137 | 9063 | unless you interpose a C<+> or put parentheses around the arguments.) |
9138 | 9064 | If FILEHANDLE is omitted, prints by default to standard output (or |
9139 | 9065 | to the last selected output channel--see L</select>). If LIST is |
9140 | 9066 | also omitted, prints C<$_> to the currently selected output channel. |
9141 | 9067 | To set the default output channel to something other than STDOUT |
9142 | 9068 | use the select operation. The current value of C<$,> (if any) is |
9143 | 9069 | printed between each LIST item. The current value of C<$\> (if |
9144 | 9070 | any) is printed after the entire LIST has been printed. Because |
9145 | 9071 | print takes a LIST, anything in the LIST is evaluated in list |
9146 | 9072 | context, and any subroutine that you call will have one or more of |
9147 | 9073 | its expressions evaluated in list context. Also be careful not to |
9148 | 9074 | follow the print keyword with a left parenthesis unless you want |
9149 | 9075 | the corresponding right parenthesis to terminate the arguments to |
9150 | 9076 | the print--interpose a C<+> or put parentheses around all the |
9151 | 9077 | arguments. |
9152 | 9078 | |
9153 | 9079 | =end original |
9154 | 9080 | |
9155 | 9081 | 文字列か文字列のリストを出力します。 |
9156 | 9082 | 成功時には、真を返します。FILEHANDLE は、スカラ変数名でもよく、 |
9157 | 9083 | その場合には、その変数にファイルハンドル名またはそのリファレンスが |
9158 | 9084 | 入っているものとして扱われますから、一段階の間接指定が行なえます。 |
9159 | 9085 | (注: FILEHANDLE に変数を使い、次のトークンが「項」のときには、 |
9160 | 9086 | 間に C<+> を置くか、引数の前後を括弧で括らなければ、 |
9161 | 9087 | 誤って解釈されることがあります。) |
9162 | 9088 | FILEHANDLE を省略した場合には、標準出力 (か、最後に選択された出力チャネル |
9163 | 9089 | -- L</select>を参照) に出力します。 |
9164 | 9090 | LIST も省略すると、C<$_> を現在選択されている出力チャネルに |
9165 | 9091 | 出力することになります。 |
9166 | 9092 | デフォルトの出力チャネルを STDOUT 以外にしたければ、select 演算子を |
9167 | 9093 | 使ってください。 |
9168 | 9094 | C<$,> の値が(もしあれば)各 LIST 要素の間に出力されます。 |
9169 | 9095 | print の引数は LIST なので、LISTの中のものは、 |
9170 | 9096 | すべてリストコンテキストで評価されます。 |
9171 | 9097 | サブルーチンの呼び出しがあれば、リストコンテキストでは、 |
9172 | 9098 | 複数の値を返すかもしれません。 |
9173 | 9099 | また、すべての引数を括弧で括るのでなければ、print というキーワードの |
9174 | 9100 | 次に開き括弧を書いてはいけません。 |
9175 | 9101 | "print" と引数の間に C<+> を書くか、すべての引数を括弧で括ってください。 |
9176 | 9102 | |
9177 | 9103 | =begin original |
9178 | 9104 | |
9179 | 9105 | Note that if you're storing FILEHANDLEs in an array, or if you're using |
9180 | 9106 | any other expression more complex than a scalar variable to retrieve it, |
9181 | 9107 | you will have to use a block returning the filehandle value instead: |
9182 | 9108 | |
9183 | 9109 | =end original |
9184 | 9110 | |
9185 | 9111 | もし FILESHANDLE を配列に保存していたり、それを得るためにスカラ変数より |
9186 | 9112 | 複雑な表現を使っている場合、 |
9187 | 9113 | 代わりにその値を返すブロックを使う必要があります: |
9188 | 9114 | |
9189 | 9115 | print { $files[$i] } "stuff\n"; |
9190 | 9116 | print { $OK ? STDOUT : STDERR } "stuff\n"; |
9191 | 9117 | |
9192 | 9118 | =item printf FILEHANDLE FORMAT, LIST |
9193 | 9119 | X<printf> |
9194 | 9120 | |
9195 | 9121 | =item printf FORMAT, LIST |
9196 | 9122 | |
9197 | 9123 | =begin original |
9198 | 9124 | |
9199 | 9125 | Equivalent to C<print FILEHANDLE sprintf(FORMAT, LIST)>, except that C<$\> |
9200 | 9126 | (the output record separator) is not appended. The first argument |
9201 | 9127 | of the list will be interpreted as the C<printf> format. See C<sprintf> |
9202 | 9128 | for an explanation of the format argument. If C<use locale> is in effect, |
9203 | 9129 | and POSIX::setlocale() has been called, the character used for the decimal |
9204 | 9130 | separator in formatted floating point numbers is affected by the LC_NUMERIC |
9205 | 9131 | locale. See L<perllocale> and L<POSIX>. |
9206 | 9132 | |
9207 | 9133 | =end original |
9208 | 9134 | |
9209 | 9135 | C<$\>(出力レコードセパレータ)を追加しないことを除けば、 |
9210 | 9136 | C<print FILEHANDLE sprintf(FORMAT, LIST)> と等価です。 |
9211 | 9137 | リストの最初の要素は、C<printf> フォーマットと解釈されます。 |
9212 | 9138 | フォーマット引数の説明については C<sprintf> を参照してください。 |
9213 | 9139 | C<use locale> が効力をもっていて、POSIX::setlocale() が呼び出されていれば、 |
9214 | 9140 | 小数点に使われる文字は LC_NUMERIC ロケールの影響を受けます。 |
9215 | 9141 | L<perllocale> と L<POSIX> を参照してください。 |
9216 | 9142 | |
9217 | 9143 | =begin original |
9218 | 9144 | |
9219 | 9145 | Don't fall into the trap of using a C<printf> when a simple |
9220 | 9146 | C<print> would do. The C<print> is more efficient and less |
9221 | 9147 | error prone. |
9222 | 9148 | |
9223 | 9149 | =end original |
9224 | 9150 | |
9225 | 9151 | 単純な C<print> を使うべきところで C<printf> を使ってしまう |
9226 | 9152 | 罠にかからないようにしてください。 |
9227 | 9153 | C<print> はより効率的で、間違いが起こりにくいです。 |
9228 | 9154 | |
9229 | 9155 | =item prototype FUNCTION |
9230 | 9156 | X<prototype> |
9231 | 9157 | |
9232 | 9158 | =begin original |
9233 | 9159 | |
9234 | 9160 | Returns the prototype of a function as a string (or C<undef> if the |
9235 | 9161 | function has no prototype). FUNCTION is a reference to, or the name of, |
9236 | 9162 | the function whose prototype you want to retrieve. |
9237 | 9163 | |
9238 | 9164 | =end original |
9239 | 9165 | |
9240 | 9166 | 関数のプロトタイプを文字列として返します(関数にプロトタイプがない場合は |
9241 | 9167 | C<undef> を返します)。 |
9242 | 9168 | FUNCTION はプロトタイプを得たい関数の名前、またはリファレンスです。 |
9243 | 9169 | |
9244 | 9170 | =begin original |
9245 | 9171 | |
9246 | 9172 | If FUNCTION is a string starting with C<CORE::>, the rest is taken as a |
9247 | 9173 | name for Perl builtin. If the builtin is not I<overridable> (such as |
9248 | 9174 | C<qw//>) or if its arguments cannot be adequately expressed by a prototype |
9249 | 9175 | (such as C<system>), prototype() returns C<undef>, because the builtin |
9250 | 9176 | does not really behave like a Perl function. Otherwise, the string |
9251 | 9177 | describing the equivalent prototype is returned. |
9252 | 9178 | |
9253 | 9179 | =end original |
9254 | 9180 | |
9255 | 9181 | FUNCTION が C<CORE::> で始まっている場合、残りは Perl ビルドインの名前として |
9256 | 9182 | 扱われます。 |
9257 | 9183 | このビルドインが(C<qw//> のように) I<オーバーライド可能> でない、 |
9258 | 9184 | またはこの引数が(C<system> のように)プロトタイプとして適切に記述できない場合、 |
9259 | 9185 | prototype() は C<undef> を返します; |
9260 | 9186 | なぜならビルドインは実際に Perl 関数のように振舞わないからです。 |
9261 | 9187 | それ以外では、等価なプロトタイプを表現した文字列が返されます。 |
9262 | 9188 | |
9263 | 9189 | =item push ARRAY,LIST |
9264 | 9190 | X<push> X<stack> |
9265 | 9191 | |
9266 | 9192 | =begin original |
9267 | 9193 | |
9268 | 9194 | Treats ARRAY as a stack, and pushes the values of LIST |
9269 | 9195 | onto the end of ARRAY. The length of ARRAY increases by the length of |
9270 | 9196 | LIST. Has the same effect as |
9271 | 9197 | |
9272 | 9198 | =end original |
9273 | 9199 | |
9274 | 9200 | ARRAY をスタックとして扱い、LIST 内の値を ARRAY の終わりにプッシュします。 |
9275 | 9201 | ARRAY の大きさは、LIST の長さ分だけ大きくなります。 |
9276 | 9202 | これは、 |
9277 | 9203 | |
9278 | 9204 | for $value (LIST) { |
9279 | 9205 | $ARRAY[++$#ARRAY] = $value; |
9280 | 9206 | } |
9281 | 9207 | |
9282 | 9208 | =begin original |
9283 | 9209 | |
9284 | 9210 | but is more efficient. Returns the number of elements in the array following |
9285 | 9211 | the completed C<push>. |
9286 | 9212 | |
9287 | 9213 | =end original |
9288 | 9214 | |
9289 | 9215 | とするのと同じ効果がありますが、より効率的です。 |
9290 | 9216 | C<push> の処理終了後の配列の要素数を返します。 |
9291 | 9217 | |
9292 | 9218 | =item q/STRING/ |
9293 | 9219 | |
9294 | 9220 | =item qq/STRING/ |
9295 | 9221 | |
9222 | =item qr/STRING/ | |
9223 | ||
9296 | 9224 | =item qx/STRING/ |
9297 | 9225 | |
9298 | 9226 | =item qw/STRING/ |
9299 | 9227 | |
9300 | 9228 | =begin original |
9301 | 9229 | |
9302 | Generalized quotes. See L<perlop/"Quote-Like Operators">. | |
9230 | Generalized quotes. See L<perlop/"Regexp Quote-Like Operators">. | |
9303 | 9231 | |
9304 | 9232 | =end original |
9305 | 9233 | |
9306 | 9234 | 汎用のクォートです。 |
9307 | L<perlop/"Quote-Like Operators"> を参照してください。 | |
9308 | ||
9309 | =item qr/STRING/ | |
9310 | ||
9311 | =begin original | |
9312 | ||
9313 | Regexp-like quote. See L<perlop/"Regexp Quote-Like Operators">. | |
9314 | ||
9315 | =end original | |
9316 | ||
9317 | 正規表現風のクォートです。 | |
9318 | 9235 | L<perlop/"Regexp Quote-Like Operators"> を参照してください。 |
9319 | 9236 | |
9320 | 9237 | =item quotemeta EXPR |
9321 | 9238 | X<quotemeta> X<metacharacter> |
9322 | 9239 | |
9323 | 9240 | =item quotemeta |
9324 | 9241 | |
9325 | 9242 | =begin original |
9326 | 9243 | |
9327 | 9244 | Returns the value of EXPR with all non-"word" |
9328 | 9245 | characters backslashed. (That is, all characters not matching |
9329 | 9246 | C</[A-Za-z_0-9]/> will be preceded by a backslash in the |
9330 | 9247 | returned string, regardless of any locale settings.) |
9331 | 9248 | This is the internal function implementing |
9332 | 9249 | the C<\Q> escape in double-quoted strings. |
9333 | 9250 | |
9334 | 9251 | =end original |
9335 | 9252 | |
9336 | 9253 | EXPR の中のすべての非英数字キャラクタをバックスラッシュで |
9337 | 9254 | エスケープしたものを返します |
9338 | 9255 | (つまり、C</[A-Za-z_0-9]/> にマッチしない全ての文字の前には |
9339 | 9256 | ロケールに関わらずバックスラッシュが前置されます)。 |
9340 | 9257 | これは、ダブルクォート文字列での C<\Q> エスケープを |
9341 | 9258 | 実装するための内部関数です。 |
9342 | 9259 | |
9343 | 9260 | =begin original |
9344 | 9261 | |
9345 | 9262 | If EXPR is omitted, uses C<$_>. |
9346 | 9263 | |
9347 | 9264 | =end original |
9348 | 9265 | |
9349 | 9266 | EXPR が省略されると、C<$_> を使います。 |
9350 | 9267 | |
9351 | 9268 | =item rand EXPR |
9352 | 9269 | X<rand> X<random> |
9353 | 9270 | |
9354 | 9271 | =item rand |
9355 | 9272 | |
9356 | 9273 | =begin original |
9357 | 9274 | |
9358 | 9275 | Returns a random fractional number greater than or equal to C<0> and less |
9359 | 9276 | than the value of EXPR. (EXPR should be positive.) If EXPR is |
9360 | 9277 | omitted, the value C<1> is used. Currently EXPR with the value C<0> is |
9361 | 9278 | also special-cased as C<1> - this has not been documented before perl 5.8.0 |
9362 | 9279 | and is subject to change in future versions of perl. Automatically calls |
9363 | 9280 | C<srand> unless C<srand> has already been called. See also C<srand>. |
9364 | 9281 | |
9365 | 9282 | =end original |
9366 | 9283 | |
9367 | 9284 | C<0> 以上 EXPR の値未満の小数の乱数値を返します。 |
9368 | 9285 | (EXPR は正の数である必要があります。) |
9369 | 9286 | EXPR を省略すると、C<1> とみなします。 |
9370 | 9287 | 現在のところ、EXPR に値 C<0> をセットすると C<1> として特別扱いされます - |
9371 | 9288 | これは perl 5.8.0 以前には文書化されておらず、将来のバージョンの perl では |
9372 | 9289 | 変更される可能性があります。 |
9373 | 9290 | C<srand> が既に呼ばれている場合以外は、自動的に C<srand> 関数を |
9374 | 9291 | 呼び出します。 |
9375 | 9292 | C<srand> も参照してください。 |
9376 | 9293 | |
9377 | 9294 | =begin original |
9378 | 9295 | |
9379 | 9296 | Apply C<int()> to the value returned by C<rand()> if you want random |
9380 | 9297 | integers instead of random fractional numbers. For example, |
9381 | 9298 | |
9382 | 9299 | =end original |
9383 | 9300 | |
9384 | 9301 | ランダムな小数ではなく、ランダムな整数がほしい場合は、C<rand()> から |
9385 | 9302 | 返された値に C<int()> を適用してください。 |
9386 | 9303 | |
9387 | 9304 | int(rand(10)) |
9388 | 9305 | |
9389 | 9306 | =begin original |
9390 | 9307 | |
9391 | 9308 | returns a random integer between C<0> and C<9>, inclusive. |
9392 | 9309 | |
9393 | 9310 | =end original |
9394 | 9311 | |
9395 | 9312 | これは C<0> から C<9> の値をランダムに返します。 |
9396 | 9313 | |
9397 | 9314 | =begin original |
9398 | 9315 | |
9399 | 9316 | (Note: If your rand function consistently returns numbers that are too |
9400 | 9317 | large or too small, then your version of Perl was probably compiled |
9401 | 9318 | with the wrong number of RANDBITS.) |
9402 | 9319 | |
9403 | 9320 | =end original |
9404 | 9321 | |
9405 | 9322 | (注: もし、rand 関数が、常に大きい値ばかりや、小さい数ばかりを |
9406 | 9323 | 返すようなら、お使いになっている Perl が、 |
9407 | 9324 | 良くない RANDBITS を使ってコンパイルされている可能性があります。) |
9408 | 9325 | |
9409 | 9326 | =item read FILEHANDLE,SCALAR,LENGTH,OFFSET |
9410 | 9327 | X<read> X<file, read> |
9411 | 9328 | |
9412 | 9329 | =item read FILEHANDLE,SCALAR,LENGTH |
9413 | 9330 | |
9414 | 9331 | =begin original |
9415 | 9332 | |
9416 | 9333 | Attempts to read LENGTH I<characters> of data into variable SCALAR |
9417 | 9334 | from the specified FILEHANDLE. Returns the number of characters |
9418 | 9335 | actually read, C<0> at end of file, or undef if there was an error (in |
9419 | 9336 | the latter case C<$!> is also set). SCALAR will be grown or shrunk |
9420 | 9337 | so that the last character actually read is the last character of the |
9421 | 9338 | scalar after the read. |
9422 | 9339 | |
9423 | 9340 | =end original |
9424 | 9341 | |
9425 | 9342 | 指定した FILEHANDLE から、変数 SCALAR に LENGTH I<文字> の |
9426 | 9343 | データを読み込みます。 |
9427 | 9344 | 実際に読み込まれた文字数、 |
9428 | 9345 | ファイル終端の場合は C<0>、エラーの場合は undef のいずれかを返します |
9429 | 9346 | (後者の場合、C<$!> もセットされます)。 |
9430 | 9347 | SCALAR は伸び縮みするので、 |
9431 | 9348 | 読み込み後は、実際に読み込んだ最後の文字がスカラの最後の文字になります。 |
9432 | 9349 | |
9433 | 9350 | =begin original |
9434 | 9351 | |
9435 | 9352 | An OFFSET may be specified to place the read data at some place in the |
9436 | 9353 | string other than the beginning. A negative OFFSET specifies |
9437 | 9354 | placement at that many characters counting backwards from the end of |
9438 | 9355 | the string. A positive OFFSET greater than the length of SCALAR |
9439 | 9356 | results in the string being padded to the required size with C<"\0"> |
9440 | 9357 | bytes before the result of the read is appended. |
9441 | 9358 | |
9442 | 9359 | =end original |
9443 | 9360 | |
9444 | OFFSET を指定すると、 | |
9361 | OFFSET を指定すると、文字列の先頭以外の場所から、 | |
9445 | できます。 | |
9362 | 読み込みを行なうことができます。 | |
9446 | OFFSET | |
9363 | 負の数の OFFSET を指定すると、文字列の最後から逆方向に何文字目かを数えます。 | |
9447 | ||
9364 | SCALAR の長さよりも大きい、正の数の OFFSET を指定すると、文字列は | |
9448 | OFFSET が正の値で、SCALAR の長さよりも大きかった場合、文字列は | |
9449 | 9365 | 読み込みの結果が追加される前に、必要なサイズまで C<"\0"> のバイトで |
9450 | 9366 | パッディングされます。 |
9451 | 9367 | |
9452 | 9368 | =begin original |
9453 | 9369 | |
9454 | 9370 | The call is actually implemented in terms of either Perl's or system's |
9455 | 9371 | fread() call. To get a true read(2) system call, see C<sysread>. |
9456 | 9372 | |
9457 | 9373 | =end original |
9458 | 9374 | |
9459 | 9375 | この関数は、Perl か システムの fread() 関数を使って実装しています。 |
9460 | 9376 | 本当の read(2) システムコールを利用するには、C<sysread> を参照してください。 |
9461 | 9377 | |
9462 | 9378 | =begin original |
9463 | 9379 | |
9464 | 9380 | Note the I<characters>: depending on the status of the filehandle, |
9465 | 9381 | either (8-bit) bytes or characters are read. By default all |
9466 | 9382 | filehandles operate on bytes, but for example if the filehandle has |
9467 | 9383 | been opened with the C<:utf8> I/O layer (see L</open>, and the C<open> |
9468 | 9384 | pragma, L<open>), the I/O will operate on UTF-8 encoded Unicode |
9469 | 9385 | characters, not bytes. Similarly for the C<:encoding> pragma: |
9470 | 9386 | in that case pretty much any characters can be read. |
9471 | 9387 | |
9472 | 9388 | =end original |
9473 | 9389 | |
9474 | 9390 | I<文字> に関する注意: ファイルハンドルの状態によって、(8 ビットの) バイトか |
9475 | 9391 | 文字が読み込まれます。 |
9476 | 9392 | デフォルトでは全てのファイルハンドルはバイトを処理しますが、 |
9477 | 9393 | 例えばファイルハンドルが C<:utf8> I/O 層(L</open>, C<open> プラグマ、 |
9478 | 9394 | L<open> を参照してください) で開かれた場合、I/O はバイトではなく、 |
9479 | 9395 | UTF-8 エンコードされた Unicode 文字を操作します。 |
9480 | 9396 | C<:encoding> プラグマも同様です: |
9481 | 9397 | この場合、ほとんど大体全ての文字が読み込めます。 |
9482 | 9398 | |
9483 | 9399 | =item readdir DIRHANDLE |
9484 | 9400 | X<readdir> |
9485 | 9401 | |
9486 | 9402 | =begin original |
9487 | 9403 | |
9488 | 9404 | Returns the next directory entry for a directory opened by C<opendir>. |
9489 | 9405 | If used in list context, returns all the rest of the entries in the |
9490 | 9406 | directory. If there are no more entries, returns an undefined value in |
9491 | 9407 | scalar context or a null list in list context. |
9492 | 9408 | |
9493 | 9409 | =end original |
9494 | 9410 | |
9495 | 9411 | C<opendir> でオープンしたディレクトリで、 |
9496 | 9412 | 次のディレクトリエントリを返します。 |
9497 | 9413 | リストコンテキストで用いると、 |
9498 | 9414 | そのディレクトリの残りのエントリを、すべて返します。 |
9499 | 9415 | エントリが残っていない場合には、スカラコンテキストでは未定義値を、 |
9500 | 9416 | リストコンテキストでは空リストを返します。 |
9501 | 9417 | |
9502 | 9418 | =begin original |
9503 | 9419 | |
9504 | 9420 | If you're planning to filetest the return values out of a C<readdir>, you'd |
9505 | 9421 | better prepend the directory in question. Otherwise, because we didn't |
9506 | 9422 | C<chdir> there, it would have been testing the wrong file. |
9507 | 9423 | |
9508 | 9424 | =end original |
9509 | 9425 | |
9510 | 9426 | C<readdir> の返り値をファイルテストに使おうと計画しているなら、 |
9511 | 9427 | 頭にディレクトリをつける必要があります。 |
9512 | 9428 | さもなければ、ここでは C<chdir> はしないので、 |
9513 | 9429 | 間違ったファイルをテストしてしまうことになるでしょう。 |
9514 | 9430 | |
9515 | opendir( | |
9431 | opendir(DIR, $some_dir) || die "can't opendir $some_dir: $!"; | |
9516 | @dots = grep { /^\./ && -f "$some_dir/$_" } readdir( | |
9432 | @dots = grep { /^\./ && -f "$some_dir/$_" } readdir(DIR); | |
9517 | closedir | |
9433 | closedir DIR; | |
9518 | 9434 | |
9519 | 9435 | =item readline EXPR |
9520 | 9436 | |
9521 | 9437 | =item readline |
9522 | 9438 | X<readline> X<gets> X<fgets> |
9523 | 9439 | |
9524 | 9440 | =begin original |
9525 | 9441 | |
9526 | 9442 | Reads from the filehandle whose typeglob is contained in EXPR (or from |
9527 | 9443 | *ARGV if EXPR is not provided). In scalar context, each call reads and |
9528 | 9444 | returns the next line, until end-of-file is reached, whereupon the |
9529 | 9445 | subsequent call returns undef. In list context, reads until end-of-file |
9530 | 9446 | is reached and returns a list of lines. Note that the notion of "line" |
9531 | 9447 | used here is however you may have defined it with C<$/> or |
9532 | 9448 | C<$INPUT_RECORD_SEPARATOR>). See L<perlvar/"$/">. |
9533 | 9449 | |
9534 | 9450 | =end original |
9535 | 9451 | |
9536 | 9452 | 型グロブが EXPR (EXPR がない場合は *ARGV) に含まれている |
9537 | 9453 | ファイルハンドルから読み込みます。 |
9538 | 9454 | スカラコンテキストでは、呼び出し毎に一行読み込んで返します。 |
9539 | 9455 | ファイルの最後まで読み込んだら、以後の呼び出しでは undef を返します。 |
9540 | 9456 | リストコンテキストでは、ファイルの最後まで読み込んで、 |
9541 | 9457 | 行のリストを返します。 |
9542 | 9458 | ここでの「行」とは、C<$/> または C<$INPUT_RECORD_SEPARATOR> で |
9543 | 9459 | 定義されることに注意してください。L<perlvar/"$/"> も参照して下さい。 |
9544 | 9460 | |
9545 | 9461 | =begin original |
9546 | 9462 | |
9547 | 9463 | When C<$/> is set to C<undef>, when readline() is in scalar |
9548 | 9464 | context (i.e. file slurp mode), and when an empty file is read, it |
9549 | 9465 | returns C<''> the first time, followed by C<undef> subsequently. |
9550 | 9466 | |
9551 | 9467 | =end original |
9552 | 9468 | |
9553 | 9469 | C<$/> に C<undef> を設定した場合は、readline() はスカラコンテキスト |
9554 | 9470 | (つまりファイル吸い込みモード)となり、 |
9555 | 9471 | 空のファイルを読み込んだ場合は、最初は C<''> を返し、 |
9556 | 9472 | それ以降は C<undef> を返します。 |
9557 | 9473 | |
9558 | 9474 | =begin original |
9559 | 9475 | |
9560 | 9476 | This is the internal function implementing the C<< <EXPR> >> |
9561 | 9477 | operator, but you can use it directly. The C<< <EXPR> >> |
9562 | 9478 | operator is discussed in more detail in L<perlop/"I/O Operators">. |
9563 | 9479 | |
9564 | 9480 | =end original |
9565 | 9481 | |
9566 | 9482 | これは C<< <EXPR> >> 演算子を実装している内部関数ですが、 |
9567 | 9483 | 直接使うこともできます。 |
9568 | 9484 | C<< <EXPR> >> 演算子についてのさらなる詳細については |
9569 | 9485 | L<perlop/"I/O Operators"> で議論されています。 |
9570 | 9486 | |
9571 | 9487 | $line = <STDIN>; |
9572 | 9488 | $line = readline(*STDIN); # same thing |
9573 | 9489 | |
9574 | 9490 | =begin original |
9575 | 9491 | |
9576 | 9492 | If readline encounters an operating system error, C<$!> will be set with the |
9577 | 9493 | corresponding error message. It can be helpful to check C<$!> when you are |
9578 | 9494 | reading from filehandles you don't trust, such as a tty or a socket. The |
9579 | 9495 | following example uses the operator form of C<readline>, and takes the necessary |
9580 | 9496 | steps to ensure that C<readline> was successful. |
9581 | 9497 | |
9582 | 9498 | =end original |
9583 | 9499 | |
9584 | 9500 | readline が OS のシステムエラーになると、C<$!> に対応するエラーメッセージが |
9585 | 9501 | セットされます。 |
9586 | 9502 | tty やソケットといった、信頼できないファイルハンドルから読み込む時には |
9587 | 9503 | C<$!> をチェックするのが助けになります。 |
9588 | 9504 | 以下の例は演算子の形の C<readline> を使っており、C<readline> が |
9589 | 9505 | 成功したことを確実にするために必要なステップを実行しています。 |
9590 | 9506 | |
9591 | 9507 | for (;;) { |
9592 | 9508 | undef $!; |
9593 | 9509 | unless (defined( $line = <> )) { |
9594 | last if eof; | |
9595 | 9510 | die $! if $!; |
9511 | last; # reached EOF | |
9596 | 9512 | } |
9597 | 9513 | # ... |
9598 | 9514 | } |
9599 | 9515 | |
9600 | 9516 | =item readlink EXPR |
9601 | 9517 | X<readlink> |
9602 | 9518 | |
9603 | 9519 | =item readlink |
9604 | 9520 | |
9605 | 9521 | =begin original |
9606 | 9522 | |
9607 | 9523 | Returns the value of a symbolic link, if symbolic links are |
9608 | 9524 | implemented. If not, gives a fatal error. If there is some system |
9609 | 9525 | error, returns the undefined value and sets C<$!> (errno). If EXPR is |
9610 | 9526 | omitted, uses C<$_>. |
9611 | 9527 | |
9612 | 9528 | =end original |
9613 | 9529 | |
9614 | 9530 | シンボリックリンクが実装されていれば、 |
9615 | 9531 | シンボリックリンクの値を返します。 |
9616 | 9532 | 実装されていないときには、致命的エラーとなります。 |
9617 | 9533 | 何らかのシステムエラーが検出されると、 |
9618 | 9534 | 未定義値を返し、C<$!> (errno)を設定します。 |
9619 | 9535 | EXPR を省略すると、C<$_> を使用します。 |
9620 | 9536 | |
9621 | 9537 | =item readpipe EXPR |
9622 | 9538 | |
9623 | 9539 | =item readpipe |
9624 | 9540 | X<readpipe> |
9625 | 9541 | |
9626 | 9542 | =begin original |
9627 | 9543 | |
9628 | 9544 | EXPR is executed as a system command. |
9629 | 9545 | The collected standard output of the command is returned. |
9630 | 9546 | In scalar context, it comes back as a single (potentially |
9631 | 9547 | multi-line) string. In list context, returns a list of lines |
9632 | 9548 | (however you've defined lines with C<$/> or C<$INPUT_RECORD_SEPARATOR>). |
9633 | 9549 | This is the internal function implementing the C<qx/EXPR/> |
9634 | 9550 | operator, but you can use it directly. The C<qx/EXPR/> |
9635 | 9551 | operator is discussed in more detail in L<perlop/"I/O Operators">. |
9636 | 9552 | If EXPR is omitted, uses C<$_>. |
9637 | 9553 | |
9638 | 9554 | =end original |
9639 | 9555 | |
9640 | 9556 | EXPR がシステムコマンドとして実行されます。 |
9641 | 9557 | コマンドの標準出力の内容が返されます。 |
9642 | 9558 | スカラコンテキストでは、単一の(内部的に複数行の)文字列を返します。 |
9643 | 9559 | リストコンテキストでは、行のリストを返します |
9644 | 9560 | (但し、行は C<$/> または C<$INPUT_RECORD_SEPARATOR> で定義されます)。 |
9645 | 9561 | これは C<qx/EXPR/> 演算子を実装する内部関数ですが、 |
9646 | 9562 | 直接使うことも出来ます。 |
9647 | 9563 | C<qx/EXPR/> 演算子は L<perlop/"I/O Operators"> でより詳細に |
9648 | 9564 | 述べられています。 |
9649 | EXPR | |
9565 | EXPR を省略すると、C<$_> を使用します。 | |
9650 | 9566 | |
9651 | 9567 | =item recv SOCKET,SCALAR,LENGTH,FLAGS |
9652 | 9568 | X<recv> |
9653 | 9569 | |
9654 | 9570 | =begin original |
9655 | 9571 | |
9656 | 9572 | Receives a message on a socket. Attempts to receive LENGTH characters |
9657 | 9573 | of data into variable SCALAR from the specified SOCKET filehandle. |
9658 | 9574 | SCALAR will be grown or shrunk to the length actually read. Takes the |
9659 | 9575 | same flags as the system call of the same name. Returns the address |
9660 | 9576 | of the sender if SOCKET's protocol supports this; returns an empty |
9661 | 9577 | string otherwise. If there's an error, returns the undefined value. |
9662 | 9578 | This call is actually implemented in terms of recvfrom(2) system call. |
9663 | 9579 | See L<perlipc/"UDP: Message Passing"> for examples. |
9664 | 9580 | |
9665 | 9581 | =end original |
9666 | 9582 | |
9667 | 9583 | ソケット上のメッセージを受信します。 |
9668 | 9584 | 指定されたファイルハンドル SOCKET から、変数 SCALAR に |
9669 | 9585 | LENGTH 文字のデータを読み込もうとします。 |
9670 | 9586 | SCALAR は、実際に読まれた長さによって、大きくなったり、 |
9671 | 9587 | 小さくなったりします。 |
9672 | 9588 | 同名のシステムコールと同じ FLAGS を使います。 |
9673 | 9589 | SOCKET のプロトコルが対応していれば、送信側のアドレスを返します。 |
9674 | 9590 | エラー発生時には、未定義値を返します。 |
9675 | 9591 | 実際には、C のrecvfrom(2) を呼びます。 |
9676 | 9592 | 例についてはL<perlipc/"UDP: Message Passing">を参照してください。 |
9677 | 9593 | |
9678 | 9594 | =begin original |
9679 | 9595 | |
9680 | 9596 | Note the I<characters>: depending on the status of the socket, either |
9681 | 9597 | (8-bit) bytes or characters are received. By default all sockets |
9682 | 9598 | operate on bytes, but for example if the socket has been changed using |
9683 | 9599 | binmode() to operate with the C<:encoding(utf8)> I/O layer (see the |
9684 | 9600 | C<open> pragma, L<open>), the I/O will operate on UTF-8 encoded Unicode |
9685 | 9601 | characters, not bytes. Similarly for the C<:encoding> pragma: in that |
9686 | 9602 | case pretty much any characters can be read. |
9687 | 9603 | |
9688 | 9604 | =end original |
9689 | 9605 | |
9690 | 9606 | I<文字> に関する注意: ソケットの状態によって、(8 ビットの) バイトか |
9691 | 9607 | 文字を受信します。 |
9692 | 9608 | デフォルトでは全てのソケットはバイトを処理しますが、 |
9693 | 9609 | 例えばソケットが binmode() で C<:encoding(utf8)> I/O 層(C<open> プラグマ、 |
9694 | 9610 | L<open> を参照してください) を使うように指定された場合、I/O はバイトではなく、 |
9695 | 9611 | UTF-8 エンコードされた Unicode 文字を操作します。 |
9696 | 9612 | C<:encoding> プラグマも同様です: |
9697 | 9613 | この場合、ほとんど大体全ての文字が読み込めます。 |
9698 | 9614 | |
9699 | 9615 | =item redo LABEL |
9700 | 9616 | X<redo> |
9701 | 9617 | |
9702 | 9618 | =item redo |
9703 | 9619 | |
9704 | 9620 | =begin original |
9705 | 9621 | |
9706 | 9622 | The C<redo> command restarts the loop block without evaluating the |
9707 | 9623 | conditional again. The C<continue> block, if any, is not executed. If |
9708 | 9624 | the LABEL is omitted, the command refers to the innermost enclosing |
9709 | 9625 | loop. Programs that want to lie to themselves about what was just input |
9710 | 9626 | normally use this command: |
9711 | 9627 | |
9712 | 9628 | =end original |
9713 | 9629 | |
9714 | 9630 | C<redo> コマンドは、条件を再評価しないで、ループブロックの始めからもう一度 |
9715 | 9631 | 実行を開始します。 |
9716 | 9632 | C<continue> ブロックがあっても、実行されません。 |
9717 | 9633 | LABEL が省略されると、このコマンドは、もっとも内側のループを参照します。 |
9718 | 9634 | このコマンドは通常、自分への入力を欺くために使用します: |
9719 | 9635 | |
9720 | 9636 | # a simpleminded Pascal comment stripper |
9721 | 9637 | # (warning: assumes no { or } in strings) |
9722 | 9638 | LINE: while (<STDIN>) { |
9723 | 9639 | while (s|({.*}.*){.*}|$1 |) {} |
9724 | 9640 | s|{.*}| |; |
9725 | 9641 | if (s|{.*| |) { |
9726 | 9642 | $front = $_; |
9727 | 9643 | while (<STDIN>) { |
9728 | 9644 | if (/}/) { # end of comment? |
9729 | 9645 | s|^|$front\{|; |
9730 | 9646 | redo LINE; |
9731 | 9647 | } |
9732 | 9648 | } |
9733 | 9649 | } |
9734 | 9650 | print; |
9735 | 9651 | } |
9736 | 9652 | |
9737 | 9653 | =begin original |
9738 | 9654 | |
9739 | 9655 | C<redo> cannot be used to retry a block which returns a value such as |
9740 | 9656 | C<eval {}>, C<sub {}> or C<do {}>, and should not be used to exit |
9741 | 9657 | a grep() or map() operation. |
9742 | 9658 | |
9743 | 9659 | =end original |
9744 | 9660 | |
9745 | 9661 | C<redo> は C<eval {}>, C<sub {}>, C<do {}> のように値を返す |
9746 | 9662 | ブロックを繰り返すのには使えません。 |
9747 | 9663 | また、grep() や map() 操作から抜けるのに使うべきではありません。 |
9748 | 9664 | |
9749 | 9665 | =begin original |
9750 | 9666 | |
9751 | 9667 | Note that a block by itself is semantically identical to a loop |
9752 | 9668 | that executes once. Thus C<redo> inside such a block will effectively |
9753 | 9669 | turn it into a looping construct. |
9754 | 9670 | |
9755 | 9671 | =end original |
9756 | 9672 | |
9757 | 9673 | ブロック自身は一回だけ実行されるループと文法的に同一であることに |
9758 | 9674 | 注意してください。 |
9759 | 9675 | 従って、ブロックの中で C<redo> を使うことで効果的に |
9760 | 9676 | ループ構造に変換します。 |
9761 | 9677 | |
9762 | 9678 | =begin original |
9763 | 9679 | |
9764 | 9680 | See also L</continue> for an illustration of how C<last>, C<next>, and |
9765 | 9681 | C<redo> work. |
9766 | 9682 | |
9767 | 9683 | =end original |
9768 | 9684 | |
9769 | 9685 | C<last>, C<next>, C<redo> がどのように働くかについては |
9770 | L</continue> | |
9686 | L</continue> を参照して下さい。 | |
9771 | 9687 | |
9772 | 9688 | =item ref EXPR |
9773 | 9689 | X<ref> X<reference> |
9774 | 9690 | |
9775 | 9691 | =item ref |
9776 | 9692 | |
9777 | 9693 | =begin original |
9778 | 9694 | |
9779 | 9695 | Returns a non-empty string if EXPR is a reference, the empty |
9780 | 9696 | string otherwise. If EXPR |
9781 | 9697 | is not specified, C<$_> will be used. The value returned depends on the |
9782 | 9698 | type of thing the reference is a reference to. |
9783 | 9699 | Builtin types include: |
9784 | 9700 | |
9785 | 9701 | =end original |
9786 | 9702 | |
9787 | 9703 | EXPR がリファレンスであれば、空でない文字列を返し、さもなくば、 |
9788 | 9704 | 空文字列を返します。 |
9789 | 9705 | EXPR が指定されなければ、C<$_> が使われます。 |
9790 | 9706 | 返される値は、リファレンスが参照するものの型に依存します。 |
9791 | 9707 | 組み込みの型には、以下のものがあります。 |
9792 | 9708 | |
9793 | 9709 | SCALAR |
9794 | 9710 | ARRAY |
9795 | 9711 | HASH |
9796 | 9712 | CODE |
9797 | 9713 | REF |
9798 | 9714 | GLOB |
9799 | 9715 | LVALUE |
9800 | 9716 | FORMAT |
9801 | 9717 | IO |
9802 | 9718 | VSTRING |
9803 | 9719 | Regexp |
9804 | 9720 | |
9805 | 9721 | =begin original |
9806 | 9722 | |
9807 | 9723 | If the referenced object has been blessed into a package, then that package |
9808 | 9724 | name is returned instead. You can think of C<ref> as a C<typeof> operator. |
9809 | 9725 | |
9810 | 9726 | =end original |
9811 | 9727 | |
9812 | 9728 | 参照されるオブジェクトが、何らかのパッケージに |
9813 | 9729 | bless されたものであれば、これらの代わりに、 |
9814 | 9730 | そのパッケージ名が返されます。 |
9815 | 9731 | C<ref> は、C<typeof> 演算子のように考えることができます。 |
9816 | 9732 | |
9817 | 9733 | if (ref($r) eq "HASH") { |
9818 | 9734 | print "r is a reference to a hash.\n"; |
9819 | 9735 | } |
9820 | 9736 | unless (ref($r)) { |
9821 | 9737 | print "r is not a reference at all.\n"; |
9822 | 9738 | } |
9823 | 9739 | |
9824 | 9740 | =begin original |
9825 | 9741 | |
9826 | 9742 | The return value C<LVALUE> indicates a reference to an lvalue that is not |
9827 | 9743 | a variable. You get this from taking the reference of function calls like |
9828 | 9744 | C<pos()> or C<substr()>. C<VSTRING> is returned if the reference points |
9829 | 9745 | to a L<version string|perldata/"Version Strings">. |
9830 | 9746 | |
9831 | 9747 | =end original |
9832 | 9748 | |
9833 | 9749 | 返り値 C<LVALUE> は、変数ではない左辺値へのリファレンスを示します。 |
9834 | 9750 | これは、C<pos()> や C<substr()> のようの関数呼び出しのリファレンスから |
9835 | 9751 | 得られます。 |
9836 | 9752 | C<VSTRING> は、リファレンスが L<version string|perldata/"Version Strings"> を |
9837 | 9753 | 指している場合に返されます。 |
9838 | 9754 | |
9839 | 9755 | =begin original |
9840 | 9756 | |
9841 | 9757 | The result C<Regexp> indicates that the argument is a regular expression |
9842 | 9758 | resulting from C<qr//>. |
9843 | 9759 | |
9844 | 9760 | =end original |
9845 | 9761 | |
9846 | 9762 | C<Regexp> という結果は、引数が C<qr//> からの結果である |
9847 | 9763 | 正規表現であることを意味します。 |
9848 | 9764 | |
9849 | 9765 | =begin original |
9850 | 9766 | |
9851 | 9767 | See also L<perlref>. |
9852 | 9768 | |
9853 | 9769 | =end original |
9854 | 9770 | |
9855 | 9771 | L<perlref> も参照してください。 |
9856 | 9772 | |
9857 | 9773 | =item rename OLDNAME,NEWNAME |
9858 | 9774 | X<rename> X<move> X<mv> X<ren> |
9859 | 9775 | |
9860 | 9776 | =begin original |
9861 | 9777 | |
9862 | 9778 | Changes the name of a file; an existing file NEWNAME will be |
9863 | 9779 | clobbered. Returns true for success, false otherwise. |
9864 | 9780 | |
9865 | 9781 | =end original |
9866 | 9782 | |
9867 | 9783 | ファイルの名前を変更します。 |
9868 | 9784 | NEWNAME というファイルが既に存在した場合、上書きされるかもしれません。 |
9869 | 9785 | 成功時には真、失敗時には偽を返します。 |
9870 | 9786 | |
9871 | 9787 | =begin original |
9872 | 9788 | |
9873 | 9789 | Behavior of this function varies wildly depending on your system |
9874 | 9790 | implementation. For example, it will usually not work across file system |
9875 | 9791 | boundaries, even though the system I<mv> command sometimes compensates |
9876 | 9792 | for this. Other restrictions include whether it works on directories, |
9877 | 9793 | open files, or pre-existing files. Check L<perlport> and either the |
9878 | 9794 | rename(2) manpage or equivalent system documentation for details. |
9879 | 9795 | |
9880 | 9796 | =end original |
9881 | 9797 | |
9882 | 9798 | この関数の振る舞いはシステムの実装に大きく依存して異なります。 |
9883 | 9799 | 普通はファイルシステムにまたがってパス名を付け替えることはできません。 |
9884 | 9800 | システムの I<mv> がこれを補完している場合でもそうです。 |
9885 | 9801 | その他の制限には、ディレクトリ、オープンしているファイル、既に存在している |
9886 | 9802 | ファイルに対して使えるか、といったことを含みます。 |
9887 | 9803 | 詳しくは、L<perlport> および rename(2) man ページあるいは同様の |
9888 | 9804 | システムドキュメントを参照してください。 |
9889 | 9805 | |
9890 | 9806 | =begin original |
9891 | 9807 | |
9892 | 9808 | For a platform independent C<move> function look at the L<File::Copy> |
9893 | 9809 | module. |
9894 | 9810 | |
9895 | 9811 | =end original |
9896 | 9812 | |
9897 | 9813 | プラットフォームに依存しない C<move> 関数については L<File::Copy> モジュールを |
9898 | 9814 | 参照してください。 |
9899 | 9815 | |
9900 | 9816 | =item require VERSION |
9901 | 9817 | X<require> |
9902 | 9818 | |
9903 | 9819 | =item require EXPR |
9904 | 9820 | |
9905 | 9821 | =item require |
9906 | 9822 | |
9907 | 9823 | =begin original |
9908 | 9824 | |
9909 | 9825 | Demands a version of Perl specified by VERSION, or demands some semantics |
9910 | 9826 | specified by EXPR or by C<$_> if EXPR is not supplied. |
9911 | 9827 | |
9912 | 9828 | =end original |
9913 | 9829 | |
9914 | 9830 | VERSION で指定される Perl のバージョンを要求するか、 |
9915 | 9831 | EXPR (省略時には C<$_>) によって指定されるいくつかの動作を要求します。 |
9916 | 9832 | |
9917 | 9833 | =begin original |
9918 | 9834 | |
9919 | 9835 | VERSION may be either a numeric argument such as 5.006, which will be |
9920 | 9836 | compared to C<$]>, or a literal of the form v5.6.1, which will be compared |
9921 | 9837 | to C<$^V> (aka $PERL_VERSION). A fatal error is produced at run time if |
9922 | 9838 | VERSION is greater than the version of the current Perl interpreter. |
9923 | 9839 | Compare with L</use>, which can do a similar check at compile time. |
9924 | 9840 | |
9925 | 9841 | =end original |
9926 | 9842 | |
9927 | 9843 | VERSION は 5.006 のような数値(C<$]> と比較されます)か、v5.6.1 の形 |
9928 | 9844 | (C<$^V> (またの名を $PERL_VERSION) と比較されます)で指定します。 |
9929 | 9845 | VERSION が Perl の現在のバージョンより大きいと、実行時に致命的エラーが |
9930 | 9846 | 発生します。 |
9931 | 9847 | L</use> と似ていますが、これはコンパイル時にチェックされます。 |
9932 | 9848 | |
9933 | 9849 | =begin original |
9934 | 9850 | |
9935 | 9851 | Specifying VERSION as a literal of the form v5.6.1 should generally be |
9936 | 9852 | avoided, because it leads to misleading error messages under earlier |
9937 | 9853 | versions of Perl that do not support this syntax. The equivalent numeric |
9938 | 9854 | version should be used instead. |
9939 | 9855 | |
9940 | 9856 | =end original |
9941 | 9857 | |
9942 | 9858 | VERSION に v5.6.1 の形のリテラルを指定することは一般的には避けるべきです; |
9943 | 9859 | なぜなら、この文法に対応していない Perl の初期のバージョンでは |
9944 | 9860 | 誤解させるようなエラーメッセージが出るからです。 |
9945 | 9861 | 代わりに等価な数値表現を使うべきです。 |
9946 | 9862 | |
9947 | 9863 | require v5.6.1; # run time version check |
9948 | 9864 | require 5.6.1; # ditto |
9949 | 9865 | require 5.006_001; # ditto; preferred for backwards compatibility |
9950 | 9866 | |
9951 | 9867 | =begin original |
9952 | 9868 | |
9953 | 9869 | Otherwise, C<require> demands that a library file be included if it |
9954 | 9870 | hasn't already been included. The file is included via the do-FILE |
9955 | 9871 | mechanism, which is essentially just a variety of C<eval> with the |
9956 | 9872 | caveat that lexical variables in the invoking script will be invisible |
9957 | 9873 | to the included code. Has semantics similar to the following subroutine: |
9958 | 9874 | |
9959 | 9875 | =end original |
9960 | 9876 | |
9961 | 9877 | それ以外の場合には、C<require> は、既に読み込まれていないときに読み込む |
9962 | 9878 | ライブラリファイルを要求するものとなります。 |
9963 | 9879 | そのファイルは、基本的には C<eval> の一種である、do-FILE によって |
9964 | 9880 | 読み込まれますが、起動したスクリプトのレキシカル変数は読み込まれたコードから |
9965 | 9881 | 見えないという欠点があります。 |
9966 | 9882 | 意味的には、次のようなサブルーチンと同じようなものです: |
9967 | 9883 | |
9968 | 9884 | sub require { |
9969 | 9885 | my ($filename) = @_; |
9970 | 9886 | if (exists $INC{$filename}) { |
9971 | 9887 | return 1 if $INC{$filename}; |
9972 | 9888 | die "Compilation failed in require"; |
9973 | 9889 | } |
9974 | 9890 | my ($realfilename,$result); |
9975 | 9891 | ITER: { |
9976 | 9892 | foreach $prefix (@INC) { |
9977 | 9893 | $realfilename = "$prefix/$filename"; |
9978 | 9894 | if (-f $realfilename) { |
9979 | 9895 | $INC{$filename} = $realfilename; |
9980 | 9896 | $result = do $realfilename; |
9981 | 9897 | last ITER; |
9982 | 9898 | } |
9983 | 9899 | } |
9984 | 9900 | die "Can't find $filename in \@INC"; |
9985 | 9901 | } |
9986 | 9902 | if ($@) { |
9987 | 9903 | $INC{$filename} = undef; |
9988 | 9904 | die $@; |
9989 | 9905 | } elsif (!$result) { |
9990 | 9906 | delete $INC{$filename}; |
9991 | 9907 | die "$filename did not return true value"; |
9992 | 9908 | } else { |
9993 | 9909 | return $result; |
9994 | 9910 | } |
9995 | 9911 | } |
9996 | 9912 | |
9997 | 9913 | =begin original |
9998 | 9914 | |
9999 | 9915 | Note that the file will not be included twice under the same specified |
10000 | 9916 | name. |
10001 | 9917 | |
10002 | 9918 | =end original |
10003 | 9919 | |
10004 | 9920 | ファイルは、同じ名前で 2 回読み込まれることはないことに注意してください。 |
10005 | 9921 | |
10006 | 9922 | =begin original |
10007 | 9923 | |
10008 | 9924 | The file must return true as the last statement to indicate |
10009 | 9925 | successful execution of any initialization code, so it's customary to |
10010 | 9926 | end such a file with C<1;> unless you're sure it'll return true |
10011 | 9927 | otherwise. But it's better just to put the C<1;>, in case you add more |
10012 | 9928 | statements. |
10013 | 9929 | |
10014 | 9930 | =end original |
10015 | 9931 | |
10016 | 9932 | 初期化コードの実行がうまくいったことを示すために、 |
10017 | 9933 | ファイルは真を返さなければなりませんから、 |
10018 | 9934 | 真を返すようになっている自信がある場合を除いては、 |
10019 | 9935 | ファイルの最後に C<1;> と書くのが習慣です。 |
10020 | 9936 | 実行文を追加するような場合に備えて、C<1;> と書いておいた方が |
10021 | 9937 | 良いでしょう。 |
10022 | 9938 | |
10023 | 9939 | =begin original |
10024 | 9940 | |
10025 | 9941 | If EXPR is a bareword, the require assumes a "F<.pm>" extension and |
10026 | 9942 | replaces "F<::>" with "F</>" in the filename for you, |
10027 | 9943 | to make it easy to load standard modules. This form of loading of |
10028 | 9944 | modules does not risk altering your namespace. |
10029 | 9945 | |
10030 | 9946 | =end original |
10031 | 9947 | |
10032 | 9948 | EXPR が裸の単語であるときには、標準モジュールのロードを |
10033 | 9949 | 簡単にするように、require は拡張子が "F<.pm>" であり、 |
10034 | 9950 | "F<::>" を "F</>" に変えたものがファイル名であると仮定します。 |
10035 | 9951 | この形式のモジュールロードは、 |
10036 | 9952 | 名前空間を変更してしまう危険はありません。 |
10037 | 9953 | |
10038 | 9954 | =begin original |
10039 | 9955 | |
10040 | 9956 | In other words, if you try this: |
10041 | 9957 | |
10042 | 9958 | =end original |
10043 | 9959 | |
10044 | 9960 | 言い換えると、以下のようにすると: |
10045 | 9961 | |
10046 | 9962 | require Foo::Bar; # a splendid bareword |
10047 | 9963 | |
10048 | 9964 | =begin original |
10049 | 9965 | |
10050 | 9966 | The require function will actually look for the "F<Foo/Bar.pm>" file in the |
10051 | 9967 | directories specified in the C<@INC> array. |
10052 | 9968 | |
10053 | 9969 | =end original |
10054 | 9970 | |
10055 | 9971 | require 関数は C<@INC> 配列で指定されたディレクトリにある |
10056 | 9972 | "F<Foo/Bar.pm>" ファイルを探します。 |
10057 | 9973 | |
10058 | 9974 | =begin original |
10059 | 9975 | |
10060 | 9976 | But if you try this: |
10061 | 9977 | |
10062 | 9978 | =end original |
10063 | 9979 | |
10064 | 9980 | しかし、以下のようにすると: |
10065 | 9981 | |
10066 | 9982 | $class = 'Foo::Bar'; |
10067 | 9983 | require $class; # $class is not a bareword |
10068 | 9984 | #or |
10069 | 9985 | require "Foo::Bar"; # not a bareword because of the "" |
10070 | 9986 | |
10071 | 9987 | =begin original |
10072 | 9988 | |
10073 | 9989 | The require function will look for the "F<Foo::Bar>" file in the @INC array and |
10074 | 9990 | will complain about not finding "F<Foo::Bar>" there. In this case you can do: |
10075 | 9991 | |
10076 | 9992 | =end original |
10077 | 9993 | |
10078 | 9994 | require 関数は @INC 配列の "F<Foo::Bar>" ファイルを探し、 |
10079 | 9995 | おそらくそこに "F<Foo::Bar>" がないと文句をいうことになるでしょう。 |
10080 | 9996 | このような場合には、以下のようにします: |
10081 | 9997 | |
10082 | 9998 | eval "require $class"; |
10083 | 9999 | |
10084 | 10000 | =begin original |
10085 | 10001 | |
10086 | 10002 | Now that you understand how C<require> looks for files in the case of a |
10087 | 10003 | bareword argument, there is a little extra functionality going on behind |
10088 | 10004 | the scenes. Before C<require> looks for a "F<.pm>" extension, it will |
10089 | 10005 | first look for a similar filename with a "F<.pmc>" extension. If this file |
10090 | 10006 | is found, it will be loaded in place of any file ending in a "F<.pm>" |
10091 | 10007 | extension. |
10092 | 10008 | |
10093 | 10009 | =end original |
10094 | 10010 | |
10095 | 10011 | 引数が裸の単語の場合、C<require> がどのようにファイルを探すかを |
10096 | 10012 | 理解してください; 水面下でちょっとした追加の機能があります。 |
10097 | 10013 | C<require> が拡張子 "F<.pm>" のファイルを探す前に、まず拡張子 "F<.pmc>" を |
10098 | 10014 | 持つファイルを探します。 |
10099 | 10015 | このファイルが見つかると、このファイルが拡張子 "F<.pm>" の代わりに |
10100 | 10016 | 読み込まれます。 |
10101 | 10017 | |
10102 | 10018 | =begin original |
10103 | 10019 | |
10104 | 10020 | You can also insert hooks into the import facility, by putting directly |
10105 | 10021 | Perl code into the @INC array. There are three forms of hooks: subroutine |
10106 | 10022 | references, array references and blessed objects. |
10107 | 10023 | |
10108 | 10024 | =end original |
10109 | 10025 | |
10110 | 10026 | @INC 配列に直接 Perl コードを入れることで、インポート機能にフックを |
10111 | 10027 | 挿入できます。 |
10112 | 10028 | 3 種類のフックがあります: サブルーチンリファレンス、配列リファレンス、 |
10113 | 10029 | bless されたオブジェクトです。 |
10114 | 10030 | |
10115 | 10031 | =begin original |
10116 | 10032 | |
10117 | 10033 | Subroutine references are the simplest case. When the inclusion system |
10118 | 10034 | walks through @INC and encounters a subroutine, this subroutine gets |
10119 | 10035 | called with two parameters, the first being a reference to itself, and the |
10120 | 10036 | second the name of the file to be included (e.g. "F<Foo/Bar.pm>"). The |
10121 | 10037 | subroutine should return nothing, or a list of up to three values in the |
10122 | 10038 | following order: |
10123 | 10039 | |
10124 | 10040 | =end original |
10125 | 10041 | |
10126 | 10042 | サブルーチンへのリファレンスは一番単純な場合です。 |
10127 | 10043 | インクルード機能が @INC を走査してサブルーチンに出会った場合、この |
10128 | 10044 | サブルーチンは二つの引数と共に呼び出されます; |
10129 | 10045 | 一つ目は自身へのリファレンス、二つ目はインクルードされるファイル名 |
10130 | 10046 | ("F<Foo/Bar.pm>" など)です。 |
10131 | 10047 | サブルーチンは C<undef> か、インクルードするファイルが読み込まれる |
10132 | 10048 | ファイルハンドルを返します。 |
10133 | 10049 | サブルーチンは何も返さないか、以下の順で最大 3 つの値のリストを |
10134 | 10050 | 返します。 |
10135 | 10051 | |
10136 | 10052 | =over |
10137 | 10053 | |
10138 | 10054 | =item 1 |
10139 | 10055 | |
10140 | 10056 | =begin original |
10141 | 10057 | |
10142 | 10058 | A filehandle, from which the file will be read. |
10143 | 10059 | |
10144 | 10060 | =end original |
10145 | 10061 | |
10146 | 10062 | ファイルが読み込まれるファイルハンドル。 |
10147 | 10063 | |
10148 | 10064 | =item 2 |
10149 | 10065 | |
10150 | 10066 | =begin original |
10151 | 10067 | |
10152 | 10068 | A reference to a subroutine. If there is no filehandle (previous item), |
10153 | 10069 | then this subroutine is expected to generate one line of source code per |
10154 | 10070 | call, writing the line into C<$_> and returning 1, then returning 0 at |
10155 | 10071 | "end of file". If there is a filehandle, then the subroutine will be |
10156 | called to act a | |
10072 | called to act a simple source filter, with the line as read in C<$_>. | |
10157 | 10073 | Again, return 1 for each valid line, and 0 after all lines have been |
10158 | 10074 | returned. |
10159 | 10075 | |
10160 | 10076 | =end original |
10161 | 10077 | |
10162 | 10078 | サブルーチンへのリファレンス。 |
10163 | 10079 | (一つ前のアイテムである)ファイルハンドルがない場合、 |
10164 | 10080 | サブルーチンは呼び出し毎に一行のソースコードを生成し、その行を C<$_> に |
10165 | 10081 | 書き込んで 1 を返し、それから「ファイル終端」で 0 を返すものと想定されます。 |
10166 | 10082 | ファイルハンドルがある場合、サブルーチンは単純なソースフィルタとして |
10167 | 10083 | 振舞うように呼び出され、行は C<$_> から読み込まれます。 |
10168 | 10084 | 再び、有効な行ごとに 1 を返し、全ての行を返した後では 0 を返します。 |
10169 | 10085 | |
10170 | 10086 | =item 3 |
10171 | 10087 | |
10172 | 10088 | =begin original |
10173 | 10089 | |
10174 | 10090 | Optional state for the subroutine. The state is passed in as C<$_[1]>. A |
10175 | 10091 | reference to the subroutine itself is passed in as C<$_[0]>. |
10176 | 10092 | |
10177 | 10093 | =end original |
10178 | 10094 | |
10179 | 10095 | サブルーチンのための状態(オプション)。 |
10180 | 10096 | 状態は C<$_[1]> として渡されます。 |
10181 | 10097 | サブルーチンへのリファレンス自身は C<$_[0]> として渡されます。 |
10182 | 10098 | |
10183 | 10099 | =back |
10184 | 10100 | |
10185 | 10101 | =begin original |
10186 | 10102 | |
10187 | 10103 | If an empty list, C<undef>, or nothing that matches the first 3 values above |
10188 | 10104 | is returned then C<require> will look at the remaining elements of @INC. |
10189 | 10105 | Note that this file handle must be a real file handle (strictly a typeglob, |
10190 | 10106 | or reference to a typeglob, blessed or unblessed) - tied file handles will be |
10191 | 10107 | ignored and return value processing will stop there. |
10192 | 10108 | |
10193 | 10109 | =end original |
10194 | 10110 | |
10195 | 10111 | 空リスト、C<undef>、または上記の最初の 3 つの値のどれとも一致しないものが |
10196 | 10112 | 返されると、C<require> は @INC の残りの要素を見ます。 |
10197 | 10113 | このファイルハンドルは実際のファイルハンドル(厳密には型グロブ、型グロブへの |
10198 | 10114 | リファレンス、bless されているかされていないか)である必要があります - |
10199 | 10115 | tie されたファイルハンドルは無視され、返り値の処理はそこで停止します。 |
10200 | 10116 | |
10201 | 10117 | =begin original |
10202 | 10118 | |
10203 | 10119 | If the hook is an array reference, its first element must be a subroutine |
10204 | 10120 | reference. This subroutine is called as above, but the first parameter is |
10205 | 10121 | the array reference. This enables to pass indirectly some arguments to |
10206 | 10122 | the subroutine. |
10207 | 10123 | |
10208 | 10124 | =end original |
10209 | 10125 | |
10210 | 10126 | フックが配列のリファレンスの場合、その最初の要素はサブルーチンへの |
10211 | 10127 | リファレンスでなければなりません。 |
10212 | 10128 | このサブルーチンは上述のように呼び出されますが、その最初の引数は |
10213 | 10129 | 配列のリファレンスです。 |
10214 | 10130 | これによって、間接的にサブルーチンに引数を渡すことが出来ます。 |
10215 | 10131 | |
10216 | 10132 | =begin original |
10217 | 10133 | |
10218 | 10134 | In other words, you can write: |
10219 | 10135 | |
10220 | 10136 | =end original |
10221 | 10137 | |
10222 | 10138 | 言い換えると、以下のように書いたり: |
10223 | 10139 | |
10224 | 10140 | push @INC, \&my_sub; |
10225 | 10141 | sub my_sub { |
10226 | 10142 | my ($coderef, $filename) = @_; # $coderef is \&my_sub |
10227 | 10143 | ... |
10228 | 10144 | } |
10229 | 10145 | |
10230 | 10146 | =begin original |
10231 | 10147 | |
10232 | 10148 | or: |
10233 | 10149 | |
10234 | 10150 | =end original |
10235 | 10151 | |
10236 | 10152 | または以下のように書けます: |
10237 | 10153 | |
10238 | 10154 | push @INC, [ \&my_sub, $x, $y, ... ]; |
10239 | 10155 | sub my_sub { |
10240 | 10156 | my ($arrayref, $filename) = @_; |
10241 | 10157 | # Retrieve $x, $y, ... |
10242 | 10158 | my @parameters = @$arrayref[1..$#$arrayref]; |
10243 | 10159 | ... |
10244 | 10160 | } |
10245 | 10161 | |
10246 | 10162 | =begin original |
10247 | 10163 | |
10248 | 10164 | If the hook is an object, it must provide an INC method that will be |
10249 | 10165 | called as above, the first parameter being the object itself. (Note that |
10250 | 10166 | you must fully qualify the sub's name, as unqualified C<INC> is always forced |
10251 | 10167 | into package C<main>.) Here is a typical code layout: |
10252 | 10168 | |
10253 | 10169 | =end original |
10254 | 10170 | |
10255 | 10171 | フックがオブジェクトの場合、INC メソッドを提供している必要があります; |
10256 | 10172 | それが、最初の引数をオブジェクト自身として上述のように呼び出されます。 |
10257 | 10173 | (修飾されていない C<INC> は常にパッケージ C<main> に強制されるため、 |
10258 | 10174 | サブルーチン名は完全修飾する必要があることに注意してください。) |
10259 | 10175 | 以下は典型的なコードレイアウトです: |
10260 | 10176 | |
10261 | 10177 | # In Foo.pm |
10262 | 10178 | package Foo; |
10263 | 10179 | sub new { ... } |
10264 | 10180 | sub Foo::INC { |
10265 | 10181 | my ($self, $filename) = @_; |
10266 | 10182 | ... |
10267 | 10183 | } |
10268 | 10184 | |
10269 | 10185 | # In the main program |
10270 | push @INC, | |
10186 | push @INC, new Foo(...); | |
10271 | 10187 | |
10272 | 10188 | =begin original |
10273 | 10189 | |
10274 | 10190 | Note that these hooks are also permitted to set the %INC entry |
10275 | 10191 | corresponding to the files they have loaded. See L<perlvar/%INC>. |
10276 | 10192 | |
10277 | 10193 | =end original |
10278 | 10194 | |
10279 | 10195 | これらのフックは、読み込まれるファイルに対応する %INC エントリを |
10280 | 10196 | セットすることも許可することに注意してください。 |
10281 | 10197 | L<perlvar/%INC> を参照してください。 |
10282 | 10198 | |
10283 | 10199 | =begin original |
10284 | 10200 | |
10285 | 10201 | For a yet-more-powerful import facility, see L</use> and L<perlmod>. |
10286 | 10202 | |
10287 | 10203 | =end original |
10288 | 10204 | |
10289 | 10205 | より強力な import 機能については、このドキュメントの |
10290 | 10206 | L</use> の項と、L<perlmod> を参照してください。 |
10291 | 10207 | |
10292 | 10208 | =item reset EXPR |
10293 | 10209 | X<reset> |
10294 | 10210 | |
10295 | 10211 | =item reset |
10296 | 10212 | |
10297 | 10213 | =begin original |
10298 | 10214 | |
10299 | 10215 | Generally used in a C<continue> block at the end of a loop to clear |
10300 | 10216 | variables and reset C<??> searches so that they work again. The |
10301 | 10217 | expression is interpreted as a list of single characters (hyphens |
10302 | 10218 | allowed for ranges). All variables and arrays beginning with one of |
10303 | 10219 | those letters are reset to their pristine state. If the expression is |
10304 | 10220 | omitted, one-match searches (C<?pattern?>) are reset to match again. Resets |
10305 | 10221 | only variables or searches in the current package. Always returns |
10306 | 10222 | 1. Examples: |
10307 | 10223 | |
10308 | 10224 | =end original |
10309 | 10225 | |
10310 | 10226 | 通常、ループの最後に、変数をクリアし、C<??> 検索を再び |
10311 | 10227 | 動作するようにリセットするため、C<continue> ブロックで使われます。 |
10312 | 10228 | EXPR は、文字を並べたもの (範囲を指定するのに、ハイフンが使えます) と |
10313 | 10229 | 解釈されます。 |
10314 | 10230 | 名前がその文字のいずれかで始まる変数や配列は、 |
10315 | 10231 | 最初の状態にリセットされます。 |
10316 | 10232 | EXPR を省略すると、1 回検索 (C<?PATTERN?>) を再びマッチするように |
10317 | 10233 | リセットできます。 |
10318 | 10234 | カレントパッケージの変数もしくは検索だけがリセットされます。 |
10319 | 10235 | 常に 1 を返します。 |
10320 | 10236 | 例: |
10321 | 10237 | |
10322 | 10238 | reset 'X'; # reset all X variables |
10323 | 10239 | reset 'a-z'; # reset lower case variables |
10324 | 10240 | reset; # just reset ?one-time? searches |
10325 | 10241 | |
10326 | 10242 | =begin original |
10327 | 10243 | |
10328 | 10244 | Resetting C<"A-Z"> is not recommended because you'll wipe out your |
10329 | 10245 | C<@ARGV> and C<@INC> arrays and your C<%ENV> hash. Resets only package |
10330 | 10246 | variables--lexical variables are unaffected, but they clean themselves |
10331 | 10247 | up on scope exit anyway, so you'll probably want to use them instead. |
10332 | 10248 | See L</my>. |
10333 | 10249 | |
10334 | 10250 | =end original |
10335 | 10251 | |
10336 | 10252 | reset C<"A-Z"> とすると、C<@ARGV>, C<@INC> 配列や C<%ENV> ハッシュも |
10337 | 10253 | なくなってしまいますから、止めた方が良いでしょう。 |
10338 | 10254 | パッケージ変数だけがリセットされます。 |
10339 | 10255 | レキシカル変数は、影響を受けませんが、スコープから外れれば、 |
10340 | 10256 | 自動的に綺麗になりますので、これからは、こちらを使うようにした方が |
10341 | 10257 | よいでしょう。 |
10342 | 10258 | L</my> を参照してください。 |
10343 | 10259 | |
10344 | 10260 | =item return EXPR |
10345 | 10261 | X<return> |
10346 | 10262 | |
10347 | 10263 | =item return |
10348 | 10264 | |
10349 | 10265 | =begin original |
10350 | 10266 | |
10351 | 10267 | Returns from a subroutine, C<eval>, or C<do FILE> with the value |
10352 | 10268 | given in EXPR. Evaluation of EXPR may be in list, scalar, or void |
10353 | 10269 | context, depending on how the return value will be used, and the context |
10354 | 10270 | may vary from one execution to the next (see C<wantarray>). If no EXPR |
10355 | 10271 | is given, returns an empty list in list context, the undefined value in |
10356 | 10272 | scalar context, and (of course) nothing at all in a void context. |
10357 | 10273 | |
10358 | 10274 | =end original |
10359 | 10275 | |
10360 | 10276 | サブルーチン, C<eval>, C<do FILE> から EXPR で与えられた値をもって、 |
10361 | 10277 | リターンします。 |
10362 | 10278 | EXPR の評価は、返り値がどのように使われるかによって |
10363 | 10279 | リスト、スカラ、無効コンテキストになります。 |
10364 | 10280 | またコンテキストは実行毎に変わります(C<wantarray> を参照してください)。 |
10365 | 10281 | EXPR が指定されなかった場合は、リストコンテキストでは空リストを、 |
10366 | 10282 | スカラコンテキストでは未定義値を返します。 |
10367 | 10283 | そして(もちろん)無効コンテキストでは何も返しません。 |
10368 | 10284 | |
10369 | 10285 | =begin original |
10370 | 10286 | |
10371 | 10287 | (Note that in the absence of an explicit C<return>, a subroutine, eval, |
10372 | 10288 | or do FILE will automatically return the value of the last expression |
10373 | 10289 | evaluated.) |
10374 | 10290 | |
10375 | 10291 | =end original |
10376 | 10292 | |
10377 | 10293 | (サブルーチン, eval, do FILE に明示的に C<return> が |
10378 | 10294 | なければ、最後に評価された値で、自動的にリターンします。) |
10379 | 10295 | |
10380 | 10296 | =item reverse LIST |
10381 | 10297 | X<reverse> X<rev> X<invert> |
10382 | 10298 | |
10383 | 10299 | =begin original |
10384 | 10300 | |
10385 | 10301 | In list context, returns a list value consisting of the elements |
10386 | 10302 | of LIST in the opposite order. In scalar context, concatenates the |
10387 | 10303 | elements of LIST and returns a string value with all characters |
10388 | 10304 | in the opposite order. |
10389 | 10305 | |
10390 | 10306 | =end original |
10391 | 10307 | |
10392 | 10308 | リストコンテキストでは、LIST を構成する要素を逆順に並べた |
10393 | 10309 | リスト値を返します。 |
10394 | 10310 | スカラコンテキストでは、LIST の要素を連結して、 |
10395 | 10311 | 全ての文字を逆順にした文字列を返します。 |
10396 | 10312 | |
10397 | print | |
10313 | print reverse <>; # line tac, last line first | |
10398 | 10314 | |
10399 | | |
10315 | undef $/; # for efficiency of <> | |
10316 | print scalar reverse <>; # character tac, last line tsrif | |
10400 | 10317 | |
10401 | 10318 | =begin original |
10402 | 10319 | |
10403 | 10320 | Used without arguments in scalar context, reverse() reverses C<$_>. |
10404 | 10321 | |
10405 | 10322 | =end original |
10406 | 10323 | |
10407 | 10324 | スカラコンテキストで引数なしで使うと、reverse() は C<$_> を逆順にします。 |
10408 | 10325 | |
10409 | $_ = "dlrow ,olleH"; | |
10410 | print reverse; # No output, list context | |
10411 | print scalar reverse; # Hello, world | |
10412 | ||
10413 | 10326 | =begin original |
10414 | 10327 | |
10415 | 10328 | This operator is also handy for inverting a hash, although there are some |
10416 | 10329 | caveats. If a value is duplicated in the original hash, only one of those |
10417 | 10330 | can be represented as a key in the inverted hash. Also, this has to |
10418 | 10331 | unwind one hash and build a whole new one, which may take some time |
10419 | 10332 | on a large hash, such as from a DBM file. |
10420 | 10333 | |
10421 | 10334 | =end original |
10422 | 10335 | |
10423 | 10336 | この演算子はハッシュの逆順にするのにも便利ですが、いくつかの弱点があります。 |
10424 | 10337 | 元のハッシュで値が重複していると、それらのうち一つだけが |
10425 | 10338 | 逆順になったハッシュのキーとして表現されます。 |
10426 | 10339 | また、これは一つのハッシュをほどいて完全に新しいハッシュを作るので、 |
10427 | 10340 | DBM ファイルからのような大きなハッシュでは少し時間がかかります。 |
10428 | 10341 | |
10429 | 10342 | %by_name = reverse %by_address; # Invert the hash |
10430 | 10343 | |
10431 | 10344 | =item rewinddir DIRHANDLE |
10432 | 10345 | X<rewinddir> |
10433 | 10346 | |
10434 | 10347 | =begin original |
10435 | 10348 | |
10436 | 10349 | Sets the current position to the beginning of the directory for the |
10437 | 10350 | C<readdir> routine on DIRHANDLE. |
10438 | 10351 | |
10439 | 10352 | =end original |
10440 | 10353 | |
10441 | 10354 | DIRHANDLE に対する C<readdir> ルーチンの現在位置を |
10442 | 10355 | ディレクトリの最初に設定します。 |
10443 | 10356 | |
10444 | 10357 | =item rindex STR,SUBSTR,POSITION |
10445 | 10358 | X<rindex> |
10446 | 10359 | |
10447 | 10360 | =item rindex STR,SUBSTR |
10448 | 10361 | |
10449 | 10362 | =begin original |
10450 | 10363 | |
10451 | 10364 | Works just like index() except that it returns the position of the I<last> |
10452 | 10365 | occurrence of SUBSTR in STR. If POSITION is specified, returns the |
10453 | 10366 | last occurrence beginning at or before that position. |
10454 | 10367 | |
10455 | 10368 | =end original |
10456 | 10369 | |
10457 | 10370 | STR 中で I<最後に> 見つかった SUBSTR の位置を返すことを除いて、 |
10458 | 10371 | index() と同じように動作します。 |
10459 | 10372 | POSITION を指定すると、その位置から始まるか、その位置より前の、 |
10460 | 10373 | 最後の位置を返します。 |
10461 | 10374 | |
10462 | 10375 | =item rmdir FILENAME |
10463 | 10376 | X<rmdir> X<rd> X<directory, remove> |
10464 | 10377 | |
10465 | 10378 | =item rmdir |
10466 | 10379 | |
10467 | 10380 | =begin original |
10468 | 10381 | |
10469 | 10382 | Deletes the directory specified by FILENAME if that directory is |
10470 | 10383 | empty. If it succeeds it returns true, otherwise it returns false and |
10471 | 10384 | sets C<$!> (errno). If FILENAME is omitted, uses C<$_>. |
10472 | 10385 | |
10473 | 10386 | =end original |
10474 | 10387 | |
10475 | 10388 | FILENAME で指定したディレクトリが空であれば、 |
10476 | 10389 | そのディレクトリを削除します。 |
10477 | 10390 | 成功時には真を返し、失敗時には偽を返し、C<$!> (errno) を設定します。 |
10478 | 10391 | FILENAMEを省略した場合には、C<$_> を使用します。 |
10479 | 10392 | |
10480 | 10393 | =begin original |
10481 | 10394 | |
10482 | 10395 | To remove a directory tree recursively (C<rm -rf> on unix) look at |
10483 | 10396 | the C<rmtree> function of the L<File::Path> module. |
10484 | 10397 | |
10485 | 10398 | =end original |
10486 | 10399 | |
10487 | 10400 | ディレクトリツリーを再帰的に削除したい (unix での C<rm -rf>) 場合、 |
10488 | 10401 | L<File::Path> モジュールの C<rmtree> 関数を参照してください。 |
10489 | 10402 | |
10490 | 10403 | =item s/// |
10491 | 10404 | |
10492 | 10405 | =begin original |
10493 | 10406 | |
10494 | The substitution operator. See L<perlop | |
10407 | The substitution operator. See L<perlop>. | |
10495 | 10408 | |
10496 | 10409 | =end original |
10497 | 10410 | |
10498 | 10411 | 置換演算子。 |
10499 | L<perlop | |
10412 | L<perlop> を参照してください。 | |
10500 | 10413 | |
10501 | 10414 | =item say FILEHANDLE LIST |
10502 | 10415 | X<say> |
10503 | 10416 | |
10504 | 10417 | =item say LIST |
10505 | 10418 | |
10506 | 10419 | =item say |
10507 | 10420 | |
10508 | 10421 | =begin original |
10509 | 10422 | |
10510 | 10423 | Just like C<print>, but implicitly appends a newline. |
10511 | 10424 | C<say LIST> is simply an abbreviation for C<{ local $\ = "\n"; print |
10512 | 10425 | LIST }>. |
10513 | 10426 | |
10514 | 10427 | =end original |
10515 | 10428 | |
10516 | 10429 | C<print> と同様ですが、暗黙に改行が追加されます。 |
10517 | 10430 | C<say LIST> は単に C<{ local $\ = "\n"; print LIST }> の省略形です。 |
10518 | 10431 | |
10519 | 10432 | =begin original |
10520 | 10433 | |
10521 | 10434 | This keyword is only available when the "say" feature is |
10522 | 10435 | enabled: see L<feature>. |
10523 | 10436 | |
10524 | 10437 | =end original |
10525 | 10438 | |
10526 | 10439 | このキーワードは、"say" 機能が有効の場合にのみ利用可能です: |
10527 | 10440 | L<feature> を参照してください。 |
10528 | 10441 | |
10529 | 10442 | =item scalar EXPR |
10530 | 10443 | X<scalar> X<context> |
10531 | 10444 | |
10532 | 10445 | =begin original |
10533 | 10446 | |
10534 | 10447 | Forces EXPR to be interpreted in scalar context and returns the value |
10535 | 10448 | of EXPR. |
10536 | 10449 | |
10537 | 10450 | =end original |
10538 | 10451 | |
10539 | 10452 | EXPR を強制的にスカラコンテキストで解釈されるようにして、 |
10540 | 10453 | EXPR の値を返します。 |
10541 | 10454 | |
10542 | 10455 | @counts = ( scalar @a, scalar @b, scalar @c ); |
10543 | 10456 | |
10544 | 10457 | =begin original |
10545 | 10458 | |
10546 | 10459 | There is no equivalent operator to force an expression to |
10547 | 10460 | be interpolated in list context because in practice, this is never |
10548 | 10461 | needed. If you really wanted to do so, however, you could use |
10549 | 10462 | the construction C<@{[ (some expression) ]}>, but usually a simple |
10550 | 10463 | C<(some expression)> suffices. |
10551 | 10464 | |
10552 | 10465 | =end original |
10553 | 10466 | |
10554 | 10467 | 式を強制的にリストコンテキストで解釈させるようにする演算子はありません。 |
10555 | 10468 | 理論的には不要だからです。 |
10556 | 10469 | それでも、もしそうしたいのなら、C<@{[ (some expression) ]}> という構造を |
10557 | 10470 | 使えます。 |
10558 | 10471 | しかし、普通は単に C<(some expression)> とすれば十分です。 |
10559 | 10472 | |
10560 | 10473 | =begin original |
10561 | 10474 | |
10562 | 10475 | Because C<scalar> is unary operator, if you accidentally use for EXPR a |
10563 | 10476 | parenthesized list, this behaves as a scalar comma expression, evaluating |
10564 | 10477 | all but the last element in void context and returning the final element |
10565 | 10478 | evaluated in scalar context. This is seldom what you want. |
10566 | 10479 | |
10567 | 10480 | =end original |
10568 | 10481 | |
10569 | 10482 | C<scalar> は単項演算子なので、EXPR として括弧でくくったリストを使った場合、 |
10570 | 10483 | これはスカラカンマ表現として振舞い、最後以外の全ては無効コンテキストとして |
10571 | 10484 | 扱われ、最後の要素をスカラコンテキストとして扱った結果が返されます。 |
10572 | 10485 | これがあなたの望むものであることはめったにないでしょう。 |
10573 | 10486 | |
10574 | 10487 | =begin original |
10575 | 10488 | |
10576 | 10489 | The following single statement: |
10577 | 10490 | |
10578 | 10491 | =end original |
10579 | 10492 | |
10580 | 10493 | 以下の 1 つの文は: |
10581 | 10494 | |
10582 | 10495 | print uc(scalar(&foo,$bar)),$baz; |
10583 | 10496 | |
10584 | 10497 | =begin original |
10585 | 10498 | |
10586 | 10499 | is the moral equivalent of these two: |
10587 | 10500 | |
10588 | 10501 | =end original |
10589 | 10502 | |
10590 | 10503 | 以下の 2 つの文と等価です。 |
10591 | 10504 | |
10592 | 10505 | &foo; |
10593 | 10506 | print(uc($bar),$baz); |
10594 | 10507 | |
10595 | 10508 | =begin original |
10596 | 10509 | |
10597 | 10510 | See L<perlop> for more details on unary operators and the comma operator. |
10598 | 10511 | |
10599 | 10512 | =end original |
10600 | 10513 | |
10601 | 10514 | 単項演算子とカンマ演算子に関する詳細については L<perlop> を参照して下さい。 |
10602 | 10515 | |
10603 | 10516 | =item seek FILEHANDLE,POSITION,WHENCE |
10604 | 10517 | X<seek> X<fseek> X<filehandle, position> |
10605 | 10518 | |
10606 | 10519 | =begin original |
10607 | 10520 | |
10608 | 10521 | Sets FILEHANDLE's position, just like the C<fseek> call of C<stdio>. |
10609 | 10522 | FILEHANDLE may be an expression whose value gives the name of the |
10610 | 10523 | filehandle. The values for WHENCE are C<0> to set the new position |
10611 | 10524 | I<in bytes> to POSITION, C<1> to set it to the current position plus |
10612 | 10525 | POSITION, and C<2> to set it to EOF plus POSITION (typically |
10613 | 10526 | negative). For WHENCE you may use the constants C<SEEK_SET>, |
10614 | 10527 | C<SEEK_CUR>, and C<SEEK_END> (start of the file, current position, end |
10615 | 10528 | of the file) from the Fcntl module. Returns C<1> upon success, C<0> |
10616 | 10529 | otherwise. |
10617 | 10530 | |
10618 | 10531 | =end original |
10619 | 10532 | |
10620 | 10533 | C<stdio> ライブラリの C<fseek> 関数のように、FILEHANDLE の |
10621 | 10534 | ファイルポインタを任意の位置に設定します。 |
10622 | 10535 | FILEHANDLE は、実際のファイルハンドル名を与える式でもかまいません。 |
10623 | 10536 | WHENCE の値が、C<0> ならば、新しい位置を POSITION の位置へ、C<1> ならば、 |
10624 | 10537 | 現在位置から I<バイト数で> POSITION 加えた位置へ、C<2> ならば、EOF から |
10625 | 10538 | POSITION だけ加えた位置へ、新しい位置を設定します。 |
10626 | 10539 | この値には、Fcntl モジュールで使われている C<SEEK_SET>、 |
10627 | 10540 | C<SEEK_CUR>、C<SEEK_END> |
10628 | 10541 | (ファイルの先頭、現在位置、ファイルの最後)という定数を使うこともできます。 |
10629 | 10542 | 成功時には、C<1> を、失敗時には C<0> を返します。 |
10630 | 10543 | |
10631 | 10544 | =begin original |
10632 | 10545 | |
10633 | 10546 | Note the I<in bytes>: even if the filehandle has been set to |
10634 | 10547 | operate on characters (for example by using the C<:encoding(utf8)> open |
10635 | 10548 | layer), tell() will return byte offsets, not character offsets |
10636 | 10549 | (because implementing that would render seek() and tell() rather slow). |
10637 | 10550 | |
10638 | 10551 | =end original |
10639 | 10552 | |
10640 | 10553 | I<バイト単位> に関する注意: ファイルハンドルが (例えば C<:encoding(utf8)> 層を |
10641 | 10554 | 使って)文字を操作するように設定されていたとしても、tell() は文字の |
10642 | 10555 | オフセットではなくバイトのオフセットを返すことに注意してください |
10643 | 10556 | (なぜならこれを実装すると seek() と tell() が遅くなってしまうからです)。 |
10644 | 10557 | |
10645 | 10558 | =begin original |
10646 | 10559 | |
10647 | 10560 | If you want to position file for C<sysread> or C<syswrite>, don't use |
10648 | 10561 | C<seek>--buffering makes its effect on the file's system position |
10649 | 10562 | unpredictable and non-portable. Use C<sysseek> instead. |
10650 | 10563 | |
10651 | 10564 | =end original |
10652 | 10565 | |
10653 | 10566 | C<sysread> や C<syswrite> のためにファイルの位置を指定したい場合は、 |
10654 | 10567 | C<seek> は使えません -- バッファリングのために動作は予測不能で |
10655 | 10568 | 移植性のないものになってしまいます。 |
10656 | 10569 | 代わりに C<sysseek> を使ってください。 |
10657 | 10570 | |
10658 | 10571 | =begin original |
10659 | 10572 | |
10660 | 10573 | Due to the rules and rigors of ANSI C, on some systems you have to do a |
10661 | 10574 | seek whenever you switch between reading and writing. Amongst other |
10662 | 10575 | things, this may have the effect of calling stdio's clearerr(3). |
10663 | 10576 | A WHENCE of C<1> (C<SEEK_CUR>) is useful for not moving the file position: |
10664 | 10577 | |
10665 | 10578 | =end original |
10666 | 10579 | |
10667 | 10580 | ANSI C の規則と困難により、システムによっては読み込みと書き込みを |
10668 | 10581 | 切り替える度にシークしなければならない場合があります。 |
10669 | 10582 | その他のことの中で、これは stdio の clearerr(3) を呼び出す効果があります。 |
10670 | 10583 | WHENCE の C<1> (C<SEEK_CUR>) が、ファイル位置を変えないので有用です: |
10671 | 10584 | |
10672 | 10585 | seek(TEST,0,1); |
10673 | 10586 | |
10674 | 10587 | =begin original |
10675 | 10588 | |
10676 | 10589 | This is also useful for applications emulating C<tail -f>. Once you hit |
10677 | 10590 | EOF on your read, and then sleep for a while, you might have to stick in a |
10678 | 10591 | seek() to reset things. The C<seek> doesn't change the current position, |
10679 | 10592 | but it I<does> clear the end-of-file condition on the handle, so that the |
10680 | 10593 | next C<< <FILE> >> makes Perl try again to read something. We hope. |
10681 | 10594 | |
10682 | 10595 | =end original |
10683 | 10596 | |
10684 | 10597 | これはアプリケーションで C<tail -f> をエミュレートするのにも有用です。 |
10685 | 10598 | 一度読み込み時に EOF に到達すると、しばらくスリープし、 |
10686 | 10599 | seek() することでリセットする必要があります。 |
10687 | 10600 | C<seek> は現在の位置を変更しませんが、ハンドルの EOF 状態を |
10688 | 10601 | I<クリアします> ので、次の C<< <FILE> >> で Perl は再び何かを |
10689 | 10602 | 読み込もうとします。 |
10690 | 10603 | そのはずです。 |
10691 | 10604 | |
10692 | 10605 | =begin original |
10693 | 10606 | |
10694 | 10607 | If that doesn't work (some IO implementations are particularly |
10695 | 10608 | cantankerous), then you may need something more like this: |
10696 | 10609 | |
10697 | 10610 | =end original |
10698 | 10611 | |
10699 | 10612 | これが動かない場合(特に意地の悪い IO 実装もあります)、 |
10700 | 10613 | 以下のようなことをする必要があります: |
10701 | 10614 | |
10702 | 10615 | for (;;) { |
10703 | 10616 | for ($curpos = tell(FILE); $_ = <FILE>; |
10704 | 10617 | $curpos = tell(FILE)) { |
10705 | 10618 | # search for some stuff and put it into files |
10706 | 10619 | } |
10707 | 10620 | sleep($for_a_while); |
10708 | 10621 | seek(FILE, $curpos, 0); |
10709 | 10622 | } |
10710 | 10623 | |
10711 | 10624 | =item seekdir DIRHANDLE,POS |
10712 | 10625 | X<seekdir> |
10713 | 10626 | |
10714 | 10627 | =begin original |
10715 | 10628 | |
10716 | 10629 | Sets the current position for the C<readdir> routine on DIRHANDLE. POS |
10717 | 10630 | must be a value returned by C<telldir>. C<seekdir> also has the same caveats |
10718 | 10631 | about possible directory compaction as the corresponding system library |
10719 | 10632 | routine. |
10720 | 10633 | |
10721 | 10634 | =end original |
10722 | 10635 | |
10723 | 10636 | DIRHANDLE での C<readdir> ルーチンの現在位置を設定します。 |
10724 | 10637 | POS は、C<telldir> が返す値でなければなりません。 |
10725 | 10638 | C<seekdir> は同名のシステムライブラリルーチンと同じく、 |
10726 | 10639 | ディレクトリ縮小時の問題が考えられます。 |
10727 | 10640 | |
10728 | 10641 | =item select FILEHANDLE |
10729 | 10642 | X<select> X<filehandle, default> |
10730 | 10643 | |
10731 | 10644 | =item select |
10732 | 10645 | |
10733 | 10646 | =begin original |
10734 | 10647 | |
10735 | 10648 | Returns the currently selected filehandle. If FILEHANDLE is supplied, |
10736 | 10649 | sets the new current default filehandle for output. This has two |
10737 | 10650 | effects: first, a C<write> or a C<print> without a filehandle will |
10738 | 10651 | default to this FILEHANDLE. Second, references to variables related to |
10739 | 10652 | output will refer to this output channel. For example, if you have to |
10740 | 10653 | set the top of form format for more than one output channel, you might |
10741 | 10654 | do the following: |
10742 | 10655 | |
10743 | 10656 | =end original |
10744 | 10657 | |
10745 | 10658 | その時点で、選択されていたファイルハンドルを返します。 |
10746 | 10659 | FILEHANDLE を指定した場合には、その値を出力のデフォルト |
10747 | 10660 | ファイルハンドルに設定します。 |
10748 | 10661 | これには、2 つの効果があります。 |
10749 | 10662 | まず、ファイルハンドルを指定しないで |
10750 | 10663 | C<write> や C<print> を行なった場合のデフォルトが、 |
10751 | 10664 | この FILEHANDLE になります。 |
10752 | 10665 | もう一つは、出力関連の変数への参照は、 |
10753 | 10666 | この出力チャネルを参照するようになります。 |
10754 | 10667 | たとえば、複数の出力チャネルに対して、ページ先頭フォーマットを |
10755 | 10668 | 設定しなければならないのであれば、 |
10756 | 10669 | 以下のようにしなければならないでしょう。 |
10757 | 10670 | |
10758 | 10671 | select(REPORT1); |
10759 | 10672 | $^ = 'report1_top'; |
10760 | 10673 | select(REPORT2); |
10761 | 10674 | $^ = 'report2_top'; |
10762 | 10675 | |
10763 | 10676 | =begin original |
10764 | 10677 | |
10765 | 10678 | FILEHANDLE may be an expression whose value gives the name of the |
10766 | 10679 | actual filehandle. Thus: |
10767 | 10680 | |
10768 | 10681 | =end original |
10769 | 10682 | |
10770 | 10683 | FILEHANDLE は、実際のファイルハンドルの名前を示す式でもかまいません。 |
10771 | 10684 | つまり、以下のようなものです: |
10772 | 10685 | |
10773 | 10686 | $oldfh = select(STDERR); $| = 1; select($oldfh); |
10774 | 10687 | |
10775 | 10688 | =begin original |
10776 | 10689 | |
10777 | 10690 | Some programmers may prefer to think of filehandles as objects with |
10778 | 10691 | methods, preferring to write the last example as: |
10779 | 10692 | |
10780 | 10693 | =end original |
10781 | 10694 | |
10782 | 10695 | ファイルハンドルはメソッドを持ったオブジェクトであると |
10783 | 10696 | 考えることを好むプログラマもいるかもしれません。 |
10784 | 10697 | そのような場合のための最後の例は以下のようなものです。 |
10785 | 10698 | |
10786 | 10699 | use IO::Handle; |
10787 | 10700 | STDERR->autoflush(1); |
10788 | 10701 | |
10789 | 10702 | =item select RBITS,WBITS,EBITS,TIMEOUT |
10790 | 10703 | X<select> |
10791 | 10704 | |
10792 | 10705 | =begin original |
10793 | 10706 | |
10794 | 10707 | This calls the select(2) system call with the bit masks specified, which |
10795 | 10708 | can be constructed using C<fileno> and C<vec>, along these lines: |
10796 | 10709 | |
10797 | 10710 | =end original |
10798 | 10711 | |
10799 | 10712 | これは、select(2) システムコールを、指定したビットマスクで呼び出します。 |
10800 | 10713 | ビットマスクは、C<fileno> と |
10801 | 10714 | C<vec> を使って、以下のようにして作成できます。 |
10802 | 10715 | |
10803 | 10716 | $rin = $win = $ein = ''; |
10804 | 10717 | vec($rin,fileno(STDIN),1) = 1; |
10805 | 10718 | vec($win,fileno(STDOUT),1) = 1; |
10806 | 10719 | $ein = $rin | $win; |
10807 | 10720 | |
10808 | 10721 | =begin original |
10809 | 10722 | |
10810 | 10723 | If you want to select on many filehandles you might wish to write a |
10811 | 10724 | subroutine: |
10812 | 10725 | |
10813 | 10726 | =end original |
10814 | 10727 | |
10815 | 10728 | 複数のファイルハンドルに select を行ないたいのであれば、 |
10816 | 10729 | 以下のようにします。 |
10817 | 10730 | |
10818 | 10731 | sub fhbits { |
10819 | 10732 | my(@fhlist) = split(' ',$_[0]); |
10820 | 10733 | my($bits); |
10821 | 10734 | for (@fhlist) { |
10822 | 10735 | vec($bits,fileno($_),1) = 1; |
10823 | 10736 | } |
10824 | 10737 | $bits; |
10825 | 10738 | } |
10826 | 10739 | $rin = fhbits('STDIN TTY SOCK'); |
10827 | 10740 | |
10828 | 10741 | =begin original |
10829 | 10742 | |
10830 | 10743 | The usual idiom is: |
10831 | 10744 | |
10832 | 10745 | =end original |
10833 | 10746 | |
10834 | 10747 | 通常は、 |
10835 | 10748 | |
10836 | 10749 | ($nfound,$timeleft) = |
10837 | 10750 | select($rout=$rin, $wout=$win, $eout=$ein, $timeout); |
10838 | 10751 | |
10839 | 10752 | =begin original |
10840 | 10753 | |
10841 | 10754 | or to block until something becomes ready just do this |
10842 | 10755 | |
10843 | 10756 | =end original |
10844 | 10757 | |
10845 | 10758 | のように使い、いずれかの準備が整うまでブロックするには、 |
10846 | 10759 | 以下のようにします。 |
10847 | 10760 | |
10848 | 10761 | $nfound = select($rout=$rin, $wout=$win, $eout=$ein, undef); |
10849 | 10762 | |
10850 | 10763 | =begin original |
10851 | 10764 | |
10852 | 10765 | Most systems do not bother to return anything useful in $timeleft, so |
10853 | 10766 | calling select() in scalar context just returns $nfound. |
10854 | 10767 | |
10855 | 10768 | =end original |
10856 | 10769 | |
10857 | 10770 | ほとんどのシステムではわざわざ意味のある値を $timeleft に返さないので、 |
10858 | 10771 | select() をスカラコンテキストで呼び出すと、単に $nfound を返します。 |
10859 | 10772 | |
10860 | 10773 | =begin original |
10861 | 10774 | |
10862 | 10775 | Any of the bit masks can also be undef. The timeout, if specified, is |
10863 | 10776 | in seconds, which may be fractional. Note: not all implementations are |
10864 | 10777 | capable of returning the $timeleft. If not, they always return |
10865 | 10778 | $timeleft equal to the supplied $timeout. |
10866 | 10779 | |
10867 | 10780 | =end original |
10868 | 10781 | |
10869 | 10782 | どのビットマスクにも undef を設定することができます。 |
10870 | 10783 | TIMEOUT を指定するときは、秒数で指定し、小数でかまいません。 |
10871 | 10784 | 注: すべての実装で、$timeleft が返せるものではありません。 |
10872 | 10785 | その場合、$timeleft には、常に指定した TIMEOUT と同じ値が返されます。 |
10873 | 10786 | |
10874 | 10787 | =begin original |
10875 | 10788 | |
10876 | 10789 | You can effect a sleep of 250 milliseconds this way: |
10877 | 10790 | |
10878 | 10791 | =end original |
10879 | 10792 | |
10880 | 10793 | 250 ミリ秒の sleep と同じ効果が、以下のようにして得られます。 |
10881 | 10794 | |
10882 | 10795 | select(undef, undef, undef, 0.25); |
10883 | 10796 | |
10884 | 10797 | =begin original |
10885 | 10798 | |
10886 | 10799 | Note that whether C<select> gets restarted after signals (say, SIGALRM) |
10887 | 10800 | is implementation-dependent. See also L<perlport> for notes on the |
10888 | 10801 | portability of C<select>. |
10889 | 10802 | |
10890 | 10803 | =end original |
10891 | 10804 | |
10892 | 10805 | C<select> がシグナル (例えば、SIGALRM) の後に再起動するかどうかは |
10893 | 10806 | 実装依存であることに注意してください。 |
10894 | 10807 | C<select> の移植性に関する注意については L<perlport> も参照してください。 |
10895 | 10808 | |
10896 | 10809 | =begin original |
10897 | 10810 | |
10898 | 10811 | On error, C<select> behaves like the select(2) system call : it returns |
10899 | 10812 | -1 and sets C<$!>. |
10900 | 10813 | |
10901 | 10814 | =end original |
10902 | 10815 | |
10903 | 10816 | エラー時は、C<select> は select(2) システムコールのように振舞います: |
10904 | 10817 | -1 を返し、C<$!> をセットします。 |
10905 | 10818 | |
10906 | 10819 | =begin original |
10907 | 10820 | |
10908 | 10821 | Note: on some Unixes, the select(2) system call may report a socket file |
10909 | 10822 | descriptor as "ready for reading", when actually no data is available, |
10910 | 10823 | thus a subsequent read blocks. It can be avoided using always the |
10911 | 10824 | O_NONBLOCK flag on the socket. See select(2) and fcntl(2) for further |
10912 | 10825 | details. |
10913 | 10826 | |
10914 | 10827 | =end original |
10915 | 10828 | |
10916 | 10829 | 注意: Unix の中には、実際に利用可能なデータがないために引き続く読み込みが |
10917 | 10830 | ブロックされる場合、select(2) システムコールが、ソケットファイル記述子が |
10918 | 10831 | 「読み込み準備中」であると報告するものもあります。 |
10919 | 10832 | これは、ソケットに対して常に O_NONBLOCK フラグを使うことで回避できます。 |
10920 | 10833 | さらなる詳細については select(2) と fcntl(2) を参照してください。 |
10921 | 10834 | |
10922 | 10835 | =begin original |
10923 | 10836 | |
10924 | 10837 | B<WARNING>: One should not attempt to mix buffered I/O (like C<read> |
10925 | 10838 | or <FH>) with C<select>, except as permitted by POSIX, and even |
10926 | 10839 | then only on POSIX systems. You have to use C<sysread> instead. |
10927 | 10840 | |
10928 | 10841 | =end original |
10929 | 10842 | |
10930 | 10843 | B<警告>: バッファ付き I/O (C<read> や <FH>) と C<select> を |
10931 | 10844 | 混ぜて使ってはいけません(例外: POSIX で認められている形で使い、 |
10932 | 10845 | POSIX システムでだけ動かす場合を除きます)。 |
10933 | 10846 | 代わりに C<sysread> を使わなければなりません。 |
10934 | 10847 | |
10935 | 10848 | =item semctl ID,SEMNUM,CMD,ARG |
10936 | 10849 | X<semctl> |
10937 | 10850 | |
10938 | 10851 | =begin original |
10939 | 10852 | |
10940 | 10853 | Calls the System V IPC function C<semctl>. You'll probably have to say |
10941 | 10854 | |
10942 | 10855 | =end original |
10943 | 10856 | |
10944 | 10857 | System V IPC 関数 C<semctl> を呼び出します。 |
10945 | 10858 | 正しい定数定義を得るために、まず |
10946 | 10859 | |
10947 | 10860 | use IPC::SysV; |
10948 | 10861 | |
10949 | 10862 | =begin original |
10950 | 10863 | |
10951 | 10864 | first to get the correct constant definitions. If CMD is IPC_STAT or |
10952 | 10865 | GETALL, then ARG must be a variable that will hold the returned |
10953 | 10866 | semid_ds structure or semaphore value array. Returns like C<ioctl>: |
10954 | 10867 | the undefined value for error, "C<0 but true>" for zero, or the actual |
10955 | 10868 | return value otherwise. The ARG must consist of a vector of native |
10956 | 10869 | short integers, which may be created with C<pack("s!",(0)x$nsem)>. |
10957 | 10870 | See also L<perlipc/"SysV IPC">, C<IPC::SysV>, C<IPC::Semaphore> |
10958 | 10871 | documentation. |
10959 | 10872 | |
10960 | 10873 | =end original |
10961 | 10874 | |
10962 | 10875 | と宣言する必要があるでしょう。 |
10963 | 10876 | CMD が、IPC_STAT か GETALL のときには、ARG は、返される |
10964 | 10877 | semid_ds 構造体か、セマフォ値の配列を納める変数でなければなりません。 |
10965 | 10878 | C<ioctl> と同じように、エラー時には未定義値、 |
10966 | 10879 | ゼロのときは C<"0 だが真">、それ以外なら、その値そのものを返します。 |
10967 | 10880 | ARG はネイティブな short int のベクターから成っていなければなりません。 |
10968 | 10881 | これは C<pack("s!",(0)x$nsem)> で作成できます。 |
10969 | 10882 | L<perlipc/"SysV IPC">, C<IPC::SysV>, C<IPC::Semaphore> も参照してください。 |
10970 | 10883 | |
10971 | 10884 | =item semget KEY,NSEMS,FLAGS |
10972 | 10885 | X<semget> |
10973 | 10886 | |
10974 | 10887 | =begin original |
10975 | 10888 | |
10976 | 10889 | Calls the System V IPC function semget. Returns the semaphore id, or |
10977 | 10890 | the undefined value if there is an error. See also |
10978 | 10891 | L<perlipc/"SysV IPC">, C<IPC::SysV>, C<IPC::SysV::Semaphore> |
10979 | 10892 | documentation. |
10980 | 10893 | |
10981 | 10894 | =end original |
10982 | 10895 | |
10983 | 10896 | System V IPC 関数 semget を呼び出します。 |
10984 | 10897 | セマフォ ID か、エラー時には未定義値を返します。 |
10985 | 10898 | L<perlipc/"SysV IPC">, C<IPC::SysV>, C<IPC::SysV::Semaphore> も |
10986 | 10899 | 参照してください。 |
10987 | 10900 | |
10988 | 10901 | =item semop KEY,OPSTRING |
10989 | 10902 | X<semop> |
10990 | 10903 | |
10991 | 10904 | =begin original |
10992 | 10905 | |
10993 | 10906 | Calls the System V IPC function semop to perform semaphore operations |
10994 | 10907 | such as signalling and waiting. OPSTRING must be a packed array of |
10995 | 10908 | semop structures. Each semop structure can be generated with |
10996 | 10909 | C<pack("s!3", $semnum, $semop, $semflag)>. The length of OPSTRING |
10997 | 10910 | implies the number of semaphore operations. Returns true if |
10998 | 10911 | successful, or false if there is an error. As an example, the |
10999 | 10912 | following code waits on semaphore $semnum of semaphore id $semid: |
11000 | 10913 | |
11001 | 10914 | =end original |
11002 | 10915 | |
11003 | 10916 | シグナルを送信や、待ち合わせなどのセマフォ操作を行なうために、 |
11004 | 10917 | System V IPC 関数 semop を呼び出します。 |
11005 | 10918 | OPSTRING は、semop 構造体の pack された配列でなければなりません。 |
11006 | 10919 | semop 構造体は、それぞれ、 |
11007 | 10920 | C<pack("s!3", $semnum, $semop, $semflag)> のように作ることができます。 |
11008 | 10921 | セマフォ操作の数は、OPSTRING の長さからわかります。 |
11009 | 10922 | 成功時には真を、エラー時には偽を返します。 |
11010 | 10923 | 以下の例は、セマフォ ID $semid のセマフォ $semnum で |
11011 | 10924 | 待ち合わせを行ないます。 |
11012 | 10925 | |
11013 | 10926 | $semop = pack("s!3", $semnum, -1, 0); |
11014 | 10927 | die "Semaphore trouble: $!\n" unless semop($semid, $semop); |
11015 | 10928 | |
11016 | 10929 | =begin original |
11017 | 10930 | |
11018 | 10931 | To signal the semaphore, replace C<-1> with C<1>. See also |
11019 | 10932 | L<perlipc/"SysV IPC">, C<IPC::SysV>, and C<IPC::SysV::Semaphore> |
11020 | 10933 | documentation. |
11021 | 10934 | |
11022 | 10935 | =end original |
11023 | 10936 | |
11024 | 10937 | セマフォにシグナルを送るには、C<-1> を C<1> に変更してください。 |
11025 | 10938 | L<perlipc/"SysV IPC">, C<IPC::SysV>, C<IPC::SysV::Semaphore> も |
11026 | 10939 | 参照してください。 |
11027 | 10940 | |
11028 | 10941 | =item send SOCKET,MSG,FLAGS,TO |
11029 | 10942 | X<send> |
11030 | 10943 | |
11031 | 10944 | =item send SOCKET,MSG,FLAGS |
11032 | 10945 | |
11033 | 10946 | =begin original |
11034 | 10947 | |
11035 | 10948 | Sends a message on a socket. Attempts to send the scalar MSG to the |
11036 | 10949 | SOCKET filehandle. Takes the same flags as the system call of the |
11037 | 10950 | same name. On unconnected sockets you must specify a destination to |
11038 | 10951 | send TO, in which case it does a C C<sendto>. Returns the number of |
11039 | 10952 | characters sent, or the undefined value if there is an error. The C |
11040 | 10953 | system call sendmsg(2) is currently unimplemented. See |
11041 | 10954 | L<perlipc/"UDP: Message Passing"> for examples. |
11042 | 10955 | |
11043 | 10956 | =end original |
11044 | 10957 | |
11045 | 10958 | ソケットにメッセージを送ります。 |
11046 | 10959 | スカラ MSG を ファイルハンドル SOCKET に送ろうとします。 |
11047 | 10960 | 同名のシステムコールと同じフラグが指定できます。 |
11048 | 10961 | 接続していないソケットには、送信先 TO を指定しなければならず、 |
11049 | 10962 | この場合、C の C<sendto> を実行します。 |
11050 | 10963 | 送信した文字数か、エラー時には、未定義値を返します。 |
11051 | 10964 | C の システムコール sendmsg(2) は現在実装されていません。 |
11052 | 10965 | 例については L<perlipc/"UDP: Message Passing"> を参照してください。 |
11053 | 10966 | |
11054 | 10967 | =begin original |
11055 | 10968 | |
11056 | 10969 | Note the I<characters>: depending on the status of the socket, either |
11057 | 10970 | (8-bit) bytes or characters are sent. By default all sockets operate |
11058 | 10971 | on bytes, but for example if the socket has been changed using |
11059 | 10972 | binmode() to operate with the C<:encoding(utf8)> I/O layer (see |
11060 | 10973 | L</open>, or the C<open> pragma, L<open>), the I/O will operate on UTF-8 |
11061 | 10974 | encoded Unicode characters, not bytes. Similarly for the C<:encoding> |
11062 | 10975 | pragma: in that case pretty much any characters can be sent. |
11063 | 10976 | |
11064 | 10977 | =end original |
11065 | 10978 | |
11066 | 10979 | I<文字> に関する注意: ソケットの状態によって、(8 ビットの) バイトか |
11067 | 10980 | 文字を送信します。 |
11068 | 10981 | デフォルトでは全てのソケットはバイトを処理しますが、 |
11069 | 10982 | 例えばソケットが binmode() で C<:encoding(utf8)> I/O 層(L</open>、 |
11070 | 10983 | C<open> プラグマ、L<open> を参照してください) を使うように指定された場合、 |
11071 | 10984 | I/O はバイトではなく、UTF-8 エンコードされた Unicode 文字を操作します。 |
11072 | 10985 | C<:encoding> プラグマも同様です: |
11073 | 10986 | この場合、ほとんど大体全ての文字が書き込めます。 |
11074 | 10987 | |
11075 | 10988 | =item setpgrp PID,PGRP |
11076 | 10989 | X<setpgrp> X<group> |
11077 | 10990 | |
11078 | 10991 | =begin original |
11079 | 10992 | |
11080 | 10993 | Sets the current process group for the specified PID, C<0> for the current |
11081 | 10994 | process. Will produce a fatal error if used on a machine that doesn't |
11082 | 10995 | implement POSIX setpgid(2) or BSD setpgrp(2). If the arguments are omitted, |
11083 | 10996 | it defaults to C<0,0>. Note that the BSD 4.2 version of C<setpgrp> does not |
11084 | 10997 | accept any arguments, so only C<setpgrp(0,0)> is portable. See also |
11085 | 10998 | C<POSIX::setsid()>. |
11086 | 10999 | |
11087 | 11000 | =end original |
11088 | 11001 | |
11089 | 11002 | 指定した PID (C<0> を指定するとカレントプロセス) に |
11090 | 11003 | 対するプロセスグループを設定します。 |
11091 | 11004 | POSIX setpgrp(2) または BSD setpgrp(2) が実装されていないマシンでは、 |
11092 | 11005 | 致命的エラーが発生します。 |
11093 | 11006 | 引数が省略された場合は、C<0,0>が使われます。 |
11094 | 11007 | BSD 4.2 版の C<setpgrp> は引数を取ることができないので、 |
11095 | 11008 | C<setpgrp(0,0)> のみが移植性があることに注意してください。 |
11096 | 11009 | C<POSIX::setsid()> も参照してください。 |
11097 | 11010 | |
11098 | 11011 | =item setpriority WHICH,WHO,PRIORITY |
11099 | 11012 | X<setpriority> X<priority> X<nice> X<renice> |
11100 | 11013 | |
11101 | 11014 | =begin original |
11102 | 11015 | |
11103 | 11016 | Sets the current priority for a process, a process group, or a user. |
11104 | 11017 | (See setpriority(2).) Will produce a fatal error if used on a machine |
11105 | 11018 | that doesn't implement setpriority(2). |
11106 | 11019 | |
11107 | 11020 | =end original |
11108 | 11021 | |
11109 | 11022 | プロセス、プロセスグループ、ユーザに対する優先順位を設定します。 |
11110 | 11023 | (setpriority(2) を参照してください。) |
11111 | 11024 | setpriority(2) が実装されていないマシンでは、 |
11112 | 11025 | 致命的エラーが発生します。 |
11113 | 11026 | |
11114 | 11027 | =item setsockopt SOCKET,LEVEL,OPTNAME,OPTVAL |
11115 | 11028 | X<setsockopt> |
11116 | 11029 | |
11117 | 11030 | =begin original |
11118 | 11031 | |
11119 | 11032 | Sets the socket option requested. Returns undefined if there is an |
11120 | 11033 | error. Use integer constants provided by the C<Socket> module for |
11121 | 11034 | LEVEL and OPNAME. Values for LEVEL can also be obtained from |
11122 | 11035 | getprotobyname. OPTVAL might either be a packed string or an integer. |
11123 | 11036 | An integer OPTVAL is shorthand for pack("i", OPTVAL). |
11124 | 11037 | |
11125 | 11038 | =end original |
11126 | 11039 | |
11127 | 11040 | 要求したソケットオプションを設定します。 |
11128 | 11041 | エラー時には、未定義値が返されます。 |
11129 | 11042 | LEVEL と OPNAME には C<Socket> モジュールが提供する整数定数を使います。 |
11130 | 11043 | LEVEL の値は getprotobyname から得ることもできます。 |
11131 | 11044 | OPTVAL は pack された文字列か整数です。 |
11132 | 11045 | 整数の OPTVAL は pack("i", OPTVAL) の省略表現です。 |
11133 | 11046 | |
11134 | 11047 | =begin original |
11135 | 11048 | |
11136 | 11049 | An example disabling the Nagle's algorithm for a socket: |
11137 | 11050 | |
11138 | 11051 | =end original |
11139 | 11052 | |
11140 | 11053 | ソケットに対する Nagle のアルゴリズムを無効にする例です: |
11141 | 11054 | |
11142 | 11055 | use Socket qw(IPPROTO_TCP TCP_NODELAY); |
11143 | 11056 | setsockopt($socket, IPPROTO_TCP, TCP_NODELAY, 1); |
11144 | 11057 | |
11145 | 11058 | =item shift ARRAY |
11146 | 11059 | X<shift> |
11147 | 11060 | |
11148 | 11061 | =item shift |
11149 | 11062 | |
11150 | 11063 | =begin original |
11151 | 11064 | |
11152 | 11065 | Shifts the first value of the array off and returns it, shortening the |
11153 | 11066 | array by 1 and moving everything down. If there are no elements in the |
11154 | 11067 | array, returns the undefined value. If ARRAY is omitted, shifts the |
11155 | 11068 | C<@_> array within the lexical scope of subroutines and formats, and the |
11156 | 11069 | C<@ARGV> array outside of a subroutine and also within the lexical scopes |
11157 | 11070 | established by the C<eval STRING>, C<BEGIN {}>, C<INIT {}>, C<CHECK {}>, |
11158 | 11071 | C<UNITCHECK {}> and C<END {}> constructs. |
11159 | 11072 | |
11160 | 11073 | =end original |
11161 | 11074 | |
11162 | 11075 | 配列の最初の値を取り出して、その値を返し、配列を一つ |
11163 | 11076 | 短くして、すべての要素を前へずらします。 |
11164 | 11077 | 配列に要素がなければ、未定義値を返します。 |
11165 | 11078 | ARRAY を省略すると、 |
11166 | 11079 | サブルーチンやフォーマットのレキシカルスコープでは C<@_> を、 |
11167 | 11080 | サブルーチンの外側で、C<eval STRING>, C<BEGIN {}>, C<INIT {}>, C<CHECK {}>, |
11168 | 11081 | C<UNITCHECK {}>, C<END {}> で作成されたレキシカルスコープでは |
11169 | 11082 | C<@ARGV> が用いられます。 |
11170 | 11083 | |
11171 | 11084 | =begin original |
11172 | 11085 | |
11173 | 11086 | See also C<unshift>, C<push>, and C<pop>. C<shift> and C<unshift> do the |
11174 | 11087 | same thing to the left end of an array that C<pop> and C<push> do to the |
11175 | 11088 | right end. |
11176 | 11089 | |
11177 | 11090 | =end original |
11178 | 11091 | |
11179 | 11092 | C<unshift>、C<push>、C<pop> も参照してください。 |
11180 | 11093 | C<shift> と C<unshift> は、C<pop> と |
11181 | 11094 | C<push> が配列の右端で行なうことを、左端で行ないます。 |
11182 | 11095 | |
11183 | 11096 | =item shmctl ID,CMD,ARG |
11184 | 11097 | X<shmctl> |
11185 | 11098 | |
11186 | 11099 | =begin original |
11187 | 11100 | |
11188 | 11101 | Calls the System V IPC function shmctl. You'll probably have to say |
11189 | 11102 | |
11190 | 11103 | =end original |
11191 | 11104 | |
11192 | 11105 | System V IPC 関数 shmctl を呼び出します。正しい定数定義を得るために、まず |
11193 | 11106 | |
11194 | 11107 | use IPC::SysV; |
11195 | 11108 | |
11196 | 11109 | =begin original |
11197 | 11110 | |
11198 | 11111 | first to get the correct constant definitions. If CMD is C<IPC_STAT>, |
11199 | 11112 | then ARG must be a variable that will hold the returned C<shmid_ds> |
11200 | 11113 | structure. Returns like ioctl: the undefined value for error, "C<0> but |
11201 | 11114 | true" for zero, or the actual return value otherwise. |
11202 | 11115 | See also L<perlipc/"SysV IPC"> and C<IPC::SysV> documentation. |
11203 | 11116 | |
11204 | 11117 | =end original |
11205 | 11118 | |
11206 | 11119 | と宣言する必要があるでしょう。 |
11207 | 11120 | CMD が、C<IPC_STAT> ならば、ARG は、返される C<shmid_ds> 構造体を |
11208 | 11121 | 納める変数でなければなりません。 |
11209 | 11122 | ioctl と同じように、エラー時には未定義値、ゼロのときは "C<0> だが真"、 |
11210 | 11123 | それ以外なら、その値そのものを返します。 |
11211 | 11124 | L<perlipc/"SysV IPC"> と C<IPC::SysV> も参照してください。 |
11212 | 11125 | |
11213 | 11126 | =item shmget KEY,SIZE,FLAGS |
11214 | 11127 | X<shmget> |
11215 | 11128 | |
11216 | 11129 | =begin original |
11217 | 11130 | |
11218 | 11131 | Calls the System V IPC function shmget. Returns the shared memory |
11219 | 11132 | segment id, or the undefined value if there is an error. |
11220 | 11133 | See also L<perlipc/"SysV IPC"> and C<IPC::SysV> documentation. |
11221 | 11134 | |
11222 | 11135 | =end original |
11223 | 11136 | |
11224 | 11137 | System V IPC 関数 shmget を呼び出します。 |
11225 | 11138 | 共有メモリのセグメント ID か、エラー時には未定義値を返します。 |
11226 | 11139 | L<perlipc/"SysV IPC"> と C<IPC::SysV> も参照してください。 |
11227 | 11140 | |
11228 | 11141 | =item shmread ID,VAR,POS,SIZE |
11229 | 11142 | X<shmread> |
11230 | 11143 | X<shmwrite> |
11231 | 11144 | |
11232 | 11145 | =item shmwrite ID,STRING,POS,SIZE |
11233 | 11146 | |
11234 | 11147 | =begin original |
11235 | 11148 | |
11236 | 11149 | Reads or writes the System V shared memory segment ID starting at |
11237 | 11150 | position POS for size SIZE by attaching to it, copying in/out, and |
11238 | 11151 | detaching from it. When reading, VAR must be a variable that will |
11239 | 11152 | hold the data read. When writing, if STRING is too long, only SIZE |
11240 | 11153 | bytes are used; if STRING is too short, nulls are written to fill out |
11241 | 11154 | SIZE bytes. Return true if successful, or false if there is an error. |
11242 | 11155 | shmread() taints the variable. See also L<perlipc/"SysV IPC">, |
11243 | 11156 | C<IPC::SysV> documentation, and the C<IPC::Shareable> module from CPAN. |
11244 | 11157 | |
11245 | 11158 | =end original |
11246 | 11159 | |
11247 | 11160 | System V 共有メモリセグメント ID に対し、アタッチして、 |
11248 | 11161 | コピーを行ない、デタッチするという形で、位置 POS から、 |
11249 | 11162 | サイズ SIZE だけ、読み込みか書き込みを行ないます。 |
11250 | 11163 | 読み込み時には、VAR は読み込んだデータを納める |
11251 | 11164 | 変数でなければなりません。 |
11252 | 11165 | 書き込み時には、STRING が長すぎても、SIZE バイトだけが使われます。 |
11253 | 11166 | STRING が短すぎる場合には、SIZE バイトを埋めるために、 |
11254 | 11167 | ヌル文字が書き込まれます。 |
11255 | 11168 | 成功時には真を、エラー時には偽を返します。 |
11256 | 11169 | shmread() は変数を汚染します。 |
11257 | 11170 | L<perlipc/"SysV IPC"> と C<IPC::SysV> の文章と、 |
11258 | 11171 | CPAN の C<IPC::Shareable> も参照してください。 |
11259 | 11172 | |
11260 | 11173 | =item shutdown SOCKET,HOW |
11261 | 11174 | X<shutdown> |
11262 | 11175 | |
11263 | 11176 | =begin original |
11264 | 11177 | |
11265 | 11178 | Shuts down a socket connection in the manner indicated by HOW, which |
11266 | 11179 | has the same interpretation as in the system call of the same name. |
11267 | 11180 | |
11268 | 11181 | =end original |
11269 | 11182 | |
11270 | 11183 | 同名のシステムコールと同じように解釈される HOW によって、 |
11271 | 11184 | 指定された方法でソケット接続のシャットダウンを行ないます。 |
11272 | 11185 | |
11273 | 11186 | shutdown(SOCKET, 0); # I/we have stopped reading data |
11274 | 11187 | shutdown(SOCKET, 1); # I/we have stopped writing data |
11275 | 11188 | shutdown(SOCKET, 2); # I/we have stopped using this socket |
11276 | 11189 | |
11277 | 11190 | =begin original |
11278 | 11191 | |
11279 | 11192 | This is useful with sockets when you want to tell the other |
11280 | 11193 | side you're done writing but not done reading, or vice versa. |
11281 | 11194 | It's also a more insistent form of close because it also |
11282 | 11195 | disables the file descriptor in any forked copies in other |
11283 | 11196 | processes. |
11284 | 11197 | |
11285 | 11198 | =end original |
11286 | 11199 | |
11287 | 11200 | これは、こちらがソケットを書き終わったが読み終わっていない、 |
11288 | 11201 | またはその逆を相手側に伝えたいときに便利です。 |
11289 | 11202 | これはその他のプロセスでフォークしたファイル記述子のコピーも |
11290 | 11203 | 無効にするので、よりしつこい閉じ方です。 |
11291 | 11204 | |
11292 | =begin original | |
11293 | ||
11294 | Returns C<1> for success. In the case of error, returns C<undef> if | |
11295 | the first argument is not a valid filehandle, or returns C<0> and sets | |
11296 | C<$!> for any other failure. | |
11297 | ||
11298 | =end original | |
11299 | ||
11300 | 成功時には C<1> を返します。 | |
11301 | エラーの場合、最初の引数が有効なファイルハンドルでない場合は C<undef> を | |
11302 | 返し、その他のエラーの場合は C<0> を返してC<$!> をセットします。 | |
11303 | ||
11304 | 11205 | =item sin EXPR |
11305 | 11206 | X<sin> X<sine> X<asin> X<arcsine> |
11306 | 11207 | |
11307 | 11208 | =item sin |
11308 | 11209 | |
11309 | 11210 | =begin original |
11310 | 11211 | |
11311 | 11212 | Returns the sine of EXPR (expressed in radians). If EXPR is omitted, |
11312 | 11213 | returns sine of C<$_>. |
11313 | 11214 | |
11314 | 11215 | =end original |
11315 | 11216 | |
11316 | 11217 | (ラジアンで示した) EXPR の正弦を返します。 |
11317 | 11218 | EXPR が省略されたときには、C<$_> の正弦を返します。 |
11318 | 11219 | |
11319 | 11220 | =begin original |
11320 | 11221 | |
11321 | 11222 | For the inverse sine operation, you may use the C<Math::Trig::asin> |
11322 | 11223 | function, or use this relation: |
11323 | 11224 | |
11324 | 11225 | =end original |
11325 | 11226 | |
11326 | 11227 | 逆正弦を求めるためには、C<Math::Trig::asin> 関数を使うか、 |
11327 | 11228 | 以下の関係を使ってください: |
11328 | 11229 | |
11329 | 11230 | sub asin { atan2($_[0], sqrt(1 - $_[0] * $_[0])) } |
11330 | 11231 | |
11331 | 11232 | =item sleep EXPR |
11332 | 11233 | X<sleep> X<pause> |
11333 | 11234 | |
11334 | 11235 | =item sleep |
11335 | 11236 | |
11336 | 11237 | =begin original |
11337 | 11238 | |
11338 | 11239 | Causes the script to sleep for EXPR seconds, or forever if no EXPR. |
11339 | ||
11240 | May be interrupted if the process receives a signal such as C<SIGALRM>. | |
11241 | Returns the number of seconds actually slept. You probably cannot | |
11242 | mix C<alarm> and C<sleep> calls, because C<sleep> is often implemented | |
11243 | using C<alarm>. | |
11340 | 11244 | |
11341 | 11245 | =end original |
11342 | 11246 | |
11343 | 11247 | スクリプトを EXPR で指定した秒数 (省略時には、永久に) |
11344 | 11248 | スリープさせます。 |
11345 | ||
11249 | そのプロセスには、C<SIGALRM>のようなシグナルを | |
11250 | 受信すると、割り込みがかかります。 | |
11251 | 実際にスリープした秒数を返します。C<sleep> は、C<alarm> を | |
11252 | 使って実装されることが多いので、C<alarm> と | |
11253 | C<sleep> は、おそらく混ぜて使用することはできません。 | |
11346 | 11254 | |
11347 | 11255 | =begin original |
11348 | 11256 | |
11349 | May be interrupted if the process receives a signal such as C<SIGALRM>. | |
11350 | ||
11351 | =end original | |
11352 | ||
11353 | そのプロセスが C<SIGALRM>のようなシグナルを受信すると、 | |
11354 | 割り込みがかかります。 | |
11355 | ||
11356 | eval { | |
11357 | local $SIG{ALARM} = sub { die "Alarm!\n" }; | |
11358 | sleep; | |
11359 | }; | |
11360 | die $@ unless $@ eq "Alarm!\n"; | |
11361 | ||
11362 | =begin original | |
11363 | ||
11364 | You probably cannot mix C<alarm> and C<sleep> calls, because C<sleep> | |
11365 | is often implemented using C<alarm>. | |
11366 | ||
11367 | =end original | |
11368 | ||
11369 | C<sleep> は、C<alarm> を使って実装されることが多いので、C<alarm> と | |
11370 | C<sleep> は、混ぜて使用することはおそらくできません。 | |
11371 | ||
11372 | =begin original | |
11373 | ||
11374 | 11257 | On some older systems, it may sleep up to a full second less than what |
11375 | 11258 | you requested, depending on how it counts seconds. Most modern systems |
11376 | 11259 | always sleep the full amount. They may appear to sleep longer than that, |
11377 | 11260 | however, because your process might not be scheduled right away in a |
11378 | 11261 | busy multitasking system. |
11379 | 11262 | |
11380 | 11263 | =end original |
11381 | 11264 | |
11382 | 11265 | 古いシステムでは、どのように秒を数えるかによって、要求した秒数に完全に |
11383 | 11266 | 満たないうちに、スリープから抜ける場合があります。 |
11384 | 11267 | 最近のシステムでは、常に完全にスリープします。 |
11385 | 11268 | しかし、負荷の高いマルチタスクシステムでは |
11386 | 11269 | 正しくスケジューリングされないがために |
11387 | 11270 | より長い時間スリープすることがあります。 |
11388 | 11271 | |
11389 | 11272 | =begin original |
11390 | 11273 | |
11391 | 11274 | For delays of finer granularity than one second, the Time::HiRes module |
11392 | 11275 | (from CPAN, and starting from Perl 5.8 part of the standard |
11393 | 11276 | distribution) provides usleep(). You may also use Perl's four-argument |
11394 | 11277 | version of select() leaving the first three arguments undefined, or you |
11395 | 11278 | might be able to use the C<syscall> interface to access setitimer(2) if |
11396 | 11279 | your system supports it. See L<perlfaq8> for details. |
11397 | 11280 | |
11398 | 11281 | =end original |
11399 | 11282 | |
11400 | 11283 | 1 秒より精度の高いスリープを行なうには、 |
11401 | 11284 | Time::HiRes モジュール(CPAN から、また Perl 5.8 からは |
11402 | 11285 | 標準配布されています) が usleep() を提供します。 |
11403 | 11286 | Perl の 4 引数版 select() を最初の 3 引数を未定義にして使うか、 |
11404 | 11287 | setitimer(2) をサポートしているシステムでは、Perl の |
11405 | 11288 | C<syscall> インタフェースを使ってアクセスすることもできます。 |
11406 | 11289 | 詳しくは L<perlfaq8> を参照してください。 |
11407 | 11290 | |
11408 | 11291 | =begin original |
11409 | 11292 | |
11410 | 11293 | See also the POSIX module's C<pause> function. |
11411 | 11294 | |
11412 | 11295 | =end original |
11413 | 11296 | |
11414 | 11297 | POSIX モジュールの C<pause> 関数も参照して下さい。 |
11415 | 11298 | |
11416 | 11299 | =item socket SOCKET,DOMAIN,TYPE,PROTOCOL |
11417 | 11300 | X<socket> |
11418 | 11301 | |
11419 | 11302 | =begin original |
11420 | 11303 | |
11421 | 11304 | Opens a socket of the specified kind and attaches it to filehandle |
11422 | 11305 | SOCKET. DOMAIN, TYPE, and PROTOCOL are specified the same as for |
11423 | 11306 | the system call of the same name. You should C<use Socket> first |
11424 | 11307 | to get the proper definitions imported. See the examples in |
11425 | 11308 | L<perlipc/"Sockets: Client/Server Communication">. |
11426 | 11309 | |
11427 | 11310 | =end original |
11428 | 11311 | |
11429 | 11312 | 指定した種類のソケットをオープンし、ファイルハンドル |
11430 | 11313 | SOCKET にアタッチします。 |
11431 | 11314 | DOMAIN、TYPE、PROTOCOL は、 |
11432 | 11315 | 同名のシステムコールと同じように指定します。 |
11433 | 11316 | 適切な定義を import するために、まず、C<use Socket> と |
11434 | 11317 | するとよいでしょう。 |
11435 | 11318 | 例については L<perlipc/"Sockets: Client/Server Communication"> を |
11436 | 11319 | 参照してください。 |
11437 | 11320 | |
11438 | 11321 | =begin original |
11439 | 11322 | |
11440 | 11323 | On systems that support a close-on-exec flag on files, the flag will |
11441 | 11324 | be set for the newly opened file descriptor, as determined by the |
11442 | 11325 | value of $^F. See L<perlvar/$^F>. |
11443 | 11326 | |
11444 | 11327 | =end original |
11445 | 11328 | |
11446 | 11329 | ファイルに対する close-on-exec フラグをサポートしているシステムでは、 |
11447 | 11330 | フラグは $^F の値で決定される、新しくオープンされたファイル記述子に対して |
11448 | 11331 | セットされます。 |
11449 | 11332 | L<perlvar/$^F> を参照してください。 |
11450 | 11333 | |
11451 | 11334 | =item socketpair SOCKET1,SOCKET2,DOMAIN,TYPE,PROTOCOL |
11452 | 11335 | X<socketpair> |
11453 | 11336 | |
11454 | 11337 | =begin original |
11455 | 11338 | |
11456 | 11339 | Creates an unnamed pair of sockets in the specified domain, of the |
11457 | 11340 | specified type. DOMAIN, TYPE, and PROTOCOL are specified the same as |
11458 | 11341 | for the system call of the same name. If unimplemented, yields a fatal |
11459 | 11342 | error. Returns true if successful. |
11460 | 11343 | |
11461 | 11344 | =end original |
11462 | 11345 | |
11463 | 11346 | 指定した DOMAIN に、指定した TYPE で名前の無いソケットのペアを生成します。 |
11464 | 11347 | DOMAIN、TYPE、PROTOCOL は、同名のシステムコールと同じように指定します。 |
11465 | 11348 | 実装されていない場合には、致命的エラーとなります。 |
11466 | 11349 | 成功時には真を返します。 |
11467 | 11350 | |
11468 | 11351 | =begin original |
11469 | 11352 | |
11470 | 11353 | On systems that support a close-on-exec flag on files, the flag will |
11471 | 11354 | be set for the newly opened file descriptors, as determined by the value |
11472 | 11355 | of $^F. See L<perlvar/$^F>. |
11473 | 11356 | |
11474 | 11357 | =end original |
11475 | 11358 | |
11476 | 11359 | ファイルに対する close-on-exec フラグをサポートしているシステムでは、 |
11477 | 11360 | フラグは $^F の値で決定される、新しくオープンされたファイル記述子に対して |
11478 | 11361 | セットされます。 |
11479 | 11362 | L<perlvar/$^F> を参照してください。 |
11480 | 11363 | |
11481 | 11364 | =begin original |
11482 | 11365 | |
11483 | 11366 | Some systems defined C<pipe> in terms of C<socketpair>, in which a call |
11484 | 11367 | to C<pipe(Rdr, Wtr)> is essentially: |
11485 | 11368 | |
11486 | 11369 | =end original |
11487 | 11370 | |
11488 | 11371 | C<pipe> を C<socketpair> を使って定義しているシステムもあります; |
11489 | 11372 | C<pipe(Rdr, Wtr)> は本質的には以下のようになります: |
11490 | 11373 | |
11491 | 11374 | use Socket; |
11492 | 11375 | socketpair(Rdr, Wtr, AF_UNIX, SOCK_STREAM, PF_UNSPEC); |
11493 | 11376 | shutdown(Rdr, 1); # no more writing for reader |
11494 | 11377 | shutdown(Wtr, 0); # no more reading for writer |
11495 | 11378 | |
11496 | 11379 | =begin original |
11497 | 11380 | |
11498 | 11381 | See L<perlipc> for an example of socketpair use. Perl 5.8 and later will |
11499 | 11382 | emulate socketpair using IP sockets to localhost if your system implements |
11500 | 11383 | sockets but not socketpair. |
11501 | 11384 | |
11502 | 11385 | =end original |
11503 | 11386 | |
11504 | 11387 | socketpair の使用例については L<perlipc> を参照してください。 |
11505 | 11388 | Perl 5.8 以降では、システムがソケットを実装しているが socketpair を |
11506 | 11389 | 実装していない場合、localhost に対して IP ソケットを使うことで |
11507 | 11390 | socketpair をエミュレートします。 |
11508 | 11391 | |
11509 | 11392 | =item sort SUBNAME LIST |
11510 | 11393 | X<sort> X<qsort> X<quicksort> X<mergesort> |
11511 | 11394 | |
11512 | 11395 | =item sort BLOCK LIST |
11513 | 11396 | |
11514 | 11397 | =item sort LIST |
11515 | 11398 | |
11516 | 11399 | =begin original |
11517 | 11400 | |
11518 | 11401 | In list context, this sorts the LIST and returns the sorted list value. |
11519 | 11402 | In scalar context, the behaviour of C<sort()> is undefined. |
11520 | 11403 | |
11521 | 11404 | =end original |
11522 | 11405 | |
11523 | 11406 | リストコンテキストでは、LIST をソートし、ソートされたリスト値を返します。 |
11524 | 11407 | スカラコンテキストでは、C<sort()> の振る舞いは未定義です。 |
11525 | 11408 | |
11526 | 11409 | =begin original |
11527 | 11410 | |
11528 | 11411 | If SUBNAME or BLOCK is omitted, C<sort>s in standard string comparison |
11529 | 11412 | order. If SUBNAME is specified, it gives the name of a subroutine |
11530 | 11413 | that returns an integer less than, equal to, or greater than C<0>, |
11531 | 11414 | depending on how the elements of the list are to be ordered. (The C<< |
11532 | 11415 | <=> >> and C<cmp> operators are extremely useful in such routines.) |
11533 | 11416 | SUBNAME may be a scalar variable name (unsubscripted), in which case |
11534 | 11417 | the value provides the name of (or a reference to) the actual |
11535 | 11418 | subroutine to use. In place of a SUBNAME, you can provide a BLOCK as |
11536 | 11419 | an anonymous, in-line sort subroutine. |
11537 | 11420 | |
11538 | 11421 | =end original |
11539 | 11422 | |
11540 | 11423 | SUBNAME や BLOCK を省略すると、標準の文字列比較の順番でソートが |
11541 | 11424 | 行なわれます。 |
11542 | 11425 | SUBNAME を指定すると、それは、リストの要素をどのような順番に並べるかに |
11543 | 11426 | 応じて、負、ゼロ、正の整数を返すサブルーチンの名前であると解釈されます。 |
11544 | 11427 | (このようなルーチンには、C<< <=> >> 演算子や cmp 演算子が、 |
11545 | 11428 | たいへん便利です。) |
11546 | 11429 | SUBNAME は、スカラ変数名(添字なし)でもよく、 |
11547 | 11430 | その場合には、その値が使用する実際のサブルーチンの |
11548 | 11431 | 名前(またはそのリファレンス)と解釈されます。 |
11549 | 11432 | SUBNAME の代わりに、無名のインライン |
11550 | 11433 | ソートルーチンとして、BLOCK を書くことができます。 |
11551 | 11434 | |
11552 | 11435 | =begin original |
11553 | 11436 | |
11554 | 11437 | If the subroutine's prototype is C<($$)>, the elements to be compared |
11555 | 11438 | are passed by reference in C<@_>, as for a normal subroutine. This is |
11556 | 11439 | slower than unprototyped subroutines, where the elements to be |
11557 | 11440 | compared are passed into the subroutine |
11558 | 11441 | as the package global variables $a and $b (see example below). Note that |
11559 | 11442 | in the latter case, it is usually counter-productive to declare $a and |
11560 | 11443 | $b as lexicals. |
11561 | 11444 | |
11562 | 11445 | =end original |
11563 | 11446 | |
11564 | 11447 | サブルーチンのプロトタイプが C<($$)>の場合、 |
11565 | 11448 | 比較する要素は通常のサブルーチンと同じように C<@_> の中に |
11566 | 11449 | リファレンスとして渡されます。 |
11567 | 11450 | これはプロトタイプなしのサブルーチンより遅いです。 |
11568 | 11451 | この場合は比較のためサブルーチンに渡される 2 つの |
11569 | 11452 | 要素は、パッケージのグローバル変数 $a と $b で渡されます |
11570 | 11453 | (次の例を参照してください)。 |
11571 | 11454 | 後者の場合、レキシカルに $a と $b を宣言するのは普通逆効果になります。 |
11572 | 11455 | |
11573 | 11456 | =begin original |
11574 | 11457 | |
11575 | 11458 | The values to be compared are always passed by reference and should not |
11576 | 11459 | be modified. |
11577 | 11460 | |
11578 | 11461 | =end original |
11579 | 11462 | |
11580 | 11463 | $a や $b はリファレンスによって渡されるので、変更するべきではありません。 |
11581 | 11464 | |
11582 | 11465 | =begin original |
11583 | 11466 | |
11584 | 11467 | You also cannot exit out of the sort block or subroutine using any of the |
11585 | 11468 | loop control operators described in L<perlsyn> or with C<goto>. |
11586 | 11469 | |
11587 | 11470 | =end original |
11588 | 11471 | |
11589 | 11472 | また、ソートブロックやサブルーチンから L<perlsyn> で説明されている |
11590 | 11473 | ループ制御子や C<goto> を使って抜けてはいけません。 |
11591 | 11474 | |
11592 | 11475 | =begin original |
11593 | 11476 | |
11594 | 11477 | When C<use locale> is in effect, C<sort LIST> sorts LIST according to the |
11595 | 11478 | current collation locale. See L<perllocale>. |
11596 | 11479 | |
11597 | 11480 | =end original |
11598 | 11481 | |
11599 | 11482 | C<use locale> が有効の場合、C<sort LIST> は LIST を現在の比較ロケールに |
11600 | 11483 | 従ってソートします。L<perllocale> を参照して下さい。 |
11601 | 11484 | |
11602 | 11485 | =begin original |
11603 | 11486 | |
11604 | 11487 | sort() returns aliases into the original list, much as a for loop's index |
11605 | 11488 | variable aliases the list elements. That is, modifying an element of a |
11606 | 11489 | list returned by sort() (for example, in a C<foreach>, C<map> or C<grep>) |
11607 | 11490 | actually modifies the element in the original list. This is usually |
11608 | 11491 | something to be avoided when writing clear code. |
11609 | 11492 | |
11610 | 11493 | =end original |
11611 | 11494 | |
11612 | 11495 | sort() は元のリストへのエイリアスを返します; for ループのインデックス変数が |
11613 | 11496 | リスト要素へのエイリアスと同様です。 |
11614 | 11497 | つまり、sort() で返されるリストの要素を(例えば、C<foreach> や C<map> や |
11615 | 11498 | C<grep> で)変更すると、実際に元のリストの要素が変更されます。 |
11616 | 11499 | これはきれいなコードを書くことで普通は回避できます。 |
11617 | 11500 | |
11618 | 11501 | =begin original |
11619 | 11502 | |
11620 | 11503 | Perl 5.6 and earlier used a quicksort algorithm to implement sort. |
11621 | 11504 | That algorithm was not stable, and I<could> go quadratic. (A I<stable> sort |
11622 | 11505 | preserves the input order of elements that compare equal. Although |
11623 | 11506 | quicksort's run time is O(NlogN) when averaged over all arrays of |
11624 | 11507 | length N, the time can be O(N**2), I<quadratic> behavior, for some |
11625 | 11508 | inputs.) In 5.7, the quicksort implementation was replaced with |
11626 | 11509 | a stable mergesort algorithm whose worst-case behavior is O(NlogN). |
11627 | 11510 | But benchmarks indicated that for some inputs, on some platforms, |
11628 | 11511 | the original quicksort was faster. 5.8 has a sort pragma for |
11629 | 11512 | limited control of the sort. Its rather blunt control of the |
11630 | 11513 | underlying algorithm may not persist into future Perls, but the |
11631 | 11514 | ability to characterize the input or output in implementation |
11632 | independent ways quite probably will. See L< | |
11515 | independent ways quite probably will. See L<sort>. | |
11633 | 11516 | |
11634 | 11517 | =end original |
11635 | 11518 | |
11636 | 11519 | Perl 5.6 以前ではソートの実装にクイックソートアルゴリズムを使っていました。 |
11637 | 11520 | このアルゴリズムは安定せず、2 乗の時間が掛かる I<可能性があります> 。 |
11638 | 11521 | (I<安定した> ソートは、比較した時に同じ要素の入力順が保存されます。 |
11639 | 11522 | クイックソートの実行時間は、長さ N の全ての配列の平均では |
11640 | 11523 | O(NlogN) ですが、入力によっては O(N**2) という I<2 乗の> 振る舞いを |
11641 | 11524 | することがあります。) |
11642 | 11525 | 5.7 では、クイックソートによる実装は、最悪の場合の振る舞いも |
11643 | 11526 | O(NlogN) である、安定したマージソートアルゴリズムに置き換えられました。 |
11644 | 11527 | しかし、入力とプラットフォームによっては、ベンチマークはクイックソートの |
11645 | 11528 | 方が速くなります。 |
11646 | 11529 | 5.8 ではソートを限定的に制御できる sort プラグマがあります。 |
11647 | 11530 | この、アルゴリズムの直接的な制御方法は将来の perl では引き継がれないかも |
11648 | 11531 | しれませんが、実装に依存しない形で入力や出力を性格付ける機能は |
11649 | 11532 | おそらくあります。 |
11650 | L< | |
11533 | L<sort> を参照してください。 | |
11651 | 11534 | |
11652 | 11535 | =begin original |
11653 | 11536 | |
11654 | 11537 | Examples: |
11655 | 11538 | |
11656 | 11539 | =end original |
11657 | 11540 | |
11658 | 11541 | 例: |
11659 | 11542 | |
11660 | 11543 | # sort lexically |
11661 | 11544 | @articles = sort @files; |
11662 | 11545 | |
11663 | 11546 | # same thing, but with explicit sort routine |
11664 | 11547 | @articles = sort {$a cmp $b} @files; |
11665 | 11548 | |
11666 | 11549 | # now case-insensitively |
11667 | 11550 | @articles = sort {uc($a) cmp uc($b)} @files; |
11668 | 11551 | |
11669 | 11552 | # same thing in reversed order |
11670 | 11553 | @articles = sort {$b cmp $a} @files; |
11671 | 11554 | |
11672 | 11555 | # sort numerically ascending |
11673 | 11556 | @articles = sort {$a <=> $b} @files; |
11674 | 11557 | |
11675 | 11558 | # sort numerically descending |
11676 | 11559 | @articles = sort {$b <=> $a} @files; |
11677 | 11560 | |
11678 | 11561 | # this sorts the %age hash by value instead of key |
11679 | 11562 | # using an in-line function |
11680 | 11563 | @eldest = sort { $age{$b} <=> $age{$a} } keys %age; |
11681 | 11564 | |
11682 | 11565 | # sort using explicit subroutine name |
11683 | 11566 | sub byage { |
11684 | 11567 | $age{$a} <=> $age{$b}; # presuming numeric |
11685 | 11568 | } |
11686 | 11569 | @sortedclass = sort byage @class; |
11687 | 11570 | |
11688 | 11571 | sub backwards { $b cmp $a } |
11689 | 11572 | @harry = qw(dog cat x Cain Abel); |
11690 | 11573 | @george = qw(gone chased yz Punished Axed); |
11691 | 11574 | print sort @harry; |
11692 | 11575 | # prints AbelCaincatdogx |
11693 | 11576 | print sort backwards @harry; |
11694 | 11577 | # prints xdogcatCainAbel |
11695 | 11578 | print sort @george, 'to', @harry; |
11696 | 11579 | # prints AbelAxedCainPunishedcatchaseddoggonetoxyz |
11697 | 11580 | |
11698 | 11581 | # inefficiently sort by descending numeric compare using |
11699 | 11582 | # the first integer after the first = sign, or the |
11700 | 11583 | # whole record case-insensitively otherwise |
11701 | 11584 | |
11702 | 11585 | @new = sort { |
11703 | 11586 | ($b =~ /=(\d+)/)[0] <=> ($a =~ /=(\d+)/)[0] |
11704 | 11587 | || |
11705 | 11588 | uc($a) cmp uc($b) |
11706 | 11589 | } @old; |
11707 | 11590 | |
11708 | 11591 | # same thing, but much more efficiently; |
11709 | 11592 | # we'll build auxiliary indices instead |
11710 | 11593 | # for speed |
11711 | 11594 | @nums = @caps = (); |
11712 | 11595 | for (@old) { |
11713 | 11596 | push @nums, /=(\d+)/; |
11714 | 11597 | push @caps, uc($_); |
11715 | 11598 | } |
11716 | 11599 | |
11717 | 11600 | @new = @old[ sort { |
11718 | 11601 | $nums[$b] <=> $nums[$a] |
11719 | 11602 | || |
11720 | 11603 | $caps[$a] cmp $caps[$b] |
11721 | 11604 | } 0..$#old |
11722 | 11605 | ]; |
11723 | 11606 | |
11724 | 11607 | # same thing, but without any temps |
11725 | 11608 | @new = map { $_->[0] } |
11726 | 11609 | sort { $b->[1] <=> $a->[1] |
11727 | 11610 | || |
11728 | 11611 | $a->[2] cmp $b->[2] |
11729 | 11612 | } map { [$_, /=(\d+)/, uc($_)] } @old; |
11730 | 11613 | |
11731 | 11614 | # using a prototype allows you to use any comparison subroutine |
11732 | 11615 | # as a sort subroutine (including other package's subroutines) |
11733 | 11616 | package other; |
11734 | 11617 | sub backwards ($$) { $_[1] cmp $_[0]; } # $a and $b are not set here |
11735 | 11618 | |
11736 | 11619 | package main; |
11737 | 11620 | @new = sort other::backwards @old; |
11738 | 11621 | |
11739 | 11622 | # guarantee stability, regardless of algorithm |
11740 | 11623 | use sort 'stable'; |
11741 | 11624 | @new = sort { substr($a, 3, 5) cmp substr($b, 3, 5) } @old; |
11742 | 11625 | |
11743 | 11626 | # force use of mergesort (not portable outside Perl 5.8) |
11744 | 11627 | use sort '_mergesort'; # note discouraging _ |
11745 | 11628 | @new = sort { substr($a, 3, 5) cmp substr($b, 3, 5) } @old; |
11746 | 11629 | |
11747 | 11630 | =begin original |
11748 | 11631 | |
11749 | Warning: syntactical care is required when sorting the list returned from | |
11750 | a function. If you want to sort the list returned by the function call | |
11751 | C<find_records(@key)>, you can use: | |
11752 | ||
11753 | =end original | |
11754 | ||
11755 | 警告: 関数からかえされたリストをソートするときには文法上の注意が必要です。 | |
11756 | 関数呼び出し C<find_records(@key)> から返されたリストをソートしたい場合、 | |
11757 | 以下のように出来ます: | |
11758 | ||
11759 | @contact = sort { $a cmp $b } find_records @key; | |
11760 | @contact = sort +find_records(@key); | |
11761 | @contact = sort &find_records(@key); | |
11762 | @contact = sort(find_records(@key)); | |
11763 | ||
11764 | =begin original | |
11765 | ||
11766 | If instead you want to sort the array @key with the comparison routine | |
11767 | C<find_records()> then you can use: | |
11768 | ||
11769 | =end original | |
11770 | ||
11771 | 一方、配列 @key を比較ルーチン C<find_records()> でソートしたい場合は、 | |
11772 | 以下のように出来ます: | |
11773 | ||
11774 | @contact = sort { find_records() } @key; | |
11775 | @contact = sort find_records(@key); | |
11776 | @contact = sort(find_records @key); | |
11777 | @contact = sort(find_records (@key)); | |
11778 | ||
11779 | =begin original | |
11780 | ||
11781 | 11632 | If you're using strict, you I<must not> declare $a |
11782 | 11633 | and $b as lexicals. They are package globals. That means |
11783 | ||
11634 | if you're in the C<main> package and type | |
11784 | 11635 | |
11785 | 11636 | =end original |
11786 | 11637 | |
11787 | 11638 | use strict している場合、$a と $b をレキシカルとして |
11788 | 11639 | 宣言しては I<いけません>。 |
11789 | 11640 | これはパッケージグローバルです。 |
11790 | 11641 | つまり、C<main> パッケージで以下のように書いた場合: |
11791 | 11642 | |
11792 | 11643 | @articles = sort {$b <=> $a} @files; |
11793 | 11644 | |
11794 | 11645 | =begin original |
11795 | 11646 | |
11796 | 11647 | then C<$a> and C<$b> are C<$main::a> and C<$main::b> (or C<$::a> and C<$::b>), |
11797 | 11648 | but if you're in the C<FooPack> package, it's the same as typing |
11798 | 11649 | |
11799 | 11650 | =end original |
11800 | 11651 | |
11801 | 11652 | C<$a> と C<$b> は C<$main::a> と C<$main::b> (または C<$::a> と C<$::b>) を |
11802 | 11653 | 意味しますが、C<FooPack> パッケージ内の場合、これは以下と同じになります: |
11803 | 11654 | |
11804 | 11655 | @articles = sort {$FooPack::b <=> $FooPack::a} @files; |
11805 | 11656 | |
11806 | 11657 | =begin original |
11807 | 11658 | |
11808 | 11659 | The comparison function is required to behave. If it returns |
11809 | 11660 | inconsistent results (sometimes saying C<$x[1]> is less than C<$x[2]> and |
11810 | 11661 | sometimes saying the opposite, for example) the results are not |
11811 | 11662 | well-defined. |
11812 | 11663 | |
11813 | 11664 | =end original |
11814 | 11665 | |
11815 | 11666 | 比較関数は一貫した振る舞いをすることが求められます。 |
11816 | 11667 | 一貫しない結果を返す(例えば、あるときは C<$x[1]> が C<$x[2]> より |
11817 | 11668 | 小さいと返し、またあるときは逆を返す)場合、結果は未定義です。 |
11818 | 11669 | |
11819 | 11670 | =begin original |
11820 | 11671 | |
11821 | 11672 | Because C<< <=> >> returns C<undef> when either operand is C<NaN> |
11822 | 11673 | (not-a-number), and because C<sort> will trigger a fatal error unless the |
11823 | 11674 | result of a comparison is defined, when sorting with a comparison function |
11824 | 11675 | like C<< $a <=> $b >>, be careful about lists that might contain a C<NaN>. |
11825 | 11676 | The following example takes advantage of the fact that C<NaN != NaN> to |
11826 | 11677 | eliminate any C<NaN>s from the input. |
11827 | 11678 | |
11828 | 11679 | =end original |
11829 | 11680 | |
11830 | 11681 | C<< <=> >> はどちらかのオペランドが C<NaN> (not-a-number) のときに |
11831 | 11682 | C<undef> を返し、C<sort> は比較の結果が未定義値だと致命的エラーになるので、 |
11832 | 11683 | C<< $a <=> $b >> といった比較関数でソートする場合はリストに C<NaN> が |
11833 | 11684 | 含まれないように注意してください。 |
11834 | 11685 | 以下の例は 入力から C<NaN> を取り除くために C<NaN != NaN> という性質を |
11835 | 11686 | 利用しています。 |
11836 | 11687 | |
11837 | 11688 | @result = sort { $a <=> $b } grep { $_ == $_ } @input; |
11838 | 11689 | |
11839 | 11690 | =item splice ARRAY,OFFSET,LENGTH,LIST |
11840 | 11691 | X<splice> |
11841 | 11692 | |
11842 | 11693 | =item splice ARRAY,OFFSET,LENGTH |
11843 | 11694 | |
11844 | 11695 | =item splice ARRAY,OFFSET |
11845 | 11696 | |
11846 | 11697 | =item splice ARRAY |
11847 | 11698 | |
11848 | 11699 | =begin original |
11849 | 11700 | |
11850 | 11701 | Removes the elements designated by OFFSET and LENGTH from an array, and |
11851 | 11702 | replaces them with the elements of LIST, if any. In list context, |
11852 | 11703 | returns the elements removed from the array. In scalar context, |
11853 | 11704 | returns the last element removed, or C<undef> if no elements are |
11854 | 11705 | removed. The array grows or shrinks as necessary. |
11855 | 11706 | If OFFSET is negative then it starts that far from the end of the array. |
11856 | 11707 | If LENGTH is omitted, removes everything from OFFSET onward. |
11857 | 11708 | If LENGTH is negative, removes the elements from OFFSET onward |
11858 | 11709 | except for -LENGTH elements at the end of the array. |
11859 | 11710 | If both OFFSET and LENGTH are omitted, removes everything. If OFFSET is |
11860 | 11711 | past the end of the array, perl issues a warning, and splices at the |
11861 | 11712 | end of the array. |
11862 | 11713 | |
11863 | 11714 | =end original |
11864 | 11715 | |
11865 | 11716 | ARRAY から OFFSET、LENGTH で指定される要素を取り除き、 |
11866 | 11717 | LIST があれば、それを代わりに挿入します。 |
11867 | 11718 | リストコンテキストでは、配列から取り除かれた要素を返します。 |
11868 | 11719 | スカラコンテキストでは、取り除かれた最後の要素を返します。 |
11869 | 11720 | 要素が取り除かれなかった場合は C<undef> を返します。 |
11870 | 11721 | 配列は、必要に応じて、大きくなったり、小さくなったりします。 |
11871 | 11722 | OFFSET が負の数の場合は、配列の最後からの距離を示します。 |
11872 | 11723 | LENGTH が省略されると、OFFSET 以降のすべての要素を取り除きます。 |
11873 | 11724 | LENGTH が負の数の場合は、OFFSET から前方へ、配列の最後から -LENGTH 要素を |
11874 | 11725 | 除いて取り除きます。 |
11875 | 11726 | OFFSET と LENGTH の両方が省略されると、全ての要素を取り除きます。 |
11876 | 11727 | OFFSET が配列の最後より後ろの場合、perl は警告を出し、配列の最後に対して |
11877 | 11728 | 処理します。 |
11878 | 11729 | |
11879 | 11730 | =begin original |
11880 | 11731 | |
11881 | 11732 | The following equivalences hold (assuming C<< $[ == 0 and $#a >= $i >> ) |
11882 | 11733 | |
11883 | 11734 | =end original |
11884 | 11735 | |
11885 | 11736 | 以下は、(C<< $[ == 0 と $#a >= $i >> と仮定すると) それぞれ、等価です。 |
11886 | 11737 | |
11887 | 11738 | push(@a,$x,$y) splice(@a,@a,0,$x,$y) |
11888 | 11739 | pop(@a) splice(@a,-1) |
11889 | 11740 | shift(@a) splice(@a,0,1) |
11890 | 11741 | unshift(@a,$x,$y) splice(@a,0,0,$x,$y) |
11891 | 11742 | $a[$i] = $y splice(@a,$i,1,$y) |
11892 | 11743 | |
11893 | 11744 | =begin original |
11894 | 11745 | |
11895 | 11746 | Example, assuming array lengths are passed before arrays: |
11896 | 11747 | |
11897 | 11748 | =end original |
11898 | 11749 | |
11899 | 11750 | 次の例では、配列の前に、それぞれの配列の大きさが渡されるものとしています: |
11900 | 11751 | |
11901 | 11752 | sub aeq { # compare two list values |
11902 | 11753 | my(@a) = splice(@_,0,shift); |
11903 | 11754 | my(@b) = splice(@_,0,shift); |
11904 | 11755 | return 0 unless @a == @b; # same len? |
11905 | 11756 | while (@a) { |
11906 | 11757 | return 0 if pop(@a) ne pop(@b); |
11907 | 11758 | } |
11908 | 11759 | return 1; |
11909 | 11760 | } |
11910 | 11761 | if (&aeq($len,@foo[1..$len],0+@bar,@bar)) { ... } |
11911 | 11762 | |
11912 | 11763 | =item split /PATTERN/,EXPR,LIMIT |
11913 | 11764 | X<split> |
11914 | 11765 | |
11915 | 11766 | =item split /PATTERN/,EXPR |
11916 | 11767 | |
11917 | 11768 | =item split /PATTERN/ |
11918 | 11769 | |
11919 | 11770 | =item split |
11920 | 11771 | |
11921 | 11772 | =begin original |
11922 | 11773 | |
11923 | 11774 | Splits the string EXPR into a list of strings and returns that list. By |
11924 | 11775 | default, empty leading fields are preserved, and empty trailing ones are |
11925 | 11776 | deleted. (If all fields are empty, they are considered to be trailing.) |
11926 | 11777 | |
11927 | 11778 | =end original |
11928 | 11779 | |
11929 | 11780 | 文字列 EXPR を文字列のリストに分割して、リストを返します。 |
11930 | 11781 | デフォルトでは、行頭の空白は保存され、末尾の空白は削除されます。 |
11931 | 11782 | (全てのフィールドが空の場合、これらは末尾であるとして扱われます。) |
11932 | 11783 | |
11933 | 11784 | =begin original |
11934 | 11785 | |
11935 | In scalar context, returns the number of fields found | |
11786 | In scalar context, returns the number of fields found and splits into | |
11936 | ||
11787 | the C<@_> array. Use of split in scalar context is deprecated, however, | |
11937 | ||
11788 | because it clobbers your subroutine arguments. | |
11938 | arguments. | |
11939 | 11789 | |
11940 | 11790 | =end original |
11941 | 11791 | |
11942 | スカラコンテキスト | |
11792 | スカラコンテキストの場合には、見つかったフィールドの数を返し、 | |
11943 | ||
11793 | 配列 C<@_> に分割結果を設定します。 | |
11944 | ||
11794 | しかし、スカラコンテキストでの split の使用は推奨されません。 | |
11945 | し | |
11795 | サブルーチンの引数を上書きしてしまうからです。 | |
11946 | 推奨されません; サブルーチンの引数を上書きしてしまうからです。 | |
11947 | 11796 | |
11948 | 11797 | =begin original |
11949 | 11798 | |
11950 | 11799 | If EXPR is omitted, splits the C<$_> string. If PATTERN is also omitted, |
11951 | 11800 | splits on whitespace (after skipping any leading whitespace). Anything |
11952 | 11801 | matching PATTERN is taken to be a delimiter separating the fields. (Note |
11953 | 11802 | that the delimiter may be longer than one character.) |
11954 | 11803 | |
11955 | 11804 | =end original |
11956 | 11805 | |
11957 | 11806 | EXPR を省略すると、文字列 C<$_> を split します。 |
11958 | 11807 | もし、PATTERN も省略すると、 |
11959 | 11808 | (先頭の空白文字をスキップした後) 空白で split します。 |
11960 | 11809 | PATTERN にマッチするものは、フィールドを分割するデリミタとして扱われます。 |
11961 | 11810 | (デリミタは、1 文字とは限りません。) |
11962 | 11811 | |
11963 | 11812 | =begin original |
11964 | 11813 | |
11965 | 11814 | If LIMIT is specified and positive, it represents the maximum number |
11966 | 11815 | of fields the EXPR will be split into, though the actual number of |
11967 | 11816 | fields returned depends on the number of times PATTERN matches within |
11968 | 11817 | EXPR. If LIMIT is unspecified or zero, trailing null fields are |
11969 | 11818 | stripped (which potential users of C<pop> would do well to remember). |
11970 | 11819 | If LIMIT is negative, it is treated as if an arbitrarily large LIMIT |
11971 | 11820 | had been specified. Note that splitting an EXPR that evaluates to the |
11972 | 11821 | empty string always returns the empty list, regardless of the LIMIT |
11973 | 11822 | specified. |
11974 | 11823 | |
11975 | 11824 | =end original |
11976 | 11825 | |
11977 | 11826 | 正の数の LIMIT を指定した場合には、EXPR が分割されるフィールドの最大数を |
11978 | 11827 | 表しますが、実際に返されるフィールドの数は EXPR の中で何回 PATTERN が |
11979 | 11828 | マッチするかに依存します。 |
11980 | 11829 | LIMIT を指定しないかゼロなら、末尾の空フィールドを捨ててしまいます |
11981 | 11830 | (C<pop> を行なうときには気を付けないといけません)。 |
11982 | 11831 | LIMIT が負ならば、LIMIT に任意の大きな数を指定したのと同じことになります。 |
11983 | 11832 | 空文字列に評価される EXPR を分割する場合、LIMIT での指定に関わらず |
11984 | 11833 | 常に空のリストが返ることに注意してください。 |
11985 | 11834 | |
11986 | 11835 | =begin original |
11987 | 11836 | |
11988 | 11837 | A pattern matching the null string (not to be confused with |
11989 | 11838 | a null pattern C<//>, which is just one member of the set of patterns |
11990 | 11839 | matching a null string) will split the value of EXPR into separate |
11991 | 11840 | characters at each point it matches that way. For example: |
11992 | 11841 | |
11993 | 11842 | =end original |
11994 | 11843 | |
11995 | 11844 | 空文字列にマッチするパターン (ヌルパターン C<//> と混同しないでください。 |
11996 | 11845 | これは、空文字列にマッチするパターンの一つでしかありません) は、 |
11997 | 11846 | どの場所にもマッチし、EXPR の値を1 文字ずつに分割します。 |
11998 | 11847 | 例えば: |
11999 | 11848 | |
12000 | print join(':', split(/ */, 'hi there')) | |
11849 | print join(':', split(/ */, 'hi there')); | |
12001 | 11850 | |
12002 | 11851 | =begin original |
12003 | 11852 | |
12004 | 11853 | produces the output 'h:i:t:h:e:r:e'. |
12005 | 11854 | |
12006 | 11855 | =end original |
12007 | 11856 | |
12008 | 11857 | は、'h:i:t:h:e:r:e' という出力になります。 |
12009 | 11858 | |
12010 | 11859 | =begin original |
12011 | 11860 | |
12012 | 11861 | As a special case for C<split>, using the empty pattern C<//> specifically |
12013 | 11862 | matches only the null string, and is not be confused with the regular use |
12014 | 11863 | of C<//> to mean "the last successful pattern match". So, for C<split>, |
12015 | 11864 | the following: |
12016 | 11865 | |
12017 | 11866 | =end original |
12018 | 11867 | |
12019 | 11868 | C<split> に関する特別な場合として、特に空パターン C<//> は空文字列に |
12020 | 11869 | マッチするので、「最後に成功したパターンマッチ」を意味する通常の C<//> の |
12021 | 11870 | 使用法と混乱しないようにしてください。 |
12022 | 11871 | それで、C<split> の場合は、以下のようにすると: |
12023 | 11872 | |
12024 | print join(':', split(//, 'hi there')) | |
11873 | print join(':', split(//, 'hi there')); | |
12025 | 11874 | |
12026 | 11875 | =begin original |
12027 | 11876 | |
12028 | 11877 | produces the output 'h:i: :t:h:e:r:e'. |
12029 | 11878 | |
12030 | 11879 | =end original |
12031 | 11880 | |
12032 | 11881 | 'h:i: :t:h:e:r:e' という出力になります。 |
12033 | 11882 | |
12034 | 11883 | =begin original |
12035 | 11884 | |
12036 | 11885 | Empty leading fields are produced when there are positive-width matches at |
12037 | 11886 | the beginning of the string; a zero-width match at the beginning of |
12038 | 11887 | the string does not produce an empty field. For example: |
12039 | 11888 | |
12040 | 11889 | =end original |
12041 | 11890 | |
12042 | 11891 | 先頭の空フィールドは、文字列の先頭で 0 でない幅でマッチした場合は |
12043 | 11892 | 生成されます。 |
12044 | 11893 | 幅 0 でマッチした場合は生成されません。 |
12045 | 11894 | 例えば: |
12046 | 11895 | |
12047 | 11896 | print join(':', split(/(?=\w)/, 'hi there!')); |
12048 | 11897 | |
12049 | 11898 | =begin original |
12050 | 11899 | |
12051 | 11900 | produces the output 'h:i :t:h:e:r:e!'. Empty trailing fields, on the other |
12052 | 11901 | hand, are produced when there is a match at the end of the string (and |
12053 | 11902 | when LIMIT is given and is not 0), regardless of the length of the match. |
12054 | 11903 | For example: |
12055 | 11904 | |
12056 | 11905 | =end original |
12057 | 11906 | |
12058 | 11907 | これの出力は 'h:i :t:h:e:r:e!' となります。 |
12059 | 11908 | 一方、末尾の空フィールドは、マッチングの長さに関わらず、文字列の最後に |
12060 | 11909 | マッチングした(そして LIMIT が与えられてそれが 0 でなかった)場合に |
12061 | 11910 | 生成されます。 |
12062 | 11911 | 例えば: |
12063 | 11912 | |
12064 | print join(':', split(//, 'hi there!', -1)) | |
11913 | print join(':', split(//, 'hi there!', -1)); | |
12065 | print join(':', split(/\W/, 'hi there!', -1)) | |
11914 | print join(':', split(/\W/, 'hi there!', -1)); | |
12066 | 11915 | |
12067 | 11916 | =begin original |
12068 | 11917 | |
12069 | 11918 | produce the output 'h:i: :t:h:e:r:e:!:' and 'hi:there:', respectively, |
12070 | 11919 | both with an empty trailing field. |
12071 | 11920 | |
12072 | 11921 | =end original |
12073 | 11922 | |
12074 | 11923 | これの出力はそれぞれ 'h:i :t:h:e:r:e:!:' および 'hi:there:' となり、 |
12075 | 11924 | 両方とも末尾に空フィールドが付きます。 |
12076 | 11925 | |
12077 | 11926 | =begin original |
12078 | 11927 | |
12079 | 11928 | The LIMIT parameter can be used to split a line partially |
12080 | 11929 | |
12081 | 11930 | =end original |
12082 | 11931 | |
12083 | 11932 | LIMIT を使うと、行を部分的に split することができます。 |
12084 | 11933 | |
12085 | 11934 | ($login, $passwd, $remainder) = split(/:/, $_, 3); |
12086 | 11935 | |
12087 | 11936 | =begin original |
12088 | 11937 | |
12089 | 11938 | When assigning to a list, if LIMIT is omitted, or zero, Perl supplies |
12090 | 11939 | a LIMIT one larger than the number of variables in the list, to avoid |
12091 | 11940 | unnecessary work. For the list above LIMIT would have been 4 by |
12092 | 11941 | default. In time critical applications it behooves you not to split |
12093 | 11942 | into more fields than you really need. |
12094 | 11943 | |
12095 | 11944 | =end original |
12096 | 11945 | |
12097 | 11946 | リストへ代入するとき、LIMIT を省略するか 0 の場合、Perl は、 |
12098 | 11947 | 無駄な仕事を避けるため、そのリストの変数の数より、1 つだけ大きい |
12099 | 11948 | LIMIT が与えられたものとして処理を行ないます。 |
12100 | 11949 | 上のリストの場合には、LIMIT はデフォルトで 4 になります。 |
12101 | 11950 | 時間が問題となるアプリケーションでは、 |
12102 | 11951 | 必要以上のフィールドに分けないようにする必要があります。 |
12103 | 11952 | |
12104 | 11953 | =begin original |
12105 | 11954 | |
12106 | 11955 | If the PATTERN contains parentheses, additional list elements are |
12107 | 11956 | created from each matching substring in the delimiter. |
12108 | 11957 | |
12109 | 11958 | =end original |
12110 | 11959 | |
12111 | 11960 | PATTERN に括弧が含まれていると、デリミタ内の部分文字列にマッチするものも、 |
12112 | 11961 | リスト要素に含まれるようになります。 |
12113 | 11962 | |
12114 | 11963 | split(/([,-])/, "1-10,20", 3); |
12115 | 11964 | |
12116 | 11965 | =begin original |
12117 | 11966 | |
12118 | 11967 | produces the list value |
12119 | 11968 | |
12120 | 11969 | =end original |
12121 | 11970 | |
12122 | 11971 | は、以下のリスト値を生成します。 |
12123 | 11972 | |
12124 | 11973 | (1, '-', 10, ',', 20) |
12125 | 11974 | |
12126 | 11975 | =begin original |
12127 | 11976 | |
12128 | 11977 | If you had the entire header of a normal Unix email message in $header, |
12129 | 11978 | you could split it up into fields and their values this way: |
12130 | 11979 | |
12131 | 11980 | =end original |
12132 | 11981 | |
12133 | 11982 | $header に Unix E メールメッセージヘッダ全体が入っているとすると、 |
12134 | 11983 | 以下のようにしてフィールドとその値に分割できます: |
12135 | 11984 | |
12136 | $header =~ s/\n | |
11985 | $header =~ s/\n\s+/ /g; # fix continuation lines | |
12137 | 11986 | %hdrs = (UNIX_FROM => split /^(\S*?):\s*/m, $header); |
12138 | 11987 | |
12139 | 11988 | =begin original |
12140 | 11989 | |
12141 | 11990 | The pattern C</PATTERN/> may be replaced with an expression to specify |
12142 | 11991 | patterns that vary at runtime. (To do runtime compilation only once, |
12143 | 11992 | use C</$variable/o>.) |
12144 | 11993 | |
12145 | 11994 | =end original |
12146 | 11995 | |
12147 | 11996 | /PATTERN/ は、実行時に変わるパターンを指定する式で置き換えることができます。 |
12148 | 11997 | (実行時のコンパイルを 1 度にするために、/$variable/o を使ってください。) |
12149 | 11998 | |
12150 | 11999 | =begin original |
12151 | 12000 | |
12152 | 12001 | As a special case, specifying a PATTERN of space (S<C<' '>>) will split on |
12153 | 12002 | white space just as C<split> with no arguments does. Thus, S<C<split(' ')>> can |
12154 | 12003 | be used to emulate B<awk>'s default behavior, whereas S<C<split(/ /)>> |
12155 | 12004 | will give you as many null initial fields as there are leading spaces. |
12156 | 12005 | A C<split> on C</\s+/> is like a S<C<split(' ')>> except that any leading |
12157 | 12006 | whitespace produces a null first field. A C<split> with no arguments |
12158 | 12007 | really does a S<C<split(' ', $_)>> internally. |
12159 | 12008 | |
12160 | 12009 | =end original |
12161 | 12010 | |
12162 | 12011 | 特別な場合として、PATTERN にスペース (S<C<' '>>) を指定すると、 |
12163 | 12012 | 引数なしの C<split> のように空白で split を行ないます。 |
12164 | 12013 | つまり、S<C<split(' ')>> は B<awk> のデフォルトの動作をエミュレートするために |
12165 | 12014 | 使うことができ、S<C<split(/ /)>> は行頭のスペースの数に応じた空フィールドが |
12166 | 12015 | できます。 |
12167 | 12016 | C<split /\s+/> は S<C<split(' ')>> と同様ですが、 |
12168 | 12017 | 先頭の空白は先頭の空フィールドとなります。 |
12169 | 12018 | 引数なしの C<split> は内部的には S<C<split(' ', $_)>> を実行します。 |
12170 | 12019 | |
12171 | 12020 | =begin original |
12172 | 12021 | |
12173 | 12022 | A PATTERN of C</^/> is treated as if it were C</^/m>, since it isn't |
12174 | 12023 | much use otherwise. |
12175 | 12024 | |
12176 | 12025 | =end original |
12177 | 12026 | |
12178 | 12027 | PATTERN に C</^/> を指定すると C</^/m> として扱われます。 |
12179 | 12028 | 他の意味に使われることはまずないからです。 |
12180 | 12029 | |
12181 | 12030 | =begin original |
12182 | 12031 | |
12183 | 12032 | Example: |
12184 | 12033 | |
12185 | 12034 | =end original |
12186 | 12035 | |
12187 | 12036 | 例: |
12188 | 12037 | |
12189 | 12038 | open(PASSWD, '/etc/passwd'); |
12190 | 12039 | while (<PASSWD>) { |
12191 | 12040 | chomp; |
12192 | 12041 | ($login, $passwd, $uid, $gid, |
12193 | 12042 | $gcos, $home, $shell) = split(/:/); |
12194 | 12043 | #... |
12195 | 12044 | } |
12196 | 12045 | |
12197 | 12046 | =begin original |
12198 | 12047 | |
12199 | 12048 | As with regular pattern matching, any capturing parentheses that are not |
12200 | 12049 | matched in a C<split()> will be set to C<undef> when returned: |
12201 | 12050 | |
12202 | 12051 | =end original |
12203 | 12052 | |
12204 | 12053 | 通常のパターンマッチングで、C<split()> でマッチしない全てのかっこは |
12205 | 12054 | 返される時には C<undef> がセットされます。 |
12206 | 12055 | |
12207 | 12056 | @fields = split /(A)|B/, "1A2B3"; |
12208 | 12057 | # @fields is (1, 'A', 2, undef, 3) |
12209 | 12058 | |
12210 | 12059 | =item sprintf FORMAT, LIST |
12211 | 12060 | X<sprintf> |
12212 | 12061 | |
12213 | 12062 | =begin original |
12214 | 12063 | |
12215 | 12064 | Returns a string formatted by the usual C<printf> conventions of the C |
12216 | 12065 | library function C<sprintf>. See below for more details |
12217 | and see | |
12066 | and see L<sprintf(3)> or L<printf(3)> on your system for an explanation of | |
12218 | 12067 | the general principles. |
12219 | 12068 | |
12220 | 12069 | =end original |
12221 | 12070 | |
12222 | 12071 | 普通の C 言語の C<printf> 記法のフォーマットで、整形された文字列を返します。 |
12223 | 12072 | 一般的な原則の説明については以下の説明と、システムの |
12224 | ||
12073 | L<sprintf(3)> または L<printf(3)> の説明を参照してください。 | |
12225 | 12074 | |
12226 | 12075 | =begin original |
12227 | 12076 | |
12228 | 12077 | For example: |
12229 | 12078 | |
12230 | 12079 | =end original |
12231 | 12080 | |
12232 | 12081 | 例: |
12233 | 12082 | |
12234 | 12083 | # Format number with up to 8 leading zeroes |
12235 | 12084 | $result = sprintf("%08d", $number); |
12236 | 12085 | |
12237 | 12086 | # Round number to 3 digits after decimal point |
12238 | 12087 | $rounded = sprintf("%.3f", $number); |
12239 | 12088 | |
12240 | 12089 | =begin original |
12241 | 12090 | |
12242 | 12091 | Perl does its own C<sprintf> formatting--it emulates the C |
12243 | 12092 | function C<sprintf>, but it doesn't use it (except for floating-point |
12244 | 12093 | numbers, and even then only the standard modifiers are allowed). As a |
12245 | 12094 | result, any non-standard extensions in your local C<sprintf> are not |
12246 | 12095 | available from Perl. |
12247 | 12096 | |
12248 | 12097 | =end original |
12249 | 12098 | |
12250 | 12099 | Perl は C<sprintf> フォーマット処理を自力で行います。 |
12251 | 12100 | これは C の C<sprintf> 関数をエミュレートしますが、 |
12252 | 12101 | C の関数は使いません(浮動小数点を除きますが、それでも標準の |
12253 | 12102 | 記述子のみが利用できます)。 |
12254 | 12103 | 従って、ローカルな非標準の C<sprintf> 拡張機能は Perl では使えません。 |
12255 | 12104 | |
12256 | 12105 | =begin original |
12257 | 12106 | |
12258 | 12107 | Unlike C<printf>, C<sprintf> does not do what you probably mean when you |
12259 | 12108 | pass it an array as your first argument. The array is given scalar context, |
12260 | 12109 | and instead of using the 0th element of the array as the format, Perl will |
12261 | 12110 | use the count of elements in the array as the format, which is almost never |
12262 | 12111 | useful. |
12263 | 12112 | |
12264 | 12113 | =end original |
12265 | 12114 | |
12266 | 12115 | C<printf> と違って、 C<sprintf> の最初の引数に配列を渡しても |
12267 | 12116 | あなたが多分望むとおりには動作しません。 |
12268 | 12117 | 配列はスカラコンテキストで渡されるので、配列の 0 番目の要素ではなく、 |
12269 | 12118 | 配列の要素数をフォーマットとして扱います。 |
12270 | 12119 | これはほとんど役に立ちません。 |
12271 | 12120 | |
12272 | 12121 | =begin original |
12273 | 12122 | |
12274 | 12123 | Perl's C<sprintf> permits the following universally-known conversions: |
12275 | 12124 | |
12276 | 12125 | =end original |
12277 | 12126 | |
12278 | 12127 | Perl の C<sprintf> は以下の一般に知られている変換に対応しています: |
12279 | 12128 | |
12280 | 12129 | %% a percent sign |
12281 | 12130 | %c a character with the given number |
12282 | 12131 | %s a string |
12283 | 12132 | %d a signed integer, in decimal |
12284 | 12133 | %u an unsigned integer, in decimal |
12285 | 12134 | %o an unsigned integer, in octal |
12286 | 12135 | %x an unsigned integer, in hexadecimal |
12287 | 12136 | %e a floating-point number, in scientific notation |
12288 | 12137 | %f a floating-point number, in fixed decimal notation |
12289 | 12138 | %g a floating-point number, in %e or %f notation |
12290 | 12139 | |
12291 | 12140 | =begin original |
12292 | 12141 | |
12293 | 12142 | In addition, Perl permits the following widely-supported conversions: |
12294 | 12143 | |
12295 | 12144 | =end original |
12296 | 12145 | |
12297 | 12146 | さらに、Perl では以下のよく使われている変換に対応しています: |
12298 | 12147 | |
12299 | 12148 | %X like %x, but using upper-case letters |
12300 | 12149 | %E like %e, but using an upper-case "E" |
12301 | 12150 | %G like %g, but with an upper-case "E" (if applicable) |
12302 | 12151 | %b an unsigned integer, in binary |
12303 | 12152 | %B like %b, but using an upper-case "B" with the # flag |
12304 | 12153 | %p a pointer (outputs the Perl value's address in hexadecimal) |
12305 | 12154 | %n special: *stores* the number of characters output so far |
12306 | 12155 | into the next variable in the parameter list |
12307 | 12156 | |
12308 | 12157 | =begin original |
12309 | 12158 | |
12310 | 12159 | Finally, for backward (and we do mean "backward") compatibility, Perl |
12311 | 12160 | permits these unnecessary but widely-supported conversions: |
12312 | 12161 | |
12313 | 12162 | =end original |
12314 | 12163 | |
12315 | 12164 | 最後に、過去との互換性(これは「過去」だと考えています)のために、 |
12316 | 12165 | Perl は以下の不要ではあるけれども広く使われている変換に対応しています。 |
12317 | 12166 | |
12318 | 12167 | %i a synonym for %d |
12319 | 12168 | %D a synonym for %ld |
12320 | 12169 | %U a synonym for %lu |
12321 | 12170 | %O a synonym for %lo |
12322 | 12171 | %F a synonym for %f |
12323 | 12172 | |
12324 | 12173 | =begin original |
12325 | 12174 | |
12326 | 12175 | Note that the number of exponent digits in the scientific notation produced |
12327 | 12176 | by C<%e>, C<%E>, C<%g> and C<%G> for numbers with the modulus of the |
12328 | 12177 | exponent less than 100 is system-dependent: it may be three or less |
12329 | 12178 | (zero-padded as necessary). In other words, 1.23 times ten to the |
12330 | 12179 | 99th may be either "1.23e99" or "1.23e099". |
12331 | 12180 | |
12332 | 12181 | =end original |
12333 | 12182 | |
12334 | 12183 | C<%e>, C<%E>, C<%g>, C<%G> において、指数部が 100 未満の場合の |
12335 | 12184 | 指数部の科学的な表記法はシステム依存であることに注意してください: |
12336 | 12185 | 3 桁かもしれませんし、それ以下かもしれません(必要に応じて 0 で |
12337 | 12186 | パッディングされます)。 |
12338 | 12187 | 言い換えると、 1.23 掛ける 10 の 99 乗は "1.23e99" かもしれませんし |
12339 | 12188 | "1.23e099" かもしれません。 |
12340 | 12189 | |
12341 | 12190 | =begin original |
12342 | 12191 | |
12343 | 12192 | Between the C<%> and the format letter, you may specify a number of |
12344 | 12193 | additional attributes controlling the interpretation of the format. |
12345 | 12194 | In order, these are: |
12346 | 12195 | |
12347 | 12196 | =end original |
12348 | 12197 | |
12349 | 12198 | C<%> とフォーマット文字の間に、フォーマットの解釈を制御するための、 |
12350 | 12199 | 任意の数の追加の属性を指定できます。 |
12351 | 12200 | 順番に、以下のものがあります: |
12352 | 12201 | |
12353 | 12202 | =over 4 |
12354 | 12203 | |
12355 | 12204 | =item format parameter index |
12356 | 12205 | |
12357 | 12206 | =begin original |
12358 | 12207 | |
12359 | 12208 | An explicit format parameter index, such as C<2$>. By default sprintf |
12360 | 12209 | will format the next unused argument in the list, but this allows you |
12361 | 12210 | to take the arguments out of order, e.g.: |
12362 | 12211 | |
12363 | 12212 | =end original |
12364 | 12213 | |
12365 | 12214 | |
12366 | 12215 | printf '%2$d %1$d', 12, 34; # prints "34 12" |
12367 | 12216 | printf '%3$d %d %1$d', 1, 2, 3; # prints "3 1 1" |
12368 | 12217 | |
12369 | 12218 | =item flags |
12370 | 12219 | |
12371 | 12220 | =begin original |
12372 | 12221 | |
12373 | 12222 | one or more of: |
12374 | 12223 | |
12375 | 12224 | =end original |
12376 | 12225 | |
12377 | 12226 | 以下のうちの一つまたは複数指定できます: |
12378 | 12227 | |
12379 | space prefix | |
12228 | space prefix positive number with a space | |
12380 | + prefix | |
12229 | + prefix positive number with a plus sign | |
12381 | 12230 | - left-justify within the field |
12382 | 12231 | 0 use zeros, not spaces, to right-justify |
12383 | 12232 | # ensure the leading "0" for any octal, |
12384 | 12233 | prefix non-zero hexadecimal with "0x" or "0X", |
12385 | 12234 | prefix non-zero binary with "0b" or "0B" |
12386 | 12235 | |
12387 | 12236 | =begin original |
12388 | 12237 | |
12389 | 12238 | For example: |
12390 | 12239 | |
12391 | 12240 | =end original |
12392 | 12241 | |
12393 | 12242 | 例: |
12394 | 12243 | |
12395 | 12244 | printf '<% d>', 12; # prints "< 12>" |
12396 | 12245 | printf '<%+d>', 12; # prints "<+12>" |
12397 | 12246 | printf '<%6s>', 12; # prints "< 12>" |
12398 | 12247 | printf '<%-6s>', 12; # prints "<12 >" |
12399 | 12248 | printf '<%06s>', 12; # prints "<000012>" |
12400 | 12249 | printf '<%#o>', 12; # prints "<014>" |
12401 | 12250 | printf '<%#x>', 12; # prints "<0xc>" |
12402 | 12251 | printf '<%#X>', 12; # prints "<0XC>" |
12403 | 12252 | printf '<%#b>', 12; # prints "<0b1100>" |
12404 | 12253 | printf '<%#B>', 12; # prints "<0B1100>" |
12405 | 12254 | |
12406 | 12255 | =begin original |
12407 | 12256 | |
12408 | 12257 | When a space and a plus sign are given as the flags at once, |
12409 | 12258 | a plus sign is used to prefix a positive number. |
12410 | 12259 | |
12411 | 12260 | =end original |
12412 | 12261 | |
12413 | 12262 | 空白とプラス記号がフラグとして同時に与えられると、プラス記号は正の数に |
12414 | 12263 | 前置するために使われます。 |
12415 | 12264 | |
12416 | 12265 | printf '<%+ d>', 12; # prints "<+12>" |
12417 | 12266 | printf '<% +d>', 12; # prints "<+12>" |
12418 | 12267 | |
12419 | 12268 | =begin original |
12420 | 12269 | |
12421 | 12270 | When the # flag and a precision are given in the %o conversion, |
12422 | 12271 | the precision is incremented if it's necessary for the leading "0". |
12423 | 12272 | |
12424 | 12273 | =end original |
12425 | 12274 | |
12426 | 12275 | %o 変換に # フラグと精度が与えられると、先頭の "0" が必要な場合は |
12427 | 12276 | 精度に 1 が加えられます。 |
12428 | 12277 | |
12429 | 12278 | printf '<%#.5o>', 012; # prints "<00012>" |
12430 | 12279 | printf '<%#.5o>', 012345; # prints "<012345>" |
12431 | 12280 | printf '<%#.0o>', 0; # prints "<0>" |
12432 | 12281 | |
12433 | 12282 | =item vector flag |
12434 | 12283 | |
12435 | 12284 | =begin original |
12436 | 12285 | |
12437 | 12286 | This flag tells perl to interpret the supplied string as a vector of |
12438 | 12287 | integers, one for each character in the string. Perl applies the format to |
12439 | 12288 | each integer in turn, then joins the resulting strings with a separator (a |
12440 | 12289 | dot C<.> by default). This can be useful for displaying ordinal values of |
12441 | 12290 | characters in arbitrary strings: |
12442 | 12291 | |
12443 | 12292 | =end original |
12444 | 12293 | |
12445 | 12294 | このフラグは perl に、与えられた文字列を、文字毎に一つの整数のベクタとして |
12446 | 12295 | 解釈させます。 |
12447 | 12296 | Perl は各数値をフォーマットし、それから結果の文字列をセパレータ |
12448 | 12297 | (デフォルトでは C<.>)で連結します。 |
12449 | 12298 | これは任意の文字列の文字を順序付きの値として表示するのに便利です: |
12450 | 12299 | |
12451 | 12300 | printf "%vd", "AB\x{100}"; # prints "65.66.256" |
12452 | 12301 | printf "version is v%vd\n", $^V; # Perl's version |
12453 | 12302 | |
12454 | 12303 | =begin original |
12455 | 12304 | |
12456 | 12305 | Put an asterisk C<*> before the C<v> to override the string to |
12457 | 12306 | use to separate the numbers: |
12458 | 12307 | |
12459 | 12308 | =end original |
12460 | 12309 | |
12461 | 12310 | アスタリスク C<*> を C<v> の前に置くと、数値を分けるために使われる文字列を |
12462 | 12311 | 上書きします: |
12463 | 12312 | |
12464 | 12313 | printf "address is %*vX\n", ":", $addr; # IPv6 address |
12465 | 12314 | printf "bits are %0*v8b\n", " ", $bits; # random bitstring |
12466 | 12315 | |
12467 | 12316 | =begin original |
12468 | 12317 | |
12469 | 12318 | You can also explicitly specify the argument number to use for |
12470 | 12319 | the join string using e.g. C<*2$v>: |
12471 | 12320 | |
12472 | 12321 | =end original |
12473 | 12322 | |
12474 | 12323 | また、C<*2$v> のように、連結する文字列として使う引数の番号を明示的に |
12475 | 12324 | 指定できます。 |
12476 | 12325 | |
12477 | 12326 | printf '%*4$vX %*4$vX %*4$vX', @addr[1..3], ":"; # 3 IPv6 addresses |
12478 | 12327 | |
12479 | 12328 | =item (minimum) width |
12480 | 12329 | |
12481 | 12330 | =begin original |
12482 | 12331 | |
12483 | 12332 | Arguments are usually formatted to be only as wide as required to |
12484 | 12333 | display the given value. You can override the width by putting |
12485 | 12334 | a number here, or get the width from the next argument (with C<*>) |
12486 | 12335 | or from a specified argument (with e.g. C<*2$>): |
12487 | 12336 | |
12488 | 12337 | =end original |
12489 | 12338 | |
12490 | 12339 | 引数は、普通は値を表示するのに必要なちょうどの幅でフォーマットされます。 |
12491 | 12340 | ここに数値を置くか、(C<*> で)次の引数か(C<*2$> で)明示的に指定した引数で |
12492 | 12341 | 幅を上書きできます。 |
12493 | 12342 | |
12494 | 12343 | printf '<%s>', "a"; # prints "<a>" |
12495 | 12344 | printf '<%6s>', "a"; # prints "< a>" |
12496 | 12345 | printf '<%*s>', 6, "a"; # prints "< a>" |
12497 | 12346 | printf '<%*2$s>', "a", 6; # prints "< a>" |
12498 | 12347 | printf '<%2s>', "long"; # prints "<long>" (does not truncate) |
12499 | 12348 | |
12500 | 12349 | =begin original |
12501 | 12350 | |
12502 | 12351 | If a field width obtained through C<*> is negative, it has the same |
12503 | 12352 | effect as the C<-> flag: left-justification. |
12504 | 12353 | |
12505 | 12354 | =end original |
12506 | 12355 | |
12507 | 12356 | C<*> を通して得られたフィールドの値が負数の場合、C<-> フラグと |
12508 | 12357 | 同様の効果 (左詰め) があります。 |
12509 | 12358 | |
12510 | 12359 | =item precision, or maximum width |
12511 | 12360 | X<precision> |
12512 | 12361 | |
12513 | 12362 | =begin original |
12514 | 12363 | |
12515 | 12364 | You can specify a precision (for numeric conversions) or a maximum |
12516 | 12365 | width (for string conversions) by specifying a C<.> followed by a number. |
12517 | 12366 | For floating point formats, with the exception of 'g' and 'G', this specifies |
12518 | 12367 | the number of decimal places to show (the default being 6), e.g.: |
12519 | 12368 | |
12520 | 12369 | =end original |
12521 | 12370 | |
12522 | 12371 | C<.> の後に数値を指定することで、(数値変換の場合)精度や(文字列変換の場合) |
12523 | 12372 | 最大幅を指定できます。 |
12524 | 12373 | 小数点数フォーマットの場合、'g' と 'G' を除いて、表示する小数点以下の |
12525 | 12374 | 桁数を指定します(デフォルトは 6 です)。 |
12526 | 12375 | 例: |
12527 | 12376 | |
12528 | 12377 | # these examples are subject to system-specific variation |
12529 | 12378 | printf '<%f>', 1; # prints "<1.000000>" |
12530 | 12379 | printf '<%.1f>', 1; # prints "<1.0>" |
12531 | 12380 | printf '<%.0f>', 1; # prints "<1>" |
12532 | 12381 | printf '<%e>', 10; # prints "<1.000000e+01>" |
12533 | 12382 | printf '<%.1e>', 10; # prints "<1.0e+01>" |
12534 | 12383 | |
12535 | 12384 | =begin original |
12536 | 12385 | |
12537 | 12386 | For 'g' and 'G', this specifies the maximum number of digits to show, |
12538 | 12387 | including prior to the decimal point as well as after it, e.g.: |
12539 | 12388 | |
12540 | 12389 | =end original |
12541 | 12390 | |
12542 | 12391 | 'g' と 'G' の場合、これは表示する数値の数を指定します; |
12543 | 12392 | これには小数点の前の数値と後の数値を含みます。 |
12544 | 12393 | 例: |
12545 | 12394 | |
12546 | 12395 | # these examples are subject to system-specific variation |
12547 | 12396 | printf '<%g>', 1; # prints "<1>" |
12548 | 12397 | printf '<%.10g>', 1; # prints "<1>" |
12549 | 12398 | printf '<%g>', 100; # prints "<100>" |
12550 | 12399 | printf '<%.1g>', 100; # prints "<1e+02>" |
12551 | 12400 | printf '<%.2g>', 100.01; # prints "<1e+02>" |
12552 | 12401 | printf '<%.5g>', 100.01; # prints "<100.01>" |
12553 | 12402 | printf '<%.4g>', 100.01; # prints "<100>" |
12554 | 12403 | |
12555 | 12404 | =begin original |
12556 | 12405 | |
12557 | 12406 | For integer conversions, specifying a precision implies that the |
12558 | 12407 | output of the number itself should be zero-padded to this width, |
12559 | 12408 | where the 0 flag is ignored: |
12560 | 12409 | |
12561 | 12410 | =end original |
12562 | 12411 | |
12563 | 12412 | 整数変換の場合、精度を指定すると、数値自体の出力はこの幅に 0 で |
12564 | 12413 | パッディングするべきであることを暗に示すことになり、0 フラグは |
12565 | 12414 | 無視されます: |
12566 | 12415 | |
12567 | 12416 | printf '<%.6d>', 1; # prints "<000001>" |
12568 | 12417 | printf '<%+.6d>', 1; # prints "<+000001>" |
12569 | 12418 | printf '<%-10.6d>', 1; # prints "<000001 >" |
12570 | 12419 | printf '<%10.6d>', 1; # prints "< 000001>" |
12571 | 12420 | printf '<%010.6d>', 1; # prints "< 000001>" |
12572 | 12421 | printf '<%+10.6d>', 1; # prints "< +000001>" |
12573 | 12422 | |
12574 | 12423 | printf '<%.6x>', 1; # prints "<000001>" |
12575 | 12424 | printf '<%#.6x>', 1; # prints "<0x000001>" |
12576 | 12425 | printf '<%-10.6x>', 1; # prints "<000001 >" |
12577 | 12426 | printf '<%10.6x>', 1; # prints "< 000001>" |
12578 | 12427 | printf '<%010.6x>', 1; # prints "< 000001>" |
12579 | 12428 | printf '<%#10.6x>', 1; # prints "< 0x000001>" |
12580 | 12429 | |
12581 | 12430 | =begin original |
12582 | 12431 | |
12583 | 12432 | For string conversions, specifying a precision truncates the string |
12584 | 12433 | to fit in the specified width: |
12585 | 12434 | |
12586 | 12435 | =end original |
12587 | 12436 | |
12588 | 12437 | 文字列変換の場合、精度を指定すると、指定された幅に収まるように文字列を |
12589 | 12438 | 切り詰めます: |
12590 | 12439 | |
12591 | 12440 | printf '<%.5s>', "truncated"; # prints "<trunc>" |
12592 | 12441 | printf '<%10.5s>', "truncated"; # prints "< trunc>" |
12593 | 12442 | |
12594 | 12443 | =begin original |
12595 | 12444 | |
12596 | 12445 | You can also get the precision from the next argument using C<.*>: |
12597 | 12446 | |
12598 | 12447 | =end original |
12599 | 12448 | |
12600 | 12449 | C<.*> を使って精度を次の引数から取ることも出来ます: |
12601 | 12450 | |
12602 | 12451 | printf '<%.6x>', 1; # prints "<000001>" |
12603 | 12452 | printf '<%.*x>', 6, 1; # prints "<000001>" |
12604 | 12453 | |
12605 | 12454 | =begin original |
12606 | 12455 | |
12607 | 12456 | If a precision obtained through C<*> is negative, it has the same |
12608 | 12457 | effect as no precision. |
12609 | 12458 | |
12610 | 12459 | =end original |
12611 | 12460 | |
12612 | 12461 | C<*> によって得られた精度が負数の場合、精度が指定されなかった場合と |
12613 | 12462 | 同じ効果となります。 |
12614 | 12463 | |
12615 | 12464 | printf '<%.*s>', 7, "string"; # prints "<string>" |
12616 | 12465 | printf '<%.*s>', 3, "string"; # prints "<str>" |
12617 | 12466 | printf '<%.*s>', 0, "string"; # prints "<>" |
12618 | 12467 | printf '<%.*s>', -1, "string"; # prints "<string>" |
12619 | 12468 | |
12620 | 12469 | printf '<%.*d>', 1, 0; # prints "<0>" |
12621 | 12470 | printf '<%.*d>', 0, 0; # prints "<>" |
12622 | 12471 | printf '<%.*d>', -1, 0; # prints "<0>" |
12623 | 12472 | |
12624 | 12473 | =begin original |
12625 | 12474 | |
12626 | 12475 | You cannot currently get the precision from a specified number, |
12627 | 12476 | but it is intended that this will be possible in the future using |
12628 | 12477 | e.g. C<.*2$>: |
12629 | 12478 | |
12630 | 12479 | =end original |
12631 | 12480 | |
12632 | 12481 | 現在のところ精度を指定した数値から得ることはできませんが、 |
12633 | 12482 | 将来は C<.*2$> のようにして可能にしようとしています: |
12634 | 12483 | |
12635 | 12484 | printf '<%.*2$x>', 1, 6; # INVALID, but in future will print "<000001>" |
12636 | 12485 | |
12637 | 12486 | =item size |
12638 | 12487 | |
12639 | 12488 | =begin original |
12640 | 12489 | |
12641 | 12490 | For numeric conversions, you can specify the size to interpret the |
12642 | 12491 | number as using C<l>, C<h>, C<V>, C<q>, C<L>, or C<ll>. For integer |
12643 | 12492 | conversions (C<d u o x X b i D U O>), numbers are usually assumed to be |
12644 | 12493 | whatever the default integer size is on your platform (usually 32 or 64 |
12645 | 12494 | bits), but you can override this to use instead one of the standard C types, |
12646 | 12495 | as supported by the compiler used to build Perl: |
12647 | 12496 | |
12648 | 12497 | =end original |
12649 | 12498 | |
12650 | 12499 | 数値変換では、C<l>, C<h>, C<V>, C<q>, C<L>, C<ll> を使って解釈する数値の |
12651 | 12500 | 大きさを指定できます。 |
12652 | 12501 | 整数変換 (C<d u o x X b i D U O>) では、数値は通常プラットフォームの |
12653 | 12502 | デフォルトの整数のサイズ (通常は 32 ビットか 64 ビット) を仮定しますが、 |
12654 | 12503 | これを Perl がビルドされたコンパイラが対応している標準 C の型の一つで |
12655 | 12504 | 上書きできます: |
12656 | 12505 | |
12657 | 12506 | =begin original |
12658 | 12507 | |
12659 | 12508 | l interpret integer as C type "long" or "unsigned long" |
12660 | 12509 | h interpret integer as C type "short" or "unsigned short" |
12661 | 12510 | q, L or ll interpret integer as C type "long long", "unsigned long long". |
12662 | 12511 | or "quads" (typically 64-bit integers) |
12663 | 12512 | |
12664 | 12513 | =end original |
12665 | 12514 | |
12666 | 12515 | l 整数を C の "long" または "unsigned long" と解釈する |
12667 | 12516 | h 整数を C の "short" または "unsigned short" と解釈する |
12668 | 12517 | q, L or ll 整数を C の "long long", "unsigned long long", |
12669 | 12518 | "quads"(典型的には 64 ビット整数) のどれかと解釈する |
12670 | 12519 | |
12671 | 12520 | =begin original |
12672 | 12521 | |
12673 | 12522 | The last will produce errors if Perl does not understand "quads" in your |
12674 | 12523 | installation. (This requires that either the platform natively supports quads |
12675 | 12524 | or Perl was specifically compiled to support quads.) You can find out |
12676 | 12525 | whether your Perl supports quads via L<Config>: |
12677 | 12526 | |
12678 | 12527 | =end original |
12679 | 12528 | |
12680 | 12529 | 最後の例では、Perl が 64 ビット整数を理解しない場合はエラーになります。 |
12681 | 12530 | (このためにはプラットフォームがネイティブに 64 ビット整数に対応しているか、 |
12682 | 12531 | Perl が特に 64 ビット整数に対応するようにコンパイルされている |
12683 | 12532 | 必要があります。) |
12684 | 12533 | Perl が 64 ビット整数に対応しているかどうかは L<Config> を使って |
12685 | 12534 | 調べられます: |
12686 | 12535 | |
12687 | 12536 | use Config; |
12688 | 12537 | ($Config{use64bitint} eq 'define' || $Config{longsize} >= 8) && |
12689 | 12538 | print "quads\n"; |
12690 | 12539 | |
12691 | 12540 | =begin original |
12692 | 12541 | |
12693 | 12542 | For floating point conversions (C<e f g E F G>), numbers are usually assumed |
12694 | 12543 | to be the default floating point size on your platform (double or long double), |
12695 | 12544 | but you can force 'long double' with C<q>, C<L>, or C<ll> if your |
12696 | 12545 | platform supports them. You can find out whether your Perl supports long |
12697 | 12546 | doubles via L<Config>: |
12698 | 12547 | |
12699 | 12548 | =end original |
12700 | 12549 | |
12701 | 12550 | 浮動小数点数変換 (C<e f g E F G>) では、普通はプラットフォームのデフォルトの |
12702 | 12551 | 不動小数点数のサイズ (double か long double) を仮定します。 |
12703 | 12552 | Perl が long double に対応しているかどうかは L<Config> を使って |
12704 | 12553 | 調べられます: |
12705 | 12554 | |
12706 | 12555 | use Config; |
12707 | 12556 | $Config{d_longdbl} eq 'define' && print "long doubles\n"; |
12708 | 12557 | |
12709 | 12558 | =begin original |
12710 | 12559 | |
12711 | 12560 | You can find out whether Perl considers 'long double' to be the default |
12712 | 12561 | floating point size to use on your platform via L<Config>: |
12713 | 12562 | |
12714 | 12563 | =end original |
12715 | 12564 | |
12716 | 12565 | Perl が 'long double' をデフォルトの浮動小数点数として扱っているかどうかは |
12717 | 12566 | L<Config> を使って調べられます: |
12718 | 12567 | |
12719 | 12568 | use Config; |
12720 | 12569 | ($Config{uselongdouble} eq 'define') && |
12721 | 12570 | print "long doubles by default\n"; |
12722 | 12571 | |
12723 | 12572 | =begin original |
12724 | 12573 | |
12725 | 12574 | It can also be the case that long doubles and doubles are the same thing: |
12726 | 12575 | |
12727 | 12576 | =end original |
12728 | 12577 | |
12729 | 12578 | long double と double が同じ場合もあります: |
12730 | 12579 | |
12731 | 12580 | use Config; |
12732 | 12581 | ($Config{doublesize} == $Config{longdblsize}) && |
12733 | 12582 | print "doubles are long doubles\n"; |
12734 | 12583 | |
12735 | 12584 | =begin original |
12736 | 12585 | |
12737 | 12586 | The size specifier C<V> has no effect for Perl code, but it is supported |
12738 | 12587 | for compatibility with XS code; it means 'use the standard size for |
12739 | 12588 | a Perl integer (or floating-point number)', which is already the |
12740 | 12589 | default for Perl code. |
12741 | 12590 | |
12742 | 12591 | =end original |
12743 | 12592 | |
12744 | 12593 | サイズ指定子 C<V> は Perl のコードには何の影響もありませんが、これは |
12745 | 12594 | XS コードとの互換性のために対応しています; これは「Perl 整数 (または |
12746 | 12595 | 浮動小数点数) として標準的なサイズを使う」ことを意味し、これは Perl の |
12747 | 12596 | コードでは既にデフォルトです。 |
12748 | 12597 | |
12749 | 12598 | =item order of arguments |
12750 | 12599 | |
12751 | 12600 | =begin original |
12752 | 12601 | |
12753 | 12602 | Normally, sprintf takes the next unused argument as the value to |
12754 | 12603 | format for each format specification. If the format specification |
12755 | 12604 | uses C<*> to require additional arguments, these are consumed from |
12756 | 12605 | the argument list in the order in which they appear in the format |
12757 | 12606 | specification I<before> the value to format. Where an argument is |
12758 | 12607 | specified using an explicit index, this does not affect the normal |
12759 | 12608 | order for the arguments (even when the explicitly specified index |
12760 | 12609 | would have been the next argument in any case). |
12761 | 12610 | |
12762 | 12611 | =end original |
12763 | 12612 | |
12764 | 12613 | 通常、sprintf は各フォーマット指定について、使われていない次の引数を |
12765 | 12614 | フォーマットする値として使います。 |
12766 | 12615 | 追加の引数を要求するためにフォーマット指定 C<*> を使うと、 |
12767 | 12616 | これらはフォーマットする値の I<前> のフォーマット指定に現れる順番に |
12768 | 12617 | 引数リストから消費されます。 |
12769 | 12618 | 引数の位置が明示的なインデックスを使って指定された場合、 |
12770 | 12619 | (明示的に指定したインデックスが次の引数の場合でも) |
12771 | 12620 | これは通常の引数の順番に影響を与えません。 |
12772 | 12621 | |
12773 | 12622 | =begin original |
12774 | 12623 | |
12775 | 12624 | So: |
12776 | 12625 | |
12777 | 12626 | =end original |
12778 | 12627 | |
12779 | 12628 | それで: |
12780 | 12629 | |
12781 | 12630 | printf '<%*.*s>', $a, $b, $c; |
12782 | 12631 | |
12783 | 12632 | =begin original |
12784 | 12633 | |
12785 | 12634 | would use C<$a> for the width, C<$b> for the precision and C<$c> |
12786 | 12635 | as the value to format, while: |
12787 | 12636 | |
12788 | 12637 | =end original |
12789 | 12638 | |
12790 | 12639 | とすると C<$a> を幅に、C<$b> を精度に、C<$c> をフォーマットの値に |
12791 | 12640 | 使いますが、一方: |
12792 | 12641 | |
12793 | 12642 | printf '<%*1$.*s>', $a, $b; |
12794 | 12643 | |
12795 | 12644 | =begin original |
12796 | 12645 | |
12797 | 12646 | would use C<$a> for the width and the precision, and C<$b> as the |
12798 | 12647 | value to format. |
12799 | 12648 | |
12800 | 12649 | =end original |
12801 | 12650 | |
12802 | 12651 | とすると C<$a> を幅と精度に、C<$b> をフォーマットの値に使います。 |
12803 | 12652 | |
12804 | 12653 | =begin original |
12805 | 12654 | |
12806 | 12655 | Here are some more examples - beware that when using an explicit |
12807 | 12656 | index, the C<$> may need to be escaped: |
12808 | 12657 | |
12809 | 12658 | =end original |
12810 | 12659 | |
12811 | 12660 | 以下にさらなる例を示します - 明示的にインデックスを使う場合、C<$> は |
12812 | 12661 | エスケープする必要があることに注意してください。 |
12813 | 12662 | |
12814 | 12663 | printf "%2\$d %d\n", 12, 34; # will print "34 12\n" |
12815 | 12664 | printf "%2\$d %d %d\n", 12, 34; # will print "34 12 34\n" |
12816 | 12665 | printf "%3\$d %d %d\n", 12, 34, 56; # will print "56 12 34\n" |
12817 | 12666 | printf "%2\$*3\$d %d\n", 12, 34, 3; # will print " 34 12\n" |
12818 | 12667 | |
12819 | 12668 | =back |
12820 | 12669 | |
12821 | 12670 | =begin original |
12822 | 12671 | |
12823 | 12672 | If C<use locale> is in effect, and POSIX::setlocale() has been called, |
12824 | 12673 | the character used for the decimal separator in formatted floating |
12825 | 12674 | point numbers is affected by the LC_NUMERIC locale. See L<perllocale> |
12826 | 12675 | and L<POSIX>. |
12827 | 12676 | |
12828 | 12677 | =end original |
12829 | 12678 | |
12830 | 12679 | C<use locale> が有効で、POSIX::setlocale() が呼び出されている場合、 |
12831 | 12680 | フォーマットされた浮動小数点数の小数点として使われる文字は |
12832 | 12681 | LC_NUMERIC ロケールの影響を受けます。 |
12833 | 12682 | L<perllocale> と L<POSIX> を参照してください。 |
12834 | 12683 | |
12835 | 12684 | =item sqrt EXPR |
12836 | 12685 | X<sqrt> X<root> X<square root> |
12837 | 12686 | |
12838 | 12687 | =item sqrt |
12839 | 12688 | |
12840 | 12689 | =begin original |
12841 | 12690 | |
12842 | 12691 | Return the square root of EXPR. If EXPR is omitted, returns square |
12843 | 12692 | root of C<$_>. Only works on non-negative operands, unless you've |
12844 | 12693 | loaded the standard Math::Complex module. |
12845 | 12694 | |
12846 | 12695 | =end original |
12847 | 12696 | |
12848 | 12697 | EXPR の平方根を返します。 |
12849 | 12698 | EXPR を省略すると、C<$_> の平方根を返します。 |
12850 | 12699 | 標準の Math::Complex モジュールを使わない場合は、 |
12851 | 12700 | 負の数の引数は扱えません。 |
12852 | 12701 | |
12853 | 12702 | use Math::Complex; |
12854 | 12703 | print sqrt(-2); # prints 1.4142135623731i |
12855 | 12704 | |
12856 | 12705 | =item srand EXPR |
12857 | 12706 | X<srand> X<seed> X<randseed> |
12858 | 12707 | |
12859 | 12708 | =item srand |
12860 | 12709 | |
12861 | 12710 | =begin original |
12862 | 12711 | |
12863 | 12712 | Sets the random number seed for the C<rand> operator. |
12864 | 12713 | |
12865 | 12714 | =end original |
12866 | 12715 | |
12867 | 12716 | rand 演算子のためのシード値を設定します。 |
12868 | 12717 | |
12869 | 12718 | =begin original |
12870 | 12719 | |
12871 | 12720 | The point of the function is to "seed" the C<rand> function so that |
12872 | 12721 | C<rand> can produce a different sequence each time you run your |
12873 | 12722 | program. |
12874 | 12723 | |
12875 | 12724 | =end original |
12876 | 12725 | |
12877 | 12726 | この関数のポイントは、プログラムを実行するごとに C<rand> 関数が |
12878 | 12727 | 異なる乱数列を生成できるように C<rand> 関数の「種」を設定することです。 |
12879 | 12728 | |
12880 | 12729 | =begin original |
12881 | 12730 | |
12882 | 12731 | If srand() is not called explicitly, it is called implicitly at the |
12883 | 12732 | first use of the C<rand> operator. However, this was not the case in |
12884 | 12733 | versions of Perl before 5.004, so if your script will run under older |
12885 | 12734 | Perl versions, it should call C<srand>. |
12886 | 12735 | |
12887 | 12736 | =end original |
12888 | 12737 | |
12889 | 12738 | srand() が明示的に呼び出されなかった場合、最初に C<rand> 演算子を使った |
12890 | 12739 | 時点で暗黙に呼び出されます。 |
12891 | 12740 | しかし、これは Perl のバージョンが 5.004 より前では行われませんので、 |
12892 | 12741 | プログラムが古い Perl で実行される場合は、C<srand> を呼ぶべきです。 |
12893 | 12742 | |
12894 | 12743 | =begin original |
12895 | 12744 | |
12896 | 12745 | Most programs won't even call srand() at all, except those that |
12897 | 12746 | need a cryptographically-strong starting point rather than the |
12898 | 12747 | generally acceptable default, which is based on time of day, |
12899 | 12748 | process ID, and memory allocation, or the F</dev/urandom> device, |
12900 | 12749 | if available. |
12901 | 12750 | |
12902 | 12751 | =end original |
12903 | 12752 | |
12904 | 12753 | ほとんどのプログラムはそもそも srand() を呼ぶ必要すらありません; |
12905 | 12754 | 例外は、時刻、プロセス ID、メモリ配置、(利用可能なら) F</dev/urandom> |
12906 | 12755 | デバイスといった、一般的に受け入れられるデフォルトよりも暗号学的に |
12907 | 12756 | 強力な開始点が必要な場合です。 |
12908 | 12757 | |
12909 | 12758 | =begin original |
12910 | 12759 | |
12911 | 12760 | You can call srand($seed) with the same $seed to reproduce the |
12912 | 12761 | I<same> sequence from rand(), but this is usually reserved for |
12913 | 12762 | generating predictable results for testing or debugging. |
12914 | 12763 | Otherwise, don't call srand() more than once in your program. |
12915 | 12764 | |
12916 | 12765 | =end original |
12917 | 12766 | |
12918 | 12767 | 同じ $seed を使って srand($seed) を呼び出すことで、rand() から I<同じ> |
12919 | 12768 | 乱数列を再現できますが、これは普通テストやデバッグのために予測された |
12920 | 12769 | 結果を生成するために使われます。 |
12921 | 12770 | それ以外では、srand() をプログラム内で 2 回以上呼び出さないでください。 |
12922 | 12771 | |
12923 | 12772 | =begin original |
12924 | 12773 | |
12925 | 12774 | Do B<not> call srand() (i.e. without an argument) more than once in |
12926 | 12775 | a script. The internal state of the random number generator should |
12927 | 12776 | contain more entropy than can be provided by any seed, so calling |
12928 | 12777 | srand() again actually I<loses> randomness. |
12929 | 12778 | |
12930 | 12779 | =end original |
12931 | 12780 | |
12932 | 12781 | srand() (引数なし)をプログラム中でI<複数回呼び出してはいけません>。 |
12933 | 12782 | 乱数生成器の内部状態はどのような種によって提供されるものよりも |
12934 | 12783 | 高いエントロピーを持っているので、srand() を再び呼び出すと |
12935 | 12784 | ランダム性が I<失われます>。 |
12936 | 12785 | |
12937 | 12786 | =begin original |
12938 | 12787 | |
12939 | 12788 | Most implementations of C<srand> take an integer and will silently |
12940 | 12789 | truncate decimal numbers. This means C<srand(42)> will usually |
12941 | 12790 | produce the same results as C<srand(42.1)>. To be safe, always pass |
12942 | 12791 | C<srand> an integer. |
12943 | 12792 | |
12944 | 12793 | =end original |
12945 | 12794 | |
12946 | 12795 | C<srand> のほとんどの実装では整数を取り、小数を暗黙に切り捨てます。 |
12947 | 12796 | これは、C<srand(42)> は普通 C<srand(42.1)> と同じ結果になることを |
12948 | 12797 | 意味します。 |
12949 | 12798 | 安全のために、C<srand> には常に整数を渡しましょう。 |
12950 | 12799 | |
12951 | 12800 | =begin original |
12952 | 12801 | |
12953 | 12802 | In versions of Perl prior to 5.004 the default seed was just the |
12954 | 12803 | current C<time>. This isn't a particularly good seed, so many old |
12955 | 12804 | programs supply their own seed value (often C<time ^ $$> or C<time ^ |
12956 | 12805 | ($$ + ($$ << 15))>), but that isn't necessary any more. |
12957 | 12806 | |
12958 | 12807 | =end original |
12959 | 12808 | |
12960 | 12809 | 5.004 以前の Perl では、デフォルトのシード値は現在の C<time> でした。 |
12961 | 12810 | これは特によいシード値ではありませんでしたので、 |
12962 | 12811 | 多くの古いプログラムは自力でシード値を指定しています |
12963 | 12812 | (C<time ^ $$> または C<time ^ ($$ + ($$ << 15))> がよく使われました)が、 |
12964 | 12813 | もはやこれは必要ありません。 |
12965 | 12814 | |
12966 | 12815 | =begin original |
12967 | 12816 | |
12968 | 12817 | For cryptographic purposes, however, you need something much more random |
12969 | 12818 | than the default seed. Checksumming the compressed output of one or more |
12970 | 12819 | rapidly changing operating system status programs is the usual method. For |
12971 | 12820 | example: |
12972 | 12821 | |
12973 | 12822 | =end original |
12974 | 12823 | |
12975 | 12824 | しかし、暗号処理にはもっとランダムな値を使う必要があります。 |
12976 | 12825 | 急激に変化するOS のステータス値プログラムの出力をひとつまたは複数用い、 |
12977 | 12826 | 圧縮してチェックサムをとる、というようなことが普通行なわれます。 |
12978 | 12827 | 例えば: |
12979 | 12828 | |
12980 | 12829 | srand (time ^ $$ ^ unpack "%L*", `ps axww | gzip -f`); |
12981 | 12830 | |
12982 | 12831 | =begin original |
12983 | 12832 | |
12984 | 12833 | If you're particularly concerned with this, see the C<Math::TrulyRandom> |
12985 | 12834 | module in CPAN. |
12986 | 12835 | |
12987 | 12836 | =end original |
12988 | 12837 | |
12989 | 12838 | 特にこのようなことに関心がある場合は、 |
12990 | 12839 | CPAN の C<Math::TrulyRandom> モジュールを参照して下さい。 |
12991 | 12840 | |
12992 | 12841 | =begin original |
12993 | 12842 | |
12994 | 12843 | Frequently called programs (like CGI scripts) that simply use |
12995 | 12844 | |
12996 | 12845 | =end original |
12997 | 12846 | |
12998 | 12847 | (CGI スクリプトのような)頻繁に呼び出されるプログラムで単純に |
12999 | 12848 | |
13000 | 12849 | time ^ $$ |
13001 | 12850 | |
13002 | 12851 | =begin original |
13003 | 12852 | |
13004 | 12853 | for a seed can fall prey to the mathematical property that |
13005 | 12854 | |
13006 | 12855 | =end original |
13007 | 12856 | |
13008 | 12857 | を種として使うと、3 回に 1 回は以下の数学特性 |
13009 | 12858 | |
13010 | 12859 | a^b == (a+1)^(b+1) |
13011 | 12860 | |
13012 | 12861 | =begin original |
13013 | 12862 | |
13014 | 12863 | one-third of the time. So don't do that. |
13015 | 12864 | |
13016 | 12865 | =end original |
13017 | 12866 | |
13018 | 12867 | の餌食になります。 |
13019 | 12868 | 従ってこのようなことはしてはいけません。 |
13020 | 12869 | |
13021 | 12870 | =item stat FILEHANDLE |
13022 | 12871 | X<stat> X<file, status> X<ctime> |
13023 | 12872 | |
13024 | 12873 | =item stat EXPR |
13025 | 12874 | |
13026 | 12875 | =item stat DIRHANDLE |
13027 | 12876 | |
13028 | 12877 | =item stat |
13029 | 12878 | |
13030 | 12879 | =begin original |
13031 | 12880 | |
13032 | 12881 | Returns a 13-element list giving the status info for a file, either |
13033 | 12882 | the file opened via FILEHANDLE or DIRHANDLE, or named by EXPR. If EXPR is |
13034 | 12883 | omitted, it stats C<$_>. Returns a null list if the stat fails. Typically |
13035 | 12884 | used as follows: |
13036 | 12885 | |
13037 | 12886 | =end original |
13038 | 12887 | |
13039 | 12888 | FILEHANDLE か DIRHANDLE を通じてオープンされているファイルか、 |
13040 | 12889 | EXPR で指定されるファイルの情報を与える、13 要素のリストを返します。 |
13041 | 12890 | EXPR が省略されると、 C<$_> が用いられます。 |
13042 | 12891 | stat に失敗した場合には、空リストを返します。 |
13043 | 12892 | 普通は、以下のようにして使います: |
13044 | 12893 | |
13045 | 12894 | ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size, |
13046 | 12895 | $atime,$mtime,$ctime,$blksize,$blocks) |
13047 | 12896 | = stat($filename); |
13048 | 12897 | |
13049 | 12898 | =begin original |
13050 | 12899 | |
13051 | 12900 | Not all fields are supported on all filesystem types. Here are the |
13052 | 12901 | meanings of the fields: |
13053 | 12902 | |
13054 | 12903 | =end original |
13055 | 12904 | |
13056 | 12905 | 全てのファイルシステムで全てのフィールドに対応しているわけではありません。 |
13057 | 12906 | フィールドの意味は以下の通りです。 |
13058 | 12907 | |
13059 | 12908 | =begin original |
13060 | 12909 | |
13061 | 12910 | 0 dev device number of filesystem |
13062 | 12911 | 1 ino inode number |
13063 | 12912 | 2 mode file mode (type and permissions) |
13064 | 12913 | 3 nlink number of (hard) links to the file |
13065 | 12914 | 4 uid numeric user ID of file's owner |
13066 | 12915 | 5 gid numeric group ID of file's owner |
13067 | 12916 | 6 rdev the device identifier (special files only) |
13068 | 12917 | 7 size total size of file, in bytes |
13069 | 12918 | 8 atime last access time in seconds since the epoch |
13070 | 12919 | 9 mtime last modify time in seconds since the epoch |
13071 | 12920 | 10 ctime inode change time in seconds since the epoch (*) |
13072 | 12921 | 11 blksize preferred block size for file system I/O |
13073 | 12922 | 12 blocks actual number of blocks allocated |
13074 | 12923 | |
13075 | 12924 | =end original |
13076 | 12925 | |
13077 | 12926 | 0 dev ファイルシステムのデバイス番号 |
13078 | 12927 | 1 ino inode 番号 |
13079 | 12928 | 2 mode ファイルモード (タイプとパーミッション) |
13080 | 12929 | 3 nlink ファイルへの(ハード)リンクの数 |
13081 | 12930 | 4 uid ファイル所有者のユーザー ID の数値 |
13082 | 12931 | 5 gid ファイル所有者のグループ ID の数値 |
13083 | 12932 | 6 rdev デバイス識別子(特殊ファイルのみ) |
13084 | 12933 | 7 size ファイルサイズ(バイト単位) |
13085 | 12934 | 8 atime 紀元から、最後にアクセスされた時刻までの秒数 |
13086 | 12935 | 9 mtime 紀元から、最後に修正(modify)された時刻までの秒数 |
13087 | 12936 | 10 ctime 紀元から、inode 変更(change)された時刻までの秒数 (*) |
13088 | 12937 | 11 blksize ファイルシステム I/O に適したブロックサイズ |
13089 | 12938 | 12 blocks 実際に割り当てられているブロックの数 |
13090 | 12939 | |
13091 | 12940 | =begin original |
13092 | 12941 | |
13093 | 12942 | (The epoch was at 00:00 January 1, 1970 GMT.) |
13094 | 12943 | |
13095 | 12944 | =end original |
13096 | 12945 | |
13097 | 12946 | (紀元は GMT で 1970/01/01 00:00:00) |
13098 | 12947 | |
13099 | 12948 | =begin original |
13100 | 12949 | |
13101 | 12950 | (*) Not all fields are supported on all filesystem types. Notably, the |
13102 | 12951 | ctime field is non-portable. In particular, you cannot expect it to be a |
13103 | 12952 | "creation time", see L<perlport/"Files and Filesystems"> for details. |
13104 | 12953 | |
13105 | 12954 | =end original |
13106 | 12955 | |
13107 | 12956 | (*) 全てのフィールドが全てのファイルシステムタイプで対応しているわけでは |
13108 | 12957 | ありません。 |
13109 | 12958 | 明らかに、ctime のフィールドは移植性がありません。 |
13110 | 12959 | 特に、これから「作成時刻」を想定することは出来ません。 |
13111 | 12960 | 詳細については L<perlport/"Files and Filesystems"> を参照してください。 |
13112 | 12961 | |
13113 | 12962 | =begin original |
13114 | 12963 | |
13115 | 12964 | If C<stat> is passed the special filehandle consisting of an underline, no |
13116 | 12965 | stat is done, but the current contents of the stat structure from the |
13117 | 12966 | last C<stat>, C<lstat>, or filetest are returned. Example: |
13118 | 12967 | |
13119 | 12968 | =end original |
13120 | 12969 | |
13121 | 12970 | 下線だけの _ という特別なファイルハンドルを C<stat> に渡すと、 |
13122 | 12971 | 実際には stat を行なわず、stat 構造体に残っている |
13123 | 12972 | 前回の stat やファイルテストの情報が返されます。 |
13124 | 12973 | 例: |
13125 | 12974 | |
13126 | 12975 | if (-x $file && (($d) = stat(_)) && $d < 0) { |
13127 | 12976 | print "$file is executable NFS file\n"; |
13128 | 12977 | } |
13129 | 12978 | |
13130 | 12979 | =begin original |
13131 | 12980 | |
13132 | 12981 | (This works on machines only for which the device number is negative |
13133 | 12982 | under NFS.) |
13134 | 12983 | |
13135 | 12984 | =end original |
13136 | 12985 | |
13137 | 12986 | (これは、NFS のもとでデバイス番号が負になるマシンで |
13138 | 12987 | のみ動作します。) |
13139 | 12988 | |
13140 | 12989 | =begin original |
13141 | 12990 | |
13142 | 12991 | Because the mode contains both the file type and its permissions, you |
13143 | 12992 | should mask off the file type portion and (s)printf using a C<"%o"> |
13144 | 12993 | if you want to see the real permissions. |
13145 | 12994 | |
13146 | 12995 | =end original |
13147 | 12996 | |
13148 | 12997 | モードにはファイルタイプとその権限の両方が含まれているので、 |
13149 | 12998 | 本当の権限を見たい場合は、(s)printf で C<"%"> を使うことで |
13150 | 12999 | ファイルタイプをマスクするべきです。 |
13151 | 13000 | |
13152 | 13001 | $mode = (stat($filename))[2]; |
13153 | 13002 | printf "Permissions are %04o\n", $mode & 07777; |
13154 | 13003 | |
13155 | 13004 | =begin original |
13156 | 13005 | |
13157 | 13006 | In scalar context, C<stat> returns a boolean value indicating success |
13158 | 13007 | or failure, and, if successful, sets the information associated with |
13159 | 13008 | the special filehandle C<_>. |
13160 | 13009 | |
13161 | 13010 | =end original |
13162 | 13011 | |
13163 | 13012 | スカラコンテキストでは、C<stat> は成功か失敗を表す真偽値を返し、 |
13164 | 13013 | 成功した場合は、特別なファイルハンドル C<_> に結び付けられた |
13165 | 13014 | 情報をセットします。 |
13166 | 13015 | |
13167 | 13016 | =begin original |
13168 | 13017 | |
13169 | 13018 | The L<File::stat> module provides a convenient, by-name access mechanism: |
13170 | 13019 | |
13171 | 13020 | =end original |
13172 | 13021 | |
13173 | 13022 | L<File::stat> モジュールは、便利な名前によるアクセス機構を提供します。 |
13174 | 13023 | |
13175 | 13024 | use File::stat; |
13176 | 13025 | $sb = stat($filename); |
13177 | 13026 | printf "File is %s, size is %s, perm %04o, mtime %s\n", |
13178 | 13027 | $filename, $sb->size, $sb->mode & 07777, |
13179 | 13028 | scalar localtime $sb->mtime; |
13180 | 13029 | |
13181 | 13030 | =begin original |
13182 | 13031 | |
13183 | 13032 | You can import symbolic mode constants (C<S_IF*>) and functions |
13184 | 13033 | (C<S_IS*>) from the Fcntl module: |
13185 | 13034 | |
13186 | 13035 | =end original |
13187 | 13036 | |
13188 | 13037 | モード定数 (C<S_IF*>) と関数 (C<S_IS*>) を Fcntl モジュールから |
13189 | 13038 | インポートできます。 |
13190 | 13039 | |
13191 | 13040 | use Fcntl ':mode'; |
13192 | 13041 | |
13193 | 13042 | $mode = (stat($filename))[2]; |
13194 | 13043 | |
13195 | 13044 | $user_rwx = ($mode & S_IRWXU) >> 6; |
13196 | 13045 | $group_read = ($mode & S_IRGRP) >> 3; |
13197 | 13046 | $other_execute = $mode & S_IXOTH; |
13198 | 13047 | |
13199 | 13048 | printf "Permissions are %04o\n", S_IMODE($mode), "\n"; |
13200 | 13049 | |
13201 | 13050 | $is_setuid = $mode & S_ISUID; |
13202 | 13051 | $is_directory = S_ISDIR($mode); |
13203 | 13052 | |
13204 | 13053 | =begin original |
13205 | 13054 | |
13206 | 13055 | You could write the last two using the C<-u> and C<-d> operators. |
13207 | 13056 | The commonly available C<S_IF*> constants are |
13208 | 13057 | |
13209 | 13058 | =end original |
13210 | 13059 | |
13211 | 13060 | 最後の二つは C<-u> と C<-d> 演算子を使っても書けます。 |
13212 | 13061 | 一般に利用可能な C<S_IF*> 定数は以下のものです。 |
13213 | 13062 | |
13214 | 13063 | # Permissions: read, write, execute, for user, group, others. |
13215 | 13064 | |
13216 | 13065 | S_IRWXU S_IRUSR S_IWUSR S_IXUSR |
13217 | 13066 | S_IRWXG S_IRGRP S_IWGRP S_IXGRP |
13218 | 13067 | S_IRWXO S_IROTH S_IWOTH S_IXOTH |
13219 | 13068 | |
13220 | 13069 | # Setuid/Setgid/Stickiness/SaveText. |
13221 | 13070 | # Note that the exact meaning of these is system dependent. |
13222 | 13071 | |
13223 | 13072 | S_ISUID S_ISGID S_ISVTX S_ISTXT |
13224 | 13073 | |
13225 | 13074 | # File types. Not necessarily all are available on your system. |
13226 | 13075 | |
13227 | 13076 | S_IFREG S_IFDIR S_IFLNK S_IFBLK S_IFCHR S_IFIFO S_IFSOCK S_IFWHT S_ENFMT |
13228 | 13077 | |
13229 | 13078 | # The following are compatibility aliases for S_IRUSR, S_IWUSR, S_IXUSR. |
13230 | 13079 | |
13231 | 13080 | S_IREAD S_IWRITE S_IEXEC |
13232 | 13081 | |
13233 | 13082 | =begin original |
13234 | 13083 | |
13235 | 13084 | and the C<S_IF*> functions are |
13236 | 13085 | |
13237 | 13086 | =end original |
13238 | 13087 | |
13239 | 13088 | 一般に利用可能な C<S_IF*> 関数は以下のものです。 |
13240 | 13089 | |
13241 | 13090 | S_IMODE($mode) the part of $mode containing the permission bits |
13242 | 13091 | and the setuid/setgid/sticky bits |
13243 | 13092 | |
13244 | 13093 | S_IFMT($mode) the part of $mode containing the file type |
13245 | 13094 | which can be bit-anded with e.g. S_IFREG |
13246 | 13095 | or with the following functions |
13247 | 13096 | |
13248 | 13097 | # The operators -f, -d, -l, -b, -c, -p, and -S. |
13249 | 13098 | |
13250 | 13099 | S_ISREG($mode) S_ISDIR($mode) S_ISLNK($mode) |
13251 | 13100 | S_ISBLK($mode) S_ISCHR($mode) S_ISFIFO($mode) S_ISSOCK($mode) |
13252 | 13101 | |
13253 | 13102 | # No direct -X operator counterpart, but for the first one |
13254 | 13103 | # the -g operator is often equivalent. The ENFMT stands for |
13255 | 13104 | # record flocking enforcement, a platform-dependent feature. |
13256 | 13105 | |
13257 | 13106 | S_ISENFMT($mode) S_ISWHT($mode) |
13258 | 13107 | |
13259 | 13108 | =begin original |
13260 | 13109 | |
13261 | 13110 | See your native chmod(2) and stat(2) documentation for more details |
13262 | 13111 | about the C<S_*> constants. To get status info for a symbolic link |
13263 | 13112 | instead of the target file behind the link, use the C<lstat> function. |
13264 | 13113 | |
13265 | 13114 | =end original |
13266 | 13115 | |
13267 | 13116 | C<S_*> 定数に関する詳細についてはネイティブの chmod(2) と stat(2) の |
13268 | 13117 | ドキュメントを参照して下さい。 |
13269 | 13118 | リンクの先にあるファイルではなく、シンボリックリンクそのものの情報を |
13270 | 13119 | 得たい場合は、C<lstat> 関数を使ってください。 |
13271 | 13120 | |
13272 | 13121 | =item state EXPR |
13273 | 13122 | X<state> |
13274 | 13123 | |
13275 | 13124 | =item state TYPE EXPR |
13276 | 13125 | |
13277 | 13126 | =item state EXPR : ATTRS |
13278 | 13127 | |
13279 | 13128 | =item state TYPE EXPR : ATTRS |
13280 | 13129 | |
13281 | 13130 | =begin original |
13282 | 13131 | |
13283 | 13132 | C<state> declares a lexically scoped variable, just like C<my> does. |
13284 | 13133 | However, those variables will never be reinitialized, contrary to |
13285 | 13134 | lexical variables that are reinitialized each time their enclosing block |
13286 | 13135 | is entered. |
13287 | 13136 | |
13288 | 13137 | =end original |
13289 | 13138 | |
13290 | 13139 | C<state> は C<my> と同様に、レキシカルスコープの変数を宣言します。 |
13291 | 13140 | しかし、ブロックに入る毎に再初期化されるレキシカル変数と違って、 |
13292 | 13141 | これらの変数は決して再初期化されません。 |
13293 | 13142 | |
13294 | 13143 | =begin original |
13295 | 13144 | |
13296 | 13145 | C<state> variables are only enabled when the C<feature 'state'> pragma is |
13297 | 13146 | in effect. See L<feature>. |
13298 | 13147 | |
13299 | 13148 | =end original |
13300 | 13149 | |
13301 | 13150 | C<state> 変数は C<feature 'state'> プラグマが有効の場合のみ有効です。 |
13302 | 13151 | L<feature> を参照してください。 |
13303 | 13152 | |
13304 | 13153 | =item study SCALAR |
13305 | 13154 | X<study> |
13306 | 13155 | |
13307 | 13156 | =item study |
13308 | 13157 | |
13309 | 13158 | =begin original |
13310 | 13159 | |
13311 | 13160 | Takes extra time to study SCALAR (C<$_> if unspecified) in anticipation of |
13312 | 13161 | doing many pattern matches on the string before it is next modified. |
13313 | 13162 | This may or may not save time, depending on the nature and number of |
13314 | 13163 | patterns you are searching on, and on the distribution of character |
13315 | 13164 | frequencies in the string to be searched--you probably want to compare |
13316 | 13165 | run times with and without it to see which runs faster. Those loops |
13317 | 13166 | that scan for many short constant strings (including the constant |
13318 | 13167 | parts of more complex patterns) will benefit most. You may have only |
13319 | 13168 | one C<study> active at a time--if you study a different scalar the first |
13320 | 13169 | is "unstudied". (The way C<study> works is this: a linked list of every |
13321 | 13170 | character in the string to be searched is made, so we know, for |
13322 | 13171 | example, where all the C<'k'> characters are. From each search string, |
13323 | 13172 | the rarest character is selected, based on some static frequency tables |
13324 | 13173 | constructed from some C programs and English text. Only those places |
13325 | 13174 | that contain this "rarest" character are examined.) |
13326 | 13175 | |
13327 | 13176 | =end original |
13328 | 13177 | |
13329 | 13178 | 次に変更される前に、何回も文字列に対するパターンマッチを |
13330 | 13179 | 行なうアプリケーションで、 |
13331 | 13180 | そのような文字列 SCALAR(省略時には C<$_>) を予め学習しておきます。 |
13332 | 13181 | これは、検索のために、どのようなパターンを何回使うかによって、 |
13333 | 13182 | また、検索される文字列内の文字頻度の分布によって、 |
13334 | 13183 | 時間を節約することになるかもしれませんし、逆に浪費する |
13335 | 13184 | ことになるかもしれません。 |
13336 | 13185 | 予習をした場合と、しない場合の実行時間を比較して、 |
13337 | 13186 | どちらが速いか調べることが、必要でしょう。 |
13338 | 13187 | 短い固定文字列 (複雑なパターンの固定部分を含みます) をたくさん |
13339 | 13188 | 検索するループで、もっとも効果があるでしょう。 |
13340 | 13189 | 同時には、一つの C<study>だけが有効です。 |
13341 | 13190 | 別のスカラを study した場合には、以前に学習した内容は |
13342 | 13191 | 「忘却」されてしまいます。 |
13343 | 13192 | (この C<study> の仕組みは、まず、検索される文字列内の |
13344 | 13193 | すべての文字のリンクされたリストが作られ、たとえば、 |
13345 | 13194 | すべての C<'k'> がどこにあるかがわかるようになります。 |
13346 | 13195 | 各々の検索文字列から、C プログラムや英語のテキストから作られた |
13347 | 13196 | 頻度の統計情報に基づいて、もっとも珍しい文字が選ばれます。 |
13348 | 13197 | この「珍しい」文字を含む場所だけが調べられるのです。) |
13349 | 13198 | |
13350 | 13199 | =begin original |
13351 | 13200 | |
13352 | 13201 | For example, here is a loop that inserts index producing entries |
13353 | 13202 | before any line containing a certain pattern: |
13354 | 13203 | |
13355 | 13204 | =end original |
13356 | 13205 | |
13357 | 13206 | たとえば、特定のパターンを含む行の前にインデックスを |
13358 | 13207 | 付けるエントリを入れる例を示します。 |
13359 | 13208 | |
13360 | 13209 | while (<>) { |
13361 | 13210 | study; |
13362 | 13211 | print ".IX foo\n" if /\bfoo\b/; |
13363 | 13212 | print ".IX bar\n" if /\bbar\b/; |
13364 | 13213 | print ".IX blurfl\n" if /\bblurfl\b/; |
13365 | 13214 | # ... |
13366 | 13215 | print; |
13367 | 13216 | } |
13368 | 13217 | |
13369 | 13218 | =begin original |
13370 | 13219 | |
13371 | 13220 | In searching for C</\bfoo\b/>, only those locations in C<$_> that contain C<f> |
13372 | 13221 | will be looked at, because C<f> is rarer than C<o>. In general, this is |
13373 | 13222 | a big win except in pathological cases. The only question is whether |
13374 | 13223 | it saves you more time than it took to build the linked list in the |
13375 | 13224 | first place. |
13376 | 13225 | |
13377 | 13226 | =end original |
13378 | 13227 | |
13379 | 13228 | C<f> は C<o> よりも珍しいので、C</\bfoo\b/> を探すとき、C<$_> で C<f> を |
13380 | 13229 | 含む場所だけが探されます。 |
13381 | 13230 | 一般に、病的な場合を除いて、かなりの結果が得られます。 |
13382 | 13231 | 唯一の問題は、節約できる時間が、最初にリンクリストを作る |
13383 | 13232 | 時間よりも多いかどうかです、 |
13384 | 13233 | |
13385 | 13234 | =begin original |
13386 | 13235 | |
13387 | 13236 | Note that if you have to look for strings that you don't know till |
13388 | 13237 | runtime, you can build an entire loop as a string and C<eval> that to |
13389 | 13238 | avoid recompiling all your patterns all the time. Together with |
13390 | 13239 | undefining C<$/> to input entire files as one record, this can be very |
13391 | 13240 | fast, often faster than specialized programs like fgrep(1). The following |
13392 | 13241 | scans a list of files (C<@files>) for a list of words (C<@words>), and prints |
13393 | 13242 | out the names of those files that contain a match: |
13394 | 13243 | |
13395 | 13244 | =end original |
13396 | 13245 | |
13397 | 13246 | 実行時まで、探そうとする文字列がわからないときには、 |
13398 | 13247 | ループ全体を文字列として組み立てて、C<eval> すれば、 |
13399 | 13248 | いつも、すべてのパターンを再コンパイルするという事態は避けられます。 |
13400 | 13249 | ファイル全体を一つのレコードとして入力するために、 |
13401 | 13250 | C<$/> を未定義にすれば、かなり速くなり、 |
13402 | 13251 | 多くの場合 fgrep(1) のような専用のプログラムより速くなります。 |
13403 | 13252 | 以下の例は、ファイルのリスト (C<@files>) から単語のリスト (C<@words>) を |
13404 | 13253 | 探して、マッチするものがあったファイル名を出力します。 |
13405 | 13254 | |
13406 | 13255 | $search = 'while (<>) { study;'; |
13407 | 13256 | foreach $word (@words) { |
13408 | 13257 | $search .= "++\$seen{\$ARGV} if /\\b$word\\b/;\n"; |
13409 | 13258 | } |
13410 | 13259 | $search .= "}"; |
13411 | 13260 | @ARGV = @files; |
13412 | 13261 | undef $/; |
13413 | 13262 | eval $search; # this screams |
13414 | 13263 | $/ = "\n"; # put back to normal input delimiter |
13415 | 13264 | foreach $file (sort keys(%seen)) { |
13416 | 13265 | print $file, "\n"; |
13417 | 13266 | } |
13418 | 13267 | |
13419 | 13268 | =item sub NAME BLOCK |
13420 | 13269 | X<sub> |
13421 | 13270 | |
13422 | 13271 | =item sub NAME (PROTO) BLOCK |
13423 | 13272 | |
13424 | 13273 | =item sub NAME : ATTRS BLOCK |
13425 | 13274 | |
13426 | 13275 | =item sub NAME (PROTO) : ATTRS BLOCK |
13427 | 13276 | |
13428 | 13277 | =begin original |
13429 | 13278 | |
13430 | 13279 | This is subroutine definition, not a real function I<per se>. |
13431 | 13280 | Without a BLOCK it's just a forward declaration. Without a NAME, |
13432 | 13281 | it's an anonymous function declaration, and does actually return |
13433 | 13282 | a value: the CODE ref of the closure you just created. |
13434 | 13283 | |
13435 | 13284 | =end original |
13436 | 13285 | |
13437 | 13286 | これはサブルーチン定義であり、I<本質的には> 実際の関数ではありません。 |
13438 | 13287 | NAME なしの場合は、無名関数定義であり、実際には値(作成したブロックの |
13439 | 13288 | コードリファレンス)を返します。 |
13440 | 13289 | |
13441 | 13290 | =begin original |
13442 | 13291 | |
13443 | 13292 | See L<perlsub> and L<perlref> for details about subroutines and |
13444 | 13293 | references, and L<attributes> and L<Attribute::Handlers> for more |
13445 | 13294 | information about attributes. |
13446 | 13295 | |
13447 | 13296 | =end original |
13448 | 13297 | |
13449 | 13298 | サブルーチンとリファレンスに関する詳細については、L<perlsub> と |
13450 | 13299 | L<perlref> を、属性に関する更なる情報については L<attributes> と |
13451 | 13300 | L<Attribute::Handlers> を参照してください。 |
13452 | 13301 | |
13453 | 13302 | =item substr EXPR,OFFSET,LENGTH,REPLACEMENT |
13454 | 13303 | X<substr> X<substring> X<mid> X<left> X<right> |
13455 | 13304 | |
13456 | 13305 | =item substr EXPR,OFFSET,LENGTH |
13457 | 13306 | |
13458 | 13307 | =item substr EXPR,OFFSET |
13459 | 13308 | |
13460 | 13309 | =begin original |
13461 | 13310 | |
13462 | 13311 | Extracts a substring out of EXPR and returns it. First character is at |
13463 | 13312 | offset C<0>, or whatever you've set C<$[> to (but don't do that). |
13464 | 13313 | If OFFSET is negative (or more precisely, less than C<$[>), starts |
13465 | 13314 | that far from the end of the string. If LENGTH is omitted, returns |
13466 | 13315 | everything to the end of the string. If LENGTH is negative, leaves that |
13467 | 13316 | many characters off the end of the string. |
13468 | 13317 | |
13469 | 13318 | =end original |
13470 | 13319 | |
13471 | 13320 | EXPR から、部分文字列を取り出して返します。 |
13472 | 13321 | 最初の文字がオフセット C<0> もしくは、C<$[> に設定した値 |
13473 | 13322 | (しかしこれを使ってはいけません)となります。 |
13474 | 13323 | OFFSET に負の値(より厳密には、C<$[>より小さい値)を設定すると、 |
13475 | 13324 | EXPR の終わりからのオフセットとなります。 |
13476 | 13325 | LENGTH を省略すると、EXPR の最後まですべてが返されます。 |
13477 | 13326 | LENGTH が負の値だと、文字列の最後から指定された数だけ文字を取り除きます。 |
13478 | 13327 | |
13479 | 13328 | my $s = "The black cat climbed the green tree"; |
13480 | 13329 | my $color = substr $s, 4, 5; # black |
13481 | 13330 | my $middle = substr $s, 4, -11; # black cat climbed the |
13482 | 13331 | my $end = substr $s, 14; # climbed the green tree |
13483 | 13332 | my $tail = substr $s, -4; # tree |
13484 | 13333 | my $z = substr $s, -4, 2; # tr |
13485 | 13334 | |
13486 | 13335 | =begin original |
13487 | 13336 | |
13488 | 13337 | You can use the substr() function as an lvalue, in which case EXPR |
13489 | 13338 | must itself be an lvalue. If you assign something shorter than LENGTH, |
13490 | 13339 | the string will shrink, and if you assign something longer than LENGTH, |
13491 | 13340 | the string will grow to accommodate it. To keep the string the same |
13492 | 13341 | length you may need to pad or chop your value using C<sprintf>. |
13493 | 13342 | |
13494 | 13343 | =end original |
13495 | 13344 | |
13496 | 13345 | substr() を左辺値として使用することも可能で、その場合には、 |
13497 | 13346 | EXPR が自身左辺値でなければなりません。 |
13498 | 13347 | LENGTH より短いものを代入したときには、 |
13499 | 13348 | EXPR は短くなり、LENGTH より長いものを代入したときには、 |
13500 | 13349 | EXPR はそれに合わせて伸びることになります。 |
13501 | 13350 | EXPR の長さを一定に保つためには、C<sprintf> を使って、 |
13502 | 13351 | 代入する値の長さを調整することが、必要になるかもしれません。 |
13503 | 13352 | |
13504 | 13353 | =begin original |
13505 | 13354 | |
13506 | 13355 | If OFFSET and LENGTH specify a substring that is partly outside the |
13507 | 13356 | string, only the part within the string is returned. If the substring |
13508 | 13357 | is beyond either end of the string, substr() returns the undefined |
13509 | 13358 | value and produces a warning. When used as an lvalue, specifying a |
13510 | 13359 | substring that is entirely outside the string is a fatal error. |
13511 | 13360 | Here's an example showing the behavior for boundary cases: |
13512 | 13361 | |
13513 | 13362 | =end original |
13514 | 13363 | |
13515 | 13364 | OFFSET と LENGTH として文字列の外側を含むような部分文字列が指定されると、 |
13516 | 13365 | 文字列の内側の部分だけが返されます。 |
13517 | 13366 | 部分文字列が文字列の両端の外側の場合、substr() は未定義値を返し、 |
13518 | 13367 | 警告が出力されます。 |
13519 | 13368 | 左辺値として使った場合、文字列の完全に外側を部分文字列として指定すると |
13520 | 13369 | 致命的エラーになります。 |
13521 | 13370 | 以下は境界条件の振る舞いを示す例です: |
13522 | 13371 | |
13523 | 13372 | my $name = 'fred'; |
13524 | 13373 | substr($name, 4) = 'dy'; # $name is now 'freddy' |
13525 | 13374 | my $null = substr $name, 6, 2; # returns '' (no warning) |
13526 | 13375 | my $oops = substr $name, 7; # returns undef, with warning |
13527 | 13376 | substr($name, 7) = 'gap'; # fatal error |
13528 | 13377 | |
13529 | 13378 | =begin original |
13530 | 13379 | |
13531 | 13380 | An alternative to using substr() as an lvalue is to specify the |
13532 | 13381 | replacement string as the 4th argument. This allows you to replace |
13533 | 13382 | parts of the EXPR and return what was there before in one operation, |
13534 | 13383 | just as you can with splice(). |
13535 | 13384 | |
13536 | 13385 | =end original |
13537 | 13386 | |
13538 | 13387 | substr() を左辺値として使う代わりの方法は、置き換える文字列を 4 番目の |
13539 | 13388 | 引数として指定することです。 |
13540 | 13389 | これにより、EXPR の一部を置き換え、置き換える前が何であったかを返す、 |
13541 | 13390 | ということを(splice() と同様) 1 動作で行えます。 |
13542 | 13391 | |
13543 | 13392 | my $s = "The black cat climbed the green tree"; |
13544 | 13393 | my $z = substr $s, 14, 7, "jumped from"; # climbed |
13545 | 13394 | # $s is now "The black cat jumped from the green tree" |
13546 | 13395 | |
13547 | 13396 | =begin original |
13548 | 13397 | |
13549 | 13398 | Note that the lvalue returned by the 3-arg version of substr() acts as |
13550 | 13399 | a 'magic bullet'; each time it is assigned to, it remembers which part |
13551 | 13400 | of the original string is being modified; for example: |
13552 | 13401 | |
13553 | 13402 | =end original |
13554 | 13403 | |
13555 | 13404 | 3 引数の substr() によって返された左辺値は「魔法の弾丸」のように振舞うことに |
13556 | 13405 | 注意してください; これが代入される毎に、元の文字列のどの部分が変更されたかが |
13557 | 13406 | 思い出されます; 例えば: |
13558 | 13407 | |
13559 | 13408 | $x = '1234'; |
13560 | 13409 | for (substr($x,1,2)) { |
13561 | 13410 | $_ = 'a'; print $x,"\n"; # prints 1a4 |
13562 | 13411 | $_ = 'xyz'; print $x,"\n"; # prints 1xyz4 |
13563 | 13412 | $x = '56789'; |
13564 | 13413 | $_ = 'pq'; print $x,"\n"; # prints 5pq9 |
13565 | 13414 | } |
13566 | 13415 | |
13567 | 13416 | =begin original |
13568 | 13417 | |
13569 | 13418 | Prior to Perl version 5.9.1, the result of using an lvalue multiple times was |
13570 | 13419 | unspecified. |
13571 | 13420 | |
13572 | 13421 | =end original |
13573 | 13422 | |
13574 | 13423 | バージョン 5.9.1 以前の Perl では、複数回左辺値を使った場合の結果は |
13575 | 13424 | 未定義でした。 |
13576 | 13425 | |
13577 | 13426 | =item symlink OLDFILE,NEWFILE |
13578 | 13427 | X<symlink> X<link> X<symbolic link> X<link, symbolic> |
13579 | 13428 | |
13580 | 13429 | =begin original |
13581 | 13430 | |
13582 | 13431 | Creates a new filename symbolically linked to the old filename. |
13583 | 13432 | Returns C<1> for success, C<0> otherwise. On systems that don't support |
13584 | 13433 | symbolic links, produces a fatal error at run time. To check for that, |
13585 | 13434 | use eval: |
13586 | 13435 | |
13587 | 13436 | =end original |
13588 | 13437 | |
13589 | 13438 | NEWFILE として、OLDFILE へのシンボリックリンクを生成します。 |
13590 | 13439 | 成功時には C<1> を返し、失敗時には C<0> を返します。 |
13591 | 13440 | シンボリックリンクをサポートしていないシステムでは、 |
13592 | 13441 | 実行時に致命的エラーが発生します。 |
13593 | 13442 | これをチェックするには、eval を使用します: |
13594 | 13443 | |
13595 | 13444 | $symlink_exists = eval { symlink("",""); 1 }; |
13596 | 13445 | |
13597 | 13446 | =item syscall NUMBER, LIST |
13598 | 13447 | X<syscall> X<system call> |
13599 | 13448 | |
13600 | 13449 | =begin original |
13601 | 13450 | |
13602 | 13451 | Calls the system call specified as the first element of the list, |
13603 | 13452 | passing the remaining elements as arguments to the system call. If |
13604 | 13453 | unimplemented, produces a fatal error. The arguments are interpreted |
13605 | 13454 | as follows: if a given argument is numeric, the argument is passed as |
13606 | 13455 | an int. If not, the pointer to the string value is passed. You are |
13607 | 13456 | responsible to make sure a string is pre-extended long enough to |
13608 | 13457 | receive any result that might be written into a string. You can't use a |
13609 | 13458 | string literal (or other read-only string) as an argument to C<syscall> |
13610 | 13459 | because Perl has to assume that any string pointer might be written |
13611 | 13460 | through. If your |
13612 | 13461 | integer arguments are not literals and have never been interpreted in a |
13613 | 13462 | numeric context, you may need to add C<0> to them to force them to look |
13614 | 13463 | like numbers. This emulates the C<syswrite> function (or vice versa): |
13615 | 13464 | |
13616 | 13465 | =end original |
13617 | 13466 | |
13618 | 13467 | LIST の最初の要素で指定するシステムコールを、残りの要素をその |
13619 | 13468 | システムコールの引数として呼び出します。 |
13620 | 13469 | 実装されていないときには、致命的エラーとなります。 |
13621 | 13470 | 引数は、以下のように解釈されます: 引数が数字であれば、int として |
13622 | 13471 | 引数を渡します。 |
13623 | 13472 | そうでなければ、文字列値へのポインタが渡されます。 |
13624 | 13473 | 文字列に結果を受け取るときには、その結果を受け取るのに十分なくらいに、 |
13625 | 13474 | 文字列を予め伸ばしておく必要があります。 |
13626 | 13475 | 文字列リテラル(あるいはその他の読み込み専用の文字列)を C<syscall> の |
13627 | 13476 | 引数として使うことはできません。 |
13628 | 13477 | Perl は全ての文字列ポインタは書き込まれると仮定しなければならないからです。 |
13629 | 13478 | 整数引数が、リテラルでなく、数値コンテキストで評価されたことの |
13630 | 13479 | ないものであれば、数値として解釈されるように、 |
13631 | 13480 | C<0> を足しておく必要があるかもしれません。 |
13632 | 13481 | 以下は C<syswrite> 関数(あるいはその逆)をエミュレートします。 |
13633 | 13482 | |
13634 | 13483 | require 'syscall.ph'; # may need to run h2ph |
13635 | 13484 | $s = "hi there\n"; |
13636 | 13485 | syscall(&SYS_write, fileno(STDOUT), $s, length $s); |
13637 | 13486 | |
13638 | 13487 | =begin original |
13639 | 13488 | |
13640 | 13489 | Note that Perl supports passing of up to only 14 arguments to your system call, |
13641 | 13490 | which in practice should usually suffice. |
13642 | 13491 | |
13643 | 13492 | =end original |
13644 | 13493 | |
13645 | 13494 | Perl は、システムコールに最大 14 個の引数しか渡せませんが、 |
13646 | 13495 | 実用上問題はないでしょう。 |
13647 | 13496 | |
13648 | 13497 | =begin original |
13649 | 13498 | |
13650 | 13499 | Syscall returns whatever value returned by the system call it calls. |
13651 | 13500 | If the system call fails, C<syscall> returns C<-1> and sets C<$!> (errno). |
13652 | 13501 | Note that some system calls can legitimately return C<-1>. The proper |
13653 | 13502 | way to handle such calls is to assign C<$!=0;> before the call and |
13654 | 13503 | check the value of C<$!> if syscall returns C<-1>. |
13655 | 13504 | |
13656 | 13505 | =end original |
13657 | 13506 | |
13658 | 13507 | syscall は、呼び出したシステムコールが返した値を返します。 |
13659 | 13508 | システムコールが失敗すると、C<syscall> は C<-1> を返し、 |
13660 | 13509 | C<$!>(errno) を設定します。 |
13661 | 13510 | システムコールが正常に C<-1> を返す場合があることに注意してください。 |
13662 | 13511 | このようなシステムコールを正しく扱うには、 |
13663 | 13512 | C<$!=0;> をシステムコールの前に実行し、 |
13664 | 13513 | syscall が C<-1> を返した時には C<$!> の値を調べてください。 |
13665 | 13514 | |
13666 | 13515 | =begin original |
13667 | 13516 | |
13668 | 13517 | There's a problem with C<syscall(&SYS_pipe)>: it returns the file |
13669 | 13518 | number of the read end of the pipe it creates. There is no way |
13670 | 13519 | to retrieve the file number of the other end. You can avoid this |
13671 | 13520 | problem by using C<pipe> instead. |
13672 | 13521 | |
13673 | 13522 | =end original |
13674 | 13523 | |
13675 | 13524 | C<syscall(&SYS_pipe)> には問題があり、 |
13676 | 13525 | 作ったパイプの、読み出し側のファイル番号を返しますが、 |
13677 | 13526 | もう一方のファイル番号を得る方法がありません。 |
13678 | 13527 | この問題を避けるためには、代わりに C<pipe> を使ってください。 |
13679 | 13528 | |
13680 | 13529 | =item sysopen FILEHANDLE,FILENAME,MODE |
13681 | 13530 | X<sysopen> |
13682 | 13531 | |
13683 | 13532 | =item sysopen FILEHANDLE,FILENAME,MODE,PERMS |
13684 | 13533 | |
13685 | 13534 | =begin original |
13686 | 13535 | |
13687 | 13536 | Opens the file whose filename is given by FILENAME, and associates it |
13688 | 13537 | with FILEHANDLE. If FILEHANDLE is an expression, its value is used as |
13689 | 13538 | the name of the real filehandle wanted. This function calls the |
13690 | 13539 | underlying operating system's C<open> function with the parameters |
13691 | 13540 | FILENAME, MODE, PERMS. |
13692 | 13541 | |
13693 | 13542 | =end original |
13694 | 13543 | |
13695 | 13544 | FILENAME で与えられたファイル名のファイルをオープンし、 |
13696 | 13545 | FILEHANDLE と結び付けます。 |
13697 | 13546 | FILEHANDLE が式の場合、その値は実際に求めているファイルハンドルの名前として |
13698 | 13547 | 扱われます。 |
13699 | 13548 | この関数呼び出しはシステムの C<open> 関数を FILENAME, MODE, PERMS の |
13700 | 13549 | 引数で呼び出すことを基礎としています。 |
13701 | 13550 | |
13702 | 13551 | =begin original |
13703 | 13552 | |
13704 | 13553 | The possible values and flag bits of the MODE parameter are |
13705 | 13554 | system-dependent; they are available via the standard module C<Fcntl>. |
13706 | 13555 | See the documentation of your operating system's C<open> to see which |
13707 | 13556 | values and flag bits are available. You may combine several flags |
13708 | 13557 | using the C<|>-operator. |
13709 | 13558 | |
13710 | 13559 | =end original |
13711 | 13560 | |
13712 | 13561 | MODE パラメータに指定できるフラグビットと値はシステム依存です; |
13713 | 13562 | これは標準モジュール C<Fcntl> 経由で利用可能です。 |
13714 | 13563 | どのようなフラグビットと値が利用可能であるかについては、 |
13715 | 13564 | OS の C<open> に関する文書を参照してください。 |
13716 | 13565 | C<|> 演算子を使って複数のフラグを結合することができます。 |
13717 | 13566 | |
13718 | 13567 | =begin original |
13719 | 13568 | |
13720 | 13569 | Some of the most common values are C<O_RDONLY> for opening the file in |
13721 | 13570 | read-only mode, C<O_WRONLY> for opening the file in write-only mode, |
13722 | 13571 | and C<O_RDWR> for opening the file in read-write mode. |
13723 | 13572 | X<O_RDONLY> X<O_RDWR> X<O_WRONLY> |
13724 | 13573 | |
13725 | 13574 | =end original |
13726 | 13575 | |
13727 | 13576 | もっともよく使われる値は、ファイルを読み込み専用で開く C<O_RDONLY>、 |
13728 | 13577 | ファイルを書き込み専用で開く C<O_WRONLY>、 |
13729 | 13578 | ファイルを読み書き両用で開く C<O_RDWR> です。 |
13730 | 13579 | X<O_RDONLY> X<O_RDWR> X<O_WRONLY> |
13731 | 13580 | |
13732 | 13581 | =begin original |
13733 | 13582 | |
13734 | 13583 | For historical reasons, some values work on almost every system |
13735 | 13584 | supported by perl: zero means read-only, one means write-only, and two |
13736 | 13585 | means read/write. We know that these values do I<not> work under |
13737 | 13586 | OS/390 & VM/ESA Unix and on the Macintosh; you probably don't want to |
13738 | 13587 | use them in new code. |
13739 | 13588 | |
13740 | 13589 | =end original |
13741 | 13590 | |
13742 | 13591 | 歴史的な理由により、perl が対応しているほとんどのシステムで |
13743 | 13592 | 使える値があります。 |
13744 | 13593 | 0 は読み込み専用、1 は書き込み専用、2 は読み書き両用を意味します。 |
13745 | 13594 | OS/390 & VM/ESA Unix と Macintosh では動作 I<しない> ことが分かっています; |
13746 | 13595 | 新しく書くコードではこれらは使わないほうがよいでしょう。 |
13747 | 13596 | |
13748 | 13597 | =begin original |
13749 | 13598 | |
13750 | 13599 | If the file named by FILENAME does not exist and the C<open> call creates |
13751 | 13600 | it (typically because MODE includes the C<O_CREAT> flag), then the value of |
13752 | 13601 | PERMS specifies the permissions of the newly created file. If you omit |
13753 | 13602 | the PERMS argument to C<sysopen>, Perl uses the octal value C<0666>. |
13754 | 13603 | These permission values need to be in octal, and are modified by your |
13755 | 13604 | process's current C<umask>. |
13756 | 13605 | X<O_CREAT> |
13757 | 13606 | |
13758 | 13607 | =end original |
13759 | 13608 | |
13760 | 13609 | FILENAME という名前のファイルが存在せず、(典型的には MODE が |
13761 | 13610 | C<O_CREAT> フラグを含んでいたために) C<open> 呼び出しがそれを作った場合、 |
13762 | 13611 | PERMS の値は新しく作られたファイルの権限を指定します。 |
13763 | 13612 | C<sysopen> の PERMS 引数を省略した場合、Perl は 8 進数 C<0666> を使います。 |
13764 | 13613 | これらの権限は 8 進数である必要があり、プロセスの現在の C<umask> で |
13765 | 13614 | 修正されます。 |
13766 | 13615 | |
13767 | 13616 | =begin original |
13768 | 13617 | |
13769 | 13618 | In many systems the C<O_EXCL> flag is available for opening files in |
13770 | 13619 | exclusive mode. This is B<not> locking: exclusiveness means here that |
13771 | 13620 | if the file already exists, sysopen() fails. C<O_EXCL> may not work |
13772 | 13621 | on network filesystems, and has no effect unless the C<O_CREAT> flag |
13773 | 13622 | is set as well. Setting C<O_CREAT|O_EXCL> prevents the file from |
13774 | 13623 | being opened if it is a symbolic link. It does not protect against |
13775 | 13624 | symbolic links in the file's path. |
13776 | 13625 | X<O_EXCL> |
13777 | 13626 | |
13778 | 13627 | =end original |
13779 | 13628 | |
13780 | 13629 | 多くのシステムではファイルを排他モードで開くために C<O_EXCL> が |
13781 | 13630 | 利用可能です。 |
13782 | 13631 | これはロック B<ではありません>: 排他性というのは既にファイルが |
13783 | 13632 | 存在していた場合、sysopen() が失敗することを意味します。 |
13784 | 13633 | C<O_EXCL> はネットワークファイルシステムでは動作せず、 |
13785 | 13634 | またC<O_CREAT> フラグも有効でない限りは効果がありません。 |
13786 | 13635 | C<O_CREAT|O_EXCL> をセットすると、これがシンボリックリンクだった場合は |
13787 | 13636 | ファイルを開くことを妨げます。 |
13788 | 13637 | これはファイルパス中のシンボリックリンクは守りません。 |
13789 | 13638 | X<O_EXCL> |
13790 | 13639 | |
13791 | 13640 | =begin original |
13792 | 13641 | |
13793 | 13642 | Sometimes you may want to truncate an already-existing file. This |
13794 | 13643 | can be done using the C<O_TRUNC> flag. The behavior of |
13795 | 13644 | C<O_TRUNC> with C<O_RDONLY> is undefined. |
13796 | 13645 | X<O_TRUNC> |
13797 | 13646 | |
13798 | 13647 | =end original |
13799 | 13648 | |
13800 | 13649 | 既に存在しているファイルを切り詰めたい場合もあるかもしれません。 |
13801 | 13650 | これは C<O_TRUNC> フラグを使うことで行えます。 |
13802 | 13651 | C<O_RDONLY> と C<O_TRUNC> を同時に指定したときの振る舞いは未定義です。 |
13803 | 13652 | X<O_TRUNC> |
13804 | 13653 | |
13805 | 13654 | =begin original |
13806 | 13655 | |
13807 | 13656 | You should seldom if ever use C<0644> as argument to C<sysopen>, because |
13808 | 13657 | that takes away the user's option to have a more permissive umask. |
13809 | 13658 | Better to omit it. See the perlfunc(1) entry on C<umask> for more |
13810 | 13659 | on this. |
13811 | 13660 | |
13812 | 13661 | =end original |
13813 | 13662 | |
13814 | 13663 | めったなことでは C<sysopen> の引数に C<0644> を指定するべきではないでしょう: |
13815 | 13664 | ユーザーがより寛大な umask を指定する選択肢を奪うからです。 |
13816 | 13665 | 省略した方がいいです。 |
13817 | 13666 | これに関するさらなる情報については perlfunc(1) の C<umask> を |
13818 | 13667 | 参照してください。 |
13819 | 13668 | |
13820 | 13669 | =begin original |
13821 | 13670 | |
13822 | 13671 | Note that C<sysopen> depends on the fdopen() C library function. |
13823 | 13672 | On many UNIX systems, fdopen() is known to fail when file descriptors |
13824 | 13673 | exceed a certain value, typically 255. If you need more file |
13825 | 13674 | descriptors than that, consider rebuilding Perl to use the C<sfio> |
13826 | 13675 | library, or perhaps using the POSIX::open() function. |
13827 | 13676 | |
13828 | 13677 | =end original |
13829 | 13678 | |
13830 | 13679 | C<sysopen> は C の fdopen() ライブラリ関数に依存していることに注意してください。 |
13831 | 13680 | 多くの UNIX システムでは、fdopen() はファイル記述子がある値(例えば 255)を超えると |
13832 | 13681 | 失敗することが知られています。 |
13833 | 13682 | これより多くのファイル記述子が必要な場合は、 |
13834 | 13683 | Perl を C<sfio> ライブラリを使って再ビルドするか、 |
13835 | 13684 | POSIX::open() 関数を使うことを健闘してください。 |
13836 | 13685 | |
13837 | 13686 | =begin original |
13838 | 13687 | |
13839 | 13688 | See L<perlopentut> for a kinder, gentler explanation of opening files. |
13840 | 13689 | |
13841 | 13690 | =end original |
13842 | 13691 | |
13843 | 13692 | ファイル操作に関するより親切な説明については L<perlopentut> を参照して下さい。 |
13844 | 13693 | |
13845 | 13694 | =item sysread FILEHANDLE,SCALAR,LENGTH,OFFSET |
13846 | 13695 | X<sysread> |
13847 | 13696 | |
13848 | 13697 | =item sysread FILEHANDLE,SCALAR,LENGTH |
13849 | 13698 | |
13850 | 13699 | =begin original |
13851 | 13700 | |
13852 | 13701 | Attempts to read LENGTH bytes of data into variable SCALAR from the |
13853 | 13702 | specified FILEHANDLE, using the system call read(2). It bypasses |
13854 | 13703 | buffered IO, so mixing this with other kinds of reads, C<print>, |
13855 | 13704 | C<write>, C<seek>, C<tell>, or C<eof> can cause confusion because the |
13856 | 13705 | perlio or stdio layers usually buffers data. Returns the number of |
13857 | 13706 | bytes actually read, C<0> at end of file, or undef if there was an |
13858 | 13707 | error (in the latter case C<$!> is also set). SCALAR will be grown or |
13859 | 13708 | shrunk so that the last byte actually read is the last byte of the |
13860 | 13709 | scalar after the read. |
13861 | 13710 | |
13862 | 13711 | =end original |
13863 | 13712 | |
13864 | 13713 | システムコール read(2) を用いて、指定した FILEHANDLE |
13865 | 13714 | から、変数 SCALAR へ、LENGTH バイトのデータの読み込みを試みます。 |
13866 | 13715 | これは、バッファ付き IO ルーチンを通りませんから、 |
13867 | 13716 | 他の入力関数, C<print>, C<write>, |
13868 | 13717 | C<seek>, C<tell>, C<eof> と混ぜて使うと、入力がおかしくなるかも |
13869 | 13718 | しれません。 |
13870 | 13719 | perlio 層や stdio 層は普通データをバッファリングするからです。 |
13871 | 13720 | ファイルの最後では C<0>が、エラー時には undef が、 |
13872 | 13721 | それ以外では実際に読み込まれたデータの長さが返されます |
13873 | 13722 | (後者の場合は C<$!> もセットされます)。 |
13874 | 13723 | 実際に読み込んだ最後のバイトが read した後の最後のバイトになるので、 |
13875 | 13724 | SCALAR は伸び縮みします。 |
13876 | 13725 | |
13877 | 13726 | =begin original |
13878 | 13727 | |
13879 | 13728 | An OFFSET may be specified to place the read data at some place in the |
13880 | 13729 | string other than the beginning. A negative OFFSET specifies |
13881 | 13730 | placement at that many characters counting backwards from the end of |
13882 | 13731 | the string. A positive OFFSET greater than the length of SCALAR |
13883 | 13732 | results in the string being padded to the required size with C<"\0"> |
13884 | 13733 | bytes before the result of the read is appended. |
13885 | 13734 | |
13886 | 13735 | =end original |
13887 | 13736 | |
13888 | 13737 | OFFSET を指定すると、SCALAR の先頭以外の場所から、読み込みを行なうことが |
13889 | 13738 | できます。 |
13890 | 13739 | OFFSET に負の値を指定すると、文字列の最後から逆向きに何文字目かで |
13891 | 13740 | 位置を指定します。 |
13892 | OFFSET が正の値で、SCALAR の長さよりも大きかった場合、 | |
13741 | OFFSET が正の値で、SCALAR の長さよりも大きかった場合、 | |
13893 | ||
13742 | 必要なサイズになるまで C<"\0"> でパッディングされ、その後に | |
13894 | ||
13743 | 読み込み結果が追加されます。 | |
13895 | 13744 | |
13896 | 13745 | =begin original |
13897 | 13746 | |
13898 | 13747 | There is no syseof() function, which is ok, since eof() doesn't work |
13899 | 13748 | very well on device files (like ttys) anyway. Use sysread() and check |
13900 | 13749 | for a return value for 0 to decide whether you're done. |
13901 | 13750 | |
13902 | 13751 | =end original |
13903 | 13752 | |
13904 | 13753 | syseof() 関数はありませんが、問題ありません。 |
13905 | 13754 | どちらにしろ eof() は(tty のような)デバイスファイルに対しては |
13906 | 13755 | うまく動作しないからです。 |
13907 | 13756 | sysread() を使って、 返り値が 0 かどうかで最後まで読んだかを |
13908 | 13757 | 判断してください。 |
13909 | 13758 | |
13910 | 13759 | =begin original |
13911 | 13760 | |
13912 | 13761 | Note that if the filehandle has been marked as C<:utf8> Unicode |
13913 | 13762 | characters are read instead of bytes (the LENGTH, OFFSET, and the |
13914 | 13763 | return value of sysread() are in Unicode characters). |
13915 | 13764 | The C<:encoding(...)> layer implicitly introduces the C<:utf8> layer. |
13916 | 13765 | See L</binmode>, L</open>, and the C<open> pragma, L<open>. |
13917 | 13766 | |
13918 | 13767 | =end original |
13919 | 13768 | |
13920 | 13769 | ファイルハンドルが C<:utf8> であるとマークが付けられると、バイトではなく |
13921 | 13770 | Unicode 文字が読み込まれます (sysread() の LENGTH, OFFSET および返り値は |
13922 | 13771 | Unicode 文字になります)。 |
13923 | 13772 | C<:encoding(...)> 層は暗黙のうちに C<:utf8> 層が導入されます。 |
13924 | 13773 | L</binmode>, L</open>, C<open> プラグマ, L<open> を参照してください。 |
13925 | 13774 | |
13926 | 13775 | =item sysseek FILEHANDLE,POSITION,WHENCE |
13927 | 13776 | X<sysseek> X<lseek> |
13928 | 13777 | |
13929 | 13778 | =begin original |
13930 | 13779 | |
13931 | 13780 | Sets FILEHANDLE's system position in bytes using the system call |
13932 | 13781 | lseek(2). FILEHANDLE may be an expression whose value gives the name |
13933 | 13782 | of the filehandle. The values for WHENCE are C<0> to set the new |
13934 | 13783 | position to POSITION, C<1> to set the it to the current position plus |
13935 | 13784 | POSITION, and C<2> to set it to EOF plus POSITION (typically |
13936 | 13785 | negative). |
13937 | 13786 | |
13938 | 13787 | =end original |
13939 | 13788 | |
13940 | 13789 | FILEHANDLE のシステム位置をバイト単位で lseek(2) システムコールを使って |
13941 | 13790 | 設定します。 |
13942 | 13791 | FILEHANDLE は式でも構いません。 |
13943 | 13792 | その場合はその値がファイルハンドルの名前となります。 |
13944 | 13793 | WHENCE の値が、C<0> ならば、新しい位置を POSITION の位置へ、C<1> ならば、 |
13945 | 13794 | 現在位置から POSITION 加えた位置へ、C<2> ならば、EOF から POSITION だけ |
13946 | 13795 | (普通は負の数です)加えた位置へ、新しい位置を設定します。 |
13947 | 13796 | |
13948 | 13797 | =begin original |
13949 | 13798 | |
13950 | 13799 | Note the I<in bytes>: even if the filehandle has been set to operate |
13951 | 13800 | on characters (for example by using the C<:encoding(utf8)> I/O layer), |
13952 | 13801 | tell() will return byte offsets, not character offsets (because |
13953 | 13802 | implementing that would render sysseek() very slow). |
13954 | 13803 | |
13955 | 13804 | =end original |
13956 | 13805 | |
13957 | 13806 | I<バイト単位> に関する注意: 文字単位で扱うようにファイルハンドルが |
13958 | 13807 | 設定されている場合(C<:encoding(utf8)> I/O 層を使っている場合など)でも、 |
13959 | 13808 | tell() は文字のオフセットではなくバイトのオフセットを返します |
13960 | 13809 | (なぜならこれを実装すると sysseek() がとても遅くなるからです)。 |
13961 | 13810 | |
13962 | 13811 | =begin original |
13963 | 13812 | |
13964 | 13813 | sysseek() bypasses normal buffered IO, so mixing this with reads (other |
13965 | 13814 | than C<sysread>, for example C<< <> >> or read()) C<print>, C<write>, |
13966 | 13815 | C<seek>, C<tell>, or C<eof> may cause confusion. |
13967 | 13816 | |
13968 | 13817 | =end original |
13969 | 13818 | |
13970 | 13819 | sysseek() は普通のバッファ付き IO をバイパスしますので、 |
13971 | 13820 | (C<sysread> 以外の、例えば C<< <> >> や read() の)読み込み、 |
13972 | 13821 | C<print>, C<write>, C<seek>, C<tell>, C<eof> と混ぜて使うと |
13973 | 13822 | 混乱を引き起こします。 |
13974 | 13823 | |
13975 | 13824 | =begin original |
13976 | 13825 | |
13977 | 13826 | For WHENCE, you may also use the constants C<SEEK_SET>, C<SEEK_CUR>, |
13978 | 13827 | and C<SEEK_END> (start of the file, current position, end of the file) |
13979 | 13828 | from the Fcntl module. Use of the constants is also more portable |
13980 | 13829 | than relying on 0, 1, and 2. For example to define a "systell" function: |
13981 | 13830 | |
13982 | 13831 | =end original |
13983 | 13832 | |
13984 | 13833 | この値には、Fcntl モジュールで使われている C<SEEK_SET>、 |
13985 | 13834 | C<SEEK_CUR>、C<SEEK_END> |
13986 | 13835 | (ファイルの先頭、現在位置、ファイルの最後)という定数を使うこともできます。 |
13987 | 13836 | |
13988 | 13837 | use Fcntl 'SEEK_CUR'; |
13989 | 13838 | sub systell { sysseek($_[0], 0, SEEK_CUR) } |
13990 | 13839 | |
13991 | 13840 | =begin original |
13992 | 13841 | |
13993 | 13842 | Returns the new position, or the undefined value on failure. A position |
13994 | 13843 | of zero is returned as the string C<"0 but true">; thus C<sysseek> returns |
13995 | 13844 | true on success and false on failure, yet you can still easily determine |
13996 | 13845 | the new position. |
13997 | 13846 | |
13998 | 13847 | =end original |
13999 | 13848 | |
14000 | 13849 | 新しい位置を返します。 |
14001 | 13850 | 失敗したときは未定義値を返します。 |
14002 | 13851 | 位置がゼロの場合は、C<"0 but true"> の文字列として返されます。 |
14003 | 13852 | 従って C<sysseek> は成功時に真を返し、失敗時に偽を返しますが、 |
14004 | 13853 | 簡単に新しい位置を判定できます。 |
14005 | 13854 | |
14006 | 13855 | =item system LIST |
14007 | 13856 | X<system> X<shell> |
14008 | 13857 | |
14009 | 13858 | =item system PROGRAM LIST |
14010 | 13859 | |
14011 | 13860 | =begin original |
14012 | 13861 | |
14013 | 13862 | Does exactly the same thing as C<exec LIST>, except that a fork is |
14014 | 13863 | done first, and the parent process waits for the child process to |
14015 | 13864 | complete. Note that argument processing varies depending on the |
14016 | 13865 | number of arguments. If there is more than one argument in LIST, |
14017 | 13866 | or if LIST is an array with more than one value, starts the program |
14018 | 13867 | given by the first element of the list with arguments given by the |
14019 | 13868 | rest of the list. If there is only one scalar argument, the argument |
14020 | 13869 | is checked for shell metacharacters, and if there are any, the |
14021 | 13870 | entire argument is passed to the system's command shell for parsing |
14022 | 13871 | (this is C</bin/sh -c> on Unix platforms, but varies on other |
14023 | 13872 | platforms). If there are no shell metacharacters in the argument, |
14024 | 13873 | it is split into words and passed directly to C<execvp>, which is |
14025 | 13874 | more efficient. |
14026 | 13875 | |
14027 | 13876 | =end original |
14028 | 13877 | |
14029 | 13878 | C<exec LIST> とほとんど同じですが、まず fork を行ない、 |
14030 | 13879 | 親プロセスではチャイルドプロセスが終了するのを wait します。 |
14031 | 13880 | exec の項で述べたように、引数の処理は、引数の数によって異なることに |
14032 | 13881 | 注意してください。 |
14033 | 13882 | LIST に複数の引数がある場合、または LIST が複数の要素からなる配列の場合、 |
14034 | 13883 | リストの最初の要素で与えられるプログラムを、リストの残りの要素を |
14035 | 13884 | 引数として起動します。 |
14036 | 13885 | スカラの引数が一つだけの場合、 |
14037 | 13886 | 引数はシェルのメタ文字をチェックされ、もしあれば |
14038 | 13887 | パーズのために引数全体がシステムコマンドシェル |
14039 | 13888 | (これは Unix プラットフォームでは C</bin/sh -c> ですが、 |
14040 | 13889 | 他のプラットフォームでは異なります)に渡されます。 |
14041 | 13890 | シェルのメタ文字がなかった場合、 |
14042 | 13891 | 引数は単語に分解されて直接 C<execvp> に渡されます。 |
14043 | 13892 | この方がより効率的です。 |
14044 | 13893 | |
14045 | 13894 | =begin original |
14046 | 13895 | |
14047 | 13896 | Beginning with v5.6.0, Perl will attempt to flush all files opened for |
14048 | 13897 | output before any operation that may do a fork, but this may not be |
14049 | 13898 | supported on some platforms (see L<perlport>). To be safe, you may need |
14050 | 13899 | to set C<$|> ($AUTOFLUSH in English) or call the C<autoflush()> method |
14051 | 13900 | of C<IO::Handle> on any open handles. |
14052 | 13901 | |
14053 | 13902 | =end original |
14054 | 13903 | |
14055 | v5.6.0 から、Perl は | |
13904 | v5.6.0 から、Perl は fork を行うようなあらゆる動作の前に | |
14056 | ||
13905 | 出力用にオープンしていた全てのファイルをフラッシュしようとします。 | |
14057 | プラットフォームもあります(L<perlport> | |
13906 | しかしこれをサポートしていないプラットフォームもあります(L<perlport> 参照)。 | |
14058 | 安全のために、C<$ | |
13907 | 安全のためには、C<$l>(English モジュールを使っているなら $AUTOFLUSH)を | |
14059 | ||
13908 | 設定するか、あらゆるオープン済みハンドルにおいて C<IO::Handle> の C<autoflush()> | |
14060 | ||
13909 | メソッドが必要となるかもしれません。 | |
14061 | 13910 | |
14062 | 13911 | =begin original |
14063 | 13912 | |
14064 | 13913 | The return value is the exit status of the program as returned by the |
14065 | 13914 | C<wait> call. To get the actual exit value, shift right by eight (see |
14066 | 13915 | below). See also L</exec>. This is I<not> what you want to use to capture |
14067 | 13916 | the output from a command, for that you should use merely backticks or |
14068 | 13917 | C<qx//>, as described in L<perlop/"`STRING`">. Return value of -1 |
14069 | 13918 | indicates a failure to start the program or an error of the wait(2) system |
14070 | 13919 | call (inspect $! for the reason). |
14071 | 13920 | |
14072 | 13921 | =end original |
14073 | 13922 | |
14074 | 13923 | 返り値は、C<wait> が返すプログラムの exit 状態です。 |
14075 | 13924 | 実際の exit 値を得るには 右に 8 ビットシフトしてください(後述)。 |
14076 | 13925 | L</exec> も参照してください。 |
14077 | 13926 | これはコマンドからの出力を捕らえるために使うものI<ではありません>。 |
14078 | 13927 | そのような用途には、L<perlop/"`STRING`"> に記述されている |
14079 | 13928 | 逆クォートや C<qx//> を使用してください。 |
14080 | 13929 | -1 の返り値はプログラムを開始させることに失敗したか、wait(2) |
14081 | 13930 | システムコールがエラーを出したことを示します |
14082 | 13931 | (理由は $! を調べてください)。 |
14083 | 13932 | |
14084 | 13933 | =begin original |
14085 | 13934 | |
14086 | If you'd like to make C<system> (and many other bits of Perl) die on error, | |
14087 | have a look at the L<autodie> pragma. | |
14088 | ||
14089 | =end original | |
14090 | ||
14091 | もし C<system> (及び Perl のその他の多くの部分) でエラー時に | |
14092 | die したいなら、L<autodie> プラグマを見てみてください。 | |
14093 | ||
14094 | =begin original | |
14095 | ||
14096 | 13935 | Like C<exec>, C<system> allows you to lie to a program about its name if |
14097 | 13936 | you use the C<system PROGRAM LIST> syntax. Again, see L</exec>. |
14098 | 13937 | |
14099 | 13938 | =end original |
14100 | 13939 | |
14101 | 13940 | C<exec> と同様に、C<system> でも C<system PROGRAM LIST> の文法を |
14102 | 13941 | 使うことで、プログラムに対してその名前を嘘をつくことができます。 |
14103 | 13942 | 再び、L</exec> を参照して下さい。 |
14104 | 13943 | |
14105 | 13944 | =begin original |
14106 | 13945 | |
14107 | 13946 | Since C<SIGINT> and C<SIGQUIT> are ignored during the execution of |
14108 | 13947 | C<system>, if you expect your program to terminate on receipt of these |
14109 | 13948 | signals you will need to arrange to do so yourself based on the return |
14110 | 13949 | value. |
14111 | 13950 | |
14112 | 13951 | =end original |
14113 | 13952 | |
14114 | 13953 | C<SIGINT> と C<SIGQUIT> は C<system> の実行中は無視されるので、 |
14115 | 13954 | これらのシグナルを受信して終了させることを想定したプログラムの場合、 |
14116 | 13955 | 返り値を利用するように変更する必要があります。 |
14117 | 13956 | |
14118 | 13957 | @args = ("command", "arg1", "arg2"); |
14119 | 13958 | system(@args) == 0 |
14120 | 13959 | or die "system @args failed: $?" |
14121 | 13960 | |
14122 | 13961 | =begin original |
14123 | 13962 | |
14124 | ||
13963 | You can check all the failure possibilities by inspecting | |
14125 | ||
13964 | C<$?> like this: | |
14126 | 13965 | |
14127 | 13966 | =end original |
14128 | 13967 | |
14129 | C<system> の失敗を手動で検査したいなら、 | |
14130 | 13968 | 以下のように C<$?> を調べることで、全ての失敗の可能性を |
14131 | チェックできます: | |
13969 | チェックすることができます: | |
14132 | 13970 | |
14133 | 13971 | if ($? == -1) { |
14134 | 13972 | print "failed to execute: $!\n"; |
14135 | 13973 | } |
14136 | 13974 | elsif ($? & 127) { |
14137 | 13975 | printf "child died with signal %d, %s coredump\n", |
14138 | 13976 | ($? & 127), ($? & 128) ? 'with' : 'without'; |
14139 | 13977 | } |
14140 | 13978 | else { |
14141 | 13979 | printf "child exited with value %d\n", $? >> 8; |
14142 | 13980 | } |
14143 | 13981 | |
14144 | 13982 | =begin original |
14145 | 13983 | |
14146 | 13984 | Alternatively you might inspect the value of C<${^CHILD_ERROR_NATIVE}> |
14147 | 13985 | with the W*() calls of the POSIX extension. |
14148 | 13986 | |
14149 | 13987 | =end original |
14150 | 13988 | |
14151 | 13989 | または、POSIX 拡張の W*() 呼び出しを使って C<${^CHILD_ERROR_NATIVE}> の |
14152 | 13990 | 値を調べることもできます。 |
14153 | 13991 | |
14154 | 13992 | =begin original |
14155 | 13993 | |
14156 | 13994 | When the arguments get executed via the system shell, results |
14157 | 13995 | and return codes will be subject to its quirks and capabilities. |
14158 | 13996 | See L<perlop/"`STRING`"> and L</exec> for details. |
14159 | 13997 | |
14160 | 13998 | =end original |
14161 | 13999 | |
14162 | 14000 | 引数がシステムシェル経由で実行された場合、 |
14163 | 14001 | 結果と返り値はシェルの癖と能力によって変更されることがあります。 |
14164 | 14002 | 詳細については L<perlop/"`STRING`"> と L</exec> を参照して下さい。 |
14165 | 14003 | |
14166 | 14004 | =item syswrite FILEHANDLE,SCALAR,LENGTH,OFFSET |
14167 | 14005 | X<syswrite> |
14168 | 14006 | |
14169 | 14007 | =item syswrite FILEHANDLE,SCALAR,LENGTH |
14170 | 14008 | |
14171 | 14009 | =item syswrite FILEHANDLE,SCALAR |
14172 | 14010 | |
14173 | 14011 | =begin original |
14174 | 14012 | |
14175 | 14013 | Attempts to write LENGTH bytes of data from variable SCALAR to the |
14176 | 14014 | specified FILEHANDLE, using the system call write(2). If LENGTH is |
14177 | 14015 | not specified, writes whole SCALAR. It bypasses buffered IO, so |
14178 | 14016 | mixing this with reads (other than C<sysread())>, C<print>, C<write>, |
14179 | 14017 | C<seek>, C<tell>, or C<eof> may cause confusion because the perlio and |
14180 | 14018 | stdio layers usually buffers data. Returns the number of bytes |
14181 | 14019 | actually written, or C<undef> if there was an error (in this case the |
14182 | 14020 | errno variable C<$!> is also set). If the LENGTH is greater than the |
14183 | 14021 | available data in the SCALAR after the OFFSET, only as much data as is |
14184 | 14022 | available will be written. |
14185 | 14023 | |
14186 | 14024 | =end original |
14187 | 14025 | |
14188 | 14026 | write(2) システムコールを使って、指定した FILEHANDLEへ、 |
14189 | 14027 | 変数 SCALAR から、LENGTH バイトのデータの書き込みを試みます。 |
14190 | 14028 | LENGTH が指定されなかった場合、 SCALAR 全体を書き込みます。 |
14191 | 14029 | これは、バッファ付き IO ルーチンを通りませんから、 |
14192 | 14030 | 他の入力関数(C<sysread()> 以外), C<print>, C<write>, |
14193 | 14031 | C<seek>, C<tell>, or C<eof>と混ぜて使うと、 |
14194 | 14032 | 出力がおかしくなるかもしれません。 |
14195 | 14033 | perlio 層と stdio 層は普通データをバッファリングするからです。 |
14196 | 14034 | 実際に読み込まれたデータの長さか、エラー時には C<undef> が返されます |
14197 | 14035 | (この場合エラー変数 C<$!> もセットされます)。 |
14198 | 14036 | LENGTH が OFFSET 以降の SCALAR の利用可能なデータより大きかった場合、 |
14199 | 14037 | 利用可能なデータのみが書き込まれます。 |
14200 | 14038 | |
14201 | 14039 | =begin original |
14202 | 14040 | |
14203 | 14041 | An OFFSET may be specified to write the data from some part of the |
14204 | 14042 | string other than the beginning. A negative OFFSET specifies writing |
14205 | 14043 | that many characters counting backwards from the end of the string. |
14206 | 14044 | In the case the SCALAR is empty you can use OFFSET but only zero offset. |
14207 | 14045 | |
14208 | 14046 | =end original |
14209 | 14047 | |
14210 | 14048 | OFFSET を指定すると、SCALAR の先頭以外の場所から、 |
14211 | 14049 | データを取り出して、書き込みを行なうことができます。 |
14212 | 14050 | OFFSET に負の値を指定すると、文字列の最後から逆向きに数えて |
14213 | 14051 | 何バイト目から書き込むかを示します。 |
14214 | 14052 | SCALAR が空の場合、OFFSET はゼロのみ使用できます。 |
14215 | 14053 | |
14216 | 14054 | =begin original |
14217 | 14055 | |
14218 | 14056 | Note that if the filehandle has been marked as C<:utf8>, Unicode |
14219 | 14057 | characters are written instead of bytes (the LENGTH, OFFSET, and the |
14220 | 14058 | return value of syswrite() are in UTF-8 encoded Unicode characters). |
14221 | 14059 | The C<:encoding(...)> layer implicitly introduces the C<:utf8> layer. |
14222 | 14060 | See L</binmode>, L</open>, and the C<open> pragma, L<open>. |
14223 | 14061 | |
14224 | 14062 | =end original |
14225 | 14063 | |
14226 | 14064 | ファイルハンドルが C<:utf8> であるとマークが付けられると、バイトではなく |
14227 | 14065 | Unicode 文字が読み込まれます (syswrite() の LENGTH, OFFSET および返り値は |
14228 | 14066 | Unicode 文字になります)。 |
14229 | 14067 | C<:encoding(...)> 層は暗黙のうちに C<:utf8> 層が導入されます。 |
14230 | 14068 | L</binmode>, L</open>, C<open> プラグマ, L<open> を参照してください。 |
14231 | 14069 | |
14232 | 14070 | =item tell FILEHANDLE |
14233 | 14071 | X<tell> |
14234 | 14072 | |
14235 | 14073 | =item tell |
14236 | 14074 | |
14237 | 14075 | =begin original |
14238 | 14076 | |
14239 | 14077 | Returns the current position I<in bytes> for FILEHANDLE, or -1 on |
14240 | 14078 | error. FILEHANDLE may be an expression whose value gives the name of |
14241 | 14079 | the actual filehandle. If FILEHANDLE is omitted, assumes the file |
14242 | 14080 | last read. |
14243 | 14081 | |
14244 | 14082 | =end original |
14245 | 14083 | |
14246 | 14084 | FILEHANDLE の現在の位置を I<バイト数で> 返します。 |
14247 | 14085 | エラーの場合は -1 を返します。 |
14248 | 14086 | FILEHANDLE は、実際のファイルハンドル名を示す式でもかまいません。 |
14249 | 14087 | FILEHANDLE が省略された場合には、 |
14250 | 14088 | 最後に読み込みを行なったファイルについて調べます。 |
14251 | 14089 | |
14252 | 14090 | =begin original |
14253 | 14091 | |
14254 | 14092 | Note the I<in bytes>: even if the filehandle has been set to |
14255 | 14093 | operate on characters (for example by using the C<:encoding(utf8)> open |
14256 | 14094 | layer), tell() will return byte offsets, not character offsets (because |
14257 | 14095 | that would render seek() and tell() rather slow). |
14258 | 14096 | |
14259 | 14097 | =end original |
14260 | 14098 | |
14261 | 14099 | I<バイト単位> に関する注意: ファイルハンドルが (例えば |
14262 | 14100 | C<:encoding(utf8)> 層を使って) |
14263 | 14101 | 文字を操作するように設定されていたとしても、tell() は文字の |
14264 | 14102 | オフセットではなくバイトのオフセットを返すことに注意してください |
14265 | 14103 | (なぜならこれは seek() と tell() が遅くなってしまうからです)。 |
14266 | 14104 | |
14267 | 14105 | =begin original |
14268 | 14106 | |
14269 | 14107 | The return value of tell() for the standard streams like the STDIN |
14270 | 14108 | depends on the operating system: it may return -1 or something else. |
14271 | 14109 | tell() on pipes, fifos, and sockets usually returns -1. |
14272 | 14110 | |
14273 | 14111 | =end original |
14274 | 14112 | |
14275 | 14113 | STDIN のような標準ストリームに対する tell() の返り値は OS に依存します。 |
14276 | 14114 | -1 やその他の値が返ってくるかもしれません。 |
14277 | 14115 | パイプ、FIFO、ソケットに対して tell() を使うと、普通は -1 が返ります。 |
14278 | 14116 | |
14279 | 14117 | =begin original |
14280 | 14118 | |
14281 | 14119 | There is no C<systell> function. Use C<sysseek(FH, 0, 1)> for that. |
14282 | 14120 | |
14283 | 14121 | =end original |
14284 | 14122 | |
14285 | 14123 | C<systell> 関数はありません。 |
14286 | 14124 | 代わりに C<sysseek(FH, 0, 1)> を使ってください。 |
14287 | 14125 | |
14288 | 14126 | =begin original |
14289 | 14127 | |
14290 | 14128 | Do not use tell() (or other buffered I/O operations) on a file handle |
14291 | 14129 | that has been manipulated by sysread(), syswrite() or sysseek(). |
14292 | 14130 | Those functions ignore the buffering, while tell() does not. |
14293 | 14131 | |
14294 | 14132 | =end original |
14295 | 14133 | |
14296 | 14134 | sysread(), syswrite(), sysseek() で操作されたファイルハンドルに tell() |
14297 | 14135 | (またはその他のバッファリング I/O 操作) を使わないでください。 |
14298 | 14136 | これらの関数はバッファリングを無視しますが、tell() は違います。 |
14299 | 14137 | |
14300 | 14138 | =item telldir DIRHANDLE |
14301 | 14139 | X<telldir> |
14302 | 14140 | |
14303 | 14141 | =begin original |
14304 | 14142 | |
14305 | 14143 | Returns the current position of the C<readdir> routines on DIRHANDLE. |
14306 | 14144 | Value may be given to C<seekdir> to access a particular location in a |
14307 | 14145 | directory. C<telldir> has the same caveats about possible directory |
14308 | 14146 | compaction as the corresponding system library routine. |
14309 | 14147 | |
14310 | 14148 | =end original |
14311 | 14149 | |
14312 | 14150 | DIRHANDLE 上の C<readdir> ルーチンに対する現在位置を返します。 |
14313 | 14151 | 値は、そのディレクトリで特定の位置をアクセスするため、 |
14314 | 14152 | C<seekdir> に渡すことができます。 |
14315 | 14153 | C<telldir> は同名のシステムライブラリルーチンと同じく、 |
14316 | 14154 | ディレクトリ縮小時の問題が考えられます。 |
14317 | 14155 | |
14318 | 14156 | =item tie VARIABLE,CLASSNAME,LIST |
14319 | 14157 | X<tie> |
14320 | 14158 | |
14321 | 14159 | =begin original |
14322 | 14160 | |
14323 | 14161 | This function binds a variable to a package class that will provide the |
14324 | 14162 | implementation for the variable. VARIABLE is the name of the variable |
14325 | 14163 | to be enchanted. CLASSNAME is the name of a class implementing objects |
14326 | 14164 | of correct type. Any additional arguments are passed to the C<new> |
14327 | 14165 | method of the class (meaning C<TIESCALAR>, C<TIEHANDLE>, C<TIEARRAY>, |
14328 | 14166 | or C<TIEHASH>). Typically these are arguments such as might be passed |
14329 | 14167 | to the C<dbm_open()> function of C. The object returned by the C<new> |
14330 | 14168 | method is also returned by the C<tie> function, which would be useful |
14331 | 14169 | if you want to access other methods in CLASSNAME. |
14332 | 14170 | |
14333 | 14171 | =end original |
14334 | 14172 | |
14335 | 14173 | この関数は、変数を、その変数の実装を行なうクラスと結び付けます。 |
14336 | 14174 | VARIABLE は、魔法をかける変数の名前です。 |
14337 | 14175 | CLASSNAME は、正しい型のオブジェクトを実装するクラスの名前です。 |
14338 | 14176 | 他に引数があれば、そのクラスの C<new> メソッドに渡されます |
14339 | 14177 | (つまり C<TIESCALAR>, C<TIEHANDLE>, C<TIEARRAY>, C<TIEHASH>)。 |
14340 | 14178 | 通常、これらは、C の C<dbm_open> などの関数に渡す引数となります。 |
14341 | 14179 | C<new> メソッドで返されるオブジェクトはまた C<tie> 関数でも返されます。 |
14342 | 14180 | これは CLASSNAME の他のメソッドにアクセスしたいときに便利です。 |
14343 | 14181 | |
14344 | 14182 | =begin original |
14345 | 14183 | |
14346 | 14184 | Note that functions such as C<keys> and C<values> may return huge lists |
14347 | 14185 | when used on large objects, like DBM files. You may prefer to use the |
14348 | 14186 | C<each> function to iterate over such. Example: |
14349 | 14187 | |
14350 | 14188 | =end original |
14351 | 14189 | |
14352 | 14190 | DBM ファイルのような大きなオブジェクトでは、C<keys> や C<values> のような |
14353 | 14191 | 関数は、大きなリストを返す可能性があります。 |
14354 | 14192 | そのような場合では、C<each> 関数を使って繰り返しを行なった方が |
14355 | 14193 | よいかもしれません。 |
14356 | 14194 | 例: |
14357 | 14195 | |
14358 | 14196 | # print out history file offsets |
14359 | 14197 | use NDBM_File; |
14360 | 14198 | tie(%HIST, 'NDBM_File', '/usr/lib/news/history', 1, 0); |
14361 | 14199 | while (($key,$val) = each %HIST) { |
14362 | 14200 | print $key, ' = ', unpack('L',$val), "\n"; |
14363 | 14201 | } |
14364 | 14202 | untie(%HIST); |
14365 | 14203 | |
14366 | 14204 | =begin original |
14367 | 14205 | |
14368 | 14206 | A class implementing a hash should have the following methods: |
14369 | 14207 | |
14370 | 14208 | =end original |
14371 | 14209 | |
14372 | 14210 | ハッシュを実装するクラスでは、次のようなメソッドを用意します: |
14373 | 14211 | |
14374 | 14212 | TIEHASH classname, LIST |
14375 | 14213 | FETCH this, key |
14376 | 14214 | STORE this, key, value |
14377 | 14215 | DELETE this, key |
14378 | 14216 | CLEAR this |
14379 | 14217 | EXISTS this, key |
14380 | 14218 | FIRSTKEY this |
14381 | 14219 | NEXTKEY this, lastkey |
14382 | 14220 | SCALAR this |
14383 | 14221 | DESTROY this |
14384 | 14222 | UNTIE this |
14385 | 14223 | |
14386 | 14224 | =begin original |
14387 | 14225 | |
14388 | 14226 | A class implementing an ordinary array should have the following methods: |
14389 | 14227 | |
14390 | 14228 | =end original |
14391 | 14229 | |
14392 | 14230 | 通常の配列を実装するクラスでは、次のようなメソッドを用意します: |
14393 | 14231 | |
14394 | 14232 | TIEARRAY classname, LIST |
14395 | 14233 | FETCH this, key |
14396 | 14234 | STORE this, key, value |
14397 | 14235 | FETCHSIZE this |
14398 | 14236 | STORESIZE this, count |
14399 | 14237 | CLEAR this |
14400 | 14238 | PUSH this, LIST |
14401 | 14239 | POP this |
14402 | 14240 | SHIFT this |
14403 | 14241 | UNSHIFT this, LIST |
14404 | 14242 | SPLICE this, offset, length, LIST |
14405 | 14243 | EXTEND this, count |
14406 | 14244 | DESTROY this |
14407 | 14245 | UNTIE this |
14408 | 14246 | |
14409 | 14247 | =begin original |
14410 | 14248 | |
14411 | 14249 | A class implementing a file handle should have the following methods: |
14412 | 14250 | |
14413 | 14251 | =end original |
14414 | 14252 | |
14415 | 14253 | ファイルハンドルを実装するクラスでは、次のようなメソッドを用意します: |
14416 | 14254 | |
14417 | 14255 | TIEHANDLE classname, LIST |
14418 | 14256 | READ this, scalar, length, offset |
14419 | 14257 | READLINE this |
14420 | 14258 | GETC this |
14421 | 14259 | WRITE this, scalar, length, offset |
14422 | 14260 | PRINT this, LIST |
14423 | 14261 | PRINTF this, format, LIST |
14424 | 14262 | BINMODE this |
14425 | 14263 | EOF this |
14426 | 14264 | FILENO this |
14427 | 14265 | SEEK this, position, whence |
14428 | 14266 | TELL this |
14429 | 14267 | OPEN this, mode, LIST |
14430 | 14268 | CLOSE this |
14431 | 14269 | DESTROY this |
14432 | 14270 | UNTIE this |
14433 | 14271 | |
14434 | 14272 | =begin original |
14435 | 14273 | |
14436 | 14274 | A class implementing a scalar should have the following methods: |
14437 | 14275 | |
14438 | 14276 | =end original |
14439 | 14277 | |
14440 | 14278 | スカラ変数を実装するクラスでは、次のようなメソッドを用意します: |
14441 | 14279 | |
14442 | 14280 | TIESCALAR classname, LIST |
14443 | 14281 | FETCH this, |
14444 | 14282 | STORE this, value |
14445 | 14283 | DESTROY this |
14446 | 14284 | UNTIE this |
14447 | 14285 | |
14448 | 14286 | =begin original |
14449 | 14287 | |
14450 | 14288 | Not all methods indicated above need be implemented. See L<perltie>, |
14451 | 14289 | L<Tie::Hash>, L<Tie::Array>, L<Tie::Scalar>, and L<Tie::Handle>. |
14452 | 14290 | |
14453 | 14291 | =end original |
14454 | 14292 | |
14455 | 14293 | 上記の全てのメソッドを実装する必要はありません。 |
14456 | 14294 | L<perltie>, L<Tie::Hash>, L<Tie::Array>, L<Tie::Scalar>, |
14457 | 14295 | L<Tie::Handle> を参照して下さい。 |
14458 | 14296 | |
14459 | 14297 | =begin original |
14460 | 14298 | |
14461 | 14299 | Unlike C<dbmopen>, the C<tie> function will not use or require a module |
14462 | 14300 | for you--you need to do that explicitly yourself. See L<DB_File> |
14463 | 14301 | or the F<Config> module for interesting C<tie> implementations. |
14464 | 14302 | |
14465 | 14303 | =end original |
14466 | 14304 | |
14467 | 14305 | C<dbmopen> と違い、C<tie> 関数はモジュールを use したり |
14468 | 14306 | require したりしません -- 自分で明示的に行う必要があります。 |
14469 | 14307 | C<tie> の興味深い実装については L<DB_File> や F<Config> モジュールを |
14470 | 14308 | 参照して下さい。 |
14471 | 14309 | |
14472 | 14310 | =begin original |
14473 | 14311 | |
14474 | 14312 | For further details see L<perltie>, L<"tied VARIABLE">. |
14475 | 14313 | |
14476 | 14314 | =end original |
14477 | 14315 | |
14478 | 14316 | 更なる詳細については L<perltie> や L<"tied VARIABLE"> を参照して下さい。 |
14479 | 14317 | |
14480 | 14318 | =item tied VARIABLE |
14481 | 14319 | X<tied> |
14482 | 14320 | |
14483 | 14321 | =begin original |
14484 | 14322 | |
14485 | 14323 | Returns a reference to the object underlying VARIABLE (the same value |
14486 | 14324 | that was originally returned by the C<tie> call that bound the variable |
14487 | 14325 | to a package.) Returns the undefined value if VARIABLE isn't tied to a |
14488 | 14326 | package. |
14489 | 14327 | |
14490 | 14328 | =end original |
14491 | 14329 | |
14492 | 14330 | VARIABLE の基となるオブジェクトへのリファレンスを返します |
14493 | 14331 | (変数をパッケージに結びつけるために C<tie> 呼び出しをしたときの |
14494 | 14332 | 返り値と同じものです)。 |
14495 | 14333 | VARIABLE がパッケージと結び付けられていない場合は未定義値を返します。 |
14496 | 14334 | |
14497 | 14335 | =item time |
14498 | 14336 | X<time> X<epoch> |
14499 | 14337 | |
14500 | 14338 | =begin original |
14501 | 14339 | |
14502 | 14340 | Returns the number of non-leap seconds since whatever time the system |
14503 | 14341 | considers to be the epoch, suitable for feeding to C<gmtime> and |
14504 | 14342 | C<localtime>. On most systems the epoch is 00:00:00 UTC, January 1, 1970; |
14505 | 14343 | a prominent exception being Mac OS Classic which uses 00:00:00, January 1, |
14506 | 14344 | 1904 in the current local time zone for its epoch. |
14507 | 14345 | |
14508 | 14346 | =end original |
14509 | 14347 | |
14510 | 14348 | C<gmtime> や C<localtime> への入力形式に合っている、 |
14511 | 14349 | システムが紀元と考える時点からの連続秒数を返します。 |
14512 | 14350 | ほとんどのシステムでは紀元は UTC 1970 年 1 月 1 日 00:00:00 です; |
14513 | 14351 | 特徴的な例外としては、古い Mac OS ではローカルタイムゾーンの |
14514 | 14352 | 1904 年 1 月 1 日 00:00:00 を紀元として使います。 |
14515 | 14353 | |
14516 | 14354 | =begin original |
14517 | 14355 | |
14518 | 14356 | For measuring time in better granularity than one second, |
14519 | 14357 | you may use either the L<Time::HiRes> module (from CPAN, and starting from |
14520 | 14358 | Perl 5.8 part of the standard distribution), or if you have |
14521 | 14359 | gettimeofday(2), you may be able to use the C<syscall> interface of Perl. |
14522 | 14360 | See L<perlfaq8> for details. |
14523 | 14361 | |
14524 | 14362 | =end original |
14525 | 14363 | |
14526 | 14364 | 1 秒よりも細かい時間を計測するためには、L<Time::HiRes> モジュール(CPAN から、 |
14527 | 14365 | また Perl 5.8 からは標準配布の一部です)を使うか、 |
14528 | 14366 | gettimeofday(2) があるなら、Perl の C<syscall> インターフェースを |
14529 | 14367 | 使った方がよいでしょう。 |
14530 | 14368 | 詳しくは L<perlfaq8> を参照して下さい。 |
14531 | 14369 | |
14532 | 14370 | =begin original |
14533 | 14371 | |
14534 | 14372 | For date and time processing look at the many related modules on CPAN. |
14535 | 14373 | For a comprehensive date and time representation look at the |
14536 | 14374 | L<DateTime> module. |
14537 | 14375 | |
14538 | 14376 | =end original |
14539 | 14377 | |
14540 | 14378 | 日付と時刻の処理は、多くの関連するモジュールが CPAN にあります。 |
14541 | 14379 | 包括的な日付と時刻の表現については、CPAN の L<DateTime> モジュールを |
14542 | 14380 | 参照してください。 |
14543 | 14381 | |
14544 | 14382 | =item times |
14545 | 14383 | X<times> |
14546 | 14384 | |
14547 | 14385 | =begin original |
14548 | 14386 | |
14549 | 14387 | Returns a four-element list giving the user and system times, in |
14550 | 14388 | seconds, for this process and the children of this process. |
14551 | 14389 | |
14552 | 14390 | =end original |
14553 | 14391 | |
14554 | 14392 | 現プロセス及びその子プロセスに対する、ユーザ時間とシステム時間を |
14555 | 14393 | 秒で示した、4 要素のリスト値を返します。 |
14556 | 14394 | |
14557 | 14395 | ($user,$system,$cuser,$csystem) = times; |
14558 | 14396 | |
14559 | 14397 | =begin original |
14560 | 14398 | |
14561 | 14399 | In scalar context, C<times> returns C<$user>. |
14562 | 14400 | |
14563 | 14401 | =end original |
14564 | 14402 | |
14565 | 14403 | スカラコンテキストでは、C<times> は C<$user> を返します。 |
14566 | 14404 | |
14567 | 14405 | =begin original |
14568 | 14406 | |
14569 | 14407 | Note that times for children are included only after they terminate. |
14570 | 14408 | |
14571 | 14409 | =end original |
14572 | 14410 | |
14573 | 14411 | 子プロセスに対する times は、それらが終了した後に含められることに |
14574 | 14412 | 注意してください。 |
14575 | 14413 | |
14576 | 14414 | =item tr/// |
14577 | 14415 | |
14578 | 14416 | =begin original |
14579 | 14417 | |
14580 | The transliteration operator. Same as C<y///>. See | |
14418 | The transliteration operator. Same as C<y///>. See L<perlop>. | |
14581 | L<perlop/"Quote and Quote-like Operators">. | |
14582 | 14419 | |
14583 | 14420 | =end original |
14584 | 14421 | |
14585 | 14422 | 変換演算子。 |
14586 | 14423 | C<y///> と同じです。 |
14587 | L<perlop | |
14424 | L<perlop> を参照してください。 | |
14588 | 14425 | |
14589 | 14426 | =item truncate FILEHANDLE,LENGTH |
14590 | 14427 | X<truncate> |
14591 | 14428 | |
14592 | 14429 | =item truncate EXPR,LENGTH |
14593 | 14430 | |
14594 | 14431 | =begin original |
14595 | 14432 | |
14596 | 14433 | Truncates the file opened on FILEHANDLE, or named by EXPR, to the |
14597 | 14434 | specified length. Produces a fatal error if truncate isn't implemented |
14598 | 14435 | on your system. Returns true if successful, the undefined value |
14599 | 14436 | otherwise. |
14600 | 14437 | |
14601 | 14438 | =end original |
14602 | 14439 | |
14603 | 14440 | FILEHANDLE 上にオープンされたファイルか、EXPR で名前を表わしたファイルを、 |
14604 | 14441 | 指定した長さに切り詰めます。 |
14605 | 14442 | システム上に truncate が実装されていなければ、致命的エラーとなります。 |
14606 | 14443 | 成功すれば真を、さもなければ未定義値を返します。 |
14607 | 14444 | |
14608 | 14445 | =begin original |
14609 | 14446 | |
14610 | 14447 | The behavior is undefined if LENGTH is greater than the length of the |
14611 | 14448 | file. |
14612 | 14449 | |
14613 | 14450 | =end original |
14614 | 14451 | |
14615 | 14452 | LENGTH がファイルの長さより大きい場合の振る舞いは未定義です。 |
14616 | 14453 | |
14617 | 14454 | =begin original |
14618 | 14455 | |
14619 | 14456 | The position in the file of FILEHANDLE is left unchanged. You may want to |
14620 | 14457 | call L<seek> before writing to the file. |
14621 | 14458 | |
14622 | 14459 | =end original |
14623 | 14460 | |
14624 | 14461 | FILEHANDLE のファイルの位置は変わりません。 |
14625 | 14462 | ファイルに書き込む前に L<seek> を呼び出したいかもしれません。 |
14626 | 14463 | |
14627 | 14464 | =item uc EXPR |
14628 | 14465 | X<uc> X<uppercase> X<toupper> |
14629 | 14466 | |
14630 | 14467 | =item uc |
14631 | 14468 | |
14632 | 14469 | =begin original |
14633 | 14470 | |
14634 | 14471 | Returns an uppercased version of EXPR. This is the internal function |
14635 | 14472 | implementing the C<\U> escape in double-quoted strings. Respects |
14636 | 14473 | current LC_CTYPE locale if C<use locale> in force. See L<perllocale> |
14637 | 14474 | and L<perlunicode> for more details about locale and Unicode support. |
14638 | 14475 | It does not attempt to do titlecase mapping on initial letters. See |
14639 | 14476 | C<ucfirst> for that. |
14640 | 14477 | |
14641 | 14478 | =end original |
14642 | 14479 | |
14643 | 14480 | EXPR を大文字に変換したものを返します。 |
14644 | 14481 | これは、ダブルクォート文字列における、C<\U> エスケープを |
14645 | 14482 | 実装する内部関数です。 |
14646 | 14483 | C<use locale> が有効な場合は、現在の LC_CTYPE ロケールを参照します。 |
14647 | 14484 | ロケールと Unicode 対応に関する更なる詳細については L<perllocale> と |
14648 | 14485 | L<perlunicode> を参照してください。 |
14649 | 14486 | 元の文字の titlecase マッピングは試みません。 |
14650 | 14487 | このためには C<ucfirst> を参照してください。 |
14651 | 14488 | |
14652 | 14489 | =begin original |
14653 | 14490 | |
14654 | 14491 | If EXPR is omitted, uses C<$_>. |
14655 | 14492 | |
14656 | 14493 | =end original |
14657 | 14494 | |
14658 | 14495 | EXPR が省略されると、C<$_> を使います。 |
14659 | 14496 | |
14660 | 14497 | =item ucfirst EXPR |
14661 | 14498 | X<ucfirst> X<uppercase> |
14662 | 14499 | |
14663 | 14500 | =item ucfirst |
14664 | 14501 | |
14665 | 14502 | =begin original |
14666 | 14503 | |
14667 | 14504 | Returns the value of EXPR with the first character in uppercase |
14668 | 14505 | (titlecase in Unicode). This is the internal function implementing |
14669 | 14506 | the C<\u> escape in double-quoted strings. Respects current LC_CTYPE |
14670 | 14507 | locale if C<use locale> in force. See L<perllocale> and L<perlunicode> |
14671 | 14508 | for more details about locale and Unicode support. |
14672 | 14509 | |
14673 | 14510 | =end original |
14674 | 14511 | |
14675 | 14512 | 最初の文字だけを大文字にした、EXPR を返します |
14676 | 14513 | (Unicode では titlecase)。 |
14677 | 14514 | これは、ダブルクォート文字列における、C<\u> エスケープを |
14678 | 14515 | 実装する内部関数です。 |
14679 | 14516 | C<use locale> が有効な場合は、現在の LC_CTYPE ロケールを参照します。 |
14680 | 14517 | ロケールと Unicode 対応に関する更なる詳細については L<perllocale> と |
14681 | 14518 | L<perlunicode> を参照してください。 |
14682 | 14519 | |
14683 | 14520 | =begin original |
14684 | 14521 | |
14685 | 14522 | If EXPR is omitted, uses C<$_>. |
14686 | 14523 | |
14687 | 14524 | =end original |
14688 | 14525 | |
14689 | 14526 | EXPR が省略されると、C<$_> を使います。 |
14690 | 14527 | |
14691 | 14528 | =item umask EXPR |
14692 | 14529 | X<umask> |
14693 | 14530 | |
14694 | 14531 | =item umask |
14695 | 14532 | |
14696 | 14533 | =begin original |
14697 | 14534 | |
14698 | 14535 | Sets the umask for the process to EXPR and returns the previous value. |
14699 | 14536 | If EXPR is omitted, merely returns the current umask. |
14700 | 14537 | |
14701 | 14538 | =end original |
14702 | 14539 | |
14703 | 14540 | 現在のプロセスの umask を EXPR に設定し、以前の値を返します。 |
14704 | 14541 | EXPR が省略されると、単にその時点の umask の値を返します。 |
14705 | 14542 | |
14706 | 14543 | =begin original |
14707 | 14544 | |
14708 | 14545 | The Unix permission C<rwxr-x---> is represented as three sets of three |
14709 | 14546 | bits, or three octal digits: C<0750> (the leading 0 indicates octal |
14710 | 14547 | and isn't one of the digits). The C<umask> value is such a number |
14711 | 14548 | representing disabled permissions bits. The permission (or "mode") |
14712 | 14549 | values you pass C<mkdir> or C<sysopen> are modified by your umask, so |
14713 | 14550 | even if you tell C<sysopen> to create a file with permissions C<0777>, |
14714 | 14551 | if your umask is C<0022> then the file will actually be created with |
14715 | 14552 | permissions C<0755>. If your C<umask> were C<0027> (group can't |
14716 | 14553 | write; others can't read, write, or execute), then passing |
14717 | 14554 | C<sysopen> C<0666> would create a file with mode C<0640> (C<0666 &~ |
14718 | 14555 | 027> is C<0640>). |
14719 | 14556 | |
14720 | 14557 | =end original |
14721 | 14558 | |
14722 | 14559 | Unix パーミッション C<rwxr-x---> は 3 ビットの 3 つの組、 |
14723 | 14560 | または 3 桁の 8 進数として表現されます: |
14724 | 14561 | C<0750> (先頭の 0 は 8 進数を意味し、実際の値ではありません)。 |
14725 | 14562 | C<umask> の値は無効にするパーミッションビットのこのような数値表現です。 |
14726 | 14563 | C<mkdir> や C<sysopen> で渡されたパーミッション(または「モード」)の値は |
14727 | 14564 | umask で修正され、たとえ C<sysopen> で C<0777> のパーミッションで |
14728 | 14565 | ファイルを作るように指定しても、umask が C<0022> なら、 |
14729 | 14566 | 結果としてファイルは C<0755> のパーミッションで作成されます。 |
14730 | 14567 | C<umask> が C<0027> (グループは書き込めない; その他は読み込み、書き込み、 |
14731 | 14568 | 実行できない) のとき、C<sysopen> に C<0666> を渡すと、 |
14732 | 14569 | ファイルはモード C<0640> (C<0666 &~ 027> は C<0640>)で作成されます。 |
14733 | 14570 | |
14734 | 14571 | =begin original |
14735 | 14572 | |
14736 | 14573 | Here's some advice: supply a creation mode of C<0666> for regular |
14737 | 14574 | files (in C<sysopen>) and one of C<0777> for directories (in |
14738 | 14575 | C<mkdir>) and executable files. This gives users the freedom of |
14739 | 14576 | choice: if they want protected files, they might choose process umasks |
14740 | 14577 | of C<022>, C<027>, or even the particularly antisocial mask of C<077>. |
14741 | 14578 | Programs should rarely if ever make policy decisions better left to |
14742 | 14579 | the user. The exception to this is when writing files that should be |
14743 | 14580 | kept private: mail files, web browser cookies, I<.rhosts> files, and |
14744 | 14581 | so on. |
14745 | 14582 | |
14746 | 14583 | =end original |
14747 | 14584 | |
14748 | 14585 | 以下は助言です: 作成モードとして、(C<sysopen> による)通常ファイルでは |
14749 | 14586 | C<0666> を、(C<mkdir> による)ディレクトリでは C<0777> を指定しましょう。 |
14750 | 14587 | これにより、ユーザーに選択の自由を与えます: もしファイルを守りたいなら、 |
14751 | 14588 | プロセスの umask として C<022>, C<027>, あるいは特に非社交的な |
14752 | 14589 | C<077> を選択できます。 |
14753 | 14590 | プログラムがユーザーより適切なポリシー選択ができることは稀です。 |
14754 | 14591 | 例外は、プライベートに保つべきファイル(メール、ウェブブラウザのクッキー、 |
14755 | 14592 | I<.rhosts> ファイルなど)を書く場合です。 |
14756 | 14593 | |
14757 | 14594 | =begin original |
14758 | 14595 | |
14759 | 14596 | If umask(2) is not implemented on your system and you are trying to |
14760 | 14597 | restrict access for I<yourself> (i.e., (EXPR & 0700) > 0), produces a |
14761 | 14598 | fatal error at run time. If umask(2) is not implemented and you are |
14762 | 14599 | not trying to restrict access for yourself, returns C<undef>. |
14763 | 14600 | |
14764 | 14601 | =end original |
14765 | 14602 | |
14766 | 14603 | umask(2) が実装されていないシステムで、I<自分自身> へのアクセスを |
14767 | 14604 | 制限しようとした(つまり (EXPR & 0700) > 0)場合、実行時に致命的エラーが |
14768 | 14605 | 発生します。 |
14769 | 14606 | umask(2) が実装されていないシステムで、自分自身へのアクセスは |
14770 | 14607 | 制限しようとしなかった場合、C<undef> を返します。 |
14771 | 14608 | |
14772 | 14609 | =begin original |
14773 | 14610 | |
14774 | 14611 | Remember that a umask is a number, usually given in octal; it is I<not> a |
14775 | 14612 | string of octal digits. See also L</oct>, if all you have is a string. |
14776 | 14613 | |
14777 | 14614 | =end original |
14778 | 14615 | |
14779 | 14616 | umask は通常 8 進数で与えられる数値であることを忘れないでください。 |
14780 | 14617 | 8 進数の文字列 I<ではありません>。 |
14781 | 14618 | 文字列しかない場合、 L</oct> も参照して下さい。 |
14782 | 14619 | |
14783 | 14620 | =item undef EXPR |
14784 | 14621 | X<undef> X<undefine> |
14785 | 14622 | |
14786 | 14623 | =item undef |
14787 | 14624 | |
14788 | 14625 | =begin original |
14789 | 14626 | |
14790 | 14627 | Undefines the value of EXPR, which must be an lvalue. Use only on a |
14791 | 14628 | scalar value, an array (using C<@>), a hash (using C<%>), a subroutine |
14792 | 14629 | (using C<&>), or a typeglob (using C<*>). (Saying C<undef $hash{$key}> |
14793 | 14630 | will probably not do what you expect on most predefined variables or |
14794 | 14631 | DBM list values, so don't do that; see L<delete>.) Always returns the |
14795 | 14632 | undefined value. You can omit the EXPR, in which case nothing is |
14796 | 14633 | undefined, but you still get an undefined value that you could, for |
14797 | 14634 | instance, return from a subroutine, assign to a variable or pass as a |
14798 | 14635 | parameter. Examples: |
14799 | 14636 | |
14800 | 14637 | =end original |
14801 | 14638 | |
14802 | 14639 | 左辺値である EXPR の値を未定義にします。 |
14803 | 14640 | スカラ値、(C<@> を使った)配列、(C<%> を使った)ハッシュ、(C<&> を使った) |
14804 | 14641 | サブルーチン、(C<*> を使った)型グロブだけに使用します。 |
14805 | 14642 | (特殊変数や DBM リスト値に C<undef $hash{$key}> などとしても |
14806 | 14643 | おそらく期待通りの結果にはなりませんから、しないでください。 |
14807 | 14644 | L</delete> を参照してください。) |
14808 | 14645 | 常に未定義値を返します。 |
14809 | 14646 | EXPR は省略することができ、その場合には何も未定義にされませんが |
14810 | 14647 | 未定義値は返されますので、それをたとえば、 |
14811 | 14648 | サブルーチンの返り値、変数への割り当て、引数などとして使うことができます。 |
14812 | 14649 | 例: |
14813 | 14650 | |
14814 | 14651 | undef $foo; |
14815 | 14652 | undef $bar{'blurfl'}; # Compare to: delete $bar{'blurfl'}; |
14816 | 14653 | undef @ary; |
14817 | 14654 | undef %hash; |
14818 | 14655 | undef &mysub; |
14819 | 14656 | undef *xyz; # destroys $xyz, @xyz, %xyz, &xyz, etc. |
14820 | 14657 | return (wantarray ? (undef, $errmsg) : undef) if $they_blew_it; |
14821 | 14658 | select undef, undef, undef, 0.25; |
14822 | 14659 | ($a, $b, undef, $c) = &foo; # Ignore third value returned |
14823 | 14660 | |
14824 | 14661 | =begin original |
14825 | 14662 | |
14826 | 14663 | Note that this is a unary operator, not a list operator. |
14827 | 14664 | |
14828 | 14665 | =end original |
14829 | 14666 | |
14830 | 14667 | これはリスト演算子ではなく、単項演算子であることに注意してください。 |
14831 | 14668 | |
14832 | 14669 | =item unlink LIST |
14833 | 14670 | X<unlink> X<delete> X<remove> X<rm> X<del> |
14834 | 14671 | |
14835 | 14672 | =item unlink |
14836 | 14673 | |
14837 | 14674 | =begin original |
14838 | 14675 | |
14839 | 14676 | Deletes a list of files. Returns the number of files successfully |
14840 | 14677 | deleted. |
14841 | 14678 | |
14842 | 14679 | =end original |
14843 | 14680 | |
14844 | 14681 | LIST に含まれるファイルを削除します。 |
14845 | 14682 | 削除に成功したファイルの数を返します。 |
14846 | 14683 | |
14847 | 14684 | $cnt = unlink 'a', 'b', 'c'; |
14848 | 14685 | unlink @goners; |
14849 | 14686 | unlink <*.bak>; |
14850 | 14687 | |
14851 | 14688 | =begin original |
14852 | 14689 | |
14853 | 14690 | Note: C<unlink> will not attempt to delete directories unless you are superuser |
14854 | 14691 | and the B<-U> flag is supplied to Perl. Even if these conditions are |
14855 | 14692 | met, be warned that unlinking a directory can inflict damage on your |
14856 | 14693 | filesystem. Finally, using C<unlink> on directories is not supported on |
14857 | 14694 | many operating systems. Use C<rmdir> instead. |
14858 | 14695 | |
14859 | 14696 | =end original |
14860 | 14697 | |
14861 | 14698 | 注: スーパーユーザ権限で、Perl に -U を付けて実行した場合でなければ、 |
14862 | 14699 | C<unlink> はディレクトリを削除しようとすることはありません。 |
14863 | 14700 | この条件にあう場合にも、ディレクトリの削除は、 |
14864 | 14701 | ファイルシステムに多大な損害を与える可能性があります。 |
14865 | 14702 | 最後に、C<unlink> をディレクトリに使うのはほとんどの OS では |
14866 | 14703 | 対応していません。 |
14867 | 14704 | 代わりに C<rmdir> を使ってください。 |
14868 | 14705 | |
14869 | 14706 | =begin original |
14870 | 14707 | |
14871 | 14708 | If LIST is omitted, uses C<$_>. |
14872 | 14709 | |
14873 | 14710 | =end original |
14874 | 14711 | |
14875 | 14712 | LIST が省略されると、C<$_> を使います。 |
14876 | 14713 | |
14877 | 14714 | =item unpack TEMPLATE,EXPR |
14878 | 14715 | X<unpack> |
14879 | 14716 | |
14880 | 14717 | =item unpack TEMPLATE |
14881 | 14718 | |
14882 | 14719 | =begin original |
14883 | 14720 | |
14884 | 14721 | C<unpack> does the reverse of C<pack>: it takes a string |
14885 | 14722 | and expands it out into a list of values. |
14886 | 14723 | (In scalar context, it returns merely the first value produced.) |
14887 | 14724 | |
14888 | 14725 | =end original |
14889 | 14726 | |
14890 | 14727 | C<unpack> は C<pack> の逆を行ないます: 構造体を表わす文字列をとり、 |
14891 | 14728 | リスト値に展開し、その配列値を返します。 |
14892 | 14729 | (スカラコンテキストでは、単に最初の値を返します。) |
14893 | 14730 | |
14894 | 14731 | =begin original |
14895 | 14732 | |
14896 | 14733 | If EXPR is omitted, unpacks the C<$_> string. |
14897 | 14734 | |
14898 | 14735 | =end original |
14899 | 14736 | |
14900 | 14737 | EXPR が省略されると、C<$_> の文字列を unpack します。 |
14901 | 14738 | |
14902 | 14739 | =begin original |
14903 | 14740 | |
14904 | 14741 | The string is broken into chunks described by the TEMPLATE. Each chunk |
14905 | 14742 | is converted separately to a value. Typically, either the string is a result |
14906 | 14743 | of C<pack>, or the characters of the string represent a C structure of some |
14907 | 14744 | kind. |
14908 | 14745 | |
14909 | 14746 | =end original |
14910 | 14747 | |
14911 | 14748 | 文字列は TEMPLATE で示された固まりに分割されます。 |
14912 | 14749 | それぞれの固まりは別々に値に変換されます。 |
14913 | 14750 | 典型的には、文字列は C<pack> の結果あるいはある種の C の構造体の |
14914 | 14751 | 文字列表現の文字列です。 |
14915 | 14752 | |
14916 | 14753 | =begin original |
14917 | 14754 | |
14918 | 14755 | The TEMPLATE has the same format as in the C<pack> function. |
14919 | 14756 | Here's a subroutine that does substring: |
14920 | 14757 | |
14921 | 14758 | =end original |
14922 | 14759 | |
14923 | 14760 | TEMPLATE は、C<pack> 関数と同じフォーマットを使います。 |
14924 | 14761 | 部分文字列を取り出すうサブルーチンの例を示します: |
14925 | 14762 | |
14926 | 14763 | sub substr { |
14927 | 14764 | my($what,$where,$howmuch) = @_; |
14928 | 14765 | unpack("x$where a$howmuch", $what); |
14929 | 14766 | } |
14930 | 14767 | |
14931 | 14768 | =begin original |
14932 | 14769 | |
14933 | 14770 | and then there's |
14934 | 14771 | |
14935 | 14772 | =end original |
14936 | 14773 | |
14937 | 14774 | これもそうです。 |
14938 | 14775 | |
14939 | 14776 | sub ordinal { unpack("W",$_[0]); } # same as ord() |
14940 | 14777 | |
14941 | 14778 | =begin original |
14942 | 14779 | |
14943 | 14780 | In addition to fields allowed in pack(), you may prefix a field with |
14944 | 14781 | a %<number> to indicate that |
14945 | 14782 | you want a <number>-bit checksum of the items instead of the items |
14946 | 14783 | themselves. Default is a 16-bit checksum. Checksum is calculated by |
14947 | 14784 | summing numeric values of expanded values (for string fields the sum of |
14948 | 14785 | C<ord($char)> is taken, for bit fields the sum of zeroes and ones). |
14949 | 14786 | |
14950 | 14787 | =end original |
14951 | 14788 | |
14952 | 14789 | pack() で利用可能なフィールドの他に、 |
14953 | 14790 | フィールドの前に %<数値> というものを付けて、 |
14954 | 14791 | 項目自身の代わりに、その項目の <数値>-ビットのチェックサムを |
14955 | 14792 | 計算させることができます。 |
14956 | 14793 | デフォルトは、16-ビットチェックサムです。 |
14957 | 14794 | チェックサムは展開された値の数値としての値の合計 |
14958 | 14795 | (文字列フィールドの場合は C<ord($char)> の合計、 |
14959 | 14796 | ビットフィールドの場合は 0 と 1 の合計) が用いられます。 |
14960 | 14797 | |
14961 | 14798 | =begin original |
14962 | 14799 | |
14963 | 14800 | For example, the following |
14964 | 14801 | computes the same number as the System V sum program: |
14965 | 14802 | |
14966 | 14803 | =end original |
14967 | 14804 | |
14968 | 14805 | たとえば、以下のコードは |
14969 | 14806 | System V の sum プログラムと同じ値を計算します。 |
14970 | 14807 | |
14971 | 14808 | $checksum = do { |
14972 | 14809 | local $/; # slurp! |
14973 | 14810 | unpack("%32W*",<>) % 65535; |
14974 | 14811 | }; |
14975 | 14812 | |
14976 | 14813 | =begin original |
14977 | 14814 | |
14978 | 14815 | The following efficiently counts the number of set bits in a bit vector: |
14979 | 14816 | |
14980 | 14817 | =end original |
14981 | 14818 | |
14982 | 14819 | 以下は、効率的にビットベクターの設定されているビットを |
14983 | 14820 | 数えるものです。 |
14984 | 14821 | |
14985 | 14822 | $setbits = unpack("%32b*", $selectmask); |
14986 | 14823 | |
14987 | 14824 | =begin original |
14988 | 14825 | |
14989 | 14826 | The C<p> and C<P> formats should be used with care. Since Perl |
14990 | 14827 | has no way of checking whether the value passed to C<unpack()> |
14991 | 14828 | corresponds to a valid memory location, passing a pointer value that's |
14992 | 14829 | not known to be valid is likely to have disastrous consequences. |
14993 | 14830 | |
14994 | 14831 | =end original |
14995 | 14832 | |
14996 | 14833 | C<p> と C<P> は注意深く使うべきです。 |
14997 | 14834 | Perl は C<unpack()> に渡された値が有効なメモリ位置を指しているかどうかを |
14998 | 14835 | 確認する方法がないので、有効かどうかわからないポインタ値を渡すと |
14999 | 14836 | 悲惨な結果を引き起こすかもしれません。 |
15000 | 14837 | |
15001 | 14838 | =begin original |
15002 | 14839 | |
15003 | 14840 | If there are more pack codes or if the repeat count of a field or a group |
15004 | 14841 | is larger than what the remainder of the input string allows, the result |
15005 | 14842 | is not well defined: in some cases, the repeat count is decreased, or |
15006 | 14843 | C<unpack()> will produce null strings or zeroes, or terminate with an |
15007 | 14844 | error. If the input string is longer than one described by the TEMPLATE, |
15008 | 14845 | the rest is ignored. |
15009 | 14846 | |
15010 | 14847 | =end original |
15011 | 14848 | |
15012 | 14849 | 多くの pack コードがある場合や、フィールドやグループの繰り返し回数が |
15013 | 14850 | 入力文字列の残りより大きい場合、結果は未定義です: |
15014 | 14851 | 繰り返し回数が減らされる場合もありますし、C<unpack()> が空文字列や 0 を |
15015 | 14852 | 返すこともありますし、エラーで終了することもあります。 |
15016 | 14853 | もし入力文字列が TEMPLATE で表現されているものより大きい場合、残りは |
15017 | 14854 | 無視されます。 |
15018 | 14855 | |
15019 | 14856 | =begin original |
15020 | 14857 | |
15021 | 14858 | See L</pack> for more examples and notes. |
15022 | 14859 | |
15023 | 14860 | =end original |
15024 | 14861 | |
15025 | 14862 | さらなる例と注意に関しては L</pack> を参照してください。 |
15026 | 14863 | |
15027 | 14864 | =item untie VARIABLE |
15028 | 14865 | X<untie> |
15029 | 14866 | |
15030 | 14867 | =begin original |
15031 | 14868 | |
15032 | 14869 | Breaks the binding between a variable and a package. (See C<tie>.) |
15033 | 14870 | Has no effect if the variable is not tied. |
15034 | 14871 | |
15035 | 14872 | =end original |
15036 | 14873 | |
15037 | 14874 | 変数とパッケージの間の結合を解きます。 |
15038 | 14875 | (L<tie> を参照してください。) |
15039 | 14876 | 結合されていない場合は何も起きません。 |
15040 | 14877 | |
15041 | 14878 | =item unshift ARRAY,LIST |
15042 | 14879 | X<unshift> |
15043 | 14880 | |
15044 | 14881 | =begin original |
15045 | 14882 | |
15046 | 14883 | Does the opposite of a C<shift>. Or the opposite of a C<push>, |
15047 | 14884 | depending on how you look at it. Prepends list to the front of the |
15048 | 14885 | array, and returns the new number of elements in the array. |
15049 | 14886 | |
15050 | 14887 | =end original |
15051 | 14888 | |
15052 | 14889 | C<shift> の逆操作を行ないます。 |
15053 | 14890 | 見方を変えれば、C<push>の逆操作とも考えられます。 |
15054 | 14891 | LIST を ARRAY の先頭に入れて、新しくできた配列の要素の数を返します。 |
15055 | 14892 | |
15056 | 14893 | unshift(@ARGV, '-e') unless $ARGV[0] =~ /^-/; |
15057 | 14894 | |
15058 | 14895 | =begin original |
15059 | 14896 | |
15060 | 14897 | Note the LIST is prepended whole, not one element at a time, so the |
15061 | 14898 | prepended elements stay in the same order. Use C<reverse> to do the |
15062 | 14899 | reverse. |
15063 | 14900 | |
15064 | 14901 | =end original |
15065 | 14902 | |
15066 | 14903 | LIST は、はらばらにではなく、一度に登録されるので、順番はそのままです。 |
15067 | 14904 | 逆順に登録するには、C<reverse> を使ってください。 |
15068 | 14905 | |
15069 | 14906 | =item use Module VERSION LIST |
15070 | 14907 | X<use> X<module> X<import> |
15071 | 14908 | |
15072 | 14909 | =item use Module VERSION |
15073 | 14910 | |
15074 | 14911 | =item use Module LIST |
15075 | 14912 | |
15076 | 14913 | =item use Module |
15077 | 14914 | |
15078 | 14915 | =item use VERSION |
15079 | 14916 | |
15080 | 14917 | =begin original |
15081 | 14918 | |
15082 | 14919 | Imports some semantics into the current package from the named module, |
15083 | 14920 | generally by aliasing certain subroutine or variable names into your |
15084 | 14921 | package. It is exactly equivalent to |
15085 | 14922 | |
15086 | 14923 | =end original |
15087 | 14924 | |
15088 | 14925 | 指定したモジュールから、現在のパッケージにさまざまな内容を |
15089 | 14926 | インポートします。 |
15090 | 14927 | 多くは、パッケージのサブルーチン名や、変数名に別名を付けることで、 |
15091 | 14928 | 実現されています。これは、 |
15092 | 14929 | 以下は等価ですが: |
15093 | 14930 | |
15094 | 14931 | BEGIN { require Module; Module->import( LIST ); } |
15095 | 14932 | |
15096 | 14933 | =begin original |
15097 | 14934 | |
15098 | 14935 | except that Module I<must> be a bareword. |
15099 | 14936 | |
15100 | 14937 | =end original |
15101 | 14938 | |
15102 | 14939 | Module が I<単なる単語でなければならない> ことを除けば、です。 |
15103 | 14940 | |
15104 | 14941 | =begin original |
15105 | 14942 | |
15106 | 14943 | In the peculiar C<use VERSION> form, VERSION may be either a numeric |
15107 | 14944 | argument such as 5.006, which will be compared to C<$]>, or a literal of |
15108 | 14945 | the form v5.6.1, which will be compared to C<$^V> (aka $PERL_VERSION). A |
15109 | 14946 | fatal error is produced if VERSION is greater than the version of the |
15110 | 14947 | current Perl interpreter; Perl will not attempt to parse the rest of the |
15111 | 14948 | file. Compare with L</require>, which can do a similar check at run time. |
15112 | 14949 | Symmetrically, C<no VERSION> allows you to specify that you want a version |
15113 | 14950 | of perl older than the specified one. |
15114 | 14951 | |
15115 | 14952 | =end original |
15116 | 14953 | |
15117 | 14954 | 特に C<use VERSION> の形式では、 |
15118 | 14955 | VERSION は 5.006 のような数値(C<$]> と比較されます)か、v5.6.1 の形 |
15119 | 14956 | (C<$^V> (またの名を $PERL_VERSION) と比較されます)で指定します。 |
15120 | 14957 | VERSION が Perl の現在のバージョンより大きいと、致命的エラーが |
15121 | 14958 | 発生します; Perl はファイルの残りを読み込みません。 |
15122 | 14959 | L</require> と似ていますが、これは実行時にチェックされます。 |
15123 | 14960 | 対称的に、C<no VERSION> は指定されたバージョンより古いバージョンの perl で |
15124 | 14961 | 動作させたいことを意味します。 |
15125 | 14962 | |
15126 | 14963 | =begin original |
15127 | 14964 | |
15128 | 14965 | Specifying VERSION as a literal of the form v5.6.1 should generally be |
15129 | 14966 | avoided, because it leads to misleading error messages under earlier |
15130 | versions of Perl | |
14967 | versions of Perl that do not support this syntax. The equivalent numeric | |
15131 | ||
14968 | version should be used instead. | |
15132 | 14969 | |
15133 | 14970 | =end original |
15134 | 14971 | |
15135 | 14972 | VERSION に v5.6.1 の形のリテラルを指定することは一般的には避けるべきです; |
15136 | なぜなら、この文法に対応していない Perl の初期のバージョン | |
14973 | なぜなら、この文法に対応していない Perl の初期のバージョンでは | |
15137 | ||
14974 | 誤解させるようなエラーメッセージが出るからです。 | |
15138 | 14975 | 代わりに等価な数値表現を使うべきです。 |
15139 | 14976 | |
14977 | =begin original | |
14978 | ||
14979 | Alternatively, you can use a numeric version C<use 5.006> followed by a | |
14980 | v-string version like C<use v5.10.1>, to avoid the unintuitive C<use | |
14981 | 5.010_001>. (older perl versions fail gracefully at the first C<use>, | |
14982 | later perl versions understand the v-string syntax in the second). | |
14983 | ||
14984 | =end original | |
14985 | ||
14986 | あるいは、直感的ではない C<use 5.010_001> を避けるために、数値のバージョン | |
14987 | C<use 5.006> を書いてから v 文字バージョン C<use v5.10.1> を書くという | |
14988 | 方法もあります。 | |
14989 | (古いバージョンの perl では最初の C<use> で安全に失敗し、より新しい | |
14990 | バージョンの perl は 2 番目の v 文字の文法を理解します)。 | |
14991 | ||
15140 | 14992 | use v5.6.1; # compile time version check |
15141 | 14993 | use 5.6.1; # ditto |
15142 | 14994 | use 5.006_001; # ditto; preferred for backwards compatibility |
14995 | use 5.006; use 5.6.1; # ditto, for compatibility and readability | |
15143 | 14996 | |
15144 | 14997 | =begin original |
15145 | 14998 | |
15146 | 14999 | This is often useful if you need to check the current Perl version before |
15147 | C<use>ing library modules that | |
15000 | C<use>ing library modules that have changed in incompatible ways from | |
15148 | (We try not to do this more than we have to.) | |
15001 | older versions of Perl. (We try not to do this more than we have to.) | |
15149 | 15002 | |
15150 | 15003 | =end original |
15151 | 15004 | |
15152 | これは古いバージョンの Perl | |
15005 | これは古いバージョンの Perl から互換性のない形で変更されたライブラリ | |
15153 | 15006 | モジュール(我々は必要な場合以外にそのようなことがないように |
15154 | 15007 | 努力していますが)を C<use> する前に、現在の Perl のバージョンを |
15155 | 15008 | 調べたい場合に有用です。 |
15156 | 15009 | |
15157 | 15010 | =begin original |
15158 | 15011 | |
15159 | 15012 | Also, if the specified perl version is greater than or equal to 5.9.5, |
15160 | 15013 | C<use VERSION> will also load the C<feature> pragma and enable all |
15161 | 15014 | features available in the requested version. See L<feature>. |
15162 | 15015 | |
15163 | 15016 | =end original |
15164 | 15017 | |
15165 | 15018 | また、指定された perl のバージョンが 5.9.5 以上の場合、C<use VERSION> は |
15166 | 15019 | C<feature> プラグマも読み込み、要求されたバージョンで利用可能な全ての機能を |
15167 | 15020 | 有効にします。 |
15168 | 15021 | L<feature> を参照してください。 |
15169 | 15022 | |
15170 | 15023 | =begin original |
15171 | 15024 | |
15172 | 15025 | The C<BEGIN> forces the C<require> and C<import> to happen at compile time. The |
15173 | 15026 | C<require> makes sure the module is loaded into memory if it hasn't been |
15174 | 15027 | yet. The C<import> is not a builtin--it's just an ordinary static method |
15175 | 15028 | call into the C<Module> package to tell the module to import the list of |
15176 | 15029 | features back into the current package. The module can implement its |
15177 | 15030 | C<import> method any way it likes, though most modules just choose to |
15178 | 15031 | derive their C<import> method via inheritance from the C<Exporter> class that |
15179 | 15032 | is defined in the C<Exporter> module. See L<Exporter>. If no C<import> |
15180 | 15033 | method can be found then the call is skipped, even if there is an AUTOLOAD |
15181 | 15034 | method. |
15182 | 15035 | |
15183 | 15036 | =end original |
15184 | 15037 | |
15185 | 15038 | C<BEGIN> によって、C<require> や C<import> は、コンパイル時に |
15186 | 15039 | 実行されることになります。 |
15187 | 15040 | C<require> は、モジュールがまだメモリにロードされていなければ、ロードします。 |
15188 | 15041 | C<import> は、組込みの関数ではなく、さまざまな機能を現在のパッケージに |
15189 | 15042 | インポートするように C<Module> パッケージに伝えるために呼ばれる、 |
15190 | 15043 | 通常の静的メソッドです。 |
15191 | 15044 | モジュール側では、C<import> メソッドをどのようにでも実装することが |
15192 | 15045 | できますが、多くのモジュールでは、C<Exporter> モジュールで定義された、 |
15193 | 15046 | C<Exporter> クラスからの継承によって、C<import> メソッドを行なうように |
15194 | 15047 | しています。 |
15195 | 15048 | L<Exporter>モジュールを参照してください。 |
15196 | 15049 | C<import>メソッドが見つからなかった場合、AUTOLOAD メソッドがあったとしても |
15197 | 15050 | 呼び出しはスキップされます。 |
15198 | 15051 | |
15199 | 15052 | =begin original |
15200 | 15053 | |
15201 | 15054 | If you do not want to call the package's C<import> method (for instance, |
15202 | 15055 | to stop your namespace from being altered), explicitly supply the empty list: |
15203 | 15056 | |
15204 | 15057 | =end original |
15205 | 15058 | |
15206 | 15059 | パッケージの C<import> メソッドを呼び出したくない場合(例えば、名前空間を |
15207 | 15060 | 変更したくない場合など)は、明示的に空リストを指定してください: |
15208 | 15061 | |
15209 | 15062 | use Module (); |
15210 | 15063 | |
15211 | 15064 | =begin original |
15212 | 15065 | |
15213 | 15066 | That is exactly equivalent to |
15214 | 15067 | |
15215 | 15068 | =end original |
15216 | 15069 | |
15217 | 15070 | これは以下と完全に等価です: |
15218 | 15071 | |
15219 | 15072 | BEGIN { require Module } |
15220 | 15073 | |
15221 | 15074 | =begin original |
15222 | 15075 | |
15223 | 15076 | If the VERSION argument is present between Module and LIST, then the |
15224 | 15077 | C<use> will call the VERSION method in class Module with the given |
15225 | 15078 | version as an argument. The default VERSION method, inherited from |
15226 | 15079 | the UNIVERSAL class, croaks if the given version is larger than the |
15227 | 15080 | value of the variable C<$Module::VERSION>. |
15228 | 15081 | |
15229 | 15082 | =end original |
15230 | 15083 | |
15231 | 15084 | Module と LIST の間に VERSION 引数がある場合、C<use> は Module クラスの |
15232 | 15085 | VERSION メソッドを、与えられたバージョンを引数として呼び出します。 |
15233 | 15086 | デフォルトの VERSION メソッドは、 UNIVERSAL クラスから継承したもので、 |
15234 | 15087 | 与えられたバージョンが 変数 C<$Module::VERSION> の値より大きい場合に |
15235 | 15088 | 警告を出します。 |
15236 | 15089 | |
15237 | 15090 | =begin original |
15238 | 15091 | |
15239 | 15092 | Again, there is a distinction between omitting LIST (C<import> called |
15240 | 15093 | with no arguments) and an explicit empty LIST C<()> (C<import> not |
15241 | 15094 | called). Note that there is no comma after VERSION! |
15242 | 15095 | |
15243 | 15096 | =end original |
15244 | 15097 | |
15245 | 15098 | 繰り返すと、LIST を省略する(C<import> が引数なしで呼び出される)ことと |
15246 | 15099 | 明示的に空の LIST C<()> を指定する (C<import> は呼び出されない)ことは |
15247 | 15100 | 違います。 |
15248 | 15101 | VERSION の後ろにカンマが不要なことに注意してください! |
15249 | 15102 | |
15250 | 15103 | =begin original |
15251 | 15104 | |
15252 | 15105 | Because this is a wide-open interface, pragmas (compiler directives) |
15253 | 15106 | are also implemented this way. Currently implemented pragmas are: |
15254 | 15107 | |
15255 | 15108 | =end original |
15256 | 15109 | |
15257 | 15110 | これは、広く公開されているインタフェースですので、 |
15258 | 15111 | プラグマ (コンパイラディレクティブ) も、この方法で実装されています。 |
15259 | 15112 | 現在実装されているプラグマには、以下のものがあります: |
15260 | 15113 | |
15261 | 15114 | use constant; |
15262 | 15115 | use diagnostics; |
15263 | 15116 | use integer; |
15264 | 15117 | use sigtrap qw(SEGV BUS); |
15265 | 15118 | use strict qw(subs vars refs); |
15266 | 15119 | use subs qw(afunc blurfl); |
15267 | 15120 | use warnings qw(all); |
15268 | 15121 | use sort qw(stable _quicksort _mergesort); |
15269 | 15122 | |
15270 | 15123 | =begin original |
15271 | 15124 | |
15272 | 15125 | Some of these pseudo-modules import semantics into the current |
15273 | 15126 | block scope (like C<strict> or C<integer>, unlike ordinary modules, |
15274 | 15127 | which import symbols into the current package (which are effective |
15275 | 15128 | through the end of the file). |
15276 | 15129 | |
15277 | 15130 | =end original |
15278 | 15131 | |
15279 | 15132 | 通常のモジュールが、現在のパッケージにシンボルをインポートする |
15280 | 15133 | (これは、ファイルの終わりまで有効です) のに対して、 |
15281 | 15134 | これらの擬似モジュールの一部(C<strict> や C<integer> など)は、 |
15282 | 15135 | 現在のブロックスコープにインポートを行ないます。 |
15283 | 15136 | |
15284 | 15137 | =begin original |
15285 | 15138 | |
15286 | 15139 | There's a corresponding C<no> command that unimports meanings imported |
15287 | 15140 | by C<use>, i.e., it calls C<unimport Module LIST> instead of C<import>. |
15288 | 15141 | It behaves exactly as C<import> does with respect to VERSION, an |
15289 | 15142 | omitted LIST, empty LIST, or no unimport method being found. |
15290 | 15143 | |
15291 | 15144 | =end original |
15292 | 15145 | |
15293 | 15146 | これに対して、C<no> コマンドという、C<use> によってインポートされたものを、 |
15294 | 15147 | インポートされていないことにするものがあります。 |
15295 | 15148 | つまり、C<import> の代わりに C<unimport Module LIST> を呼び出します。 |
15296 | 15149 | これは VERSION、省略された LIST、空の LIST、unimport メソッドが見つからない |
15297 | 15150 | 場合などの観点では、正確に C<import> と同様に振る舞います。 |
15298 | 15151 | |
15299 | 15152 | no integer; |
15300 | 15153 | no strict 'refs'; |
15301 | 15154 | no warnings; |
15302 | 15155 | |
15303 | 15156 | =begin original |
15304 | 15157 | |
15305 | 15158 | See L<perlmodlib> for a list of standard modules and pragmas. See L<perlrun> |
15306 | 15159 | for the C<-M> and C<-m> command-line options to perl that give C<use> |
15307 | 15160 | functionality from the command-line. |
15308 | 15161 | |
15309 | 15162 | =end original |
15310 | 15163 | |
15311 | 15164 | 標準モジュールやプラグマの一覧は、L<perlmodlib> を参照してください。 |
15312 | 15165 | コマンドラインから C<use> 機能を指定するための C<-M> と C<-m> の |
15313 | 15166 | コマンドラインオプションについては L<perlrun> を参照して下さい。 |
15314 | 15167 | |
15315 | 15168 | =item utime LIST |
15316 | 15169 | X<utime> |
15317 | 15170 | |
15318 | 15171 | =begin original |
15319 | 15172 | |
15320 | 15173 | Changes the access and modification times on each file of a list of |
15321 | 15174 | files. The first two elements of the list must be the NUMERICAL access |
15322 | 15175 | and modification times, in that order. Returns the number of files |
15323 | 15176 | successfully changed. The inode change time of each file is set |
15324 | 15177 | to the current time. For example, this code has the same effect as the |
15325 | 15178 | Unix touch(1) command when the files I<already exist> and belong to |
15326 | 15179 | the user running the program: |
15327 | 15180 | |
15328 | 15181 | =end original |
15329 | 15182 | |
15330 | 15183 | ファイルのアクセス時刻と修正(modification) 時刻を変更します。 |
15331 | 15184 | LIST の最初の 2 つの要素に、数値で表わしたアクセス時刻と修正時刻を |
15332 | 15185 | 順に指定します。 |
15333 | 15186 | LIST の残りの要素が、変更の対象となるファイルです。 |
15334 | 15187 | 変更に成功したファイルの数を返します。 |
15335 | 15188 | 各ファイルの inode 変更(change)時刻には、その時点の時刻が設定されます。 |
15336 | 15189 | 例えば、このコードはファイルが I<既に存在して> いて、ユーザーが |
15337 | 15190 | 実行しているプログラムに従っているなら、 |
15338 | 15191 | Unix の touch(1) コマンドと同じ効果があります。 |
15339 | 15192 | |
15340 | 15193 | #!/usr/bin/perl |
15341 | 15194 | $atime = $mtime = time; |
15342 | 15195 | utime $atime, $mtime, @ARGV; |
15343 | 15196 | |
15344 | 15197 | =begin original |
15345 | 15198 | |
15346 | 15199 | Since perl 5.7.2, if the first two elements of the list are C<undef>, then |
15347 | 15200 | the utime(2) function in the C library will be called with a null second |
15348 | 15201 | argument. On most systems, this will set the file's access and |
15349 | 15202 | modification times to the current time (i.e. equivalent to the example |
15350 | 15203 | above) and will even work on other users' files where you have write |
15351 | 15204 | permission: |
15352 | 15205 | |
15353 | 15206 | =end original |
15354 | 15207 | |
15355 | 15208 | perl 5.7.2 から、リストの最初の二つの要素が C<undef> である場合、 |
15356 | 15209 | C ライブラリの utime(2) 関数を、秒の引数を null として呼び出します。 |
15357 | 15210 | ほとんどのシステムでは、これによってファイルのアクセス時刻と修正時刻を |
15358 | 15211 | 現在の時刻にセットし(つまり、上記の例と等価です)、 |
15359 | 15212 | 書き込み権限があれば他のユーザーのファイルに対しても動作します。 |
15360 | 15213 | |
15361 | 15214 | utime undef, undef, @ARGV; |
15362 | 15215 | |
15363 | 15216 | =begin original |
15364 | 15217 | |
15365 | 15218 | Under NFS this will use the time of the NFS server, not the time of |
15366 | 15219 | the local machine. If there is a time synchronization problem, the |
15367 | 15220 | NFS server and local machine will have different times. The Unix |
15368 | 15221 | touch(1) command will in fact normally use this form instead of the |
15369 | 15222 | one shown in the first example. |
15370 | 15223 | |
15371 | 15224 | =end original |
15372 | 15225 | |
15373 | 15226 | NFS では、これはローカルマシンの時刻ではなく、NFS サーバーの時刻が |
15374 | 15227 | 使われます。 |
15375 | 15228 | 時刻同期に問題がある場合、NFS サーバーとローカルマシンで違う時刻に |
15376 | 15229 | なっている場合があります。 |
15377 | 15230 | 実際のところ、Unix の touch(1) コマンドは普通、最初の例ではなく、 |
15378 | 15231 | この形を使います。 |
15379 | 15232 | |
15380 | 15233 | =begin original |
15381 | 15234 | |
15382 | 15235 | Note that only passing one of the first two elements as C<undef> will |
15383 | 15236 | be equivalent of passing it as 0 and will not have the same effect as |
15384 | 15237 | described when they are both C<undef>. This case will also trigger an |
15385 | 15238 | uninitialized warning. |
15386 | 15239 | |
15387 | 15240 | =end original |
15388 | 15241 | |
15389 | 15242 | 最初の二つの要素のうち、一つだけに C<undef> を渡すと、その要素は 0 を |
15390 | 15243 | 渡すのと等価となり、上述の、両方に C<undef> を渡した時と同じ |
15391 | 15244 | 効果ではないことに注意してください。 |
15392 | 15245 | この場合は、未初期化の警告が出ます。 |
15393 | 15246 | |
15394 | 15247 | =begin original |
15395 | 15248 | |
15396 | 15249 | On systems that support futimes, you might pass file handles among the |
15397 | 15250 | files. On systems that don't support futimes, passing file handles |
15398 | 15251 | produces a fatal error at run time. The file handles must be passed |
15399 | 15252 | as globs or references to be recognized. Barewords are considered |
15400 | 15253 | file names. |
15401 | 15254 | |
15402 | 15255 | =end original |
15403 | 15256 | |
15404 | 15257 | futimes に対応しているシステムでは、ファイルハンドルを引数として渡せます。 |
15405 | 15258 | futimes に対応していないシステムでは、ファイルハンドルを渡すと実行時に |
15406 | 15259 | 致命的エラーになります。 |
15407 | 15260 | ファイルハンドルを認識させるためには、グロブまたはリファレンスとして |
15408 | 15261 | 渡されなければなりません。 |
15409 | 15262 | 裸の単語はファイル名として扱われます。 |
15410 | 15263 | |
15411 | 15264 | =item values HASH |
15412 | 15265 | X<values> |
15413 | 15266 | |
15414 | 15267 | =begin original |
15415 | 15268 | |
15416 | 15269 | Returns a list consisting of all the values of the named hash. |
15417 | 15270 | (In a scalar context, returns the number of values.) |
15418 | 15271 | |
15419 | 15272 | =end original |
15420 | 15273 | |
15421 | 15274 | 指定したハッシュのすべての value からなるリストを返します。 |
15422 | 15275 | (スカラコンテキストでは、value の数を返します。) |
15423 | 15276 | |
15424 | 15277 | =begin original |
15425 | 15278 | |
15426 | 15279 | The values are returned in an apparently random order. The actual |
15427 | 15280 | random order is subject to change in future versions of perl, but it |
15428 | 15281 | is guaranteed to be the same order as either the C<keys> or C<each> |
15429 | 15282 | function would produce on the same (unmodified) hash. Since Perl |
15430 | 15283 | 5.8.1 the ordering is different even between different runs of Perl |
15431 | 15284 | for security reasons (see L<perlsec/"Algorithmic Complexity Attacks">). |
15432 | 15285 | |
15433 | 15286 | =end original |
15434 | 15287 | |
15435 | 15288 | 返される value の順序は、見た目にばらばらなものです。 |
15436 | 15289 | 実際のランダムな順序は将来のバージョンの perl では変わる可能性が |
15437 | 15290 | ありますが、同じ(変更されていない)ハッシュに対して、 |
15438 | 15291 | C<keys>関数や C<each>関数が返すものと同じ順序であることは保証されます。 |
15439 | 15292 | Perl 5.8.1 以降ではセキュリティ上の理由により、 |
15440 | 15293 | 実行される毎に順番は変わります |
15441 | 15294 | (L<perlsec/"Algorithmic Complexity Attacks"> を参照してください)。 |
15442 | 15295 | |
15443 | 15296 | =begin original |
15444 | 15297 | |
15445 | 15298 | As a side effect, calling values() resets the HASH's internal iterator, |
15446 | 15299 | see L</each>. (In particular, calling values() in void context resets |
15447 | 15300 | the iterator with no other overhead.) |
15448 | 15301 | |
15449 | 15302 | =end original |
15450 | 15303 | |
15451 | 15304 | 副作用として、values() を呼び出すと HASH の内部イテレータをリセットします: |
15452 | 15305 | C</each> を参照してください。 |
15453 | 15306 | (特に、values() を無効コンテキストで呼び出すとその他のオーバーヘッドなしで |
15454 | 15307 | イテレータをリセットします。) |
15455 | 15308 | |
15456 | 15309 | =begin original |
15457 | 15310 | |
15458 | 15311 | Note that the values are not copied, which means modifying them will |
15459 | 15312 | modify the contents of the hash: |
15460 | 15313 | |
15461 | 15314 | =end original |
15462 | 15315 | |
15463 | 15316 | 値はコピーされないので、返されたリストを変更すると |
15464 | 15317 | ハッシュの中身が変更されることに注意してください。 |
15465 | 15318 | |
15466 | 15319 | for (values %hash) { s/foo/bar/g } # modifies %hash values |
15467 | 15320 | for (@hash{keys %hash}) { s/foo/bar/g } # same |
15468 | 15321 | |
15469 | 15322 | =begin original |
15470 | 15323 | |
15471 | 15324 | See also C<keys>, C<each>, and C<sort>. |
15472 | 15325 | |
15473 | 15326 | =end original |
15474 | 15327 | |
15475 | 15328 | C<keys>, C<each>, C<sort> も参照してください。 |
15476 | 15329 | |
15477 | 15330 | =item vec EXPR,OFFSET,BITS |
15478 | 15331 | X<vec> X<bit> X<bit vector> |
15479 | 15332 | |
15480 | 15333 | =begin original |
15481 | 15334 | |
15482 | 15335 | Treats the string in EXPR as a bit vector made up of elements of |
15483 | 15336 | width BITS, and returns the value of the element specified by OFFSET |
15484 | 15337 | as an unsigned integer. BITS therefore specifies the number of bits |
15485 | 15338 | that are reserved for each element in the bit vector. This must |
15486 | 15339 | be a power of two from 1 to 32 (or 64, if your platform supports |
15487 | 15340 | that). |
15488 | 15341 | |
15489 | 15342 | =end original |
15490 | 15343 | |
15491 | 15344 | 文字列 EXPR を BITS 幅の要素からなるビットベクターとして扱い、 |
15492 | 15345 | OFFSET で指定される要素の値を返します。 |
15493 | 15346 | OFFSET で指定された要素を符号なし整数として返します。 |
15494 | 15347 | 従って、 BITS はビットベクターの中の各要素について予約されるビット数です。 |
15495 | 15348 | BIT は、1 から 32 まで(プラットホームが |
15496 | 15349 | 対応していれば 64 まで) の 2 のべき乗でなければなりません。 |
15497 | 15350 | |
15498 | 15351 | =begin original |
15499 | 15352 | |
15500 | 15353 | If BITS is 8, "elements" coincide with bytes of the input string. |
15501 | 15354 | |
15502 | 15355 | =end original |
15503 | 15356 | |
15504 | 15357 | BITS が 8 の場合、「要素」は入力文字列の各バイトと一致します。 |
15505 | 15358 | |
15506 | 15359 | =begin original |
15507 | 15360 | |
15508 | 15361 | If BITS is 16 or more, bytes of the input string are grouped into chunks |
15509 | 15362 | of size BITS/8, and each group is converted to a number as with |
15510 | 15363 | pack()/unpack() with big-endian formats C<n>/C<N> (and analogously |
15511 | 15364 | for BITS==64). See L<"pack"> for details. |
15512 | 15365 | |
15513 | 15366 | =end original |
15514 | 15367 | |
15515 | 15368 | BITS が 16 以上の場合、入力のバイト列は BITS/8 のサイズの固まりに |
15516 | 15369 | グループ化され、各グループは pack()/unpack() のビッグエンディアン |
15517 | 15370 | フォーマット C<n>/C<N> を用いて(BITS==64 の類似として)数値に変換されます。 |
15518 | 15371 | 詳細は L<"pack"> を参照してください。 |
15519 | 15372 | |
15520 | 15373 | =begin original |
15521 | 15374 | |
15522 | 15375 | If bits is 4 or less, the string is broken into bytes, then the bits |
15523 | 15376 | of each byte are broken into 8/BITS groups. Bits of a byte are |
15524 | 15377 | numbered in a little-endian-ish way, as in C<0x01>, C<0x02>, |
15525 | 15378 | C<0x04>, C<0x08>, C<0x10>, C<0x20>, C<0x40>, C<0x80>. For example, |
15526 | 15379 | breaking the single input byte C<chr(0x36)> into two groups gives a list |
15527 | 15380 | C<(0x6, 0x3)>; breaking it into 4 groups gives C<(0x2, 0x1, 0x3, 0x0)>. |
15528 | 15381 | |
15529 | 15382 | =end original |
15530 | 15383 | |
15531 | 15384 | BITS が 4 以下の場合、文字列はバイトに分解され、バイトの各ビットは |
15532 | 15385 | 8/BITS 個のグループに分割されます。 |
15533 | 15386 | ビットはリトルエンディアン風に、C<0x01>, C<0x02>, |
15534 | 15387 | C<0x04>, C<0x08>, C<0x10>, C<0x20>, C<0x40>, C<0x80> の順になります。 |
15535 | 15388 | 例えば、入力バイト C<chr(0x36)> を 2 つのグループに分割すると、 |
15536 | 15389 | C<(0x6, 0x3)> になります。 |
15537 | 15390 | 4 つに分割すると C<(0x2, 0x1, 0x3, 0x0)> になります。 |
15538 | 15391 | |
15539 | 15392 | =begin original |
15540 | 15393 | |
15541 | 15394 | C<vec> may also be assigned to, in which case parentheses are needed |
15542 | 15395 | to give the expression the correct precedence as in |
15543 | 15396 | |
15544 | 15397 | =end original |
15545 | 15398 | |
15546 | 15399 | 左辺値として、代入の対象にすることもできます。 |
15547 | 15400 | この場合、式を正しく先行させるために以下のように括弧が必要です。 |
15548 | 15401 | |
15549 | 15402 | vec($image, $max_x * $x + $y, 8) = 3; |
15550 | 15403 | |
15551 | 15404 | =begin original |
15552 | 15405 | |
15553 | 15406 | If the selected element is outside the string, the value 0 is returned. |
15554 | 15407 | If an element off the end of the string is written to, Perl will first |
15555 | 15408 | extend the string with sufficiently many zero bytes. It is an error |
15556 | 15409 | to try to write off the beginning of the string (i.e. negative OFFSET). |
15557 | 15410 | |
15558 | 15411 | =end original |
15559 | 15412 | |
15560 | 15413 | 選択された要素が文字列の外側だった場合、値 0 が返されます。 |
15561 | 15414 | 文字列の最後よりも後ろの要素に書き込もうとした場合、 |
15562 | 15415 | Perl はまず文字列を必要な分だけ 0 のバイトで拡張します。 |
15563 | 15416 | 文字列の先頭より前に書き込もうとした(つまり OFFSET が負の数だった) |
15564 | 15417 | 場合はエラーとなります。 |
15565 | 15418 | |
15566 | 15419 | =begin original |
15567 | 15420 | |
15568 | 15421 | If the string happens to be encoded as UTF-8 internally (and thus has |
15569 | 15422 | the UTF8 flag set), this is ignored by C<vec>, and it operates on the |
15570 | 15423 | internal byte string, not the conceptual character string, even if you |
15571 | 15424 | only have characters with values less than 256. |
15572 | 15425 | |
15573 | 15426 | =end original |
15574 | 15427 | |
15575 | 15428 | 文字列がなぜか内部で UTF-8 でエンコードされている場合(したがって UTF8 フラグが |
15576 | 15429 | セットされている場合)、これは C<vec> では無視され、たとえ値が 256 未満の |
15577 | 15430 | 文字だけであったとしても、概念的な |
15578 | 15431 | 文字列ではなく内部バイト文字列で操作されます。 |
15579 | 15432 | |
15580 | 15433 | =begin original |
15581 | 15434 | |
15582 | 15435 | Strings created with C<vec> can also be manipulated with the logical |
15583 | 15436 | operators C<|>, C<&>, C<^>, and C<~>. These operators will assume a bit |
15584 | 15437 | vector operation is desired when both operands are strings. |
15585 | 15438 | See L<perlop/"Bitwise String Operators">. |
15586 | 15439 | |
15587 | 15440 | =end original |
15588 | 15441 | |
15589 | 15442 | vec() で作られた文字列は、論理演算子 C<|>、C<&>、C<^> で扱うこともできます。 |
15590 | 15443 | これらの演算子は、両方の被演算子に文字列を使うと、 |
15591 | 15444 | ビットベクター演算を行ないます。 |
15592 | 15445 | |
15593 | 15446 | =begin original |
15594 | 15447 | |
15595 | 15448 | The following code will build up an ASCII string saying C<'PerlPerlPerl'>. |
15596 | 15449 | The comments show the string after each step. Note that this code works |
15597 | 15450 | in the same way on big-endian or little-endian machines. |
15598 | 15451 | |
15599 | 15452 | =end original |
15600 | 15453 | |
15601 | 15454 | 次のコードは C<'PerlPerlPerl'> という ASCII 文字列を作成します。 |
15602 | 15455 | コメントは各行の実行後の文字列を示します。 |
15603 | 15456 | このコードはビッグエンディアンでもリトルエンディアンでも同じように |
15604 | 15457 | 動作することに注意してください。 |
15605 | 15458 | |
15606 | 15459 | my $foo = ''; |
15607 | 15460 | vec($foo, 0, 32) = 0x5065726C; # 'Perl' |
15608 | 15461 | |
15609 | 15462 | # $foo eq "Perl" eq "\x50\x65\x72\x6C", 32 bits |
15610 | 15463 | print vec($foo, 0, 8); # prints 80 == 0x50 == ord('P') |
15611 | 15464 | |
15612 | 15465 | vec($foo, 2, 16) = 0x5065; # 'PerlPe' |
15613 | 15466 | vec($foo, 3, 16) = 0x726C; # 'PerlPerl' |
15614 | 15467 | vec($foo, 8, 8) = 0x50; # 'PerlPerlP' |
15615 | 15468 | vec($foo, 9, 8) = 0x65; # 'PerlPerlPe' |
15616 | 15469 | vec($foo, 20, 4) = 2; # 'PerlPerlPe' . "\x02" |
15617 | 15470 | vec($foo, 21, 4) = 7; # 'PerlPerlPer' |
15618 | 15471 | # 'r' is "\x72" |
15619 | 15472 | vec($foo, 45, 2) = 3; # 'PerlPerlPer' . "\x0c" |
15620 | 15473 | vec($foo, 93, 1) = 1; # 'PerlPerlPer' . "\x2c" |
15621 | 15474 | vec($foo, 94, 1) = 1; # 'PerlPerlPerl' |
15622 | 15475 | # 'l' is "\x6c" |
15623 | 15476 | |
15624 | 15477 | =begin original |
15625 | 15478 | |
15626 | 15479 | To transform a bit vector into a string or list of 0's and 1's, use these: |
15627 | 15480 | |
15628 | 15481 | =end original |
15629 | 15482 | |
15630 | 15483 | ビットベクターを、0 と 1 の文字列や配列に変換するには、 |
15631 | 15484 | 以下のようにします。 |
15632 | 15485 | |
15633 | 15486 | $bits = unpack("b*", $vector); |
15634 | 15487 | @bits = split(//, unpack("b*", $vector)); |
15635 | 15488 | |
15636 | 15489 | =begin original |
15637 | 15490 | |
15638 | 15491 | If you know the exact length in bits, it can be used in place of the C<*>. |
15639 | 15492 | |
15640 | 15493 | =end original |
15641 | 15494 | |
15642 | 15495 | ビット長が分かっていれば、* の代わりにその長さを使うことができます。 |
15643 | 15496 | |
15644 | 15497 | =begin original |
15645 | 15498 | |
15646 | 15499 | Here is an example to illustrate how the bits actually fall in place: |
15647 | 15500 | |
15648 | 15501 | =end original |
15649 | 15502 | |
15650 | 15503 | これはビットが実際にどのような位置に入るかを図示する例です。 |
15651 | 15504 | |
15652 | 15505 | #!/usr/bin/perl -wl |
15653 | 15506 | |
15654 | 15507 | print <<'EOT'; |
15655 | 15508 | 0 1 2 3 |
15656 | 15509 | unpack("V",$_) 01234567890123456789012345678901 |
15657 | 15510 | ------------------------------------------------------------------ |
15658 | 15511 | EOT |
15659 | 15512 | |
15660 | 15513 | for $w (0..3) { |
15661 | 15514 | $width = 2**$w; |
15662 | 15515 | for ($shift=0; $shift < $width; ++$shift) { |
15663 | 15516 | for ($off=0; $off < 32/$width; ++$off) { |
15664 | 15517 | $str = pack("B*", "0"x32); |
15665 | 15518 | $bits = (1<<$shift); |
15666 | 15519 | vec($str, $off, $width) = $bits; |
15667 | 15520 | $res = unpack("b*",$str); |
15668 | 15521 | $val = unpack("V", $str); |
15669 | 15522 | write; |
15670 | 15523 | } |
15671 | 15524 | } |
15672 | 15525 | } |
15673 | 15526 | |
15674 | 15527 | format STDOUT = |
15675 | 15528 | vec($_,@#,@#) = @<< == @######### @>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> |
15676 | 15529 | $off, $width, $bits, $val, $res |
15677 | 15530 | . |
15678 | 15531 | __END__ |
15679 | 15532 | |
15680 | 15533 | =begin original |
15681 | 15534 | |
15682 | 15535 | Regardless of the machine architecture on which it is run, the above |
15683 | 15536 | example should print the following table: |
15684 | 15537 | |
15685 | 15538 | =end original |
15686 | 15539 | |
15687 | 15540 | 実行するマシンのアーキテクチャに関わらず、 |
15688 | 15541 | 上記の例は以下の表を出力します。 |
15689 | 15542 | |
15690 | 15543 | 0 1 2 3 |
15691 | 15544 | unpack("V",$_) 01234567890123456789012345678901 |
15692 | 15545 | ------------------------------------------------------------------ |
15693 | 15546 | vec($_, 0, 1) = 1 == 1 10000000000000000000000000000000 |
15694 | 15547 | vec($_, 1, 1) = 1 == 2 01000000000000000000000000000000 |
15695 | 15548 | vec($_, 2, 1) = 1 == 4 00100000000000000000000000000000 |
15696 | 15549 | vec($_, 3, 1) = 1 == 8 00010000000000000000000000000000 |
15697 | 15550 | vec($_, 4, 1) = 1 == 16 00001000000000000000000000000000 |
15698 | 15551 | vec($_, 5, 1) = 1 == 32 00000100000000000000000000000000 |
15699 | 15552 | vec($_, 6, 1) = 1 == 64 00000010000000000000000000000000 |
15700 | 15553 | vec($_, 7, 1) = 1 == 128 00000001000000000000000000000000 |
15701 | 15554 | vec($_, 8, 1) = 1 == 256 00000000100000000000000000000000 |
15702 | 15555 | vec($_, 9, 1) = 1 == 512 00000000010000000000000000000000 |
15703 | 15556 | vec($_,10, 1) = 1 == 1024 00000000001000000000000000000000 |
15704 | 15557 | vec($_,11, 1) = 1 == 2048 00000000000100000000000000000000 |
15705 | 15558 | vec($_,12, 1) = 1 == 4096 00000000000010000000000000000000 |
15706 | 15559 | vec($_,13, 1) = 1 == 8192 00000000000001000000000000000000 |
15707 | 15560 | vec($_,14, 1) = 1 == 16384 00000000000000100000000000000000 |
15708 | 15561 | vec($_,15, 1) = 1 == 32768 00000000000000010000000000000000 |
15709 | 15562 | vec($_,16, 1) = 1 == 65536 00000000000000001000000000000000 |
15710 | 15563 | vec($_,17, 1) = 1 == 131072 00000000000000000100000000000000 |
15711 | 15564 | vec($_,18, 1) = 1 == 262144 00000000000000000010000000000000 |
15712 | 15565 | vec($_,19, 1) = 1 == 524288 00000000000000000001000000000000 |
15713 | 15566 | vec($_,20, 1) = 1 == 1048576 00000000000000000000100000000000 |
15714 | 15567 | vec($_,21, 1) = 1 == 2097152 00000000000000000000010000000000 |
15715 | 15568 | vec($_,22, 1) = 1 == 4194304 00000000000000000000001000000000 |
15716 | 15569 | vec($_,23, 1) = 1 == 8388608 00000000000000000000000100000000 |
15717 | 15570 | vec($_,24, 1) = 1 == 16777216 00000000000000000000000010000000 |
15718 | 15571 | vec($_,25, 1) = 1 == 33554432 00000000000000000000000001000000 |
15719 | 15572 | vec($_,26, 1) = 1 == 67108864 00000000000000000000000000100000 |
15720 | 15573 | vec($_,27, 1) = 1 == 134217728 00000000000000000000000000010000 |
15721 | 15574 | vec($_,28, 1) = 1 == 268435456 00000000000000000000000000001000 |
15722 | 15575 | vec($_,29, 1) = 1 == 536870912 00000000000000000000000000000100 |
15723 | 15576 | vec($_,30, 1) = 1 == 1073741824 00000000000000000000000000000010 |
15724 | 15577 | vec($_,31, 1) = 1 == 2147483648 00000000000000000000000000000001 |
15725 | 15578 | vec($_, 0, 2) = 1 == 1 10000000000000000000000000000000 |
15726 | 15579 | vec($_, 1, 2) = 1 == 4 00100000000000000000000000000000 |
15727 | 15580 | vec($_, 2, 2) = 1 == 16 00001000000000000000000000000000 |
15728 | 15581 | vec($_, 3, 2) = 1 == 64 00000010000000000000000000000000 |
15729 | 15582 | vec($_, 4, 2) = 1 == 256 00000000100000000000000000000000 |
15730 | 15583 | vec($_, 5, 2) = 1 == 1024 00000000001000000000000000000000 |
15731 | 15584 | vec($_, 6, 2) = 1 == 4096 00000000000010000000000000000000 |
15732 | 15585 | vec($_, 7, 2) = 1 == 16384 00000000000000100000000000000000 |
15733 | 15586 | vec($_, 8, 2) = 1 == 65536 00000000000000001000000000000000 |
15734 | 15587 | vec($_, 9, 2) = 1 == 262144 00000000000000000010000000000000 |
15735 | 15588 | vec($_,10, 2) = 1 == 1048576 00000000000000000000100000000000 |
15736 | 15589 | vec($_,11, 2) = 1 == 4194304 00000000000000000000001000000000 |
15737 | 15590 | vec($_,12, 2) = 1 == 16777216 00000000000000000000000010000000 |
15738 | 15591 | vec($_,13, 2) = 1 == 67108864 00000000000000000000000000100000 |
15739 | 15592 | vec($_,14, 2) = 1 == 268435456 00000000000000000000000000001000 |
15740 | 15593 | vec($_,15, 2) = 1 == 1073741824 00000000000000000000000000000010 |
15741 | 15594 | vec($_, 0, 2) = 2 == 2 01000000000000000000000000000000 |
15742 | 15595 | vec($_, 1, 2) = 2 == 8 00010000000000000000000000000000 |
15743 | 15596 | vec($_, 2, 2) = 2 == 32 00000100000000000000000000000000 |
15744 | 15597 | vec($_, 3, 2) = 2 == 128 00000001000000000000000000000000 |
15745 | 15598 | vec($_, 4, 2) = 2 == 512 00000000010000000000000000000000 |
15746 | 15599 | vec($_, 5, 2) = 2 == 2048 00000000000100000000000000000000 |
15747 | 15600 | vec($_, 6, 2) = 2 == 8192 00000000000001000000000000000000 |
15748 | 15601 | vec($_, 7, 2) = 2 == 32768 00000000000000010000000000000000 |
15749 | 15602 | vec($_, 8, 2) = 2 == 131072 00000000000000000100000000000000 |
15750 | 15603 | vec($_, 9, 2) = 2 == 524288 00000000000000000001000000000000 |
15751 | 15604 | vec($_,10, 2) = 2 == 2097152 00000000000000000000010000000000 |
15752 | 15605 | vec($_,11, 2) = 2 == 8388608 00000000000000000000000100000000 |
15753 | 15606 | vec($_,12, 2) = 2 == 33554432 00000000000000000000000001000000 |
15754 | 15607 | vec($_,13, 2) = 2 == 134217728 00000000000000000000000000010000 |
15755 | 15608 | vec($_,14, 2) = 2 == 536870912 00000000000000000000000000000100 |
15756 | 15609 | vec($_,15, 2) = 2 == 2147483648 00000000000000000000000000000001 |
15757 | 15610 | vec($_, 0, 4) = 1 == 1 10000000000000000000000000000000 |
15758 | 15611 | vec($_, 1, 4) = 1 == 16 00001000000000000000000000000000 |
15759 | 15612 | vec($_, 2, 4) = 1 == 256 00000000100000000000000000000000 |
15760 | 15613 | vec($_, 3, 4) = 1 == 4096 00000000000010000000000000000000 |
15761 | 15614 | vec($_, 4, 4) = 1 == 65536 00000000000000001000000000000000 |
15762 | 15615 | vec($_, 5, 4) = 1 == 1048576 00000000000000000000100000000000 |
15763 | 15616 | vec($_, 6, 4) = 1 == 16777216 00000000000000000000000010000000 |
15764 | 15617 | vec($_, 7, 4) = 1 == 268435456 00000000000000000000000000001000 |
15765 | 15618 | vec($_, 0, 4) = 2 == 2 01000000000000000000000000000000 |
15766 | 15619 | vec($_, 1, 4) = 2 == 32 00000100000000000000000000000000 |
15767 | 15620 | vec($_, 2, 4) = 2 == 512 00000000010000000000000000000000 |
15768 | 15621 | vec($_, 3, 4) = 2 == 8192 00000000000001000000000000000000 |
15769 | 15622 | vec($_, 4, 4) = 2 == 131072 00000000000000000100000000000000 |
15770 | 15623 | vec($_, 5, 4) = 2 == 2097152 00000000000000000000010000000000 |
15771 | 15624 | vec($_, 6, 4) = 2 == 33554432 00000000000000000000000001000000 |
15772 | 15625 | vec($_, 7, 4) = 2 == 536870912 00000000000000000000000000000100 |
15773 | 15626 | vec($_, 0, 4) = 4 == 4 00100000000000000000000000000000 |
15774 | 15627 | vec($_, 1, 4) = 4 == 64 00000010000000000000000000000000 |
15775 | 15628 | vec($_, 2, 4) = 4 == 1024 00000000001000000000000000000000 |
15776 | 15629 | vec($_, 3, 4) = 4 == 16384 00000000000000100000000000000000 |
15777 | 15630 | vec($_, 4, 4) = 4 == 262144 00000000000000000010000000000000 |
15778 | 15631 | vec($_, 5, 4) = 4 == 4194304 00000000000000000000001000000000 |
15779 | 15632 | vec($_, 6, 4) = 4 == 67108864 00000000000000000000000000100000 |
15780 | 15633 | vec($_, 7, 4) = 4 == 1073741824 00000000000000000000000000000010 |
15781 | 15634 | vec($_, 0, 4) = 8 == 8 00010000000000000000000000000000 |
15782 | 15635 | vec($_, 1, 4) = 8 == 128 00000001000000000000000000000000 |
15783 | 15636 | vec($_, 2, 4) = 8 == 2048 00000000000100000000000000000000 |
15784 | 15637 | vec($_, 3, 4) = 8 == 32768 00000000000000010000000000000000 |
15785 | 15638 | vec($_, 4, 4) = 8 == 524288 00000000000000000001000000000000 |
15786 | 15639 | vec($_, 5, 4) = 8 == 8388608 00000000000000000000000100000000 |
15787 | 15640 | vec($_, 6, 4) = 8 == 134217728 00000000000000000000000000010000 |
15788 | 15641 | vec($_, 7, 4) = 8 == 2147483648 00000000000000000000000000000001 |
15789 | 15642 | vec($_, 0, 8) = 1 == 1 10000000000000000000000000000000 |
15790 | 15643 | vec($_, 1, 8) = 1 == 256 00000000100000000000000000000000 |
15791 | 15644 | vec($_, 2, 8) = 1 == 65536 00000000000000001000000000000000 |
15792 | 15645 | vec($_, 3, 8) = 1 == 16777216 00000000000000000000000010000000 |
15793 | 15646 | vec($_, 0, 8) = 2 == 2 01000000000000000000000000000000 |
15794 | 15647 | vec($_, 1, 8) = 2 == 512 00000000010000000000000000000000 |
15795 | 15648 | vec($_, 2, 8) = 2 == 131072 00000000000000000100000000000000 |
15796 | 15649 | vec($_, 3, 8) = 2 == 33554432 00000000000000000000000001000000 |
15797 | 15650 | vec($_, 0, 8) = 4 == 4 00100000000000000000000000000000 |
15798 | 15651 | vec($_, 1, 8) = 4 == 1024 00000000001000000000000000000000 |
15799 | 15652 | vec($_, 2, 8) = 4 == 262144 00000000000000000010000000000000 |
15800 | 15653 | vec($_, 3, 8) = 4 == 67108864 00000000000000000000000000100000 |
15801 | 15654 | vec($_, 0, 8) = 8 == 8 00010000000000000000000000000000 |
15802 | 15655 | vec($_, 1, 8) = 8 == 2048 00000000000100000000000000000000 |
15803 | 15656 | vec($_, 2, 8) = 8 == 524288 00000000000000000001000000000000 |
15804 | 15657 | vec($_, 3, 8) = 8 == 134217728 00000000000000000000000000010000 |
15805 | 15658 | vec($_, 0, 8) = 16 == 16 00001000000000000000000000000000 |
15806 | 15659 | vec($_, 1, 8) = 16 == 4096 00000000000010000000000000000000 |
15807 | 15660 | vec($_, 2, 8) = 16 == 1048576 00000000000000000000100000000000 |
15808 | 15661 | vec($_, 3, 8) = 16 == 268435456 00000000000000000000000000001000 |
15809 | 15662 | vec($_, 0, 8) = 32 == 32 00000100000000000000000000000000 |
15810 | 15663 | vec($_, 1, 8) = 32 == 8192 00000000000001000000000000000000 |
15811 | 15664 | vec($_, 2, 8) = 32 == 2097152 00000000000000000000010000000000 |
15812 | 15665 | vec($_, 3, 8) = 32 == 536870912 00000000000000000000000000000100 |
15813 | 15666 | vec($_, 0, 8) = 64 == 64 00000010000000000000000000000000 |
15814 | 15667 | vec($_, 1, 8) = 64 == 16384 00000000000000100000000000000000 |
15815 | 15668 | vec($_, 2, 8) = 64 == 4194304 00000000000000000000001000000000 |
15816 | 15669 | vec($_, 3, 8) = 64 == 1073741824 00000000000000000000000000000010 |
15817 | 15670 | vec($_, 0, 8) = 128 == 128 00000001000000000000000000000000 |
15818 | 15671 | vec($_, 1, 8) = 128 == 32768 00000000000000010000000000000000 |
15819 | 15672 | vec($_, 2, 8) = 128 == 8388608 00000000000000000000000100000000 |
15820 | 15673 | vec($_, 3, 8) = 128 == 2147483648 00000000000000000000000000000001 |
15821 | 15674 | |
15822 | 15675 | =item wait |
15823 | 15676 | X<wait> |
15824 | 15677 | |
15825 | 15678 | =begin original |
15826 | 15679 | |
15827 | 15680 | Behaves like the wait(2) system call on your system: it waits for a child |
15828 | 15681 | process to terminate and returns the pid of the deceased process, or |
15829 | 15682 | C<-1> if there are no child processes. The status is returned in C<$?> |
15830 | and C< | |
15683 | and C<{^CHILD_ERROR_NATIVE}>. | |
15831 | 15684 | Note that a return value of C<-1> could mean that child processes are |
15832 | 15685 | being automatically reaped, as described in L<perlipc>. |
15833 | 15686 | |
15834 | 15687 | =end original |
15835 | 15688 | |
15836 | 15689 | wait(2) システムコールと同様に振る舞います。 |
15837 | 15690 | チャイルドプロセスが終了するのを待ち、消滅したプロセスの pid を返します。 |
15838 | 15691 | チャイルドプロセスが存在しないときには、C<-1> を返します。 |
15839 | ステータスは C<$?> と C< | |
15692 | ステータスは C<$?> と C<{^CHILD_ERROR_NATIVE}> に返されます。 | |
15840 | 15693 | L<perlipc> に書いているように、返り値が C<-1> の場合は子プロセスが |
15841 | 15694 | 自動的に刈り取られたことを意味するかもしれないことに注意してください。 |
15842 | 15695 | |
15843 | 15696 | =item waitpid PID,FLAGS |
15844 | 15697 | X<waitpid> |
15845 | 15698 | |
15846 | 15699 | =begin original |
15847 | 15700 | |
15848 | 15701 | Waits for a particular child process to terminate and returns the pid of |
15849 | 15702 | the deceased process, or C<-1> if there is no such child process. On some |
15850 | 15703 | systems, a value of 0 indicates that there are processes still running. |
15851 | The status is returned in C<$?> and C< | |
15704 | The status is returned in C<$?> and C<{^CHILD_ERROR_NATIVE}>. If you say | |
15852 | 15705 | |
15853 | 15706 | =end original |
15854 | 15707 | |
15855 | 15708 | 特定のチャイルドプロセスが終了するのを待ち、消滅した |
15856 | 15709 | プロセスの pid を返します。 |
15857 | 15710 | 指定したチャイルドプロセスが存在しないときには、C<-1> を返します。 |
15858 | 15711 | 値 0 がプロセスがまだ実行中であることを示すシステムもあります。 |
15859 | ステータスは C<$?> と C< | |
15712 | ステータスは C<$?> と C<{^CHILD_ERROR_NATIVE}> に返されます。 | |
15860 | 15713 | |
15861 | 15714 | use POSIX ":sys_wait_h"; |
15862 | 15715 | #... |
15863 | 15716 | do { |
15864 | 15717 | $kid = waitpid(-1, WNOHANG); |
15865 | 15718 | } while $kid > 0; |
15866 | 15719 | |
15867 | 15720 | =begin original |
15868 | 15721 | |
15869 | 15722 | then you can do a non-blocking wait for all pending zombie processes. |
15870 | 15723 | Non-blocking wait is available on machines supporting either the |
15871 | 15724 | waitpid(2) or wait4(2) system calls. However, waiting for a particular |
15872 | 15725 | pid with FLAGS of C<0> is implemented everywhere. (Perl emulates the |
15873 | 15726 | system call by remembering the status values of processes that have |
15874 | 15727 | exited but have not been harvested by the Perl script yet.) |
15875 | 15728 | |
15876 | 15729 | =end original |
15877 | 15730 | |
15878 | 15731 | とすると、ブロックが起こらないようにして、全ての待機中ゾンビプロセスを |
15879 | 15732 | wait します。 |
15880 | 15733 | ブロックなしの wait は、システムコール wait_pid(2) か、 |
15881 | 15734 | システムコール wait4(2) をサポートしているマシンで利用可能です。 |
15882 | 15735 | しかしながら、特定の pid を C<0> の FLAGS での wait はどこでも |
15883 | 15736 | 実装されています。 |
15884 | 15737 | (exit したプロセスのステータス値を覚えておいて、Perl がシステムコールを |
15885 | 15738 | エミュレートしますが、Perl スクリプトには取り入れられていません。) |
15886 | 15739 | |
15887 | 15740 | =begin original |
15888 | 15741 | |
15889 | 15742 | Note that on some systems, a return value of C<-1> could mean that child |
15890 | 15743 | processes are being automatically reaped. See L<perlipc> for details, |
15891 | 15744 | and for other examples. |
15892 | 15745 | |
15893 | 15746 | =end original |
15894 | 15747 | |
15895 | 15748 | システムによっては、返り値が C<-1> の場合は子プロセスが自動的に |
15896 | 15749 | 刈り取られたことを意味するかもしれないことに注意してください。 |
15897 | 15750 | 詳細やその他の例については L<perlipc> を参照してください。 |
15898 | 15751 | |
15899 | 15752 | =item wantarray |
15900 | 15753 | X<wantarray> X<context> |
15901 | 15754 | |
15902 | 15755 | =begin original |
15903 | 15756 | |
15904 | 15757 | Returns true if the context of the currently executing subroutine or |
15905 | 15758 | C<eval> is looking for a list value. Returns false if the context is |
15906 | 15759 | looking for a scalar. Returns the undefined value if the context is |
15907 | 15760 | looking for no value (void context). |
15908 | 15761 | |
15909 | 15762 | =end original |
15910 | 15763 | |
15911 | 15764 | 現在実行中のサブルーチンか eval() ブロックのコンテキストが、リスト値を |
15912 | 15765 | 要求するものであれば、真を返します。 |
15913 | 15766 | スカラを要求するコンテキストであれば、偽を返します。 |
15914 | 15767 | 何も値を要求しない(無効コンテキスト)場合は未定義値を返します。 |
15915 | 15768 | |
15916 | 15769 | return unless defined wantarray; # don't bother doing more |
15917 | 15770 | my @a = complex_calculation(); |
15918 | 15771 | return wantarray ? @a : "@a"; |
15919 | 15772 | |
15920 | 15773 | =begin original |
15921 | 15774 | |
15922 | 15775 | C<wantarray()>'s result is unspecified in the top level of a file, |
15923 | 15776 | in a C<BEGIN>, C<UNITCHECK>, C<CHECK>, C<INIT> or C<END> block, or |
15924 | 15777 | in a C<DESTROY> method. |
15925 | 15778 | |
15926 | 15779 | =end original |
15927 | 15780 | |
15928 | 15781 | ファイルのトップレベル、C<BEGIN>, C<UNITCHECK>, C<CHECK>, C<INIT>, C<END> |
15929 | 15782 | ブロック内、C<DESTROY> メソッド内では C<wantarray()> の結果は未定義です。 |
15930 | 15783 | |
15931 | 15784 | =begin original |
15932 | 15785 | |
15933 | 15786 | This function should have been named wantlist() instead. |
15934 | 15787 | |
15935 | 15788 | =end original |
15936 | 15789 | |
15937 | 15790 | この関数は wantlist() という名前にするべきでした。 |
15938 | 15791 | |
15939 | 15792 | =item warn LIST |
15940 | 15793 | X<warn> X<warning> X<STDERR> |
15941 | 15794 | |
15942 | 15795 | =begin original |
15943 | 15796 | |
15944 | 15797 | Prints the value of LIST to STDERR. If the last element of LIST does |
15945 | 15798 | not end in a newline, it appends the same file/line number text as C<die> |
15946 | 15799 | does. |
15947 | 15800 | |
15948 | 15801 | =end original |
15949 | 15802 | |
15950 | 15803 | LIST の値を STDERR に出力します。 |
15951 | 15804 | LIST の最後の要素が改行で終わっていない場合、C<die> が行うのと同様の |
15952 | 15805 | ファイル/行番号のテキストが追加されます。 |
15953 | 15806 | |
15954 | 15807 | =begin original |
15955 | 15808 | |
15956 | 15809 | If LIST is empty and C<$@> already contains a value (typically from a |
15957 | 15810 | previous eval) that value is used after appending C<"\t...caught"> |
15958 | 15811 | to C<$@>. This is useful for staying almost, but not entirely similar to |
15959 | 15812 | C<die>. |
15960 | 15813 | |
15961 | 15814 | =end original |
15962 | 15815 | |
15963 | 15816 | LIST が空かつ、(典型的には以前の eval によって) C<$@> に既に値が入っている |
15964 | 15817 | 場合、C<$@> に C<"\t...caught"> を追加した値が用いられます。 |
15965 | 15818 | これはほとんどそのままにするときに便利ですが、 |
15966 | 15819 | C<die> と全体的に似ているわけではありません。 |
15967 | 15820 | |
15968 | 15821 | =begin original |
15969 | 15822 | |
15970 | 15823 | If C<$@> is empty then the string C<"Warning: Something's wrong"> is used. |
15971 | 15824 | |
15972 | 15825 | =end original |
15973 | 15826 | |
15974 | 15827 | C<$@> が空の場合は、C<"Warning: Something's wrong"> という文字列が |
15975 | 15828 | 使われます。 |
15976 | 15829 | |
15977 | 15830 | =begin original |
15978 | 15831 | |
15979 | 15832 | No message is printed if there is a C<$SIG{__WARN__}> handler |
15980 | 15833 | installed. It is the handler's responsibility to deal with the message |
15981 | 15834 | as it sees fit (like, for instance, converting it into a C<die>). Most |
15982 | 15835 | handlers must therefore make arrangements to actually display the |
15983 | 15836 | warnings that they are not prepared to deal with, by calling C<warn> |
15984 | 15837 | again in the handler. Note that this is quite safe and will not |
15985 | 15838 | produce an endless loop, since C<__WARN__> hooks are not called from |
15986 | 15839 | inside one. |
15987 | 15840 | |
15988 | 15841 | =end original |
15989 | 15842 | |
15990 | 15843 | C<$SIG{__WARN__}> ハンドラが設定されている場合は何のメッセージも |
15991 | 15844 | 表示されません。 |
15992 | 15845 | メッセージをどう扱うか(例えば C<die> に変換するか)はハンドラの |
15993 | 15846 | 責任ということです。 |
15994 | 15847 | 従ってほとんどのハンドラは、扱おうと準備していない警告を表示するために、 |
15995 | 15848 | ハンドラの中で C<warn> を再び呼び出します。 |
15996 | 15849 | C<__WARN__> フックはハンドラ内では呼び出されないので、これは十分安全で、 |
15997 | 15850 | 無限ループを引き起こすことはないということに注意してください。 |
15998 | 15851 | |
15999 | 15852 | =begin original |
16000 | 15853 | |
16001 | 15854 | You will find this behavior is slightly different from that of |
16002 | 15855 | C<$SIG{__DIE__}> handlers (which don't suppress the error text, but can |
16003 | 15856 | instead call C<die> again to change it). |
16004 | 15857 | |
16005 | 15858 | =end original |
16006 | 15859 | |
16007 | 15860 | この振る舞いは C<$SIG{__DIE__}> ハンドラ(エラーテキストは削除しませんが、 |
16008 | 15861 | 代わりに C<die> をもう一度呼び出すことで変更できます)とは |
16009 | 15862 | 少し違うことに気付くことでしょう。 |
16010 | 15863 | |
16011 | 15864 | =begin original |
16012 | 15865 | |
16013 | 15866 | Using a C<__WARN__> handler provides a powerful way to silence all |
16014 | 15867 | warnings (even the so-called mandatory ones). An example: |
16015 | 15868 | |
16016 | 15869 | =end original |
16017 | 15870 | |
16018 | 15871 | C<__WARN__> ハンドラを使うと、(いわゆる必須のものを含む)全ての |
16019 | 15872 | 警告を黙らせる強力な手段となります。 |
16020 | 15873 | 例: |
16021 | 15874 | |
16022 | 15875 | # wipe out *all* compile-time warnings |
16023 | 15876 | BEGIN { $SIG{'__WARN__'} = sub { warn $_[0] if $DOWARN } } |
16024 | 15877 | my $foo = 10; |
16025 | 15878 | my $foo = 20; # no warning about duplicate my $foo, |
16026 | 15879 | # but hey, you asked for it! |
16027 | 15880 | # no compile-time or run-time warnings before here |
16028 | 15881 | $DOWARN = 1; |
16029 | 15882 | |
16030 | 15883 | # run-time warnings enabled after here |
16031 | 15884 | warn "\$foo is alive and $foo!"; # does show up |
16032 | 15885 | |
16033 | 15886 | =begin original |
16034 | 15887 | |
16035 | 15888 | See L<perlvar> for details on setting C<%SIG> entries, and for more |
16036 | 15889 | examples. See the Carp module for other kinds of warnings using its |
16037 | 15890 | carp() and cluck() functions. |
16038 | 15891 | |
16039 | 15892 | =end original |
16040 | 15893 | |
16041 | 15894 | C<%SIG> エントリのセットに関する詳細とさらなる例に関しては |
16042 | 15895 | L<perlvar> を参照して下さい。 |
16043 | 15896 | carp() 関数と cluck() 関数を用いた警告の方法に関しては |
16044 | 15897 | Carp モジュールを参照して下さい。 |
16045 | 15898 | |
16046 | 15899 | =item write FILEHANDLE |
16047 | 15900 | X<write> |
16048 | 15901 | |
16049 | 15902 | =item write EXPR |
16050 | 15903 | |
16051 | 15904 | =item write |
16052 | 15905 | |
16053 | 15906 | =begin original |
16054 | 15907 | |
16055 | 15908 | Writes a formatted record (possibly multi-line) to the specified FILEHANDLE, |
16056 | 15909 | using the format associated with that file. By default the format for |
16057 | 15910 | a file is the one having the same name as the filehandle, but the |
16058 | 15911 | format for the current output channel (see the C<select> function) may be set |
16059 | 15912 | explicitly by assigning the name of the format to the C<$~> variable. |
16060 | 15913 | |
16061 | 15914 | =end original |
16062 | 15915 | |
16063 | 15916 | 指定された FILEHANDLE に対して、そのファイルに対応させた |
16064 | 15917 | フォーマットを使って、(複数行の場合もある) 整形された |
16065 | 15918 | レコードを書き出します。 |
16066 | 15919 | デフォルトでは、ファイルに対応するフォーマットは、ファイルハンドルと |
16067 | 15920 | 同じ名前のものですが、その時点の出力チャネル (C<select> 関数の項を |
16068 | 15921 | 参照してください) のフォーマットは、その名前を明示的に変数 C<$~> に |
16069 | 15922 | 代入することで、変更が可能です。 |
16070 | 15923 | |
16071 | 15924 | =begin original |
16072 | 15925 | |
16073 | 15926 | Top of form processing is handled automatically: if there is |
16074 | 15927 | insufficient room on the current page for the formatted record, the |
16075 | 15928 | page is advanced by writing a form feed, a special top-of-page format |
16076 | 15929 | is used to format the new page header, and then the record is written. |
16077 | 15930 | By default the top-of-page format is the name of the filehandle with |
16078 | 15931 | "_TOP" appended, but it may be dynamically set to the format of your |
16079 | 15932 | choice by assigning the name to the C<$^> variable while the filehandle is |
16080 | 15933 | selected. The number of lines remaining on the current page is in |
16081 | 15934 | variable C<$->, which can be set to C<0> to force a new page. |
16082 | 15935 | |
16083 | 15936 | =end original |
16084 | 15937 | |
16085 | 15938 | ページの先頭の処理は、自動的に行なわれます。 |
16086 | 15939 | 現在のページに整形されたレコードを出力するだけのスペースがない場合には、 |
16087 | 15940 | 改ページを行なってページを進め、新しいページヘッダを整形するため、 |
16088 | 15941 | ページ先頭フォーマットが使われ、その後でレコードが書かれます。 |
16089 | 15942 | デフォルトでは、ページ先頭フォーマットは、ファイルハンドルの名前に |
16090 | 15943 | "_TOP" をつなげたものですが、ファイルハンドルが選択されている間に、 |
16091 | 15944 | 変数 C<$^> に名前を設定すれば、動的にフォーマットを |
16092 | 15945 | 変更することができます。 |
16093 | 15946 | そのページの残り行数は、変数 C<$-> に入っており、この変数を 0 に |
16094 | 15947 | 設定することで、強制的に改ページを行なうことができます。 |
16095 | 15948 | |
16096 | 15949 | =begin original |
16097 | 15950 | |
16098 | 15951 | If FILEHANDLE is unspecified, output goes to the current default output |
16099 | 15952 | channel, which starts out as STDOUT but may be changed by the |
16100 | 15953 | C<select> operator. If the FILEHANDLE is an EXPR, then the expression |
16101 | 15954 | is evaluated and the resulting string is used to look up the name of |
16102 | 15955 | the FILEHANDLE at run time. For more on formats, see L<perlform>. |
16103 | 15956 | |
16104 | 15957 | =end original |
16105 | 15958 | |
16106 | 15959 | FILEHANDLE を指定しないと、出力はその時点のデフォルト |
16107 | 15960 | 出力チャネルに対して行なわれます。 |
16108 | 15961 | これは、スクリプトの開始時点では STDOUT ですが、select() 演算子で |
16109 | 15962 | 変更することができます。 |
16110 | 15963 | FILEHANDLE が EXPR ならば、式が評価され、その結果の文字列が |
16111 | 15964 | 実行時に FILEHANDLE の名前として見られます。 |
16112 | 15965 | フォーマットについて、さらには、L<perlform> を参照してください。 |
16113 | 15966 | |
16114 | 15967 | =begin original |
16115 | 15968 | |
16116 | 15969 | Note that write is I<not> the opposite of C<read>. Unfortunately. |
16117 | 15970 | |
16118 | 15971 | =end original |
16119 | 15972 | |
16120 | 15973 | 残念ながら、write は C<read> の反対のことをするもの |
16121 | 15974 | I<ではありません>。 |
16122 | 15975 | |
16123 | 15976 | =item y/// |
16124 | 15977 | |
16125 | 15978 | =begin original |
16126 | 15979 | |
16127 | The transliteration operator. Same as C<tr///>. See | |
15980 | The transliteration operator. Same as C<tr///>. See L<perlop>. | |
16128 | L<perlop/"Quote and Quote-like Operators">. | |
16129 | 15981 | |
16130 | 15982 | =end original |
16131 | 15983 | |
16132 | 15984 | 文字変換演算子です。 |
16133 | 15985 | C<tr///> と同じです。 |
16134 | L<perlop | |
15986 | L<perlop> を参照してください。 | |
16135 | 15987 | |
16136 | 15988 | =back |
16137 | 15989 | |
16138 | 15990 | =begin meta |
16139 | 15991 | |
16140 | 15992 | Translate: 吉村 寿人 <JAE00534@niftyserve.or.jp> |
16141 | Update: | |
15993 | Update: Kentaro Shirakata <argrath@ub32.org> | |
16142 | Status: completed | |
16143 | 15994 | |
16144 | 15995 | =end meta |