perlfaq7 >
5.14.1
との差分
perlfaq7 5.14.1 と 5.6.1 の差分
1 | 1 | |
2 | 2 | =encoding euc-jp |
3 | 3 | |
4 | 4 | =head1 NAME |
5 | 5 | |
6 | 6 | =begin original |
7 | 7 | |
8 | perlfaq7 - | |
8 | perlfaq7 - Perl Language Issues ($Revision: 1.28 $, $Date: 1999/05/23 20:36:18 $) | |
9 | 9 | |
10 | 10 | =end original |
11 | 11 | |
12 | perlfaq7 - Perl | |
12 | perlfaq7 - Perlに関することがら ($Revision: 1.28 $, $Date: 1999/05/23 20:36:18 $) | |
13 | 13 | |
14 | 14 | =head1 DESCRIPTION |
15 | 15 | |
16 | =begin original | |
17 | ||
18 | This section deals with general Perl language issues that don't | |
19 | clearly fit into any of the other sections. | |
20 | ||
21 | =end original | |
22 | ||
23 | 16 | このセクションでは、他のセクションにはきっちりとあてはまらないような |
24 | 17 | Perl 言語に関する一般的な事柄を扱います。 |
25 | 18 | |
26 | 19 | =head2 Can I get a BNF/yacc/RE for the Perl language? |
27 | 20 | |
28 | (Perl | |
21 | (Perlのための BNF/yacc/RE は入手できますか?) | |
29 | 22 | |
30 | 23 | =begin original |
31 | 24 | |
32 | 25 | There is no BNF, but you can paw your way through the yacc grammar in |
33 | 26 | perly.y in the source distribution if you're particularly brave. The |
34 | 27 | grammar relies on very smart tokenizing code, so be prepared to |
35 | 28 | venture into toke.c as well. |
36 | 29 | |
37 | 30 | =end original |
38 | 31 | |
39 | 32 | BNFはありませんが、もし多少の勇気を持ちあわせているのであれば |
40 | 配布ソースに含まれている | |
33 | 配布ソースに含まれているperly.yにあるyacc文法をいじくりまわすことができます。 | |
41 | できます。 | |
42 | 34 | その文法は非常に賢い字句解析ルーチンに依存したものなので、 |
43 | 35 | toke.c を眺める準備もしておきましょう。 |
44 | 36 | |
45 | ||
37 | Chaim Frenkelの言葉を借りればこうです: “Perlの文法は | |
38 | BNF まで縮小(reduce)することができない。 | |
39 | perl の構文解析の作業は yacc、字句解析器、煙と鏡とに分配される” | |
46 | 40 | |
47 | In the words of Chaim Frenkel: "Perl's grammar can not be reduced to BNF. | |
48 | The work of parsing perl is distributed between yacc, the lexer, smoke | |
49 | and mirrors." | |
50 | ||
51 | =end original | |
52 | ||
53 | reduce | |
54 | Chaim Frenkel の言葉を借りればこうです: | |
55 | 「Perl の文法は BNF まで縮小することができない。 | |
56 | perl の構文解析の作業は yacc、字句解析器、煙と鏡とに分配される。」 | |
57 | ||
58 | 41 | =head2 What are all these $@%&* punctuation signs, and how do I know when to use them? |
59 | 42 | |
60 | ($@%&* のマークはなんですか? | |
43 | ($@%&* のマークはなんですか?これらをいつ使えばいいのかを知るには | |
44 | どうすればいいですか?) | |
61 | 45 | |
62 | ||
46 | これらは型指定子(type specifiers)で、L<perldata>で説明されています: | |
63 | 47 | |
64 | They are type specifiers, as detailed in L<perldata>: | |
65 | ||
66 | =end original | |
67 | ||
68 | これらは型指定子(type specifiers)で、L<perldata> で説明されています: | |
69 | ||
70 | 48 | =begin original |
71 | 49 | |
72 | ||
50 | $ for scalar values (number, string or reference) | |
73 | ||
51 | @ for arrays | |
74 | ||
52 | % for hashes (associative arrays) | |
75 | ||
53 | & for subroutines (aka functions, procedures, methods) | |
76 | ||
54 | * for all types of that symbol name. In version 4 you used them like | |
77 | ||
55 | pointers, but in modern perls you can just use references. | |
78 | 56 | |
79 | 57 | =end original |
80 | 58 | |
81 | ||
59 | $ スカラー値(数値、文字列、リファレンス)に対するもの | |
82 | ||
60 | @ 配列に対するもの | |
83 | ||
61 | % ハッシュ(連想配列)に対するもの | |
84 | ||
62 | & サブルーチン(またの名を関数、手続き、メソッド)に対するもの | |
85 | ||
63 | * シンボル名に対する全ての型。バージョン4ではポインターのように | |
86 | ||
64 | 使われていましたが、新しい perl ではリファレンスが使えます。 | |
87 | 65 | |
88 | =begin original | |
89 | ||
90 | There are a couple of other symbols that | |
91 | you're likely to encounter that aren't | |
92 | really type specifiers: | |
93 | ||
94 | =end original | |
95 | ||
96 | 66 | 実際には型指定子として見ることはないであろう二つのものが |
97 | 67 | この他にもあります: |
98 | 68 | |
99 | ||
69 | <> あるファイルハンドルからレコードを入力するのに使われます | |
70 | \ なにかのリファレンスを取ります。 | |
100 | 71 | |
101 | <> are used for inputting a record from a filehandle. | |
102 | \ takes a reference to something. | |
103 | ||
104 | =end original | |
105 | ||
106 | <> あるファイルハンドルからレコードを入力するのに使われます。 | |
107 | \ なにかのリファレンスを取ります。 | |
108 | ||
109 | 72 | =begin original |
110 | 73 | |
111 | 74 | Note that <FILE> is I<neither> the type specifier for files |
112 | 75 | nor the name of the handle. It is the C<< <> >> operator applied |
113 | 76 | to the handle FILE. It reads one line (well, record--see |
114 | L<perlvar/$ | |
77 | L<perlvar/$/>) from the handle FILE in scalar context, or I<all> lines | |
115 | 78 | in list context. When performing open, close, or any other operation |
116 | 79 | besides C<< <> >> on files, or even when talking about the handle, do |
117 | 80 | I<not> use the brackets. These are correct: C<eof(FH)>, C<seek(FH, 0, |
118 | 81 | 2)> and "copying from STDIN to FILE". |
119 | 82 | |
120 | 83 | =end original |
121 | 84 | |
122 | <FILE> | |
85 | <FILE>は、ファイルに対する型指定子にもハンドルの名前の | |
123 | I<どちらでもない> | |
86 | I<どちらでもない>ということに注意してください。 | |
124 | これはハンドル FILE に対する C<< <> >> | |
87 | これはハンドル FILE に対する C<< <> >>演算子です。 | |
125 | 一行(そう、レコードですね。L<perlvar/$ | |
88 | 一行(そう、レコードですね。L<perlvar/$/>を参照してください) を | |
126 | FILE というハンドルからスカラコンテキストで読み出します。 | |
89 | FILE というハンドルからスカラーコンテキストで読み出します。 | |
127 | リストコンテキストの場合は | |
90 | リストコンテキストの場合はB<全ての>行を読み出します。 | |
128 | ファイルの C<< <> >> に関係する | |
91 | ファイルの C<< <> >> に関係するopen、closeなどの操作を行うときには、 | |
129 | 92 | ハンドルについて行っている場合であっても |
130 | アングルブラケットを使っては | |
93 | アングルブラケットを使ってはB<いけません>。 | |
131 | C<eof(FH)>, C<seek(FH, 0,2)> や "copying from STDIN to FILE" は | |
94 | C<eof(FH)>, C<seek(FH, 0,2)> や "copying from STDIN to FILE" は | |
132 | 95 | 正しいものなのです。 |
133 | 96 | |
134 | 97 | =head2 Do I always/never have to quote my strings or use semicolons and commas? |
135 | 98 | |
136 | (文字列では常にクォートする/決してクォートしない必要があるのでしょうか? | |
99 | (文字列では 常にクォートする/決してクォートしない 必要があるのでしょうか? | |
100 | また、セミコロンやカンマについては?) | |
137 | 101 | |
138 | =begin original | |
139 | ||
140 | Normally, a bareword doesn't need to be quoted, but in most cases | |
141 | probably should be (and must be under C<use strict>). But a hash key | |
142 | consisting of a simple word and the left-hand | |
143 | operand to the C<< => >> operator both | |
144 | count as though they were quoted: | |
145 | ||
146 | =end original | |
147 | ||
148 | 102 | 通常は、裸の単語(barewords)はクォートする必要はありませんが、 |
149 | ほとんど場合はクォートすべきでしょう(そして、C<use strcit> | |
103 | ほとんど場合はクォートすべきでしょう(そして、C<use strcit>しているときは | |
150 | 104 | しなければなりません)。 |
151 | しかし、単純な単語から | |
105 | しかし、単純な単語(サブルーチンの名前として定義されていないもの)から | |
152 | 106 | 構成されるハッシュと、C<< => >> 演算子の左側にあるオペランドは |
153 | 107 | 両方ともクォートされているとみなされます: |
154 | 108 | |
155 | ||
109 | This is like this | |
156 | ||
110 | ------------ --------------- | |
157 | ||
111 | $foo{line} $foo{"line"} | |
158 | ||
112 | bar => stuff "bar" => stuff | |
159 | 113 | |
160 | =begin original | |
161 | ||
162 | The final semicolon in a block is optional, as is the final comma in a | |
163 | list. Good style (see L<perlstyle>) says to put them in except for | |
164 | one-liners: | |
165 | ||
166 | =end original | |
167 | ||
168 | 114 | ブロックの最後にあるセミコロンは、リストの最後にあるカンマと同じく |
169 | 115 | 省略可能です。 |
170 | 良いスタイル(L<perlstyle> | |
116 | 良いスタイル(L<perlstyle>を参照)は一行野郎(one-liners)でなければ | |
171 | 117 | それらを使うようにしましょうと言っています。 |
172 | 118 | |
173 | ||
119 | if ($whoops) { exit 1 } | |
174 | ||
120 | @nums = (1, 2, 3); | |
175 | 121 | |
176 | ||
122 | if ($whoops) { | |
177 | ||
123 | exit 1; | |
178 | ||
124 | } | |
125 | @lines = ( | |
180 | @lines = ( | |
181 | 126 | "There Beren came from mountains cold", |
182 | 127 | "And lost he wandered under leaves", |
183 | ||
128 | ); | |
184 | 129 | |
185 | 130 | =head2 How do I skip some return values? |
186 | 131 | |
187 | 132 | (戻り値の一部をスキップするには?) |
188 | 133 | |
189 | =begin original | |
190 | ||
191 | One way is to treat the return values as a list and index into it: | |
192 | ||
193 | =end original | |
194 | ||
195 | 134 | 方法の一つは、戻り値をリストとみなして、それに添え字づけするというものです: |
196 | 135 | |
197 | ||
136 | $dir = (getpwnam($user))[7]; | |
198 | 137 | |
199 | ||
138 | もう一つのやりかたは、左辺の要素としてundefを使うというものです: | |
200 | 139 | |
201 | ||
140 | ($dev, $ino, undef, undef, $uid, $gid) = stat($file); | |
202 | 141 | |
203 | =end original | |
204 | ||
205 | もう一つのやりかたは、左辺の要素として undef を使うというものです: | |
206 | ||
207 | ($dev, $ino, undef, undef, $uid, $gid) = stat($file); | |
208 | ||
209 | =begin original | |
210 | ||
211 | You can also use a list slice to select only the elements that | |
212 | you need: | |
213 | ||
214 | =end original | |
215 | ||
216 | 必要な要素だけを選択するために、リストスライスも使えます: | |
217 | ||
218 | ($dev, $ino, $uid, $gid) = ( stat($file) )[0,1,4,5]; | |
219 | ||
220 | 142 | =head2 How do I temporarily block warnings? |
221 | 143 | |
222 | 144 | (一時的に警告をブロックするには?) |
223 | 145 | |
224 | 146 | =begin original |
225 | 147 | |
226 | 148 | If you are running Perl 5.6.0 or better, the C<use warnings> pragma |
227 | 149 | allows fine control of what warning are produced. |
228 | 150 | See L<perllexwarn> for more details. |
229 | 151 | |
230 | 152 | =end original |
231 | 153 | |
232 | 154 | Perl 5.6.0 以降を使っているなら、C<use warnings> プラグマで |
233 | 155 | どんな警告を生成するかをうまく制御できます。 |
234 | 156 | 詳細については L<perllexwarn> を参照してください。 |
235 | 157 | |
236 | ||
158 | { | |
237 | 159 | no warnings; # temporarily turn off warnings |
238 | 160 | $a = $b + $c; # I know these might be undef |
239 | ||
161 | } | |
240 | 162 | |
241 | 163 | =begin original |
242 | 164 | |
243 | Additionally, you can enable and disable categories of warnings. | |
244 | You turn off the categories you want to ignore and you can still | |
245 | get other categories of warnings. See L<perllexwarn> for the | |
246 | complete details, including the category names and hierarchy. | |
247 | ||
248 | =end original | |
249 | ||
250 | さらに、警告の分野毎に警告を有効または無効にできます。 | |
251 | 無視したいカテゴリを無効にしても、残りのカテゴリの警告は受けられます。 | |
252 | カテゴリ名と階層を含む、完全な詳細については L<perllexwarn> を | |
253 | 参照してください。 | |
254 | ||
255 | { | |
256 | no warnings 'uninitialized'; | |
257 | $a = $b + $c; | |
258 | } | |
259 | ||
260 | =begin original | |
261 | ||
262 | 165 | If you have an older version of Perl, the C<$^W> variable (documented |
263 | 166 | in L<perlvar>) controls runtime warnings for a block: |
264 | 167 | |
265 | 168 | =end original |
266 | 169 | |
267 | より古いバージョンの場合は、変数 C<$^W>(L<perlvar> | |
170 | より古いバージョンの場合は、変数 C<$^W>(L<perlvar>に説明があります)は | |
268 | 171 | 実行時の警告のブロックを制御します: |
269 | 172 | |
270 | =begin original | |
271 | ||
272 | { | |
273 | local $^W = 0; # temporarily turn off warnings | |
274 | $a = $b + $c; # I know these might be undef | |
275 | } | |
276 | ||
277 | =end original | |
278 | ||
279 | 173 | { |
280 | 174 | local $^W = 0; # 一時的に警告をオフにする |
281 | $a = $b + $c; # これらが | |
175 | $a = $b + $c; # これらがundefかもしれないことを知っている | |
282 | 176 | } |
283 | 177 | |
284 | ||
178 | 全ての句読点変数(punctuation variable)と同様、現時点ではC<$^W>に対して | |
179 | my() を使うことはできず、local()だけしか使えないということに注意してください。 | |
285 | 180 | |
286 | Note that like all the punctuation variables, you cannot currently | |
287 | use my() on C<$^W>, only local(). | |
288 | ||
289 | =end original | |
290 | ||
291 | 全ての句読点変数(punctuation variable)と同様、現時点では C<$^W> に対して | |
292 | my() を使うことはできず、local() だけしか使えないということに | |
293 | 注意してください。 | |
294 | ||
295 | 181 | =head2 What's an extension? |
296 | 182 | |
297 | 183 | (エクステンションてなんですか?) |
298 | 184 | |
299 | ||
185 | エクステンションとは、Perlからコンパイル済みのCコードを呼び出すための方法です。 | |
186 | エクステンションについて知るにはL<perlxstut>を読むのが良いでしょう。 | |
300 | 187 | |
301 | An extension is a way of calling compiled C code from Perl. Reading | |
302 | L<perlxstut> is a good place to learn more about extensions. | |
303 | ||
304 | =end original | |
305 | ||
306 | エクステンションとは、Perl からコンパイル済みの C コードを呼び出すための | |
307 | 方法です。 | |
308 | エクステンションについて知るには L<perlxstut> を読むのが良いでしょう。 | |
309 | ||
310 | 188 | =head2 Why do Perl operators have different precedence than C operators? |
311 | 189 | |
312 | 190 | (なぜ Perl の演算子は C の演算子とは異なった優先順位を持っているのでしょうか?) |
313 | 191 | |
314 | ||
192 | 実際はそうではありません。Perlに持ち込まれたCの演算子はすべて、 | |
315 | ||
316 | Actually, they don't. All C operators that Perl copies have the same | |
317 | precedence in Perl as they do in C. The problem is with operators that C | |
318 | doesn't have, especially functions that give a list context to everything | |
319 | on their right, eg. print, chmod, exec, and so on. Such functions are | |
320 | called "list operators" and appear as such in the precedence table in | |
321 | L<perlop>. | |
322 | ||
323 | =end original | |
324 | ||
325 | 実際はそうではありません。Perl に持ち込まれたCの演算子はすべて、 | |
326 | 193 | C と Perl とで同じ優先順位を持っています。 |
327 | 194 | 問題は、C にはない演算子、特にその右辺に対してつねにリストコンテキストを |
328 | 195 | 与える関数、例えば print、chmod、exec などです。 |
329 | そういった関数は | |
196 | そういった関数は“リスト演算子”と呼ばれ、 | |
330 | 197 | L<perlop> にある優先順位テーブルにあります。 |
331 | 198 | |
332 | ||
199 | ありがちな間違いは | |
333 | 200 | |
334 | ||
201 | unlink $file || die "snafu"; | |
335 | 202 | |
336 | ||
203 | のように書いてしまうことです。これは以下のように解釈されます: | |
337 | 204 | |
338 | ||
205 | unlink ($file || die "snafu"); | |
339 | 206 | |
340 | ||
207 | この問題を避けるためには、余計な括弧をつけるかより優先順位の低い C<or> 演算子を | |
208 | 使うようにします: | |
341 | 209 | |
342 | ||
210 | (unlink $file) || die "snafu"; | |
211 | unlink $file or die "snafu"; | |
343 | 212 | |
344 | ||
213 | “English”演算子(C<and>, C<or>, C<xor>, C<not>)は先に説明している | |
345 | ||
346 | =end original | |
347 | ||
348 | これは以下のように解釈されます: | |
349 | ||
350 | unlink ($file || die "snafu"); | |
351 | ||
352 | =begin original | |
353 | ||
354 | To avoid this problem, either put in extra parentheses or use the | |
355 | super low precedence C<or> operator: | |
356 | ||
357 | =end original | |
358 | ||
359 | この問題を避けるためには、余計な括弧をつけるかより優先順位の低い | |
360 | C<or> 演算子を使うようにします: | |
361 | ||
362 | (unlink $file) || die "snafu"; | |
363 | unlink $file or die "snafu"; | |
364 | ||
365 | =begin original | |
366 | ||
367 | The "English" operators (C<and>, C<or>, C<xor>, and C<not>) | |
368 | deliberately have precedence lower than that of list operators for | |
369 | just such situations as the one above. | |
370 | ||
371 | =end original | |
372 | ||
373 | "English" 演算子(C<and>, C<or>, C<xor>, C<not>) は先に説明している | |
374 | 214 | 同じ働きをするリスト演算子よりも低い優先順位を故意に持たされています。 |
375 | 215 | |
376 | =begin original | |
377 | ||
378 | Another operator with surprising precedence is exponentiation. It | |
379 | binds more tightly even than unary minus, making C<-2**2> produce a | |
380 | negative not a positive four. It is also right-associating, meaning | |
381 | that C<2**3**2> is two raised to the ninth power, not eight squared. | |
382 | ||
383 | =end original | |
384 | ||
385 | 216 | もう一つの、びっくりするような優先順位を持っている演算子は |
386 | 217 | べき乗(exponentiation)です。 |
387 | これは単項のマイナスよりも強く結び付くので、C<-2**2> | |
218 | これは単項のマイナスよりも強く結び付くので、C<-2**2>はプラス4ではなく、 | |
388 | マイナス | |
219 | マイナス4を生成します。この演算子は右結合するので、 | |
389 | ||
220 | C<2**3**2>は8の二乗ではなく、2の九乗です。 | |
390 | 2 の 9 乗です。 | |
391 | 221 | |
392 | 222 | =begin original |
393 | 223 | |
394 | 224 | Although it has the same precedence as in C, Perl's C<?:> operator |
395 | 225 | produces an lvalue. This assigns $x to either $a or $b, depending |
396 | 226 | on the trueness of $maybe: |
397 | 227 | |
398 | 228 | =end original |
399 | 229 | |
400 | C と同じ優先順位を持っているにも関らず、Perl | |
230 | C と同じ優先順位を持っているにも関らず、PerlではC<?:>演算子は | |
401 | ||
231 | lvalue(代入可能な値)を作り出します。以下の代入では、$maybe の値に応じて、 | |
402 | ||
232 | $a か $b のいずれかに$xの値を代入します: | |
403 | $x の値を代入します: | |
404 | 233 | |
405 | ||
234 | ($maybe ? $a : $b) = $x; | |
406 | 235 | |
407 | 236 | =head2 How do I declare/create a structure? |
408 | 237 | |
409 | 238 | (構造体を宣言したり生成するには?) |
410 | 239 | |
411 | ||
240 | 一般的には、構造体を“宣言”することはありません。単に(おそらくは無名の) | |
241 | ハッシュリファレンスを使うだけです。 | |
242 | 詳しくはL<perlref>と L<perldsc> を参照してください。例を挙げましょう: | |
412 | 243 | |
413 | ||
244 | $person = {}; # 新しい無名ハッシュ | |
414 | ||
245 | $person->{AGE} = 24; # フィールド AGE に24を設定 | |
415 | ||
246 | $person->{NAME} = "Nat"; # フィールド NAME に "Nat"を設定 | |
416 | 247 | |
417 | =end original | |
418 | ||
419 | 一般的には、構造体を“宣言”することはありません。 | |
420 | 単に(おそらくは無名の)ハッシュリファレンスを使うだけです。 | |
421 | 詳しくは L<perlref>と L<perldsc> を参照してください。 | |
422 | 例を挙げましょう: | |
423 | ||
424 | $person = {}; # new anonymous hash | |
425 | $person->{AGE} = 24; # set field AGE to 24 | |
426 | $person->{NAME} = "Nat"; # set field NAME to "Nat" | |
427 | ||
428 | =begin original | |
429 | ||
430 | If you're looking for something a bit more rigorous, try L<perltoot>. | |
431 | ||
432 | =end original | |
433 | ||
434 | 248 | もうちょっと正確ななにかを求めているのなら、 |
435 | 249 | L<perltoot> に挑戦してみてください。 |
436 | 250 | |
437 | 251 | =head2 How do I create a module? |
438 | 252 | |
439 | 253 | (モジュールを作成するには?) |
440 | 254 | |
441 | ||
255 | モジュールとは同じ名前を持ったファイルに置かれているパッケージです。 | |
256 | 例えば、Hello::There というモジュールはHello/There.pmに置かれます。 | |
257 | 詳しいことは L<perlmod>を参照してください。L<Exporter>も助けになるでしょう。 | |
258 | C か、C と Perl を混ぜたモジュールを作ろうとしているなら、 | |
259 | L<perlxstut>で学んでおくべきでしょう。 | |
442 | 260 | |
443 | ||
261 | 以下のものは、自分のモジュールを作ろうとしたときに使いたくなるような | |
262 | 便利なテンプレートです。間違いなく名前を適切に変更してください。 | |
444 | 263 | |
445 | ||
264 | package Some::Module; # Some/Module.pmを仮定 | |
446 | 265 | |
447 | ||
266 | use strict; | |
267 | use warnings; | |
448 | 268 | |
449 | ||
269 | BEGIN { | |
270 | use Exporter (); | |
271 | our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS); | |
450 | 272 | |
451 | ||
273 | ## バージョンチェックのためのバージョンを設定。使うにはコメントを外す | |
452 | ||
274 | ## $VERSION = 1.00; | |
453 | overview of the process along with a couple of suggestions | |
454 | about style. | |
455 | 275 | |
456 | ||
276 | # RCS/VCSを使っているのなら次の行が好ましいでしょうが | |
277 | # 二桁のバージョンに注意してください | |
278 | $VERSION = do{my@r=q$Revision: 1.28 $=~/\d+/g;sprintf '%d.'.'%02d'x$#r,@r}; | |
457 | 279 | |
458 | ||
280 | @ISA = qw(Exporter); | |
459 | ||
281 | @EXPORT = qw(&func1 &func2 &func3); | |
460 | ||
282 | %EXPORT_TAGS = ( ); # eg: TAG => [ qw!name1 name2! ], | |
461 | いくつかの忠告があります。 | |
462 | 283 | |
463 | ||
284 | # exportするパッケージグローバルなものをここに置く。 | |
285 | # export可能な関数もここに置く。 | |
286 | @EXPORT_OK = qw($Var1 %Hashit); | |
287 | } | |
288 | our @EXPORT_OK; | |
464 | 289 | |
465 | ||
290 | # exported package globals go here | |
466 | ||
291 | our $Var1; | |
467 | ||
292 | our %Hashit; | |
468 | you'll need. L<perlxs> and L<perlxstut> explain the details. | |
469 | 293 | |
470 | ||
294 | # exportしないパッケージグローバルをここに | |
295 | our @more; | |
296 | our $stuff; | |
471 | 297 | |
472 | ||
298 | # パッケージグローバルを初期化する。最初にexportしているものを。 | |
473 | ||
299 | $Var1 = ''; | |
474 | ||
300 | %Hashit = (); | |
475 | 作成します。 | |
476 | L<perlxs> と L<perlxstut> は詳細を説明しています。 | |
477 | 301 | |
478 | ||
302 | # それから他のもの(これらは$Some::Module::stuffとすればアクセスできます)を初期化する | |
303 | $stuff = ''; | |
304 | @more = (); | |
479 | 305 | |
480 | ||
306 | # ファイルスコープを持った全てのlecicalは | |
481 | ||
307 | # 関数がそれ使うよりも前に生成しておかねばならない | |
482 | create a skeleton module distribution. | |
483 | 308 | |
484 | ||
309 | # ファイルプライベートなlexicalsはここに | |
310 | my $priv_var = ''; | |
311 | my %secret_hash = (); | |
485 | 312 | |
486 | ||
313 | # これはクロージャーとしてのファイルプライベートな関数で、 | |
487 | ||
314 | # &$priv_funcで呼び出しができる。これはプロトタイプを作れない。 | |
488 | ||
315 | my $priv_func = sub { | |
316 | # 内容をここに | |
317 | }; | |
489 | 318 | |
490 | ||
319 | # exportするしないに関らず、全ての関数を作成する。 | |
320 | # {}スタブを適当に埋めることを忘れないように | |
321 | sub func1 {} # プロトタイプなし | |
322 | sub func2() {} # void のプロトタイプ | |
323 | sub func3($$) {} # 二つのスカラーを取るプロトタイプ | |
491 | 324 | |
492 | ||
325 | # これはexportされないが、それでも呼び出しは可能! | |
493 | ||
326 | sub func4(\%) {} # 一つのハッシュリファレンスを取るプロトタイプ | |
494 | which is the best hands-on guide to creating module | |
495 | distributions. | |
496 | 327 | |
497 | ||
328 | END { } # モジュールの後始末コードをここに (グローバルデストラクター) | |
498 | 329 | |
499 | モジュール | |
330 | 1; # モジュールはtureを返さなければならない | |
500 | "Writing Perl Modules for CPAN" | |
501 | ( http://apress.com/book/bookDisplay.html?bID=14 ) を見るのもよいでしょう。 | |
502 | 331 | |
503 | ||
332 | h2xsというプログラムはあなたのために、重要なもの全てのためのスタブを作成します。 | |
504 | 333 | |
505 | ||
334 | % h2xs -XA -n My::Module | |
506 | 335 | |
507 | =begin original | |
508 | ||
509 | (contributed by brian d foy) | |
510 | ||
511 | =end original | |
512 | ||
513 | (brian d foy によって寄贈されました) | |
514 | ||
515 | =begin original | |
516 | ||
517 | The easiest way to take over a module is to have the current | |
518 | module maintainer either make you a co-maintainer or transfer | |
519 | the module to you. | |
520 | ||
521 | =end original | |
522 | ||
523 | モジュールを引き継ぐのに一番簡単な方法は、現在のモジュールのメンテナに | |
524 | 連絡して、共同メンテナにしてもらうか、モジュールを引き渡してもらう | |
525 | ことです。 | |
526 | ||
527 | =begin original | |
528 | ||
529 | If you can't reach the author for some reason (e.g. email bounces), | |
530 | the PAUSE admins at modules@perl.org can help. The PAUSE admins | |
531 | treat each case individually. | |
532 | ||
533 | =end original | |
534 | ||
535 | もし何らかの理由(メールが返ってきたなど)で作者と連絡が取れない場合、 | |
536 | PAUSE 管理者である modules@perl.org が助けになるかもしれません。 | |
537 | PAUSE 管理者はそれぞれの場合を個別に扱います。 | |
538 | ||
539 | =over 4 | |
540 | ||
541 | =item * | |
542 | ||
543 | =begin original | |
544 | ||
545 | Get a login for the Perl Authors Upload Server (PAUSE) if you don't | |
546 | already have one: http://pause.perl.org | |
547 | ||
548 | =end original | |
549 | ||
550 | まだ持っていないなら、the Perl Authors Upload Server (PAUSE) のアカウントを | |
551 | 取ります: http://pause.perl.org | |
552 | ||
553 | =item * | |
554 | ||
555 | =begin original | |
556 | ||
557 | Write to modules@perl.org explaining what you did to contact the | |
558 | current maintainer. The PAUSE admins will also try to reach the | |
559 | maintainer. | |
560 | ||
561 | =end original | |
562 | ||
563 | 現在のメンテナに連絡するためにしたことの説明を modules@perl.org に書きます。 | |
564 | PAUSE 管理者もメンテナに連絡を試みます。 | |
565 | ||
566 | =item * | |
567 | ||
568 | =begin original | |
569 | ||
570 | Post a public message in a heavily trafficked site announcing your | |
571 | intention to take over the module. | |
572 | ||
573 | =end original | |
574 | ||
575 | モジュールを引き継ぎたいという意思を表明するために、トラフィックの多い | |
576 | サイトに公的なメッセージを投稿します。 | |
577 | ||
578 | =item * | |
579 | ||
580 | =begin original | |
581 | ||
582 | Wait a bit. The PAUSE admins don't want to act too quickly in case | |
583 | the current maintainer is on holiday. If there's no response to | |
584 | private communication or the public post, a PAUSE admin can transfer | |
585 | it to you. | |
586 | ||
587 | =end original | |
588 | ||
589 | しばらく待ちます。 | |
590 | PAUSE 管理者は、現在のメンテナが休暇中の場合に、あまりに性急に行動したいとは | |
591 | 思いません。 | |
592 | もし私的な通信や公的な投稿に反応がない場合、PAUSE 管理者はモジュールを | |
593 | あなたに移行できます。 | |
594 | ||
595 | =back | |
596 | ||
597 | 336 | =head2 How do I create a class? |
598 | X<class, creation> X<package> | |
599 | 337 | |
600 | 338 | (クラスを作るには?) |
601 | 339 | |
602 | ||
340 | クラスとオブジェクトについての入門は L<perltoot>を参照してください。 | |
341 | 同様に、L<perlobj> と L<perlbot>も参照してください。 | |
603 | 342 | |
604 | (contributed by brian d foy) | |
605 | ||
606 | =end original | |
607 | ||
608 | (brian d foy によって寄贈されました) | |
609 | ||
610 | =begin original | |
611 | ||
612 | In Perl, a class is just a package, and methods are just subroutines. | |
613 | Perl doesn't get more formal than that and lets you set up the package | |
614 | just the way that you like it (that is, it doesn't set up anything for | |
615 | you). | |
616 | ||
617 | =end original | |
618 | ||
619 | Perl では、クラスは単なるパッケージで、メソッドは単なるサブルーチンです。 | |
620 | Perl はそれ以上に形式的なことはしませんし、あなたの好きな方法でパッケージを | |
621 | 設定できるようにします (つまり、Perl はあなたのために何の設定もしません)。 | |
622 | ||
623 | =begin original | |
624 | ||
625 | The Perl documentation has several tutorials that cover class | |
626 | creation, including L<perlboot> (Barnyard Object Oriented Tutorial), | |
627 | L<perltoot> (Tom's Object Oriented Tutorial), L<perlbot> (Bag o' | |
628 | Object Tricks), and L<perlobj>. | |
629 | ||
630 | =end original | |
631 | ||
632 | Perl の文書には、 | |
633 | L<perlboot> (Barnyard Object Oriented Tutorial), | |
634 | L<perltoot> (Tom's Object Oriented Tutorial), L<perlbot> (Bag o' | |
635 | Object Tricks), L<perlobj> といった、クラス生成に対応する | |
636 | いくつかのチュートリアルがあります。 | |
637 | ||
638 | 343 | =head2 How can I tell if a variable is tainted? |
639 | 344 | |
640 | (変数が汚染されているかどうかを確かめ | |
345 | (変数が汚染されているかどうかをどうやれば確かめられますか?) | |
641 | 346 | |
642 | ||
347 | L<perlsec/"Laundering and Detecting Tainted Data">を参照してください。 | |
348 | 以下に例を示します(これはkill()がシグナルを送る何のプロセスも | |
349 | 与えられていないのでなんのシステムコールも使いません): | |
643 | 350 | |
644 | ||
351 | sub is_tainted { | |
645 | ||
352 | return ! eval { join('',@_), kill 0; 1; }; | |
646 | ||
353 | } | |
647 | 354 | |
648 | ||
355 | しかしこれは C<-w>クリーンではありません。汚染検査を C<-w>クリーンにする | |
356 | 方法はありません。 | |
357 | これは汚染された可能性のあるデータ全てを浄化(untaint)すべき | |
358 | ヒントであると受け取ってください。 | |
649 | 359 | |
650 | CPAN にある (リリース 5.8.0 からは Perl に含まれている) Scalar::Util | |
651 | モジュールの tainted() 関数が使えます。 | |
652 | L<perlsec/"Laundering and Detecting Tainted Data"> も参照してください。 | |
653 | ||
654 | 360 | =head2 What's a closure? |
655 | 361 | |
656 | (クロージャ(closure)ってなんですか?) | |
362 | (クロージャー(closure)ってなんですか?) | |
657 | 363 | |
658 | 364 | =begin original |
659 | 365 | |
660 | 366 | Closures are documented in L<perlref>. |
661 | 367 | |
662 | 368 | =end original |
663 | 369 | |
664 | クロージャは | |
370 | クロージャーはL<perlref>に説明があります。 | |
665 | 371 | |
666 | ||
372 | B<クロージャー>は、きちんとした定義を持ったコンピューター科学の用語ですが | |
667 | ||
668 | I<Closure> is a computer science term with a precise but | |
669 | hard-to-explain meaning. Usually, closures are implemented in Perl as | |
670 | anonymous subroutines with lasting references to lexical variables | |
671 | outside their own scopes. These lexicals magically refer to the | |
672 | variables that were around when the subroutine was defined (deep | |
673 | binding). | |
674 | ||
675 | =end original | |
676 | ||
677 | B<クロージャ> は、きちんとした定義を持ったコンピュータ科学の用語ですが | |
678 | 373 | その意味を説明するのはとても難しいのです。 |
679 | クロージャは Perl では、そのスコープの外側でもレキシカル変数に対する | |
374 | クロージャーは Perl では、そのスコープの外側でもレキシカル変数に対する | |
680 | 375 | リファレンスを保持しつづける無名サブルーチンとして実装されています。 |
681 | 376 | これらのレキシカルは、サブルーチンが定義されたときの変数に対して、 |
682 | 377 | 魔法のような参照(magically refer)を行います(深い束縛、deep binding)。 |
683 | 378 | |
684 | ||
379 | クロージャーは、Perlができるような関数の戻り値として関数それ自身を返す関数を | |
380 | 持つことができるプログラミング言語で意味があります。 | |
686 | ||
381 | 一部の言語では、無名関数を提供しているけれども適切なクロージャーを提供する | |
687 | have the return value of a function be itself a function, as you can | |
688 | in Perl. Note that some languages provide anonymous functions but are | |
689 | not capable of providing proper closures: the Python language, for | |
690 | example. For more information on closures, check out any textbook on | |
691 | functional programming. Scheme is a language that not only supports | |
692 | but encourages closures. | |
693 | ||
694 | =end original | |
695 | ||
696 | クロージャは、Perl ができるような関数の戻り値として関数それ自身を返す関数を | |
697 | 持つことができるプログラミング言語でもっともよく使われます。 | |
698 | 一部の言語では、無名関数を提供しているけれども適切なクロージャを提供する | |
699 | 382 | 能力はないということに注意してください。 |
700 | 383 | たとえば Python がそうです。 |
701 | クロージャに関するより詳しいことは、関数言語に関するなんらかの教科書を | |
384 | クロージャーに関するより詳しいことは、関数言語に関するなんらかの教科書を | |
702 | 385 | みてください。 |
703 | Scheme はクロージャをサポートするだけでなく、それを推奨している言語です。 | |
386 | Scheme はクロージャーをサポートするだけでなく、それを推奨している言語です。 | |
704 | 387 | |
705 | ||
388 | 以下は、古典的な関数を生成する関数です: | |
706 | 389 | |
707 | ||
390 | sub add_function_generator { | |
391 | return sub { shift + shift }; | |
392 | } | |
708 | 393 | |
709 | ||
394 | $add_sub = add_function_generator(); | |
395 | $sum = $add_sub->(4,5); # $sum はここで 9 となる | |
710 | 396 | |
711 | ||
397 | クロージャーは、後で埋めることのできるカスタマイズ可能な幾つかのスロットを持つ | |
398 | B<関数テンプレート>のように働きます。 | |
713 | sub add_function_generator { | |
714 | return sub { shift() + shift() }; | |
715 | } | |
716 | ||
717 | $add_sub = add_function_generator(); | |
718 | $sum = $add_sub->(4,5); # $sum is 9 now. | |
719 | ||
720 | =begin original | |
721 | ||
722 | The anonymous subroutine returned by add_function_generator() isn't | |
723 | technically a closure because it refers to no lexicals outside its own | |
724 | scope. Using a closure gives you a I<function template> with some | |
725 | customization slots left out to be filled later. | |
726 | ||
727 | =end original | |
728 | ||
729 | 399 | add_function_generator() が返した無名サブルーチンは技術的には |
730 | クロージャではありません。 | |
400 | クロージャーではありません。 | |
731 | 401 | なぜなら、あれはスコープの外側で参照するようなレキシカルがないからです。 |
732 | クロージャを使うことによって、後で埋めることのできるカスタマイズ可能な | |
733 | 幾つかのスロットを持つ B<関数テンプレート> のように働きます。 | |
734 | 402 | |
735 | ||
403 | それとは対照的に、次の関数 make_adder()では、関数自身のスコープの外側で | |
736 | ||
737 | Contrast this with the following make_adder() function, in which the | |
738 | returned anonymous function contains a reference to a lexical variable | |
739 | outside the scope of that function itself. Such a reference requires | |
740 | that Perl return a proper closure, thus locking in for all time the | |
741 | value that the lexical had when the function was created. | |
742 | ||
743 | =end original | |
744 | ||
745 | それとは対照的に、次の関数 make_adder() では、関数自身のスコープの外側で | |
746 | 404 | レキシカル変数に対するリファレンスを持つ無名関数を返します。 |
747 | そのようなリファレンスは Perl が適切なクロージャを返すように要求するので、 | |
405 | そのようなリファレンスは Perl が適切なクロージャーを返すように要求するので、 | |
748 | 406 | その変数を参照するときはいつでも関数が生成されたときの |
749 | 407 | レキシカルが参照されます。 |
750 | 408 | |
751 | ||
409 | sub make_adder { | |
752 | ||
410 | my $addpiece = shift; | |
753 | ||
411 | return sub { shift + $addpiece }; | |
754 | ||
412 | } | |
755 | 413 | |
756 | ||
414 | $f1 = make_adder(20); | |
757 | ||
415 | $f2 = make_adder(555); | |
758 | 416 | |
759 | ||
417 | これで、C<&$f1($n)>は それに渡した $nに対して常に20を加え、 | |
418 | C<&$f2($n)>は渡された $nに常に555を加えます。 | |
419 | クロージャーの中にある $addpieceが仕事をしています。 | |
760 | 420 | |
761 | Now C<&$f1($n)> is always 20 plus whatever $n you pass in, whereas | |
762 | C<&$f2($n)> is always 555 plus whatever $n you pass in. The $addpiece | |
763 | in the closure sticks around. | |
764 | 421 | |
765 | ||
422 | クロージャーは、それほど難しくない状況でよく使われます。たとえば、 | |
423 | 関数にちょっとしたコードを押しこみたいときがそうです: | |
766 | 424 | |
767 | ||
425 | my $line; | |
768 | ||
426 | timeout( 30, sub { $line = <STDIN> } ); | |
769 | クロージャの中にある $addpiece が仕事をしています。 | |
770 | 427 | |
771 | 428 | =begin original |
772 | 429 | |
773 | Closures are often used for less esoteric purposes. For example, when | |
774 | you want to pass in a bit of code into a function: | |
775 | ||
776 | =end original | |
777 | ||
778 | クロージャは、それほど難しくない状況でよく使われます。 | |
779 | たとえば、関数にちょっとしたコードを押しこみたいときがそうです: | |
780 | ||
781 | my $line; | |
782 | timeout( 30, sub { $line = <STDIN> } ); | |
783 | ||
784 | =begin original | |
785 | ||
786 | 430 | If the code to execute had been passed in as a string, |
787 | 431 | C<< '$line = <STDIN>' >>, there would have been no way for the |
788 | 432 | hypothetical timeout() function to access the lexical variable |
789 | 433 | $line back in its caller's scope. |
790 | 434 | |
791 | 435 | =end original |
792 | 436 | |
793 | 437 | もし実行すべきコードが文字列として渡されていたのであれば、 |
794 | C<< '$line = <STDIN>' >> としているところは、仮想的な timeout() | |
438 | C<< '$line = <STDIN>' >> としているところは、仮想的な timeout() | |
795 | アクセスする | |
439 | 関数が アクセスする lexical変数 $lineを呼び出し元のスコープのものに戻す | |
796 | 440 | 手段がなくなってしまいます。 |
797 | 441 | |
798 | =begin original | |
799 | ||
800 | Another use for a closure is to make a variable I<private> to a | |
801 | named subroutine, e.g. a counter that gets initialized at creation | |
802 | time of the sub and can only be modified from within the sub. | |
803 | This is sometimes used with a BEGIN block in package files to make | |
804 | sure a variable doesn't get meddled with during the lifetime of the | |
805 | package: | |
806 | ||
807 | =end original | |
808 | ||
809 | もう一つのクロージャの使用法は、ある変数を名前つきサブルーチンで | |
810 | I<プライベート> にすることです; 例えば、サブルーチンの作成時に初期化され、 | |
811 | サブルーチン内でのみ変更可能なカウンタです。 | |
812 | これは、パッケージの生存期間中に変数が干渉されることがないように、とき毒 | |
813 | パッケージファイルの BEGIN ブロックで使われます: | |
814 | ||
815 | BEGIN { | |
816 | my $id = 0; | |
817 | sub next_id { ++$id } | |
818 | } | |
819 | ||
820 | =begin original | |
821 | ||
822 | This is discussed in more detail in L<perlsub>; see the entry on | |
823 | I<Persistent Private Variables>. | |
824 | ||
825 | =end original | |
826 | ||
827 | これは L<perlsub> でより詳しく議論されています; | |
828 | I<Persistent Private Variables> のエントリを参照してください。 | |
829 | ||
830 | 442 | =head2 What is variable suicide and how can I prevent it? |
831 | 443 | |
832 | 444 | (変数の自殺(variable suicide)って何で、それをどうすれば防げますか?) |
833 | 445 | |
834 | 446 | =begin original |
835 | 447 | |
836 | ||
448 | Variable suicide is when you (temporarily or permanently) lose the | |
837 | ||
449 | value of a variable. It is caused by scoping through my() and local() | |
450 | interacting with either closures or aliased foreach() iterator | |
451 | variables and subroutine arguments. It used to be easy to | |
452 | inadvertently lose a variable's value this way, but now it's much | |
453 | harder. Take this code: | |
838 | 454 | |
839 | 455 | =end original |
840 | 456 | |
841 | ||
457 | 変数の自殺とは、(一時的にしろ、恒久的にしろ)変数の値を失ったときのことを指します。 | |
842 | ||
458 | これは、クロージャー、もしくは別名つけされた foreach イテレーター変数や | |
459 | サブルーチンの引数と相互作用しているmy()やlocal()を通したスコープによって | |
460 | 引き起こされます。 | |
461 | 以前はこのやり方で変数の値をうっかりとなくしてしまうように使われがちでしたが、 | |
462 | 現在は非常に難しくなっています。以下のコードを考えてみましょう: | |
843 | 463 | |
844 | = | |
464 | my $f = "foo"; | |
465 | sub T { | |
466 | while ($i++ < 3) { my $f = $f; $f .= "bar"; print $f, "\n" } | |
467 | } | |
468 | T; | |
469 | print "Finally $f\n"; | |
845 | 470 | |
846 | Variable suicide is when you (temporarily or permanently) lose the value | |
847 | of a variable. It is caused by scoping through my() and local() | |
848 | interacting with either closures or aliased foreach() iterator variables | |
849 | and subroutine arguments. It used to be easy to inadvertently lose a | |
850 | variable's value this way, but now it's much harder. Take this code: | |
851 | ||
852 | =end original | |
853 | ||
854 | 変数の自殺とは、(一時的にしろ、恒久的にしろ)変数の値を失ったときのことを | |
855 | 指します。 | |
856 | これは、クロージャ、もしくは別名つけされた foreach イテレータ変数や | |
857 | サブルーチンの引数と相互作用している my() や local() を通した | |
858 | スコープによって引き起こされます。 | |
859 | 以前はこのやり方で変数の値をうっかりとなくしてしまうように | |
860 | 使われがちでしたが、現在は非常に難しくなっています。 | |
861 | 以下のコードを考えてみましょう: | |
862 | ||
863 | my $f = 'foo'; | |
864 | sub T { | |
865 | while ($i++ < 3) { my $f = $f; $f .= "bar"; print $f, "\n" } | |
866 | } | |
867 | ||
868 | T; | |
869 | print "Finally $f\n"; | |
870 | ||
871 | 471 | =begin original |
872 | 472 | |
873 | If you are experiencing variable suicide, that C<my $f> in the subroutine | |
874 | doesn't pick up a fresh copy of the C<$f> whose value is <foo>. The output | |
875 | shows that inside the subroutine the value of C<$f> leaks through when it | |
876 | shouldn't, as in this output: | |
877 | ||
878 | =end original | |
879 | ||
880 | もし変数の自殺に遭遇したら、サブルーチン内の C<my $f> は、値が C<foo> である | |
881 | C<$f> の最新のコピーをではありません。 | |
882 | この出力は、漏れてはいけないサブルーチン内の C<$f> の値が漏れていることを | |
883 | 示し、以下のようになります: | |
884 | ||
885 | foobar | |
886 | foobarbar | |
887 | foobarbarbar | |
888 | Finally foo | |
889 | ||
890 | =begin original | |
891 | ||
892 | 473 | The $f that has "bar" added to it three times should be a new C<$f> |
893 | C<my $f> should create a new l | |
474 | (C<my $f> should create a new local variable each time through the loop). | |
894 | ||
475 | It isn't, however. This was a bug, now fixed in the latest releases | |
476 | (tested against 5.004_05, 5.005_03, and 5.005_56). | |
895 | 477 | |
896 | 478 | =end original |
897 | 479 | |
898 | "bar" を保持している | |
480 | "bar" を保持している$fは三回 new C<$f>されるべきものです (C<my $f>は、 | |
899 | ループが通る度に新たな | |
481 | ループが通る度に新たなローカル変数を生成すべきなのです)。 | |
900 | ||
482 | しかしそうではありません。これはバグでした。 | |
483 | 最新のリリース(5.004_05, 5.005_03, and 5.005_56)では修正されています。 | |
901 | 484 | |
902 | foobar | |
903 | foobar | |
904 | foobar | |
905 | Finally foo | |
906 | ||
907 | 485 | =head2 How can I pass/return a {Function, FileHandle, Array, Hash, Method, Regex}? |
908 | 486 | |
909 | ({関数, ファイルハンドル, 配列, ハッシュ, メソッド, 正規表現} を渡したり返 | |
487 | ({関数, ファイルハンドル, 配列, ハッシュ, メソッド, 正規表現} を渡したり返すのは | |
488 | どうすればできますか?) | |
910 | 489 | |
490 | 正規表現を除いて、これらのオブジェクトのリファレンスを渡す必要があります。 | |
491 | L<perlsub/"Pass by Reference">にある関連した質問と、 | |
492 | L<perlref>にあるリファレンスに関する情報を参照してください。 | |
493 | ||
911 | 494 | =begin original |
912 | 495 | |
913 | ||
496 | See ``Passing Regexes'', below, for information on passing regular | |
914 | ||
497 | expressions. | |
915 | information on references. | |
916 | 498 | |
917 | 499 | =end original |
918 | 500 | |
919 | ||
501 | 正規表現を渡すための情報については、下記の``Passing Regexes''を | |
920 | ||
502 | 参照して下さい。 | |
921 | L<perlref> にあるリファレンスに関する情報を参照してください。 | |
922 | 503 | |
923 | 504 | =over 4 |
924 | 505 | |
925 | 506 | =item Passing Variables and Functions |
926 | 507 | |
927 | 508 | (変数や関数を渡す) |
928 | 509 | |
929 | =begin original | |
930 | ||
931 | Regular variables and functions are quite easy to pass: just pass in a | |
932 | reference to an existing or anonymous variable or function: | |
933 | ||
934 | =end original | |
935 | ||
936 | 510 | 普通の変数や関数はとても簡単に渡せます: 既に存在している変数や関数に対する |
937 | 511 | リファレンスか、無名変数や無名関数に対するリファレンスを渡せばよいのです。 |
938 | 512 | |
939 | ||
513 | func( \$some_scalar ); | |
940 | 514 | |
941 | ||
515 | func( \@some_array ); | |
942 | ||
516 | func( [ 1 .. 10 ] ); | |
943 | 517 | |
944 | ||
518 | func( \%some_hash ); | |
945 | ||
519 | func( { this => 10, that => 20 } ); | |
946 | 520 | |
947 | ||
521 | func( \&some_func ); | |
948 | ||
522 | func( sub { $_[0] ** $_[1] } ); | |
949 | 523 | |
950 | =item | |
524 | =item ファイルハンドルを渡す | |
951 | 525 | |
952 | 526 | =begin original |
953 | 527 | |
954 | ||
528 | To pass filehandles to subroutines, use the C<*FH> or C<\*FH> notations. | |
955 | which you treat as any other scalar. | |
956 | ||
957 | =end original | |
958 | ||
959 | Perl 5.6 から、他のスカラと同様にファイルハンドルもスカラ変数で扱えます。 | |
960 | ||
961 | open my $fh, $filename or die "Cannot open $filename! $!"; | |
962 | func( $fh ); | |
963 | ||
964 | sub func { | |
965 | my $passed_fh = shift; | |
966 | ||
967 | my $line = <$passed_fh>; | |
968 | } | |
969 | ||
970 | =begin original | |
971 | ||
972 | Before Perl 5.6, you had to use the C<*FH> or C<\*FH> notations. | |
973 | 529 | These are "typeglobs"--see L<perldata/"Typeglobs and Filehandles"> |
974 | 530 | and especially L<perlsub/"Pass by Reference"> for more information. |
975 | 531 | |
976 | 532 | =end original |
977 | 533 | |
978 | ||
534 | サブルーチンにファイルハンドルを渡すには、C<*FH>やC<\*FH>といった記法を使います。 | |
979 | 535 | これらは“型グロブ”(typeglob)です。 |
980 | L<perldata/"Typeglobs and Filehandles">) | |
536 | L<perldata/"Typeglobs and Filehandles">)とL<perlsub/"Pass by Reference">に | |
981 | 537 | 詳しい説明があります。 |
982 | 538 | |
983 | ||
539 | 以下はその抜粋です: | |
984 | 540 | |
985 | ||
541 | ファイルハンドルを渡すのであれば | |
542 | *STDOUTのような裸の型グロブを使うこともできますが、 | |
543 | 型グロブのリファレンスを渡す方がもっと良いです。 | |
544 | なぜなら、そうしておけばC<use strict 'refs'>の状態でも正しく働くからです。 | |
545 | 例を挙げましょう: | |
986 | 546 | |
987 | ||
547 | splutter(\*STDOUT); | |
548 | sub splutter { | |
549 | my $fh = shift; | |
550 | print $fh "her um well a hmmm\n"; | |
551 | } | |
988 | 552 | |
989 | ||
553 | $rec = get_rec(\*STDIN); | |
990 | ||
554 | sub get_rec { | |
991 | ||
555 | my $fh = shift; | |
556 | return scalar <$fh>; | |
557 | } | |
992 | 558 | |
993 | ||
559 | もし新しいファイルハンドルを生成しようと考えているのなら、 | |
560 | 以下のようにすればできます: | |
994 | 561 | |
995 | ||
562 | sub openit { | |
996 | ||
563 | my $path = shift; | |
564 | local *FH; | |
565 | return open (FH, $path) ? *FH : undef; | |
566 | } | |
567 | $fh = openit('< /etc/motd'); | |
568 | print <$fh>; | |
997 | 569 | |
998 | ||
570 | =item 正規表現を渡す | |
999 | my ($val1, $regex) = @_; | |
1000 | my $retval = $val1 =~ /$regex/; | |
1001 | return $retval; | |
1002 | } | |
1003 | $match = compare("old McDonald", qr/d.*D/i); | |
1004 | 571 | |
1005 | =item Passing Methods | |
1006 | ||
1007 | (メソッドを渡す) | |
1008 | ||
1009 | 572 | =begin original |
1010 | 573 | |
1011 | To pass an o | |
574 | To pass regexes around, you'll need to be using a release of Perl | |
575 | sufficiently recent as to support the C<qr//> construct, pass around | |
576 | strings and use an exception-trapping eval, or else be very, very clever. | |
1012 | 577 | |
1013 | 578 | =end original |
1014 | 579 | |
1015 | ||
580 | 正規表現を渡すには、C<qr//> 構造が利用可能な最近の Perl を使うか、 | |
581 | 文字列を渡したあとで exception-trapping evalを使うか、 | |
582 | さもなければとてもとても賢明でなければなりません。 | |
1016 | 583 | |
1017 | call_a_lot(10, $some_obj, "methname") | |
1018 | sub call_a_lot { | |
1019 | my ($count, $widget, $trick) = @_; | |
1020 | for (my $i = 0; $i < $count; $i++) { | |
1021 | $widget->$trick(); | |
1022 | } | |
1023 | } | |
1024 | ||
1025 | 584 | =begin original |
1026 | 585 | |
1027 | ||
586 | Here's an example of how to pass in a string to be regex compared | |
1028 | ||
587 | using C<qr//>: | |
1029 | 588 | |
1030 | 589 | =end original |
1031 | 590 | |
1032 | ||
591 | 以下の例は、c<qr//>を使って正規表現として比較すべき文字列を渡す方法の例です: | |
1033 | クロージャを使うこともできます: | |
1034 | 592 | |
1035 | ||
593 | sub compare($$) { | |
1036 | ||
594 | my ($val1, $regex) = @_; | |
1037 | ||
595 | my $retval = $val1 =~ /$regex/; | |
1038 | | |
596 | return $retval; | |
1039 | ||
597 | } | |
1040 | ||
598 | $match = compare("old McDonald", qr/d.*D/i); | |
1041 | 599 | |
1042 | 600 | =begin original |
1043 | 601 | |
1044 | ||
602 | Notice how C<qr//> allows flags at the end. That pattern was compiled | |
1045 | ||
603 | at compile time, although it was executed later. The nifty C<qr//> | |
604 | notation wasn't introduced until the 5.005 release. Before that, you | |
605 | had to approach this problem much less intuitively. For example, here | |
606 | it is again if you don't have C<qr//>: | |
1046 | 607 | |
1047 | 608 | =end original |
1048 | 609 | |
1049 | ||
610 | C<qr//> の末尾に付けることができるフラグに注意してください。 | |
1050 | ||
611 | このパターンは一度だけコンパイル時にコンパイルされ、 | |
612 | 実行時にはコンパイルされません。 | |
613 | このしゃれた C<qr//> 記法は 5.005 リリースで初めて提供されました。 | |
614 | それまでは、この問題に対して遥かに直感的でない手法をとらなければなりませんでした。 | |
615 | 例えば、先ほどのコードを C<qr//> なしで書くと: | |
1051 | 616 | |
1052 | ||
617 | sub compare($$) { | |
618 | my ($val1, $regex) = @_; | |
619 | my $retval = eval { $val1 =~ /$regex/ }; | |
620 | die if $@; | |
621 | return $retval; | |
622 | } | |
1053 | 623 | |
1054 | ||
624 | $match = compare("old McDonald", q/($?i)d.*D/); | |
1055 | 625 | |
1056 | ||
626 | 決して以下のようにしてはいけません: | |
1057 | 627 | |
1058 | ||
628 | return eval "\$val =~ /$regex/"; # 間違い | |
1059 | 629 | |
1060 | ||
630 | あるいは、誰かがevalの二重展開とダブルクォートで括られた文字列のために、 | |
631 | 正規表現に妙なシェルエスケープを押し込むかもしれません。 | |
632 | 例を挙げましょう: | |
1061 | 633 | |
1062 | ||
634 | $pattern_of_evil = 'danger ${ system("rm -rf * &") } danger'; | |
1063 | 635 | |
1064 | ||
636 | eval "\$string =~ /$pattern_of_evil/"; | |
1065 | 637 | |
1066 | 638 | =begin original |
1067 | 639 | |
1068 | ||
640 | Those preferring to be very, very clever might see the O'Reilly book, | |
1069 | ||
641 | I<Mastering Regular Expressions>, by Jeffrey Friedl. Page 273's | |
1070 | to | |
642 | Build_MatchMany_Function() is particularly interesting. A complete | |
643 | citation of this book is given in L<perlfaq2>. | |
1071 | 644 | |
1072 | 645 | =end original |
1073 | 646 | |
1074 | ||
647 | これらのことに関してとてもとても賢明になるには、 | |
1075 | ||
648 | Jeffrey Friedl による O'Reillyの本 I<Mastering Regular Expressions>を | |
1076 | ||
649 | 読むことでしょう。 | |
650 | 特に 273ページにある Build_MatchMany_Function()は興味深いものです。 | |
651 | この本に関する完全な情報は L<perlfaq2>にあります。 | |
1077 | 652 | |
1078 | ||
653 | =item Passing Methods | |
1079 | 654 | |
1080 | ||
655 | (メソッドを渡す) | |
1081 | 656 | |
1082 | ||
657 | サブルーチンにオブジェクトメソッドを渡すには、以下のようにできます: | |
1083 | out of scope. In this example, you define the subroutine C<counter>, and | |
1084 | it uses the lexical variable C<$count>. Since you wrap this in a BEGIN | |
1085 | block, C<$count> is defined at compile-time, but also goes out of | |
1086 | scope at the end of the BEGIN block. The BEGIN block also ensures that | |
1087 | the subroutine and the value it uses is defined at compile-time so the | |
1088 | subroutine is ready to use just like any other subroutine, and you can | |
1089 | put this code in the same place as other subroutines in the program | |
1090 | text (i.e. at the end of the code, typically). The subroutine | |
1091 | C<counter> still has a reference to the data, and is the only way you | |
1092 | can access the value (and each time you do, you increment the value). | |
1093 | The data in chunk of memory defined by C<$count> is private to | |
1094 | C<counter>. | |
1095 | 658 | |
1096 | ||
659 | call_a_lot(10, $some_obj, "methname") | |
660 | sub call_a_lot { | |
661 | my ($count, $widget, $trick) = @_; | |
662 | for (my $i = 0; $i < $count; $i++) { | |
663 | $widget->$trick(); | |
664 | } | |
665 | } | |
1097 | 666 | |
1098 | ||
667 | オブジェクトとそのメソッド呼び出しと引数とをまとめるために | |
1099 | ||
668 | クロージャーを使うこともできます: | |
1100 | この例では、サブルーチン C<counter> を定義し、そこでレキシカル変数 | |
1101 | C<$count> を使います。 | |
1102 | これを BEGIN ブロックで囲っているので、C<$count> はコンパイル時に | |
1103 | 定義されますが、BEGIN ブロックの終わりでスコープから外れます。 | |
1104 | BEGIN ブロックはまた、サブルーチンとそこで使われている値はコンパイル時に | |
1105 | 定義されるので、このサブルーチンはその他のどのサブルーチンからも | |
1106 | 使える準備が出来ていて、このコードをプログラムテキスト中の他のサブルーチンと | |
1107 | 同様に同じ場所(典型的には、コードの最後)にこのコードを置けることを保証します。 | |
1108 | サブルーチン C<counter> は未だにデータへのリファレンスを持っていて、 | |
1109 | これが値にアクセスできる唯一の方法です(そしてそうする度に、値が | |
1110 | インクリメントされます)。 | |
1111 | C<$count> で定義されたメモリの塊にあるデータは C<counter> に | |
1112 | プライベートです。 | |
1113 | 669 | |
1114 | ||
670 | my $whatnot = sub { $some_obj->obfuscate(@args) }; | |
1115 | ||
671 | func($whatnot); | |
1116 | ||
672 | sub func { | |
1117 | ||
673 | my $code = shift; | |
674 | &$code(); | |
675 | } | |
1118 | 676 | |
1119 | ||
677 | UNIVERSALクラスにあるcan()メソッドを試すこともできます | |
678 | (これは標準の perl 配布キットの一部です)。 | |
1120 | 679 | |
1121 | ||
680 | =back | |
1122 | 681 | |
1123 | ||
682 | =head2 How do I create a static variable? | |
1124 | 683 | |
684 | (静的変数(static variable)を作るには?) | |
685 | ||
686 | Perlにおけるほとんどの事柄と同様、TMTOWTDI(やり方はいくつもあります)です。 | |
687 | 他の言語における“静的変数”は、Perlでは関数にプライベート | |
688 | (その変数が宣言されたある単一の関数でのみ可視で、 | |
689 | 関数の呼び出しの間その値を保っている)な変数かファイルに | |
690 | プライベート(変数が宣言されたファイルにある関数からのみ可視)な変数の | |
691 | いずれかになります。 | |
692 | ||
1125 | 693 | =begin original |
1126 | 694 | |
1127 | ||
695 | Here's code to implement a function-private variable: | |
1128 | because only one function remembered its reference. You could define | |
1129 | multiple functions while the variable is in scope, and each function | |
1130 | can share the "private" variable. It's not really "static" because you | |
1131 | can access it outside the function while the lexical variable is in | |
1132 | scope, and even create references to it. In this example, | |
1133 | C<increment_count> and C<return_count> share the variable. One | |
1134 | function adds to the value and the other simply returns the value. | |
1135 | They can both access C<$count>, and since it has gone out of scope, | |
1136 | there is no other way to access it. | |
1137 | 696 | |
1138 | 697 | =end original |
1139 | 698 | |
1140 | ||
699 | 以下の例は、関数にプライベートな変数を実装するものです: | |
1141 | ただ一つの関数だけがこのリファレンスを覚えているからです。 | |
1142 | 変数がスコープ内にある間に複数の関数を定義でき、各関数は「プライベート」 | |
1143 | 変数を共有できます。 | |
1144 | レキシカル変数がスコープ内ではあるけれども関数の外側から変数にアクセスでき、 | |
1145 | それに対するリファレンスすら作成できるため、これは実際には | |
1146 | 「静的」変数ではありません。 | |
1147 | この例では、C<increment_count> と C<return_count> は変数を共有しています。 | |
1148 | 片方の関数が値を加え、もう片方は単に値を返します。 | |
1149 | これらは両方とも C<$count> にアクセスでき、スコープ外から離れるため、 | |
1150 | これにアクセスするその他の方法はありません。 | |
1151 | 700 | |
1152 | ||
701 | BEGIN { | |
1153 | ||
702 | my $counter = 42; | |
1154 | ||
703 | sub prev_counter { return --$counter } | |
1155 | ||
704 | sub next_counter { return $counter++ } | |
1156 | ||
705 | } | |
1157 | 706 | |
707 | これで、prev_counter() と next_counter() はコンパイル時に初期化された | |
708 | $counter というプライベート変数を共有します。 | |
709 | ||
1158 | 710 | =begin original |
1159 | 711 | |
1160 | To declare a file-private variable, you still use a | |
712 | To declare a file-private variable, you'll still use a my(), putting | |
1161 | ||
713 | the declaration at the outer scope level at the top of the file. | |
1162 | ||
714 | Assume this is in file Pax.pm: | |
1163 | 715 | |
1164 | 716 | =end original |
1165 | 717 | |
1166 | ファイルプライベートな変数を宣言するには | |
718 | ファイルプライベートな変数を宣言するにはmy()を使う必要があり、 | |
1167 | ファイル | |
719 | ファイルの先頭で outer スコープレベルに宣言を置きます。 | |
1168 | ||
720 | 以下のコードが Pax.pm というファイルにあったと仮定します: | |
1169 | 721 | |
722 | package Pax; | |
723 | my $started = scalar(localtime(time())); | |
724 | ||
725 | sub begun { return $started } | |
726 | ||
1170 | 727 | =begin original |
1171 | 728 | |
1172 | ||
729 | When C<use Pax> or C<require Pax> loads this module, the variable will | |
1173 | ||
730 | be initialized. It won't get garbage-collected the way most variables | |
1174 | ||
731 | going out of scope do, because the begun() function cares about it, | |
1175 | ||
732 | but no one else can get it. It is not called $Pax::started because | |
733 | its scope is unrelated to the package. It's scoped to the file. You | |
734 | could conceivably have several packages in that same file all | |
735 | accessing the same private variable, but another file with the same | |
736 | package couldn't get to it. | |
1176 | 737 | |
1177 | 738 | =end original |
1178 | 739 | |
1179 | ||
740 | C<use Pax> か C<require Pax> によってこのモジュールがロードされたとき、 | |
1180 | ||
741 | この変数は初期化されます。 | |
1181 | ||
742 | この変数はほとんどの変数がスコープから外れたときのように | |
1182 | ||
743 | ガーベッジコレクトされることはありません。 | |
1183 | ||
744 | なぜなら、begun()関数はこの変数を使っていて、他の誰もこの変数を | |
745 | 手に入れることはできないからです。 | |
746 | これは $Pax::started と呼び出すこともできません。 | |
747 | なぜなら、そのスコープはパッケージには関係ないものだからです。 | |
748 | それはファイルスコープなのです。 | |
749 | 同じファイル中で同じプライベート変数をアクセスできるけれども、 | |
750 | 同じパッケージの他のファイルが変数にアクセスすることはできないような、 | |
751 | 幾つかのパッケージを持つことができると想像できるでしょう | |
1184 | 752 | |
753 | 詳しくは L<perlsub/"Perstent Private Variables"> を参照してください。 | |
754 | ||
1185 | 755 | =head2 What's the difference between dynamic and lexical (static) scoping? Between local() and my()? |
1186 | 756 | |
1187 | (動的スコープとレキシカル | |
757 | (動的スコープ(dynamic scoping)とレキシカルスコープ(lexical scoping, もしくは | |
758 | static scoping)との間の違いは何ですか? local()とmy()との違いは?) | |
1188 | 759 | |
1189 | 760 | =begin original |
1190 | 761 | |
1191 | 762 | C<local($x)> saves away the old value of the global variable C<$x> |
1192 | 763 | and assigns a new value for the duration of the subroutine I<which is |
1193 | 764 | visible in other functions called from that subroutine>. This is done |
1194 | 765 | at run-time, so is called dynamic scoping. local() always affects global |
1195 | 766 | variables, also called package variables or dynamic variables. |
1196 | 767 | |
1197 | 768 | =end original |
1198 | 769 | |
1199 | C<local($x)> は、 | |
770 | C<local($x)> は、C<$x>のグローバルな値を保存して、サブルーチンの | |
1200 | I<そのサブルーチンから呼び出された関数から参照できる> サブルーチンの | |
1201 | 771 | 存在する間の新たな値を代入します。 |
1202 | こ | |
772 | この変数はB<そのサブルーチンから呼び出された関数から参照できます>。 | |
773 | これはコンパイル時ではなく、実行時に行われます。 | |
1203 | 774 | local() は常にグローバル変数に対して作用し、 |
1204 | パッケージ変数とか動的変数と呼ばれることもあります。 | |
775 | パッケージ変数とか動的変数(dynamic variables)と呼ばれることもあります。 | |
1205 | 776 | |
1206 | 777 | =begin original |
1207 | 778 | |
1208 | 779 | C<my($x)> creates a new variable that is only visible in the current |
1209 | 780 | subroutine. This is done at compile-time, so it is called lexical or |
1210 | 781 | static scoping. my() always affects private variables, also called |
1211 | 782 | lexical variables or (improperly) static(ly scoped) variables. |
1212 | 783 | |
1213 | 784 | =end original |
1214 | 785 | |
1215 | C<my($x)> | |
786 | C<my($x)>は、現在のサブルーチンにおいてのみ参照できる変数を新たに生成します。 | |
1216 | 生成します。 | |
1217 | 787 | これはコンパイル時に行われるので、レキシカルスコープだとか |
1218 | 静的スコープと呼ばれます。 | |
788 | 静的スコープと呼ばれます。my()はプライベート変数に作用し、 | |
1219 | ||
789 | レキシカル変数とか(間違って)静的(スコープを持った)変数と呼ばれることもあります。 | |
1220 | (スコープを持った)変数と呼ばれることもあります。 | |
1221 | 790 | |
1222 | =begin original | |
1223 | ||
1224 | For instance: | |
1225 | ||
1226 | =end original | |
1227 | ||
1228 | 791 | 例を挙げましょう: |
1229 | 792 | |
1230 | ||
793 | sub visible { | |
1231 | | |
794 | print "var has value $var\n"; | |
1232 | ||
795 | } | |
1233 | 796 | |
1234 | ||
797 | sub dynamic { | |
1235 | | |
798 | local $var = 'local'; # $var という名前の変数の新たなす | |
1236 | | |
799 | visible(); # テンポラリ値はまだグローバルです | |
1237 | ||
800 | } | |
1238 | 801 | |
1239 | ||
802 | sub lexical { | |
1240 | | |
803 | my $var = 'private'; # 新しいプライベート変数である $var | |
1241 | | |
804 | visible(); # (subスコープの外側では不可視です) | |
1242 | ||
805 | } | |
1243 | 806 | |
1244 | ||
807 | $var = 'global'; | |
1245 | 808 | |
1246 | ||
809 | visible(); # グローバルなものを出力 | |
1247 | ||
810 | dynamic(); # ローカルなものを出力 | |
1248 | ||
811 | lexical(); # グローバルなものを出力 | |
1249 | 812 | |
1250 | =begin original | |
1251 | ||
1252 | Notice how at no point does the value "private" get printed. That's | |
1253 | because $var only has that value within the block of the lexical() | |
1254 | function, and it is hidden from the called subroutine. | |
1255 | ||
1256 | =end original | |
1257 | ||
1258 | 813 | "private"という値を出力させる方法がないということに注目してください。 |
1259 | これは、$var | |
814 | これは、$varが関数の lexical()なブロックの内側でのみその値を持っていて、 | |
1260 | 815 | そこから呼び出されたサブルーチンからは隠されてしまうからです。 |
1261 | 816 | |
1262 | ||
817 | まとめると、local()はあなたがプライベートと考えるようなことをローカルな | |
1263 | ||
1264 | In summary, local() doesn't make what you think of as private, local | |
1265 | variables. It gives a global variable a temporary value. my() is | |
1266 | what you're looking for if you want private variables. | |
1267 | ||
1268 | =end original | |
1269 | ||
1270 | まとめると、local() はあなたがプライベートと考えるようなことをローカルな | |
1271 | 818 | 変数に対して行うようなことはありません。 |
1272 | 819 | これはグローバル変数に一時的な値を与えるだけです。 |
1273 | あなたがプライベート変数を必要としている | |
820 | my()は、あなたがプライベート変数を必要としているときに探し求めているものです。 | |
1274 | 探し求めているものです。 | |
1275 | 821 | |
1276 | ||
822 | このことをより詳しく説明している | |
823 | See L<perlsub/"Private Variables via my()"> と | |
1278 | See L<perlsub/"Private Variables via my()"> and | |
1279 | L<perlsub/"Temporary Values via local()"> for excruciating details. | |
1280 | ||
1281 | =end original | |
1282 | ||
1283 | このことをより詳しく説明している L<perlsub/"Private Variables via my()"> と | |
1284 | 824 | L<perlsub/"Temporary Values via local()"> を参照してください。 |
1285 | 825 | |
1286 | 826 | =head2 How can I access a dynamic variable while a similarly named lexical is in scope? |
1287 | 827 | |
1288 | (どうやれば同じ名前の | |
828 | (どうやれば同じ名前のlexicalがスコープにあるときに動的変数にアクセスできますか?) | |
1289 | 829 | |
1290 | ||
830 | これは、C<use strict "refs">とせずにシンボリックリファレンスを使えばできます。 | |
831 | ですから、$var ではなく C<${'var'}>を使います | |
1291 | 832 | |
1292 | ||
833 | local $var = "global"; | |
1293 | ||
834 | my $var = "lexical"; | |
1294 | in the current package, but rather the one in the "main" package, as | |
1295 | though you had written $main::var. | |
1296 | 835 | |
1297 | ||
836 | print "lexical is $var\n"; | |
1298 | 837 | |
1299 | ||
838 | no strict 'refs'; | |
1300 | ||
839 | print "global is ${'var'}\n"; | |
1301 | $::var という記法はカレントパッケージにおける動的な $var ではなく、 | |
1302 | "main" パッケージの中にあるそれなのだということに注意してください。 | |
1303 | 840 | |
1304 | ||
841 | 自分のパッケージを知っているのなら、これを $Some_Pack::var と陽に記述できます。 | |
1305 | ||
842 | $::var という記法はカレントパッケージにおける動的な$varではなく、 | |
1306 | ||
843 | C<main>パッケージの中にあるそれなのだということに注意してください。 | |
844 | パッケージを直接指定することによってその名前をハードコードすることになりますが、 | |
845 | 実行速度を速くし、C<use strict "refs">と衝突することを避けます。 | |
1307 | 846 | |
1308 | print "lexical is $var\n"; | |
1309 | print "global is $main::var\n"; | |
1310 | ||
1311 | =begin original | |
1312 | ||
1313 | Alternatively you can use the compiler directive our() to bring a | |
1314 | dynamic variable into the current lexical scope. | |
1315 | ||
1316 | =end original | |
1317 | ||
1318 | あるいは、動的変数を現在のレキシカルスコープにもたらすために、コンパイラ | |
1319 | 指示子 our() を使えます。 | |
1320 | ||
1321 | require 5.006; # our() did not exist before 5.6 | |
1322 | use vars '$var'; | |
1323 | ||
1324 | local $var = "global"; | |
1325 | my $var = "lexical"; | |
1326 | ||
1327 | print "lexical is $var\n"; | |
1328 | ||
1329 | { | |
1330 | our $var; | |
1331 | print "global is $var\n"; | |
1332 | } | |
1333 | ||
1334 | 847 | =head2 What's the difference between deep and shallow binding? |
1335 | 848 | |
1336 | 849 | (深い束縛(deep binding)と浅い束縛(shallow binding)との間の違いとは?) |
1337 | 850 | |
1338 | ||
851 | 深い束縛では、無名サブルーチンに置かれているleical変数はそのサブルーチンが | |
852 | 生成されたときのものと同一のものになります。 | |
1340 | ||
853 | 浅い束縛では、これはそのサブルーチンが呼び出されたときのスコープに存在している | |
1341 | ||
854 | 同じ名前を持った変数のどれか、になります。 | |
1342 | ||
855 | Perl はレキシカル変数(つまり、my()によって作られるもの)に対しては | |
1343 | happen to be in scope when the subroutine is called. Perl always uses | |
1344 | deep binding of lexical variables (i.e., those created with my()). | |
1345 | However, dynamic variables (aka global, local, or package variables) | |
1346 | are effectively shallowly bound. Consider this just one more reason | |
1347 | not to use them. See the answer to L<"What's a closure?">. | |
1348 | ||
1349 | =end original | |
1350 | ||
1351 | 深い束縛では、無名サブルーチンに置かれているレキシカル変数は | |
1352 | そのサブルーチンが生成されたときのものと同一のものになります。 | |
1353 | 浅い束縛では、これはそのサブルーチンが呼び出されたときのスコープに | |
1354 | 存在している同じ名前を持った変数のどれか、になります。 | |
1355 | Perl はレキシカル変数(つまり、my() によって作られるもの)に対しては | |
1356 | 856 | 常に深い束縛を使います。 |
1357 | それに対し動的変数(つまりグローバル変数か、ローカル変数か、 | |
857 | それに対し動的変数(つまりグローバル変数か、ローカル変数か、パッケージ変数)では、 | |
1358 | ||
858 | 浅い束縛がなされます。 | |
1359 | 859 | こういったものを使わない理由がもう一つあります。 |
1360 | その答えは L<"What's a closure?"> | |
860 | その答えは L<"What's a closure?">を参照してください。 | |
1361 | 861 | |
1362 | =head2 Why doesn't "my($foo) = | |
862 | =head2 Why doesn't "my($foo) = <FILE>;" work right? | |
1363 | 863 | |
1364 | (なぜ "my($foo) = | |
864 | (なぜ "my($foo) = <FILE>;" が正しく動作しないのでしょうか?) | |
1365 | 865 | |
1366 | ||
866 | C<my()>とC<local()>はC<=>の右辺に対してリストコンテキストを与えます。 | |
867 | 読み込み操作 <FH>は Perlの関数や演算子の多くと同じくそれが呼び出されたときの | |
1368 | C<my()> and C<local()> give list context to the right hand side | |
1369 | of C<=>. The <FH> read operation, like so many of Perl's | |
1370 | functions and operators, can tell which context it was called in and | |
1371 | behaves appropriately. In general, the scalar() function can help. | |
1372 | This function does nothing to the data itself (contrary to popular myth) | |
1373 | but rather tells its argument to behave in whatever its scalar fashion is. | |
1374 | If that function doesn't have a defined scalar behavior, this of course | |
1375 | doesn't help you (such as with sort()). | |
1376 | ||
1377 | =end original | |
1378 | ||
1379 | C<my()> と C<local()> は C<=> の右辺に対してリストコンテキストを与えます。 | |
1380 | 読み込み操作 <FH> は Perlの関数や演算子の多くと同じくそれが呼び出されたときの | |
1381 | 868 | コンテキストを見分けることができて、それに応じて適切に振る舞います。 |
1382 | 一般的には、scalar() | |
869 | 一般的には、scalar()関数が助けになるでしょう。 | |
1383 | その関数は(一般的な神話とは反して)引数となるデータに対して | |
870 | その関数は(一般的な神話(myth)とは反して)引数となるデータに対して | |
1384 | 何も行いませんが、引数がスカラとして振る舞うように指示します。 | |
871 | 何も行いませんが、引数がスカラーとして振る舞うように指示します。 | |
1385 | 関数のスカラ時の振る舞いが定義されていないのであれば、 | |
872 | 関数のスカラー時の振る舞いが定義されていないのであれば、 | |
1386 | 当然ながらこれはあなたの助けにはなりません(sort() | |
873 | 当然ながらこれはあなたの助けにはなりません(sort()がそうです)。 | |
1387 | 874 | |
1388 | ||
875 | しかしながら特定のケースにおいては、スカラーコンテキストを強制するために | |
1389 | ||
1390 | To enforce scalar context in this particular case, however, you need | |
1391 | merely omit the parentheses: | |
1392 | ||
1393 | =end original | |
1394 | ||
1395 | しかしながら特定のケースにおいては、スカラコンテキストを強制するために | |
1396 | 876 | 単に括弧を取り除く必要があります: |
1397 | 877 | |
1398 | ||
878 | local($foo) = <FILE>; # 間違い | |
1399 | ||
879 | local($foo) = scalar(<FILE>); # ok | |
1400 | ||
880 | local $foo = <FILE>; # 正しい | |
1401 | 881 | |
1402 | =begin original | |
1403 | ||
1404 | You should probably be using lexical variables anyway, although the | |
1405 | issue is the same here: | |
1406 | ||
1407 | =end original | |
1408 | ||
1409 | 882 | これと同じ問題があるものの、なんにしろレキシカル変数を使うべきでしょう。 |
1410 | 883 | |
1411 | ||
884 | my($foo) = <FILE>; # 間違い | |
1412 | ||
885 | my $foo = <FILE>; # 正しい | |
1413 | 886 | |
1414 | 887 | =head2 How do I redefine a builtin function, operator, or method? |
1415 | 888 | |
1416 | 889 | (組み込みの関数や演算子、メソッドを再定義するには?) |
1417 | 890 | |
1418 | =begin original | |
1419 | ||
1420 | Why do you want to do that? :-) | |
1421 | ||
1422 | =end original | |
1423 | ||
1424 | 891 | なんだってそんなことをしたがるのですか? :-) |
1425 | 892 | |
1426 | ||
893 | open()のようなあらかじめ定義されている関数をオーバーライドしたいのであれば、 | |
894 | 異なるモジュールから新しい定義をimportする必要があるでしょう。 | |
895 | L<perlsub/"Overriding Built-in Functions">を参照してください。 | |
896 | L<perltoot/"Class::Template">にも例があります。 | |
1427 | 897 | |
1428 | ||
898 | C<+> や C<**>のようなPerlの演算子をオーバーロードしたいのであれば | |
1429 | ||
899 | L<overload>で説明されているような C<use overload>プラグマを使いたくなるでしょう。 | |
1430 | module. See L<perlsub/"Overriding Built-in Functions">. | |
1431 | 900 | |
1432 | ||
901 | 親クラス郡における不明瞭なメソッド呼び出しについて考えているのなら、 | |
902 | L<perltoot/"Overridden Methods">を参照してください。 | |
1433 | 903 | |
1434 | open() のようなあらかじめ定義されている関数をオーバーライドしたいのであれば、 | |
1435 | 異なるモジュールから新しい定義をインポートする必要があります。 | |
1436 | L<perlsub/"Overriding Built-in Functions"> を参照してください。 | |
1437 | ||
1438 | =begin original | |
1439 | ||
1440 | If you want to overload a Perl operator, such as C<+> or C<**>, | |
1441 | then you'll want to use the C<use overload> pragma, documented | |
1442 | in L<overload>. | |
1443 | ||
1444 | =end original | |
1445 | ||
1446 | C<+> や C<**> のような Perl の演算子をオーバーロードしたいのであれば | |
1447 | L<overload> で説明されているような C<use overload> プラグマを | |
1448 | 使いたくなるでしょう。 | |
1449 | ||
1450 | =begin original | |
1451 | ||
1452 | If you're talking about obscuring method calls in parent classes, | |
1453 | see L<perltoot/"Overridden Methods">. | |
1454 | ||
1455 | =end original | |
1456 | ||
1457 | 親クラスにおける不明瞭なメソッド呼び出しについて考えているのなら、 | |
1458 | L<perltoot/"Overridden Methods"> を参照してください。 | |
1459 | ||
1460 | 904 | =head2 What's the difference between calling a function as &foo and foo()? |
1461 | 905 | |
1462 | (関数呼び出しを &foo | |
906 | (関数呼び出しを &fooで行ったときとfoo()で行ったときとの違いはなんですか?) | |
1463 | 907 | |
1464 | ||
908 | 関数を C<&foo>として呼び出したとき、その関数はプロトタイプを無視して | |
909 | カレントの@_の値全てにアクセスすることができます。 | |
910 | これはつまり、空の@_を受け取るのではなくてあなたが使っていた | |
911 | それを受け取るということなのです! | |
912 | これは厳密にバグとは考えられていませんが(L<perlsub>に説明があります)、 | |
913 | ほとんどの場合はこの機能が使われるとは考えられないでしょう。 | |
1465 | 914 | |
1466 | ||
915 | C<&foo()>のように関数を呼び出した場合には新たな @_を受け取りますが、 | |
916 | この場合もプロトタイプは無視されます。 | |
1467 | 917 | |
1468 | ||
918 | 通常は、関数を C<foo()>のように呼び出したいでしょう。 | |
919 | 既に定義がされていたり(C<require> ではなくC<use>)、先行宣言を使っていたり、 | |
920 | C<use subs> 宣言などがあってコンパイラーが既にその関数を知っているときにのみ | |
921 | 括弧を省略することができます。 | |
922 | この場合でさえ、そこにあるべきでない古い値が漏れだしていることのない | |
923 | クリーンな @_ を得られます。 | |
1469 | 924 | |
1470 | (brian d foy によって寄贈されました) | |
1471 | ||
1472 | =begin original | |
1473 | ||
1474 | Calling a subroutine as C<&foo> with no trailing parentheses ignores | |
1475 | the prototype of C<foo> and passes it the current value of the argument | |
1476 | list, C<@_>. Here's an example; the C<bar> subroutine calls C<&foo>, | |
1477 | which prints its arguments list: | |
1478 | ||
1479 | =end original | |
1480 | ||
1481 | C<&foo> の形で、引き続くかっこなしでサブルーチンを呼び出すと、 | |
1482 | C<foo> のプロトタイプを無視して、引数リスト C<@_> の現在の値を渡します。 | |
1483 | 以下は例です: C<bar> サブルーチンは、引数リストを表示する C<&foo> を | |
1484 | 呼び出します: | |
1485 | ||
1486 | sub bar { &foo } | |
1487 | ||
1488 | sub foo { print "Args in foo are: @_\n" } | |
1489 | ||
1490 | bar( qw( a b c ) ); | |
1491 | ||
1492 | =begin original | |
1493 | ||
1494 | When you call C<bar> with arguments, you see that C<foo> got the same C<@_>: | |
1495 | ||
1496 | =end original | |
1497 | ||
1498 | C<bar> を引数付きで呼び出した場合、C<foo> も同じ C<@_> を得ます: | |
1499 | ||
1500 | Args in foo are: a b c | |
1501 | ||
1502 | =begin original | |
1503 | ||
1504 | Calling the subroutine with trailing parentheses, with or without arguments, | |
1505 | does not use the current C<@_> and respects the subroutine prototype. Changing | |
1506 | the example to put parentheses after the call to C<foo> changes the program: | |
1507 | ||
1508 | =end original | |
1509 | ||
1510 | サブルーチンをかっこ付きで呼び出した場合、引数のありなしに関わらず、 | |
1511 | 現在の C<@_> は使わずに、サブルーチンのプロトタイプに従います。 | |
1512 | C<foo> を呼び出すときにかっこをつけるように例を変更します: | |
1513 | ||
1514 | sub bar { &foo() } | |
1515 | ||
1516 | sub foo { print "Args in foo are: @_\n" } | |
1517 | ||
1518 | bar( qw( a b c ) ); | |
1519 | ||
1520 | =begin original | |
1521 | ||
1522 | Now the output shows that C<foo> doesn't get the C<@_> from its caller. | |
1523 | ||
1524 | =end original | |
1525 | ||
1526 | 今度は、C<foo> が呼び出し元から C<@_> を得ていないことが分かります: | |
1527 | ||
1528 | Args in foo are: | |
1529 | ||
1530 | =begin original | |
1531 | ||
1532 | The main use of the C<@_> pass-through feature is to write subroutines | |
1533 | whose main job it is to call other subroutines for you. For further | |
1534 | details, see L<perlsub>. | |
1535 | ||
1536 | =end original | |
1537 | ||
1538 | C<@_> パススルー機能の主な利用法は、他のサブルーチンを呼び出すのが | |
1539 | 主な仕事であるサブルーチンを書くためです。 | |
1540 | 更なる詳細については、L<perlsub> を参照してください。 | |
1541 | ||
1542 | 925 | =head2 How do I create a switch or case statement? |
1543 | 926 | |
1544 | (switch | |
927 | (switch文やcase文を作るには?) | |
1545 | 928 | |
1546 | ||
929 | これは L<perlsyn>でより詳細に説明されています。 | |
930 | 簡単にいうと、Perl では様々なやり方(数値比較、文字列比較、グロブ比較、 | |
931 | 正規表現マッチング、オーバーロードされた比較…)で検査が行えるので、 | |
932 | 正式な case 文はありません。 | |
933 | Larry はこのどれが最善であるかを決めることができないので、 | |
934 | perl1 の時代から wish リストに置かれているにも関らずこの問題は | |
935 | そのままにしておかれているのです。 | |
1547 | 936 | |
1548 | ||
937 | 構造文を書くための一般的な答えは以下のようなものです: | |
1549 | 938 | |
1550 | ||
939 | for ($variable_to_test) { | |
940 | if (/pat1/) { } # 何かを行う | |
941 | elsif (/pat2/) { } # 別の何かを行う | |
942 | elsif (/pat3/) { } # 別の何かを行う | |
943 | else { } # デフォルト | |
944 | } | |
1551 | 945 | |
1552 | ||
946 | 以下の例は、パターンマッチングに基づいた単純なswitchの例です。 | |
1553 | ||
947 | $whatchamacallitに格納されたリファレンスの型に基づいて多様なやり方の | |
1554 | ||
1555 | use 5.010; | |
1556 | ||
1557 | given ( $string ) { | |
1558 | when( 'Fred' ) { say "I found Fred!" } | |
1559 | when( 'Barney' ) { say "I found Barney!" } | |
1560 | when( /Bamm-?Bamm/ ) { say "I found Bamm-Bamm!" } | |
1561 | default { say "I don't recognize the name!" } | |
1562 | }; | |
1563 | ||
1564 | =begin original | |
1565 | ||
1566 | If one wants to use pure Perl and to be compatible with Perl versions | |
1567 | prior to 5.10, the general answer is to use C<if-elsif-else>: | |
1568 | ||
1569 | =end original | |
1570 | ||
1571 | もしピュア Perl を使って、バージョン 5.10 より前と互換性を持たせたいなら、 | |
1572 | 構造文を書くための一般的な答えは C<if-elsif-else> を使うことです: | |
1573 | ||
1574 | for ($variable_to_test) { | |
1575 | if (/pat1/) { } # do something | |
1576 | elsif (/pat2/) { } # do something else | |
1577 | elsif (/pat3/) { } # do something else | |
1578 | else { } # default | |
1579 | } | |
1580 | ||
1581 | =begin original | |
1582 | ||
1583 | Here's a simple example of a switch based on pattern matching, | |
1584 | lined up in a way to make it look more like a switch statement. | |
1585 | We'll do a multiway conditional based on the type of reference stored | |
1586 | in $whatchamacallit: | |
1587 | ||
1588 | =end original | |
1589 | ||
1590 | 以下の例は、パターンマッチングに基づいた単純な switch の例です。 | |
1591 | $whatchamacallit に格納されたリファレンスの型に基づいて多様なやり方の | |
1592 | 948 | 条件判断を行っています: |
1593 | 949 | |
1594 | 950 | SWITCH: for (ref $whatchamacallit) { |
1595 | 951 | |
1596 | 952 | /^$/ && die "not a reference"; |
1597 | 953 | |
1598 | 954 | /SCALAR/ && do { |
1599 | 955 | print_scalar($$ref); |
1600 | 956 | last SWITCH; |
1601 | 957 | }; |
1602 | 958 | |
1603 | 959 | /ARRAY/ && do { |
1604 | 960 | print_array(@$ref); |
1605 | 961 | last SWITCH; |
1606 | 962 | }; |
1607 | 963 | |
1608 | 964 | /HASH/ && do { |
1609 | 965 | print_hash(%$ref); |
1610 | 966 | last SWITCH; |
1611 | 967 | }; |
1612 | 968 | |
1613 | 969 | /CODE/ && do { |
1614 | 970 | warn "can't print function ref"; |
1615 | 971 | last SWITCH; |
1616 | 972 | }; |
1617 | 973 | |
1618 | 974 | # DEFAULT |
1619 | 975 | |
1620 | 976 | warn "User defined type skipped"; |
1621 | 977 | |
1622 | 978 | } |
1623 | 979 | |
1624 | ||
980 | この形式の他の例についてはC<perlsyn/"Basic BLOCKs and Switch Statements">を | |
981 | 参照してください。 | |
1625 | 982 | |
1626 | See L<perlsyn> for other examples in this style. | |
1627 | ||
1628 | =end original | |
1629 | ||
1630 | このスタイルに関するその他の例については L<perlsyn> を参照してください。 | |
1631 | ||
1632 | 983 | =begin original |
1633 | 984 | |
1634 | 985 | Sometimes you should change the positions of the constant and the variable. |
1635 | 986 | For example, let's say you wanted to test which of many answers you were |
1636 | 987 | given, but in a case-insensitive way that also allows abbreviations. |
1637 | 988 | You can use the following technique if the strings all start with |
1638 | 989 | different characters or if you want to arrange the matches so that |
1639 | 990 | one takes precedence over another, as C<"SEND"> has precedence over |
1640 | 991 | C<"STOP"> here: |
1641 | 992 | |
1642 | 993 | =end original |
1643 | 994 | |
1644 | 995 | 定数や変数の位置を変えた方が良いことがあるかもしれません。 |
1645 | 996 | たとえば、与えられたたくさんの答についてテストを行いたいとしましょう。 |
1646 | 997 | この場合大小文字を無視することもできますし、 |
1647 | 998 | 略記することもあります。もし全て文字列が異なるキャラクターで始まっていたり、 |
1648 | C<"SEND"> | |
999 | C<"SEND">がC<"STOP"> | |
1649 | ||
1000 | より高い優先順位を持つように調整したくいのならマッチの順序を | |
1650 | 使うことができます。 | |
1001 | アレンジしたいのであれば以下に示すようなテクニックを使うことができます。 | |
1651 | 1002 | |
1652 | ||
1003 | chomp($answer = <>); | |
1653 | ||
1004 | if ("SEND" =~ /^\Q$answer/i) { print "Action is send\n" } | |
1654 | ||
1005 | elsif ("STOP" =~ /^\Q$answer/i) { print "Action is stop\n" } | |
1655 | ||
1006 | elsif ("ABORT" =~ /^\Q$answer/i) { print "Action is abort\n" } | |
1656 | ||
1007 | elsif ("LIST" =~ /^\Q$answer/i) { print "Action is list\n" } | |
1657 | ||
1008 | elsif ("EDIT" =~ /^\Q$answer/i) { print "Action is edit\n" } | |
1658 | 1009 | |
1659 | =begin original | |
1660 | ||
1661 | A totally different approach is to create a hash of function references. | |
1662 | ||
1663 | =end original | |
1664 | ||
1665 | 1010 | まったく異なるアプローチに、関数のリファレンスのハッシュを作成するという |
1666 | 1011 | やり方があります。 |
1667 | 1012 | |
1668 | ||
1013 | my %commands = ( | |
1669 | ||
1014 | "happy" => \&joy, | |
1670 | ||
1015 | "sad", => \&sullen, | |
1671 | ||
1016 | "done" => sub { die "See ya!" }, | |
1672 | ||
1017 | "mad" => \&angry, | |
1673 | ||
1018 | ); | |
1674 | 1019 | |
1675 | ||
1020 | print "How are you? "; | |
1676 | ||
1021 | chomp($string = <STDIN>); | |
1677 | ||
1022 | if ($commands{$string}) { | |
1678 | ||
1023 | $commands{$string}->(); | |
1679 | ||
1024 | } else { | |
1680 | ||
1025 | print "No such command: $string\n"; | |
1681 | ||
1026 | } | |
1682 | 1027 | |
1683 | = | |
1028 | =head2 How can I catch accesses to undefined variables/functions/methods? | |
1684 | 1029 | |
1685 | ||
1030 | (どうすれば undefinedな変数/関数/メソッドに対するアクセスを捕捉できますか?) | |
1686 | used to get switch and case. Its use is now discouraged, because it's | |
1687 | not fully compatible with the native switch of Perl 5.10, and because, | |
1688 | as it's implemented as a source filter, it doesn't always work as intended | |
1689 | when complex syntax is involved. | |
1690 | 1031 | |
1691 | ||
1032 | L<perlsub/"Autoloading"> と L<perltoot/"AUTOLOAD: Proxy Methods">で | |
1033 | 言及されている AUTOLOADメソッドは、undefine な関数やメソッドに対する | |
1034 | 呼び出しを捕捉させてくれます。 | |
1692 | 1035 | |
1693 | ||
1036 | C<-w>が有効なときに警告の引き金になるような未定義変数へのアクセスがあったとき、 | |
1694 | ||
1037 | 以下の例にあるような擬似シグナルC<__WARN__>をトラップするハンドラーを | |
1695 | ||
1038 | 使うことができます: | |
1696 | フィルタとして実装されているので、複雑な文法の場合はいつも想定通りに | |
1697 | 動くとは限らないからです。 | |
1698 | 1039 | |
1699 | ||
1040 | $SIG{__WARN__} = sub { | |
1700 | 1041 | |
1701 | ( | |
1042 | for ( $_[0] ) { # voici un switch statement | |
1702 | 1043 | |
1703 | ||
1044 | /Use of uninitialized value/ && do { | |
1045 | # 警告をエラーにします | |
1046 | die $_; | |
1047 | }; | |
1704 | 1048 | |
1705 | ||
1049 | # その他の警告はここで捕捉できます | |
1706 | L<perltoot/"AUTOLOAD: Proxy Methods">, lets you capture calls to | |
1707 | undefined functions and methods. | |
1708 | 1050 | |
1709 | ||
1051 | warn $_; | |
1052 | } | |
1710 | 1053 | |
1711 | ||
1054 | }; | |
1712 | 言及されている AUTOLOAD メソッドは、未定義な関数やメソッドに対する | |
1713 | 呼び出しを捕捉させてくれます。 | |
1714 | 1055 | |
1715 | =begin original | |
1716 | ||
1717 | When it comes to undefined variables that would trigger a warning | |
1718 | under C<use warnings>, you can promote the warning to an error. | |
1719 | ||
1720 | =end original | |
1721 | ||
1722 | C<use warnings> が有効なときに警告の引き金になるような未定義変数への | |
1723 | アクセスがあったとき、警告をエラーに昇格させることができます。 | |
1724 | ||
1725 | use warnings FATAL => qw(uninitialized); | |
1726 | ||
1727 | 1056 | =head2 Why can't a method included in this same file be found? |
1728 | 1057 | |
1729 | 1058 | (なぜ同じファイルにあるメソッドが見つけられないのでしょうか?) |
1730 | 1059 | |
1731 | =begin original | |
1732 | ||
1733 | Some possible reasons: your inheritance is getting confused, you've | |
1734 | misspelled the method name, or the object is of the wrong type. Check | |
1735 | out L<perltoot> for details about any of the above cases. You may | |
1736 | also use C<print ref($object)> to find out the class C<$object> was | |
1737 | blessed into. | |
1738 | ||
1739 | =end original | |
1740 | ||
1741 | 1060 | 幾つかの理由が考えられます: あなたが継承したものが混乱していているか、 |
1742 | 1061 | メソッド名を間違えたか、あるいはオブジェクトの型が間違っていたか。 |
1743 | 1062 | 上記の場合に関する詳細は L<perltoot> をチェックしてみてください。 |
1744 | C<$object> が | |
1063 | C<$object> がblessされているクラスは C<print ref($object)>として | |
1745 | 1064 | 見分けることができます。 |
1746 | 1065 | |
1747 | ||
1066 | もう一つありうる理由は、Perlがパッケージを見いだす前にクラス名を使った | |
1067 | 間接オブジェクト構文(C<find Guru "Samy">のようなもの)を使ったためでしょう。 | |
1749 | Another possible reason for problems is that you've used the | |
1750 | indirect object syntax (eg, C<find Guru "Samy">) on a class name | |
1751 | before Perl has seen that such a package exists. It's wisest to make | |
1752 | sure your packages are all defined before you start using them, which | |
1753 | will be taken care of if you use the C<use> statement instead of | |
1754 | C<require>. If not, make sure to use arrow notation (eg., | |
1755 | C<< Guru->find("Samy") >>) instead. Object notation is explained in | |
1756 | L<perlobj>. | |
1757 | ||
1758 | =end original | |
1759 | ||
1760 | もう一つありうる理由は、Perl がパッケージを見いだす前にクラス名を使った | |
1761 | 間接オブジェクト構文(C<find Guru "Samy"> のようなもの)を使ったためでしょう。 | |
1762 | 1068 | パッケージは、それを使うよりも前に全てが定義されているようにします。 |
1763 | これは | |
1069 | これはC<require>文ではなくC<use>文を使えば考慮されます。 | |
1764 | 1070 | あるいは、代わりに矢印記法(arrow notation、 |
1765 | 1071 | C<< Guru->find("Samy") >> のようなもの)を使うようにしてください。 |
1766 | オブジェクトの記法は | |
1072 | オブジェクトの記法はL<perlobj>で説明されています。 | |
1767 | 1073 | |
1768 | 1074 | =begin original |
1769 | 1075 | |
1770 | 1076 | Make sure to read about creating modules in L<perlmod> and |
1771 | the perils of indirect objects in L<perlobj/" | |
1077 | the perils of indirect objects in L<perlobj/"WARNING">. | |
1772 | 1078 | |
1773 | 1079 | =end original |
1774 | 1080 | |
1775 | モジュールの作り方については L<perlmod> を、 | |
1081 | モジュールの作り方については L<perlmod> を、 | |
1776 | ついては L<perlobj/"WARNING"> を | |
1082 | 間接オブジェクトの問題点については L<perlobj/"WARNING"> を | |
1083 | 確認してください。 | |
1777 | 1084 | |
1778 | =head2 How can I find out my current | |
1085 | =head2 How can I find out my current package? | |
1779 | 1086 | |
1780 | (カレントのパッケージ | |
1087 | (カレントのパッケージはどうすればわかりますか?) | |
1781 | 1088 | |
1782 | ||
1089 | ランダムなプログラムであるなら、以下のようにすれば現在コンパイルされている | |
1090 | パッケージを知ることができます: | |
1783 | 1091 | |
1784 | ||
1092 | my $packname = __PACKAGE__; | |
1785 | 1093 | |
1786 | =end original | |
1787 | ||
1788 | (brian d foy によって寄贈されました) | |
1789 | ||
1790 | 1094 | =begin original |
1791 | 1095 | |
1792 | ||
1096 | But, if you're a method and you want to print an error message | |
1793 | ||
1097 | that includes the kind of object you were called on (which is | |
1794 | ||
1098 | not necessarily the same as the one in which you were compiled): | |
1795 | into strings like you can with variables: | |
1796 | 1099 | |
1797 | 1100 | =end original |
1798 | 1101 | |
1799 | ||
1102 | しかし、対象がメソッドで、呼び出されたオブジェクトの種類がなんであるかを | |
1800 | ||
1103 | 含んだエラーメッセージを出力したいのなら(これはそれが | |
1801 | ||
1104 | コンパイルされたものと同一である必要はありません): | |
1802 | 文字列に展開できません: | |
1803 | 1105 | |
1804 | my $current_package = __PACKAGE__; | |
1805 | print "I am in package $current_package\n"; | |
1806 | 1106 | |
1807 | ||
1107 | sub amethod { | |
1108 | my $self = shift; | |
1109 | my $class = ref($self) || $self; | |
1110 | warn "called me from a $class object"; | |
1111 | } | |
1808 | 1112 | |
1809 | ||
1113 | =head2 How can I comment out a large block of perl code? | |
1810 | diagnostics as C<Carp> does, use the C<caller> built-in: | |
1811 | 1114 | |
1812 | ||
1115 | (perlプログラムの大きなブロックをコメントアウトするのは | |
1116 | どうすればできますか?) | |
1813 | 1117 | |
1814 | ||
1118 | 埋め込みのPODを説明のために使います: | |
1815 | よりよい診断メッセージのためでしょう) C<caller> 組み込み関数を | |
1816 | 使ってください: | |
1817 | 1119 | |
1818 | ||
1120 | # プログラムはここに | |
1819 | my @args = ...; | |
1820 | my( $package, $filename, $line ) = caller; | |
1821 | 1121 | |
1822 | ||
1122 | =for nobody | |
1823 | ||
1123 | このパラグラフはコメントアウトされます | |
1824 | 1124 | |
1825 | ||
1125 | # プログラムの続き | |
1826 | 1126 | |
1827 | ||
1127 | =begin comment text | |
1828 | always be in some package. | |
1829 | 1128 | |
1830 | ||
1129 | ここにあるものすべては | |
1831 | 1130 | |
1832 | ||
1131 | なにからも無視されます | |
1833 | いつもなんらかのパッケージ内にいるはずです。 | |
1834 | 1132 | |
1835 | = | |
1133 | =end comment text | |
1836 | 1134 | |
1837 | ||
1135 | =cut | |
1838 | into, which might not be the current package. For that, use C<blessed> | |
1839 | from C<Scalar::Util>, part of the Standard Library since Perl 5.8: | |
1840 | 1136 | |
1841 | =end original | |
1842 | ||
1843 | これはオブジェクトが bless されているパッケージを知ることとは違います; | |
1844 | これは現在のパッケージではないかもしれません。 | |
1845 | この目的のためには、Perl 5.8 から標準ライブラリの一部となっている | |
1846 | C<Scalar::Util> の C<blessed> を使ってください: | |
1847 | ||
1848 | use Scalar::Util qw(blessed); | |
1849 | my $object_package = blessed( $object ); | |
1850 | ||
1851 | 1137 | =begin original |
1852 | 1138 | |
1853 | ||
1139 | This can't go just anywhere. You have to put a pod directive where | |
1854 | ||
1140 | the parser is expecting a new statement, not just in the middle | |
1141 | of an expression or some other arbitrary yacc grammar production. | |
1855 | 1142 | |
1856 | 1143 | =end original |
1857 | 1144 | |
1858 | ||
1145 | これはどこにでも置けるわけではありません。パーザーが | |
1859 | ||
1146 | 新しいステートメントを期待している場所にpod指令を置く必要があって、 | |
1860 | ||
1147 | 式の途中であるとかその他任意の yacc 文法生成規則には置けません。 | |
1861 | 1148 | |
1862 | my $is_right_class = eval { $object->isa( $package ) }; # true or false | |
1863 | ||
1864 | =begin original | |
1865 | ||
1866 | And, with Perl 5.10 and later, you don't have to check for an | |
1867 | inheritance to see if the object can handle a role. For that, you can | |
1868 | use C<DOES>, which comes from C<UNIVERSAL>: | |
1869 | ||
1870 | =end original | |
1871 | ||
1872 | また、Perl 5.10 以降では、オブジェクトがロールを扱うかどうかを見るために | |
1873 | 継承をチェックする必要はありません。 | |
1874 | このために、C<UNIVERSAL> にある C<DOES> を使えます: | |
1875 | ||
1876 | my $class_does_it = eval { $object->DOES( $role ) }; # true or false | |
1877 | ||
1878 | =begin original | |
1879 | ||
1880 | You can safely replace C<isa> with C<DOES> (although the converse is not true). | |
1881 | ||
1882 | =end original | |
1883 | ||
1884 | 安全に C<isa> を C<DOES> に置き換えられます (しかし逆は真ではありません)。 | |
1885 | ||
1886 | =head2 How can I comment out a large block of Perl code? | |
1887 | ||
1888 | (Perl プログラムの大きなブロックをコメントアウトするには?) | |
1889 | ||
1890 | =begin original | |
1891 | ||
1892 | (contributed by brian d foy) | |
1893 | ||
1894 | =end original | |
1895 | ||
1896 | (brian d foy によって寄贈されました) | |
1897 | ||
1898 | =begin original | |
1899 | ||
1900 | The quick-and-dirty way to comment out more than one line of Perl is | |
1901 | to surround those lines with Pod directives. You have to put these | |
1902 | directives at the beginning of the line and somewhere where Perl | |
1903 | expects a new statement (so not in the middle of statements like the # | |
1904 | comments). You end the comment with C<=cut>, ending the Pod section: | |
1905 | ||
1906 | =end original | |
1907 | ||
1908 | Perl の複数行をコメントアウトするための、汚いけれども簡単な方法は、 | |
1909 | コメントアウトしたい部分を Pod 指示子で囲むことです。 | |
1910 | これらの指示子は、行の先頭、かつ、Perl が新しい文が始まると | |
1911 | 想定する場所に置く必要があります(従って、# コメントのように文の途中には | |
1912 | 置けません)。 | |
1913 | Pod セクションの終了を示す C<=cut> でコメントを終了します: | |
1914 | ||
1915 | =pod | |
1916 | ||
1917 | my $object = NotGonnaHappen->new(); | |
1918 | ||
1919 | ignored_sub(); | |
1920 | ||
1921 | $wont_be_assigned = 37; | |
1922 | ||
1923 | =cut | |
1924 | ||
1925 | =begin original | |
1926 | ||
1927 | The quick-and-dirty method only works well when you don't plan to | |
1928 | leave the commented code in the source. If a Pod parser comes along, | |
1929 | you're multiline comment is going to show up in the Pod translation. | |
1930 | A better way hides it from Pod parsers as well. | |
1931 | ||
1932 | =end original | |
1933 | ||
1934 | 汚いけれども簡単な方法は、ソースにコメントされたコードを残す予定が | |
1935 | ない(つまりいずれ消す)場合にのみうまく働きます。 | |
1936 | もし Pod パーサを使うと、複数行コメントは Pod によって表示されます。 | |
1937 | よりよい方法は、Pod パーサからも隠すことです。 | |
1938 | ||
1939 | =begin original | |
1940 | ||
1941 | The C<=begin> directive can mark a section for a particular purpose. | |
1942 | If the Pod parser doesn't want to handle it, it just ignores it. Label | |
1943 | the comments with C<comment>. End the comment using C<=end> with the | |
1944 | same label. You still need the C<=cut> to go back to Perl code from | |
1945 | the Pod comment: | |
1946 | ||
1947 | =end original | |
1948 | ||
1949 | C<=begin> 指示子で、段落を特定の目的のためにマークできます。 | |
1950 | Pod パーサがこれを扱えない場合、単に無視されます。 | |
1951 | コメントは C<comment> でラベル付けします。 | |
1952 | コメントの終了は同じラベルで C<=end> を使います。 | |
1953 | Pod コメントから Perl コードに戻るにはやはり C<=cut> が必要です: | |
1954 | ||
1955 | =begin comment | |
1956 | ||
1957 | my $object = NotGonnaHappen->new(); | |
1958 | ||
1959 | ignored_sub(); | |
1960 | ||
1961 | $wont_be_assigned = 37; | |
1962 | ||
1963 | =end comment | |
1964 | ||
1965 | =cut | |
1966 | ||
1967 | =begin original | |
1968 | ||
1969 | For more information on Pod, check out L<perlpod> and L<perlpodspec>. | |
1970 | ||
1971 | =end original | |
1972 | ||
1973 | Pod に関するさらなる情報については、L<perlpod> と L<perlpodspec> を | |
1974 | 調べてください。 | |
1975 | ||
1976 | 1149 | =head2 How do I clear a package? |
1977 | 1150 | |
1978 | 1151 | (パッケージをクリアするには?) |
1979 | 1152 | |
1980 | ||
1153 | Mark-Jason Dominusによる以下のコードを使います: | |
1981 | 1154 | |
1982 | Use this code, provided by Mark-Jason Dominus: | |
1983 | 1155 | |
1984 | ||
1156 | sub scrub_package { | |
1157 | no strict 'refs'; | |
1986 | ||
1158 | my $pack = shift; | |
1159 | die "Shouldn't delete main package" | |
1988 | | |
1160 | if $pack eq "" || $pack eq "main"; | |
1989 | | |
1161 | my $stash = *{$pack . '::'}{HASH}; | |
1990 | | |
1162 | my $name; | |
1991 | | |
1163 | foreach $name (keys %$stash) { | |
1992 | | |
1164 | my $fullname = $pack . '::' . $name; | |
1993 | | |
1165 | # 名前に関する全てのものを取り去る | |
1994 | | |
1166 | undef $$fullname; | |
1995 | | |
1167 | undef @$fullname; | |
1996 | | |
1168 | undef %$fullname; | |
1997 | | |
1169 | undef &$fullname; | |
1998 | | |
1170 | undef *$fullname; | |
1999 | undef @$fullname; | |
2000 | undef %$fullname; | |
2001 | undef &$fullname; | |
2002 | undef *$fullname; | |
2003 | 1171 | } |
2004 | ||
1172 | } | |
2005 | 1173 | |
2006 | =begin original | |
2007 | ||
2008 | Or, if you're using a recent release of Perl, you can | |
2009 | just use the Symbol::delete_package() function instead. | |
2010 | ||
2011 | =end original | |
2012 | ||
2013 | 1174 | あるいは、あなたが使っている Perl が最近のリリースのものであれば、 |
2014 | 1175 | 単に Symbol::delete_package() という関数を代わりに使うことができます。 |
2015 | 1176 | |
2016 | 1177 | =head2 How can I use a variable as a variable name? |
2017 | 1178 | |
2018 | (変数を変数名として使うには?) | |
1179 | (変数を変数名として使うにはどうすればいいですか?) | |
2019 | 1180 | |
2020 | 1181 | =begin original |
2021 | 1182 | |
2022 | 1183 | Beginners often think they want to have a variable contain the name |
2023 | 1184 | of a variable. |
2024 | 1185 | |
2025 | 1186 | =end original |
2026 | 1187 | |
2027 | 1188 | 初心者はしばしば変数名が入った変数を使いたいと考えます。 |
2028 | 1189 | |
2029 | ||
1190 | $fred = 23; | |
2030 | ||
1191 | $varname = "fred"; | |
2031 | ||
1192 | ++$$varname; # $fred now 24 | |
2032 | 1193 | |
2033 | 1194 | =begin original |
2034 | 1195 | |
2035 | 1196 | This works I<sometimes>, but it is a very bad idea for two reasons. |
2036 | 1197 | |
2037 | 1198 | =end original |
2038 | 1199 | |
2039 | これは | |
1200 | これはI<時には>動作します。しかしこれは二つの理由により悪いアイデアです。 | |
2040 | しかしこれは二つの理由により悪いアイデアです。 | |
2041 | 1201 | |
2042 | 1202 | =begin original |
2043 | 1203 | |
2044 | 1204 | The first reason is that this technique I<only works on global |
2045 | 1205 | variables>. That means that if $fred is a lexical variable created |
2046 | 1206 | with my() in the above example, the code wouldn't work at all: you'd |
2047 | 1207 | accidentally access the global and skip right over the private lexical |
2048 | 1208 | altogether. Global variables are bad because they can easily collide |
2049 | 1209 | accidentally and in general make for non-scalable and confusing code. |
2050 | 1210 | |
2051 | 1211 | =end original |
2052 | 1212 | |
2053 | 一つ目の理由は、このテクニックは | |
1213 | 一つ目の理由は、このテクニックはI<グローバル変数でのみ動作する>からです。 | |
2054 | 1214 | つまり、もし上記の例において $fred が my() で作成されたレキシカル変数の場合、 |
2055 | このコードは全く動作しません。 | |
1215 | このコードは全く動作しません。プライベートなレキシカル変数を飛ばして、 | |
2056 | ||
1216 | 思いがけずグローバル変数にアクセスすることになります。 | |
2057 | アクセスすることになります。 | |
2058 | 1217 | グローバル変数は、簡単に衝突し、一般に拡張性がなく、混乱するコードを |
2059 | 1218 | 作ることになるので、よくないものです。 |
2060 | 1219 | |
2061 | 1220 | =begin original |
2062 | 1221 | |
2063 | 1222 | Symbolic references are forbidden under the C<use strict> pragma. |
2064 | They are not true references and consequently are not reference | |
1223 | They are not true references and consequently are not reference counted | |
2065 | or garbage | |
1224 | or garbage collected. | |
2066 | 1225 | |
2067 | 1226 | =end original |
2068 | 1227 | |
2069 | 1228 | シンボリックリファレンスは C<use strict> プラグマの元では禁止されます。 |
2070 | 1229 | これは真のリファレンスではないので、リファレンスカウントに含まれず、 |
2071 | 1230 | ガベージゴレクションもされません。 |
2072 | 1231 | |
2073 | 1232 | =begin original |
2074 | 1233 | |
2075 | 1234 | The other reason why using a variable to hold the name of another |
2076 | 1235 | variable is a bad idea is that the question often stems from a lack of |
2077 | 1236 | understanding of Perl data structures, particularly hashes. By using |
2078 | 1237 | symbolic references, you are just using the package's symbol-table hash |
2079 | 1238 | (like C<%main::>) instead of a user-defined hash. The solution is to |
2080 | 1239 | use your own hash or a real reference instead. |
2081 | 1240 | |
2082 | 1241 | =end original |
2083 | 1242 | |
2084 | 1243 | 変数に他の変数の名前を記録するというのがよくない考えであるという |
2085 | 1244 | 別の理由としては、このような疑問はしばしば Perl のデータ構造、 |
2086 | 1245 | 特にハッシュに関する理解の不足から発生するからです。 |
2087 | シンボリックリファレンスを使うことによって、 | |
1246 | シンボリックリファレンスを使うことによって、 | |
2088 | パッケージのシンボルテーブルハッシュ | |
1247 | ユーザー定義のハッシュの代わりにパッケージのシンボルテーブルハッシュ | |
1248 | (C<%main::> など)を使うことができます。 | |
2089 | 1249 | 解決法は、代わりに自分自身のハッシュや真のリファレンスを使うことです。 |
2090 | 1250 | |
2091 | ||
1251 | $fred = 23; | |
2092 | ||
1252 | $varname = "fred"; | |
2093 | ||
1253 | $USER_VARS{$varname}++; # not $$varname++ | |
2094 | 1254 | |
2095 | 1255 | =begin original |
2096 | 1256 | |
2097 | 1257 | There we're using the %USER_VARS hash instead of symbolic references. |
2098 | 1258 | Sometimes this comes up in reading strings from the user with variable |
2099 | 1259 | references and wanting to expand them to the values of your perl |
2100 | 1260 | program's variables. This is also a bad idea because it conflates the |
2101 | 1261 | program-addressable namespace and the user-addressable one. Instead of |
2102 | 1262 | reading a string and expanding it to the actual contents of your program's |
2103 | 1263 | own variables: |
2104 | 1264 | |
2105 | 1265 | =end original |
2106 | 1266 | |
2107 | ここではシンボリックリファレンスの代わりに %USER_VARS ハッシュを | |
1267 | ここではシンボリックリファレンスの代わりに %USER_VARS ハッシュを使っています。 | |
2108 | 使っています。 | |
2109 | 1268 | 時々これはユーザーから文字列を変数へのリファレンスとして読み込んで、 |
2110 | 1269 | それを perl プログラムの変数の値として拡張することがあります。 |
2111 | これもよくない考えです。 | |
1270 | これもよくない考えです。なぜなら、プログラムが指定する名前空間と | |
2112 | ||
1271 | ユーザーが指定する名前空間を融合させることになるからです。 | |
2113 | 融合させることになるからです。 | |
2114 | 1272 | 以下のように文字列を読み込んであなたのプログラムの変数の実際の内容の |
2115 | 1273 | ために拡張するのではなく: |
2116 | 1274 | |
2117 | $str = 'this has a $fred and $barney in it'; | |
2118 | $str =~ s/(\$\w+)/$1/eeg; # need double eval | |
2119 | 1275 | |
1276 | $str = 'this has a $fred and $barney in it'; | |
1277 | $str =~ s/(\$\w+)/$1/eeg; # need double eval | |
1278 | ||
2120 | 1279 | =begin original |
2121 | 1280 | |
2122 | 1281 | it would be better to keep a hash around like %USER_VARS and have |
2123 | 1282 | variable references actually refer to entries in that hash: |
2124 | 1283 | |
2125 | 1284 | =end original |
2126 | 1285 | |
2127 | %USER_VARS のようなハッシュを保存し、このハッシュの | |
1286 | %USER_VARS のようなハッシュを保存し、このハッシュの | |
2128 | 変数リファレンスを持つよりよい方法です: | |
1287 | エントリを参照する変数リファレンスを持つよりよい方法です: | |
2129 | 1288 | |
2130 | ||
1289 | $str =~ s/\$(\w+)/$USER_VARS{$1}/g; # no /e here at all | |
2131 | 1290 | |
2132 | 1291 | =begin original |
2133 | 1292 | |
2134 | 1293 | That's faster, cleaner, and safer than the previous approach. Of course, |
2135 | 1294 | you don't need to use a dollar sign. You could use your own scheme to |
2136 | 1295 | make it less confusing, like bracketed percent symbols, etc. |
2137 | 1296 | |
2138 | 1297 | =end original |
2139 | 1298 | |
2140 | 1299 | これは前述の手法よりも、より高速で、より明快で、より安全です。 |
2141 | 1300 | もちろん、ドル記号を使う必要はありません。 |
2142 | 1301 | パーセント記号で囲むなどのより混乱しにくい独自のスキームを使えます。 |
2143 | 1302 | |
2144 | ||
1303 | $str = 'this has a %fred% and %barney% in it'; | |
2145 | ||
1304 | $str =~ s/%(\w+)%/$USER_VARS{$1}/g; # no /e here at all | |
2146 | 1305 | |
2147 | 1306 | =begin original |
2148 | 1307 | |
2149 | 1308 | Another reason that folks sometimes think they want a variable to |
2150 | contain the name of a variable is | |
1309 | contain the name of a variable is because they don't know how to build | |
2151 | 1310 | proper data structures using hashes. For example, let's say they |
2152 | 1311 | wanted two hashes in their program: %fred and %barney, and that they |
2153 | 1312 | wanted to use another scalar variable to refer to those by name. |
2154 | 1313 | |
2155 | 1314 | =end original |
2156 | 1315 | |
2157 | 1316 | 人々が時々変数名が入った変数を欲しがるもう一つの理由は、 |
2158 | 1317 | ハッシュを使った適切なデータ構造を構築する方法を知らないからです。 |
2159 | 例えば、プログラムで | |
1318 | 例えば、プログラムで%fredと%barneyが必要で、 | |
2160 | 1319 | さらにこれらを名前で参照するスカラへ変数が必要だとします。 |
2161 | 1320 | |
2162 | ||
1321 | $name = "fred"; | |
2163 | ||
1322 | $$name{WIFE} = "wilma"; # set %fred | |
2164 | 1323 | |
2165 | ||
1324 | $name = "barney"; | |
2166 | ||
1325 | $$name{WIFE} = "betty"; # set %barney | |
2167 | 1326 | |
2168 | 1327 | =begin original |
2169 | 1328 | |
2170 | 1329 | This is still a symbolic reference, and is still saddled with the |
2171 | 1330 | problems enumerated above. It would be far better to write: |
2172 | 1331 | |
2173 | 1332 | =end original |
2174 | 1333 | |
2175 | 1334 | これはやはりシンボリックリファレンスで、やはり上記の問題を抱えたままです。 |
2176 | 1335 | 以下のように書けば遥かに改善します: |
2177 | 1336 | |
2178 | ||
1337 | $folks{"fred"}{WIFE} = "wilma"; | |
2179 | ||
1338 | $folks{"barney"}{WIFE} = "betty"; | |
2180 | 1339 | |
2181 | 1340 | =begin original |
2182 | 1341 | |
2183 | 1342 | And just use a multilevel hash to start with. |
2184 | 1343 | |
2185 | 1344 | =end original |
2186 | 1345 | |
2187 | 1346 | そして始めるのに単に多段ハッシュを使います。 |
2188 | 1347 | |
2189 | 1348 | =begin original |
2190 | 1349 | |
2191 | 1350 | The only times that you absolutely I<must> use symbolic references are |
2192 | 1351 | when you really must refer to the symbol table. This may be because it's |
2193 | something that | |
1352 | something that can't take a real reference to, such as a format name. | |
2194 | 1353 | Doing so may also be important for method calls, since these always go |
2195 | 1354 | through the symbol table for resolution. |
2196 | 1355 | |
2197 | 1356 | =end original |
2198 | 1357 | |
2199 | 1358 | 唯一あなたが完全にシンボリックリファレンスを I<使わなければならない> 場合は、 |
2200 | 1359 | シンボルテーブルに対するリファレンスが必要なときだけです。 |
2201 | 1360 | これは、フォーマット名といったものに対する真のリファレンスを得ることが |
2202 | 1361 | できないからです。 |
2203 | 1362 | そうすることはメソッド呼び出しのためにも重要です。 |
2204 | 1363 | なぜなら名前解決のためにシンボルテーブルを使うからです。 |
2205 | 1364 | |
2206 | 1365 | =begin original |
2207 | 1366 | |
2208 | 1367 | In those cases, you would turn off C<strict 'refs'> temporarily so you |
2209 | 1368 | can play around with the symbol table. For example: |
2210 | 1369 | |
2211 | 1370 | =end original |
2212 | 1371 | |
2213 | 1372 | これらの場合、一時的に C<strict 'refs'> にして |
2214 | シンボルテーブルを使うようにできます。 | |
1373 | シンボルテーブルを使うようにできます。例えば: | |
2215 | 例えば: | |
2216 | 1374 | |
2217 | ||
1375 | @colors = qw(red blue green yellow orange purple violet); | |
2218 | ||
1376 | for my $name (@colors) { | |
2219 | ||
1377 | no strict 'refs'; # renege for the block | |
2220 | ||
1378 | *$name = sub { "<FONT COLOR='$name'>@_</FONT>" }; | |
2221 | ||
1379 | } | |
2222 | 1380 | |
2223 | 1381 | =begin original |
2224 | 1382 | |
2225 | 1383 | All those functions (red(), blue(), green(), etc.) appear to be separate, |
2226 | 1384 | but the real code in the closure actually was compiled only once. |
2227 | 1385 | |
2228 | 1386 | =end original |
2229 | 1387 | |
2230 | これら(red(), blue(), green() | |
1388 | これら(red(), blue(), green()など)の関数全ては別々に現れますが、 | |
2231 | 1389 | クロージャの中の実際のコードは一回だけコンパイルされます。 |
2232 | 1390 | |
2233 | 1391 | =begin original |
2234 | 1392 | |
2235 | So, sometimes you might want to use symbolic references to | |
1393 | So, sometimes you might want to use symbolic references to directly | |
2236 | the symbol table | |
1394 | manipulate the symbol table. This doesn't matter for formats, handles, and | |
2237 | 1395 | subroutines, because they are always global--you can't use my() on them. |
2238 | 1396 | For scalars, arrays, and hashes, though--and usually for subroutines-- |
2239 | 1397 | you probably only want to use hard references. |
2240 | 1398 | |
2241 | 1399 | =end original |
2242 | 1400 | |
2243 | 1401 | シンボルテーブルを直接操作するためにシンボリックリファレンスを |
2244 | 1402 | 使いたくなることがあるかもしれません。 |
2245 | 1403 | これには、フォーマット、ハンドル、サブルーチンには関係ありません。 |
2246 | 1404 | これらは常にグローバルだからです--これらに my() を使うことはできません。 |
2247 | おそらく、スカラ、配列、ハッシュのために--そして普通はサブルーチンの | |
1405 | おそらく、スカラー、配列、ハッシュのために--そして普通はサブルーチンのために-- | |
2248 | ||
1406 | だけ、ハードリファレンスが必要でしょう。 | |
2249 | 1407 | |
2250 | =head2 What does "bad interpreter" mean? | |
2251 | ||
2252 | ("bad interpreter" とはどういう意味ですか?) | |
2253 | ||
2254 | =begin original | |
2255 | ||
2256 | (contributed by brian d foy) | |
2257 | ||
2258 | =end original | |
2259 | ||
2260 | (brian d foy によって寄贈されました) | |
2261 | ||
2262 | =begin original | |
2263 | ||
2264 | The "bad interpreter" message comes from the shell, not perl. The | |
2265 | actual message may vary depending on your platform, shell, and locale | |
2266 | settings. | |
2267 | ||
2268 | =end original | |
2269 | ||
2270 | "bad interpreter" というメッセージは perl ではなく、シェルが出力しています。 | |
2271 | 実際のメッセージはプラットフォーム、シェル、ロケール設定によって | |
2272 | 様々です。 | |
2273 | ||
2274 | =begin original | |
2275 | ||
2276 | If you see "bad interpreter - no such file or directory", the first | |
2277 | line in your perl script (the "shebang" line) does not contain the | |
2278 | right path to perl (or any other program capable of running scripts). | |
2279 | Sometimes this happens when you move the script from one machine to | |
2280 | another and each machine has a different path to perl--/usr/bin/perl | |
2281 | versus /usr/local/bin/perl for instance. It may also indicate | |
2282 | that the source machine has CRLF line terminators and the | |
2283 | destination machine has LF only: the shell tries to find | |
2284 | /usr/bin/perl<CR>, but can't. | |
2285 | ||
2286 | =end original | |
2287 | ||
2288 | "bad interpreter - no such file or directory" と表示されたら、perl | |
2289 | スクリプトの最初の行 ("#!" 行) に perl (あるいはスクリプトを実行する | |
2290 | 機能のあるその他のプログラム) への正しいパスが含まれていません。 | |
2291 | スクリプトをあるマシンから、perl のパスが異なる -- 例えば /usr/bin/perl と | |
2292 | /usr/local/bin/perl -- 他のマシンに移動させた場合に時々起こります。 | |
2293 | これはまた、元のマシンの行終端が CRLF で、移動先のマシンが LF のみの場合にも | |
2294 | 起こります; シェルが /usr/bin/perl<CR> を探そうとしますが、失敗します。 | |
2295 | ||
2296 | =begin original | |
2297 | ||
2298 | If you see "bad interpreter: Permission denied", you need to make your | |
2299 | script executable. | |
2300 | ||
2301 | =end original | |
2302 | ||
2303 | "bad interpreter: Permission denied" と表示されたら、スクリプトを | |
2304 | 実行可能にする必要があります。 | |
2305 | ||
2306 | =begin original | |
2307 | ||
2308 | In either case, you should still be able to run the scripts with perl | |
2309 | explicitly: | |
2310 | ||
2311 | =end original | |
2312 | ||
2313 | どちらの場合でも、明示的に perl でスクリプトを実行できるようにするべきです: | |
2314 | ||
2315 | % perl script.pl | |
2316 | ||
2317 | =begin original | |
2318 | ||
2319 | If you get a message like "perl: command not found", perl is not in | |
2320 | your PATH, which might also mean that the location of perl is not | |
2321 | where you expect it so you need to adjust your shebang line. | |
2322 | ||
2323 | =end original | |
2324 | ||
2325 | "perl: command not found" のようなメッセージが出た場合、perl が PATH に | |
2326 | ありません; つまりおそらくは perl の位置があなたの想定している | |
2327 | 場所ではないことも意味しているので、#! 行を調整する必要があります。 | |
2328 | ||
2329 | 1408 | =head1 AUTHOR AND COPYRIGHT |
2330 | 1409 | |
2331 | Copyright (c) 1997- | |
1410 | Copyright (c) 1997-1999 Tom Christiansen and Nathan Torkington. | |
2332 | ||
1411 | All rights reserved. | |
2333 | 1412 | |
2334 | ||
1413 | When included as part of the Standard Version of Perl, or as part of | |
2335 | ||
1414 | its complete documentation whether printed or otherwise, this work | |
1415 | may be distributed only under the terms of Perl's Artistic License. | |
1416 | Any distribution of this file or derivatives thereof I<outside> | |
1417 | of that package require that special arrangements be made with | |
1418 | copyright holder. | |
2336 | 1419 | |
2337 | 1420 | Irrespective of its distribution, all code examples in this file |
2338 | 1421 | are hereby placed into the public domain. You are permitted and |
2339 | 1422 | encouraged to use this code in your own programs for fun |
2340 | 1423 | or for profit as you see fit. A simple comment in the code giving |
2341 | 1424 | credit would be courteous but is not required. |
2342 | ||
2343 | =begin meta | |
2344 | ||
2345 | Translate: 吉村 寿人 <JAE00534@niftyserve.or.jp> | |
2346 | Update: SHIRAKATA Kentaro <argrath@ub32.org> (5.6.1-) | |
2347 | Status: completed | |
2348 | ||
2349 | =end meta |