perlopentut >
5.36.0
との差分
perlopentut 5.36.0 と 5.26.1 の差分
| 1 | 1 | |
| 2 | =encoding u | |
| 2 | =encoding euc-jp | |
| 3 | 3 | |
| 4 | 4 | =head1 NAME |
| 5 | 5 | |
| 6 | 6 | =begin original |
| 7 | 7 | |
| 8 | 8 | perlopentut - simple recipes for opening files and pipes in Perl |
| 9 | 9 | |
| 10 | 10 | =end original |
| 11 | 11 | |
| 12 | 12 | perlopentut - Perl でファイルを開いたりパイプを使ったりするための簡単なレシピ |
| 13 | 13 | |
| 14 | 14 | =head1 DESCRIPTION |
| 15 | 15 | |
| 16 | 16 | =begin original |
| 17 | 17 | |
| 18 | 18 | Whenever you do I/O on a file in Perl, you do so through what in Perl is |
| 19 | 19 | called a B<filehandle>. A filehandle is an internal name for an external |
| 20 | 20 | file. It is the job of the C<open> function to make the association |
| 21 | 21 | between the internal name and the external name, and it is the job |
| 22 | 22 | of the C<close> function to break that association. |
| 23 | 23 | |
| 24 | 24 | =end original |
| 25 | 25 | |
| 26 | 26 | Perl でファイルに対して入出力をするとき、Perl では B<ファイルハンドル> と |
| 27 | 27 | 呼ばれるものを通して行います。 |
| 28 | 28 | ファイルハンドルは外部ファイルに対する内部名です。 |
| 29 | 29 | C<open> 関数の仕事は内部名と外部名を関連づけることで、C<close> 関数は |
| 30 | 30 | 関連づけを壊すことです。 |
| 31 | 31 | |
| 32 | 32 | =begin original |
| 33 | 33 | |
| 34 | 34 | For your convenience, Perl sets up a few special filehandles that are |
| 35 | 35 | already open when you run. These include C<STDIN>, C<STDOUT>, C<STDERR>, |
| 36 | 36 | and C<ARGV>. Since those are pre-opened, you can use them right away |
| 37 | 37 | without having to go to the trouble of opening them yourself: |
| 38 | 38 | |
| 39 | 39 | =end original |
| 40 | 40 | |
| 41 | 41 | 便利なように、Perl は実行開始時に既に開いているいくつかの特別な |
| 42 | 42 | ファイルハンドルを設定します。 |
| 43 | 43 | それは C<STDIN>, C<STDOUT>, C<STDERR>, C<ARGV> です。 |
| 44 | 44 | これらは既に開いているので、自分でこれらを開くときの問題を受けることなく |
| 45 | 45 | 正しく使うことができます。 |
| 46 | 46 | |
| 47 | 47 | print STDERR "This is a debugging message.\n"; |
| 48 | 48 | |
| 49 | 49 | print STDOUT "Please enter something: "; |
| 50 | 50 | $response = <STDIN> // die "how come no input?"; |
| 51 | 51 | print STDOUT "Thank you!\n"; |
| 52 | 52 | |
| 53 | 53 | while (<ARGV>) { ... } |
| 54 | 54 | |
| 55 | 55 | =begin original |
| 56 | 56 | |
| 57 | 57 | As you see from those examples, C<STDOUT> and C<STDERR> are output |
| 58 | 58 | handles, and C<STDIN> and C<ARGV> are input handles. They are |
| 59 | 59 | in all capital letters because they are reserved to Perl, much |
| 60 | 60 | like the C<@ARGV> array and the C<%ENV> hash are. Their external |
| 61 | 61 | associations were set up by your shell. |
| 62 | 62 | |
| 63 | 63 | =end original |
| 64 | 64 | |
| 65 | 65 | これらの例で見られるように、C<STDOUT> と C<STDERR> は出力ハンドルで、 |
| 66 | 66 | C<STDIN> と C<ARGV> は入力ハンドルです。 |
| 67 | 67 | これらは C<@ARGV> 配列や C<%ENV> ハッシュと同様に Perl によって |
| 68 | 68 | 予約されているので、全て大文字になっています。 |
| 69 | 69 | これらの外部関連づけはシェルによって行われます。 |
| 70 | 70 | |
| 71 | 71 | =begin original |
| 72 | 72 | |
| 73 | 73 | You will need to open every other filehandle on your own. Although there |
| 74 | 74 | are many variants, the most common way to call Perl's open() function |
| 75 | 75 | is with three arguments and one return value: |
| 76 | 76 | |
| 77 | 77 | =end original |
| 78 | 78 | |
| 79 | 79 | その他のファイルハンドルは自分で開く必要があります。 |
| 80 | 80 | 多くのバリエーションはありますが、Perl の open() 関数を開く最も一般的な方法は |
| 81 | 81 | 3 引数と一つの返り値のものです: |
| 82 | 82 | |
| 83 | 83 | =begin original |
| 84 | 84 | |
| 85 | 85 | C< I<OK> = open(I<HANDLE>, I<MODE>, I<PATHNAME>)> |
| 86 | 86 | |
| 87 | 87 | =end original |
| 88 | 88 | |
| 89 | 89 | C< I<OK> = open(I<HANDLE>, I<MODE>, I<PATHNAME>)> |
| 90 | 90 | |
| 91 | 91 | =begin original |
| 92 | 92 | |
| 93 | 93 | Where: |
| 94 | 94 | |
| 95 | 95 | =end original |
| 96 | 96 | |
| 97 | 97 | ここで: |
| 98 | 98 | |
| 99 | 99 | =over |
| 100 | 100 | |
| 101 | 101 | =item I<OK> |
| 102 | 102 | |
| 103 | 103 | =begin original |
| 104 | 104 | |
| 105 | 105 | will be some defined value if the open succeeds, but |
| 106 | 106 | C<undef> if it fails; |
| 107 | 107 | |
| 108 | 108 | =end original |
| 109 | 109 | |
| 110 | 110 | これは、開くのに成功すれば何らかの定義された値、失敗すれば C<undef> です; |
| 111 | 111 | |
| 112 | 112 | =item I<HANDLE> |
| 113 | 113 | |
| 114 | 114 | =begin original |
| 115 | 115 | |
| 116 | 116 | should be an undefined scalar variable to be filled in by the |
| 117 | 117 | C<open> function if it succeeds; |
| 118 | 118 | |
| 119 | 119 | =end original |
| 120 | 120 | |
| 121 | 121 | これは、成功すれば C<open> 関数によって埋められる未定義のスカラ変数です; |
| 122 | 122 | |
| 123 | 123 | =item I<MODE> |
| 124 | 124 | |
| 125 | 125 | =begin original |
| 126 | 126 | |
| 127 | 127 | is the access mode and the encoding format to open the file with; |
| 128 | 128 | |
| 129 | 129 | =end original |
| 130 | 130 | |
| 131 | 131 | これはファイルを開くときのアクセスモードとエンコーディング型式です; |
| 132 | 132 | |
| 133 | 133 | =item I<PATHNAME> |
| 134 | 134 | |
| 135 | 135 | =begin original |
| 136 | 136 | |
| 137 | 137 | is the external name of the file you want opened. |
| 138 | 138 | |
| 139 | 139 | =end original |
| 140 | 140 | |
| 141 | 141 | これは開きたいファイルの外部名です。 |
| 142 | 142 | |
| 143 | 143 | =back |
| 144 | 144 | |
| 145 | 145 | =begin original |
| 146 | 146 | |
| 147 | 147 | Most of the complexity of the C<open> function lies in the many |
| 148 | 148 | possible values that the I<MODE> parameter can take on. |
| 149 | 149 | |
| 150 | 150 | =end original |
| 151 | 151 | |
| 152 | 152 | C<open> 関数の複雑さの大部分は、I<MODE> 引数が多くの値を |
| 153 | 153 | 取ることのできることにあります。 |
| 154 | 154 | |
| 155 | 155 | =begin original |
| 156 | 156 | |
| 157 | 157 | One last thing before we show you how to open files: opening |
| 158 | 158 | files does not (usually) automatically lock them in Perl. See |
| 159 | 159 | L<perlfaq5> for how to lock. |
| 160 | 160 | |
| 161 | 161 | =end original |
| 162 | 162 | |
| 163 | 163 | ファイルの開き方を説明する前に最後に一言: Perl ではファイルを開いても |
| 164 | 164 | (普通は)自動的にロックすることはしません。 |
| 165 | 165 | ロックの方法については L<perlfaq5> を参照してください。 |
| 166 | 166 | |
| 167 | 167 | =head1 Opening Text Files |
| 168 | 168 | |
| 169 | 169 | (テキストファイルを開く) |
| 170 | 170 | |
| 171 | 171 | =head2 Opening Text Files for Reading |
| 172 | 172 | |
| 173 | 173 | (読み込み用にテキストファイルを開く) |
| 174 | 174 | |
| 175 | 175 | =begin original |
| 176 | 176 | |
| 177 | 177 | If you want to read from a text file, first open it in |
| 178 | 178 | read-only mode like this: |
| 179 | 179 | |
| 180 | 180 | =end original |
| 181 | 181 | |
| 182 | 182 | テキストファイルを読み込みたい場合、まず次のように読み込み専用モードで |
| 183 | 183 | 開きます: |
| 184 | 184 | |
| 185 | 185 | my $filename = "/some/path/to/a/textfile/goes/here"; |
| 186 | 186 | my $encoding = ":encoding(UTF-8)"; |
| 187 | 187 | my $handle = undef; # this will be filled in on success |
| 188 | 188 | |
| 189 | 189 | open($handle, "< $encoding", $filename) |
| 190 | 190 | || die "$0: can't open $filename for reading: $!"; |
| 191 | 191 | |
| 192 | 192 | =begin original |
| 193 | 193 | |
| 194 | 194 | As with the shell, in Perl the C<< "<" >> is used to open the file in |
| 195 | 195 | read-only mode. If it succeeds, Perl allocates a brand new filehandle for |
| 196 | 196 | you and fills in your previously undefined C<$handle> argument with a |
| 197 | 197 | reference to that handle. |
| 198 | 198 | |
| 199 | 199 | =end original |
| 200 | 200 | |
| 201 | 201 | シェルと同様に、Perl でもファイルを読み込み専用モードで開くために |
| 202 | 202 | C<< "<" >> が使われます。 |
| 203 | 203 | これに成功すると、Perl は新しいファイルハンドルを割り当て、未定義だった |
| 204 | 204 | C<$handle> 引数にそのハンドルへのリファレンスを設定します。 |
| 205 | 205 | |
| 206 | 206 | =begin original |
| 207 | 207 | |
| 208 | 208 | Now you may use functions like C<readline>, C<read>, C<getc>, and |
| 209 | 209 | C<sysread> on that handle. Probably the most common input function |
| 210 | 210 | is the one that looks like an operator: |
| 211 | 211 | |
| 212 | 212 | =end original |
| 213 | 213 | |
| 214 | 214 | これでこのハンドルに対して C<readline>, C<read>, C<getc>, |
| 215 | 215 | C<sysread> のような関数が使えます。 |
| 216 | 216 | おそらく最も一般的な入力関数は演算子のように見えるものでしょう: |
| 217 | 217 | |
| 218 | 218 | $line = readline($handle); |
| 219 | 219 | $line = <$handle>; # same thing |
| 220 | 220 | |
| 221 | 221 | =begin original |
| 222 | 222 | |
| 223 | 223 | Because the C<readline> function returns C<undef> at end of file or |
| 224 | 224 | upon error, you will sometimes see it used this way: |
| 225 | 225 | |
| 226 | 226 | =end original |
| 227 | 227 | |
| 228 | 228 | C<readline> 関数はファイル終端やエラーのときに C<undef> を返すので、 |
| 229 | 229 | 時々次のように使われているのを見るでしょう: |
| 230 | 230 | |
| 231 | 231 | $line = <$handle>; |
| 232 | 232 | if (defined $line) { |
| 233 | 233 | # do something with $line |
| 234 | 234 | } |
| 235 | 235 | else { |
| 236 | 236 | # $line is not valid, so skip it |
| 237 | 237 | } |
| 238 | 238 | |
| 239 | 239 | =begin original |
| 240 | 240 | |
| 241 | 241 | You can also just quickly C<die> on an undefined value this way: |
| 242 | 242 | |
| 243 | 243 | =end original |
| 244 | 244 | |
| 245 | 245 | また、次のようにして単に未定義値に対してすばやく C<die> することもできます: |
| 246 | 246 | |
| 247 | 247 | $line = <$handle> // die "no input found"; |
| 248 | 248 | |
| 249 | 249 | =begin original |
| 250 | 250 | |
| 251 | 251 | However, if hitting EOF is an expected and normal event, you do not want to |
| 252 | 252 | exit simply because you have run out of input. Instead, you probably just want |
| 253 | 253 | to exit an input loop. You can then test to see if an actual error has caused |
| 254 | 254 | the loop to terminate, and act accordingly: |
| 255 | 255 | |
| 256 | 256 | =end original |
| 257 | 257 | |
| 258 | 258 | しかし、EOF に到達するのが想定されていて通常の出来事の場合は、 |
| 259 | 259 | 入力がなくなっただけで終了したくありません。 |
| 260 | 260 | そうではなく、単に入力ループを終了したいでしょう。 |
| 261 | 261 | 実際のエラーがループを終了させたのかをテストして、適切に行動できます: |
| 262 | 262 | |
| 263 | 263 | while (<$handle>) { |
| 264 | 264 | # do something with data in $_ |
| 265 | 265 | } |
| 266 | 266 | if ($!) { |
| 267 | 267 | die "unexpected error while reading from $filename: $!"; |
| 268 | 268 | } |
| 269 | 269 | |
| 270 | 270 | =begin original |
| 271 | 271 | |
| 272 | 272 | B<A Note on Encodings>: Having to specify the text encoding every time |
| 273 | 273 | might seem a bit of a bother. To set up a default encoding for C<open> so |
| 274 | 274 | that you don't have to supply it each time, you can use the C<open> pragma: |
| 275 | 275 | |
| 276 | 276 | =end original |
| 277 | 277 | |
| 278 | 278 | B<エンコーディングに関する注意>: テキストエンコーディングを毎回指定する |
| 279 | 279 | 必要があるのは少し面倒に感じるかもしれません。 |
| 280 | 280 | 毎回設定する必要がないように C<open> のためのデフォルトエンコーディングを |
| 281 | 281 | 設定するために、C<open> プラグマを使えます: |
| 282 | 282 | |
| 283 | 283 | use open qw< :encoding(UTF-8) >; |
| 284 | 284 | |
| 285 | 285 | =begin original |
| 286 | 286 | |
| 287 | 287 | Once you've done that, you can safely omit the encoding part of the |
| 288 | 288 | open mode: |
| 289 | 289 | |
| 290 | 290 | =end original |
| 291 | 291 | |
| 292 | 292 | 一度これを行えば、open モードからエンコーディングの部分を安全に省略できます: |
| 293 | 293 | |
| 294 | 294 | open($handle, "<", $filename) |
| 295 | 295 | || die "$0: can't open $filename for reading: $!"; |
| 296 | 296 | |
| 297 | 297 | =begin original |
| 298 | 298 | |
| 299 | 299 | But never use the bare C<< "<" >> without having set up a default encoding |
| 300 | 300 | first. Otherwise, Perl cannot know which of the many, many, many possible |
| 301 | 301 | flavors of text file you have, and Perl will have no idea how to correctly |
| 302 | 302 | map the data in your file into actual characters it can work with. Other |
| 303 | 303 | common encoding formats including C<"ASCII">, C<"ISO-8859-1">, |
| 304 | 304 | C<"ISO-8859-15">, C<"Windows-1252">, C<"MacRoman">, and even C<"UTF-16LE">. |
| 305 | 305 | See L<perlunitut> for more about encodings. |
| 306 | 306 | |
| 307 | 307 | =end original |
| 308 | 308 | |
| 309 | 309 | しかし、先にデフォルトのエンコーディングを設定することなく裸の |
| 310 | 310 | C<< "<" >> を使うことは決してしないでください。 |
| 311 | 311 | さもなければ、Perl はとてもとてもとてもたくさんあるテキストファイルの |
| 312 | 312 | 種類のうちどれかを知ることができず、Perl はあなたのファイルのデータを |
| 313 | 313 | 動作させるための実際の文字にマッピングすることができません。 |
| 314 | 314 | その他のよくあるエンコーディング形式には |
| 315 | 315 | C<"ASCII">, C<"ISO-8859-1">, |
| 316 | 316 | C<"ISO-8859-15">, C<"Windows-1252">, C<"MacRoman"> および、 |
| 317 | 317 | C<"UTF-16LE"> すらもあります。 |
| 318 | 318 | エンコーディングに関するさらなる情報については L<perlunitut> を |
| 319 | 319 | 参照してください。 |
| 320 | 320 | |
| 321 | 321 | =head2 Opening Text Files for Writing |
| 322 | 322 | |
| 323 | 323 | (書き込み用にテキストファイルを開く) |
| 324 | 324 | |
| 325 | 325 | =begin original |
| 326 | 326 | |
| 327 | 327 | When you want to write to a file, you first have to decide what to do about |
| 328 | 328 | any existing contents of that file. You have two basic choices here: to |
| 329 | 329 | preserve or to clobber. |
| 330 | 330 | |
| 331 | 331 | =end original |
| 332 | 332 | |
| 333 | 333 | ファイルに書き込みたい場合、そのファイルの既存の内容をどうするかを |
| 334 | 334 | まず決定する必要があります。 |
| 335 | 335 | 二つの基本的な選択肢があります: 保存するか上書きするかです。 |
| 336 | 336 | |
| 337 | 337 | =begin original |
| 338 | 338 | |
| 339 | 339 | If you want to preserve any existing contents, then you want to open the file |
| 340 | 340 | in append mode. As in the shell, in Perl you use C<<< ">>" >>> to open an |
| 341 | 341 | existing file in append mode. C<<< ">>" >>> creates the file if it does not |
| 342 | 342 | already exist. |
| 343 | 343 | |
| 344 | 344 | =end original |
| 345 | 345 | |
| 346 | 346 | 既存の内容を保存したい場合、ファイルを追記モードで開きます。 |
| 347 | 347 | シェルと同様に、 Perl でも既存のファイルを追記モードで開くために |
| 348 | 348 | C<<< ">>" >>> が使われます。 |
| 349 | 349 | ファイルがない場合、C<<< ">>" >>> はファイルを作ります。 |
| 350 | 350 | |
| 351 | 351 | my $handle = undef; |
| 352 | 352 | my $filename = "/some/path/to/a/textfile/goes/here"; |
| 353 | 353 | my $encoding = ":encoding(UTF-8)"; |
| 354 | 354 | |
| 355 | 355 | open($handle, ">> $encoding", $filename) |
| 356 | 356 | || die "$0: can't open $filename for appending: $!"; |
| 357 | 357 | |
| 358 | 358 | =begin original |
| 359 | 359 | |
| 360 | 360 | Now you can write to that filehandle using any of C<print>, C<printf>, |
| 361 | 361 | C<say>, C<write>, or C<syswrite>. |
| 362 | 362 | |
| 363 | 363 | =end original |
| 364 | 364 | |
| 365 | 365 | これでこのハンドルに対して C<print>, C<printf>, |
| 366 | 366 | C<say>, C<write>, C<syswrite> を使って書き込めます。 |
| 367 | 367 | |
| 368 | 368 | =begin original |
| 369 | 369 | |
| 370 | 370 | As noted above, if the file does not already exist, then the append-mode open |
| 371 | 371 | will create it for you. But if the file does already exist, its contents are |
| 372 | 372 | safe from harm because you will be adding your new text past the end of the |
| 373 | 373 | old text. |
| 374 | 374 | |
| 375 | 375 | =end original |
| 376 | 376 | |
| 377 | 377 | 前述したように、ファイルが既に存在していない場合、追記モードで開くと |
| 378 | 378 | ファイルを作ります。 |
| 379 | 379 | しかしファイルが既に存在している場合、その内容は保護されます; 新しいテキストは |
| 380 | 380 | 既存のテキストの末尾に追加されるからです。 |
| 381 | 381 | |
| 382 | 382 | =begin original |
| 383 | 383 | |
| 384 | 384 | On the other hand, sometimes you want to clobber whatever might already be |
| 385 | 385 | there. To empty out a file before you start writing to it, you can open it |
| 386 | 386 | in write-only mode: |
| 387 | 387 | |
| 388 | 388 | =end original |
| 389 | 389 | |
| 390 | 390 | 一方、時々、既に何かがあっても上書きしたいときもあります。 |
| 391 | 391 | 書き込みを始める前にファイルを消すために、書き込み専用モードで |
| 392 | 392 | 開くことができます: |
| 393 | 393 | |
| 394 | 394 | my $handle = undef; |
| 395 | 395 | my $filename = "/some/path/to/a/textfile/goes/here"; |
| 396 | 396 | my $encoding = ":encoding(UTF-8)"; |
| 397 | 397 | |
| 398 | 398 | open($handle, "> $encoding", $filename) |
| 399 | 399 | || die "$0: can't open $filename in write-open mode: $!"; |
| 400 | 400 | |
| 401 | 401 | =begin original |
| 402 | 402 | |
| 403 | 403 | Here again Perl works just like the shell in that the C<< ">" >> clobbers |
| 404 | 404 | an existing file. |
| 405 | 405 | |
| 406 | 406 | =end original |
| 407 | 407 | |
| 408 | 408 | ここで再び Perl はシェルと同様に動作し、C<< ">" >> は既存のファイルを |
| 409 | 409 | 上書きします。 |
| 410 | 410 | |
| 411 | 411 | =begin original |
| 412 | 412 | |
| 413 | 413 | As with the append mode, when you open a file in write-only mode, |
| 414 | 414 | you can now write to that filehandle using any of C<print>, C<printf>, |
| 415 | 415 | C<say>, C<write>, or C<syswrite>. |
| 416 | 416 | |
| 417 | 417 | =end original |
| 418 | 418 | |
| 419 | 419 | 追記モードと同様に、ファイルを書き込みモードで開くと、 |
| 420 | 420 | C<print>, C<printf>, C<say>, C<write>, C<syswrite> を使って |
| 421 | 421 | ファイルハンドルに書き込めるようになります。 |
| 422 | 422 | |
| 423 | 423 | =begin original |
| 424 | 424 | |
| 425 | 425 | What about read-write mode? You should probably pretend it doesn't exist, |
| 426 | 426 | because opening text files in read-write mode is unlikely to do what you |
| 427 | 427 | would like. See L<perlfaq5> for details. |
| 428 | 428 | |
| 429 | 429 | =end original |
| 430 | 430 | |
| 431 | 431 | 読み書きモードについては? |
| 432 | 432 | おそらくそれは存在しないというふりをした方がよいでしょう; |
| 433 | 433 | なぜならテキストファイルを読み書きモードで開いても |
| 434 | 434 | おそらくあなたが望んでいることをしないからです。 |
| 435 | 435 | 詳しくは L<perlfaq5> を参照してください。 |
| 436 | 436 | |
| 437 | 437 | =head1 Opening Binary Files |
| 438 | 438 | |
| 439 | 439 | (バイナリファイルを開く) |
| 440 | 440 | |
| 441 | 441 | =begin original |
| 442 | 442 | |
| 443 | 443 | If the file to be opened contains binary data instead of text characters, |
| 444 | 444 | then the C<MODE> argument to C<open> is a little different. Instead of |
| 445 | 445 | specifying the encoding, you tell Perl that your data are in raw bytes. |
| 446 | 446 | |
| 447 | 447 | =end original |
| 448 | 448 | |
| 449 | 449 | 開こうとしているファイルがテキスト文字ではなくバイナリデータが含まれている |
| 450 | 450 | 場合、C<open> の C<MODE> 引数は少し異なるものになります。 |
| 451 | 451 | エンコーディングを指定する代わりに、データが生のバイト列であることを |
| 452 | 452 | Perl に知らせます。 |
| 453 | 453 | |
| 454 | 454 | my $filename = "/some/path/to/a/binary/file/goes/here"; |
| 455 | 455 | my $encoding = ":raw :bytes" |
| 456 | 456 | my $handle = undef; # this will be filled in on success |
| 457 | 457 | |
| 458 | 458 | =begin original |
| 459 | 459 | |
| 460 | 460 | And then open as before, choosing C<<< "<" >>>, C<<< ">>" >>>, or |
| 461 | 461 | C<<< ">" >>> as needed: |
| 462 | 462 | |
| 463 | 463 | =end original |
| 464 | 464 | |
| 465 | 465 | それから前述の通り、必要に応じて |
| 466 | 466 | C<<< "<" >>>, C<<< ">>" >>>, C<<< ">" >>> を選びます: |
| 467 | 467 | |
| 468 | 468 | open($handle, "< $encoding", $filename) |
| 469 | 469 | || die "$0: can't open $filename for reading: $!"; |
| 470 | 470 | |
| 471 | 471 | open($handle, ">> $encoding", $filename) |
| 472 | 472 | || die "$0: can't open $filename for appending: $!"; |
| 473 | 473 | |
| 474 | 474 | open($handle, "> $encoding", $filename) |
| 475 | 475 | || die "$0: can't open $filename in write-open mode: $!"; |
| 476 | 476 | |
| 477 | 477 | =begin original |
| 478 | 478 | |
| 479 | 479 | Alternately, you can change to binary mode on an existing handle this way: |
| 480 | 480 | |
| 481 | 481 | =end original |
| 482 | 482 | |
| 483 | 483 | あるいは、次のようにして既に存在しているハンドルをバイナリモードに |
| 484 | 484 | 変えることが出来ます: |
| 485 | 485 | |
| 486 | 486 | binmode($handle) || die "cannot binmode handle"; |
| 487 | 487 | |
| 488 | 488 | =begin original |
| 489 | 489 | |
| 490 | 490 | This is especially handy for the handles that Perl has already opened for you. |
| 491 | 491 | |
| 492 | 492 | =end original |
| 493 | 493 | |
| 494 | 494 | これは、Perl が既に開いているハンドルに対して特に有用です。 |
| 495 | 495 | |
| 496 | 496 | binmode(STDIN) || die "cannot binmode STDIN"; |
| 497 | 497 | binmode(STDOUT) || die "cannot binmode STDOUT"; |
| 498 | 498 | |
| 499 | 499 | =begin original |
| 500 | 500 | |
| 501 | 501 | You can also pass C<binmode> an explicit encoding to change it on the fly. |
| 502 | 502 | This isn't exactly "binary" mode, but we still use C<binmode> to do it: |
| 503 | 503 | |
| 504 | 504 | =end original |
| 505 | 505 | |
| 506 | 506 | また、その場で変更するために C<binmode> に明示的にエンコーディングを |
| 507 | 507 | 渡すこともできます。 |
| 508 | 508 | これは正確には「バイナリ」モードではありませんが、それでも |
| 509 | 509 | これをするために C<binmode> を使います: |
| 510 | 510 | |
| 511 | 511 | binmode(STDIN, ":encoding(MacRoman)") || die "cannot binmode STDIN"; |
| 512 | 512 | binmode(STDOUT, ":encoding(UTF-8)") || die "cannot binmode STDOUT"; |
| 513 | 513 | |
| 514 | 514 | =begin original |
| 515 | 515 | |
| 516 | 516 | Once you have your binary file properly opened in the right mode, you can |
| 517 | 517 | use all the same Perl I/O functions as you used on text files. However, |
| 518 | 518 | you may wish to use the fixed-size C<read> instead of the variable-sized |
| 519 | 519 | C<readline> for your input. |
| 520 | 520 | |
| 521 | 521 | =end original |
| 522 | 522 | |
| 523 | 523 | 一旦バイナリファイルを正しいモードで適切に開くと、テキストファイルで |
| 524 | 524 | 使ったものと全て同じ Perl I/O 関数を使えます。 |
| 525 | 525 | しかし、入力に対して可変長の C<readline> ではなく固定長の |
| 526 | 526 | C<read> を使った方が良いでしょう。 |
| 527 | 527 | |
| 528 | 528 | =begin original |
| 529 | 529 | |
| 530 | 530 | Here's an example of how to copy a binary file: |
| 531 | 531 | |
| 532 | 532 | =end original |
| 533 | 533 | |
| 534 | 534 | 次のものはバイナリファイルをコピーする例です: |
| 535 | 535 | |
| 536 | 536 | my $BUFSIZ = 64 * (2 ** 10); |
| 537 | 537 | my $name_in = "/some/input/file"; |
| 538 | 538 | my $name_out = "/some/output/flie"; |
| 539 | 539 | |
| 540 | 540 | my($in_fh, $out_fh, $buffer); |
| 541 | 541 | |
| 542 | 542 | open($in_fh, "<", $name_in) |
| 543 | 543 | || die "$0: cannot open $name_in for reading: $!"; |
| 544 | 544 | open($out_fh, ">", $name_out) |
| 545 | 545 | || die "$0: cannot open $name_out for writing: $!"; |
| 546 | 546 | |
| 547 | 547 | for my $fh ($in_fh, $out_fh) { |
| 548 | 548 | binmode($fh) || die "binmode failed"; |
| 549 | 549 | } |
| 550 | 550 | |
| 551 | 551 | while (read($in_fh, $buffer, $BUFSIZ)) { |
| 552 | 552 | unless (print $out_fh $buffer) { |
| 553 | 553 | die "couldn't write to $name_out: $!"; |
| 554 | 554 | } |
| 555 | 555 | } |
| 556 | 556 | |
| 557 | 557 | close($in_fh) || die "couldn't close $name_in: $!"; |
| 558 | 558 | close($out_fh) || die "couldn't close $name_out: $!"; |
| 559 | 559 | |
| 560 | 560 | =head1 Opening Pipes |
| 561 | 561 | |
| 562 | 562 | (パイプを開く) |
| 563 | 563 | |
| 564 | 564 | =begin original |
| 565 | 565 | |
| 566 | ||
| 566 | To be announced. | |
| 567 | command rather than into a file. You can do this in order to pass data | |
| 568 | from your Perl program to an external command for further processing, or | |
| 569 | to receive data from another program for your own Perl program to | |
| 570 | process. | |
| 571 | 567 | |
| 572 | 568 | =end original |
| 573 | 569 | |
| 574 | ||
| 570 | 未定。 | |
| 575 | ファイルハンドルも開きます。 | |
| 576 | これを、更なる処理のために Perl プログラムから外部コマンドへ渡すため、 | |
| 577 | または処理する Perl プログラムのために他のプログラムからデータを | |
| 578 | 受け取るために行えます。 | |
| 579 | 571 | |
| 580 | = | |
| 572 | =head1 Low-level File Opens via sysopen | |
| 581 | 573 | |
| 582 | ||
| 574 | (sysopen 経由で低レベルにファイルを開く) | |
| 583 | similar inter-process communication principles as Unix pipelines. Such a | |
| 584 | filehandle has an active program instead of a static file on its | |
| 585 | external end, but in every other sense it works just like a more typical | |
| 586 | file-based filehandle, with all the techniques discussed earlier in this | |
| 587 | article just as applicable. | |
| 588 | 575 | |
| 589 | =end original | |
| 590 | ||
| 591 | コマンドへのファイルハンドルは、I<パイプ> としても知られます; | |
| 592 | Unix パイプラインという似たようなプロセス間通信原則に基づいて | |
| 593 | 動作するからです。 | |
| 594 | そのようなファイルハンドルは、外側が静的なファイルではなく | |
| 595 | 動作中のプログラムですが、それ以外の点については | |
| 596 | より典型的なファイルベースのファイルハンドルとちょうど同じように | |
| 597 | 動作し、この文書で既に議論した全てのテクニックが利用可能です。 | |
| 598 | ||
| 599 | 576 | =begin original |
| 600 | 577 | |
| 601 | ||
| 578 | To be announced. Or deleted. | |
| 602 | opening files, setting the second (C<MODE>) argument to special | |
| 603 | characters that indicate either an input or an output pipe. Use C<"-|"> for a | |
| 604 | filehandle that will let your Perl program read data from an external | |
| 605 | program, and C<"|-"> for a filehandle that will send data to that | |
| 606 | program instead. | |
| 607 | 579 | |
| 608 | 580 | =end original |
| 609 | 581 | |
| 610 | ||
| 582 | 未定。 | |
| 611 | ||
| 583 | または削除する。 | |
| 612 | パイプを開きます。 | |
| 613 | Perl プログラムが外部プログラムからデータを読み込むファイルハンドルには | |
| 614 | C<"-|"> を使います; プログラムにデータを送るファイルハンドルには | |
| 615 | C<"|-"> を使います。 | |
| 616 | 584 | |
| 617 | =head2 Opening a pipe for reading | |
| 618 | ||
| 619 | (読み込み用にパイプを開く) | |
| 620 | ||
| 621 | =begin original | |
| 622 | ||
| 623 | Let's say you'd like your Perl program to process data stored in a nearby | |
| 624 | directory called C<unsorted>, which contains a number of textfiles. | |
| 625 | You'd also like your program to sort all the contents from these files | |
| 626 | into a single, alphabetically sorted list of unique lines before it | |
| 627 | starts processing them. | |
| 628 | ||
| 629 | =end original | |
| 630 | ||
| 631 | たくさんのテキストファイルが含まれている、C<unsorted> と呼ばれる | |
| 632 | 近くのディレクトリに保管されているデータを処理する | |
| 633 | Perl プログラムが欲しいとしましょう。 | |
| 634 | また、処理を開始する前に、複数のファイルを単一の、ユニークな行を | |
| 635 | アルファベット順にソートしたいとします。 | |
| 636 | ||
| 637 | =begin original | |
| 638 | ||
| 639 | You could do this through opening an ordinary filehandle into each of | |
| 640 | those files, gradually building up an in-memory array of all the file | |
| 641 | contents you load this way, and finally sorting and filtering that array | |
| 642 | when you've run out of files to load. I<Or>, you could offload all that | |
| 643 | merging and sorting into your operating system's own C<sort> command by | |
| 644 | opening a pipe directly into its output, and get to work that much | |
| 645 | faster. | |
| 646 | ||
| 647 | =end original | |
| 648 | ||
| 649 | それぞれのファイルに対して通常のファイルハンドルを開き、 | |
| 650 | このようにして読み込んだ全てのファイルの内容を徐々にメモリ内の配列に | |
| 651 | 構築し、読み込むファイルがなくなったら最後にソートとフィルタリングをする、 | |
| 652 | という形でこれを行うことも出来ます。 | |
| 653 | I<あるいは>、結合とソートをオペレーティング自身の C<sort> コマンドに | |
| 654 | 任せて、その出力を直接パイプで開くことで、遙かに速く作業することも出来ます。 | |
| 655 | ||
| 656 | =begin original | |
| 657 | ||
| 658 | Here's how that might look: | |
| 659 | ||
| 660 | =end original | |
| 661 | ||
| 662 | 以下は、これがどのように見えるかです: | |
| 663 | ||
| 664 | open(my $sort_fh, '-|', 'sort -u unsorted/*.txt') | |
| 665 | or die "Couldn't open a pipe into sort: $!"; | |
| 666 | ||
| 667 | # And right away, we can start reading sorted lines: | |
| 668 | while (my $line = <$sort_fh>) { | |
| 669 | # | |
| 670 | # ... Do something interesting with each $line here ... | |
| 671 | # | |
| 672 | } | |
| 673 | ||
| 674 | =begin original | |
| 675 | ||
| 676 | The second argument to C<open>, C<"-|">, makes it a read-pipe into a | |
| 677 | separate program, rather than an ordinary filehandle into a file. | |
| 678 | ||
| 679 | =end original | |
| 680 | ||
| 681 | C<open> の 2 番目の引数である C<"-|"> は、ファイルへの通常の | |
| 682 | ファイルハンドルではなく、別個のプログラムへの読み込みパイプにします。 | |
| 683 | ||
| 684 | =begin original | |
| 685 | ||
| 686 | Note that the third argument to C<open> is a string containing the | |
| 687 | program name (C<sort>) plus all its arguments: in this case, C<-u> to | |
| 688 | specify unqiue sort, and then a fileglob specifying the files to sort. | |
| 689 | The resulting filehandle C<$sort_fh> works just like a read-only (C<< | |
| 690 | "<" >>) filehandle, and your program can subsequently read data | |
| 691 | from it as if it were opened onto an ordinary, single file. | |
| 692 | ||
| 693 | =end original | |
| 694 | ||
| 695 | C<open> の 3 番目の引数は、 | |
| 696 | プログラム名 (C<sort>) とその全ての引数を含んだ文字列です: | |
| 697 | この場合、C<-u> はユニークソートを指定し、それからファイルグロブは | |
| 698 | ソートするファイルを指定することに注意してください。 | |
| 699 | 結果のファイルハンドル C<$sort_fh> は | |
| 700 | ちょうど読み込み専用 (C<< "<" >>) ファイルハンドルのように動作し、 | |
| 701 | プログラムは、通常の単一のファイルが開かれたかのように、 | |
| 702 | 引き続いてそこからデータを読み込むことができます。 | |
| 703 | ||
| 704 | =head2 Opening a pipe for writing | |
| 705 | ||
| 706 | (書き込み用にパイプを開く) | |
| 707 | ||
| 708 | =begin original | |
| 709 | ||
| 710 | Continuing the previous example, let's say that your program has | |
| 711 | completed its processing, and the results sit in an array called | |
| 712 | C<@processed>. You want to print these lines to a file called | |
| 713 | C<numbered.txt> with a neatly formatted column of line-numbers. | |
| 714 | ||
| 715 | =end original | |
| 716 | ||
| 717 | 前回の例の続きとして、プログラムの処理を完成させて、 | |
| 718 | 結果は C<@processed> と呼ばれる配列に入っているとしましょう。 | |
| 719 | これらの行を C<numbered.txt> というファイル名に、 | |
| 720 | いい感じに整形された行番号の列と共に出力したいとします。 | |
| 721 | ||
| 722 | =begin original | |
| 723 | ||
| 724 | Certainly you could write your own code to do this ? or, once again, | |
| 725 | you could kick that work over to another program. In this case, C<cat>, | |
| 726 | running with its own C<-n> option to activate line numbering, should do | |
| 727 | the trick: | |
| 728 | ||
| 729 | =end original | |
| 730 | ||
| 731 | 確かにこれをするコードを自分で書くこともできます - あるいは、再び、 | |
| 732 | この作業を他のプログラムに送ることもできます。 | |
| 733 | この場合、C<cat> を、行番号付けを有効にする C<-n> オプション込みで | |
| 734 | 実行するには、次の技を使います: | |
| 735 | ||
| 736 | open(my $cat_fh, '|-', 'cat -n > numbered.txt') | |
| 737 | or die "Couldn't open a pipe into cat: $!"; | |
| 738 | ||
| 739 | for my $line (@processed) { | |
| 740 | print $cat_fh $line; | |
| 741 | } | |
| 742 | ||
| 743 | =begin original | |
| 744 | ||
| 745 | Here, we use a second C<open> argument of C<"|-">, signifying that the | |
| 746 | filehandle assigned to C<$cat_fh> should be a write-pipe. We can then | |
| 747 | use it just as we would a write-only ordinary filehandle, including the | |
| 748 | basic function of C<print>-ing data to it. | |
| 749 | ||
| 750 | =end original | |
| 751 | ||
| 752 | ここで、C<open> の 2 番目の引数に C<"|-"> を使います; | |
| 753 | これにより、C<$cat_fh> に代入されるファイルハンドルが書き込み | |
| 754 | パイプであることを示します。 | |
| 755 | それから、データを C<print> する基本的な関数を含めて、 | |
| 756 | 書き込み専用の普通のファイルハンドルを使うのと同じようにこれを使えます。 | |
| 757 | ||
| 758 | =begin original | |
| 759 | ||
| 760 | Note that the third argument, specifying the command that we wish to | |
| 761 | pipe to, sets up C<cat> to redirect its output via that C<< ">" >> | |
| 762 | symbol into the file C<numbered.txt>. This can start to look a little | |
| 763 | tricky, because that same symbol would have meant something | |
| 764 | entirely different had it showed it in the second argument to C<open>! | |
| 765 | But here in the third argument, it's simply part of the shell command that | |
| 766 | Perl will open the pipe into, and Perl itself doesn't invest any special | |
| 767 | meaning to it. | |
| 768 | ||
| 769 | =end original | |
| 770 | ||
| 771 | パイプしたいコマンドを指定する 3 番目の引数は、 | |
| 772 | C<cat> の出力を C<< ">" >> 記号を使ってファイル C<numbered.txt> に | |
| 773 | リダイレクトするように指定していることに注意してください。 | |
| 774 | これは最初は少しおかしく見えるかもしれません; | |
| 775 | この同じ記号は、C<open> の 2 番目の引数では全く違うものを意味するからです! | |
| 776 | しかし、ここ 3 番目の引数では、これは単に Perl がパイプを開く | |
| 777 | シェルコマンドの一部であり、Perl 自身はこれに何の特別な意味も与えません。 | |
| 778 | ||
| 779 | =head2 Expressing the command as a list | |
| 780 | ||
| 781 | (コマンドをリストとして表現する) | |
| 782 | ||
| 783 | =begin original | |
| 784 | ||
| 785 | For opening pipes, Perl offers the option to call C<open> with a list | |
| 786 | comprising the desired command and all its own arguments as separate | |
| 787 | elements, rather than combining them into a single string as in the | |
| 788 | examples above. For instance, we could have phrased the C<open> call in | |
| 789 | the first example like this: | |
| 790 | ||
| 791 | =end original | |
| 792 | ||
| 793 | パイプを開くために、Perl は、目的のコマンドとそれ自身の引数を、 | |
| 794 | 前述の例のように単一の文字列として結合するのではなく、 | |
| 795 | 別個の要素として構成されたリストで C<open> を呼び出すという | |
| 796 | 選択肢を提供しています。 | |
| 797 | 例えば、最初の例の C<open> 呼び出しは次のように書けます: | |
| 798 | ||
| 799 | open(my $sort_fh, '-|', 'sort', '-u', glob('unsorted/*.txt')) | |
| 800 | or die "Couldn't open a pipe into sort: $!"; | |
| 801 | ||
| 802 | =begin original | |
| 803 | ||
| 804 | When you call C<open> this way, Perl invokes the given command directly, | |
| 805 | bypassing the shell. As such, the shell won't try to interpret any | |
| 806 | special characters within the command's argument list, which might | |
| 807 | overwise have unwanted effects. This can make for safer, less | |
| 808 | error-prone C<open> calls, useful in cases such as passing in variables | |
| 809 | as arguments, or even just referring to filenames with spaces in them. | |
| 810 | ||
| 811 | =end original | |
| 812 | ||
| 813 | この方法で C<open> を呼び出す場合、 | |
| 814 | Perl はシェルをバイパスして指定されたコマンドを直接起動します。 | |
| 815 | シェルはコマンド路の引数リストの中の特殊文字を解釈しようとはしません; | |
| 816 | さもなければ望まない効果を生むことがあります。 | |
| 817 | これはより安全で、C<open> 呼び出しの誤りを減らし、 | |
| 818 | 引数として変数の内容を渡すような場合に有用で、 | |
| 819 | 単に空白を含むファイルを参照する場合にも安全です。 | |
| 820 | ||
| 821 | =begin original | |
| 822 | ||
| 823 | However, when you I<do> want to pass a meaningful metacharacter to the | |
| 824 | shell, such with the C<"*"> inside that final C<unsorted/*.txt> argument | |
| 825 | here, you can't use this alternate syntax. In this case, we have worked | |
| 826 | around it via Perl's handy C<glob> built-in function, which evaluates | |
| 827 | its argument into a list of filenames ? and we can safely pass that | |
| 828 | resulting list right into C<open>, as shown above. | |
| 829 | ||
| 830 | =end original | |
| 831 | ||
| 832 | しかし、シェルに意味のあるメタ文字を I<渡したい>、 | |
| 833 | 例えば最終的な C<unsorted/*.txt> の中の C<"*"> のような場合、 | |
| 834 | この代替文法は使えません。 | |
| 835 | この場合、引数をファイル名として評価する Perl の便利な C<glob> 組み込み関数で | |
| 836 | 回避します; そして前述したように、結果のリストを C<open> に安全に | |
| 837 | 渡せます。 | |
| 838 | ||
| 839 | =begin original | |
| 840 | ||
| 841 | Note also that representing piped-command arguments in list form like | |
| 842 | this doesn't work on every platform. It will work on any Unix-based OS | |
| 843 | that provides a real C<fork> function (e.g. macOS or Linux), as well as | |
| 844 | on Windows when running Perl 5.22 or later. | |
| 845 | ||
| 846 | =end original | |
| 847 | ||
| 848 | また、このようなリスト形式でのパイプコマンド引数表現は、全ての | |
| 849 | プラットフォームで動作するわけではないことに注意してください。 | |
| 850 | 真の C<fork> 関数を提供する Unix ベースの OS (例えば macOS や Linux)、 | |
| 851 | および Perl 5.22 以降の Windows では動作します。 | |
| 852 | ||
| 853 | 585 | =head1 SEE ALSO |
| 854 | 586 | |
| 855 | 587 | =begin original |
| 856 | 588 | |
| 857 | T | |
| 589 | To be announced. | |
| 858 | provides a thorough reference to this function, beyond the best-practice | |
| 859 | basics covered here. | |
| 860 | 590 | |
| 861 | 591 | =end original |
| 862 | 592 | |
| 863 | ||
| 593 | 未定。 | |
| 864 | ここでカバーしているベストプラクティスベースのものを超えて、 | |
| 865 | この関数の完全なリファレンスを提供します。 | |
| 866 | 594 | |
| 867 | 595 | =head1 AUTHOR and COPYRIGHT |
| 868 | 596 | |
| 869 | Copyright 2013 Tom Christiansen | |
| 597 | Copyright 2013 Tom Christiansen. | |
| 870 | 598 | |
| 871 | 599 | This documentation is free; you can redistribute it and/or modify it under |
| 872 | 600 | the same terms as Perl itself. |
| 873 | 601 | |
| 874 | 602 | =begin meta |
| 875 | 603 | |
| 876 | 604 | Translate: SHIRAKTA Kentaro <argrath@ub32.org> |
| 877 | 605 | Status: completed |
| 878 | 606 | |
| 879 | 607 | =end meta |