perlsub >
5.42.0
との差分
perlsub 5.42.0 と 5.16.1 の差分
1 | 1 | |
2 | =encoding u | |
2 | =encoding euc-jp | |
3 | 3 | |
4 | 4 | =head1 NAME |
5 | 5 | X<subroutine> X<function> |
6 | 6 | |
7 | 7 | =begin original |
8 | 8 | |
9 | perlsub - Perl subroutines | |
9 | perlsub - Perl subroutines | |
10 | 10 | |
11 | 11 | =end original |
12 | 12 | |
13 | perlsub - Perl のサブルーチン | |
13 | perlsub - Perl のサブルーチン | |
14 | 14 | |
15 | 15 | =head1 SYNOPSIS |
16 | 16 | |
17 | 17 | =begin original |
18 | 18 | |
19 | 19 | To declare subroutines: |
20 | 20 | X<subroutine, declaration> X<sub> |
21 | 21 | |
22 | 22 | =end original |
23 | 23 | |
24 | 24 | サブルーチンを宣言するには: |
25 | 25 | X<subroutine, declaration> X<sub> |
26 | 26 | |
27 | 27 | =begin original |
28 | 28 | |
29 | sub NAME; | |
29 | sub NAME; # A "forward" declaration. | |
30 | sub NAME(PROTO); | |
30 | sub NAME(PROTO); # ditto, but with prototypes | |
31 | sub NAME : ATTRS; | |
31 | sub NAME : ATTRS; # with attributes | |
32 | sub NAME(PROTO) : ATTRS; | |
32 | sub NAME(PROTO) : ATTRS; # with attributes and prototypes | |
33 | 33 | |
34 | 34 | =end original |
35 | 35 | |
36 | sub NAME; | |
36 | sub NAME; # "先行" 宣言 | |
37 | sub NAME(PROTO); | |
37 | sub NAME(PROTO); # 同上。ただしプロトタイプ付き | |
38 | sub NAME : ATTRS; | |
38 | sub NAME : ATTRS; # 属性付き | |
39 | sub NAME(PROTO) : ATTRS; | |
39 | sub NAME(PROTO) : ATTRS; # 属性とプロトタイプ付き | |
40 | 40 | |
41 | 41 | =begin original |
42 | 42 | |
43 | sub NAME BLOCK | |
43 | sub NAME BLOCK # A declaration and a definition. | |
44 | sub NAME(PROTO) BLOCK | |
44 | sub NAME(PROTO) BLOCK # ditto, but with prototypes | |
45 | sub NAME : ATTRS BLOCK | |
45 | sub NAME : ATTRS BLOCK # with attributes | |
46 | sub NAME(PROTO) : ATTRS BLOCK | |
46 | sub NAME(PROTO) : ATTRS BLOCK # with prototypes and attributes | |
47 | 47 | |
48 | 48 | =end original |
49 | 49 | |
50 | sub NAME BLOCK | |
50 | sub NAME BLOCK # 宣言と定義 | |
51 | sub NAME(PROTO) BLOCK | |
51 | sub NAME(PROTO) BLOCK # 同上。ただしプロトタイプ付き | |
52 | sub NAME : ATTRS BLOCK | |
52 | sub NAME : ATTRS BLOCK # 属性付き | |
53 | sub NAME(PROTO) : ATTRS BLOCK | |
53 | sub NAME(PROTO) : ATTRS BLOCK # プロトタイプと属性付き | |
54 | 54 | |
55 | 55 | =begin original |
56 | 56 | |
57 | use feature 'signatures'; | |
58 | sub NAME(SIG) BLOCK # with signature | |
59 | sub NAME :ATTRS (SIG) BLOCK # with signature, attributes | |
60 | sub NAME :prototype(PROTO) (SIG) BLOCK # with signature, prototype | |
61 | ||
62 | =end original | |
63 | ||
64 | use feature 'signatures'; | |
65 | sub NAME(SIG) BLOCK # with signature | |
66 | sub NAME :ATTRS (SIG) BLOCK # with signature, attributes | |
67 | sub NAME :prototype(PROTO) (SIG) BLOCK # with signature, prototype | |
68 | ||
69 | =begin original | |
70 | ||
71 | 57 | To define an anonymous subroutine at runtime: |
72 | 58 | X<subroutine, anonymous> |
73 | 59 | |
74 | 60 | =end original |
75 | 61 | |
76 | 62 | 実行時に無名サブルーチンを定義するには: |
77 | 63 | X<subroutine, anonymous> |
78 | 64 | |
79 | 65 | =begin original |
80 | 66 | |
81 | $subref = sub BLOCK; | |
67 | $subref = sub BLOCK; # no proto | |
82 | $subref = sub (PROTO) BLOCK; | |
68 | $subref = sub (PROTO) BLOCK; # with proto | |
83 | $subref = sub : ATTRS BLOCK; | |
69 | $subref = sub : ATTRS BLOCK; # with attributes | |
84 | $subref = sub (PROTO) : ATTRS BLOCK; | |
70 | $subref = sub (PROTO) : ATTRS BLOCK; # with proto and attributes | |
85 | 71 | |
86 | 72 | =end original |
87 | 73 | |
88 | $subref = sub BLOCK; | |
74 | $subref = sub BLOCK; # プロトタイプなし | |
89 | $subref = sub (PROTO) BLOCK; | |
75 | $subref = sub (PROTO) BLOCK; # プロトタイプ付き | |
90 | $subref = sub : ATTRS BLOCK; | |
76 | $subref = sub : ATTRS BLOCK; # 属性付き | |
91 | $subref = sub (PROTO) : ATTRS BLOCK; | |
77 | $subref = sub (PROTO) : ATTRS BLOCK; # プロトタイプと属性付き | |
92 | 78 | |
93 | 79 | =begin original |
94 | 80 | |
95 | use feature 'signatures'; | |
96 | $subref = sub (SIG) BLOCK; # with signature | |
97 | $subref = sub : ATTRS(SIG) BLOCK; # with signature, attributes | |
98 | ||
99 | =end original | |
100 | ||
101 | use feature 'signatures'; | |
102 | $subref = sub (SIG) BLOCK; # シグネチャ付き | |
103 | $subref = sub : ATTRS(SIG) BLOCK; # シグネチャと属性付き | |
104 | ||
105 | =begin original | |
106 | ||
107 | 81 | To import subroutines: |
108 | 82 | X<import> |
109 | 83 | |
110 | 84 | =end original |
111 | 85 | |
112 | 86 | サブルーチンをインポートするには: |
113 | 87 | X<import> |
114 | 88 | |
115 | 89 | use MODULE qw(NAME1 NAME2 NAME3); |
116 | 90 | |
117 | 91 | =begin original |
118 | 92 | |
119 | 93 | To call subroutines: |
120 | 94 | X<subroutine, call> X<call> |
121 | 95 | |
122 | 96 | =end original |
123 | 97 | |
124 | 98 | サブルーチンを呼び出すには: |
125 | 99 | X<subroutine, call> X<call> |
126 | 100 | |
127 | 101 | =begin original |
128 | 102 | |
129 | NAME(LIST); | |
103 | NAME(LIST); # & is optional with parentheses. | |
130 | NAME LIST; | |
104 | NAME LIST; # Parentheses optional if predeclared/imported. | |
131 | &NAME(LIST); | |
105 | &NAME(LIST); # Circumvent prototypes. | |
132 | &NAME; | |
106 | &NAME; # Makes current @_ visible to called subroutine. | |
133 | 107 | |
134 | 108 | =end original |
135 | 109 | |
136 | NAME(LIST); | |
110 | NAME(LIST); # かっこが付いているときは & は省略可能。 | |
137 | NAME LIST; | |
111 | NAME LIST; # 予め宣言/インポートされているならかっこは省略可能。 | |
138 | &NAME(LIST); | |
112 | &NAME(LIST); # プロトタイプを回避する。 | |
139 | &NAME; | |
113 | &NAME; # 呼び出されたサブルーチンから現在の @_ を可視化する。 | |
140 | 114 | |
141 | 115 | =head1 DESCRIPTION |
142 | 116 | |
143 | 117 | =begin original |
144 | 118 | |
145 | 119 | Like many languages, Perl provides for user-defined subroutines. |
146 | 120 | These may be located anywhere in the main program, loaded in from |
147 | 121 | other files via the C<do>, C<require>, or C<use> keywords, or |
148 | 122 | generated on the fly using C<eval> or anonymous subroutines. |
149 | 123 | You can even call a function indirectly using a variable containing |
150 | 124 | its name or a CODE reference. |
151 | 125 | |
152 | 126 | =end original |
153 | 127 | |
154 | 128 | 多くの言語と同様、Perl はユーザー定義のサブルーチンを提供しています。 |
155 | 129 | これらのサブルーチンは、メインプログラムのどこにでも置くことができ、 |
156 | 130 | C<do>, C<require>, C<use> といったキーワードを使って他のファイルから |
157 | 131 | ロードすることができ、C<eval> や無名サブルーチンを使って |
158 | 132 | 生成することもできます。 |
159 | 133 | サブルーチンの名前や、コードリファレンスを保持する変数を使って |
160 | 134 | 間接的に関数を呼び出すことも可能です。 |
161 | 135 | |
162 | 136 | =begin original |
163 | 137 | |
164 | 138 | The Perl model for function call and return values is simple: all |
165 | 139 | functions are passed as parameters one single flat list of scalars, and |
166 | 140 | all functions likewise return to their caller one single flat list of |
167 | 141 | scalars. Any arrays or hashes in these call and return lists will |
168 | 142 | collapse, losing their identities--but you may always use |
169 | 143 | pass-by-reference instead to avoid this. Both call and return lists may |
170 | 144 | contain as many or as few scalar elements as you'd like. (Often a |
171 | 145 | function without an explicit return statement is called a subroutine, but |
172 | 146 | there's really no difference from Perl's perspective.) |
173 | 147 | X<subroutine, parameter> X<parameter> |
174 | 148 | |
175 | 149 | =end original |
176 | 150 | |
177 | 151 | Perl での関数呼び出しと戻り値のモデルは単純です。全ての関数は引数を、 |
178 | 152 | 平坦で一つのスカラのリストで受け取り、同様に全ての関数は呼び出し元に |
179 | 153 | 対して平坦で一つのスカラのりストで返すというものです。 |
180 | 154 | これらの呼び出しリストと戻り値リストにある全ての配列とハッシュは、 |
181 | 155 | 潰されてそのアイデンティティを失います。 |
182 | 156 | しかし、これを避けるために常にリファレンスで渡すことができます。 |
183 | 157 | 呼び出しリストと戻り値リストの両方とも好きなだけの数のスカラを |
184 | 158 | 保持することができます(明確な return 文のない関数はしばしばサブルーチンと |
185 | 159 | 呼ばれますが、Perl の定義上はこれらの間に何の違いもありません)。 |
186 | 160 | |
187 | 161 | =begin original |
188 | 162 | |
189 | ||
163 | Any arguments passed in show up in the array C<@_>. Therefore, if | |
190 | ||
164 | you called a function with two arguments, those would be stored in | |
191 | ||
165 | C<$_[0]> and C<$_[1]>. The array C<@_> is a local array, but its | |
192 | ||
166 | elements are aliases for the actual scalar parameters. In particular, | |
193 | ||
167 | if an element C<$_[0]> is updated, the corresponding argument is | |
194 | ||
168 | updated (or an error occurs if it is not updatable). If an argument | |
169 | is an array or hash element which did not exist when the function | |
196 | ||
170 | was called, that element is created only when (and if) it is modified | |
171 | or a reference to it is taken. (Some earlier versions of Perl | |
198 | ||
172 | created the element whether or not the element was assigned to.) | |
199 | ||
173 | Assigning to the whole array C<@_> removes that aliasing, and does | |
200 | ||
174 | not update any arguments. | |
201 | C<@_> 配列からもアクセス可能ですが、 | |
202 | この方法にこれらにアクセスするのは、このようなシグネチャを使っている | |
203 | サブルーチンでは非推奨です。 | |
204 | ||
205 | =begin original | |
206 | ||
207 | In a subroutine that does not use signatures, any arguments passed in | |
208 | show up in the array C<@_>. Therefore, if you called a function with | |
209 | two arguments, those would be stored in C<$_[0]> and C<$_[1]>. The | |
210 | array C<@_> is a local array, but its elements are aliases for the | |
211 | actual scalar parameters. In particular, if an element C<$_[0]> is | |
212 | updated, the corresponding argument is updated (or an error occurs if it | |
213 | is not updatable). If an argument is an array or hash element which did | |
214 | not exist when the function was called, that element is created only | |
215 | when (and if) it is modified or a reference to it is taken. (Some | |
216 | earlier versions of Perl created the element whether or not the element | |
217 | was assigned to.) Assigning to the whole array C<@_> removes that | |
218 | aliasing, and does not update any arguments. | |
219 | 175 | X<subroutine, argument> X<argument> X<@_> |
220 | 176 | |
221 | 177 | =end original |
222 | 178 | |
223 | シグネチャを使わないサブルーチンでは、 | |
224 | 179 | ルーチンに渡されるすべての引数は配列 C<@_> に置かれます。 |
225 | 180 | したがって、ある関数を二つの引数を付けて呼び出したならば、 |
226 | 181 | その引数は C<$_[0]> と C<$_[1]> に格納されます。 |
227 | 182 | 配列 C<@_> は local 配列ですが、その要素は実際の |
228 | 183 | スカラパラメータの別名です。 |
229 | 184 | たとえば C<$_[0]> が更新された場合、対応する引数が更新されます |
230 | 185 | (更新できない場合にはエラーとなります)。 |
231 | 186 | 引数が、配列やハッシュの(関数が呼び出された時点では存在してない) |
232 | 187 | 要素であった場合、その要素は(対応する別名が)修正されたり |
233 | 188 | リファレンスが取られたときにのみ作成されます。 |
234 | 189 | (以前の一部のバージョンの Perl では、この要素は代入が行われようが行われまいが |
235 | 190 | 作成されていました。) |
236 | 191 | 配列 C<@_> 全体に対する代入は別名を破棄し、何の引数も更新しません。 |
237 | 192 | X<subroutine, argument> X<argument> X<@_> |
238 | 193 | |
239 | 194 | =begin original |
240 | 195 | |
241 | When not using signatures, Perl does not otherwise provide a means to | |
242 | create named formal parameters. In practice all you do is assign to a | |
243 | C<my()> list of these. Variables that aren't declared to be private are | |
244 | global variables. For gory details on creating private variables, see | |
245 | L</"Private Variables via my()"> and L</"Temporary Values via local()">. | |
246 | To create protected environments for a set of functions in a separate | |
247 | package (and probably a separate file), see L<perlmod/"Packages">. | |
248 | ||
249 | =end original | |
250 | ||
251 | シグネチャを使わない場合、Perl は名前付き仮引数を作る他の手段を | |
252 | 提供しません。 | |
253 | 実際には、C<my()> にこれらのリストを代入することで行えます。 | |
254 | プライベートであると宣言されずに使われている変数は全てグローバル変数です。 | |
255 | プライベート変数に関する詳細はL</"Private Variables via my()"> と | |
256 | L</"Temporary Values via local()"> を参照してください。 | |
257 | パッケージで(おそらくはファイルでも)分けられている関数のセットのための | |
258 | 保護された環境を作るには L<perlmod/"Packages"> を参照してください。 | |
259 | ||
260 | =begin original | |
261 | ||
262 | 196 | A C<return> statement may be used to exit a subroutine, optionally |
263 | 197 | specifying the returned value, which will be evaluated in the |
264 | 198 | appropriate context (list, scalar, or void) depending on the context of |
265 | 199 | the subroutine call. If you specify no return value, the subroutine |
266 | 200 | returns an empty list in list context, the undefined value in scalar |
267 | 201 | context, or nothing in void context. If you return one or more |
268 | 202 | aggregates (arrays and hashes), these will be flattened together into |
269 | 203 | one large indistinguishable list. |
270 | 204 | |
271 | 205 | =end original |
272 | 206 | |
273 | 207 | サブルーチンから脱出するために使われた C<return> 文が |
274 | 208 | 使われることもありますし、サブルーチン呼び出しのコンテキストによって |
275 | 209 | 適切なコンテキスト(リスト、スカラ、無効)で評価される戻り値の指定を |
276 | 210 | 省略する事が可能です。 |
277 | 211 | もし何の戻り値も指定しなければ、サブルーチンはリストコンテキストにおいては |
278 | 212 | 空リストを返し、スカラコンテキストにおいては未定義値を返し、 |
279 | 213 | 無効コンテキストではなにも返しません。 |
280 | 214 | 一つまたは複数の集合体 (配列やハッシュ) を返す場合、一つの大きな区別できない |
281 | 215 | リストに平板化されます。 |
282 | 216 | |
283 | 217 | =begin original |
284 | 218 | |
285 | 219 | If no C<return> is found and if the last statement is an expression, its |
286 | value is returned. | |
220 | value is returned. If the last statement is a loop control structure | |
287 | like a C<foreach> or a C<while>, the returned value is unspecified. | |
221 | like a C<foreach> or a C<while>, the returned value is unspecified. The | |
288 | 222 | empty sub returns the empty list. |
289 | 223 | X<subroutine, return value> X<return value> X<return> |
290 | 224 | |
291 | 225 | =end original |
292 | 226 | |
293 | 227 | C<return> がなく、最後の文が式だった場合、その値が返されます。 |
294 | 228 | 最後の文が C<foreach> や C<while> のようなループ制御構造だった場合、 |
295 | 229 | 返される値は不定です。 |
296 | 230 | 空のサブルーチンは空リストを返します。 |
297 | 231 | X<subroutine, return value> X<return value> X<return> |
298 | 232 | |
299 | 233 | =begin original |
300 | 234 | |
235 | Perl does not have named formal parameters. In practice all you | |
236 | do is assign to a C<my()> list of these. Variables that aren't | |
237 | declared to be private are global variables. For gory details | |
238 | on creating private variables, see L<"Private Variables via my()"> | |
239 | and L<"Temporary Values via local()">. To create protected | |
240 | environments for a set of functions in a separate package (and | |
241 | probably a separate file), see L<perlmod/"Packages">. | |
242 | X<formal parameter> X<parameter, formal> | |
243 | ||
244 | =end original | |
245 | ||
246 | Perlは名前付き仮引数を持っていません。 | |
247 | C<my()> にこれらのリストを代入することで行えます。 | |
248 | プライベートであると宣言されずに使われている変数は全てグローバル変数です。 | |
249 | プライベート変数に関する詳細はL<"Private Variables via my()"> と | |
250 | L<"Temporary Values via local()"> を参照してください。 | |
251 | パッケージで(おそらくはファイルでも)分けられている関数のセットのための | |
252 | 保護された環境を作るには L<perlmod/"Packages"> を参照してください。 | |
253 | X<formal parameter> X<parameter, formal> | |
254 | ||
255 | =begin original | |
256 | ||
301 | 257 | Example: |
302 | 258 | |
303 | 259 | =end original |
304 | 260 | |
305 | 261 | 例: |
306 | 262 | |
307 | 263 | sub max { |
308 | ||
264 | my $max = shift(@_); | |
309 | ||
265 | foreach $foo (@_) { | |
310 | ||
266 | $max = $foo if $max < $foo; | |
311 | ||
267 | } | |
312 | ||
268 | return $max; | |
313 | 269 | } |
314 | 270 | $bestday = max($mon,$tue,$wed,$thu,$fri); |
315 | 271 | |
316 | 272 | =begin original |
317 | 273 | |
318 | 274 | Example: |
319 | 275 | |
320 | 276 | =end original |
321 | 277 | |
322 | 278 | 例: |
323 | 279 | |
324 | 280 | =begin original |
325 | 281 | |
326 | 282 | # get a line, combining continuation lines |
327 | 283 | # that start with whitespace |
328 | 284 | |
329 | 285 | =end original |
330 | 286 | |
331 | 287 | # 行を取り、空白で始まる継続行を連結します |
332 | 288 | |
333 | 289 | sub get_line { |
334 | ||
290 | $thisline = $lookahead; # global variables! | |
335 | ||
291 | LINE: while (defined($lookahead = <STDIN>)) { | |
336 | ||
292 | if ($lookahead =~ /^[ \t]/) { | |
337 | ||
293 | $thisline .= $lookahead; | |
338 | ||
294 | } | |
339 | ||
295 | else { | |
340 | ||
296 | last LINE; | |
341 | ||
297 | } | |
342 | ||
298 | } | |
343 | ||
299 | return $thisline; | |
344 | 300 | } |
345 | 301 | |
346 | $lookahead = <STDIN>; | |
302 | $lookahead = <STDIN>; # get first line | |
347 | 303 | while (defined($line = get_line())) { |
348 | ||
304 | ... | |
349 | 305 | } |
350 | 306 | |
351 | 307 | =begin original |
352 | 308 | |
353 | 309 | Assigning to a list of private variables to name your arguments: |
354 | 310 | |
355 | 311 | =end original |
356 | 312 | |
357 | 313 | 引数に名前を付けるためにプライベート変数のリストに代入する: |
358 | 314 | |
359 | 315 | sub maybeset { |
360 | ||
316 | my($key, $value) = @_; | |
361 | ||
317 | $Foo{$key} = $value unless $Foo{$key}; | |
362 | 318 | } |
363 | 319 | |
364 | 320 | =begin original |
365 | 321 | |
366 | 322 | Because the assignment copies the values, this also has the effect |
367 | 323 | of turning call-by-reference into call-by-value. Otherwise a |
368 | 324 | function is free to do in-place modifications of C<@_> and change |
369 | 325 | its caller's values. |
370 | 326 | X<call-by-reference> X<call-by-value> |
371 | 327 | |
372 | 328 | =end original |
373 | 329 | |
374 | 330 | これは、参照渡しを値渡しにする効果もあります; なぜなら、値のコピーを |
375 | 331 | 代入しているからです。 |
376 | 332 | このようにしないのであれば、関数は自由に C<@_> の値をその場で |
377 | 333 | 書き換えることができ、それはそのまま呼び出し側の値を変更します。 |
378 | 334 | X<call-by-reference> X<call-by-value> |
379 | 335 | |
380 | 336 | upcase_in($v1, $v2); # this changes $v1 and $v2 |
381 | 337 | sub upcase_in { |
382 | ||
338 | for (@_) { tr/a-z/A-Z/ } | |
383 | 339 | } |
384 | 340 | |
385 | 341 | =begin original |
386 | 342 | |
387 | 343 | You aren't allowed to modify constants in this way, of course. If an |
388 | 344 | argument were actually literal and you tried to change it, you'd take a |
389 | 345 | (presumably fatal) exception. For example, this won't work: |
390 | 346 | X<call-by-reference> X<call-by-value> |
391 | 347 | |
392 | 348 | =end original |
393 | 349 | |
394 | 350 | もちろん、このやり方で定数を変更することは許されません。 |
395 | 351 | ある引数が実際にはリテラルであった場合にその引数を変更しようとすると、 |
396 | 352 | (おそらくは致命的な)例外が引き起こされることになるでしょう。 |
397 | 353 | 例えば次の例はうまく働きません: |
398 | 354 | X<call-by-reference> X<call-by-value> |
399 | 355 | |
400 | 356 | upcase_in("frederick"); |
401 | 357 | |
402 | 358 | =begin original |
403 | 359 | |
404 | 360 | It would be much safer if the C<upcase_in()> function |
405 | 361 | were written to return a copy of its parameters instead |
406 | 362 | of changing them in place: |
407 | 363 | |
408 | 364 | =end original |
409 | 365 | |
410 | 366 | C<upcase_in()> 関数を安全なものにするには、パラメータそのものを |
411 | 367 | 書き換えるのではなくそのコピーを返すように記述するようにします: |
412 | 368 | |
413 | 369 | ($v3, $v4) = upcase($v1, $v2); # this doesn't change $v1 and $v2 |
414 | 370 | sub upcase { |
415 | ||
371 | return unless defined wantarray; # void context, do nothing | |
416 | ||
372 | my @parms = @_; | |
417 | ||
373 | for (@parms) { tr/a-z/A-Z/ } | |
418 | | |
374 | return wantarray ? @parms : $parms[0]; | |
419 | 375 | } |
420 | 376 | |
421 | 377 | =begin original |
422 | 378 | |
423 | 379 | Notice how this (unprototyped) function doesn't care whether it was |
424 | 380 | passed real scalars or arrays. Perl sees all arguments as one big, |
425 | 381 | long, flat parameter list in C<@_>. This is one area where |
426 | 382 | Perl's simple argument-passing style shines. The C<upcase()> |
427 | 383 | function would work perfectly well without changing the C<upcase()> |
428 | 384 | definition even if we fed it things like this: |
429 | 385 | |
430 | 386 | =end original |
431 | 387 | |
432 | 388 | この(プロトタイプがついていない)関数が自分に対して本当のスカラが |
433 | 389 | 渡されたのか配列が渡されたのかを気にしていないということに注意してください。 |
434 | 390 | Perl は一つの巨大な平坦な(リストの中にリストが含まれることはない、 |
435 | 391 | ということ) C<@_> パラメータリストとして全ての引数を見るのです。 |
436 | 392 | これは Perl の単純な引数渡しの形式のやり方の一つです。 |
437 | 393 | この C<upcase()> 関数は、以下のような呼び出しをした場合でも C<upcase()> の |
438 | 394 | 定義を変更することなく完璧に動作します: |
439 | 395 | |
440 | 396 | @newlist = upcase(@list1, @list2); |
441 | 397 | @newlist = upcase( split /:/, $var ); |
442 | 398 | |
443 | 399 | =begin original |
444 | 400 | |
445 | 401 | Do not, however, be tempted to do this: |
446 | 402 | |
447 | 403 | =end original |
448 | 404 | |
449 | 405 | ただし、以下のような呼び出しをやろうとしてはいけません: |
450 | 406 | |
451 | (@ | |
407 | (@a, @b) = upcase(@list1, @list2); | |
452 | 408 | |
453 | 409 | =begin original |
454 | 410 | |
455 | 411 | Like the flattened incoming parameter list, the return list is also |
456 | 412 | flattened on return. So all you have managed to do here is stored |
457 | everything in C<@ | |
413 | everything in C<@a> and made C<@b> empty. See | |
458 | L< | |
414 | L<Pass by Reference> for alternatives. | |
459 | 415 | |
460 | 416 | =end original |
461 | 417 | |
462 | 418 | 関数に渡される引数リストは平坦なリストであるのと同様、戻り値のリストも |
463 | 419 | また平坦なリストです。 |
464 | ですから、関数が返した全ての要素は C<@ | |
420 | ですから、関数が返した全ての要素は C<@a> に格納され、C<@b> は | |
465 | 421 | 空になります。 |
466 | 別のやり方については L</Pass by Reference> を参照してください。 | |
422 | 別のやり方については L</"Pass by Reference"> を参照してください。 | |
467 | 423 | |
468 | 424 | =begin original |
469 | 425 | |
470 | 426 | A subroutine may be called using an explicit C<&> prefix. The |
471 | 427 | C<&> is optional in modern Perl, as are parentheses if the |
472 | 428 | subroutine has been predeclared. The C<&> is I<not> optional |
473 | 429 | when just naming the subroutine, such as when it's used as |
474 | 430 | an argument to defined() or undef(). Nor is it optional when you |
475 | 431 | want to do an indirect subroutine call with a subroutine name or |
476 | 432 | reference using the C<&$subref()> or C<&{$subref}()> constructs, |
477 | 433 | although the C<< $subref->() >> notation solves that problem. |
478 | 434 | See L<perlref> for more about all that. |
479 | 435 | X<&> |
480 | 436 | |
481 | 437 | =end original |
482 | 438 | |
483 | 439 | サブルーチンは、明示的な C<&> というプリフィックスを付けて呼び出すことが |
484 | 440 | できます。 |
485 | 441 | 最近の Perl では C<&> は省略可能であり、サブルーチンがあらかじめ |
486 | 442 | 宣言されている場合には括弧も省略できます。 |
487 | 443 | defined() や undef() の引数として使ったような、単なる名前付き |
488 | サブルーチンであるときの C<&> は | |
444 | サブルーチンであるときの C<&> は B<省略可能ではありません>。 | |
489 | 445 | C<&$subref()> や C<&{$subref}()> のような、サブルーチンの名前や |
490 | 446 | リファレンスを使った間接的なサブルーン呼び出しを行いたいたいときにも |
491 | 447 | C<&> は省略することはできません; 但し C<< $subref->() >> の記法が問題を |
492 | 448 | 解決します。 |
493 | 449 | 詳しくは L<perlref> を参照してください。 |
494 | 450 | X<&> |
495 | 451 | |
496 | 452 | =begin original |
497 | 453 | |
498 | 454 | Subroutines may be called recursively. If a subroutine is called |
499 | 455 | using the C<&> form, the argument list is optional, and if omitted, |
500 | 456 | no C<@_> array is set up for the subroutine: the C<@_> array at the |
501 | 457 | time of the call is visible to subroutine instead. This is an |
502 | 458 | efficiency mechanism that new users may wish to avoid. |
503 | 459 | X<recursion> |
504 | 460 | |
505 | 461 | =end original |
506 | 462 | |
507 | 463 | サブルーチンは再帰的に呼び出すこともできます。 |
508 | 464 | あるサブルーチンが C<&> 付きで呼び出されたなら、引数リストは省略可能で、 |
509 | 465 | もし省略されたなら、そのサブルーチンに対して C<@_> 配列は設定されません: |
510 | 466 | 代わりに呼び出し時の C<@_> 配列がサブルーチンに対して可視となります。 |
511 | 467 | これは新しいユーザーが避けたいと思う効果的なメカニズムです。 |
512 | 468 | X<recursion> |
513 | 469 | |
514 | 470 | =begin original |
515 | 471 | |
516 | &foo(1,2,3); | |
472 | &foo(1,2,3); # pass three arguments | |
517 | foo(1,2,3); | |
473 | foo(1,2,3); # the same | |
518 | 474 | |
519 | 475 | =end original |
520 | 476 | |
521 | &foo(1,2,3); | |
477 | &foo(1,2,3); # 三つの引数を渡す | |
522 | foo(1,2,3); | |
478 | foo(1,2,3); # 上と同じ | |
523 | 479 | |
524 | 480 | =begin original |
525 | 481 | |
526 | foo(); | |
482 | foo(); # pass a null list | |
527 | &foo(); | |
483 | &foo(); # the same | |
528 | 484 | |
529 | 485 | =end original |
530 | 486 | |
531 | foo(); | |
487 | foo(); # 空リストを渡す | |
532 | &foo(); | |
488 | &foo(); # 上と同じ | |
533 | 489 | |
534 | 490 | =begin original |
535 | 491 | |
536 | &foo; | |
492 | &foo; # foo() get current args, like foo(@_) !! | |
537 | | |
493 | foo; # like foo() IFF sub foo predeclared, else "foo" | |
538 | foo; # like foo() iff sub foo predeclared, else | |
539 | # a compile-time error | |
540 | no strict 'subs'; | |
541 | foo; # like foo() iff sub foo predeclared, else | |
542 | # a literal string "foo" | |
543 | 494 | |
544 | 495 | =end original |
545 | 496 | |
546 | &foo; | |
497 | &foo; # foo() は foo(@_) と同様現在の引数を取る!! | |
547 | | |
498 | foo; # サブルーチン foo が予め定義されている場合に限り | |
548 | | |
499 | # foo() と同様、それ以外では "foo" | |
549 | # foo() と同様、それ以外ではコンパイル時エラー | |
550 | no strict 'subs'; | |
551 | foo; # サブルーチン foo が予め定義されている場合に限り | |
552 | # foo() と同様、それ以外では文字列リテラル "foo" | |
553 | 500 | |
554 | 501 | =begin original |
555 | 502 | |
556 | 503 | Not only does the C<&> form make the argument list optional, it also |
557 | 504 | disables any prototype checking on arguments you do provide. This |
558 | 505 | is partly for historical reasons, and partly for having a convenient way |
559 | 506 | to cheat if you know what you're doing. See L</Prototypes> below. |
560 | 507 | X<&> |
561 | 508 | |
562 | 509 | =end original |
563 | 510 | |
564 | 511 | C<&> 形式は引数リストを省略可能にするばかりでなく、与えられた引数に |
565 | 512 | 対するすべてのプロトタイプチェックも無効にします。 |
566 | 513 | これは一部には歴史的な理由であり、一部にはあなたが自分の行っている動作を |
567 | 514 | わかっているときにうまくごまかしを行う便利な方法を残しておくためです。 |
568 | 515 | 後に出てくる L</Prototypes> を参照してください。 |
569 | 516 | X<&> |
570 | 517 | |
571 | 518 | =begin original |
572 | 519 | |
573 | 520 | Since Perl 5.16.0, the C<__SUB__> token is available under C<use feature |
574 | 'current_sub'> and C<use | |
521 | 'current_sub'> and C<use 5.16.0>. It will evaluate to a reference to the | |
575 | 522 | currently-running sub, which allows for recursive calls without knowing |
576 | 523 | your subroutine's name. |
577 | 524 | |
578 | 525 | =end original |
579 | 526 | |
580 | Perl 5.16.0 から、C<use feature 'current_sub'> または C<use | |
527 | Perl 5.16.0 から、C<use feature 'current_sub'> または C<use 5.16.0> が有効の | |
581 | 528 | 場合は C<__SUB__> トークンが利用可能です。 |
582 | 529 | これは現在実行しているサブルーチンへのリファレンスを評価するので、 |
583 | 530 | 現在のサブルーチン名を知ることなく再帰呼び出しができるようになります。 |
584 | 531 | |
585 | use | |
532 | use 5.16.0; | |
586 | 533 | my $factorial = sub { |
587 | | |
534 | my ($x) = @_; | |
588 | | |
535 | return 1 if $x == 1; | |
589 | | |
536 | return($x * __SUB__->( $x - 1 ) ); | |
590 | 537 | }; |
591 | 538 | |
592 | 539 | =begin original |
593 | 540 | |
594 | The behavior of C<__SUB__> within a regex code block (such as C</(?{...})/>) | |
595 | is subject to change. | |
596 | ||
597 | =end original | |
598 | ||
599 | (C</(?{...})/> のような) 正規表現コードブロック中の C<__SUB__> の振る舞いは | |
600 | 変更されるかもしれません。 | |
601 | ||
602 | =begin original | |
603 | ||
604 | 541 | Subroutines whose names are in all upper case are reserved to the Perl |
605 | 542 | core, as are modules whose names are in all lower case. A subroutine in |
606 | 543 | all capitals is a loosely-held convention meaning it will be called |
607 | 544 | indirectly by the run-time system itself, usually due to a triggered event. |
608 | Subroutines | |
545 | Subroutines that do special, pre-defined things include C<AUTOLOAD>, C<CLONE>, | |
609 | ||
546 | C<DESTROY> plus all functions mentioned in L<perltie> and L<PerlIO::via>. | |
610 | special, pre-defined things. | |
611 | 547 | |
612 | 548 | =end original |
613 | 549 | |
614 | 名前が全て大文字であるサブルーチンは、 | |
550 | 名前が全て大文字であるサブルーチンは、 | |
615 | 予約されているのと同じように | |
551 | 名前が全て小文字のモジュール名が (訳注: pragma 用として) 予約されているのと同じように | |
616 | 実行時にシステム自身によって | |
552 | Perl のコアで予約されています。実行時にシステム自身によって | |
617 | 呼び出されるサブルーチンは、 | |
553 | (通常はイベントをトリガーとして) 間接的に呼び出されるサブルーチンは、 | |
618 | 名前が | |
554 | 名前を全て大文字で書くのがしきたりです。 | |
619 | ||
555 | 特殊で、あらかじめ決められていることをするサブルーチンは、 | |
556 | C<AUTOLOAD>, C<CLONE>, C<DESTROY> に、L<perltie> と L<PerlIO::via> で | |
557 | 説明されている関数全てを加えたものです。 | |
620 | 558 | |
621 | =over | |
622 | ||
623 | =item documented later in this document | |
624 | ||
625 | (この文書で後述するもの) | |
626 | ||
627 | C<AUTOLOAD> | |
628 | ||
629 | =item documented in L<perlmod> | |
630 | ||
631 | (L<perlmod> に記述しているもの) | |
632 | ||
633 | C<CLONE>, C<CLONE_SKIP> | |
634 | ||
635 | =item documented in L<perlobj> | |
636 | ||
637 | (L<perlobj> に記述しているもの) | |
638 | ||
639 | C<DESTROY>, C<DOES> | |
640 | ||
641 | =item documented in L<perltie> | |
642 | ||
643 | (L<perltie> に記述しているもの) | |
644 | ||
645 | C<BINMODE>, C<CLEAR>, C<CLOSE>, C<DELETE>, C<DESTROY>, C<EOF>, C<EXISTS>, | |
646 | C<EXTEND>, C<FETCH>, C<FETCHSIZE>, C<FILENO>, C<FIRSTKEY>, C<GETC>, | |
647 | C<NEXTKEY>, C<OPEN>, C<POP>, C<PRINT>, C<PRINTF>, C<PUSH>, C<READ>, | |
648 | C<READLINE>, C<SCALAR>, C<SEEK>, C<SHIFT>, C<SPLICE>, C<STORE>, | |
649 | C<STORESIZE>, C<TELL>, C<TIEARRAY>, C<TIEHANDLE>, C<TIEHASH>, | |
650 | C<TIESCALAR>, C<UNSHIFT>, C<UNTIE>, C<WRITE> | |
651 | ||
652 | =item documented in L<PerlIO::via> | |
653 | ||
654 | (L<PerlIO::via> に記述しているもの) | |
655 | ||
656 | C<BINMODE>, C<CLEARERR>, C<CLOSE>, C<EOF>, C<ERROR>, C<FDOPEN>, C<FILENO>, | |
657 | C<FILL>, C<FLUSH>, C<OPEN>, C<POPPED>, C<PUSHED>, C<READ>, C<SEEK>, | |
658 | C<SETLINEBUF>, C<SYSOPEN>, C<TELL>, C<UNREAD>, C<UTF8>, C<WRITE> | |
659 | ||
660 | =item documented in L<perlfunc> | |
661 | ||
662 | (L<perlfunc> に記述しているもの) | |
663 | ||
664 | L<< C<import>|perlfunc/use >>, L<< C<unimport>|perlfunc/use >>, | |
665 | L<< C<INC>|perlfunc/require >> | |
666 | ||
667 | =item documented in L<UNIVERSAL> | |
668 | ||
669 | (L<UNIVERSAL> に記述しているもの) | |
670 | ||
671 | C<VERSION> | |
672 | ||
673 | =item documented in L<perldebguts> | |
674 | ||
675 | (L<perldebguts> に記述しているもの) | |
676 | ||
677 | C<DB::DB>, C<DB::sub>, C<DB::lsub>, C<DB::goto>, C<DB::postponed> | |
678 | ||
679 | =item undocumented, used internally by the L<overload> feature | |
680 | ||
681 | (文書化されておらず、L<overload> 機能によって内部で使われているもの) | |
682 | ||
683 | 559 | =begin original |
684 | 560 | |
685 | any starting with C<(> | |
686 | ||
687 | =end original | |
688 | ||
689 | C<(> で始まるもの全て | |
690 | ||
691 | =back | |
692 | ||
693 | =begin original | |
694 | ||
695 | 561 | The C<BEGIN>, C<UNITCHECK>, C<CHECK>, C<INIT> and C<END> subroutines |
696 | 562 | are not so much subroutines as named special code blocks, of which you |
697 | 563 | can have more than one in a package, and which you can B<not> call |
698 | 564 | explicitly. See L<perlmod/"BEGIN, UNITCHECK, CHECK, INIT and END"> |
699 | 565 | |
700 | 566 | =end original |
701 | 567 | |
702 | C<BEGIN>, C<UNITCHECK>, C<CHECK>, C<INIT>, C<END> サブルーチンは | |
568 | The C<BEGIN>, C<UNITCHECK>, C<CHECK>, C<INIT>, C<END> サブルーチンは | |
703 | 569 | サブルーチンというよりは特殊コードブロックで、その中でも一つのパッケージに |
704 | 570 | 複数作ることができ、明示的には B<呼び出せません> 。 |
705 | 571 | L<perlmod/"BEGIN, UNITCHECK, CHECK, INIT and END"> を参照してください。 |
706 | 572 | |
707 | =head2 Signatures | |
708 | ||
709 | (シグネチャ) | |
710 | ||
711 | X<formal parameter> X<parameter, formal> | |
712 | ||
713 | =begin original | |
714 | ||
715 | Perl has a facility to allow a subroutine's formal parameters to be | |
716 | declared by special syntax, separate from the procedural code of the | |
717 | subroutine body. The formal parameter list is known as a I<signature>. | |
718 | ||
719 | =end original | |
720 | ||
721 | Perl は、サブルーチン本体の手続き的コードと分離された特殊な文法を | |
722 | 宣言することで、サブルーチンの仮引数を指定できる機能を持ちます。 | |
723 | 仮引数リストは I<シグネチャ> (signature) として知られます。 | |
724 | ||
725 | =begin original | |
726 | ||
727 | This facility must be enabled before it can be used. It is enabled | |
728 | automatically by a C<use v5.36> (or higher) declaration, or more | |
729 | directly by C<use feature 'signatures'>, in the current scope. | |
730 | ||
731 | =end original | |
732 | ||
733 | この機能は、使われる前に有効にされなければなりません。 | |
734 | これは C<use v5.36> (またはそれ以上) 宣言で自動的に、 | |
735 | あるいはより直接的に現在のスコープ内で C<use feature 'signatures'> と | |
736 | することで有効になります。 | |
737 | ||
738 | =begin original | |
739 | ||
740 | The signature is part of a subroutine's body. Normally the body of a | |
741 | subroutine is simply a braced block of code, but when using a signature, | |
742 | the signature is a parenthesised list that goes immediately before the | |
743 | block, after any name or attributes. | |
744 | ||
745 | =end original | |
746 | ||
747 | シグネチャはサブルーチンの本体の一部です。 | |
748 | 通常サブルーチンの本体は単に中かっこのブロックのコードですが、 | |
749 | シグネチャを使うとき、シグネチャはブロックの直前、 | |
750 | なんらかの名前や属性の直後に置かれたかっこつきリストです。 | |
751 | ||
752 | =begin original | |
753 | ||
754 | For example, | |
755 | ||
756 | =end original | |
757 | ||
758 | 例えば、 | |
759 | ||
760 | sub foo :lvalue ($x, $y = 1, @z) { .... } | |
761 | ||
762 | =begin original | |
763 | ||
764 | The signature declares lexical variables that are | |
765 | in scope for the block. When the subroutine is called, the signature | |
766 | takes control first. It populates the signature variables from the | |
767 | list of arguments that were passed. If the argument list doesn't meet | |
768 | the requirements of the signature, then it will throw an exception. | |
769 | When the signature processing is complete, control passes to the block. | |
770 | ||
771 | =end original | |
772 | ||
773 | シグネチャはそのブロックのスコープのレキシカル変数を宣言します。 | |
774 | サブルーチンが呼び出されるとき、シグネチャが最初に制御します。 | |
775 | 渡された引数リストからシグネチャ変数を作ります。 | |
776 | 引数リストがシグネチャの要求に一致しない場合、例外が投げられます。 | |
777 | シグネチャ処理が完了したとき、制御はブロックに移ります。 | |
778 | ||
779 | =begin original | |
780 | ||
781 | Positional parameters are handled by simply naming scalar variables in | |
782 | the signature. For example, | |
783 | ||
784 | =end original | |
785 | ||
786 | 位置パラメータは単にシグネチャの名前付きスカラ変数で扱われます。 | |
787 | 例えば、 | |
788 | ||
789 | sub foo ($left, $right) { | |
790 | return $left + $right; | |
791 | } | |
792 | ||
793 | =begin original | |
794 | ||
795 | takes two positional parameters, which must be filled at runtime by | |
796 | two arguments. By default the parameters are mandatory, and it is | |
797 | not permitted to pass more arguments than expected. So the above is | |
798 | equivalent to | |
799 | ||
800 | =end original | |
801 | ||
802 | これは二つの位置パラメータを持ち、二つの引数によって実行時に | |
803 | 埋められなければなりません。 | |
804 | デフォルトではパラメータは必須で、想定以上の引数を渡すことは許されません。 | |
805 | 従って前述のものは以下と等価です | |
806 | ||
807 | sub foo { | |
808 | die "Too many arguments for subroutine" unless @_ <= 2; | |
809 | die "Too few arguments for subroutine" unless @_ >= 2; | |
810 | my $left = $_[0]; | |
811 | my $right = $_[1]; | |
812 | return $left + $right; | |
813 | } | |
814 | ||
815 | =begin original | |
816 | ||
817 | An argument can be ignored by omitting the main part of the name from | |
818 | a parameter declaration, leaving just a bare C<$> sigil. For example, | |
819 | ||
820 | =end original | |
821 | ||
822 | 引数は、パラメータ宣言の名前のメイン部分を省略して裸の C<$> 印だけに | |
823 | することで無視できます。 | |
824 | 例えば、 | |
825 | ||
826 | sub foo ($first, $, $third) { | |
827 | return "first=$first, third=$third"; | |
828 | } | |
829 | ||
830 | =begin original | |
831 | ||
832 | Although the ignored argument doesn't go into a variable, it is still | |
833 | mandatory for the caller to pass it. | |
834 | ||
835 | =end original | |
836 | ||
837 | 無視された引数は変数には入りませんが、呼び出し元から渡されるときには | |
838 | 必須のままです。 | |
839 | ||
840 | =begin original | |
841 | ||
842 | A positional parameter is made optional by giving a default value, | |
843 | separated from the parameter name by C<=>: | |
844 | ||
845 | =end original | |
846 | ||
847 | 位置パラメータは、パラメータ名と C<=> で分けられたデフォルト値を与えることで | |
848 | オプションにできます: | |
849 | ||
850 | sub foo ($left, $right = 0) { | |
851 | return $left + $right; | |
852 | } | |
853 | ||
854 | =begin original | |
855 | ||
856 | The above subroutine may be called with either one or two arguments. | |
857 | The default value expression is evaluated when the subroutine is called, | |
858 | so it may provide different default values for different calls. It is | |
859 | only evaluated if the argument was actually omitted from the call. | |
860 | For example, | |
861 | ||
862 | =end original | |
863 | ||
864 | 前述のサブルーチンは 1 引数か 2 引数で呼び出せます。 | |
865 | デフォルト値式はサブルーチンが呼び出されるときに評価されます; 従って | |
866 | 呼び出し毎に異なったデフォルト値を提供できます。 | |
867 | これは呼び出しで引数が実際に省略されたときにのみ評価されます。 | |
868 | 例えば、 | |
869 | ||
870 | my $auto_id = 0; | |
871 | sub foo ($thing, $id = $auto_id++) { | |
872 | print "$thing has ID $id"; | |
873 | } | |
874 | ||
875 | =begin original | |
876 | ||
877 | automatically assigns distinct sequential IDs to things for which no | |
878 | ID was supplied by the caller. A default value expression may also | |
879 | refer to parameters earlier in the signature, making the default for | |
880 | one parameter vary according to the earlier parameters. For example, | |
881 | ||
882 | =end original | |
883 | ||
884 | これは、呼び出し元から ID が指定されなかった場合、ユニークな連番 ID が | |
885 | 自動的に代入されます。 | |
886 | デフォルト値式はシグネチャ内で前にあるパラメータを参照することもでき、 | |
887 | 前のパラメータによって異なったパラメータのデフォルトを作ります。 | |
888 | 例えば、 | |
889 | ||
890 | sub foo ($first_name, $surname, $nickname = $first_name) { | |
891 | print "$first_name $surname is known as \"$nickname\""; | |
892 | } | |
893 | ||
894 | =begin original | |
895 | ||
896 | Since Perl 5.38, a default value expression can also be written using the | |
897 | C<//=> operator, where it will be evaluated and used if the caller omitted | |
898 | a value or the value provided was C<undef>. | |
899 | ||
900 | =end original | |
901 | ||
902 | Perl 5.38 から、デフォルト値式は、C<//=> 演算子を使って書くこともできます; | |
903 | これは、呼び出し元が値を省略するか、提供された値が | |
904 | C<undef> の時に評価されて使われます。 | |
905 | ||
906 | sub foo ($name //= "world") { | |
907 | print "Hello, $name"; | |
908 | } | |
909 | ||
910 | foo(undef); # will print "Hello, world" | |
911 | ||
912 | =begin original | |
913 | ||
914 | Similarly since Perl 5.38, the C<||=> operator can be used to provide a | |
915 | default expression to be used whenever the caller provided a false value | |
916 | (and remember that a missing or C<undef> value are also false). | |
917 | ||
918 | =end original | |
919 | ||
920 | 同様に Perl 5.38 から、C<||=> 演算子は、呼び出し元が何らかの偽の値を | |
921 | 提供したときに使われるデフォルト式を提供するために使われます | |
922 | (値がなかったり C<undef> 値の場合も偽であることを忘れないでください)。 | |
923 | ||
924 | sub foo ($x ||= 10) { | |
925 | return 5 + $x; | |
926 | } | |
927 | ||
928 | =begin original | |
929 | ||
930 | An optional parameter can be nameless just like a mandatory parameter. | |
931 | For example, | |
932 | ||
933 | =end original | |
934 | ||
935 | オプションのパラメータは必須のパラメータと同様に名前なしにできます。 | |
936 | 例えば、 | |
937 | ||
938 | sub foo ($thing, $ = 1) { | |
939 | print $thing; | |
940 | } | |
941 | ||
942 | =begin original | |
943 | ||
944 | The parameter's default value will still be evaluated if the corresponding | |
945 | argument isn't supplied, even though the value won't be stored anywhere. | |
946 | This is in case evaluating it has important side effects. However, it | |
947 | will be evaluated in void context, so if it doesn't have side effects | |
948 | and is not trivial it will generate a warning if the "void" warning | |
949 | category is enabled. If a nameless optional parameter's default value | |
950 | is not important, it may be omitted just as the parameter's name was: | |
951 | ||
952 | =end original | |
953 | ||
954 | パラメータのデフォルト値は対応する引数が指定されなくても評価されますが、 | |
955 | その値はどこにも保管されません。 | |
956 | これは、評価が重要な副作用を持つ場合です。 | |
957 | しかし、これは無効コンテキストで評価されるので、副作用がなくて | |
958 | ありふれていなければ、"void" 警告カテゴリが有効の場合は警告が生成されます。 | |
959 | 名前なしオプションパラメータのデフォルト値が重要でないなら、パラメータの名前と | |
960 | 同じように省略できます: | |
961 | ||
962 | sub foo ($thing, $=) { | |
963 | print $thing; | |
964 | } | |
965 | ||
966 | =begin original | |
967 | ||
968 | Optional positional parameters must come after all mandatory positional | |
969 | parameters. (If there are no mandatory positional parameters then the | |
970 | optional positional parameters can be the first thing in the signature.) | |
971 | If there are multiple optional positional parameters and not enough | |
972 | arguments are supplied to fill them all, they will be filled from left | |
973 | to right. | |
974 | ||
975 | =end original | |
976 | ||
977 | オプション位置パラメータは全ての必須位置パラメータの | |
978 | 後ろでなければなりません。 | |
979 | (必須位置パラメータがない場合は、オプション位置パラメータをシグネチャの | |
980 | 最初の位置に置けます。) | |
981 | 複数のオプション位置パラメータがあって全てのパラメータを設定するのに十分な | |
982 | 引数が供給されなかった場合、左から右に設定されます。 | |
983 | ||
984 | =begin original | |
985 | ||
986 | After positional parameters, additional arguments may be captured in a | |
987 | slurpy parameter. The simplest form of this is just an array variable: | |
988 | ||
989 | =end original | |
990 | ||
991 | 位置パラメータの後、追加の引数は吸い込みパラメータに捕捉されます。 | |
992 | これの最も単純な形式は単なる配列変数です: | |
993 | ||
994 | sub foo ($filter, @inputs) { | |
995 | print $filter->($_) foreach @inputs; | |
996 | } | |
997 | ||
998 | =begin original | |
999 | ||
1000 | With a slurpy parameter in the signature, there is no upper limit on how | |
1001 | many arguments may be passed. A slurpy array parameter may be nameless | |
1002 | just like a positional parameter, in which case its only effect is to | |
1003 | turn off the argument limit that would otherwise apply: | |
1004 | ||
1005 | =end original | |
1006 | ||
1007 | シグネチャで吸い込みパラメータがある場合、渡せる引数の数に上限はありません。 | |
1008 | 吸い込み配列パラメータは位置パラメータと同様に名前なしにでき、この場合は | |
1009 | その他の場合で適用される引数制限をオフにする効果のみがあります: | |
1010 | ||
1011 | sub foo ($thing, @) { | |
1012 | print $thing; | |
1013 | } | |
1014 | ||
1015 | =begin original | |
1016 | ||
1017 | A slurpy parameter may instead be a hash, in which case the arguments | |
1018 | available to it are interpreted as alternating keys and values. | |
1019 | There must be as many keys as values: if there is an odd argument then | |
1020 | an exception will be thrown. Keys will be stringified, and if there are | |
1021 | duplicates then the later instance takes precedence over the earlier, | |
1022 | as with standard hash construction. | |
1023 | ||
1024 | =end original | |
1025 | ||
1026 | 吸い込みパラメータは代わりにハッシュにすることもでき、その場合利用可能な | |
1027 | 引数は交互にキーと値として解釈されます。 | |
1028 | キーと値の数は同じでなければなりません: 引数が奇数だと例外が投げられます。 | |
1029 | キーは文字列化され、重複があると、通常のハッシュの構築と同じように、 | |
1030 | 後から指定したものが先に指定したものを上書きします。 | |
1031 | ||
1032 | sub foo ($filter, %inputs) { | |
1033 | print $filter->($_, $inputs{$_}) foreach sort keys %inputs; | |
1034 | } | |
1035 | ||
1036 | =begin original | |
1037 | ||
1038 | A slurpy hash parameter may be nameless just like other kinds of | |
1039 | parameter. It still insists that the number of arguments available to | |
1040 | it be even, even though they're not being put into a variable. | |
1041 | ||
1042 | =end original | |
1043 | ||
1044 | 吸い込みハッシュパラメータは他の種類のパラメータと同様に名前なしにできます。 | |
1045 | 変数に代入されなくても、利用できる引数の数が偶数であるという必要はあります。 | |
1046 | ||
1047 | sub foo ($thing, %) { | |
1048 | print $thing; | |
1049 | } | |
1050 | ||
1051 | =begin original | |
1052 | ||
1053 | A slurpy parameter, either array or hash, must be the last thing in the | |
1054 | signature. It may follow mandatory and optional positional parameters; | |
1055 | it may also be the only thing in the signature. Slurpy parameters cannot | |
1056 | have default values: if no arguments are supplied for them then you get | |
1057 | an empty array or empty hash. | |
1058 | ||
1059 | =end original | |
1060 | ||
1061 | 吸い込みパラメータは、配列でもハッシュでも、シグネチャの | |
1062 | 最後でなければなりません。 | |
1063 | 必須とオプションの位置パラメータに引き続くことができます; シグネチャの唯一の | |
1064 | 要素でもかまいません。 | |
1065 | 吸い込みパラメータはデフォルト値を持つことはできません: 引数が指定されないと | |
1066 | 空配列や空ハッシュを得ます。 | |
1067 | ||
1068 | =begin original | |
1069 | ||
1070 | A signature may be entirely empty, in which case all it does is check | |
1071 | that the caller passed no arguments: | |
1072 | ||
1073 | =end original | |
1074 | ||
1075 | シグネチャは完全に空にすることもできます; この場合、呼び出し側が引数を | |
1076 | 渡していないことだけをチェックします: | |
1077 | ||
1078 | sub foo () { | |
1079 | return 123; | |
1080 | } | |
1081 | ||
1082 | =begin original | |
1083 | ||
1084 | Prior to Perl 5.36 these were considered experimental, and emitted a | |
1085 | warning in the C<experimental::signatures> category. From Perl 5.36 | |
1086 | onwards this no longer happens, though the warning category still exists | |
1087 | for back-compatibility with code that attempts to disable it with a | |
1088 | statement such as: | |
1089 | ||
1090 | =end original | |
1091 | ||
1092 | Perl 5.36 より前では、これらは実験的と考えられていて、 | |
1093 | C<experimental::signatures> カテゴリの警告を出力していました。 | |
1094 | Perl 5.36 以降は、もはやこれはおきませんが、 | |
1095 | 次のような文でこれを無効にしようとするコードとの後方互換性のために、 | |
1096 | 警告カテゴリは存在したままです: | |
1097 | ||
1098 | no warnings 'experimental::signatures'; | |
1099 | ||
1100 | =begin original | |
1101 | ||
1102 | In the current Perl implementation, when using a signature the arguments | |
1103 | are still also available in the special array variable C<@_>. However, | |
1104 | accessing them via this array is now discouraged, and should not be | |
1105 | relied upon in newly-written code as this ability may change in a future | |
1106 | version. Code that attempts to access the C<@_> array will produce | |
1107 | warnings in the C<experimental::args_array_with_signatures> category when | |
1108 | compiled: | |
1109 | ||
1110 | =end original | |
1111 | ||
1112 | 現在の perl の実装では、シグネチャを使うときも、 | |
1113 | 特殊配列変数 C<@_> にも入ります。 | |
1114 | しかし、この配列を通してこれらにアクセするのは現在非推奨であり、 | |
1115 | この機能は将来のバージョンで変わるかもしれないので、 | |
1116 | 新しく書かれるコードはこれに依存するべきではありません。 | |
1117 | C<@_> 配列にアクセスしようとするコードは、 | |
1118 | コンパイル時に C<experimental::args_array_with_signatures> カテゴリの | |
1119 | 警告を発生させます: | |
1120 | ||
1121 | sub f ($x) { | |
1122 | # This line emits the warning seen below | |
1123 | print "Arguments are @_"; | |
1124 | } | |
1125 | ||
1126 | Z<> | |
1127 | ||
1128 | Use of @_ in join or string with signatured subroutine is | |
1129 | experimental at ... | |
1130 | ||
1131 | =begin original | |
1132 | ||
1133 | There is a difference between the two ways of accessing the arguments: | |
1134 | C<@_> I<aliases> the arguments, but the signature variables get | |
1135 | I<copies> of the arguments. So writing to a signature variable only | |
1136 | changes that variable, and has no effect on the caller's variables, but | |
1137 | writing to an element of C<@_> modifies whatever the caller used to | |
1138 | supply that argument. | |
1139 | ||
1140 | =end original | |
1141 | ||
1142 | 引数への二つのアクセス方法には違いがあります: | |
1143 | C<@_> は引数の I<別名> ですが、 | |
1144 | シグネチャ変数は引数の I<コピー> です。 | |
1145 | 従って、シグネチャ変数に書き込むとその変数だけが変更され、呼び出し元の | |
1146 | 変数には影響しませんが、C<@_> の要素に書き込むと引数を指定した呼び出し元が | |
1147 | 使ったものが修正されます。 | |
1148 | ||
1149 | =begin original | |
1150 | ||
1151 | There is a potential syntactic ambiguity between signatures and prototypes | |
1152 | (see L</Prototypes>), because both start with an opening parenthesis and | |
1153 | both can appear in some of the same places, such as just after the name | |
1154 | in a subroutine declaration. For historical reasons, when signatures | |
1155 | are not enabled, any opening parenthesis in such a context will trigger | |
1156 | very forgiving prototype parsing. Most signatures will be interpreted | |
1157 | as prototypes in those circumstances, but won't be valid prototypes. | |
1158 | (A valid prototype cannot contain any alphabetic character.) This will | |
1159 | lead to somewhat confusing error messages. | |
1160 | ||
1161 | =end original | |
1162 | ||
1163 | シグネチャとプロトタイプ(L</Prototypes> 参照)の間には潜在的に文法的な | |
1164 | あいまいさがあります; 両方とも開きかっこで始まり、サブルーチン宣言の名前の | |
1165 | 直後といった同じ場所に現れるからです。 | |
1166 | 歴史的な理由により、シグネチャが有効でない場合、このようなコンテキストでの | |
1167 | あらゆる開きかっこはとても寛容なプロトタイプのパースを引き起こします。 | |
1168 | ほとんどのシグネチャはこの状態ではプロトタイプとして解釈されますが、 | |
1169 | 正当なプロトタイプではありません。 | |
1170 | (正当なプロトタイプは英数字を含むことができません。) | |
1171 | これは少し混乱したエラーメッセージを引き起こします。 | |
1172 | ||
1173 | =begin original | |
1174 | ||
1175 | To avoid ambiguity, when signatures are enabled the special syntax | |
1176 | for prototypes is disabled. There is no attempt to guess whether a | |
1177 | parenthesised group was intended to be a prototype or a signature. | |
1178 | To give a subroutine a prototype under these circumstances, use a | |
1179 | L<prototype attribute|attributes/Built-in Attributes>. For example, | |
1180 | ||
1181 | =end original | |
1182 | ||
1183 | 曖昧さを避けるために、シグネチャが有効の時はプロトタイプのための特殊文法は | |
1184 | 無効になります。 | |
1185 | かっこのグループがプロトタイプかシグネチャかを推測しようとはしません。 | |
1186 | この状態でサブルーチンにプロトタイプを指定するには、 | |
1187 | L<プロトタイプ属性|attributes/Built-in Attributes> を使ってください。 | |
1188 | 例えば、 | |
1189 | ||
1190 | sub foo :prototype($) { $_[0] } | |
1191 | ||
1192 | =begin original | |
1193 | ||
1194 | It is entirely possible for a subroutine to have both a prototype and | |
1195 | a signature. They do different jobs: the prototype affects compilation | |
1196 | of calls to the subroutine, and the signature puts argument values into | |
1197 | lexical variables at runtime. You can therefore write | |
1198 | ||
1199 | =end original | |
1200 | ||
1201 | プロトタイプとシグネチャの両方を持ったサブルーチンは完全に可能です。 | |
1202 | これらは異なった仕事をします: プロトタイプはサブルーチン呼び出しのコンパイルに | |
1203 | 影響を与え、シグネチャは実行時に引数の値をレキシカル変数に設定します。 | |
1204 | 従って、このように書けます | |
1205 | ||
1206 | sub foo :prototype($$) ($left, $right) { | |
1207 | return $left + $right; | |
1208 | } | |
1209 | ||
1210 | =begin original | |
1211 | ||
1212 | The prototype attribute, and any other attributes, must come before | |
1213 | the signature. The signature always immediately precedes the block of | |
1214 | the subroutine's body. | |
1215 | ||
1216 | =end original | |
1217 | ||
1218 | プロトタイプ属性、およびその他の属性はシグネチャの前に来なければなりません。 | |
1219 | シグネチャは常にサブルーチンの本体のブロックの直前です。 | |
1220 | ||
1221 | 573 | =head2 Private Variables via my() |
1222 | 574 | X<my> X<variable, lexical> X<lexical> X<lexical variable> X<scope, lexical> |
1223 | 575 | X<lexical scope> X<attributes, my> |
1224 | 576 | |
1225 | 577 | (my() によるプライベート変数) |
1226 | 578 | |
1227 | 579 | =begin original |
1228 | 580 | |
1229 | 581 | Synopsis: |
1230 | 582 | |
1231 | 583 | =end original |
1232 | 584 | |
1233 | 585 | 概要: |
1234 | 586 | |
1235 | my $foo; | |
587 | my $foo; # declare $foo lexically local | |
1236 | my (@wid, %get); | |
588 | my (@wid, %get); # declare list of variables local | |
1237 | my $foo = "flurp"; | |
589 | my $foo = "flurp"; # declare $foo lexical, and init it | |
1238 | my @oof = @bar; | |
590 | my @oof = @bar; # declare @oof lexical, and init it | |
1239 | my $x : Foo = $y; | |
591 | my $x : Foo = $y; # similar, with an attribute applied | |
1240 | 592 | |
1241 | 593 | =begin original |
1242 | 594 | |
1243 | 595 | B<WARNING>: The use of attribute lists on C<my> declarations is still |
1244 | 596 | evolving. The current semantics and interface are subject to change. |
1245 | 597 | See L<attributes> and L<Attribute::Handlers>. |
1246 | 598 | |
1247 | 599 | =end original |
1248 | 600 | |
1249 | 601 | B<警告>: C<my> 定義での属性リストの使用はいまだ改良の途中です。 |
1250 | 602 | 現在の文法とインターフェースは変更される可能性があります。 |
1251 | 603 | L<attributes> と L<Attribute::Handlers> を参照してください。 |
1252 | 604 | |
1253 | 605 | =begin original |
1254 | 606 | |
1255 | 607 | The C<my> operator declares the listed variables to be lexically |
1256 | confined to the enclosing block, conditional | |
608 | confined to the enclosing block, conditional (C<if/unless/elsif/else>), | |
1257 | (C< | |
609 | loop (C<for/foreach/while/until/continue>), subroutine, C<eval>, | |
1258 | ||
610 | or C<do/require/use>'d file. If more than one value is listed, the | |
1259 | or C<do>/C<require>/C<use>'d file. If more than one value is listed, the | |
1260 | 611 | list must be placed in parentheses. All listed elements must be |
1261 | 612 | legal lvalues. Only alphanumeric identifiers may be lexically |
1262 | 613 | scoped--magical built-ins like C<$/> must currently be C<local>ized |
1263 | with C<local> instead | |
614 | with C<local> instead. | |
1264 | 615 | |
1265 | 616 | =end original |
1266 | 617 | |
1267 | C<my> 演算子は、それを囲んでいるブロック、条件文 | |
618 | C<my> 演算子は、それを囲んでいるブロック、条件文 (C<if/unless/elsif/else>)、 | |
1268 | (C< | |
619 | ループ(C<for/foreach/while/until/continue>)、サブルーチン、C<eval>、 | |
1269 | ||
620 | あるいは C<do/require/use> されたファイルにレキシカルに閉じ込められる | |
1270 | あるいは C<do>/C<require>/C<use> されたファイルにレキシカルに閉じ込められる | |
1271 | 621 | 変数を定義します。二つ以上リストアップされている場合には、そのリストは |
1272 | 622 | 括弧でくくられていなければなりません。 |
1273 | 623 | すべてのリスト要素は正しい左辺値でなければなりません。 |
1274 | C<$/> のような組み込み変数が現時点では | |
624 | C<$/> のような組み込み変数が現時点では C<local> で | |
1275 | スコープを動的に制限する代わりに C<local> で | |
1276 | 625 | B<局所化> されなければならないのに対し、 |
1277 | アルファベットと数字による識別子はレキシカルでマジカルな | |
626 | アルファベットと数字による識別子はレキシカルでマジカルなスコープになります。 | |
1278 | スコープになります。 | |
1279 | 627 | |
1280 | 628 | =begin original |
1281 | 629 | |
1282 | Unlike | |
630 | Unlike dynamic variables created by the C<local> operator, lexical | |
1283 | ||
631 | variables declared with C<my> are totally hidden from the outside | |
1284 | world, including any called subroutines. This is true if it's the | |
632 | world, including any called subroutines. This is true if it's the | |
1285 | subroutine called from itself or elsewhere--every call gets | |
633 | same subroutine called from itself or elsewhere--every call gets | |
634 | its own copy. | |
1286 | 635 | X<local> |
1287 | 636 | |
1288 | 637 | =end original |
1289 | 638 | |
1290 | C<local> 文によって | |
639 | C<local> 文によって生成される動的変数とは異なり、C<my> を使って | |
1291 | C<my> を使って | |
1292 | 640 | 宣言されたレキシカル変数はそれを呼び出したサブルーチンも含め、外側の |
1293 | 641 | 世界からは秘匿されます。 |
1294 | 642 | 自分自身が同じサブルーチンを呼んだ場合でさえそうです--個々の呼び出しは |
1295 | 643 | それぞれのコピーを所有します。 |
1296 | 644 | X<local> |
1297 | 645 | |
1298 | 646 | =begin original |
1299 | 647 | |
1300 | 648 | This doesn't mean that a C<my> variable declared in a statically |
1301 | 649 | enclosing lexical scope would be invisible. Only dynamic scopes |
1302 | 650 | are cut off. For example, the C<bumpx()> function below has access |
1303 | 651 | to the lexical $x variable because both the C<my> and the C<sub> |
1304 | 652 | occurred at the same scope, presumably file scope. |
1305 | 653 | |
1306 | 654 | =end original |
1307 | 655 | |
1308 | 656 | このことは静的に閉じているレキシカルスコープで宣言されている |
1309 | 657 | C<my> 変数が見えなくなるということは意味しません。 |
1310 | 658 | 動的変数だけが切り取られます。 |
1311 | 659 | 例を挙げると、以下の C<bumpx()> はレキシカル変数 $x にアクセスします; |
1312 | 660 | なぜなら、C<my> と C<sub> の両方が同じスコープに現れているからです。 |
1313 | 661 | |
1314 | 662 | my $x = 10; |
1315 | sub bumpx { $x++ } | |
663 | sub bumpx { $x++ } | |
1316 | 664 | |
1317 | 665 | =begin original |
1318 | 666 | |
1319 | 667 | An C<eval()>, however, can see lexical variables of the scope it is |
1320 | 668 | being evaluated in, so long as the names aren't hidden by declarations within |
1321 | 669 | the C<eval()> itself. See L<perlref>. |
1322 | 670 | X<eval, scope of> |
1323 | 671 | |
1324 | 672 | =end original |
1325 | 673 | |
1326 | 674 | しかしながら C<eval()> は、C<eval()> 自身の内側にある宣言によって隠されない |
1327 | 675 | 名前の寿命と同じ長さを持つスコープのレキシカル変数を見ることができます。 |
1328 | 676 | L<perlref> を参照してください。 |
1329 | 677 | X<eval, scope of> |
1330 | 678 | |
1331 | 679 | =begin original |
1332 | 680 | |
1333 | 681 | The parameter list to my() may be assigned to if desired, which allows you |
1334 | 682 | to initialize your variables. (If no initializer is given for a |
1335 | 683 | particular variable, it is created with the undefined value.) Commonly |
1336 | 684 | this is used to name input parameters to a subroutine. Examples: |
1337 | 685 | |
1338 | 686 | =end original |
1339 | 687 | |
1340 | 688 | my() に対するパラーメータリストには、お望みとあらば変数を初期化するための |
1341 | 689 | 代入を行うことができます。 |
1342 | 690 | (変数に対して初期値が与えられなければ、その変数は未定義値を使って |
1343 | 691 | 生成されます。) |
1344 | 692 | 一般的にこの機能はサブルーチンに対する入力パラメータに名前を付けるために |
1345 | 693 | 使われています。 |
1346 | 694 | 例: |
1347 | 695 | |
1348 | $arg = "fred"; | |
696 | $arg = "fred"; # "global" variable | |
1349 | 697 | $n = cube_root(27); |
1350 | 698 | print "$arg thinks the root is $n\n"; |
1351 | | |
699 | fred thinks the root is 3 | |
1352 | 700 | |
1353 | 701 | sub cube_root { |
1354 | ||
702 | my $arg = shift; # name doesn't matter | |
1355 | ||
703 | $arg **= 1/3; | |
1356 | ||
704 | return $arg; | |
1357 | 705 | } |
1358 | 706 | |
1359 | 707 | =begin original |
1360 | 708 | |
1361 | 709 | The C<my> is simply a modifier on something you might assign to. So when |
1362 | 710 | you do assign to variables in its argument list, C<my> doesn't |
1363 | 711 | change whether those variables are viewed as a scalar or an array. So |
1364 | 712 | |
1365 | 713 | =end original |
1366 | 714 | |
1367 | 715 | C<my> は、あなたが代入を行いたいと考えている何かに対する修飾子です。 |
1368 | 716 | ですから、引数リストの中にある変数に対して代入を行うときには C<my> は |
1369 | 717 | それらの変数がスカラとして見えているのか配列として見えているかという |
1370 | 718 | 状態を変えることはありません。ですから、 |
1371 | 719 | |
1372 | my ($foo) = <STDIN>; | |
720 | my ($foo) = <STDIN>; # WRONG? | |
1373 | 721 | my @FOO = <STDIN>; |
1374 | 722 | |
1375 | 723 | =begin original |
1376 | 724 | |
1377 | 725 | both supply a list context to the right-hand side, while |
1378 | 726 | |
1379 | 727 | =end original |
1380 | 728 | |
1381 | 729 | これらの両方ともが右辺をリストコンテキストにするのに対して、 |
1382 | 730 | |
1383 | 731 | my $foo = <STDIN>; |
1384 | 732 | |
1385 | 733 | =begin original |
1386 | 734 | |
1387 | 735 | supplies a scalar context. But the following declares only one variable: |
1388 | 736 | |
1389 | 737 | =end original |
1390 | 738 | |
1391 | 739 | これはスカラコンテキストを与えます。しかし、以下のような宣言を |
1392 | 740 | 行った場合、一つの変数だけが有効です: |
1393 | 741 | |
1394 | my $foo, $bar = 1; | |
742 | my $foo, $bar = 1; # WRONG | |
1395 | 743 | |
1396 | 744 | =begin original |
1397 | 745 | |
1398 | 746 | That has the same effect as |
1399 | 747 | |
1400 | 748 | =end original |
1401 | 749 | |
1402 | 750 | これは以下のように書いたのと同じことになります |
1403 | 751 | |
1404 | 752 | my $foo; |
1405 | 753 | $bar = 1; |
1406 | 754 | |
1407 | 755 | =begin original |
1408 | 756 | |
1409 | 757 | The declared variable is not introduced (is not visible) until after |
1410 | 758 | the current statement. Thus, |
1411 | 759 | |
1412 | 760 | =end original |
1413 | 761 | |
1414 | 762 | 宣言された変数は、その文が終了するまでは導入されません(不可視の |
1415 | 763 | 状態です)。 |
1416 | 764 | したがって、 |
1417 | 765 | |
1418 | 766 | my $x = $x; |
1419 | 767 | |
1420 | 768 | =begin original |
1421 | 769 | |
1422 | 770 | can be used to initialize a new $x with the value of the old $x, and |
1423 | 771 | the expression |
1424 | 772 | |
1425 | 773 | =end original |
1426 | 774 | |
1427 | 775 | これは古い $x の値を使って新しい $x を初期化するのに使うことができます; |
1428 | 776 | そして |
1429 | 777 | |
1430 | 778 | my $x = 123 and $x == 123 |
1431 | 779 | |
1432 | 780 | =begin original |
1433 | 781 | |
1434 | 782 | is false unless the old $x happened to have the value C<123>. |
1435 | 783 | |
1436 | 784 | =end original |
1437 | 785 | |
1438 | 786 | これは古い $x の値がたまたま C<123> でない限り、新しい $x には偽が |
1439 | 787 | 設定されます。 |
1440 | 788 | |
1441 | 789 | =begin original |
1442 | 790 | |
1443 | 791 | Lexical scopes of control structures are not bounded precisely by the |
1444 | 792 | braces that delimit their controlled blocks; control expressions are |
1445 | 793 | part of that scope, too. Thus in the loop |
1446 | 794 | |
1447 | 795 | =end original |
1448 | 796 | |
1449 | 797 | 制御構文のレキシカルスコープは、その制御ブロックを区切る中かっこによって |
1450 | 798 | 厳格に束縛されることはありません; |
1451 | 799 | 制御式もまた、スコープの一部です。 |
1452 | 800 | ですから以下のループでは |
1453 | 801 | |
1454 | 802 | while (my $line = <>) { |
1455 | 803 | $line = lc $line; |
1456 | 804 | } continue { |
1457 | 805 | print $line; |
1458 | 806 | } |
1459 | 807 | |
1460 | 808 | =begin original |
1461 | 809 | |
1462 | 810 | the scope of $line extends from its declaration throughout the rest of |
1463 | 811 | the loop construct (including the C<continue> clause), but not beyond |
1464 | 812 | it. Similarly, in the conditional |
1465 | 813 | |
1466 | 814 | =end original |
1467 | 815 | |
1468 | 816 | $line のスコープはその宣言から(C<continue>ブロックを含む)ループ構造の残りの |
1469 | 817 | 部分まで拡張されますが、それを越えることはありません。 |
1470 | 818 | 同様に、以下の条件文では |
1471 | 819 | |
1472 | 820 | if ((my $answer = <STDIN>) =~ /^yes$/i) { |
1473 | 821 | user_agrees(); |
1474 | 822 | } elsif ($answer =~ /^no$/i) { |
1475 | 823 | user_disagrees(); |
1476 | 824 | } else { |
1477 | ||
825 | chomp $answer; | |
1478 | 826 | die "'$answer' is neither 'yes' nor 'no'"; |
1479 | 827 | } |
1480 | 828 | |
1481 | 829 | =begin original |
1482 | 830 | |
1483 | 831 | the scope of $answer extends from its declaration through the rest |
1484 | of that conditional, including any C<elsif> and C<else> clauses, | |
832 | of that conditional, including any C<elsif> and C<else> clauses, | |
1485 | 833 | but not beyond it. See L<perlsyn/"Simple Statements"> for information |
1486 | 834 | on the scope of variables in statements with modifiers. |
1487 | 835 | |
1488 | 836 | =end original |
1489 | 837 | |
1490 | 838 | $answer のスコープはその宣言から条件文の |
1491 | 839 | C<elsif> と C<else> ブロックを含む残りの部分まで拡張されますが、 |
1492 | 840 | それを越えることはありません。 |
1493 | 841 | 修飾子付きの文での変数のスコープに関する情報については |
1494 | 842 | L<perlsyn/"Simple Statements"> を参照してください。 |
1495 | 843 | |
1496 | 844 | =begin original |
1497 | 845 | |
1498 | 846 | The C<foreach> loop defaults to scoping its index variable dynamically |
1499 | 847 | in the manner of C<local>. However, if the index variable is |
1500 | 848 | prefixed with the keyword C<my>, or if there is already a lexical |
1501 | 849 | by that name in scope, then a new lexical is created instead. Thus |
1502 | 850 | in the loop |
1503 | 851 | X<foreach> X<for> |
1504 | 852 | |
1505 | 853 | =end original |
1506 | 854 | |
1507 | 855 | C<foreach> はその添え字変数に対するスコープをデフォルトでは |
1508 | 856 | C<local> のやり方で動的なものにしています。 |
1509 | 857 | しかしながら、添え字変数に C<my> というキーワードが前置されていた場合、 |
1510 | 858 | または現在のスコープでその名前がすでにレキシカルである場合には、 |
1511 | 859 | 新しいレキシカル変数が代わりに作られます。 |
1512 | 860 | ですから以下のループでは: |
1513 | 861 | X<foreach> X<for> |
1514 | 862 | |
1515 | 863 | for my $i (1, 2, 3) { |
1516 | 864 | some_function(); |
1517 | 865 | } |
1518 | 866 | |
1519 | 867 | =begin original |
1520 | 868 | |
1521 | 869 | the scope of $i extends to the end of the loop, but not beyond it, |
1522 | 870 | rendering the value of $i inaccessible within C<some_function()>. |
1523 | 871 | X<foreach> X<for> |
1524 | 872 | |
1525 | 873 | =end original |
1526 | 874 | |
1527 | 875 | $i のスコープはループの終端まで拡張されますが、それを越えることはないので、 |
1528 | 876 | $i の値は C<some_function()> の中では参照することができなくなります。 |
1529 | 877 | X<foreach> X<for> |
1530 | 878 | |
1531 | 879 | =begin original |
1532 | 880 | |
1533 | 881 | Some users may wish to encourage the use of lexically scoped variables. |
1534 | 882 | As an aid to catching implicit uses to package variables, |
1535 | 883 | which are always global, if you say |
1536 | 884 | |
1537 | 885 | =end original |
1538 | 886 | |
1539 | 887 | ユーザーの一部には、レキシカルなスコープの変数の使用を奨励することを |
1540 | 888 | 望んでいる人もいるでしょう。 |
1541 | 889 | 常にグローバルであるパッケージ変数に対する暗黙の使用を捕捉することを |
1542 | 890 | 助けるものとして、 |
1543 | 891 | |
1544 | 892 | use strict 'vars'; |
1545 | 893 | |
1546 | 894 | =begin original |
1547 | 895 | |
1548 | 896 | then any variable mentioned from there to the end of the enclosing |
1549 | 897 | block must either refer to a lexical variable, be predeclared via |
1550 | 898 | C<our> or C<use vars>, or else must be fully qualified with the package name. |
1551 | 899 | A compilation error results otherwise. An inner block may countermand |
1552 | 900 | this with C<no strict 'vars'>. |
1553 | 901 | |
1554 | 902 | =end original |
1555 | 903 | |
1556 | 904 | のようにすると、この文からそれを囲むブロックの終端までの間の変数の参照は、 |
1557 | 905 | レキシカル変数に対する参照か、C<our> または C<use vars> による |
1558 | 906 | 事前宣言か、さもなければ完全なパッケージ名で修飾した |
1559 | 907 | 名前でなければなりません。 |
1560 | 908 | それ以外のものがあるとコンパイルエラーが発生します。 |
1561 | 909 | これの対象となっているブロックの内側にあるブロックで |
1562 | 910 | C<no strict 'vars'> とすると(内側のブロック中では)この制限を |
1563 | 911 | 取り消すことができます。 |
1564 | 912 | |
1565 | 913 | =begin original |
1566 | 914 | |
1567 | 915 | A C<my> has both a compile-time and a run-time effect. At compile |
1568 | 916 | time, the compiler takes notice of it. The principal usefulness |
1569 | 917 | of this is to quiet C<use strict 'vars'>, but it is also essential |
1570 | 918 | for generation of closures as detailed in L<perlref>. Actual |
1571 | 919 | initialization is delayed until run time, though, so it gets executed |
1572 | 920 | at the appropriate time, such as each time through a loop, for |
1573 | 921 | example. |
1574 | 922 | |
1575 | 923 | =end original |
1576 | 924 | |
1577 | 925 | C<my> はコンパイル時の効果と実行時の効果の両方を持っています。 |
1578 | 926 | コンパイル時においては、コンパイラはその変数を認識します。 |
1579 | 927 | これの基本的な実用性は C<use strict 'vars'> を黙らせるということですが、 |
1580 | 928 | L<perlref> で詳細を記述しているクロージャの作成にも有用です。 |
1581 | 929 | 実際の初期化は実行時まで遅らされ、そのためたとえばループを通る度に |
1582 | 930 | 適切に実行されるのです。 |
1583 | 931 | |
1584 | 932 | =begin original |
1585 | 933 | |
1586 | 934 | Variables declared with C<my> are not part of any package and are therefore |
1587 | 935 | never fully qualified with the package name. In particular, you're not |
1588 | 936 | allowed to try to make a package variable (or other global) lexical: |
1589 | 937 | |
1590 | 938 | =end original |
1591 | 939 | |
1592 | 940 | C<my> によって宣言された変数はどのパッケージの一部でもなく、 |
1593 | 941 | そのためパッケージ名を使って修飾されることは決してありません。 |
1594 | 942 | 特に、パッケージ変数(もしくはその他のグローバルな変数)を |
1595 | 943 | レキシカルにしようとすることは許されていません。 |
1596 | 944 | |
1597 | my $pack::var; | |
945 | my $pack::var; # ERROR! Illegal syntax | |
1598 | 946 | |
1599 | 947 | =begin original |
1600 | 948 | |
1601 | In fact, a package or global variable | |
949 | In fact, a dynamic variable (also known as package or global variables) | |
1602 | 950 | are still accessible using the fully qualified C<::> notation even while a |
1603 | 951 | lexical of the same name is also visible: |
1604 | 952 | |
1605 | 953 | =end original |
1606 | 954 | |
1607 | 実際のところ、パッケージ | |
955 | 実際のところ、動的変数(パッケージ変数やグローバル変数として | |
1608 | 同じ名前を持ったレキシカル | |
956 | 知られているもの)は、同じ名前を持ったレキシカルが可視な状態であったとしても、 | |
1609 | 957 | C<::> 記法を使った(名前の)完全な修飾を行うことによって |
1610 | まだアクセスで | |
958 | まだアクセス可能なのです: | |
1611 | 959 | |
1612 | 960 | package main; |
1613 | o | |
961 | local $x = 10; | |
1614 | my $x = 20; | |
962 | my $x = 20; | |
1615 | 963 | print "$x and $::x\n"; |
1616 | 964 | |
1617 | 965 | =begin original |
1618 | 966 | |
1619 | 967 | That will print out C<20> and C<10>. |
1620 | 968 | |
1621 | 969 | =end original |
1622 | 970 | |
1623 | 971 | この例は C<20> と C<10> を出力します。 |
1624 | 972 | |
1625 | 973 | =begin original |
1626 | 974 | |
1627 | 975 | You may declare C<my> variables at the outermost scope of a file |
1628 | 976 | to hide any such identifiers from the world outside that file. This |
1629 | 977 | is similar in spirit to C's static variables when they are used at |
1630 | 978 | the file level. To do this with a subroutine requires the use of |
1631 | 979 | a closure (an anonymous function that accesses enclosing lexicals). |
1632 | 980 | If you want to create a private subroutine that cannot be called |
1633 | 981 | from outside that block, it can declare a lexical variable containing |
1634 | 982 | an anonymous sub reference: |
1635 | 983 | |
1636 | 984 | =end original |
1637 | 985 | |
1638 | 986 | このファイルの外側の世界からこのような識別子を隠すために、C<my> 変数を |
1639 | 987 | ファイルの最も外側のスコープで宣言することができます。 |
1640 | 988 | これは、概念としては C でのファイルレベルの static 変数と似ています。 |
1641 | 989 | これをサブルーチンの内側で行うには、 |
1642 | 990 | クロージャ(レキシカルアクセスを伴った無名関数)を使います。 |
1643 | 991 | ブロックで外側のブロックから呼び出すことのできないようなプライベートな |
1644 | 992 | サブルーチンを作りたいなら、無名サブルーチンのリファレンスを |
1645 | 993 | 保持するレキシカル変数を宣言することができます: |
1646 | 994 | |
1647 | 995 | my $secret_version = '1.001-beta'; |
1648 | 996 | my $secret_sub = sub { print $secret_version }; |
1649 | $secret_sub | |
997 | &$secret_sub(); | |
1650 | 998 | |
1651 | 999 | =begin original |
1652 | 1000 | |
1653 | 1001 | As long as the reference is never returned by any function within the |
1654 | 1002 | module, no outside module can see the subroutine, because its name is not in |
1655 | 1003 | any package's symbol table. Remember that it's not I<REALLY> called |
1656 | 1004 | C<$some_pack::secret_version> or anything; it's just $secret_version, |
1657 | 1005 | unqualified and unqualifiable. |
1658 | 1006 | |
1659 | 1007 | =end original |
1660 | 1008 | |
1661 | 1009 | このリファレンスが決して、モジュールの内側にあるどんな関数からも |
1662 | 1010 | 返されることがないのと同様に、外側のモジュールはサブルーチンを |
1663 | 1011 | 見ることができません; なぜなら、その名前はどのパッケージのシンボル |
1664 | 1012 | テーブルにも存在していないからです。 |
1665 | 1013 | C<$some_pack::secret_version> などの手段を使って呼び出すことは |
1666 | 1014 | B<できない> のだということを思い出してください; これは単なる |
1667 | 1015 | $secret_version であって、修飾されることはなく修飾することはできません。 |
1668 | 1016 | |
1669 | 1017 | =begin original |
1670 | 1018 | |
1671 | 1019 | This does not work with object methods, however; all object methods |
1672 | 1020 | have to be in the symbol table of some package to be found. See |
1673 | 1021 | L<perlref/"Function Templates"> for something of a work-around to |
1674 | 1022 | this. |
1675 | 1023 | |
1676 | 1024 | =end original |
1677 | 1025 | |
1678 | 1026 | しかしこれはオブジェクトメソッドと共に動作させることはできません; 全ての |
1679 | 1027 | オブジェクトメソッドは、発見可能な何らかのパッケージのシンボルテーブルに |
1680 | 1028 | 存在している必要があります。 |
1681 | 1029 | これを回避する方法については L<perlref/"Function Templates"> を |
1682 | 1030 | 参照してください。 |
1683 | 1031 | |
1684 | 1032 | =head2 Persistent Private Variables |
1685 | X<state> X<state variable> X<static> X<variable, persistent> | |
1033 | X<state> X<state variable> X<static> X<variable, persistent> X<variable, static> X<closure> | |
1686 | X<variable, static> X<closure> | |
1687 | 1034 | |
1688 | 1035 | (永続的なプライベート変数) |
1689 | 1036 | |
1690 | 1037 | =begin original |
1691 | 1038 | |
1692 | 1039 | There are two ways to build persistent private variables in Perl 5.10. |
1693 | First, you can simply use the C<state> feature. | |
1040 | First, you can simply use the C<state> feature. Or, you can use closures, | |
1694 | 1041 | if you want to stay compatible with releases older than 5.10. |
1695 | 1042 | |
1696 | 1043 | =end original |
1697 | 1044 | |
1698 | 1045 | Perl 5.10 で永続的プライベート変数を構築するには二つの方法があります。 |
1699 | 1046 | 一つ目は単に C<state> 機能を使うことです。 |
1700 | 1047 | あるいは、5.10 よりも古いリリースとの互換性を維持したいなら、クロージャを |
1701 | 1048 | 使うことです。 |
1702 | 1049 | |
1703 | 1050 | =head3 Persistent variables via state() |
1704 | 1051 | |
1705 | 1052 | (state() による永続変数) |
1706 | 1053 | |
1707 | 1054 | =begin original |
1708 | 1055 | |
1709 | Beginning with Perl 5. | |
1056 | Beginning with Perl 5.9.4, you can declare variables with the C<state> | |
1710 | 1057 | keyword in place of C<my>. For that to work, though, you must have |
1711 | 1058 | enabled that feature beforehand, either by using the C<feature> pragma, or |
1712 | 1059 | by using C<-E> on one-liners (see L<feature>). Beginning with Perl 5.16, |
1713 | 1060 | the C<CORE::state> form does not require the |
1714 | 1061 | C<feature> pragma. |
1715 | 1062 | |
1063 | ||
1716 | 1064 | =end original |
1717 | 1065 | |
1718 | perl 5. | |
1066 | perl 5.9.4 から、C<my> の代わりに C<state> キーワードを使って変数を | |
1719 | 1067 | 宣言できます。 |
1720 | 1068 | しかし、これを働かせるには、C<feature> プラグマを使うか、一行野郎では |
1721 | 1069 | C<-E> を使うことで予めこの機能を有効にしなければなりません |
1722 | 1070 | (L<feature> を参照してください)。 |
1723 | 1071 | Perl 5.16 から、C<CORE::state> 形式は C<feature> プラグマが |
1724 | 1072 | 不要になりました。 |
1725 | 1073 | |
1726 | 1074 | =begin original |
1727 | 1075 | |
1728 | The C<state> keyword creates a lexical variable (following the same scoping | |
1729 | rules as C<my>) that persists from one subroutine call to the next. If a | |
1730 | state variable resides inside an anonymous subroutine, then each copy of | |
1731 | the subroutine has its own copy of the state variable. However, the value | |
1732 | of the state variable will still persist between calls to the same copy of | |
1733 | the anonymous subroutine. (Don't forget that C<sub { ... }> creates a new | |
1734 | subroutine each time it is executed.) | |
1735 | ||
1736 | =end original | |
1737 | ||
1738 | The C<state> キーワードは、あるサブルーチン呼び出しから次の呼び出しまで永続する | |
1739 | (C<my> と同じスコープ規則に従う)レキシカル変数を作成します。 | |
1740 | state 変数が無名サブルーチンの中にある場合は、サブルーチンのそれぞれのコピーは | |
1741 | 独自の state 変数を持ちます。 | |
1742 | しかし、state 変数の値は同じ無名サブルーチンの呼び出しの間では永続します。 | |
1743 | (C<sub { ... }> は、実行されるごとに新しいサブルーチンを作ることを | |
1744 | 忘れないでください。) | |
1745 | ||
1746 | =begin original | |
1747 | ||
1748 | 1076 | For example, the following code maintains a private counter, incremented |
1749 | 1077 | each time the gimme_another() function is called: |
1750 | 1078 | |
1751 | 1079 | =end original |
1752 | 1080 | |
1753 | 1081 | 例えば、以下のコードはプライベートなカウンタを管理し、gimme_another() 関数が |
1754 | 1082 | 呼び出されるたびにカウンタを増加させます: |
1755 | 1083 | |
1756 | 1084 | use feature 'state'; |
1757 | 1085 | sub gimme_another { state $x; return ++$x } |
1758 | 1086 | |
1759 | 1087 | =begin original |
1760 | 1088 | |
1761 | And this example uses anonymous subroutines to create separate counters: | |
1762 | ||
1763 | =end original | |
1764 | ||
1765 | そしてこの例は、別々のカウンタを作るために無名サブルーチンを使います: | |
1766 | ||
1767 | use feature 'state'; | |
1768 | sub create_counter { | |
1769 | return sub { state $x; return ++$x } | |
1770 | } | |
1771 | ||
1772 | =begin original | |
1773 | ||
1774 | 1089 | Also, since C<$x> is lexical, it can't be reached or modified by any Perl |
1775 | 1090 | code outside. |
1776 | 1091 | |
1777 | 1092 | =end original |
1778 | 1093 | |
1779 | 1094 | また、C<$x> はレキシカルなので、その外側の Perl コードからアクセスすることは |
1780 | 1095 | できません。 |
1781 | 1096 | |
1782 | 1097 | =begin original |
1783 | 1098 | |
1784 | When combined with variable declaration, simple assignment to C<state> | |
1099 | When combined with variable declaration, simple scalar assignment to C<state> | |
1785 | 1100 | variables (as in C<state $x = 42>) is executed only the first time. When such |
1786 | 1101 | statements are evaluated subsequent times, the assignment is ignored. The |
1787 | behavior of assignment to | |
1102 | behavior of this sort of assignment to non-scalar variables is undefined. | |
1788 | of the assignment involves any parentheses is currently undefined. | |
1789 | 1103 | |
1790 | 1104 | =end original |
1791 | 1105 | |
1792 | 1106 | 変数宣言と結び付けられたとき、(C<state $x = 42> のような)C<state> 変数への |
1793 | 単純な代入は一度目だけ実行されます。 | |
1107 | 単純なスカラ代入は一度目だけ実行されます。 | |
1794 | 1108 | その後再びこの文が評価されても、代入は無視されます。 |
1795 | ||
1109 | 非スカラ変数へのこの種の代入の振る舞いは未定義です。 | |
1796 | 振る舞いは現在のところ未定義です。 | |
1797 | 1110 | |
1798 | 1111 | =head3 Persistent variables with closures |
1799 | 1112 | |
1800 | 1113 | (クロージャによる永続変数) |
1801 | 1114 | |
1802 | 1115 | =begin original |
1803 | 1116 | |
1804 | 1117 | Just because a lexical variable is lexically (also called statically) |
1805 | 1118 | scoped to its enclosing block, C<eval>, or C<do> FILE, this doesn't mean that |
1806 | 1119 | within a function it works like a C static. It normally works more |
1807 | like a C auto, but with implicit garbage collection. | |
1120 | like a C auto, but with implicit garbage collection. | |
1808 | 1121 | |
1809 | 1122 | =end original |
1810 | 1123 | |
1811 | 1124 | レキシカル変数はそれを囲むブロック、C<eval>、C<do> FILE に属する |
1812 | 1125 | レキシカル(もしくは静的)スコープに属します; このことは C の static と同様に |
1813 | 1126 | 動作するということを意味しません。 |
1814 | 1127 | 通常は C の auto と同じように動作しますが、暗黙のガベージコレクションを |
1815 | 1128 | 伴っています。 |
1816 | 1129 | |
1817 | 1130 | =begin original |
1818 | 1131 | |
1819 | 1132 | Unlike local variables in C or C++, Perl's lexical variables don't |
1820 | 1133 | necessarily get recycled just because their scope has exited. |
1821 | 1134 | If something more permanent is still aware of the lexical, it will |
1822 | 1135 | stick around. So long as something else references a lexical, that |
1823 | 1136 | lexical won't be freed--which is as it should be. You wouldn't want |
1824 | 1137 | memory being free until you were done using it, or kept around once you |
1825 | 1138 | were done. Automatic garbage collection takes care of this for you. |
1826 | 1139 | |
1827 | 1140 | =end original |
1828 | 1141 | |
1829 | 1142 | C や C++ におけるローカル変数とは異なり、Perl のレキシカル変数は |
1830 | 1143 | 単にそのスコープから抜けたからという理由で、必ずしもリサイクルされません。 |
1831 | 1144 | 何かもっと永続的なものがそのレキシカル変数に関してまだ気付いていれば、 |
1832 | 1145 | それは留まるでしょう。 |
1833 | 1146 | 何か他のものがレキシカル変数を参照していれば、そのレキシカル変数は |
1834 | 1147 | 解放されません -- そしてそうあるべきです。 |
1835 | 1148 | あなたはそれを使いおわるまではメモリを解放したいとは思わないでしょうし、 |
1836 | 1149 | 使い終わったものを保持し続けたいとも思わないでしょう。 |
1837 | 1150 | あなたのために自動ガベージコレクションがこれを行います。 |
1838 | 1151 | |
1839 | 1152 | =begin original |
1840 | 1153 | |
1841 | 1154 | This means that you can pass back or save away references to lexical |
1842 | 1155 | variables, whereas to return a pointer to a C auto is a grave error. |
1843 | 1156 | It also gives us a way to simulate C's function statics. Here's a |
1844 | 1157 | mechanism for giving a function private variables with both lexical |
1845 | 1158 | scoping and a static lifetime. If you do want to create something like |
1846 | 1159 | C's static variables, just enclose the whole function in an extra block, |
1847 | 1160 | and put the static variable outside the function but in the block. |
1848 | 1161 | |
1849 | 1162 | =end original |
1850 | 1163 | |
1851 | 1164 | これはつまり、レキシカル変数に対するリファレンスを返したり保存したり |
1852 | 1165 | することができるということです; 一方 C の auto 変数に対するポインタを |
1853 | 1166 | 返すことはエラーとなります。 |
1854 | 1167 | レキシカル変数はまた、C の関数に static な変数のシミュレートも行います。 |
1855 | 1168 | 以下の例はレキシカルスコープを持つと同時に static な寿命を持つ関数に |
1856 | 1169 | プライベートな変数です。 |
1857 | 1170 | C の static 変数のようなものを作りたいというのであれば、 |
1858 | 1171 | 関数全体をさらにブロックで囲んでやり、static 変数をブロックの内側で、 |
1859 | 1172 | かつ関数の外側の場所においてやります。 |
1860 | 1173 | |
1861 | 1174 | { |
1862 | ||
1175 | my $secret_val = 0; | |
1863 | ||
1176 | sub gimme_another { | |
1864 | ||
1177 | return ++$secret_val; | |
1865 | ||
1178 | } | |
1866 | 1179 | } |
1867 | 1180 | # $secret_val now becomes unreachable by the outside |
1868 | 1181 | # world, but retains its value between calls to gimme_another |
1869 | 1182 | |
1870 | 1183 | =begin original |
1871 | 1184 | |
1872 | 1185 | If this function is being sourced in from a separate file |
1873 | 1186 | via C<require> or C<use>, then this is probably just fine. If it's |
1874 | 1187 | all in the main program, you'll need to arrange for the C<my> |
1875 | 1188 | to be executed early, either by putting the whole block above |
1876 | 1189 | your main program, or more likely, placing merely a C<BEGIN> |
1877 | 1190 | code block around it to make sure it gets executed before your program |
1878 | 1191 | starts to run: |
1879 | 1192 | |
1880 | 1193 | =end original |
1881 | 1194 | |
1882 | 1195 | この関数が C<require> や C<use> を通じて別の独立したファイルから |
1883 | 1196 | 取り込まれた場合、これはとても役に立つことでしょう。 |
1884 | 1197 | もしこれがすべてメインプログラムの中にあるのであれば、 |
1885 | 1198 | ブロック全体をメインプログラムの前に置くか、(こちらか好ましいやり方ですが) |
1886 | 1199 | プログラムが実行されるよりも前に実行されることが保証されている |
1887 | 1200 | BEGIN コードブロックの中に置くようにして、C<my> を早めに実行するように |
1888 | 1201 | 変更する必要があるでしょう: |
1889 | 1202 | |
1890 | 1203 | BEGIN { |
1891 | ||
1204 | my $secret_val = 0; | |
1892 | ||
1205 | sub gimme_another { | |
1893 | ||
1206 | return ++$secret_val; | |
1894 | ||
1207 | } | |
1895 | 1208 | } |
1896 | 1209 | |
1897 | 1210 | =begin original |
1898 | 1211 | |
1899 | 1212 | See L<perlmod/"BEGIN, UNITCHECK, CHECK, INIT and END"> about the |
1900 | 1213 | special triggered code blocks, C<BEGIN>, C<UNITCHECK>, C<CHECK>, |
1901 | 1214 | C<INIT> and C<END>. |
1902 | 1215 | |
1903 | 1216 | =end original |
1904 | 1217 | |
1905 | 1218 | 特別なトリガーコードブロックである C<BEGIN>, C<CHECK>, C<INIT>, C<END> に |
1906 | 1219 | ついては |
1907 | 1220 | L<perlmod/"BEGIN, UNITCHECK, CHECK, INIT and END"> を参照してください。 |
1908 | 1221 | |
1909 | 1222 | =begin original |
1910 | 1223 | |
1911 | 1224 | If declared at the outermost scope (the file scope), then lexicals |
1912 | 1225 | work somewhat like C's file statics. They are available to all |
1913 | 1226 | functions in that same file declared below them, but are inaccessible |
1914 | 1227 | from outside that file. This strategy is sometimes used in modules |
1915 | 1228 | to create private variables that the whole module can see. |
1916 | 1229 | |
1917 | 1230 | =end original |
1918 | 1231 | |
1919 | 1232 | 最も外側のスコープ(ファイルスコープ)で宣言されたのであれば、 |
1920 | 1233 | レキシカル変数は C のファイルに static なものと同じように振る舞います。 |
1921 | 1234 | 同じファイルの、宣言の後にある全ての関数から参照可能でありますが、 |
1922 | 1235 | そのファイルの外側から参照することはできません。 |
1923 | 1236 | この戦略はモジュールの中でモジュール全体から見える |
1924 | 1237 | プライベートな変数を作るために使われます。 |
1925 | 1238 | |
1926 | 1239 | =head2 Temporary Values via local() |
1927 | 1240 | X<local> X<scope, dynamic> X<dynamic scope> X<variable, local> |
1928 | 1241 | X<variable, temporary> |
1929 | 1242 | |
1930 | 1243 | (local() を使った一時的な値) |
1931 | 1244 | |
1932 | 1245 | =begin original |
1933 | 1246 | |
1934 | 1247 | B<WARNING>: In general, you should be using C<my> instead of C<local>, because |
1935 | 1248 | it's faster and safer. Exceptions to this include the global punctuation |
1936 | 1249 | variables, global filehandles and formats, and direct manipulation of the |
1937 | 1250 | Perl symbol table itself. C<local> is mostly used when the current value |
1938 | 1251 | of a variable must be visible to called subroutines. |
1939 | 1252 | |
1940 | 1253 | =end original |
1941 | 1254 | |
1942 | 1255 | B<警告>: 一般的には、C<local> ではなくC<my> を使うべきです; なぜなら、そちらの |
1943 | 1256 | ほうが早く、安全だからです。 |
1944 | 1257 | この例外には、グローバルな句読点変数(punctuation variable)、グローバル |
1945 | 1258 | ファイルハンドル、フォーマット、そして Perl のシンボルテーブル自身に対する |
1946 | 1259 | 直接の操作が含まれます。 |
1947 | 1260 | C<local> はほとんどの場合、変数の現在の値が呼び出されたサブルーチンに |
1948 | 1261 | 対して可視にしなければならない場合に使われます。 |
1949 | 1262 | |
1950 | 1263 | =begin original |
1951 | 1264 | |
1952 | 1265 | Synopsis: |
1953 | 1266 | |
1954 | 1267 | =end original |
1955 | 1268 | |
1956 | 1269 | 概要: |
1957 | 1270 | |
1958 | 1271 | =begin original |
1959 | 1272 | |
1960 | 1273 | # localization of values |
1961 | 1274 | |
1962 | 1275 | =end original |
1963 | 1276 | |
1964 | 1277 | # 値のローカル化 |
1965 | 1278 | |
1966 | 1279 | =begin original |
1967 | 1280 | |
1968 | local $foo; | |
1281 | local $foo; # make $foo dynamically local | |
1969 | local (@wid, %get); | |
1282 | local (@wid, %get); # make list of variables local | |
1970 | local $foo = "flurp"; | |
1283 | local $foo = "flurp"; # make $foo dynamic, and init it | |
1971 | local @oof = @bar; | |
1284 | local @oof = @bar; # make @oof dynamic, and init it | |
1972 | 1285 | |
1973 | 1286 | =end original |
1974 | 1287 | |
1975 | local $foo; | |
1288 | local $foo; # $foo を動的にローカルにする | |
1976 | local (@wid, %get); | |
1289 | local (@wid, %get); # 変数のリストをローカルにする | |
1977 | local $foo = "flurp"; | |
1290 | local $foo = "flurp"; # $foo を動的にして、初期化する | |
1978 | local @oof = @bar; | |
1291 | local @oof = @bar; # @oof を動的にして、初期化する | |
1979 | 1292 | |
1980 | 1293 | =begin original |
1981 | 1294 | |
1982 | local $hash{key} = "val"; | |
1295 | local $hash{key} = "val"; # sets a local value for this hash entry | |
1983 | delete local $hash{key}; # delete this entry for the current block | |
1296 | delete local $hash{key}; # delete this entry for the current block | |
1984 | local ($cond ? $v1 : $v2); | |
1297 | local ($cond ? $v1 : $v2); # several types of lvalues support | |
1985 | ||
1298 | # localization | |
1986 | 1299 | |
1987 | 1300 | =end original |
1988 | 1301 | |
1989 | local $hash{key} = "val"; | |
1302 | local $hash{key} = "val"; # このハッシュエントリにローカルな値を入れる | |
1990 | delete local $hash{key}; # 現在のブロックでこのエントリを削除 | |
1303 | delete local $hash{key}; # 現在のブロックでこのエントリを削除 | |
1991 | local ($cond ? $v1 : $v2); | |
1304 | local ($cond ? $v1 : $v2); # いくつかの種類の左辺値はローカル化に | |
1992 | ||
1305 | # 対応している | |
1993 | 1306 | |
1994 | 1307 | =begin original |
1995 | 1308 | |
1996 | 1309 | # localization of symbols |
1997 | 1310 | |
1998 | 1311 | =end original |
1999 | 1312 | |
2000 | 1313 | # シンボルのローカル化 |
2001 | 1314 | |
2002 | 1315 | =begin original |
2003 | 1316 | |
2004 | local *FH; | |
1317 | local *FH; # localize $FH, @FH, %FH, &FH ... | |
2005 | local *merlyn = *randal; | |
1318 | local *merlyn = *randal; # now $merlyn is really $randal, plus | |
2006 | # @merlyn is really @randal, etc | |
1319 | # @merlyn is really @randal, etc | |
2007 | local *merlyn = 'randal'; | |
1320 | local *merlyn = 'randal'; # SAME THING: promote 'randal' to *randal | |
2008 | local *merlyn = \$randal; # just alias $merlyn, not @merlyn etc | |
1321 | local *merlyn = \$randal; # just alias $merlyn, not @merlyn etc | |
2009 | 1322 | |
2010 | 1323 | =end original |
2011 | 1324 | |
2012 | local *FH; | |
1325 | local *FH; # $FH, @FH, %FH, &FH ... をローカル化 | |
2013 | local *merlyn = *randal; | |
1326 | local *merlyn = *randal; # ここで $merlyn は実際には $randal、さらに | |
2014 | # @merlyn は実際には @randal、など | |
1327 | # @merlyn は実際には @randal、など | |
2015 | local *merlyn = 'randal'; | |
1328 | local *merlyn = 'randal'; # 「同様」: 'randal' を *randal にする | |
2016 | local *merlyn = \$randal; # 単に $merlyn の別名で @merlyn などではない | |
1329 | local *merlyn = \$randal; # 単に $merlyn の別名で @merlyn などではない | |
2017 | 1330 | |
2018 | 1331 | =begin original |
2019 | 1332 | |
2020 | 1333 | A C<local> modifies its listed variables to be "local" to the |
2021 | 1334 | enclosing block, C<eval>, or C<do FILE>--and to I<any subroutine |
2022 | 1335 | called from within that block>. A C<local> just gives temporary |
2023 | 1336 | values to global (meaning package) variables. It does I<not> create |
2024 | 1337 | a local variable. This is known as dynamic scoping. Lexical scoping |
2025 | 1338 | is done with C<my>, which works more like C's auto declarations. |
2026 | 1339 | |
2027 | 1340 | =end original |
2028 | 1341 | |
2029 | 1342 | C<local> は、引数に取ったそのリスト中の変数を(local() を囲む)ブロック |
2030 | 1343 | (あるいは、サブルーチン, C<eval>、C<do FILE>)と、B<そのブロック中で |
2031 | 1344 | 呼び出された全てのもの> にローカルなものにします。 |
2032 | 1345 | C<local> は単にグローバル変数(やパッケージ変数)に一時的な値を与えるだけです。 |
2033 | 1346 | これはローカル変数を I<作りません>。 |
2034 | 1347 | これは動的スコープとして知られています。 |
2035 | 1348 | レキシカルスコープは C<my> を使って行われ、これは C の 自動変数宣言のように |
2036 | 1349 | 働きます。 |
2037 | 1350 | |
2038 | 1351 | =begin original |
2039 | 1352 | |
2040 | 1353 | Some types of lvalues can be localized as well: hash and array elements |
2041 | 1354 | and slices, conditionals (provided that their result is always |
2042 | 1355 | localizable), and symbolic references. As for simple variables, this |
2043 | 1356 | creates new, dynamically scoped values. |
2044 | 1357 | |
2045 | 1358 | =end original |
2046 | 1359 | |
2047 | 1360 | いくつかの種類の左辺値もローカル化できます: ハッシュと配列の要素とスライス、 |
2048 | 1361 | 条件文(その結果が常にローカル化可能なもの)、シンボリックリファレンス。 |
2049 | 1362 | 単純変数に関しては、これは新しく、動的スコープを持つ値を作ります。 |
2050 | 1363 | |
2051 | 1364 | =begin original |
2052 | 1365 | |
2053 | 1366 | If more than one variable or expression is given to C<local>, they must be |
2054 | 1367 | placed in parentheses. This operator works |
2055 | 1368 | by saving the current values of those variables in its argument list on a |
2056 | 1369 | hidden stack and restoring them upon exiting the block, subroutine, or |
2057 | 1370 | eval. This means that called subroutines can also reference the local |
2058 | 1371 | variable, but not the global one. The argument list may be assigned to if |
2059 | 1372 | desired, which allows you to initialize your local variables. (If no |
2060 | 1373 | initializer is given for a particular variable, it is created with an |
2061 | 1374 | undefined value.) |
2062 | 1375 | |
2063 | 1376 | =end original |
2064 | 1377 | |
2065 | 1378 | C<local> に対して二つ以上の変数や表現を与えるのならば、それらの変数は括弧で |
2066 | 1379 | くくっておかなければなりません。 |
2067 | 1380 | この演算子は引数リストにある変数のカレントの値を隠れたスタックに保存し、 |
2068 | 1381 | ブロックやサブルーチン、eval から抜けるときにその値を復帰することによって |
2069 | 1382 | 動作しています。 |
2070 | 1383 | これはつまり、呼び出されたサブルーチンからもローカル変数を |
2071 | 1384 | 参照することができるけれども、グローバルなものを見ることは |
2072 | 1385 | できないということです。 |
2073 | 1386 | 引数リストには、好みに応じてリスト中のローカル変数を初期化するための |
2074 | 1387 | 代入を行うことができます。 |
2075 | 1388 | (ある特定の変数に対して初期値が与えられなかった場合には、その変数は未定義値を |
2076 | 1389 | 伴って生成されます。) |
2077 | 1390 | |
2078 | 1391 | =begin original |
2079 | 1392 | |
2080 | 1393 | Because C<local> is a run-time operator, it gets executed each time |
2081 | 1394 | through a loop. Consequently, it's more efficient to localize your |
2082 | 1395 | variables outside the loop. |
2083 | 1396 | |
2084 | 1397 | =end original |
2085 | 1398 | |
2086 | 1399 | C<local> は実行時演算子なので、ループで通る度に実行されます。 |
2087 | 1400 | 現在の Perl はメモリの使用に関して改善されていますが、 |
2088 | 1401 | 結果として、ループの外側で変数を宣言したほうがより効率が良いのです。 |
2089 | 1402 | |
2090 | 1403 | =head3 Grammatical note on local() |
2091 | 1404 | X<local, context> |
2092 | 1405 | |
2093 | 1406 | (local() の文法的注意) |
2094 | 1407 | |
2095 | 1408 | =begin original |
2096 | 1409 | |
2097 | 1410 | A C<local> is simply a modifier on an lvalue expression. When you assign to |
2098 | a C<local>ized variable, the C<local> doesn't change whether its list is | |
1411 | a C<local>ized variable, the C<local> doesn't change whether its list is viewed | |
2099 | ||
1412 | as a scalar or an array. So | |
2100 | 1413 | |
2101 | 1414 | =end original |
2102 | 1415 | |
2103 | 1416 | C<local> は左辺値に対する単なる修飾子です。 |
2104 | 1417 | 局所化された変数に代入を行ったとき、C<local> はそのリストがスカラとして |
2105 | 1418 | 見えているのか配列として見えているかということを変えることはありません。 |
2106 | 1419 | ですから、 |
2107 | 1420 | |
2108 | 1421 | local($foo) = <STDIN>; |
2109 | 1422 | local @FOO = <STDIN>; |
2110 | 1423 | |
2111 | 1424 | =begin original |
2112 | 1425 | |
2113 | 1426 | both supply a list context to the right-hand side, while |
2114 | 1427 | |
2115 | 1428 | =end original |
2116 | 1429 | |
2117 | 1430 | これらの両方ともが右辺をリストコンテキストにするのに対して、 |
2118 | 1431 | |
2119 | 1432 | local $foo = <STDIN>; |
2120 | 1433 | |
2121 | 1434 | =begin original |
2122 | 1435 | |
2123 | 1436 | supplies a scalar context. |
2124 | 1437 | |
2125 | 1438 | =end original |
2126 | 1439 | |
2127 | 1440 | これはスカラコンテキストを与えます. |
2128 | 1441 | |
2129 | 1442 | =head3 Localization of special variables |
2130 | 1443 | X<local, special variable> |
2131 | 1444 | |
2132 | 1445 | (特殊変数のローカル化) |
2133 | 1446 | |
2134 | 1447 | =begin original |
2135 | 1448 | |
2136 | 1449 | If you localize a special variable, you'll be giving a new value to it, |
2137 | 1450 | but its magic won't go away. That means that all side-effects related |
2138 | 1451 | to this magic still work with the localized value. |
2139 | 1452 | |
2140 | 1453 | =end original |
2141 | 1454 | |
2142 | 1455 | 特殊変数をローカル化すると、それに新しい値を入れられますが、特殊機能は |
2143 | 1456 | なくなりません。 |
2144 | 1457 | これは、その特殊機能に関係する全ての副作用はローカル化された値に対しても |
2145 | 1458 | 働くということを意味します。 |
2146 | 1459 | |
2147 | 1460 | =begin original |
2148 | 1461 | |
2149 | 1462 | This feature allows code like this to work : |
2150 | 1463 | |
2151 | 1464 | =end original |
2152 | 1465 | |
2153 | 1466 | この機能は以下のようなコードが動作するようにします: |
2154 | 1467 | |
2155 | 1468 | # Read the whole contents of FILE in $slurp |
2156 | 1469 | { local $/ = undef; $slurp = <FILE>; } |
2157 | 1470 | |
2158 | 1471 | =begin original |
2159 | 1472 | |
2160 | 1473 | Note, however, that this restricts localization of some values ; for |
2161 | example, the following statement dies, as of | |
1474 | example, the following statement dies, as of perl 5.9.0, with an error | |
2162 | 1475 | I<Modification of a read-only value attempted>, because the $1 variable is |
2163 | 1476 | magical and read-only : |
2164 | 1477 | |
2165 | 1478 | =end original |
2166 | 1479 | |
2167 | 1480 | しかし、これはいくつかの値のローカル化を制限することに注意してください; |
2168 | 例えば、以下の文は、 | |
1481 | 例えば、以下の文は、perl 5.9.0 からは、 | |
2169 | 1482 | I<Modification of a read-only value attempted> というエラーで die します; |
2170 | 1483 | なぜなら $1 変数はマジカルで読み込み専用だからです: |
2171 | 1484 | |
2172 | 1485 | local $1 = 2; |
2173 | 1486 | |
2174 | 1487 | =begin original |
2175 | 1488 | |
2176 | One exception is the default scalar variable: starting with | |
1489 | One exception is the default scalar variable: starting with perl 5.14 | |
2177 | 1490 | C<local($_)> will always strip all magic from $_, to make it possible |
2178 | 1491 | to safely reuse $_ in a subroutine. |
2179 | 1492 | |
2180 | 1493 | =end original |
2181 | 1494 | |
2182 | 一つの例外は、デフォルトスカラ変数です: | |
1495 | 一つの例外は、デフォルトスカラ変数です: perl 5.14 から、サブルーチン内で | |
2183 | 1496 | 安全に $_ を再利用できるように、C<local($_)> は常に |
2184 | 1497 | 全てのマジックを取り除きます。 |
2185 | 1498 | |
2186 | 1499 | =begin original |
2187 | 1500 | |
2188 | 1501 | B<WARNING>: Localization of tied arrays and hashes does not currently |
2189 | 1502 | work as described. |
2190 | 1503 | This will be fixed in a future release of Perl; in the meantime, avoid |
2191 | code that relies on any particular behavior of localising tied arrays | |
1504 | code that relies on any particular behaviour of localising tied arrays | |
2192 | 1505 | or hashes (localising individual elements is still okay). |
2193 | 1506 | See L<perl58delta/"Localising Tied Arrays and Hashes Is Broken"> for more |
2194 | 1507 | details. |
2195 | 1508 | X<local, tie> |
2196 | 1509 | |
2197 | 1510 | =end original |
2198 | 1511 | |
2199 | 1512 | B<警告>: tie された配列とハッシュのローカル化は現在のところ |
2200 | 1513 | 記述されたとおりに動作しません。 |
2201 | 1514 | これは Perl の将来のリリースで修正されます; |
2202 | 1515 | 当面の間は、tie された配列やハッシュのローカル化によるどのような |
2203 | 1516 | 振る舞いにも依存しないようなコードにしてください |
2204 | 1517 | (個々の要素のローカル化は問題ありません)。 |
2205 | 1518 | さらなる詳細については |
2206 | 1519 | L<perl58delta/"Localising Tied Arrays and Hashes Is Broken"> を |
2207 | 1520 | 参照してください。 |
2208 | 1521 | X<local, tie> |
2209 | 1522 | |
2210 | 1523 | =head3 Localization of globs |
2211 | 1524 | X<local, glob> X<glob> |
2212 | 1525 | |
2213 | 1526 | (グロブのローカル化) |
2214 | 1527 | |
2215 | 1528 | =begin original |
2216 | 1529 | |
2217 | 1530 | The construct |
2218 | 1531 | |
2219 | 1532 | =end original |
2220 | 1533 | |
2221 | 1534 | 以下のような構造は |
2222 | 1535 | |
2223 | 1536 | local *name; |
2224 | 1537 | |
2225 | 1538 | =begin original |
2226 | 1539 | |
2227 | 1540 | creates a whole new symbol table entry for the glob C<name> in the |
2228 | 1541 | current package. That means that all variables in its glob slot ($name, |
2229 | 1542 | @name, %name, &name, and the C<name> filehandle) are dynamically reset. |
2230 | 1543 | |
2231 | 1544 | =end original |
2232 | 1545 | |
2233 | 1546 | 現在のパッケージでグロブ C<name> のための完全に新しいシンボルテーブル |
2234 | 1547 | エントリを作成します。 |
2235 | 1548 | これは、このグロブスロットに入る全ての変数 ($name, @name, %name, &name, |
2236 | 1549 | C<name> ファイルハンドル) は動的にリセットされることを意味します。 |
2237 | 1550 | |
2238 | 1551 | =begin original |
2239 | 1552 | |
2240 | 1553 | This implies, among other things, that any magic eventually carried by |
2241 | 1554 | those variables is locally lost. In other words, saying C<local */> |
2242 | 1555 | will not have any effect on the internal value of the input record |
2243 | 1556 | separator. |
2244 | 1557 | |
2245 | 1558 | =end original |
2246 | 1559 | |
2247 | 1560 | これは特に、 |
2248 | 1561 | 最終的にこれらの変数によってもたらされるマジックは局所的には |
2249 | 1562 | 失われることを意味します。 |
2250 | 1563 | 言い換えると、C<local */> としても、入力レコードセパレータの内部の |
2251 | 1564 | 値には何の影響も与えないということです。 |
2252 | 1565 | |
2253 | 1566 | =head3 Localization of elements of composite types |
2254 | X<local, composite type element> X<local, array element> | |
1567 | X<local, composite type element> X<local, array element> X<local, hash element> | |
2255 | X<local, hash element> | |
2256 | 1568 | |
2257 | 1569 | (複合型の要素のローカル化) |
2258 | 1570 | |
2259 | 1571 | =begin original |
2260 | 1572 | |
2261 | 1573 | It's also worth taking a moment to explain what happens when you |
2262 | 1574 | C<local>ize a member of a composite type (i.e. an array or hash element). |
2263 | In this case, the element is C<local>ized I<by name>. | |
1575 | In this case, the element is C<local>ized I<by name>. This means that | |
2264 | 1576 | when the scope of the C<local()> ends, the saved value will be |
2265 | 1577 | restored to the hash element whose key was named in the C<local()>, or |
2266 | 1578 | the array element whose index was named in the C<local()>. If that |
2267 | 1579 | element was deleted while the C<local()> was in effect (e.g. by a |
2268 | 1580 | C<delete()> from a hash or a C<shift()> of an array), it will spring |
2269 | 1581 | back into existence, possibly extending an array and filling in the |
2270 | 1582 | skipped elements with C<undef>. For instance, if you say |
2271 | 1583 | |
2272 | 1584 | =end original |
2273 | 1585 | |
2274 | 1586 | 合成型のメンバー(たとえば配列やハッシュの要素)の局所化したときに |
2275 | 1587 | どうなるかを説明しましょう。 |
2276 | 1588 | この場合、その要素は B<名前によって> 局所化が行われます。 |
2277 | 1589 | これはつまり、C<local> のスコープが終わったときに、C<local> で名前が |
2278 | 1590 | 使われていたキーを持つハッシュの要素と C<local> で名前が使われていた |
2279 | 1591 | 配列要素に関して保存されていた値を復帰するということです。 |
2280 | 1592 | C<local()> が効果を持っているところでそういった要素が削除された場合 |
2281 | 1593 | (例えばハッシュに対して delete() を使うとか配列に対して |
2282 | 1594 | C<shift()>を使う)に、C<local()> の有効範囲を抜けたところで |
2283 | 1595 | 削除された要素が復活し、配列の拡張と拡張した分の要素の値を |
2284 | 1596 | C<undef> にするということを行います。例えば以下のような場合、 |
2285 | 1597 | |
2286 | 1598 | %hash = ( 'This' => 'is', 'a' => 'test' ); |
2287 | 1599 | @ary = ( 0..5 ); |
2288 | 1600 | { |
2289 | local($ary[5]) = 6; | |
1601 | local($ary[5]) = 6; | |
2290 | local($hash{'a'}) = 'drill'; | |
1602 | local($hash{'a'}) = 'drill'; | |
2291 | while (my $e = pop(@ary)) { | |
1603 | while (my $e = pop(@ary)) { | |
2292 | print "$e . . .\n"; | |
1604 | print "$e . . .\n"; | |
2293 | last unless $e > 3; | |
1605 | last unless $e > 3; | |
2294 | } | |
1606 | } | |
2295 | if (@ary) { | |
1607 | if (@ary) { | |
2296 | $hash{'only a'} = 'test'; | |
1608 | $hash{'only a'} = 'test'; | |
2297 | delete $hash{'a'}; | |
1609 | delete $hash{'a'}; | |
2298 | } | |
1610 | } | |
2299 | 1611 | } |
2300 | 1612 | print join(' ', map { "$_ $hash{$_}" } sort keys %hash),".\n"; |
2301 | 1613 | print "The array has ",scalar(@ary)," elements: ", |
2302 | join(', ', map { defined $_ ? $_ : 'undef' } @ary),"\n"; | |
1614 | join(', ', map { defined $_ ? $_ : 'undef' } @ary),"\n"; | |
2303 | 1615 | |
2304 | 1616 | =begin original |
2305 | 1617 | |
2306 | 1618 | Perl will print |
2307 | 1619 | |
2308 | 1620 | =end original |
2309 | 1621 | |
2310 | 1622 | Perl は以下のような出力をします。 |
2311 | 1623 | |
2312 | 1624 | 6 . . . |
2313 | 1625 | 4 . . . |
2314 | 1626 | 3 . . . |
2315 | 1627 | This is a test only a test. |
2316 | 1628 | The array has 6 elements: 0, 1, 2, undef, undef, 5 |
2317 | 1629 | |
2318 | 1630 | =begin original |
2319 | 1631 | |
2320 | 1632 | The behavior of local() on non-existent members of composite |
2321 | types is subject to change in future. | |
1633 | types is subject to change in future. | |
2322 | on array elements specified using negative indexes is particularly | |
2323 | surprising, and is very likely to change. | |
2324 | 1634 | |
2325 | 1635 | =end original |
2326 | 1636 | |
2327 | 1637 | 複合型の存在していないメンバーに対する local() の振る舞いは |
2328 | 1638 | 将来変更される予定です。 |
2329 | 負のインデックスを使って指定された配列要素の local() の振る舞いは | |
2330 | 特に驚くもので、おそらく変更されます。 | |
2331 | 1639 | |
2332 | 1640 | =head3 Localized deletion of elements of composite types |
2333 | X<delete> X<local, composite type element> X<local, array element> | |
1641 | X<delete> X<local, composite type element> X<local, array element> X<local, hash element> | |
2334 | X<local, hash element> | |
2335 | 1642 | |
2336 | (複合型の要素のローカル化された削除) | |
2337 | ||
2338 | 1643 | =begin original |
2339 | 1644 | |
2340 | 1645 | You can use the C<delete local $array[$idx]> and C<delete local $hash{key}> |
2341 | 1646 | constructs to delete a composite type entry for the current block and restore |
2342 | it when it ends. | |
1647 | it when it ends. They return the array/hash value before the localization, | |
2343 | 1648 | which means that they are respectively equivalent to |
2344 | 1649 | |
2345 | 1650 | =end original |
2346 | 1651 | |
2347 | 1652 | 現在のブロックからある複合型エントリを削除してブロックの終了時に |
2348 | 1653 | 復元するために、C<delete local $array[$idx]> および |
2349 | 1654 | C<delete local $hash{key}> 構文を使えます。 |
2350 | 1655 | これらはローカル化される前に配列/ハッシュの値を返すので、以下はそれぞれ |
2351 | 1656 | 等価です: |
2352 | 1657 | |
2353 | 1658 | do { |
2354 | 1659 | my $val = $array[$idx]; |
2355 | 1660 | local $array[$idx]; |
2356 | 1661 | delete $array[$idx]; |
2357 | 1662 | $val |
2358 | 1663 | } |
2359 | 1664 | |
2360 | 1665 | =begin original |
2361 | 1666 | |
2362 | 1667 | and |
2363 | 1668 | |
2364 | 1669 | =end original |
2365 | 1670 | |
2366 | 1671 | および |
2367 | 1672 | |
2368 | 1673 | do { |
2369 | 1674 | my $val = $hash{key}; |
2370 | 1675 | local $hash{key}; |
2371 | 1676 | delete $hash{key}; |
2372 | 1677 | $val |
2373 | 1678 | } |
2374 | 1679 | |
2375 | 1680 | =begin original |
2376 | 1681 | |
2377 | except that for those the C<local> is | |
1682 | except that for those the C<local> is scoped to the C<do> block. Slices are | |
2378 | scoped to the C<do> block. Slices are | |
2379 | 1683 | also accepted. |
2380 | 1684 | |
2381 | 1685 | =end original |
2382 | 1686 | |
2383 | 1687 | 但し C<local> は C<do> ブロックのスコープです。 |
2384 | 1688 | スライスも受け付けられます。 |
2385 | 1689 | |
2386 | 1690 | my %hash = ( |
2387 | | |
1691 | a => [ 7, 8, 9 ], | |
2388 | | |
1692 | b => 1, | |
2389 | 1693 | ) |
2390 | 1694 | |
2391 | 1695 | { |
2392 | | |
1696 | my $a = delete local $hash{a}; | |
2393 | | |
1697 | # $a is [ 7, 8, 9 ] | |
2394 | | |
1698 | # %hash is (b => 1) | |
2395 | 1699 | |
2396 | | |
1700 | { | |
2397 | | |
1701 | my @nums = delete local @$a[0, 2] | |
2398 | | |
1702 | # @nums is (7, 9) | |
2399 | | |
1703 | # $a is [ undef, 8 ] | |
2400 | 1704 | |
2401 | | |
1705 | $a[0] = 999; # will be erased when the scope ends | |
2402 | | |
1706 | } | |
2403 | | |
1707 | # $a is back to [ 7, 8, 9 ] | |
2404 | 1708 | |
2405 | 1709 | } |
2406 | 1710 | # %hash is back to its original state |
2407 | 1711 | |
1712 | =head2 Lvalue subroutines | |
1713 | X<lvalue> X<subroutine, lvalue> | |
1714 | ||
1715 | (左辺値サブルーチン) | |
1716 | ||
2408 | 1717 | =begin original |
2409 | 1718 | |
2410 | ||
1719 | B<WARNING>: Lvalue subroutines are still experimental and the | |
1720 | implementation may change in future versions of Perl. | |
2411 | 1721 | |
2412 | 1722 | =end original |
2413 | 1723 | |
2414 | ||
1724 | B<警告>: 左辺値サブルーチンは未だ実験的機能であり、 | |
1725 | 将来のバージョンの Perl では実装が変更されるかもしれません。 | |
2415 | 1726 | |
2416 | =head2 Lvalue subroutines | |
2417 | X<lvalue> X<subroutine, lvalue> | |
2418 | ||
2419 | (左辺値サブルーチン) | |
2420 | ||
2421 | 1727 | =begin original |
2422 | 1728 | |
2423 | 1729 | It is possible to return a modifiable value from a subroutine. |
2424 | 1730 | To do this, you have to declare the subroutine to return an lvalue. |
2425 | 1731 | |
2426 | 1732 | =end original |
2427 | 1733 | |
2428 | 1734 | サブルーチンから、変更可能な値を返すことが可能です。 |
2429 | 1735 | そうするためには、サブルーチンが左辺値を返すように宣言しなければなりません。 |
2430 | 1736 | |
2431 | 1737 | my $val; |
2432 | 1738 | sub canmod : lvalue { |
2433 | ||
1739 | $val; # or: return $val; | |
2434 | 1740 | } |
2435 | 1741 | sub nomod { |
2436 | ||
1742 | $val; | |
2437 | 1743 | } |
2438 | 1744 | |
2439 | 1745 | canmod() = 5; # assigns to $val |
2440 | 1746 | nomod() = 5; # ERROR |
2441 | 1747 | |
2442 | 1748 | =begin original |
2443 | 1749 | |
2444 | 1750 | The scalar/list context for the subroutine and for the right-hand |
2445 | 1751 | side of assignment is determined as if the subroutine call is replaced |
2446 | by a scalar. | |
1752 | by a scalar. For example, consider: | |
2447 | 1753 | |
2448 | 1754 | =end original |
2449 | 1755 | |
2450 | 1756 | サブルーチンと、代入の右側のスカラ/リストコンテキストは |
2451 | 1757 | サブルーチン呼び出しがスカラに置き換えられたかのようにして |
2452 | 1758 | 決定されます。 |
2453 | 1759 | 例として、以下を考えると: |
2454 | 1760 | |
2455 | 1761 | data(2,3) = get_data(3,4); |
2456 | 1762 | |
2457 | 1763 | =begin original |
2458 | 1764 | |
2459 | 1765 | Both subroutines here are called in a scalar context, while in: |
2460 | 1766 | |
2461 | 1767 | =end original |
2462 | 1768 | |
2463 | 1769 | 両方のサブルーチンはスカラコンテキストで呼び出され、一方: |
2464 | 1770 | |
2465 | 1771 | (data(2,3)) = get_data(3,4); |
2466 | 1772 | |
2467 | 1773 | =begin original |
2468 | 1774 | |
2469 | 1775 | and in: |
2470 | 1776 | |
2471 | 1777 | =end original |
2472 | 1778 | |
2473 | 1779 | 及び: |
2474 | 1780 | |
2475 | 1781 | (data(2),data(3)) = get_data(3,4); |
2476 | 1782 | |
2477 | 1783 | =begin original |
2478 | 1784 | |
2479 | 1785 | all the subroutines are called in a list context. |
2480 | 1786 | |
2481 | 1787 | =end original |
2482 | 1788 | |
2483 | 1789 | については全てのサブルーチンはリストコンテキストで呼び出されます。 |
2484 | 1790 | |
2485 | = | |
1791 | =over 4 | |
2486 | 1792 | |
2487 | Lvalue subroutines are | |
1793 | =item Lvalue subroutines are EXPERIMENTAL | |
2488 | when used with objects, they may violate encapsulation. A normal | |
2489 | mutator can check the supplied argument before setting the attribute | |
2490 | it is protecting, an lvalue subroutine cannot. If you require any | |
2491 | special processing when storing and retrieving the values, consider | |
2492 | using the CPAN module Sentinel or something similar. | |
2493 | 1794 | |
2494 | =end original | |
2495 | ||
2496 | 左辺値サブルーチンは便利ですが、オブジェクトに対して使うと、カプセル化に | |
2497 | 違反するかも知れないことを心に留めておく必要があります。 | |
2498 | 通常のミューテータは、指定された引数を守られている属性に代入する前に | |
2499 | チェックできますが、左辺値サブルーチンはできません。 | |
2500 | 値の補完と取り出しに何らかの特別処理が必要な場合は、CPAN モジュールの | |
2501 | Sentinel または似たようなものを使うことを考慮してください。 | |
2502 | ||
2503 | =head2 Lexical Subroutines | |
2504 | X<my sub> X<state sub> X<our sub> X<subroutine, lexical> | |
2505 | ||
2506 | (レキシカルサブルーチン) | |
2507 | ||
2508 | 1795 | =begin original |
2509 | 1796 | |
2510 | ||
1797 | They appear to be convenient, but there is at least one reason to be | |
2511 | ||
1798 | circumspect. | |
2512 | available under C<use feature 'state'> or C<use v5.10> or higher. | |
2513 | 1799 | |
2514 | 1800 | =end original |
2515 | 1801 | |
2516 | ||
1802 | これは便利なようですが、慎重になるべき少なくとも一つの理由があります。 | |
2517 | 宣言できます。 | |
2518 | state 変数の場合、C<state> キーワードは C<use feature 'state'> または | |
2519 | C<use v5.10> またはそれ以上の下でのみ利用可能です。 | |
2520 | 1803 | |
2521 | 1804 | =begin original |
2522 | 1805 | |
2523 | ||
1806 | They violate encapsulation. A normal mutator can check the supplied | |
2524 | a | |
1807 | argument before setting the attribute it is protecting, an lvalue | |
2525 | ||
1808 | subroutine never gets that chance. Consider; | |
2526 | category was disabled. | |
2527 | 1809 | |
2528 | 1810 | =end original |
2529 | 1811 | |
2530 | ||
1812 | これはカプセル化に違反しています。 | |
2531 | ||
1813 | 通常の mutator は、自身が保護している属性にセットする前に | |
2532 | ま | |
1814 | 引数をチェックできますが、 | |
2533 | ||
1815 | 左辺値サブルーチンにはその機会はありません。 | |
1816 | 以下を考えてみてください; | |
2534 | 1817 | |
2535 | = | |
1818 | my $some_array_ref = []; # protected by mutators ?? | |
2536 | 1819 | |
2537 | ||
1820 | sub set_arr { # normal mutator | |
2538 | ||
1821 | my $val = shift; | |
1822 | die("expected array, you supplied ", ref $val) | |
2540 | ||
1823 | unless ref $val eq 'ARRAY'; | |
1824 | $some_array_ref = $val; | |
2542 | これらのサブルーチンは、宣言されたブロックの内側で、宣言された後でのみ | |
2543 | 見えます: | |
2544 | ||
2545 | # Include these two lines if your code is intended to run under Perl | |
2546 | # versions earlier than 5.26. | |
2547 | no warnings "experimental::lexical_subs"; | |
2548 | use feature 'lexical_subs'; | |
2549 | ||
2550 | foo(); # calls the package/global subroutine | |
2551 | state sub foo { | |
2552 | foo(); # also calls the package subroutine | |
2553 | 1825 | } |
2554 | | |
1826 | sub set_arr_lv : lvalue { # lvalue mutator | |
2555 | ||
1827 | $some_array_ref; | |
2556 | ||
2557 | my sub bar { ... } | |
2558 | bar(); # calls "my" sub | |
2559 | ||
2560 | =begin original | |
2561 | ||
2562 | You can't (directly) write a recursive lexical subroutine: | |
2563 | ||
2564 | =end original | |
2565 | ||
2566 | (直接)再帰レキシカルサブルーチンを書くことは出来ません: | |
2567 | ||
2568 | # WRONG | |
2569 | my sub baz { | |
2570 | baz(); | |
2571 | 1828 | } |
2572 | 1829 | |
2573 | ||
1830 | # set_arr_lv cannot stop this ! | |
1831 | set_arr_lv() = { a => 1 }; | |
2574 | 1832 | |
2575 | ||
1833 | =back | |
2576 | C<baz>, not the lexical subroutine currently being defined. | |
2577 | 1834 | |
2578 | =end original | |
2579 | ||
2580 | This example fails because | |
2581 | C<baz()> は、現在定義しているレキシカルサブルーチンではなく、 | |
2582 | パッケージ/グローバルサブルーチン C<baz> を参照するので、 | |
2583 | この例は失敗します。 | |
2584 | ||
2585 | =begin original | |
2586 | ||
2587 | The solution is to use L<C<__SUB__>|perlfunc/__SUB__>: | |
2588 | ||
2589 | =end original | |
2590 | ||
2591 | 解決法は、L<C<__SUB__>|perlfunc/__SUB__> を使うことです: | |
2592 | ||
2593 | my sub baz { | |
2594 | __SUB__->(); # calls itself | |
2595 | } | |
2596 | ||
2597 | =begin original | |
2598 | ||
2599 | It is possible to predeclare a lexical subroutine. The C<sub foo {...}> | |
2600 | subroutine definition syntax respects any previous C<my sub;> or C<state sub;> | |
2601 | declaration. Using this to define recursive subroutines is a bad idea, | |
2602 | however: | |
2603 | ||
2604 | =end original | |
2605 | ||
2606 | レキシカルサブルーチンを先行宣言することは可能です。 | |
2607 | C<sub foo {...}> サブルーチン宣言文法は、先行する C<my sub;> または | |
2608 | C<state sub;> 宣言を考慮します。 | |
2609 | しかし、これを再帰サブルーチンを定義するのに使うのは良くない考えです: | |
2610 | ||
2611 | my sub baz; # predeclaration | |
2612 | sub baz { # define the "my" sub | |
2613 | baz(); # WRONG: calls itself, but leaks memory | |
2614 | } | |
2615 | ||
2616 | =begin original | |
2617 | ||
2618 | Just like C<< my $f; $f = sub { $f->() } >>, this example leaks memory. The | |
2619 | name C<baz> is a reference to the subroutine, and the subroutine uses the name | |
2620 | C<baz>; they keep each other alive (see L<perlref/Circular References>). | |
2621 | ||
2622 | =end original | |
2623 | ||
2624 | C<< my $f; $f = sub { $f->() } >> と同様、この例はメモリリークします。 | |
2625 | C<baz> という名前はサブルーチンへのリファレンスで、 | |
2626 | サブルーチンは C<baz> という名前を使います; | |
2627 | これは互いに相手を有効にします (L<perlref/Circular References> 参照)。 | |
2628 | ||
2629 | =head3 C<state sub> vs C<my sub> | |
2630 | ||
2631 | =begin original | |
2632 | ||
2633 | What is the difference between "state" subs and "my" subs? Each time that | |
2634 | execution enters a block when "my" subs are declared, a new copy of each | |
2635 | sub is created. "State" subroutines persist from one execution of the | |
2636 | containing block to the next. | |
2637 | ||
2638 | =end original | |
2639 | ||
2640 | "state" サブルーチンと "my" サブルーチンの違いはなんでしょう? | |
2641 | "my" サブルーチンが宣言されていると、ブロックが実行される毎に新しい | |
2642 | サブルーチンが作成されます。 | |
2643 | "state" サブルーチンは、一度目の内部のブロックの実行から次まで永続します。 | |
2644 | ||
2645 | =begin original | |
2646 | ||
2647 | So, in general, "state" subroutines are faster. But "my" subs are | |
2648 | necessary if you want to create closures: | |
2649 | ||
2650 | =end original | |
2651 | ||
2652 | したがって、一般的に、"state" サブルーチンの方が速いです。 | |
2653 | しかし、クロージャを作成したいときには "my" サブルーチンが必要です: | |
2654 | ||
2655 | sub whatever { | |
2656 | my $x = shift; | |
2657 | my sub inner { | |
2658 | ... do something with $x ... | |
2659 | } | |
2660 | inner(); | |
2661 | } | |
2662 | ||
2663 | =begin original | |
2664 | ||
2665 | In this example, a new C<$x> is created when C<whatever> is called, and | |
2666 | also a new C<inner>, which can see the new C<$x>. A "state" sub will only | |
2667 | see the C<$x> from the first call to C<whatever>. | |
2668 | ||
2669 | =end original | |
2670 | ||
2671 | この例で、C<whatever> が呼び出されたときに新しい C<$x> が作成され、さらに | |
2672 | 新しい C<$x> が見える新しい C<inner> が作成されます。 | |
2673 | "state" サブルーチンは最初の C<whatever> の呼び出しからの C<$x> だけが | |
2674 | 見えます。 | |
2675 | ||
2676 | =head3 C<our> subroutines | |
2677 | ||
2678 | (C<our> サブルーチン) | |
2679 | ||
2680 | =begin original | |
2681 | ||
2682 | Like C<our $variable>, C<our sub> creates a lexical alias to the package | |
2683 | subroutine of the same name. | |
2684 | ||
2685 | =end original | |
2686 | ||
2687 | C<our $variable> と同様、C<our sub> は同じ名前のパッケージサブルーチンへの | |
2688 | レキシカルな別名を作成します。 | |
2689 | ||
2690 | =begin original | |
2691 | ||
2692 | The two main uses for this are to switch back to using the package sub | |
2693 | inside an inner scope: | |
2694 | ||
2695 | =end original | |
2696 | ||
2697 | これの主な二つの使用法は、内側のスコープの内側のパッケージサブルーチンを | |
2698 | 使うために切り替えのためです: | |
2699 | ||
2700 | sub foo { ... } | |
2701 | ||
2702 | sub bar { | |
2703 | my sub foo { ... } | |
2704 | { | |
2705 | # need to use the outer foo here | |
2706 | our sub foo; | |
2707 | foo(); | |
2708 | } | |
2709 | } | |
2710 | ||
2711 | =begin original | |
2712 | ||
2713 | and to make a subroutine visible to other packages in the same scope: | |
2714 | ||
2715 | =end original | |
2716 | ||
2717 | もう一つは同じスコープのほかのパッケージから見えるサブルーチンを作ることです: | |
2718 | ||
2719 | package MySneakyModule; | |
2720 | ||
2721 | our sub do_something { ... } | |
2722 | ||
2723 | sub do_something_with_caller { | |
2724 | package DB; | |
2725 | () = caller 1; # sets @DB::args | |
2726 | do_something(@args); # uses MySneakyModule::do_something | |
2727 | } | |
2728 | ||
2729 | 1835 | =head2 Passing Symbol Table Entries (typeglobs) |
2730 | 1836 | X<typeglob> X<*> |
2731 | 1837 | |
2732 | 1838 | (シンボルテーブルのエントリを渡す(型グロブ)) |
2733 | 1839 | |
2734 | 1840 | =begin original |
2735 | 1841 | |
2736 | 1842 | B<WARNING>: The mechanism described in this section was originally |
2737 | 1843 | the only way to simulate pass-by-reference in older versions of |
2738 | 1844 | Perl. While it still works fine in modern versions, the new reference |
2739 | 1845 | mechanism is generally easier to work with. See below. |
2740 | 1846 | |
2741 | 1847 | =end original |
2742 | 1848 | |
2743 | 1849 | B<警告>: このセクションで説明されている仕組みは、古いバージョンの Perl に |
2744 | 1850 | おいて参照渡しをシミュレートするための唯一の方法でした。 |
2745 | 1851 | これは現在でも使うことができるのですが、新しいリファレンスの仕組みは |
2746 | 1852 | これをより簡単に行います。後の説明を参照してください。 |
2747 | 1853 | |
2748 | 1854 | =begin original |
2749 | 1855 | |
2750 | 1856 | Sometimes you don't want to pass the value of an array to a subroutine |
2751 | 1857 | but rather the name of it, so that the subroutine can modify the global |
2752 | copy of it rather than working with a local copy. In | |
1858 | copy of it rather than working with a local copy. In perl you can | |
2753 | 1859 | refer to all objects of a particular name by prefixing the name |
2754 | 1860 | with a star: C<*foo>. This is often known as a "typeglob", because the |
2755 | 1861 | star on the front can be thought of as a wildcard match for all the |
2756 | 1862 | funny prefix characters on variables and subroutines and such. |
2757 | 1863 | |
2758 | 1864 | =end original |
2759 | 1865 | |
2760 | 1866 | サブルーチンが渡された配列のローカルなコピーではなくてグローバルな |
2761 | 1867 | ものの変更ができるように、サブルーチンに対して配列の値ではなく |
2762 | 1868 | 配列の名前を渡したくなることもあるでしょう。 |
2763 | ||
1869 | perl においては、これを C<*foo> のようにアスタリスクを使って | |
2764 | 1870 | 行うことができます。 |
2765 | 1871 | これは前に置かれたアスタリスクが変数やサブルーチンなどに対して使われる |
2766 | 前置 | |
1872 | 前置キャラクター全てにマッチするワイルドカードとしてみなすことが | |
2767 | 1873 | できるので、“型グロブ”としてよく知られています。 |
2768 | 1874 | |
2769 | 1875 | =begin original |
2770 | 1876 | |
2771 | 1877 | When evaluated, the typeglob produces a scalar value that represents |
2772 | 1878 | all the objects of that name, including any filehandle, format, or |
2773 | 1879 | subroutine. When assigned to, it causes the name mentioned to refer to |
2774 | 1880 | whatever C<*> value was assigned to it. Example: |
2775 | 1881 | |
2776 | 1882 | =end original |
2777 | 1883 | |
2778 | 1884 | 型グロブは評価されたときにその名前を持つ全てのオブジェクト(ファイルハンドル、 |
2779 | 1885 | フォーマット、サブルーチンも含まれます)を表すスカラ値を生成します。 |
2780 | 1886 | 代入が行われたとき、C<*> は代入したものを反映するようになります。 |
2781 | 1887 | 例: |
2782 | 1888 | |
2783 | 1889 | sub doubleary { |
2784 | ||
1890 | local(*someary) = @_; | |
2785 | ||
1891 | foreach $elem (@someary) { | |
2786 | ||
1892 | $elem *= 2; | |
2787 | ||
1893 | } | |
2788 | 1894 | } |
2789 | 1895 | doubleary(*foo); |
2790 | 1896 | doubleary(*bar); |
2791 | 1897 | |
2792 | 1898 | =begin original |
2793 | 1899 | |
2794 | 1900 | Scalars are already passed by reference, so you can modify |
2795 | 1901 | scalar arguments without using this mechanism by referring explicitly |
2796 | 1902 | to C<$_[0]> etc. You can modify all the elements of an array by passing |
2797 | 1903 | all the elements as scalars, but you have to use the C<*> mechanism (or |
2798 | 1904 | the equivalent reference mechanism) to C<push>, C<pop>, or change the size of |
2799 | 1905 | an array. It will certainly be faster to pass the typeglob (or reference). |
2800 | 1906 | |
2801 | 1907 | =end original |
2802 | 1908 | |
2803 | 1909 | スカラは既に参照渡しされているので、陽に C<$_[0]> などで参照して |
2804 | 1910 | この機構を使わなくてもスカラ引数を変更することができます。 |
2805 | 1911 | 全ての要素がスカラとして渡された配列の要素はすべて |
2806 | 1912 | 変更することができますが、C<push>、C<pop>、もしくは配列のサイズを |
2807 | 1913 | 変更するためには C<*> 機構(もしくは同等のリファレンス機構)を |
2808 | 1914 | 使う必要があります。 |
2809 | 1915 | これは型グロブ(もしくはリファレンス)を渡すのを確実に高速にするでしょう。 |
2810 | 1916 | |
2811 | 1917 | =begin original |
2812 | 1918 | |
2813 | 1919 | Even if you don't want to modify an array, this mechanism is useful for |
2814 | 1920 | passing multiple arrays in a single LIST, because normally the LIST |
2815 | 1921 | mechanism will merge all the array values so that you can't extract out |
2816 | 1922 | the individual arrays. For more on typeglobs, see |
2817 | 1923 | L<perldata/"Typeglobs and Filehandles">. |
2818 | 1924 | |
2819 | 1925 | =end original |
2820 | 1926 | |
2821 | 1927 | 配列の変更を望まないとしても、この機構は単一のリストの中にある複数の |
2822 | 1928 | リストを渡すのに便利です; なぜなら、通常はリスト機構はすべての配列の値を |
2823 | 1929 | 結合してしまうので独立した配列を取り出すことができないからです。 |
2824 | 1930 | 型グロブについては、 |
2825 | 1931 | L<perldata/"Typeglobs and Filehandles"> も参照してください。 |
2826 | 1932 | |
2827 | 1933 | =head2 When to Still Use local() |
2828 | 1934 | X<local> X<variable, local> |
2829 | 1935 | |
2830 | 1936 | (今でも local() を使うとき) |
2831 | 1937 | |
2832 | 1938 | =begin original |
2833 | 1939 | |
2834 | 1940 | Despite the existence of C<my>, there are still three places where the |
2835 | 1941 | C<local> operator still shines. In fact, in these three places, you |
2836 | 1942 | I<must> use C<local> instead of C<my>. |
2837 | 1943 | |
2838 | 1944 | =end original |
2839 | 1945 | |
2840 | 1946 | C<my> があるにも関らず、今でも C<local> 演算子を使うべき場所が三ヶ所あります。 |
2841 | 1947 | 実際にはその三ヶ所においては、C<my> ではなく、B<必ず> C<local> を |
2842 | 1948 | 使わなければなりません。 |
2843 | 1949 | |
2844 | 1950 | =over 4 |
2845 | 1951 | |
2846 | 1952 | =item 1. |
2847 | 1953 | |
2848 | 1954 | =begin original |
2849 | 1955 | |
2850 | 1956 | You need to give a global variable a temporary value, especially $_. |
2851 | 1957 | |
2852 | 1958 | =end original |
2853 | 1959 | |
2854 | 1960 | グローバル変数(特に C<$_>) に関して、一時的な値を与えたいとき。 |
2855 | 1961 | |
2856 | 1962 | =begin original |
2857 | 1963 | |
2858 | The global variables, like C<@ARGV> or the punctuation variables, must be | |
1964 | The global variables, like C<@ARGV> or the punctuation variables, must be | |
2859 | 1965 | C<local>ized with C<local()>. This block reads in F</etc/motd>, and splits |
2860 | 1966 | it up into chunks separated by lines of equal signs, which are placed |
2861 | 1967 | in C<@Fields>. |
2862 | 1968 | |
2863 | 1969 | =end original |
2864 | 1970 | |
2865 | 1971 | C<@ARGV> や句読点変数(punctuation variables)のようなグローバル変数では、 |
2866 | 1972 | 局所化のために C<local()> を使わなければなりません。 |
2867 | 1973 | 以下のブロックは F</etc/motd> を読み込み、等価記号を使って行の塊を分割し、 |
2868 | 1974 | その結果を C<@Fields> に格納します。 |
2869 | 1975 | |
2870 | 1976 | { |
2871 | ||
1977 | local @ARGV = ("/etc/motd"); | |
2872 | 1978 | local $/ = undef; |
2873 | local $_ = <>; | |
1979 | local $_ = <>; | |
2874 | ||
1980 | @Fields = split /^\s*=+\s*$/; | |
2875 | } | |
1981 | } | |
2876 | 1982 | |
2877 | 1983 | =begin original |
2878 | 1984 | |
2879 | I | |
1985 | It particular, it's important to C<local>ize $_ in any routine that assigns | |
2880 | 1986 | to it. Look out for implicit assignments in C<while> conditionals. |
2881 | 1987 | |
2882 | 1988 | =end original |
2883 | 1989 | |
2884 | 1990 | 特に重要なのは、任意のルーチンの中で $_ を局所化し、それに対して |
2885 | 1991 | 代入することです。 |
2886 | 1992 | C<while> 文での暗黙的な代入に注意してください。 |
2887 | 1993 | |
2888 | 1994 | =item 2. |
2889 | 1995 | |
2890 | 1996 | =begin original |
2891 | 1997 | |
2892 | 1998 | You need to create a local file or directory handle or a local function. |
2893 | 1999 | |
2894 | 2000 | =end original |
2895 | 2001 | |
2896 | 2002 | ローカルなファイルハンドルやディレクトリハンドル、 |
2897 | 2003 | もしくはローカル関数を作成する必要がある場合。 |
2898 | 2004 | |
2899 | 2005 | =begin original |
2900 | 2006 | |
2901 | 2007 | A function that needs a filehandle of its own must use |
2902 | 2008 | C<local()> on a complete typeglob. This can be used to create new symbol |
2903 | 2009 | table entries: |
2904 | 2010 | |
2905 | 2011 | =end original |
2906 | 2012 | |
2907 | 2013 | 関数に固有のファイルハンドルが必要な関数は、完全な型グロブを使った |
2908 | 2014 | C<local()> を使わなければなりません。 |
2909 | 2015 | これによって、新しいシンボルテーブルのエントリが作成されます: |
2910 | 2016 | |
2911 | 2017 | sub ioqueue { |
2912 | 2018 | local (*READER, *WRITER); # not my! |
2913 | 2019 | pipe (READER, WRITER) or die "pipe: $!"; |
2914 | 2020 | return (*READER, *WRITER); |
2915 | 2021 | } |
2916 | 2022 | ($head, $tail) = ioqueue(); |
2917 | 2023 | |
2918 | 2024 | =begin original |
2919 | 2025 | |
2920 | 2026 | See the Symbol module for a way to create anonymous symbol table |
2921 | 2027 | entries. |
2922 | 2028 | |
2923 | 2029 | =end original |
2924 | 2030 | |
2925 | 2031 | 無名シンボルテーブルを生成する方法については、Symbol モジュールを |
2926 | 2032 | 参照してください。 |
2927 | 2033 | |
2928 | 2034 | =begin original |
2929 | 2035 | |
2930 | 2036 | Because assignment of a reference to a typeglob creates an alias, this |
2931 | 2037 | can be used to create what is effectively a local function, or at least, |
2932 | 2038 | a local alias. |
2933 | 2039 | |
2934 | 2040 | =end original |
2935 | 2041 | |
2936 | 2042 | 型グロブに対するリファレンスの代入はエイリアスを生成するので、 |
2937 | 2043 | 以下の例では効果的にローカル関数(あるいは少なくともローカルエイリアス)を |
2938 | 2044 | 生成するのに使うことができます。 |
2939 | 2045 | |
2940 | 2046 | { |
2941 | 2047 | local *grow = \&shrink; # only until this block exits |
2942 | grow(); # really calls shrink() | |
2048 | grow(); # really calls shrink() | |
2943 | ||
2049 | move(); # if move() grow()s, it shrink()s too | |
2944 | 2050 | } |
2945 | grow(); | |
2051 | grow(); # get the real grow() again | |
2946 | 2052 | |
2947 | 2053 | =begin original |
2948 | 2054 | |
2949 | 2055 | See L<perlref/"Function Templates"> for more about manipulating |
2950 | 2056 | functions by name in this way. |
2951 | 2057 | |
2952 | 2058 | =end original |
2953 | 2059 | |
2954 | 2060 | こういったやり方で、名前によって関数を操作する方法に関しての詳細は |
2955 | 2061 | L<perlref/"Function Templates"> を参照してください。 |
2956 | 2062 | |
2957 | 2063 | =item 3. |
2958 | 2064 | |
2959 | 2065 | =begin original |
2960 | 2066 | |
2961 | 2067 | You want to temporarily change just one element of an array or hash. |
2962 | 2068 | |
2963 | 2069 | =end original |
2964 | 2070 | |
2965 | 2071 | 配列やハッシュのある要素だけを一時的に変更したい場合。 |
2966 | 2072 | |
2967 | 2073 | =begin original |
2968 | 2074 | |
2969 | 2075 | You can C<local>ize just one element of an aggregate. Usually this |
2970 | 2076 | is done on dynamics: |
2971 | 2077 | |
2972 | 2078 | =end original |
2973 | 2079 | |
2974 | 2080 | 一つの要素だけを局所化することが可能です。 |
2975 | 2081 | 通常はこれは動的に行われます。 |
2976 | 2082 | |
2977 | 2083 | { |
2978 | ||
2084 | local $SIG{INT} = 'IGNORE'; | |
2979 | ||
2085 | funct(); # uninterruptible | |
2980 | } | |
2086 | } | |
2981 | 2087 | # interruptibility automatically restored here |
2982 | 2088 | |
2983 | 2089 | =begin original |
2984 | 2090 | |
2985 | But it also works on lexically declared aggregates. | |
2091 | But it also works on lexically declared aggregates. Prior to 5.005, | |
2092 | this operation could on occasion misbehave. | |
2986 | 2093 | |
2987 | 2094 | =end original |
2988 | 2095 | |
2989 | 2096 | しかし、これはレキシカル変数であっても動作します。 |
2097 | 5.005 より前のものでは、この操作は間違った状況になる可能性がありました。 | |
2990 | 2098 | |
2991 | 2099 | =back |
2992 | 2100 | |
2993 | 2101 | =head2 Pass by Reference |
2994 | 2102 | X<pass by reference> X<pass-by-reference> X<reference> |
2995 | 2103 | |
2996 | 2104 | (参照渡し) |
2997 | 2105 | |
2998 | 2106 | =begin original |
2999 | 2107 | |
3000 | 2108 | If you want to pass more than one array or hash into a function--or |
3001 | 2109 | return them from it--and have them maintain their integrity, then |
3002 | 2110 | you're going to have to use an explicit pass-by-reference. Before you |
3003 | 2111 | do that, you need to understand references as detailed in L<perlref>. |
3004 | 2112 | This section may not make much sense to you otherwise. |
3005 | 2113 | |
3006 | 2114 | =end original |
3007 | 2115 | |
3008 | 2116 | もし、配列やハッシュを二つ以上関数に渡したいとか関数から返したいと |
3009 | 2117 | 考えていて、同時にそれらを完全な状態で扱いたいというのであれば、 |
3010 | 2118 | 明示的に参照渡しを使う必要があるでしょう。 |
3011 | 2119 | これを行う前に、L<perlref> で説明されているリファレンスを理解する |
3012 | 2120 | 必要があります。 |
3013 | 2121 | このセクションでは改めて説明するようなことはしません。 |
3014 | 2122 | |
3015 | 2123 | =begin original |
3016 | 2124 | |
3017 | 2125 | Here are a few simple examples. First, let's pass in several arrays |
3018 | to a function and have it C<pop> all of the | |
2126 | to a function and have it C<pop> all of then, returning a new list | |
3019 | 2127 | of all their former last elements: |
3020 | 2128 | |
3021 | 2129 | =end original |
3022 | 2130 | |
3023 | 2131 | 幾つか単純な例を挙げましょう。 |
3024 | 2132 | まず最初に、ある関数に幾つかの配列を渡し、 |
3025 | 2133 | 全ての配列に対して C<pop> を行って引数として受け取った配列の |
3026 | 2134 | それぞれの最後の要素からなる新しいリストを返すということを |
3027 | 2135 | 考えてみましょう。 |
3028 | 2136 | |
3029 | @tailings = popmany ( \@ | |
2137 | @tailings = popmany ( \@a, \@b, \@c, \@d ); | |
3030 | 2138 | |
3031 | 2139 | sub popmany { |
3032 | ||
2140 | my $aref; | |
3033 | ||
2141 | my @retlist = (); | |
3034 | ||
2142 | foreach $aref ( @_ ) { | |
3035 | ||
2143 | push @retlist, pop @$aref; | |
3036 | ||
2144 | } | |
3037 | ||
2145 | return @retlist; | |
3038 | 2146 | } |
3039 | 2147 | |
3040 | 2148 | =begin original |
3041 | 2149 | |
3042 | 2150 | Here's how you might write a function that returns a |
3043 | 2151 | list of keys occurring in all the hashes passed to it: |
3044 | 2152 | |
3045 | 2153 | =end original |
3046 | 2154 | |
3047 | 2155 | 以下の例は、引数として渡された全てのハッシュにあるキーのリストを |
3048 | 2156 | 返す関数です: |
3049 | 2157 | |
3050 | 2158 | @common = inter( \%foo, \%bar, \%joe ); |
3051 | 2159 | sub inter { |
3052 | ||
2160 | my ($k, $href, %seen); # locals | |
3053 | ||
2161 | foreach $href (@_) { | |
3054 | ||
2162 | while ( $k = each %$href ) { | |
3055 | ||
2163 | $seen{$k}++; | |
3056 | ||
2164 | } | |
3057 | ||
2165 | } | |
3058 | ||
2166 | return grep { $seen{$_} == @_ } keys %seen; | |
3059 | 2167 | } |
3060 | 2168 | |
3061 | 2169 | =begin original |
3062 | 2170 | |
3063 | 2171 | So far, we're using just the normal list return mechanism. |
3064 | 2172 | What happens if you want to pass or return a hash? Well, |
3065 | 2173 | if you're using only one of them, or you don't mind them |
3066 | 2174 | concatenating, then the normal calling convention is ok, although |
3067 | 2175 | a little expensive. |
3068 | 2176 | |
3069 | 2177 | =end original |
3070 | 2178 | |
3071 | 2179 | とはいうものの、これは通常のリストを返す機構を使っています。 |
3072 | 2180 | ハッシュを渡そうとしたり、ハッシュを返そうとすると何が起きるのでしょうか? |
3073 | 2181 | そうです、引数の一つだけを使い引数の連結が行われることを気にしなければ |
3074 | 2182 | 通常の呼び出し規約と同じことです; ただし、少々高くつきます。 |
3075 | 2183 | |
3076 | 2184 | =begin original |
3077 | 2185 | |
3078 | 2186 | Where people get into trouble is here: |
3079 | 2187 | |
3080 | 2188 | =end original |
3081 | 2189 | |
3082 | 2190 | このように書くのはトラブルのもとです: |
3083 | 2191 | |
3084 | (@ | |
2192 | (@a, @b) = func(@c, @d); | |
3085 | 2193 | or |
3086 | (% | |
2194 | (%a, %b) = func(%c, %d); | |
3087 | 2195 | |
3088 | 2196 | =begin original |
3089 | 2197 | |
3090 | That syntax simply won't work. It sets just C<@ | |
2198 | That syntax simply won't work. It sets just C<@a> or C<%a> and | |
3091 | clears the C<@ | |
2199 | clears the C<@b> or C<%b>. Plus the function didn't get passed | |
3092 | 2200 | into two separate arrays or hashes: it got one long list in C<@_>, |
3093 | 2201 | as always. |
3094 | 2202 | |
3095 | 2203 | =end original |
3096 | 2204 | |
3097 | 2205 | これらの構文は単純にうまくいきません。 |
3098 | 戻り値は C<@ | |
2206 | 戻り値は C<@a>や C<%a>だけにセットされて、C<@b> や C<%b> はクリアされます。 | |
3099 | 2207 | それに加え、この関数は引数として二つの配列、二つのハッシュを受け取りません: |
3100 | 2208 | 受け取るのは常に C<@_> に格納されている(二つの引数の内容が連結された)一つの |
3101 | 2209 | 長いリストなのです。 |
3102 | 2210 | |
3103 | 2211 | =begin original |
3104 | 2212 | |
3105 | 2213 | If you can arrange for everyone to deal with this through references, it's |
3106 | 2214 | cleaner code, although not so nice to look at. Here's a function that |
3107 | 2215 | takes two array references as arguments, returning the two array elements |
3108 | 2216 | in order of how many elements they have in them: |
3109 | 2217 | |
3110 | 2218 | =end original |
3111 | 2219 | |
3112 | 2220 | これをリファレンス経由で扱うように変更できるのであれば、見た目はそれ程 |
3113 | 2221 | 良くありませんがプログラムを明確にできます。 |
3114 | 2222 | 以下の例は引数として配列のリファレンスを二つ取り、その配列の要素の数によって |
3115 | 2223 | 順序づけられた二つの配列を返す関数です: |
3116 | 2224 | |
3117 | ($ | |
2225 | ($aref, $bref) = func(\@c, \@d); | |
3118 | print "@$ | |
2226 | print "@$aref has more than @$bref\n"; | |
3119 | 2227 | sub func { |
3120 | ||
2228 | my ($cref, $dref) = @_; | |
3121 | ||
2229 | if (@$cref > @$dref) { | |
3122 | | |
2230 | return ($cref, $dref); | |
3123 | ||
2231 | } else { | |
3124 | | |
2232 | return ($dref, $cref); | |
3125 | ||
2233 | } | |
3126 | 2234 | } |
3127 | 2235 | |
3128 | 2236 | =begin original |
3129 | 2237 | |
3130 | 2238 | It turns out that you can actually do this also: |
3131 | 2239 | |
3132 | 2240 | =end original |
3133 | 2241 | |
3134 | 2242 | これは以下のように書くこともできます: |
3135 | 2243 | |
3136 | (* | |
2244 | (*a, *b) = func(\@c, \@d); | |
3137 | print "@ | |
2245 | print "@a has more than @b\n"; | |
3138 | 2246 | sub func { |
3139 | ||
2247 | local (*c, *d) = @_; | |
3140 | ||
2248 | if (@c > @d) { | |
3141 | | |
2249 | return (\@c, \@d); | |
3142 | ||
2250 | } else { | |
3143 | | |
2251 | return (\@d, \@c); | |
3144 | ||
2252 | } | |
3145 | 2253 | } |
3146 | 2254 | |
3147 | 2255 | =begin original |
3148 | 2256 | |
3149 | 2257 | Here we're using the typeglobs to do symbol table aliasing. It's |
3150 | 2258 | a tad subtle, though, and also won't work if you're using C<my> |
3151 | 2259 | variables, because only globals (even in disguise as C<local>s) |
3152 | 2260 | are in the symbol table. |
3153 | 2261 | |
3154 | 2262 | =end original |
3155 | 2263 | |
3156 | 2264 | この例では、シンボルテーブルの別名づけをするために型グロブを使っています。 |
3157 | 2265 | しかし、これは微妙なものであり、C<my> 変数を使った場合にはうまくいきません; |
3158 | 2266 | なぜなら、グローバルなもの(C<local()> で偽装したものを含む)だけが |
3159 | 2267 | シンボルテーブルにあるからです。 |
3160 | 2268 | |
3161 | 2269 | =begin original |
3162 | 2270 | |
3163 | 2271 | If you're passing around filehandles, you could usually just use the bare |
3164 | 2272 | typeglob, like C<*STDOUT>, but typeglobs references work, too. |
3165 | 2273 | For example: |
3166 | 2274 | |
3167 | 2275 | =end original |
3168 | 2276 | |
3169 | 2277 | ファイルハンドルを扱おうというのであれば、通常は C<*STDOUT> のような |
3170 | 2278 | 裸の型グロブ(bare typeglob)を使うことができますが、型グロブの |
3171 | 2279 | リファレンスも動作します。 |
3172 | 2280 | 例えば: |
3173 | 2281 | |
3174 | 2282 | splutter(\*STDOUT); |
3175 | 2283 | sub splutter { |
3176 | ||
2284 | my $fh = shift; | |
3177 | ||
2285 | print $fh "her um well a hmmm\n"; | |
3178 | 2286 | } |
3179 | 2287 | |
3180 | 2288 | $rec = get_rec(\*STDIN); |
3181 | 2289 | sub get_rec { |
3182 | ||
2290 | my $fh = shift; | |
3183 | ||
2291 | return scalar <$fh>; | |
3184 | 2292 | } |
3185 | 2293 | |
3186 | 2294 | =begin original |
3187 | 2295 | |
3188 | 2296 | If you're planning on generating new filehandles, you could do this. |
3189 | 2297 | Notice to pass back just the bare *FH, not its reference. |
3190 | 2298 | |
3191 | 2299 | =end original |
3192 | 2300 | |
3193 | 2301 | 新しいファイルハンドルを生成することを考えているのであれば、 |
3194 | 2302 | 以下のようにできます。 |
3195 | 2303 | リファレンスではなく、生の *FH を渡すことに注意してください。 |
3196 | 2304 | |
3197 | 2305 | sub openit { |
3198 | ||
2306 | my $path = shift; | |
3199 | ||
2307 | local *FH; | |
3200 | ||
2308 | return open (FH, $path) ? *FH : undef; | |
3201 | 2309 | } |
3202 | 2310 | |
3203 | 2311 | =head2 Prototypes |
3204 | 2312 | X<prototype> X<subroutine, prototype> |
3205 | 2313 | |
3206 | 2314 | (プロトタイプ) |
3207 | 2315 | |
3208 | 2316 | =begin original |
3209 | 2317 | |
3210 | 2318 | Perl supports a very limited kind of compile-time argument checking |
3211 | using function prototyping. | |
2319 | using function prototyping. If you declare | |
3212 | section or with a L<prototype attribute|attributes/Built-in Attributes>. | |
3213 | If you declare either of | |
3214 | 2320 | |
3215 | 2321 | =end original |
3216 | 2322 | |
3217 | 2323 | Perl はとても限られた形のコンパイル時引数チェックに対応しています。 |
3218 | ||
2324 | 以下のように宣言すると: | |
3219 | L<プロトタイプ属性|attributes/Built-in Attributes> で宣言できます。 | |
3220 | 以下のどちらかのように宣言すると: | |
3221 | 2325 | |
3222 | sub mypush ( | |
2326 | sub mypush (+@) | |
3223 | sub mypush :prototype(\@@) | |
3224 | 2327 | |
3225 | 2328 | =begin original |
3226 | 2329 | |
3227 | then C<mypush()> takes arguments exactly like C<push()> does. | |
2330 | then C<mypush()> takes arguments exactly like C<push()> does. The | |
3228 | ||
3229 | =end original | |
3230 | ||
3231 | C<mypush()> は C<push()> が取るのとまったく同じ引数を取ります。 | |
3232 | ||
3233 | =begin original | |
3234 | ||
3235 | If subroutine signatures are enabled (see L</Signatures>), then | |
3236 | the shorter PROTO syntax is unavailable, because it would clash with | |
3237 | signatures. In that case, a prototype can only be declared in the form | |
3238 | of an attribute. | |
3239 | ||
3240 | =end original | |
3241 | ||
3242 | サブルーチンシグネチャ(L</Signatures> 参照)が有効の場合、より短い | |
3243 | PROTO 文法は利用できません; シグネチャと衝突するからです。 | |
3244 | この場合、プロトタイプは属性の形式でのみ宣言できます。 | |
3245 | ||
3246 | =begin original | |
3247 | ||
3248 | The | |
3249 | 2331 | function declaration must be visible at compile time. The prototype |
3250 | affects only interpretation of | |
2332 | affects only interpretation of new-style calls to the function, | |
3251 | where | |
2333 | where new-style is defined as not using the C<&> character. In | |
3252 | 2334 | other words, if you call it like a built-in function, then it behaves |
3253 | like a built-in function. If you call it like an old-fashioned | |
2335 | like a built-in function. If you call it like an old-fashioned | |
3254 | 2336 | subroutine, then it behaves like an old-fashioned subroutine. It |
3255 | 2337 | naturally falls out from this rule that prototypes have no influence |
3256 | 2338 | on subroutine references like C<\&foo> or on indirect subroutine |
3257 | calls like C<&{$subref} | |
2339 | calls like C<&{$subref}> or C<< $subref->() >>. | |
3258 | 2340 | |
3259 | 2341 | =end original |
3260 | 2342 | |
2343 | C<mypush()> は C<push()> が取るのとまったく同じ引数を取ります。 | |
3261 | 2344 | 関数宣言はコンパイル時に可視でなければなりません。 |
3262 | プロトタイプの効果は、関数を C<&> を使わない | |
2345 | プロトタイプの効果は、関数を C<&> を使わない新しい形式の呼び出しで | |
3263 | 2346 | 解釈したときのみです。 |
3264 | 2347 | 言い換えれば、組み込み関数と同じように関数を呼び出せば、 |
3265 | 2348 | それは組み込み関数と同じように振る舞う、ということです。 |
3266 | 古い | |
2349 | 古い形式のサブルーチンと同じように呼び出せば、それは古い形式の | |
3267 | 2350 | サブルーチンと同じように振る舞います。 |
3268 | C<\&foo> のようなサブルーチンのリファレンスや C<&{$subref} | |
2351 | C<\&foo> のようなサブルーチンのリファレンスや C<&{$subref}> のような | |
3269 | 2352 | 間接的なサブルーチン呼び出し、C<< $subref->() >> に関してはプロトタイプは |
3270 | 2353 | なんの影響も及ぼさないので、この規則から外れます。 |
3271 | 2354 | |
3272 | 2355 | =begin original |
3273 | 2356 | |
3274 | 2357 | Method calls are not influenced by prototypes either, because the |
3275 | 2358 | function to be called is indeterminate at compile time, since |
3276 | 2359 | the exact code called depends on inheritance. |
3277 | 2360 | |
3278 | 2361 | =end original |
3279 | 2362 | |
3280 | 2363 | メソッド呼び出しはプロトタイプを行う/行わないによる影響を受けません; なぜなら |
3281 | 2364 | 正確な呼び出しコードは、継承に依存しているためにコンパイル時には |
3282 | 2365 | 不確定だからです。 |
3283 | 2366 | |
3284 | 2367 | =begin original |
3285 | 2368 | |
3286 | 2369 | Because the intent of this feature is primarily to let you define |
3287 | 2370 | subroutines that work like built-in functions, here are prototypes |
3288 | 2371 | for some other functions that parse almost exactly like the |
3289 | 2372 | corresponding built-in. |
3290 | 2373 | |
3291 | 2374 | =end original |
3292 | 2375 | |
3293 | 2376 | 組み込み関数のように動作するサブルーチンをあなたに定義させるのが |
3294 | 2377 | この機能の基本的な目的なので、ここで対応した組み込み関数のように |
3295 | 2378 | 解釈される幾つかの関数プロトタイプを挙げておきましょう。 |
3296 | 2379 | |
3297 | 2380 | =begin original |
3298 | 2381 | |
3299 | Declared as | |
2382 | Declared as Called as | |
3300 | 2383 | |
3301 | 2384 | =end original |
3302 | 2385 | |
3303 | | |
2386 | 宣言 呼び出し | |
3304 | 2387 | |
3305 | sub mylink ($$) | |
2388 | sub mylink ($$) mylink $old, $new | |
3306 | sub myvec ($$$) | |
2389 | sub myvec ($$$) myvec $var, $offset, 1 | |
3307 | sub myindex ($$;$) | |
2390 | sub myindex ($$;$) myindex &getstring, "substr" | |
3308 | sub mysyswrite ($$$;$) mysyswrite $buf, 0, length($buf) - $off, $off | |
2391 | sub mysyswrite ($$$;$) mysyswrite $buf, 0, length($buf) - $off, $off | |
3309 | sub myreverse (@) | |
2392 | sub myreverse (@) myreverse $a, $b, $c | |
3310 | sub myjoin ($@) | |
2393 | sub myjoin ($@) myjoin ":", $a, $b, $c | |
3311 | sub mypop ( | |
2394 | sub mypop (+) mypop @array | |
3312 | sub mysplice ( | |
2395 | sub mysplice (+$$@) mysplice @array, 0, 2, @pushme | |
3313 | sub mykeys ( | |
2396 | sub mykeys (+) mykeys %{$hashref} | |
3314 | sub myopen (*;$) | |
2397 | sub myopen (*;$) myopen HANDLE, $name | |
3315 | sub mypipe (**) | |
2398 | sub mypipe (**) mypipe READHANDLE, WRITEHANDLE | |
3316 | sub mygrep (&@) | |
2399 | sub mygrep (&@) mygrep { /foo/ } $a, $b, $c | |
3317 | sub myrand (;$) | |
2400 | sub myrand (;$) myrand 42 | |
3318 | sub mytime () | |
2401 | sub mytime () mytime | |
3319 | 2402 | |
3320 | 2403 | =begin original |
3321 | 2404 | |
3322 | 2405 | Any backslashed prototype character represents an actual argument |
3323 | 2406 | that must start with that character (optionally preceded by C<my>, |
3324 | 2407 | C<our> or C<local>), with the exception of C<$>, which will |
3325 | 2408 | accept any scalar lvalue expression, such as C<$foo = 7> or |
3326 | C<< my_function()->[0] >>. | |
2409 | C<< my_function()->[0] >>. The value passed as part of C<@_> will be a | |
3327 | 2410 | reference to the actual argument given in the subroutine call, |
3328 | 2411 | obtained by applying C<\> to that argument. |
3329 | 2412 | |
3330 | 2413 | =end original |
3331 | 2414 | |
3332 | 2415 | バックスラッシュが付けられたプロトタイプ文字は、実引数が |
3333 | 2416 | その文字で始まるものでなければならないことを表します |
3334 | 2417 | (オプションとして C<my>, C<our>, C<local> が前置されます); |
3335 | 2418 | C<$foo = 7> や C<< my_function()->[0] >> のようにマークなしでも任意の |
3336 | 2419 | 左辺値表現を受け付ける C<$> は例外です。 |
3337 | 2420 | C<@_> の一部として渡された引数は、そのサブルーチン呼び出しにおいて |
3338 | 2421 | 与えられた実引数に C<\> を適用したリファレンスとなります。 |
3339 | 2422 | |
3340 | 2423 | =begin original |
3341 | 2424 | |
3342 | 2425 | You can use the C<\[]> backslash group notation to specify more than one |
3343 | allowed argument type. | |
2426 | allowed argument type. For example: | |
3344 | 2427 | |
3345 | 2428 | =end original |
3346 | 2429 | |
3347 | 2430 | C<\[]> バックスラッシュ記法を、一つまたは複数の引数型を指定するために |
3348 | 2431 | 使えます。 |
3349 | 2432 | 例えば: |
3350 | 2433 | |
3351 | 2434 | sub myref (\[$@%&*]) |
3352 | 2435 | |
3353 | 2436 | =begin original |
3354 | 2437 | |
3355 | 2438 | will allow calling myref() as |
3356 | 2439 | |
3357 | 2440 | =end original |
3358 | 2441 | |
3359 | 2442 | というのは以下のように myref() を呼び出すのを許し: |
3360 | 2443 | |
3361 | 2444 | myref $var |
3362 | 2445 | myref @array |
3363 | 2446 | myref %hash |
3364 | 2447 | myref &sub |
3365 | 2448 | myref *glob |
3366 | 2449 | |
3367 | 2450 | =begin original |
3368 | 2451 | |
3369 | 2452 | and the first argument of myref() will be a reference to |
3370 | a scalar, an array, a hash, a | |
2453 | a scalar, an array, a hash, a code, or a glob. | |
3371 | 2454 | |
3372 | 2455 | =end original |
3373 | 2456 | |
3374 | myref() の最初の引数は スカラ、配列、ハッシュ、 | |
2457 | myref() の最初の引数は スカラ、配列、ハッシュ、コード、グロブのいずれかへの | |
3375 | ||
2458 | リファレンスです。 | |
3376 | 2459 | |
3377 | 2460 | =begin original |
3378 | 2461 | |
3379 | 2462 | Unbackslashed prototype characters have special meanings. Any |
3380 | 2463 | unbackslashed C<@> or C<%> eats all remaining arguments, and forces |
3381 | 2464 | list context. An argument represented by C<$> forces scalar context. An |
3382 | 2465 | C<&> requires an anonymous subroutine, which, if passed as the first |
3383 | argument, | |
2466 | argument, does not require the C<sub> keyword or a subsequent comma. | |
3384 | or a subsequent comma. | |
3385 | 2467 | |
3386 | 2468 | =end original |
3387 | 2469 | |
3388 | 2470 | バックスラッシュが付けられていない文字は特別な意味を持っています。 |
3389 | 2471 | バックスラッシュを付けられていない すべての C<@> とC<%> は残りの引数全てを |
3390 | 2472 | 取ってしまい、さらにリストコンテキストを強制します。 |
3391 | 2473 | C<$> で表される引数はスカラコンテキストを強制されます。 |
3392 | ||
2474 | 第一引数として渡された場合、C<&> は C<sub> キーワードや連続したカンマを | |
3393 | 無名サブルーチンを要求します | |
2475 | 要求しないような無名サブルーチンを要求します。 | |
3394 | これは C<sub> キーワードや連続したカンマを要求しません。 | |
3395 | 2476 | |
3396 | 2477 | =begin original |
3397 | 2478 | |
3398 | 2479 | A C<*> allows the subroutine to accept a bareword, constant, scalar expression, |
3399 | 2480 | typeglob, or a reference to a typeglob in that slot. The value will be |
3400 | 2481 | available to the subroutine either as a simple scalar, or (in the latter |
3401 | 2482 | two cases) as a reference to the typeglob. If you wish to always convert |
3402 | 2483 | such arguments to a typeglob reference, use Symbol::qualify_to_ref() as |
3403 | 2484 | follows: |
3404 | 2485 | |
3405 | 2486 | =end original |
3406 | 2487 | |
3407 | 2488 | C<*> は、スロットにある裸の単語、定数、スカラ式、型グロブ、 |
3408 | 2489 | 型グロブに対するリファレンスを許しています。 |
3409 | 2490 | その値はサブルーチンにとって単純なスカラとすることも |
3410 | 2491 | 型グロブに対するリファレンスにもなります。 |
3411 | 2492 | そのような引数を常に型グロブリファレンスに変換したい場合は、 |
3412 | 2493 | Symbol::qualify_to_ref() を以下のように使います: |
3413 | 2494 | |
3414 | 2495 | use Symbol 'qualify_to_ref'; |
3415 | 2496 | |
3416 | 2497 | sub foo (*) { |
3417 | ||
2498 | my $fh = qualify_to_ref(shift, caller); | |
3418 | ||
2499 | ... | |
3419 | 2500 | } |
3420 | 2501 | |
3421 | 2502 | =begin original |
3422 | 2503 | |
3423 | 2504 | The C<+> prototype is a special alternative to C<$> that will act like |
3424 | 2505 | C<\[@%]> when given a literal array or hash variable, but will otherwise |
3425 | 2506 | force scalar context on the argument. This is useful for functions which |
3426 | 2507 | should accept either a literal array or an array reference as the argument: |
3427 | 2508 | |
3428 | 2509 | =end original |
3429 | 2510 | |
3430 | 2511 | C<+> プロトタイプは C<$> の特殊な代替物で、リテラルな配列やハッシュ変数が |
3431 | 2512 | 与えられると C<\[@%]> のように動作しますが、さもなければ引数に |
3432 | 2513 | スカラコンテキストを強制します。 |
3433 | 2514 | これは引数としてリテラルの配列または配列リファレンスのどちらかを |
3434 | 2515 | 受け付けるような関数に有用です: |
3435 | 2516 | |
3436 | 2517 | sub mypush (+@) { |
3437 | 2518 | my $aref = shift; |
3438 | 2519 | die "Not an array or arrayref" unless ref $aref eq 'ARRAY'; |
3439 | 2520 | push @$aref, @_; |
3440 | 2521 | } |
3441 | 2522 | |
3442 | 2523 | =begin original |
3443 | 2524 | |
3444 | 2525 | When using the C<+> prototype, your function must check that the argument |
3445 | 2526 | is of an acceptable type. |
3446 | 2527 | |
3447 | 2528 | =end original |
3448 | 2529 | |
3449 | 2530 | 関数で C<+> プロトタイプを使ったとき、引数が受け付けられる型かを |
3450 | 2531 | チェックしなければなりません。 |
3451 | 2532 | |
3452 | 2533 | =begin original |
3453 | 2534 | |
3454 | 2535 | A semicolon (C<;>) separates mandatory arguments from optional arguments. |
3455 | 2536 | It is redundant before C<@> or C<%>, which gobble up everything else. |
3456 | 2537 | |
3457 | 2538 | =end original |
3458 | 2539 | |
3459 | 2540 | セミコロン (C<;>) は、必須の引数と省略可能な引数とを分割します。 |
3460 | 2541 | C<@> や C<%> の前ではこれは冗長です; その他のものを吸収します。 |
3461 | 2542 | |
3462 | 2543 | =begin original |
3463 | 2544 | |
3464 | 2545 | As the last character of a prototype, or just before a semicolon, a C<@> |
3465 | 2546 | or a C<%>, you can use C<_> in place of C<$>: if this argument is not |
3466 | 2547 | provided, C<$_> will be used instead. |
3467 | 2548 | |
3468 | 2549 | =end original |
3469 | 2550 | |
3470 | 2551 | プロトタイプの最後の文字、あるいはセミコロン、C<@>、C<%> の直前に、C<$> の |
3471 | 2552 | 代わりに C<_> を使えます: 引数が与えられない場合、C<$_> が代わりに |
3472 | 2553 | 使われます。 |
3473 | 2554 | |
3474 | 2555 | =begin original |
3475 | 2556 | |
3476 | 2557 | Note how the last three examples in the table above are treated |
3477 | 2558 | specially by the parser. C<mygrep()> is parsed as a true list |
3478 | 2559 | operator, C<myrand()> is parsed as a true unary operator with unary |
3479 | 2560 | precedence the same as C<rand()>, and C<mytime()> is truly without |
3480 | 2561 | arguments, just like C<time()>. That is, if you say |
3481 | 2562 | |
3482 | 2563 | =end original |
3483 | 2564 | |
3484 | 2565 | 上の例の最後の三つのものは、構文解析器が特別扱いするということに |
3485 | 2566 | 注意してください。 |
3486 | 2567 | C<mygrep()> は本当のリスト演算子として解析され、C<myrand()> は C<rand()> と |
3487 | 2568 | 同じく単項演算子の優先順位を持った真の単項演算子として、 |
3488 | 2569 | そして C<mytime()> は C<time()> と同じ様な引数を取らないものとして |
3489 | 2570 | 解析されます。つまり、 |
3490 | 2571 | |
3491 | 2572 | mytime +2; |
3492 | 2573 | |
3493 | 2574 | =begin original |
3494 | 2575 | |
3495 | 2576 | you'll get C<mytime() + 2>, not C<mytime(2)>, which is how it would be parsed |
3496 | 2577 | without a prototype. If you want to force a unary function to have the |
3497 | 2578 | same precedence as a list operator, add C<;> to the end of the prototype: |
3498 | 2579 | |
3499 | 2580 | =end original |
3500 | 2581 | |
3501 | 2582 | とすると、プロトタイプなしの場合にパースされる C<mytime(2)> ではなく、 |
3502 | 2583 | C<mytime() + 2> となります。 |
3503 | 2584 | 単項関数がリスト演算子と同じ優先順位を持つようにさせたいなら、プロトタイプの |
3504 | 2585 | 末尾に C<;> を追加してください: |
3505 | 2586 | |
3506 | 2587 | sub mygetprotobynumber($;); |
3507 | mygetprotobynumber $ | |
2588 | mygetprotobynumber $a > $b; # parsed as mygetprotobynumber($a > $b) | |
3508 | 2589 | |
3509 | 2590 | =begin original |
3510 | 2591 | |
3511 | 2592 | The interesting thing about C<&> is that you can generate new syntax with it, |
3512 | 2593 | provided it's in the initial position: |
3513 | 2594 | X<&> |
3514 | 2595 | |
3515 | 2596 | =end original |
3516 | 2597 | |
3517 | 2598 | C<&> に関して興味深いことは、初期位置を使って新しい構文を |
3518 | 2599 | 生成できるということです: |
3519 | 2600 | X<&> |
3520 | 2601 | |
3521 | 2602 | sub try (&@) { |
3522 | ||
2603 | my($try,$catch) = @_; | |
3523 | ||
2604 | eval { &$try }; | |
3524 | ||
2605 | if ($@) { | |
3525 | ||
2606 | local $_ = $@; | |
3526 | ||
2607 | &$catch; | |
3527 | ||
2608 | } | |
3528 | 2609 | } |
3529 | 2610 | sub catch (&) { $_[0] } |
3530 | 2611 | |
3531 | 2612 | try { |
3532 | ||
2613 | die "phooey"; | |
3533 | 2614 | } catch { |
3534 | ||
2615 | /phooey/ and print "unphooey\n"; | |
3535 | 2616 | }; |
3536 | 2617 | |
3537 | 2618 | =begin original |
3538 | 2619 | |
3539 | 2620 | That prints C<"unphooey">. (Yes, there are still unresolved |
3540 | 2621 | issues having to do with visibility of C<@_>. I'm ignoring that |
3541 | 2622 | question for the moment. (But note that if we make C<@_> lexically |
3542 | 2623 | scoped, those anonymous subroutines can act like closures... (Gee, |
3543 | 2624 | is this sounding a little Lispish? (Never mind.)))) |
3544 | 2625 | |
3545 | 2626 | =end original |
3546 | 2627 | |
3547 | 2628 | これは C<“unphooey”> を出力します。 |
3548 | 2629 | (そう、C<@_> の可視性に関して解決していないことがあります。 |
3549 | 2630 | 現時点ではこれを無視します。 |
3550 | 2631 | (ただし、C<@_> をレキシカルスコープにすれば上記のサブルーチンはクロージャーの |
3551 | 2632 | ように振る舞うことができます... |
3552 | 2633 | (んー、これってちょーっと Lisp っぽいかな? (気にしないでね。)))) |
3553 | 2634 | |
3554 | 2635 | =begin original |
3555 | 2636 | |
3556 | 2637 | And here's a reimplementation of the Perl C<grep> operator: |
3557 | 2638 | X<grep> |
3558 | 2639 | |
3559 | 2640 | =end original |
3560 | 2641 | |
3561 | 2642 | 以下の例は Perl の C<grep> 演算子のの再実装です: |
3562 | 2643 | X<grep> |
3563 | 2644 | |
3564 | 2645 | sub mygrep (&@) { |
3565 | ||
2646 | my $code = shift; | |
3566 | ||
2647 | my @result; | |
3567 | ||
2648 | foreach $_ (@_) { | |
3568 | ||
2649 | push(@result, $_) if &$code; | |
3569 | ||
2650 | } | |
3570 | ||
2651 | @result; | |
3571 | 2652 | } |
3572 | 2653 | |
3573 | 2654 | =begin original |
3574 | 2655 | |
3575 | 2656 | Some folks would prefer full alphanumeric prototypes. Alphanumerics have |
3576 | 2657 | been intentionally left out of prototypes for the express purpose of |
3577 | 2658 | someday in the future adding named, formal parameters. The current |
3578 | 2659 | mechanism's main goal is to let module writers provide better diagnostics |
3579 | 2660 | for module users. Larry feels the notation quite understandable to Perl |
3580 | 2661 | programmers, and that it will not intrude greatly upon the meat of the |
3581 | 2662 | module, nor make it harder to read. The line noise is visually |
3582 | 2663 | encapsulated into a small pill that's easy to swallow. |
3583 | 2664 | |
3584 | 2665 | =end original |
3585 | 2666 | |
3586 | 2667 | 一部の人は、完全に英数字を使ったプロトタイプを好むかもしれません。 |
3587 | 2668 | 英数字は、将来名前付き仮引数を追加するときのために意図的に使わずに |
3588 | 2669 | 残してあるのです。 |
3589 | 2670 | 現時点でのプロトタイプの機構の主な目的はモジュール作者がそのユーザーに |
3590 | 2671 | 対してより良い診断メッセージを提供させるようにするためのものなのです。 |
3591 | 2672 | この記法は Perl プログラマが非常に理解しやすく、モジュールの中身に |
3592 | 2673 | 進入するようなことも読みづらくしたりすることもないと Larry は考えています。 |
3593 | 2674 | 回線の雑音は飲み込みやすい小さな丸薬に視覚的に押し込められるのです。 |
3594 | 2675 | |
3595 | 2676 | =begin original |
3596 | 2677 | |
3597 | 2678 | If you try to use an alphanumeric sequence in a prototype you will |
3598 | 2679 | generate an optional warning - "Illegal character in prototype...". |
3599 | 2680 | Unfortunately earlier versions of Perl allowed the prototype to be |
3600 | 2681 | used as long as its prefix was a valid prototype. The warning may be |
3601 | 2682 | upgraded to a fatal error in a future version of Perl once the |
3602 | 2683 | majority of offending code is fixed. |
3603 | 2684 | |
3604 | 2685 | =end original |
3605 | 2686 | |
3606 | 2687 | もしプロトタイプに英数字を使おうとすると、オプションの警告 |
3607 | 2688 | "Illegal character in prototype..." が生成されます。 |
3608 | 2689 | 残念ながら、過去のバージョンの Perl では、有効なプロトタイプが前置されてさえ |
3609 | 2690 | いれば、英数字を含むプロトタイプも認められていました。 |
3610 | 2691 | この警告は、目障りなコードの大部分が修正されたなら、将来のバージョンの |
3611 | 2692 | Perl で致命的エラーに格上げされるかもしれません。 |
3612 | 2693 | |
3613 | 2694 | =begin original |
3614 | 2695 | |
3615 | 2696 | It's probably best to prototype new functions, not retrofit prototyping |
3616 | 2697 | into older ones. That's because you must be especially careful about |
3617 | 2698 | silent impositions of differing list versus scalar contexts. For example, |
3618 | 2699 | if you decide that a function should take just one parameter, like this: |
3619 | 2700 | |
3620 | 2701 | =end original |
3621 | 2702 | |
3622 | 2703 | 新しい関数にプロトタイプを付けるのがおそらく最善であり、古い関数に対して |
3623 | 2704 | プロトタイプを付けることはよくありません。 |
3624 | 2705 | なぜなら、(プロトタイプを付けたことによって)リストコンテキストと |
3625 | 2706 | スカラコンテキストを黙って変えてしまうようなことに関して |
3626 | 2707 | 特に注意しなければならないからです。 |
3627 | 2708 | たとえば以下の例のようなただ一つの引数を取ると決めた関数を考えてみましょう: |
3628 | 2709 | |
3629 | 2710 | sub func ($) { |
3630 | ||
2711 | my $n = shift; | |
3631 | ||
2712 | print "you gave me $n\n"; | |
3632 | 2713 | } |
3633 | 2714 | |
3634 | 2715 | =begin original |
3635 | 2716 | |
3636 | 2717 | and someone has been calling it with an array or expression |
3637 | 2718 | returning a list: |
3638 | 2719 | |
3639 | 2720 | =end original |
3640 | 2721 | |
3641 | 2722 | そして、誰かがこの関数をリストを返すような配列や式を引数として |
3642 | 2723 | 渡したとすると: |
3643 | 2724 | |
3644 | 2725 | func(@foo); |
3645 | func( | |
2726 | func( split /:/ ); | |
3646 | 2727 | |
3647 | 2728 | =begin original |
3648 | 2729 | |
3649 | 2730 | Then you've just supplied an automatic C<scalar> in front of their |
3650 | 2731 | argument, which can be more than a bit surprising. The old C<@foo> |
3651 | 2732 | which used to hold one thing doesn't get passed in. Instead, |
3652 | 2733 | C<func()> now gets passed in a C<1>; that is, the number of elements |
3653 | in C<@foo>. And the C< | |
2734 | in C<@foo>. And the C<split> gets called in scalar context so it | |
3654 | ||
2735 | starts scribbling on your C<@_> parameter list. Ouch! | |
3655 | 2736 | |
3656 | 2737 | =end original |
3657 | 2738 | |
3658 | 2739 | 自動的に C<scalar> が引数の前に(こっそりと)付けられます; これには |
3659 | 2740 | いささかびっくりすることになるかもしれません。 |
3660 | 2741 | これまでそうであったような、要素を持った C<@foo> が渡されることはありません。 |
3661 | 2742 | その代わり、C<func()> は C<1>、つまり C<@foo> の要素の数を得るようになります。 |
3662 | そして C< | |
2743 | そして C<split> はスカラコンテキストで呼び出され、パラメータリスト | |
3663 | ||
2744 | C<@_> に落書きを始めてしまいます。 | |
3664 | 2745 | あいたた。 |
3665 | 2746 | |
3666 | 2747 | =begin original |
3667 | 2748 | |
3668 | If a sub has both a PROTO and a BLOCK, the prototype is not applied | |
3669 | until after the BLOCK is completely defined. This means that a recursive | |
3670 | function with a prototype has to be predeclared for the prototype to take | |
3671 | effect, like so: | |
3672 | ||
3673 | =end original | |
3674 | ||
3675 | サブルーチンに PROTO と BLOCK の両方がある場合、プロトタイプは BLOCK が | |
3676 | 完全に定義されるまで適用されません。 | |
3677 | これは、プロトタイプを持つ再帰関数は、次のように、プロトタイプが | |
3678 | 有効になるように事前定義する必要があります: | |
3679 | ||
3680 | sub foo($$); | |
3681 | sub foo($$) { | |
3682 | foo 1, 2; | |
3683 | } | |
3684 | ||
3685 | =begin original | |
3686 | ||
3687 | 2749 | This is all very powerful, of course, and should be used only in moderation |
3688 | 2750 | to make the world a better place. |
3689 | 2751 | |
3690 | 2752 | =end original |
3691 | 2753 | |
3692 | 2754 | もちろんこれは十分強力なものであり、世界をより良い場所にするという目的に |
3693 | 2755 | 限って使用すべきものでしょう。 |
3694 | 2756 | |
3695 | 2757 | =head2 Constant Functions |
3696 | 2758 | X<constant> |
3697 | 2759 | |
3698 | 2760 | (定数関数) |
3699 | 2761 | |
3700 | 2762 | =begin original |
3701 | 2763 | |
3702 | 2764 | Functions with a prototype of C<()> are potential candidates for |
3703 | 2765 | inlining. If the result after optimization and constant folding |
3704 | 2766 | is either a constant or a lexically-scoped scalar which has no other |
3705 | 2767 | references, then it will be used in place of function calls made |
3706 | 2768 | without C<&>. Calls made using C<&> are never inlined. (See |
3707 | ||
2769 | F<constant.pm> for an easy way to declare most constants.) | |
3708 | 2770 | |
3709 | 2771 | =end original |
3710 | 2772 | |
3711 | 2773 | C<()> というプロトタイプを持った関数はインライン展開される可能性を |
3712 | 2774 | 持っています。 |
3713 | 2775 | 最適化と定数畳み込み後の結果が一つの定数か、何のリファレンスも持たない |
3714 | 2776 | レキシカルスコープのスカラであったならば、その関数が C<&> を使わずに |
3715 | 2777 | 呼びされたときに呼び出しのその場に置かれます。 |
3716 | 2778 | C<&> を使って呼び出された関数は決してインライン展開されることは |
3717 | 2779 | ありません。 |
3718 | (ほとんどの定数を宣言するための簡単な方法は | |
2780 | (ほとんどの定数を宣言するための簡単な方法は F<constant.pm> を | |
3719 | 2781 | 参照してください。) |
3720 | 2782 | |
3721 | 2783 | =begin original |
3722 | 2784 | |
3723 | 2785 | The following functions would all be inlined: |
3724 | 2786 | |
3725 | 2787 | =end original |
3726 | 2788 | |
3727 | 2789 | 以下に挙げた関数は全てインライン展開されるでしょう: |
3728 | 2790 | |
3729 | 2791 | =begin original |
3730 | 2792 | |
3731 | sub pi () | |
2793 | sub pi () { 3.14159 } # Not exact, but close. | |
3732 | sub PI () | |
2794 | sub PI () { 4 * atan2 1, 1 } # As good as it gets, | |
3733 | ||
2795 | # and it's inlined, too! | |
3734 | sub ST_DEV () | |
2796 | sub ST_DEV () { 0 } | |
3735 | sub ST_INO () | |
2797 | sub ST_INO () { 1 } | |
3736 | 2798 | |
3737 | 2799 | =end original |
3738 | 2800 | |
3739 | sub pi () | |
2801 | sub pi () { 3.14159 } # 正確ではないが近い。 | |
3740 | sub PI () | |
2802 | sub PI () { 4 * atan2 1, 1 } # 可能な限り良いもの | |
3741 | ||
2803 | # そしてこれも展開されます! | |
3742 | sub ST_DEV () | |
2804 | sub ST_DEV () { 0 } | |
3743 | sub ST_INO () | |
2805 | sub ST_INO () { 1 } | |
3744 | 2806 | |
3745 | sub FLAG_FOO () | |
2807 | sub FLAG_FOO () { 1 << 8 } | |
3746 | sub FLAG_BAR () | |
2808 | sub FLAG_BAR () { 1 << 9 } | |
3747 | sub FLAG_MASK () | |
2809 | sub FLAG_MASK () { FLAG_FOO | FLAG_BAR } | |
3748 | 2810 | |
3749 | sub OPT_BAZ () | |
2811 | sub OPT_BAZ () { not (0x1B58 & FLAG_MASK) } | |
3750 | 2812 | |
3751 | 2813 | sub N () { int(OPT_BAZ) / 3 } |
3752 | 2814 | |
3753 | 2815 | sub FOO_SET () { 1 if FLAG_MASK & FLAG_FOO } |
3754 | sub FOO_SET2 () { if (FLAG_MASK & FLAG_FOO) { 1 } } | |
3755 | 2816 | |
3756 | 2817 | =begin original |
3757 | 2818 | |
3758 | ||
2819 | Be aware that these will not be inlined; as they contain inner scopes, | |
3759 | e | |
2820 | the constant folding doesn't reduce them to a single constant: | |
3760 | inner scopes.) You can countermand inlining by using an explicit | |
3761 | C<return>: | |
3762 | 2821 | |
3763 | 2822 | =end original |
3764 | 2823 | |
3765 | ||
2824 | 以下のものはインライン展開されないことに注意してください; | |
3766 | ||
2825 | より内側にスコープがあるので、畳み込まれた定数が一つの定数に削減できません: | |
3767 | 振る舞いをしません。) | |
3768 | インライン化は明示的な C<return> を使うことで撤回できます: | |
3769 | 2826 | |
2827 | sub foo_set () { if (FLAG_MASK & FLAG_FOO) { 1 } } | |
2828 | ||
3770 | 2829 | sub baz_val () { |
3771 | ||
2830 | if (OPT_BAZ) { | |
3772 | ||
2831 | return 23; | |
3773 | ||
2832 | } | |
3774 | ||
2833 | else { | |
3775 | ||
2834 | return 42; | |
3776 | ||
2835 | } | |
3777 | 2836 | } |
3778 | sub bonk_val () { return 12345 } | |
3779 | 2837 | |
3780 | 2838 | =begin original |
3781 | 2839 | |
3782 | ||
2840 | If you redefine a subroutine that was eligible for inlining, you'll get | |
3783 | ||
2841 | a warning by default. (You can use this warning to tell whether or not a | |
3784 | ||
2842 | particular subroutine is considered constant.) The warning is | |
2843 | considered severe enough not to be affected by the B<-w> | |
2844 | switch (or its absence) because previously compiled | |
2845 | invocations of the function will still be using the old value of the | |
2846 | function. If you need to be able to redefine the subroutine, you need to | |
2847 | ensure that it isn't inlined, either by dropping the C<()> prototype | |
2848 | (which changes calling semantics, so beware) or by thwarting the | |
2849 | inlining mechanism in some other way, such as | |
3785 | 2850 | |
3786 | 2851 | =end original |
3787 | 2852 | |
3788 | 先に暗示したように、本体がその他にリファレンスを持たないレキシカル | |
3789 | スコープのスカラで構成されている場合、BEGIN の時点でインラインサブルーチンを | |
3790 | 動的に宣言することもできます。 | |
3791 | ここでの最初の例のみがインライン化されます: | |
3792 | ||
3793 | BEGIN { | |
3794 | my $var = 1; | |
3795 | no strict 'refs'; | |
3796 | *INLINED = sub () { $var }; | |
3797 | } | |
3798 | ||
3799 | BEGIN { | |
3800 | my $var = 1; | |
3801 | my $ref = \$var; | |
3802 | no strict 'refs'; | |
3803 | *NOT_INLINED = sub () { $var }; | |
3804 | } | |
3805 | ||
3806 | =begin original | |
3807 | ||
3808 | A not so obvious caveat with this (see [RT #79908]) is what happens if the | |
3809 | variable is potentially modifiable. For example: | |
3810 | ||
3811 | =end original | |
3812 | ||
3813 | これに関するそれほど明白でない欠陥 ([RT #79908] 参照) は、変数が | |
3814 | 潜在的に変更可能であるときに起きることです。 | |
3815 | 例えば: | |
3816 | ||
3817 | BEGIN { | |
3818 | my $x = 10; | |
3819 | *FOO = sub () { $x }; | |
3820 | $x++; | |
3821 | } | |
3822 | print FOO(); # printed 10 prior to 5.32.0 | |
3823 | ||
3824 | =begin original | |
3825 | ||
3826 | From Perl 5.22 onwards this gave a deprecation warning, and from Perl 5.32 | |
3827 | onwards it became a run-time error. Previously the variable was | |
3828 | immediately inlined, and stopped behaving like a normal lexical variable; | |
3829 | so it printed C<10>, not C<11>. | |
3830 | ||
3831 | =end original | |
3832 | ||
3833 | Perl 5.22 から、これは廃止予定警告を出力し、 | |
3834 | Perl 5.32 から、致命的エラーになりました。 | |
3835 | 以前はこの変数は直ちにインライン化され、 | |
3836 | 通常のレキシカル変数のような振る舞いを止めていました; | |
3837 | 従ってこれは C<11> ではなく C<10> を表示していました。 | |
3838 | ||
3839 | =begin original | |
3840 | ||
3841 | If you still want such a subroutine to be inlined (with no warning), make | |
3842 | sure the variable is not used in a context where it could be modified | |
3843 | aside from where it is declared. | |
3844 | ||
3845 | =end original | |
3846 | ||
3847 | まだそのようなサブルーチンを (警告なしで) インライン化したい場合、 | |
3848 | その変数が宣言された | |
3849 | 場所以外で変更されるかも知れないコンテキストで使われないことを | |
3850 | 確認してください。 | |
3851 | ||
3852 | # Fine, no warning | |
3853 | BEGIN { | |
3854 | my $x = 54321; | |
3855 | *INLINED = sub () { $x }; | |
3856 | } | |
3857 | # Error | |
3858 | BEGIN { | |
3859 | my $x; | |
3860 | $x = 54321; | |
3861 | *ALSO_INLINED = sub () { $x }; | |
3862 | } | |
3863 | ||
3864 | =begin original | |
3865 | ||
3866 | Perl 5.22 also introduced the "const" attribute as an alternative. It was | |
3867 | initially experimental, but made stable in Perl 5.40. When applied to an | |
3868 | anonymous subroutine, it forces the sub to be called when the C<sub> | |
3869 | expression is evaluated. The return value is captured and turned into a | |
3870 | constant subroutine: | |
3871 | ||
3872 | =end original | |
3873 | ||
3874 | Perl 5.22 ではまた代替案として "const" 属性を導入しました。 | |
3875 | これは当初実験的でしたが、Perl 5.40 で安定的になりました。 | |
3876 | 無名サブルーチンに適用されると、サブルーチンを C<sub> 式が評価されたときの | |
3877 | 呼び出しに固定します。 | |
3878 | 返り値は捕捉され、定数サブルーチンに変換されます: | |
3879 | ||
3880 | my $x = 54321; | |
3881 | *INLINED = sub : const { $x }; | |
3882 | $x++; | |
3883 | ||
3884 | =begin original | |
3885 | ||
3886 | The return value of C<INLINED> in this example will always be 54321, | |
3887 | regardless of later modifications to $x. You can also put any arbitrary | |
3888 | code inside the sub, at it will be executed immediately and its return | |
3889 | value captured the same way. | |
3890 | ||
3891 | =end original | |
3892 | ||
3893 | この例での C<INLINED> の返り値は、その後 $x が変更されても常に 54321 です。 | |
3894 | また、サブルーチンの内部で任意のコードを書くことができ、直ちに実行されて | |
3895 | その返り値は同様に捕捉されます。 | |
3896 | ||
3897 | =begin original | |
3898 | ||
3899 | If you really want a subroutine with a C<()> prototype that returns a | |
3900 | lexical variable you can easily force it to not be inlined by adding | |
3901 | an explicit C<return>: | |
3902 | ||
3903 | =end original | |
3904 | ||
3905 | C<()> プロトタイプを持ち、レキシカル変数を返すサブルーチンが本当にほしいなら、 | |
3906 | 明示的な C<return> を追加することでインライン化しないことを簡単に強制できます: | |
3907 | ||
3908 | BEGIN { | |
3909 | my $x = 10; | |
3910 | *FOO = sub () { return $x }; | |
3911 | $x++; | |
3912 | } | |
3913 | print FOO(); # prints 11 | |
3914 | ||
3915 | =begin original | |
3916 | ||
3917 | The easiest way to tell if a subroutine was inlined is by using | |
3918 | L<B::Deparse>. Consider this example of two subroutines returning | |
3919 | C<1>, one with a C<()> prototype causing it to be inlined, and one | |
3920 | without (with deparse output truncated for clarity): | |
3921 | ||
3922 | =end original | |
3923 | ||
3924 | サブルーチンがインライン化されていることを知る最も簡単な方法は | |
3925 | L<B::Deparse> によるものです。 | |
3926 | C<1> を返す二つのサブルーチンの例を考えると、 | |
3927 | C<()> プロトタイプを持つものはインライン化を引き起こし、もう一つは | |
3928 | 引き起こしません (明確化のために deparse の出力を切り詰めています): | |
3929 | ||
3930 | $ perl -MO=Deparse -e 'sub ONE { 1 } if (ONE) { print ONE if ONE }' | |
3931 | sub ONE { | |
3932 | 1; | |
3933 | } | |
3934 | if (ONE ) { | |
3935 | print ONE() if ONE ; | |
3936 | } | |
3937 | ||
3938 | $ perl -MO=Deparse -e 'sub ONE () { 1 } if (ONE) { print ONE if ONE }' | |
3939 | sub ONE () { 1 } | |
3940 | do { | |
3941 | print 1 | |
3942 | }; | |
3943 | ||
3944 | =begin original | |
3945 | ||
3946 | If you redefine a subroutine that was eligible for inlining, you'll | |
3947 | get a warning by default. You can use this warning to tell whether or | |
3948 | not a particular subroutine is considered inlinable, since it's | |
3949 | different than the warning for overriding non-inlined subroutines: | |
3950 | ||
3951 | =end original | |
3952 | ||
3953 | 2853 | インライン展開するのに適切であったサブルーチンを再定義するとデフォルトでは |
3954 | 2854 | 警告を受けることになります。 |
3955 | この警告を、あるサブルーチンが | |
2855 | (この警告を、あるサブルーチンが定数サブルーチンとして認識されるかどうかを | |
3956 | 区別するために使うことができます | |
2856 | 区別するために使うことができます。) | |
3957 | ||
2857 | 以前にコンパイルした関数の起動によってこの関数の古い値を使い続けるので、 | |
3958 | ||
3959 | $ perl -e 'sub one () {1} sub one () {2}' | |
3960 | Constant subroutine one redefined at -e line 1. | |
3961 | $ perl -we 'sub one {1} sub one {2}' | |
3962 | Subroutine one redefined at -e line 1. | |
3963 | ||
3964 | =begin original | |
3965 | ||
3966 | The warning is considered severe enough not to be affected by the | |
3967 | B<-w> switch (or its absence) because previously compiled invocations | |
3968 | of the function will still be using the old value of the function. If | |
3969 | you need to be able to redefine the subroutine, you need to ensure | |
3970 | that it isn't inlined, either by dropping the C<()> prototype (which | |
3971 | changes calling semantics, so beware) or by thwarting the inlining | |
3972 | mechanism in some other way, e.g. by adding an explicit C<return>, as | |
3973 | mentioned above: | |
3974 | ||
3975 | =end original | |
3976 | ||
3977 | 2858 | この警告は B<-w> オプション(またはそれがないこと)に影響を受けるものに |
3978 | 2859 | するには重大すぎると考えられました。 |
3979 | 2860 | そのサブルーチンを再定義できる必要があるのならば、C<()> プロトタイプを |
3980 | 2861 | やめる(これは呼び出しのセマンティクスを変えてしまうので注意してください)か、 |
3981 | ||
2862 | 以下の例のようにしてインライン展開機構を | |
3982 | ||
2863 | 働かせないようにしてサブルーチンがインライン展開されていないことを | |
3983 | ||
2864 | 保証する必要があります。 | |
3984 | 2865 | |
3985 | sub not_inlined () { | |
2866 | sub not_inlined () { | |
2867 | 23 if $]; | |
2868 | } | |
3986 | 2869 | |
3987 | 2870 | =head2 Overriding Built-in Functions |
3988 | 2871 | X<built-in> X<override> X<CORE> X<CORE::GLOBAL> |
3989 | 2872 | |
3990 | 2873 | (組み込み関数のオーバーライド) |
3991 | 2874 | |
3992 | 2875 | =begin original |
3993 | 2876 | |
3994 | 2877 | Many built-in functions may be overridden, though this should be tried |
3995 | 2878 | only occasionally and for good reason. Typically this might be |
3996 | 2879 | done by a package attempting to emulate missing built-in functionality |
3997 | 2880 | on a non-Unix system. |
3998 | 2881 | |
3999 | 2882 | =end original |
4000 | 2883 | |
4001 | 2884 | 多くの組み込み関数はオーバーライドすることができます; ただし、これを行うのは、 |
4002 | 2885 | そうする理由と状況とがあるときのみに限るべきでしょう。 |
4003 | 2886 | これが行われる典型的な例は、非 UNIX システムにおいて欠けている組み込み機能を |
4004 | 2887 | エミュレートするためにパッケージを使おうとする場合でしょう。 |
4005 | 2888 | |
4006 | 2889 | =begin original |
4007 | 2890 | |
4008 | 2891 | Overriding may be done only by importing the name from a module at |
4009 | 2892 | compile time--ordinary predeclaration isn't good enough. However, the |
4010 | 2893 | C<use subs> pragma lets you, in effect, predeclare subs |
4011 | 2894 | via the import syntax, and these names may then override built-in ones: |
4012 | 2895 | |
4013 | 2896 | =end original |
4014 | 2897 | |
4015 | 2898 | オーバーライドはコンパイル時にモジュールからのみ名前を import できます-- |
4016 | 2899 | 通常の先行宣言では十分ではありません。 |
4017 | 2900 | しかしながら、C<use subs> プラグマは import 構文を通して先行宣言を行い、 |
4018 | 2901 | さらにそれらの名前が組み込みのものをオーバーライドできるようにします: |
4019 | 2902 | |
4020 | 2903 | use subs 'chdir', 'chroot', 'chmod', 'chown'; |
4021 | 2904 | chdir $somewhere; |
4022 | 2905 | sub chdir { ... } |
4023 | 2906 | |
4024 | 2907 | =begin original |
4025 | 2908 | |
4026 | 2909 | To unambiguously refer to the built-in form, precede the |
4027 | 2910 | built-in name with the special package qualifier C<CORE::>. For example, |
4028 | 2911 | saying C<CORE::open()> always refers to the built-in C<open()>, even |
4029 | 2912 | if the current package has imported some other subroutine called |
4030 | 2913 | C<&open()> from elsewhere. Even though it looks like a regular |
4031 | function call, it isn't: the C | |
2914 | function call, it isn't: the CORE:: prefix in that case is part of Perl's | |
4032 | syntax, and works for any keyword, regardless of what is in the C | |
2915 | syntax, and works for any keyword, regardless of what is in the CORE | |
4033 | 2916 | package. Taking a reference to it, that is, C<\&CORE::open>, only works |
4034 | 2917 | for some keywords. See L<CORE>. |
4035 | 2918 | |
4036 | 2919 | =end original |
4037 | 2920 | |
4038 | 2921 | 曖昧さなく組み込みのものを参照するために、特別なパッケージ修飾子 |
4039 | 2922 | C<CORE::> を組み込みの名前に前置することができます。 |
4040 | 2923 | たとえば C<CORE::open()> とすると、たとえカレントパッケージが |
4041 | 2924 | C<&open()> といった別のサブルーチンを import していたとしても |
4042 | 2925 | これは常に組み込み関数の C<open()> を参照します。 |
4043 | 2926 | これは通常の関数呼び出しのように見えますが、そうではありません: |
4044 | この場合の C | |
2927 | この場合の CORE:: 接頭辞は Perl の文法の一部で、CORE パッケージに何があるかに | |
4045 | ||
2928 | 関わらず、どのようなキーワードでも動作します。 | |
4046 | 2929 | C<\&CORE::open> のように、これに対するリファレンスの取得は、一部の |
4047 | 2930 | キーワードでのみ動作します。 |
4048 | 2931 | L<CORE> を参照してください。 |
4049 | 2932 | |
4050 | 2933 | =begin original |
4051 | 2934 | |
4052 | 2935 | Library modules should not in general export built-in names like C<open> |
4053 | 2936 | or C<chdir> as part of their default C<@EXPORT> list, because these may |
4054 | 2937 | sneak into someone else's namespace and change the semantics unexpectedly. |
4055 | 2938 | Instead, if the module adds that name to C<@EXPORT_OK>, then it's |
4056 | 2939 | possible for a user to import the name explicitly, but not implicitly. |
4057 | 2940 | That is, they could say |
4058 | 2941 | |
4059 | 2942 | =end original |
4060 | 2943 | |
4061 | 2944 | ライブラリモジュールは、C<open> だとか C<chdir> のような組み込みの名前を |
4062 | 2945 | デフォルトの C<@EXPORT> リストの一部としてexport すべきではありません; |
4063 | 2946 | なぜなら、ライブラリを使った人の名前空間を汚し、予期しない動作を |
4064 | 2947 | 呼び起こす可能性があるからです。 |
4065 | 2948 | C<@EXPORT_OK> に名前を追加すれば、ユーザーがその名前を陽に指定すれば |
4066 | 2949 | import することができ、暗黙の内に import されてしまうことはありません。 |
4067 | 2950 | つまり、以下のようにすると |
4068 | 2951 | |
4069 | 2952 | use Module 'open'; |
4070 | 2953 | |
4071 | 2954 | =begin original |
4072 | 2955 | |
4073 | 2956 | and it would import the C<open> override. But if they said |
4074 | 2957 | |
4075 | 2958 | =end original |
4076 | 2959 | |
4077 | 2960 | C<open()> のオーバーライドをインポートします。 |
4078 | 2961 | しかし、以下のようにすると |
4079 | 2962 | |
4080 | 2963 | use Module; |
4081 | 2964 | |
4082 | 2965 | =begin original |
4083 | 2966 | |
4084 | 2967 | they would get the default imports without overrides. |
4085 | 2968 | |
4086 | 2969 | =end original |
4087 | 2970 | |
4088 | 2971 | デフォルトのインポートを行い、オーバーライドはしません。 |
4089 | 2972 | |
4090 | 2973 | =begin original |
4091 | 2974 | |
4092 | 2975 | The foregoing mechanism for overriding built-in is restricted, quite |
4093 | 2976 | deliberately, to the package that requests the import. There is a second |
4094 | 2977 | method that is sometimes applicable when you wish to override a built-in |
4095 | 2978 | everywhere, without regard to namespace boundaries. This is achieved by |
4096 | 2979 | importing a sub into the special namespace C<CORE::GLOBAL::>. Here is an |
4097 | 2980 | example that quite brazenly replaces the C<glob> operator with something |
4098 | 2981 | that understands regular expressions. |
4099 | 2982 | |
4100 | 2983 | =end original |
4101 | 2984 | |
4102 | 2985 | 組み込みのものに対するオーバーライディングのための機構は |
4103 | 2986 | インポートを要求したパッケージに制限されています。 |
4104 | 2987 | 名前空間に関係なく組み込みの任意のものをオーバーライドしたいと |
4105 | 2988 | 考えたときに使えることもある二番目の方法があります。 |
4106 | 2989 | それは特殊な名前空間 C<CORE::GLOBAL> に対して sub を |
4107 | 2990 | インポートすることによるものです。 |
4108 | 2991 | 以下にちょっとした正規表現を使って C<glob> 演算子を置き換える例を挙げます。 |
4109 | 2992 | |
4110 | 2993 | package REGlob; |
4111 | 2994 | require Exporter; |
4112 | 2995 | @ISA = 'Exporter'; |
4113 | 2996 | @EXPORT_OK = 'glob'; |
4114 | 2997 | |
4115 | 2998 | sub import { |
4116 | ||
2999 | my $pkg = shift; | |
4117 | ||
3000 | return unless @_; | |
4118 | ||
3001 | my $sym = shift; | |
4119 | ||
3002 | my $where = ($sym =~ s/^GLOBAL_// ? 'CORE::GLOBAL' : caller(0)); | |
4120 | ||
3003 | $pkg->export($where, $sym, @_); | |
4121 | 3004 | } |
4122 | 3005 | |
4123 | 3006 | sub glob { |
4124 | ||
3007 | my $pat = shift; | |
4125 | ||
3008 | my @got; | |
4126 | ||
3009 | if (opendir my $d, '.') { | |
4127 | | |
3010 | @got = grep /$pat/, readdir $d; | |
4128 | | |
3011 | closedir $d; | |
4129 | ||
3012 | } | |
4130 | ||
3013 | return @got; | |
4131 | 3014 | } |
4132 | 3015 | 1; |
4133 | 3016 | |
4134 | 3017 | =begin original |
4135 | 3018 | |
4136 | 3019 | And here's how it could be (ab)used: |
4137 | 3020 | |
4138 | 3021 | =end original |
4139 | 3022 | |
4140 | 3023 | そして以下は悪い使い方(かもしれない)例です: |
4141 | 3024 | |
4142 | #use REGlob 'GLOBAL_glob'; | |
3025 | #use REGlob 'GLOBAL_glob'; # override glob() in ALL namespaces | |
4143 | 3026 | package Foo; |
4144 | use REGlob 'glob'; | |
3027 | use REGlob 'glob'; # override glob() in Foo:: only | |
4145 | print for <^[a-z_]+\.pm\$>; | |
3028 | print for <^[a-z_]+\.pm\$>; # show all pragmatic modules | |
4146 | 3029 | |
4147 | 3030 | =begin original |
4148 | 3031 | |
4149 | 3032 | The initial comment shows a contrived, even dangerous example. |
4150 | 3033 | By overriding C<glob> globally, you would be forcing the new (and |
4151 | 3034 | subversive) behavior for the C<glob> operator for I<every> namespace, |
4152 | 3035 | without the complete cognizance or cooperation of the modules that own |
4153 | 3036 | those namespaces. Naturally, this should be done with extreme caution--if |
4154 | 3037 | it must be done at all. |
4155 | 3038 | |
4156 | 3039 | =end original |
4157 | 3040 | |
4158 | 3041 | 最初のコメント部分は不自然で、危険ですらある例です。 |
4159 | 3042 | グローバルに C<glob> をオーバーライドすることによって、 |
4160 | 3043 | 完全に認識されていなかったりモジュール固有の名前空間との協調を |
4161 | 3044 | 考慮せずに、C<glob> の動作が I<全ての> |
4162 | 3045 | 名前空間で強制的に新しい(そして破壊的な)ものになります。 |
4163 | 3046 | こういったことは(それが本当にやらなければいけないこととした上で) |
4164 | 3047 | 大いに注意したうえで行うべきものです。 |
4165 | 3048 | |
4166 | 3049 | =begin original |
4167 | 3050 | |
4168 | 3051 | The C<REGlob> example above does not implement all the support needed to |
4169 | cleanly override | |
3052 | cleanly override perl's C<glob> operator. The built-in C<glob> has | |
4170 | 3053 | different behaviors depending on whether it appears in a scalar or list |
4171 | context, but our C<REGlob> doesn't. Indeed, many | |
3054 | context, but our C<REGlob> doesn't. Indeed, many perl built-in have such | |
4172 | 3055 | context sensitive behaviors, and these must be adequately supported by |
4173 | 3056 | a properly written override. For a fully functional example of overriding |
4174 | 3057 | C<glob>, study the implementation of C<File::DosGlob> in the standard |
4175 | 3058 | library. |
4176 | 3059 | |
4177 | 3060 | =end original |
4178 | 3061 | |
4179 | 前述の C<REGlob> の例は、 | |
3062 | 前述の C<REGlob> の例は、perl の C<glob> 演算子をオーバーライドするのに | |
4180 | 3063 | 必要な全てのことを実装してはいません。 |
4181 | 3064 | 組み込みの C<glob> はそれがスカラコンテキストで使われたのか |
4182 | 3065 | リストコンテキストで使われたのかによって異なる動作をしますが、 |
4183 | 3066 | 先程の例の C<REGlob> ではそうなっていません。 |
4184 | ||
3067 | perl の組み込みのものの多くがこのようにコンテキストによって違う動作をし、 | |
4185 | 3068 | オーバーライドするものを記述する場合にはきちんとそれを |
4186 | 3069 | サポートしていなければなりません。 |
4187 | 3070 | C<glob> のオーバーライディングの完全版は、 |
4188 | 3071 | 標準ライブラリにある C<File::DosGlob> の実装で勉強してください。 |
4189 | 3072 | |
4190 | 3073 | =begin original |
4191 | 3074 | |
4192 | 3075 | When you override a built-in, your replacement should be consistent (if |
4193 | 3076 | possible) with the built-in native syntax. You can achieve this by using |
4194 | 3077 | a suitable prototype. To get the prototype of an overridable built-in, |
4195 | 3078 | use the C<prototype> function with an argument of C<"CORE::builtin_name"> |
4196 | 3079 | (see L<perlfunc/prototype>). |
4197 | 3080 | |
4198 | 3081 | =end original |
4199 | 3082 | |
4200 | 3083 | 組み込み関数をオーバーライドするとき、オーバーライドした関数は |
4201 | 3084 | 組み込み関数の元の文法(もしあれば)と一貫しているべきです。 |
4202 | 3085 | これは適切なプロトタイプを使うことで達成できます。 |
4203 | 3086 | オーバーライドできる組み込み関数のプロトタイプを得るためには、 |
4204 | 3087 | C<"CORE::builtin_name"> を引き数として C<prototype> 関数を使ってください |
4205 | 3088 | (L<perlfunc/prototype> を参照してください)。 |
4206 | 3089 | |
4207 | 3090 | =begin original |
4208 | 3091 | |
4209 | 3092 | Note however that some built-ins can't have their syntax expressed by a |
4210 | 3093 | prototype (such as C<system> or C<chomp>). If you override them you won't |
4211 | 3094 | be able to fully mimic their original syntax. |
4212 | 3095 | |
4213 | 3096 | =end original |
4214 | 3097 | |
4215 | 3098 | (C<system> や C<chomp> のように)文法をプロトタイプで表現できない |
4216 | 3099 | 組み込み関数に注意してください。 |
4217 | 3100 | もしこれらをオーバーライドすると、もとの文法を完全に真似ることは |
4218 | 3101 | できません。 |
4219 | 3102 | |
4220 | 3103 | =begin original |
4221 | 3104 | |
4222 | 3105 | The built-ins C<do>, C<require> and C<glob> can also be overridden, but due |
4223 | 3106 | to special magic, their original syntax is preserved, and you don't have |
4224 | 3107 | to define a prototype for their replacements. (You can't override the |
4225 | 3108 | C<do BLOCK> syntax, though). |
4226 | 3109 | |
4227 | 3110 | =end original |
4228 | 3111 | |
4229 | 3112 | 組み込みの C<do>, C<require>, C<glob> もオーバーライドできますが、特別な |
4230 | 3113 | 魔法によって、これらの元の文法は保存され、オーバーライドしたもののために |
4231 | 3114 | プロトタイプを定義する必要はありません。 |
4232 | 3115 | (しかし、C<do BLOCK> 文法はオーバーライドできません)。 |
4233 | 3116 | |
4234 | 3117 | =begin original |
4235 | 3118 | |
4236 | 3119 | C<require> has special additional dark magic: if you invoke your |
4237 | 3120 | C<require> replacement as C<require Foo::Bar>, it will actually receive |
4238 | 3121 | the argument C<"Foo/Bar.pm"> in @_. See L<perlfunc/require>. |
4239 | 3122 | |
4240 | 3123 | =end original |
4241 | 3124 | |
4242 | 3125 | C<require> は特別な追加の黒魔法を持ちます: C<require> をオーバーライドした |
4243 | 3126 | ものを C<require Foo::Bar> として起動すると、実際には @_ に引き数として |
4244 | 3127 | C<"Foo/Bar.pm"> を受け取ります。 |
4245 | 3128 | L<perlfunc/require> を参照してください。 |
4246 | 3129 | |
4247 | 3130 | =begin original |
4248 | 3131 | |
4249 | 3132 | And, as you'll have noticed from the previous example, if you override |
4250 | 3133 | C<glob>, the C<< <*> >> glob operator is overridden as well. |
4251 | 3134 | |
4252 | 3135 | =end original |
4253 | 3136 | |
4254 | 3137 | そして、以前の例から気付いたかもしれませんが、もし C<glob> を |
4255 | 3138 | オーバーライドすると、グロブ演算子 C<< <*> >> もオーバーライドされます。 |
4256 | 3139 | |
4257 | 3140 | =begin original |
4258 | 3141 | |
4259 | 3142 | In a similar fashion, overriding the C<readline> function also overrides |
4260 | the equivalent I/O operator C<< <FILEHANDLE> >>. | |
3143 | the equivalent I/O operator C<< <FILEHANDLE> >>. Also, overriding | |
4261 | 3144 | C<readpipe> also overrides the operators C<``> and C<qx//>. |
4262 | 3145 | |
4263 | 3146 | =end original |
4264 | 3147 | |
4265 | 3148 | 同様に、C<readline> 関数をオーバーライドすると 等価な |
4266 | 3149 | I/O 演算子である C<< <FILEHANDLE> >> もオーバーライドします。 |
4267 | 3150 | また、C<readpipe> をオーバーライドすると、演算子 C<``> と C<qx//> も |
4268 | 3151 | オーバーライドします。 |
4269 | 3152 | |
4270 | 3153 | =begin original |
4271 | 3154 | |
4272 | 3155 | Finally, some built-ins (e.g. C<exists> or C<grep>) can't be overridden. |
4273 | 3156 | |
4274 | 3157 | =end original |
4275 | 3158 | |
4276 | 3159 | 最後に、いくつかの組み込み関数(C<exists> や C<grep>) は |
4277 | 3160 | オーバーライドできません。 |
4278 | 3161 | |
4279 | 3162 | =head2 Autoloading |
4280 | 3163 | X<autoloading> X<AUTOLOAD> |
4281 | 3164 | |
4282 | 3165 | (オートロード) |
4283 | 3166 | |
4284 | 3167 | =begin original |
4285 | 3168 | |
4286 | 3169 | If you call a subroutine that is undefined, you would ordinarily |
4287 | 3170 | get an immediate, fatal error complaining that the subroutine doesn't |
4288 | 3171 | exist. (Likewise for subroutines being used as methods, when the |
4289 | 3172 | method doesn't exist in any base class of the class's package.) |
4290 | 3173 | However, if an C<AUTOLOAD> subroutine is defined in the package or |
4291 | 3174 | packages used to locate the original subroutine, then that |
4292 | 3175 | C<AUTOLOAD> subroutine is called with the arguments that would have |
4293 | 3176 | been passed to the original subroutine. The fully qualified name |
4294 | 3177 | of the original subroutine magically appears in the global $AUTOLOAD |
4295 | 3178 | variable of the same package as the C<AUTOLOAD> routine. The name |
4296 | 3179 | is not passed as an ordinary argument because, er, well, just |
4297 | 3180 | because, that's why. (As an exception, a method call to a nonexistent |
4298 | 3181 | C<import> or C<unimport> method is just skipped instead. Also, if |
4299 | 3182 | the AUTOLOAD subroutine is an XSUB, there are other ways to retrieve the |
4300 | 3183 | subroutine name. See L<perlguts/Autoloading with XSUBs> for details.) |
4301 | 3184 | |
4302 | 3185 | =end original |
4303 | 3186 | |
4304 | 3187 | 定義されていないサブルーチンを呼び出した場合、通常はそんなサブルーチンは |
4305 | 3188 | ないといった内容のエラーが即座に発生するでしょう(メソッドとして |
4306 | 3189 | 使われているサブルーチンも同様に、メソッドがそのクラスのパッケージの |
4307 | 3190 | すべての基底クラス中に存在していなかったとき)。 |
4308 | 3191 | しかし、C<AUTOLOAD> サブルーチンがそのパッケージの中で定義されているか |
4309 | 3192 | 元のサブルーチンで使われるパッケージの中で定義されていれば、 |
4310 | 3193 | 元のサブルーチンに対して渡されたであろう引数を伴ってその |
4311 | 3194 | C<AUTOLOAD> サブルーチンが呼び出されます。 |
4312 | 3195 | 元のサブルーチンの完全修飾された名前は C<AUTOLOAD> ルーチンと同じ |
4313 | 3196 | パッケージのグローバルな変数 $AUTOLOAD の中に現れます。 |
4314 | 3197 | この名前は通常の引数として渡されることはありません。 |
4315 | 3198 | なぜなら、その、あー、なんというかこれが理由です。 |
4316 | 3199 | (例外として、存在しない C<import> か C<unimport> メソッドへの呼び出しは |
4317 | 3200 | 単に無視されます。 |
4318 | 3201 | また、AUTOLOAD サブルーチンが XSUB なら、サブルーチン名を取得するための |
4319 | 3202 | その他の方法があります。 |
4320 | 3203 | 詳しくは L<perlguts/Autoloading with XSUBs> を参照してください。) |
4321 | 3204 | |
4322 | 3205 | =begin original |
4323 | 3206 | |
4324 | 3207 | Many C<AUTOLOAD> routines load in a definition for the requested |
4325 | 3208 | subroutine using eval(), then execute that subroutine using a special |
4326 | 3209 | form of goto() that erases the stack frame of the C<AUTOLOAD> routine |
4327 | 3210 | without a trace. (See the source to the standard module documented |
4328 | 3211 | in L<AutoLoader>, for example.) But an C<AUTOLOAD> routine can |
4329 | 3212 | also just emulate the routine and never define it. For example, |
4330 | 3213 | let's pretend that a function that wasn't defined should just invoke |
4331 | 3214 | C<system> with those arguments. All you'd do is: |
4332 | 3215 | |
4333 | 3216 | =end original |
4334 | 3217 | |
4335 | 3218 | 多くの C<AUTOLOAD> ルーチンは eval() を使った要求サブルーチンに |
4336 | 3219 | 対する定義でロードされ、C<AUTOLOAD> ルーチンのスタックフレームを |
4337 | 3220 | トレースなしに消してしまうような特別な形式の goto() を使うサブルーチンを |
4338 | 3221 | 実行します。 |
4339 | 3222 | (実例は標準の C<AutoLoader> モジュールのソースを参照してください。) |
4340 | 3223 | しかし、C<AUTOLOAD> ルーチンはそのようなルーチンの模倣をすることもできて、 |
4341 | 3224 | かつそれを定義しません。 |
4342 | 3225 | たとえば、定義されてない関数があったときに、それを引数として |
4343 | 3226 | C<system()> を起動するようにさせます。 |
4344 | 3227 | あなたがやろうとしていることはこういうことです: |
4345 | 3228 | |
4346 | 3229 | sub AUTOLOAD { |
4347 | | |
3230 | my $program = $AUTOLOAD; | |
4348 | ||
3231 | $program =~ s/.*:://; | |
4349 | ||
3232 | system($program, @_); | |
4350 | system($program, @_); | |
4351 | 3233 | } |
4352 | 3234 | date(); |
4353 | who(); | |
3235 | who('am', 'i'); | |
4354 | 3236 | ls('-l'); |
4355 | 3237 | |
4356 | 3238 | =begin original |
4357 | 3239 | |
4358 | 3240 | In fact, if you predeclare functions you want to call that way, you don't |
4359 | 3241 | even need parentheses: |
4360 | 3242 | |
4361 | 3243 | =end original |
4362 | 3244 | |
4363 | 3245 | このやり方で呼び出したい関数を先行宣言しておけば、括弧すら |
4364 | 3246 | 必要ではなくなります。 |
4365 | 3247 | |
4366 | 3248 | use subs qw(date who ls); |
4367 | 3249 | date; |
4368 | who; | |
3250 | who "am", "i"; | |
4369 | 3251 | ls '-l'; |
4370 | 3252 | |
4371 | 3253 | =begin original |
4372 | 3254 | |
4373 | 3255 | A more complete example of this is the Shell module on CPAN, which |
4374 | 3256 | can treat undefined subroutine calls as calls to external programs. |
4375 | 3257 | |
4376 | 3258 | =end original |
4377 | 3259 | |
4378 | 3260 | この例のもっと完全なものが、CPAN にある Shell モジュールです; これは |
4379 | 3261 | 未定義のサブルーチンの呼び出しを外部プログラムの呼び出しとして扱います。 |
4380 | 3262 | |
4381 | 3263 | =begin original |
4382 | 3264 | |
4383 | 3265 | Mechanisms are available to help modules writers split their modules |
4384 | 3266 | into autoloadable files. See the standard AutoLoader module |
4385 | 3267 | described in L<AutoLoader> and in L<AutoSplit>, the standard |
4386 | 3268 | SelfLoader modules in L<SelfLoader>, and the document on adding C |
4387 | 3269 | functions to Perl code in L<perlxs>. |
4388 | 3270 | |
4389 | 3271 | =end original |
4390 | 3272 | |
4391 | 3273 | この仕掛けは、モジュール作者がモジュールを自動ロード可能なファイルに |
4392 | 3274 | 分割するのを助けるために使用可能です。 |
4393 | 3275 | L<AutoLoader> と L<AutoSplit> で説明されている |
4394 | 3276 | 標準の AutoLoader モジュールと、L<SelfLoader> で説明されている |
4395 | 3277 | 標準の SelfLoader モジュール、そして L<perlxs> にある Perl プログラムに |
4396 | 3278 | C の関数を追加することに関するドキュメントを参照してください。 |
4397 | 3279 | |
4398 | 3280 | =head2 Subroutine Attributes |
4399 | 3281 | X<attribute> X<subroutine, attribute> X<attrs> |
4400 | 3282 | |
4401 | 3283 | (サブルーチン属性) |
4402 | 3284 | |
4403 | 3285 | =begin original |
4404 | 3286 | |
4405 | 3287 | A subroutine declaration or definition may have a list of attributes |
4406 | 3288 | associated with it. If such an attribute list is present, it is |
4407 | 3289 | broken up at space or colon boundaries and treated as though a |
4408 | 3290 | C<use attributes> had been seen. See L<attributes> for details |
4409 | 3291 | about what attributes are currently supported. |
4410 | 3292 | Unlike the limitation with the obsolescent C<use attrs>, the |
4411 | 3293 | C<sub : ATTRLIST> syntax works to associate the attributes with |
4412 | 3294 | a pre-declaration, and not just with a subroutine definition. |
4413 | 3295 | |
4414 | 3296 | =end original |
4415 | 3297 | |
4416 | 3298 | サブルーチン宣言や定義には、それと結び付けられた属性のリストを |
4417 | 3299 | 持たせることが出来ます。 |
4418 | 3300 | このような属性が合った場合、スペースかコロンを区切りとして分割され、 |
4419 | 3301 | C<use attributes> があったかのように扱われます。 |
4420 | 3302 | 属性が現在対応している詳細については L<attributes> を参照してください。 |
4421 | 3303 | 古い C<use attrs> の制限と違って、C<sub : ATTRLIST> の文法は |
4422 | 3304 | 前方宣言でも動作し、サブルーチン定義だけではありません。 |
4423 | 3305 | |
4424 | 3306 | =begin original |
4425 | 3307 | |
4426 | 3308 | The attributes must be valid as simple identifier names (without any |
4427 | 3309 | punctuation other than the '_' character). They may have a parameter |
4428 | 3310 | list appended, which is only checked for whether its parentheses ('(',')') |
4429 | 3311 | nest properly. |
4430 | 3312 | |
4431 | 3313 | =end original |
4432 | 3314 | |
4433 | 3315 | 属性は単純な識別子名('_' 以外の句読点なし)でなければなりません。 |
4434 | 3316 | パラメータリストを追加するときに、括弧('(',')')のネストが |
4435 | 3317 | 正しいかどうかだけをチェックします。 |
4436 | 3318 | |
4437 | 3319 | =begin original |
4438 | 3320 | |
4439 | 3321 | Examples of valid syntax (even though the attributes are unknown): |
4440 | 3322 | |
4441 | 3323 | =end original |
4442 | 3324 | |
4443 | 3325 | (属性が不明だとしても)正常な文法の例です: |
4444 | 3326 | |
4445 | 3327 | sub fnord (&\%) : switch(10,foo(7,3)) : expensive; |
4446 | 3328 | sub plugh () : Ugly('\(") :Bad; |
4447 | 3329 | sub xyzzy : _5x5 { ... } |
4448 | 3330 | |
4449 | 3331 | =begin original |
4450 | 3332 | |
4451 | 3333 | Examples of invalid syntax: |
4452 | 3334 | |
4453 | 3335 | =end original |
4454 | 3336 | |
4455 | 3337 | 不正な文法の例です: |
4456 | 3338 | |
4457 | sub fnord : switch(10,foo(); | |
3339 | sub fnord : switch(10,foo(); # ()-string not balanced | |
4458 | sub snoid : Ugly('('); | |
3340 | sub snoid : Ugly('('); # ()-string not balanced | |
4459 | sub xyzzy : 5x5; | |
3341 | sub xyzzy : 5x5; # "5x5" not a valid identifier | |
4460 | sub plugh : Y2::north; | |
3342 | sub plugh : Y2::north; # "Y2::north" not a simple identifier | |
4461 | sub snurt : foo + bar; | |
3343 | sub snurt : foo + bar; # "+" not a colon or space | |
4462 | 3344 | |
4463 | 3345 | =begin original |
4464 | 3346 | |
4465 | 3347 | The attribute list is passed as a list of constant strings to the code |
4466 | 3348 | which associates them with the subroutine. In particular, the second example |
4467 | 3349 | of valid syntax above currently looks like this in terms of how it's |
4468 | 3350 | parsed and invoked: |
4469 | 3351 | |
4470 | 3352 | =end original |
4471 | 3353 | |
4472 | 3354 | 属性リストはサブルーチンと結び付けられたコードに定数文字列の |
4473 | 3355 | リストとして渡されます。 |
4474 | 3356 | 特に、上記の有効な文法の第二の例では、どのようにパースと起動が |
4475 | 3357 | 行われるかという点においては以下のようになります: |
4476 | 3358 | |
4477 | 3359 | use attributes __PACKAGE__, \&plugh, q[Ugly('\(")], 'Bad'; |
4478 | 3360 | |
4479 | 3361 | =begin original |
4480 | 3362 | |
4481 | 3363 | For further details on attribute lists and their manipulation, |
4482 | 3364 | see L<attributes> and L<Attribute::Handlers>. |
4483 | 3365 | |
4484 | 3366 | =end original |
4485 | 3367 | |
4486 | 3368 | 属性リストとその操作に関するさらなる詳細については、 |
4487 | 3369 | L<attributes> を参照してください。 |
4488 | 3370 | |
4489 | 3371 | =head1 SEE ALSO |
4490 | 3372 | |
4491 | 3373 | =begin original |
4492 | 3374 | |
4493 | 3375 | See L<perlref/"Function Templates"> for more about references and closures. |
4494 | See L<perlxs> if you'd like to learn about calling C subroutines from Perl. | |
3376 | See L<perlxs> if you'd like to learn about calling C subroutines from Perl. | |
4495 | See L<perlembed> if you'd like to learn about calling Perl subroutines from C. | |
3377 | See L<perlembed> if you'd like to learn about calling Perl subroutines from C. | |
4496 | 3378 | See L<perlmod> to learn about bundling up your functions in separate files. |
4497 | 3379 | See L<perlmodlib> to learn what library modules come standard on your system. |
4498 | 3380 | See L<perlootut> to learn how to make object method calls. |
4499 | 3381 | |
4500 | 3382 | =end original |
4501 | 3383 | |
4502 | 3384 | リファレンスとクロージャーについては L<perlref/"Function Templates"> を |
4503 | 3385 | 参照してください。 |
4504 | 3386 | Perl から C のサブルーチンを呼び出すことに関して知りたければ、 |
4505 | 3387 | L<perlxs>を参照してください。 |
4506 | 3388 | Perl のサブルーチンを C から呼び出したい場合は L<perlembed> を参照してください。 |
4507 | 3389 | 自分の関数を別のファイルで構築することに関しては L<perlmod> を参照してください。 |
4508 | 3390 | どのライブラリモジュールがあなたのシステムで標準となっているかを |
4509 | 3391 | 学ぶためには L<perlmodlib> を参照してください。 |
4510 | 3392 | オブジェクトメソッド呼び出しの作り方を学ぶには L<perlootut> を |
4511 | 3393 | 参照してください。 |
4512 | 3394 | |
4513 | 3395 | =begin meta |
4514 | 3396 | |
4515 | 3397 | Translate: KIMURA Koichi (5.005_03) |
4516 | 3398 | Update: SHIRAKATA Kentaro <argrath@ub32.org> (5.6.1-) |
4517 | 3399 | Status: completed |
4518 | 3400 | |
4519 | 3401 | =end meta |