perlopentut >
5.12.1
との差分
perlopentut 5.12.1 と 5.36.0 の差分
| 1 | 1 | |
| 2 | =encoding | |
| 2 | =encoding utf8 | |
| 3 | 3 | |
| 4 | 4 | =head1 NAME |
| 5 | 5 | |
| 6 | 6 | =begin original |
| 7 | 7 | |
| 8 | perlopentut - | |
| 8 | perlopentut - simple recipes for opening files and pipes in Perl | |
| 9 | 9 | |
| 10 | 10 | =end original |
| 11 | 11 | |
| 12 | perlopentut - Perl で | |
| 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 | c | |
| 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 | of the C<close> function to break that association. | |
| 22 | 23 | |
| 23 | 24 | =end original |
| 24 | 25 | |
| 25 | Perl | |
| 26 | Perl でファイルに対して入出力をするとき、Perl では B<ファイルハンドル> と | |
| 26 | ||
| 27 | 呼ばれるものを通して行います。 | |
| 27 | ||
| 28 | ファイルハンドルは外部ファイルに対する内部名です。 | |
| 28 | ||
| 29 | C<open> 関数の仕事は内部名と外部名を関連づけることで、C<close> 関数は | |
| 29 | ||
| 30 | 関連づけを壊すことです。 | |
| 30 | 31 | |
| 31 | =head1 Open E<agrave> la shell | |
| 32 | ||
| 33 | (シェル風に開く) | |
| 34 | ||
| 35 | 32 | =begin original |
| 36 | 33 | |
| 37 | ||
| 34 | For your convenience, Perl sets up a few special filehandles that are | |
| 38 | red | |
| 35 | already open when you run. These include C<STDIN>, C<STDOUT>, C<STDERR>, | |
| 39 | ||
| 36 | and C<ARGV>. Since those are pre-opened, you can use them right away | |
| 37 | without having to go to the trouble of opening them yourself: | |
| 40 | 38 | |
| 41 | 39 | =end original |
| 42 | 40 | |
| 43 | Perl | |
| 41 | 便利なように、Perl は実行開始時に既に開いているいくつかの特別な | |
| 44 | 設 | |
| 42 | ファイルハンドルを設定します。 | |
| 45 | ||
| 43 | それは C<STDIN>, C<STDOUT>, C<STDERR>, C<ARGV> です。 | |
| 44 | これらは既に開いているので、自分でこれらを開くときの問題を受けることなく | |
| 45 | 正しく使うことができます。 | |
| 46 | 46 | |
| 47 | | |
| 47 | print STDERR "This is a debugging message.\n"; | |
| 48 | $ myprogram < inputfile | |
| 49 | $ myprogram > outputfile | |
| 50 | $ myprogram >> outputfile | |
| 51 | $ myprogram | otherprogram | |
| 52 | $ otherprogram | myprogram | |
| 53 | 48 | |
| 54 | ||
| 49 | print STDOUT "Please enter something: "; | |
| 50 | $response = <STDIN> // die "how come no input?"; | |
| 51 | print STDOUT "Thank you!\n"; | |
| 55 | 52 | |
| 56 | ||
| 53 | while (<ARGV>) { ... } | |
| 57 | 54 | |
| 58 | =end original | |
| 59 | ||
| 60 | そして以下はもう少し高度な例です: | |
| 61 | ||
| 62 | $ otherprogram | myprogram f1 - f2 | |
| 63 | $ otherprogram 2>&1 | myprogram - | |
| 64 | $ myprogram <&3 | |
| 65 | $ myprogram >&4 | |
| 66 | ||
| 67 | 55 | =begin original |
| 68 | 56 | |
| 69 | ||
| 57 | As you see from those examples, C<STDOUT> and C<STDERR> are output | |
| 70 | ||
| 58 | handles, and C<STDIN> and C<ARGV> are input handles. They are | |
| 71 | ||
| 59 | in all capital letters because they are reserved to Perl, much | |
| 60 | like the C<@ARGV> array and the C<%ENV> hash are. Their external | |
| 61 | associations were set up by your shell. | |
| 72 | 62 | |
| 73 | 63 | =end original |
| 74 | 64 | |
| 75 | ||
| 65 | これらの例で見られるように、C<STDOUT> と C<STDERR> は出力ハンドルで、 | |
| 76 | ||
| 66 | C<STDIN> と C<ARGV> は入力ハンドルです。 | |
| 77 | ||
| 67 | これらは C<@ARGV> 配列や C<%ENV> ハッシュと同様に Perl によって | |
| 68 | 予約されているので、全て大文字になっています。 | |
| 69 | これらの外部関連づけはシェルによって行われます。 | |
| 78 | 70 | |
| 79 | =head2 Simple Opens | |
| 80 | ||
| 81 | (単純に開く) | |
| 82 | ||
| 83 | 71 | =begin original |
| 84 | 72 | |
| 85 | ||
| 73 | You will need to open every other filehandle on your own. Although there | |
| 86 | a | |
| 74 | are many variants, the most common way to call Perl's open() function | |
| 87 | ||
| 75 | is with three arguments and one return value: | |
| 88 | returns a false value and sets the special variable C<$!> to reflect | |
| 89 | the system error. If the filehandle was previously opened, it will | |
| 90 | be implicitly closed first. | |
| 91 | 76 | |
| 92 | 77 | =end original |
| 93 | 78 | |
| 94 | ||
| 79 | その他のファイルハンドルは自分で開く必要があります。 | |
| 95 | ||
| 80 | 多くのバリエーションはありますが、Perl の open() 関数を開く最も一般的な方法は | |
| 96 | ||
| 81 | 3 引数と一つの返り値のものです: | |
| 97 | システムエラーを反映します。 | |
| 98 | 指定されたファイルハンドルが以前に開かれていた場合は、暗黙の内に | |
| 99 | まず閉じられます。 | |
| 100 | 82 | |
| 101 | 83 | =begin original |
| 102 | 84 | |
| 103 | ||
| 85 | C< I<OK> = open(I<HANDLE>, I<MODE>, I<PATHNAME>)> | |
| 104 | 86 | |
| 105 | 87 | =end original |
| 106 | 88 | |
| 107 | ||
| 89 | C< I<OK> = open(I<HANDLE>, I<MODE>, I<PATHNAME>)> | |
| 108 | 90 | |
| 109 | open(INFO, "datafile") || die("can't open datafile: $!"); | |
| 110 | open(INFO, "< datafile") || die("can't open datafile: $!"); | |
| 111 | open(RESULTS,"> runstats") || die("can't open runstats: $!"); | |
| 112 | open(LOG, ">> logfile ") || die("can't open logfile: $!"); | |
| 113 | ||
| 114 | 91 | =begin original |
| 115 | 92 | |
| 116 | ||
| 93 | Where: | |
| 117 | 94 | |
| 118 | 95 | =end original |
| 119 | 96 | |
| 120 | ||
| 97 | ここで: | |
| 121 | 98 | |
| 122 | ||
| 99 | =over | |
| 123 | open RESULTS,"> runstats" or die "can't open runstats: $!"; | |
| 124 | open LOG, ">> logfile " or die "can't open logfile: $!"; | |
| 125 | 100 | |
| 126 | = | |
| 101 | =item I<OK> | |
| 127 | 102 | |
| 128 | A few things to notice. First, the leading less-than is optional. | |
| 129 | If omitted, Perl assumes that you want to open the file for reading. | |
| 130 | ||
| 131 | =end original | |
| 132 | ||
| 133 | いくつか気がつくことがあります。 | |
| 134 | まず、先頭の「大なり」は省略可能です。 | |
| 135 | 省略されると、Perl はファイルを読み込みのために開きたいと仮定します。 | |
| 136 | ||
| 137 | 103 | =begin original |
| 138 | 104 | |
| 139 | ||
| 105 | will be some defined value if the open succeeds, but | |
| 140 | ||
| 106 | C<undef> if it fails; | |
| 141 | examples would effectively mean | |
| 142 | 107 | |
| 143 | 108 | =end original |
| 144 | 109 | |
| 145 | ||
| 110 | これは、開くのに成功すれば何らかの定義された値、失敗すれば C<undef> です; | |
| 146 | C<or> を使っていることにも注意してください。 | |
| 147 | 後者の例で C<||> を使うと、実際には以下のような意味になり | |
| 148 | 111 | |
| 149 | ||
| 112 | =item I<HANDLE> | |
| 150 | 113 | |
| 151 | 114 | =begin original |
| 152 | 115 | |
| 153 | ||
| 116 | should be an undefined scalar variable to be filled in by the | |
| 117 | C<open> function if it succeeds; | |
| 154 | 118 | |
| 155 | 119 | =end original |
| 156 | 120 | |
| 157 | ||
| 121 | これは、成功すれば C<open> 関数によって埋められる未定義のスカラ変数です; | |
| 158 | 122 | |
| 159 | = | |
| 123 | =item I<MODE> | |
| 160 | 124 | |
| 161 | The other important thing to notice is that, just as in the shell, | |
| 162 | any whitespace before or after the filename is ignored. This is good, | |
| 163 | because you wouldn't want these to do different things: | |
| 164 | ||
| 165 | =end original | |
| 166 | ||
| 167 | 他の注意するべき重要なこととしては、シェルと同様、ファイル名の前後の | |
| 168 | 空白は無視されることです。 | |
| 169 | これはよいことです; なぜなら、以下のものが違うことをすることは | |
| 170 | 望まないだろうからです: | |
| 171 | ||
| 172 | open INFO, "<datafile" | |
| 173 | open INFO, "< datafile" | |
| 174 | open INFO, "< datafile" | |
| 175 | ||
| 176 | 125 | =begin original |
| 177 | 126 | |
| 178 | ||
| 127 | is the access mode and the encoding format to open the file with; | |
| 179 | in from a different file, and forget to trim it before opening: | |
| 180 | 128 | |
| 181 | 129 | =end original |
| 182 | 130 | |
| 183 | ||
| 131 | これはファイルを開くときのアクセスモードとエンコーディング型式です; | |
| 184 | 開く前に空白を取り除くのを忘れたときにも助けになります: | |
| 185 | 132 | |
| 186 | ||
| 133 | =item I<PATHNAME> | |
| 187 | open(EXTRA, "< $filename") || die "can't open $filename: $!"; | |
| 188 | 134 | |
| 189 | 135 | =begin original |
| 190 | 136 | |
| 191 | ||
| 137 | is the external name of the file you want opened. | |
| 192 | its style of using redirection arrows to specify how to open the file, it | |
| 193 | also does so with respect to extra whitespace around the filename itself | |
| 194 | as well. For accessing files with naughty names, see | |
| 195 | L<"Dispelling the Dweomer">. | |
| 196 | 138 | |
| 197 | 139 | =end original |
| 198 | 140 | |
| 199 | これは | |
| 141 | これは開きたいファイルの外部名です。 | |
| 200 | C<open> はどのようにファイルを開くかを指定するのにリダイレクトの矢印を | |
| 201 | 使うことでシェルを真似ているので、ファイル名の周りの空白についても | |
| 202 | 同じように扱います。 | |
| 203 | 行儀の悪い名前のファイルにアクセスするためには、 | |
| 204 | L<"Dispelling the Dweomer"> を参照してください。 | |
| 205 | 142 | |
| 206 | =b | |
| 143 | =back | |
| 207 | 144 | |
| 208 | There is also a 3-argument version of C<open>, which lets you put the | |
| 209 | special redirection characters into their own argument: | |
| 210 | ||
| 211 | =end original | |
| 212 | ||
| 213 | また、3 引数版の C<open> もあって、これは特殊なリダイレクト文字を | |
| 214 | 独立した引数にしたものです: | |
| 215 | ||
| 216 | open( INFO, ">", $datafile ) || die "Can't create $datafile: $!"; | |
| 217 | ||
| 218 | 145 | =begin original |
| 219 | 146 | |
| 220 | ||
| 147 | Most of the complexity of the C<open> function lies in the many | |
| 221 | ||
| 148 | possible values that the I<MODE> parameter can take on. | |
| 222 | that might influence the open mode, or whitespace at the beginning of | |
| 223 | the filename that would be absorbed in the 2-argument version. Also, | |
| 224 | any reduction of unnecessary string interpolation is a good thing. | |
| 225 | 149 | |
| 226 | 150 | =end original |
| 227 | 151 | |
| 228 | ||
| 152 | C<open> 関数の複雑さの大部分は、I<MODE> 引数が多くの値を | |
| 229 | ||
| 153 | 取ることのできることにあります。 | |
| 230 | 2 引数版では吸収されるファイル名の先頭の空白が含まれているかどうかを | |
| 231 | 心配する必要はありません。 | |
| 232 | また、不必要な文字列変換が削減されるのもよいことです。 | |
| 233 | 154 | |
| 234 | =head2 Indirect Filehandles | |
| 235 | ||
| 236 | (間接ファイルハンドル) | |
| 237 | ||
| 238 | 155 | =begin original |
| 239 | 156 | |
| 240 | ||
| 157 | One last thing before we show you how to open files: opening | |
| 241 | ||
| 158 | files does not (usually) automatically lock them in Perl. See | |
| 242 | ||
| 159 | L<perlfaq5> for how to lock. | |
| 243 | like so: | |
| 244 | 160 | |
| 245 | 161 | =end original |
| 246 | 162 | |
| 247 | ||
| 163 | ファイルの開き方を説明する前に最後に一言: Perl ではファイルを開いても | |
| 248 | ||
| 164 | (普通は)自動的にロックすることはしません。 | |
| 249 | ||
| 165 | ロックの方法については L<perlfaq5> を参照してください。 | |
| 250 | 最初の引数に設定します: | |
| 251 | 166 | |
| 252 | | |
| 167 | =head1 Opening Text Files | |
| 253 | while ( <$in> ) { | |
| 254 | # do something with $_ | |
| 255 | } | |
| 256 | close $in; | |
| 257 | 168 | |
| 258 | ||
| 169 | (テキストファイルを開く) | |
| 259 | 170 | |
| 260 | ||
| 171 | =head2 Opening Text Files for Reading | |
| 261 | are global to the current package, two subroutines trying to open | |
| 262 | C<INFILE> will clash. With two functions opening indirect filehandles | |
| 263 | like C<my $infile>, there's no clash and no need to worry about future | |
| 264 | conflicts. | |
| 265 | 172 | |
| 266 | ||
| 173 | (読み込み用にテキストファイルを開く) | |
| 267 | 174 | |
| 268 | 間接ファイルハンドルは、名前空間管理をより容易にします。 | |
| 269 | ファイルハンドルは現在のパッケージに対してグローバルなので、 | |
| 270 | 二つのサブルーチンが C<INFILE> を開こうとすると衝突します。 | |
| 271 | 二つの関数が C<my $infil> のように間接ファイルハンドルで開いていると、 | |
| 272 | 衝突は発生せず、将来の衝突を気にする必要もありません。 | |
| 273 | ||
| 274 | 175 | =begin original |
| 275 | 176 | |
| 276 | ||
| 177 | If you want to read from a text file, first open it in | |
| 277 | ||
| 178 | read-only mode like this: | |
| 278 | 179 | |
| 279 | 180 | =end original |
| 280 | 181 | |
| 281 | ||
| 182 | テキストファイルを読み込みたい場合、まず次のように読み込み専用モードで | |
| 282 | ||
| 183 | 開きます: | |
| 283 | 184 | |
| 284 | | |
| 185 | my $filename = "/some/path/to/a/textfile/goes/here"; | |
| 285 | ||
| 186 | my $encoding = ":encoding(UTF-8)"; | |
| 286 | ||
| 187 | my $handle = undef; # this will be filled in on success | |
| 287 | } | |
| 288 | 188 | |
| 289 | ||
| 189 | open($handle, "< $encoding", $filename) | |
| 190 | || die "$0: can't open $filename for reading: $!"; | |
| 290 | 191 | |
| 291 | (パイプを開く) | |
| 292 | ||
| 293 | 192 | =begin original |
| 294 | 193 | |
| 295 | ||
| 194 | As with the shell, in Perl the C<< "<" >> is used to open the file in | |
| 296 | ||
| 195 | read-only mode. If it succeeds, Perl allocates a brand new filehandle for | |
| 297 | ||
| 196 | you and fills in your previously undefined C<$handle> argument with a | |
| 298 | ||
| 197 | reference to that handle. | |
| 299 | remains the same--just its argument differs. | |
| 300 | 198 | |
| 301 | 199 | =end original |
| 302 | 200 | |
| 303 | ||
| 201 | シェルと同様に、Perl でもファイルを読み込み専用モードで開くために | |
| 304 | ||
| 202 | C<< "<" >> が使われます。 | |
| 305 | ||
| 203 | これに成功すると、Perl は新しいファイルハンドルを割り当て、未定義だった | |
| 306 | ||
| 204 | C<$handle> 引数にそのハンドルへのリファレンスを設定します。 | |
| 307 | C<open> 呼び出しは同じままです -- 単にその引数が変わります。 | |
| 308 | 205 | |
| 309 | 206 | =begin original |
| 310 | 207 | |
| 311 | ||
| 208 | Now you may use functions like C<readline>, C<read>, C<getc>, and | |
| 312 | ||
| 209 | C<sysread> on that handle. Probably the most common input function | |
| 313 | ||
| 210 | is the one that looks like an operator: | |
| 314 | that command's standard input. For example: | |
| 315 | 211 | |
| 316 | 212 | =end original |
| 317 | 213 | |
| 318 | ||
| 214 | これでこのハンドルに対して C<readline>, C<read>, C<getc>, | |
| 319 | ||
| 215 | C<sysread> のような関数が使えます。 | |
| 320 | ||
| 216 | おそらく最も一般的な入力関数は演算子のように見えるものでしょう: | |
| 321 | 標準入力に渡されるようになります。 | |
| 322 | 例えば: | |
| 323 | 217 | |
| 324 | | |
| 218 | $line = readline($handle); | |
| 325 | | |
| 219 | $line = <$handle>; # same thing | |
| 326 | close(PRINTER) || die "can't close lpr: $!"; | |
| 327 | 220 | |
| 328 | 221 | =begin original |
| 329 | 222 | |
| 330 | ||
| 223 | Because the C<readline> function returns C<undef> at end of file or | |
| 331 | ||
| 224 | upon error, you will sometimes see it used this way: | |
| 332 | command writes to its standard output show up on your handle for reading. | |
| 333 | For example: | |
| 334 | 225 | |
| 335 | 226 | =end original |
| 336 | 227 | |
| 337 | ||
| 228 | C<readline> 関数はファイル終端やエラーのときに C<undef> を返すので、 | |
| 338 | ||
| 229 | 時々次のように使われているのを見るでしょう: | |
| 339 | これにより、そのコマンドが標準出力にしたものはなんでも読み込み用の | |
| 340 | ファイルハンドルに現れます。 | |
| 341 | 例えば: | |
| 342 | 230 | |
| 343 | | |
| 231 | $line = <$handle>; | |
| 344 | | |
| 232 | if (defined $line) { | |
| 345 | | |
| 233 | # do something with $line | |
| 234 | } | |
| 235 | else { | |
| 236 | # $line is not valid, so skip it | |
| 237 | } | |
| 346 | 238 | |
| 347 | 239 | =begin original |
| 348 | 240 | |
| 349 | ||
| 241 | You can also just quickly C<die> on an undefined value this way: | |
| 350 | command? If possible, Perl will detect the failure and set C<$!> as | |
| 351 | usual. But if the command contains special shell characters, such as | |
| 352 | C<E<gt>> or C<*>, called 'metacharacters', Perl does not execute the | |
| 353 | command directly. Instead, Perl runs the shell, which then tries to | |
| 354 | run the command. This means that it's the shell that gets the error | |
| 355 | indication. In such a case, the C<open> call will only indicate | |
| 356 | failure if Perl can't even run the shell. See L<perlfaq8/"How can I | |
| 357 | capture STDERR from an external command?"> to see how to cope with | |
| 358 | this. There's also an explanation in L<perlipc>. | |
| 359 | 242 | |
| 360 | 243 | =end original |
| 361 | 244 | |
| 362 | ||
| 245 | また、次のようにして単に未定義値に対してすばやく C<die> することもできます: | |
| 363 | 可能なら、Perl は失敗を検出していつも通り C<$!> をセットします。 | |
| 364 | しかし、もしコマンドに「メタ文字」と呼ばれる C<E<gt>> や C<*> のような | |
| 365 | 特殊シェル文字が含まれていると、Perl はコマンドを直接実行しません。 | |
| 366 | その代わりに、Perl はシェルを実行し、それからコマンドを | |
| 367 | 実行しようとします。 | |
| 368 | これは、エラーを受け取るのはシェルであることを意味します。 | |
| 369 | このような場合、C<open> 呼び出しは、たとえ Perl がシェルを実行できなかった | |
| 370 | 場合でも、失敗を示すだけです。 | |
| 371 | これを扱う方法については、 | |
| 372 | L<perlfaq8/"How can I capture STDERR from an external command?"> を | |
| 373 | 参照してください。 | |
| 374 | L<perlipc> にも説明があります。 | |
| 375 | 246 | |
| 376 | = | |
| 247 | $line = <$handle> // die "no input found"; | |
| 377 | 248 | |
| 378 | If you would like to open a bidirectional pipe, the IPC::Open2 | |
| 379 | library will handle this for you. Check out | |
| 380 | L<perlipc/"Bidirectional Communication with Another Process"> | |
| 381 | ||
| 382 | =end original | |
| 383 | ||
| 384 | 双方向パイプを開きたい場合は、IPC::Open2 ライブラリが使えます。 | |
| 385 | L<perlipc/"Bidirectional Communication with Another Process"> を | |
| 386 | 参照してください。 | |
| 387 | ||
| 388 | 249 | =begin original |
| 389 | 250 | |
| 390 | ||
| 251 | However, if hitting EOF is an expected and normal event, you do not want to | |
| 391 | ||
| 252 | exit simply because you have run out of input. Instead, you probably just want | |
| 392 | to | |
| 253 | to exit an input loop. You can then test to see if an actual error has caused | |
| 393 | ||
| 254 | the loop to terminate, and act accordingly: | |
| 394 | constructs. (It is also not supported on Microsoft Windows, Mac OS Classic | |
| 395 | or RISC OS.) | |
| 396 | 255 | |
| 397 | 256 | =end original |
| 398 | 257 | |
| 399 | ||
| 258 | しかし、EOF に到達するのが想定されていて通常の出来事の場合は、 | |
| 400 | ||
| 259 | 入力がなくなっただけで終了したくありません。 | |
| 401 | ||
| 260 | そうではなく、単に入力ループを終了したいでしょう。 | |
| 402 | ||
| 261 | 実際のエラーがループを終了させたのかをテストして、適切に行動できます: | |
| 403 | 構文は使えません。 | |
| 404 | (また、Microsoft Windows, Mac OS Classic, RISC OS でも対応していません。) | |
| 405 | 262 | |
| 406 | ||
| 263 | while (<$handle>) { | |
| 264 | # do something with data in $_ | |
| 408 | Here's an example of C<open '-|'>, which prints a random Unix | |
| 409 | fortune cookie as uppercase: | |
| 410 | ||
| 411 | =end original | |
| 412 | ||
| 413 | 以下は C<open '-|'> の例で、ランダムな Unix おみくじを大文字で表示します: | |
| 414 | ||
| 415 | my $collection = shift(@ARGV); | |
| 416 | open my $fortune, '-|', 'fortune', $collection | |
| 417 | or die "Could not find fortune - $!"; | |
| 418 | while (<$fortune>) | |
| 419 | { | |
| 420 | print uc($_); | |
| 421 | 265 | } |
| 422 | | |
| 266 | if ($!) { | |
| 267 | die "unexpected error while reading from $filename: $!"; | |
| 268 | } | |
| 423 | 269 | |
| 424 | 270 | =begin original |
| 425 | 271 | |
| 426 | A | |
| 272 | B<A Note on Encodings>: Having to specify the text encoding every time | |
| 273 | might seem a bit of a bother. To set up a default encoding for C<open> so | |
| 274 | that you don't have to supply it each time, you can use the C<open> pragma: | |
| 427 | 275 | |
| 428 | 276 | =end original |
| 429 | 277 | |
| 430 | ||
| 278 | B<エンコーディングに関する注意>: テキストエンコーディングを毎回指定する | |
| 279 | 必要があるのは少し面倒に感じるかもしれません。 | |
| 280 | 毎回設定する必要がないように C<open> のためのデフォルトエンコーディングを | |
| 281 | 設定するために、C<open> プラグマを使えます: | |
| 431 | 282 | |
| 432 | open | |
| 283 | use open qw< :encoding(UTF-8) >; | |
| 433 | or die "can't run lpr: $!"; | |
| 434 | print {$printer} "stuff\n"; | |
| 435 | close($printer) | |
| 436 | or die "can't close lpr: $!"; | |
| 437 | 284 | |
| 438 | =head2 The Minus File | |
| 439 | ||
| 440 | ("-" ファイル) | |
| 441 | ||
| 442 | 285 | =begin original |
| 443 | 286 | |
| 444 | ||
| 287 | Once you've done that, you can safely omit the encoding part of the | |
| 445 | ||
| 288 | open mode: | |
| 446 | special way. If you open minus for reading, it really means to access | |
| 447 | the standard input. If you open minus for writing, it really means to | |
| 448 | access the standard output. | |
| 449 | 289 | |
| 450 | 290 | =end original |
| 451 | 291 | |
| 452 | ||
| 292 | 一度これを行えば、open モードからエンコーディングの部分を安全に省略できます: | |
| 453 | C<open> 関数は、名前がマイナス一つ "-" だけのファイルを特別に扱います。 | |
| 454 | 読み込み用にマイナスを開くと、実際には標準入力にアクセスします。 | |
| 455 | 書き込み用にマイナスを開くと、実際には標準出力にアクセスします。 | |
| 456 | 293 | |
| 457 | ||
| 294 | open($handle, "<", $filename) | |
| 295 | || die "$0: can't open $filename for reading: $!"; | |
| 458 | 296 | |
| 459 | If minus can be used as the default input or default output, what happens | |
| 460 | if you open a pipe into or out of minus? What's the default command it | |
| 461 | would run? The same script as you're currently running! This is actually | |
| 462 | a stealth C<fork> hidden inside an C<open> call. See | |
| 463 | L<perlipc/"Safe Pipe Opens"> for details. | |
| 464 | ||
| 465 | =end original | |
| 466 | ||
| 467 | マイナスがデフォルトの入力やデフォルトの出力として使えるとすると、 | |
| 468 | パイプに対してマイナスを使うとどうなるでしょう? | |
| 469 | デフォルトのコマンドとして何が実行されるのでしょう? | |
| 470 | 今実行している同じスクリプトです! | |
| 471 | これは実際には C<open> 呼び出し内で隠れた C<fork> が行われます。 | |
| 472 | 詳しくは L<perlipc/"Safe Pipe Opens"> を参照してください。 | |
| 473 | ||
| 474 | =head2 Mixing Reads and Writes | |
| 475 | ||
| 476 | (読み書きを混ぜる) | |
| 477 | ||
| 478 | 297 | =begin original |
| 479 | 298 | |
| 480 | ||
| 299 | But never use the bare C<< "<" >> without having set up a default encoding | |
| 481 | ||
| 300 | first. Otherwise, Perl cannot know which of the many, many, many possible | |
| 482 | ||
| 301 | flavors of text file you have, and Perl will have no idea how to correctly | |
| 483 | e | |
| 302 | map the data in your file into actual characters it can work with. Other | |
| 484 | ||
| 303 | common encoding formats including C<"ASCII">, C<"ISO-8859-1">, | |
| 485 | ||
| 304 | C<"ISO-8859-15">, C<"Windows-1252">, C<"MacRoman">, and even C<"UTF-16LE">. | |
| 486 | ||
| 305 | See L<perlunitut> for more about encodings. | |
| 487 | 306 | |
| 488 | 307 | =end original |
| 489 | 308 | |
| 490 | ||
| 309 | しかし、先にデフォルトのエンコーディングを設定することなく裸の | |
| 491 | ||
| 310 | C<< "<" >> を使うことは決してしないでください。 | |
| 492 | ||
| 311 | さもなければ、Perl はとてもとてもとてもたくさんあるテキストファイルの | |
| 493 | ||
| 312 | 種類のうちどれかを知ることができず、Perl はあなたのファイルのデータを | |
| 494 | ||
| 313 | 動作させるための実際の文字にマッピングすることができません。 | |
| 495 | ||
| 314 | その他のよくあるエンコーディング形式には | |
| 496 | ||
| 315 | C<"ASCII">, C<"ISO-8859-1">, | |
| 497 | ||
| 316 | C<"ISO-8859-15">, C<"Windows-1252">, C<"MacRoman"> および、 | |
| 317 | C<"UTF-16LE"> すらもあります。 | |
| 318 | エンコーディングに関するさらなる情報については L<perlunitut> を | |
| 319 | 参照してください。 | |
| 498 | 320 | |
| 499 | | |
| 321 | =head2 Opening Text Files for Writing | |
| 500 | || die "can't open /usr/adm/wtmp: $!"; | |
| 501 | 322 | |
| 502 | ||
| 323 | (書き込み用にテキストファイルを開く) | |
| 503 | || die "can't open lkscreen: $!"; | |
| 504 | 324 | |
| 505 | open(LOGFILE, "+>> /var/log/applog") | |
| 506 | || die "can't open /var/log/applog: $!"; | |
| 507 | ||
| 508 | 325 | =begin original |
| 509 | 326 | |
| 510 | ||
| 327 | When you want to write to a file, you first have to decide what to do about | |
| 511 | ||
| 328 | any existing contents of that file. You have two basic choices here: to | |
| 512 | ||
| 329 | preserve or to clobber. | |
| 513 | in the file, but all writes will always go to the end. In short, | |
| 514 | the first case is substantially more common than the second and third | |
| 515 | cases, which are almost always wrong. (If you know C, the plus in | |
| 516 | Perl's C<open> is historically derived from the one in C's fopen(3S), | |
| 517 | which it ultimately calls.) | |
| 518 | 330 | |
| 519 | 331 | =end original |
| 520 | 332 | |
| 521 | ||
| 333 | ファイルに書き込みたい場合、そのファイルの既存の内容をどうするかを | |
| 522 | ||
| 334 | まず決定する必要があります。 | |
| 523 | ||
| 335 | 二つの基本的な選択肢があります: 保存するか上書きするかです。 | |
| 524 | 上書きせず、ファイルのどの地点でも読み込むことができますが、 | |
| 525 | 書き込みは常に末尾に行われます。 | |
| 526 | 要するに、一つ目のものは(ほとんど常に間違っている)二つ目や三つ目の | |
| 527 | ものよりもかなり一般的です。 | |
| 528 | (もし C を知っているなら、Perl の C<open> で使われるプラス記号が | |
| 529 | 歴史的には (最終的に呼ばれることになる) C の fopen(3S) に由来しています。) | |
| 530 | 336 | |
| 531 | 337 | =begin original |
| 532 | 338 | |
| 533 | I | |
| 339 | If you want to preserve any existing contents, then you want to open the file | |
| 534 | ||
| 340 | in append mode. As in the shell, in Perl you use C<<< ">>" >>> to open an | |
| 535 | ||
| 341 | existing file in append mode. C<<< ">>" >>> creates the file if it does not | |
| 536 | ||
| 342 | already exist. | |
| 537 | or header files and changes all their foo's to bar's, leaving | |
| 538 | the old version in the original filename with a ".orig" tacked | |
| 539 | on the end: | |
| 540 | 343 | |
| 541 | 344 | =end original |
| 542 | 345 | |
| 543 | ||
| 346 | 既存の内容を保存したい場合、ファイルを追記モードで開きます。 | |
| 544 | ||
| 347 | シェルと同様に、 Perl でも既存のファイルを追記モードで開くために | |
| 545 | 使 | |
| 348 | C<<< ">>" >>> が使われます。 | |
| 546 | ||
| 349 | ファイルがない場合、C<<< ">>" >>> はファイルを作ります。 | |
| 547 | 以下のコマンドは C, C++, yacc 全てののソースファイルとヘッダファイルを | |
| 548 | 取って、その中の全ての foo を bar に変更し、原版は元のファイル名の末尾に | |
| 549 | ".orig" を付けたファイルに保持します: | |
| 550 | 350 | |
| 551 | | |
| 351 | my $handle = undef; | |
| 352 | my $filename = "/some/path/to/a/textfile/goes/here"; | |
| 353 | my $encoding = ":encoding(UTF-8)"; | |
| 552 | 354 | |
| 553 | ||
| 355 | open($handle, ">> $encoding", $filename) | |
| 356 | || die "$0: can't open $filename for appending: $!"; | |
| 554 | 357 | |
| 555 | This is a short cut for some renaming games that are really | |
| 556 | the best way to update textfiles. See the second question in | |
| 557 | L<perlfaq5> for more details. | |
| 558 | ||
| 559 | =end original | |
| 560 | ||
| 561 | これは実際にはテキストファイルを更新するための最良の方法であるリネーム | |
| 562 | 手法へのショートカットです。 | |
| 563 | さらなる詳細については L<perlfaq5> の 2 番目の質問を参照してください。 | |
| 564 | ||
| 565 | =head2 Filters | |
| 566 | ||
| 567 | (フィルタ) | |
| 568 | ||
| 569 | 358 | =begin original |
| 570 | 359 | |
| 571 | ||
| 360 | Now you can write to that filehandle using any of C<print>, C<printf>, | |
| 572 | ||
| 361 | C<say>, C<write>, or C<syswrite>. | |
| 573 | C<< <ARGV> >>, Perl actually does an implicit open | |
| 574 | on each file in @ARGV. Thus a program called like this: | |
| 575 | 362 | |
| 576 | 363 | =end original |
| 577 | 364 | |
| 578 | C< | |
| 365 | これでこのハンドルに対して C<print>, C<printf>, | |
| 579 | ||
| 366 | C<say>, C<write>, C<syswrite> を使って書き込めます。 | |
| 580 | ARGV ファイルハンドルを C<< <ARGV> >> を使って処理するとき、Perl は | |
| 581 | 実際は @ARGV の各ファイルを暗黙の内に開いています。 | |
| 582 | 従って、以下のようなプログラムは: | |
| 583 | 367 | |
| 584 | $ myprogram file1 file2 file3 | |
| 585 | ||
| 586 | 368 | =begin original |
| 587 | 369 | |
| 588 | ||
| 370 | As noted above, if the file does not already exist, then the append-mode open | |
| 589 | ||
| 371 | will create it for you. But if the file does already exist, its contents are | |
| 372 | safe from harm because you will be adding your new text past the end of the | |
| 373 | old text. | |
| 590 | 374 | |
| 591 | 375 | =end original |
| 592 | 376 | |
| 593 | ||
| 377 | 前述したように、ファイルが既に存在していない場合、追記モードで開くと | |
| 594 | ||
| 378 | ファイルを作ります。 | |
| 379 | しかしファイルが既に存在している場合、その内容は保護されます; 新しいテキストは | |
| 380 | 既存のテキストの末尾に追加されるからです。 | |
| 595 | 381 | |
| 596 | while (<>) { | |
| 597 | # do something with $_ | |
| 598 | } | |
| 599 | ||
| 600 | 382 | =begin original |
| 601 | 383 | |
| 602 | ||
| 384 | On the other hand, sometimes you want to clobber whatever might already be | |
| 603 | ||
| 385 | there. To empty out a file before you start writing to it, you can open it | |
| 604 | ||
| 386 | in write-only mode: | |
| 605 | in these circumstances. | |
| 606 | 387 | |
| 607 | 388 | =end original |
| 608 | 389 | |
| 609 | ||
| 390 | 一方、時々、既に何かがあっても上書きしたいときもあります。 | |
| 610 | ||
| 391 | 書き込みを始める前にファイルを消すために、書き込み専用モードで | |
| 611 | ||
| 392 | 開くことができます: | |
| 612 | この慣習によって "-" がセットされます。 | |
| 613 | 393 | |
| 614 | ||
| 394 | my $handle = undef; | |
| 395 | my $filename = "/some/path/to/a/textfile/goes/here"; | |
| 396 | my $encoding = ":encoding(UTF-8)"; | |
| 615 | 397 | |
| 616 | ||
| 398 | open($handle, "> $encoding", $filename) | |
| 617 | ||
| 399 | || die "$0: can't open $filename in write-open mode: $!"; | |
| 618 | command options beginning with a minus. While you can always roll the | |
| 619 | simple ones by hand, the Getopts modules are good for this: | |
| 620 | 400 | |
| 621 | =end original | |
| 622 | ||
| 623 | 好みの形にするために、ループの開始前に @ARGV を前処理しても問題ありません。 | |
| 624 | こうするための理由の一つは、マイナスから始まるコマンドオプションを | |
| 625 | 削除するためです。 | |
| 626 | いつでも自分で単純なものを作ることができる一方、 | |
| 627 | Getopts モジュールはこれを行うのによいものです: | |
| 628 | ||
| 629 | use Getopt::Std; | |
| 630 | ||
| 631 | # -v, -D, -o ARG, sets $opt_v, $opt_D, $opt_o | |
| 632 | getopts("vDo:"); | |
| 633 | ||
| 634 | # -v, -D, -o ARG, sets $args{v}, $args{D}, $args{o} | |
| 635 | getopts("vDo:", \%args); | |
| 636 | ||
| 637 | 401 | =begin original |
| 638 | 402 | |
| 639 | ||
| 403 | Here again Perl works just like the shell in that the C<< ">" >> clobbers | |
| 404 | an existing file. | |
| 640 | 405 | |
| 641 | 406 | =end original |
| 642 | 407 | |
| 643 | ||
| 408 | ここで再び Perl はシェルと同様に動作し、C<< ">" >> は既存のファイルを | |
| 644 | ||
| 409 | 上書きします。 | |
| 645 | 410 | |
| 646 | use Getopt::Long; | |
| 647 | GetOptions( "verbose" => \$verbose, # --verbose | |
| 648 | "Debug" => \$debug, # --Debug | |
| 649 | "output=s" => \$output ); | |
| 650 | # --output=somestring or --output somestring | |
| 651 | ||
| 652 | 411 | =begin original |
| 653 | 412 | |
| 654 | A | |
| 413 | As with the append mode, when you open a file in write-only mode, | |
| 655 | ||
| 414 | you can now write to that filehandle using any of C<print>, C<printf>, | |
| 415 | C<say>, C<write>, or C<syswrite>. | |
| 656 | 416 | |
| 657 | 417 | =end original |
| 658 | 418 | |
| 659 | ||
| 419 | 追記モードと同様に、ファイルを書き込みモードで開くと、 | |
| 660 | ||
| 420 | C<print>, C<printf>, C<say>, C<write>, C<syswrite> を使って | |
| 421 | ファイルハンドルに書き込めるようになります。 | |
| 661 | 422 | |
| 662 | @ARGV = glob("*") unless @ARGV; | |
| 663 | ||
| 664 | 423 | =begin original |
| 665 | 424 | |
| 666 | ||
| 425 | What about read-write mode? You should probably pretend it doesn't exist, | |
| 667 | ||
| 426 | because opening text files in read-write mode is unlikely to do what you | |
| 427 | would like. See L<perlfaq5> for details. | |
| 668 | 428 | |
| 669 | 429 | =end original |
| 670 | 430 | |
| 671 | ||
| 431 | 読み書きモードについては? | |
| 672 | ||
| 432 | おそらくそれは存在しないというふりをした方がよいでしょう; | |
| 433 | なぜならテキストファイルを読み書きモードで開いても | |
| 434 | おそらくあなたが望んでいることをしないからです。 | |
| 435 | 詳しくは L<perlfaq5> を参照してください。 | |
| 673 | 436 | |
| 674 | ||
| 437 | =head1 Opening Binary Files | |
| 675 | 438 | |
| 676 | ||
| 439 | (バイナリファイルを開く) | |
| 677 | 440 | |
| 678 | If you're using the B<-n> or B<-p> command-line options, you | |
| 679 | should put changes to @ARGV in a C<BEGIN{}> block. | |
| 680 | ||
| 681 | =end original | |
| 682 | ||
| 683 | もし B<-n> や B<-p> のコマンドラインオプションを使っているなら、 | |
| 684 | @ARGV への変更は C<BEGIN{}> ブロックで行うべきです。 | |
| 685 | ||
| 686 | 441 | =begin original |
| 687 | 442 | |
| 688 | ||
| 443 | If the file to be opened contains binary data instead of text characters, | |
| 689 | ||
| 444 | then the C<MODE> argument to C<open> is a little different. Instead of | |
| 690 | ||
| 445 | specifying the encoding, you tell Perl that your data are in raw bytes. | |
| 691 | Here's an example: | |
| 692 | 446 | |
| 693 | 447 | =end original |
| 694 | 448 | |
| 695 | ||
| 449 | 開こうとしているファイルがテキスト文字ではなくバイナリデータが含まれている | |
| 696 | ||
| 450 | 場合、C<open> の C<MODE> 引数は少し異なるものになります。 | |
| 697 | ||
| 451 | エンコーディングを指定する代わりに、データが生のバイト列であることを | |
| 698 | ||
| 452 | Perl に知らせます。 | |
| 699 | 453 | |
| 700 | $ | |
| 454 | my $filename = "/some/path/to/a/binary/file/goes/here"; | |
| 701 | | |
| 455 | my $encoding = ":raw :bytes" | |
| 702 | | |
| 456 | my $handle = undef; # this will be filled in on success | |
| 703 | 457 | |
| 704 | open(PWD, $pwdinfo) | |
| 705 | or die "can't open $pwdinfo: $!"; | |
| 706 | ||
| 707 | 458 | =begin original |
| 708 | 459 | |
| 709 | ||
| 460 | And then open as before, choosing C<<< "<" >>>, C<<< ">>" >>>, or | |
| 710 | C<< | |
| 461 | C<<< ">" >>> as needed: | |
| 711 | it respects all the special things we've already seen: | |
| 712 | 462 | |
| 713 | 463 | =end original |
| 714 | 464 | |
| 715 | ||
| 465 | それから前述の通り、必要に応じて | |
| 716 | C<< < | |
| 466 | C<<< "<" >>>, C<<< ">>" >>>, C<<< ">" >>> を選びます: | |
| 717 | 今までに見てきた全ての特別なことが反映されます: | |
| 718 | 467 | |
| 719 | | |
| 468 | open($handle, "< $encoding", $filename) | |
| 469 | || die "$0: can't open $filename for reading: $!"; | |
| 720 | 470 | |
| 721 | ||
| 471 | open($handle, ">> $encoding", $filename) | |
| 472 | || die "$0: can't open $filename for appending: $!"; | |
| 722 | 473 | |
| 723 | ||
| 474 | open($handle, "> $encoding", $filename) | |
| 724 | in | |
| 475 | || die "$0: can't open $filename in write-open mode: $!"; | |
| 725 | and finally the F<f3> file. | |
| 726 | 476 | |
| 727 | =end original | |
| 728 | ||
| 729 | このプログラムはファイル F<f1>、プロセス F<cmd1>、標準入力 | |
| 730 | (この場合は F<tmpfile>)、ファイル F<f2>、コマンド F<cmd2>、 | |
| 731 | ファイル F<f3> から読み込みます。 | |
| 732 | ||
| 733 | 477 | =begin original |
| 734 | 478 | |
| 735 | ||
| 479 | Alternately, you can change to binary mode on an existing handle this way: | |
| 736 | your directory, they won't be processed as literal files by C<open>. | |
| 737 | You'll need to pass them as "./-", much as you would for the I<rm> program, | |
| 738 | or you could use C<sysopen> as described below. | |
| 739 | 480 | |
| 740 | 481 | =end original |
| 741 | 482 | |
| 742 | ||
| 483 | あるいは、次のようにして既に存在しているハンドルをバイナリモードに | |
| 743 | ||
| 484 | 変えることが出来ます: | |
| 744 | I<rm> プログラムに対して行うのと同様に "./-" という形で渡すか、後述する | |
| 745 | C<sysopen> を使う必要があります。 | |
| 746 | 485 | |
| 747 | ||
| 486 | binmode($handle) || die "cannot binmode handle"; | |
| 748 | 487 | |
| 749 | One of the more interesting applications is to change files of a certain | |
| 750 | name into pipes. For example, to autoprocess gzipped or compressed | |
| 751 | files by decompressing them with I<gzip>: | |
| 752 | ||
| 753 | =end original | |
| 754 | ||
| 755 | もっと興味深いアプリケーションの一つは、ある名前を持ったファイルを | |
| 756 | パイプに変更するものです。 | |
| 757 | 例えば、gzip や compress されたファイルを、I<gzip> を使って自動的に | |
| 758 | 展開するには: | |
| 759 | ||
| 760 | @ARGV = map { /\.(gz|Z)$/ ? "gzip -dc $_ |" : $_ } @ARGV; | |
| 761 | ||
| 762 | 488 | =begin original |
| 763 | 489 | |
| 764 | ||
| 490 | This is especially handy for the handles that Perl has already opened for you. | |
| 765 | you can fetch URLs before processing them: | |
| 766 | 491 | |
| 767 | 492 | =end original |
| 768 | 493 | |
| 769 | ||
| 494 | これは、Perl が既に開いているハンドルに対して特に有用です。 | |
| 770 | 処理する前に URL をフェッチできます: | |
| 771 | 495 | |
| 772 | | |
| 496 | binmode(STDIN) || die "cannot binmode STDIN"; | |
| 497 | binmode(STDOUT) || die "cannot binmode STDOUT"; | |
| 773 | 498 | |
| 774 | 499 | =begin original |
| 775 | 500 | |
| 776 | ||
| 501 | You can also pass C<binmode> an explicit encoding to change it on the fly. | |
| 777 | ||
| 502 | This isn't exactly "binary" mode, but we still use C<binmode> to do it: | |
| 778 | 503 | |
| 779 | 504 | =end original |
| 780 | 505 | |
| 781 | ||
| 506 | また、その場で変更するために C<binmode> に明示的にエンコーディングを | |
| 782 | ||
| 507 | 渡すこともできます。 | |
| 508 | これは正確には「バイナリ」モードではありませんが、それでも | |
| 509 | これをするために C<binmode> を使います: | |
| 783 | 510 | |
| 784 | ||
| 511 | binmode(STDIN, ":encoding(MacRoman)") || die "cannot binmode STDIN"; | |
| 512 | binmode(STDOUT, ":encoding(UTF-8)") || die "cannot binmode STDOUT"; | |
| 785 | 513 | |
| 786 | (C 風に開く) | |
| 787 | ||
| 788 | 514 | =begin original |
| 789 | 515 | |
| 790 | ||
| 516 | Once you have your binary file properly opened in the right mode, you can | |
| 791 | ||
| 517 | use all the same Perl I/O functions as you used on text files. However, | |
| 792 | ||
| 518 | you may wish to use the fixed-size C<read> instead of the variable-sized | |
| 793 | C< | |
| 519 | C<readline> for your input. | |
| 794 | That does mean it's a bit more involved, but that's the price of | |
| 795 | precision. | |
| 796 | 520 | |
| 797 | 521 | =end original |
| 798 | 522 | |
| 799 | ||
| 523 | 一旦バイナリファイルを正しいモードで適切に開くと、テキストファイルで | |
| 800 | ||
| 524 | 使ったものと全て同じ Perl I/O 関数を使えます。 | |
| 801 | ||
| 525 | しかし、入力に対して可変長の C<readline> ではなく固定長の | |
| 802 | C< | |
| 526 | C<read> を使った方が良いでしょう。 | |
| 803 | これはもう少し深く関わることを意味しますが、これは精度のコストです。 | |
| 804 | 527 | |
| 805 | 528 | =begin original |
| 806 | 529 | |
| 807 | ||
| 530 | Here's an example of how to copy a binary file: | |
| 808 | 531 | |
| 809 | 532 | =end original |
| 810 | 533 | |
| 811 | ||
| 534 | 次のものはバイナリファイルをコピーする例です: | |
| 812 | 535 | |
| 813 | | |
| 536 | my $BUFSIZ = 64 * (2 ** 10); | |
| 537 | my $name_in = "/some/input/file"; | |
| 538 | my $name_out = "/some/output/flie"; | |
| 814 | 539 | |
| 815 | ||
| 540 | my($in_fh, $out_fh, $buffer); | |
| 816 | 541 | |
| 817 | ||
| 542 | open($in_fh, "<", $name_in) | |
| 818 | ||
| 543 | || die "$0: cannot open $name_in for reading: $!"; | |
| 819 | ||
| 544 | open($out_fh, ">", $name_out) | |
| 820 | ||
| 545 | || die "$0: cannot open $name_out for writing: $!"; | |
| 821 | derived from the Fcntl module that have been or'd together using the | |
| 822 | bitwise "|" operator. The final argument, the MASK, is optional; if | |
| 823 | present, it is combined with the user's current umask for the creation | |
| 824 | mode of the file. You should usually omit this. | |
| 825 | 546 | |
| 826 | ||
| 547 | for my $fh ($in_fh, $out_fh) { | |
| 548 | binmode($fh) || die "binmode failed"; | |
| 549 | } | |
| 827 | 550 | |
| 828 | ||
| 551 | while (read($in_fh, $buffer, $BUFSIZ)) { | |
| 829 | ||
| 552 | unless (print $out_fh $buffer) { | |
| 830 | ||
| 553 | die "couldn't write to $name_out: $!"; | |
| 831 | ||
| 554 | } | |
| 832 | ||
| 555 | } | |
| 833 | 由来する一つ以上の値を指定します。 | |
| 834 | 最後の引数である MASK はオプションです; もしあれば、これは | |
| 835 | ファイルの作成モードのためのユーザーの現在の umask と組み合わされます。 | |
| 836 | 普通はこれは省略するべきです。 | |
| 837 | 556 | |
| 838 | ||
| 557 | close($in_fh) || die "couldn't close $name_in: $!"; | |
| 558 | close($out_fh) || die "couldn't close $name_out: $!"; | |
| 839 | 559 | |
| 840 | ||
| 560 | =head1 Opening Pipes | |
| 841 | are 0, 1, and 2 respectively, this is known not to hold true on some | |
| 842 | systems. Instead, it's best to load in the appropriate constants first | |
| 843 | from the Fcntl module, which supplies the following standard flags: | |
| 844 | 561 | |
| 845 | ||
| 562 | (パイプを開く) | |
| 846 | 563 | |
| 847 | 読み込み専用、書き込み専用、読み書きを示す伝統的な値は | |
| 848 | それぞれ 0, 1, 2 ですが、これが正しくないシステムもあることが | |
| 849 | 知られています。 | |
| 850 | 代わりに、以下の標準フラグを提供している Fcntl モジュールから | |
| 851 | 最初に適切な定数を読み込むのが最善です: | |
| 852 | ||
| 853 | O_RDONLY Read only | |
| 854 | O_WRONLY Write only | |
| 855 | O_RDWR Read and write | |
| 856 | O_CREAT Create the file if it doesn't exist | |
| 857 | O_EXCL Fail if the file already exists | |
| 858 | O_APPEND Append to the file | |
| 859 | O_TRUNC Truncate the file | |
| 860 | O_NONBLOCK Non-blocking access | |
| 861 | ||
| 862 | 564 | =begin original |
| 863 | 565 | |
| 864 | ||
| 566 | Perl also lets you open a filehandle into an external program or shell | |
| 865 | ||
| 567 | command rather than into a file. You can do this in order to pass data | |
| 866 | ||
| 568 | from your Perl program to an external command for further processing, or | |
| 867 | ||
| 569 | to receive data from another program for your own Perl program to | |
| 868 | ||
| 570 | process. | |
| 869 | Perl release 5.6 the C<O_LARGEFILE> flag, if available, is automatically | |
| 870 | added to the sysopen() flags because large files are the default.) | |
| 871 | 571 | |
| 872 | 572 | =end original |
| 873 | 573 | |
| 874 | ||
| 574 | Perl はまた、ファイルではなく外部プログラムやシェルコマンドへの | |
| 875 | ||
| 575 | ファイルハンドルも開きます。 | |
| 876 | ||
| 576 | これを、更なる処理のために Perl プログラムから外部コマンドへ渡すため、 | |
| 877 | ||
| 577 | または処理する Perl プログラムのために他のプログラムからデータを | |
| 878 | ||
| 578 | 受け取るために行えます。 | |
| 879 | 詳しくは open(2) man ページその等価物を参照してください。 | |
| 880 | (注意: Perl リリース 5.6 から、もし利用可能なら、sysopen() のフラグに | |
| 881 | 自動的に C<O_LARGEFILE> フラグが付きます; 大きなファイルがデフォルトに | |
| 882 | なったからです。) | |
| 883 | 579 | |
| 884 | 580 | =begin original |
| 885 | 581 | |
| 886 | ||
| 582 | Filehandles into commands are also known as I<pipes>, since they work on | |
| 887 | ||
| 583 | similar inter-process communication principles as Unix pipelines. Such a | |
| 888 | ||
| 584 | filehandle has an active program instead of a static file on its | |
| 889 | t | |
| 585 | external end, but in every other sense it works just like a more typical | |
| 890 | b | |
| 586 | file-based filehandle, with all the techniques discussed earlier in this | |
| 587 | article just as applicable. | |
| 891 | 588 | |
| 892 | 589 | =end original |
| 893 | 590 | |
| 894 | ||
| 591 | コマンドへのファイルハンドルは、I<パイプ> としても知られます; | |
| 895 | ||
| 592 | Unix パイプラインという似たようなプロセス間通信原則に基づいて | |
| 896 | ||
| 593 | 動作するからです。 | |
| 897 | ||
| 594 | そのようなファイルハンドルは、外側が静的なファイルではなく | |
| 898 | ||
| 595 | 動作中のプログラムですが、それ以外の点については | |
| 899 | ||
| 596 | より典型的なファイルベースのファイルハンドルとちょうど同じように | |
| 597 | 動作し、この文書で既に議論した全てのテクニックが利用可能です。 | |
| 900 | 598 | |
| 901 | 599 | =begin original |
| 902 | 600 | |
| 903 | ||
| 601 | As such, you open a pipe using the same C<open> call that you use for | |
| 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. | |
| 904 | 607 | |
| 905 | 608 | =end original |
| 906 | 609 | |
| 907 | ||
| 610 | 2 番目の (C<MODE>) 引数にパイプの入力または出力を示す特殊な文字を | |
| 611 | 設定することで、ファイルを開くのに使うのと同じ C<open> で | |
| 612 | パイプを開きます。 | |
| 613 | Perl プログラムが外部プログラムからデータを読み込むファイルハンドルには | |
| 614 | C<"-|"> を使います; プログラムにデータを送るファイルハンドルには | |
| 615 | C<"|-"> を使います。 | |
| 908 | 616 | |
| 909 | | |
| 617 | =head2 Opening a pipe for reading | |
| 910 | sysopen(FH, $path, O_RDONLY); | |
| 911 | 618 | |
| 912 | ||
| 619 | (読み込み用にパイプを開く) | |
| 913 | 620 | |
| 914 | To open a file for writing, creating a new file if needed or else truncating | |
| 915 | an old file: | |
| 916 | ||
| 917 | =end original | |
| 918 | ||
| 919 | ファイルを書き込み用に開いて、必要なら新しいファイルを作り、そうでなければ | |
| 920 | 古いファイルを切り詰めるには: | |
| 921 | ||
| 922 | open(FH, "> $path"); | |
| 923 | sysopen(FH, $path, O_WRONLY | O_TRUNC | O_CREAT); | |
| 924 | ||
| 925 | 621 | =begin original |
| 926 | 622 | |
| 927 | ||
| 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. | |
| 928 | 628 | |
| 929 | 629 | =end original |
| 930 | 630 | |
| 931 | ファイル | |
| 631 | たくさんのテキストファイルが含まれている、C<unsorted> と呼ばれる | |
| 632 | 近くのディレクトリに保管されているデータを処理する | |
| 633 | Perl プログラムが欲しいとしましょう。 | |
| 634 | また、処理を開始する前に、複数のファイルを単一の、ユニークな行を | |
| 635 | アルファベット順にソートしたいとします。 | |
| 932 | 636 | |
| 933 | open(FH, ">> $path"); | |
| 934 | sysopen(FH, $path, O_WRONLY | O_APPEND | O_CREAT); | |
| 935 | ||
| 936 | 637 | =begin original |
| 937 | 638 | |
| 938 | ||
| 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. | |
| 939 | 646 | |
| 940 | 647 | =end original |
| 941 | 648 | |
| 942 | ||
| 649 | それぞれのファイルに対して通常のファイルハンドルを開き、 | |
| 650 | このようにして読み込んだ全てのファイルの内容を徐々にメモリ内の配列に | |
| 651 | 構築し、読み込むファイルがなくなったら最後にソートとフィルタリングをする、 | |
| 652 | という形でこれを行うことも出来ます。 | |
| 653 | I<あるいは>、結合とソートをオペレーティング自身の C<sort> コマンドに | |
| 654 | 任せて、その出力を直接パイプで開くことで、遙かに速く作業することも出来ます。 | |
| 943 | 655 | |
| 944 | open(FH, "+< $path"); | |
| 945 | sysopen(FH, $path, O_RDWR); | |
| 946 | ||
| 947 | 656 | =begin original |
| 948 | 657 | |
| 949 | ||
| 658 | Here's how that might look: | |
| 950 | a regular C<open>. As you'll see, it's just a matter of controlling the | |
| 951 | flags in the third argument. | |
| 952 | 659 | |
| 953 | 660 | =end original |
| 954 | 661 | |
| 955 | ||
| 662 | 以下は、これがどのように見えるかです: | |
| 956 | 見てきたように、これは単に 3 番目の引数のフラグの制御の問題です。 | |
| 957 | 663 | |
| 958 | ||
| 664 | open(my $sort_fh, '-|', 'sort -u unsorted/*.txt') | |
| 665 | or die "Couldn't open a pipe into sort: $!"; | |
| 959 | 666 | |
| 960 | ||
| 667 | # And right away, we can start reading sorted lines: | |
| 961 | e | |
| 668 | while (my $line = <$sort_fh>) { | |
| 669 | # | |
| 670 | # ... Do something interesting with each $line here ... | |
| 671 | # | |
| 672 | } | |
| 962 | 673 | |
| 963 | =end original | |
| 964 | ||
| 965 | 既に存在していたりはしない新しいファイルを作成して、ファイルを書き込み用に | |
| 966 | 開くには: | |
| 967 | ||
| 968 | sysopen(FH, $path, O_WRONLY | O_EXCL | O_CREAT); | |
| 969 | ||
| 970 | 674 | =begin original |
| 971 | 675 | |
| 972 | T | |
| 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. | |
| 973 | 678 | |
| 974 | 679 | =end original |
| 975 | 680 | |
| 976 | ||
| 681 | C<open> の 2 番目の引数である C<"-|"> は、ファイルへの通常の | |
| 682 | ファイルハンドルではなく、別個のプログラムへの読み込みパイプにします。 | |
| 977 | 683 | |
| 978 | sysopen(FH, $path, O_WRONLY | O_APPEND); | |
| 979 | ||
| 980 | 684 | =begin original |
| 981 | 685 | |
| 982 | ||
| 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. | |
| 983 | 692 | |
| 984 | 693 | =end original |
| 985 | 694 | |
| 986 | ||
| 695 | C<open> の 3 番目の引数は、 | |
| 696 | プログラム名 (C<sort>) とその全ての引数を含んだ文字列です: | |
| 697 | この場合、C<-u> はユニークソートを指定し、それからファイルグロブは | |
| 698 | ソートするファイルを指定することに注意してください。 | |
| 699 | 結果のファイルハンドル C<$sort_fh> は | |
| 700 | ちょうど読み込み専用 (C<< "<" >>) ファイルハンドルのように動作し、 | |
| 701 | プログラムは、通常の単一のファイルが開かれたかのように、 | |
| 702 | 引き続いてそこからデータを読み込むことができます。 | |
| 987 | 703 | |
| 988 | | |
| 704 | =head2 Opening a pipe for writing | |
| 989 | 705 | |
| 990 | ||
| 706 | (書き込み用にパイプを開く) | |
| 991 | 707 | |
| 992 | To open a file for update, where that file must not already exist: | |
| 993 | ||
| 994 | =end original | |
| 995 | ||
| 996 | 予め存在していてはならないファイルを交信用に開くには: | |
| 997 | ||
| 998 | sysopen(FH, $path, O_RDWR | O_EXCL | O_CREAT); | |
| 999 | ||
| 1000 | 708 | =begin original |
| 1001 | 709 | |
| 1002 | ||
| 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. | |
| 1003 | 714 | |
| 1004 | 715 | =end original |
| 1005 | 716 | |
| 1006 | ||
| 717 | 前回の例の続きとして、プログラムの処理を完成させて、 | |
| 718 | 結果は C<@processed> と呼ばれる配列に入っているとしましょう。 | |
| 719 | これらの行を C<numbered.txt> というファイル名に、 | |
| 720 | いい感じに整形された行番号の列と共に出力したいとします。 | |
| 1007 | 721 | |
| 1008 | sysopen(FH, $path, O_WRONLY | O_NONBLOCK | O_CREAT); | |
| 1009 | ||
| 1010 | =head2 Permissions E<agrave> la mode | |
| 1011 | ||
| 1012 | (権限モード) | |
| 1013 | ||
| 1014 | 722 | =begin original |
| 1015 | 723 | |
| 1016 | ||
| 724 | Certainly you could write your own code to do this ? or, once again, | |
| 1017 | ||
| 725 | you could kick that work over to another program. In this case, C<cat>, | |
| 1018 | ||
| 726 | running with its own C<-n> option to activate line numbering, should do | |
| 727 | the trick: | |
| 1019 | 728 | |
| 1020 | 729 | =end original |
| 1021 | 730 | |
| 1022 | ||
| 731 | 確かにこれをするコードを自分で書くこともできます - あるいは、再び、 | |
| 1023 | ||
| 732 | この作業を他のプログラムに送ることもできます。 | |
| 1024 | ||
| 733 | この場合、C<cat> を、行番号付けを有効にする C<-n> オプション込みで | |
| 734 | 実行するには、次の技を使います: | |
| 1025 | 735 | |
| 1026 | ||
| 736 | open(my $cat_fh, '|-', 'cat -n > numbered.txt') | |
| 737 | or die "Couldn't open a pipe into cat: $!"; | |
| 1027 | 738 | |
| 1028 | ||
| 739 | for my $line (@processed) { | |
| 1029 | ||
| 740 | print $cat_fh $line; | |
| 1030 | I<disabled> permissions bits; that is, bits that will not be turned on | |
| 1031 | in the created file's permissions field. | |
| 1032 | ||
| 1033 | =end original | |
| 1034 | ||
| 1035 | なぜそんなに権限を与えるのでしょう? | |
| 1036 | えっと、実際にはそうではありません。 | |
| 1037 | MASK はプロセスの現在の C<umask> で修正されます。 | |
| 1038 | umask は I<無効にする> 許可ビットを表現する数値です; つまり、 | |
| 1039 | 作成したファイルの許可フィールドを有効にすることはないということです。 | |
| 1040 | ||
| 1041 | =begin original | |
| 1042 | ||
| 1043 | For example, if your C<umask> were 027, then the 020 part would | |
| 1044 | disable the group from writing, and the 007 part would disable others | |
| 1045 | from reading, writing, or executing. Under these conditions, passing | |
| 1046 | C<sysopen> 0666 would create a file with mode 0640, since C<0666 & ~027> | |
| 1047 | is 0640. | |
| 1048 | ||
| 1049 | =end original | |
| 1050 | ||
| 1051 | 例えば、C<umask> が 027 の場合、020 の部分はグループによる書き込みと | |
| 1052 | 実行を無効にし、007 の部分は他のユーザーによる読み込み、書き込み、 | |
| 1053 | 実行を無効にします。 | |
| 1054 | この条件では、C<sysopen> に 0666 を渡すとモード 0640 でファイルを作ります; | |
| 1055 | C<0666 & ~027> は 0640 だからです。 | |
| 1056 | ||
| 1057 | =begin original | |
| 1058 | ||
| 1059 | You should seldom use the MASK argument to C<sysopen()>. That takes | |
| 1060 | away the user's freedom to choose what permission new files will have. | |
| 1061 | Denying choice is almost always a bad thing. One exception would be for | |
| 1062 | cases where sensitive or private data is being stored, such as with mail | |
| 1063 | folders, cookie files, and internal temporary files. | |
| 1064 | ||
| 1065 | =end original | |
| 1066 | ||
| 1067 | C<sysopen()> に MASK 引数を使うことはほとんどないでしょう。 | |
| 1068 | これは、新しいファイルにどのパーミッションを与えるかというユーザーの | |
| 1069 | 自由を奪います。 | |
| 1070 | 選択を拒むということは、ほとんど常に悪いことです。 | |
| 1071 | 一つの例外は、メールフォルダ、クッキーファイル、内部用一時ファイルのような、 | |
| 1072 | 微妙な、あるいはプライベートなデータを保管する場合でしょう。 | |
| 1073 | ||
| 1074 | =head1 Obscure Open Tricks | |
| 1075 | ||
| 1076 | (わかりにくい開くときの小技) | |
| 1077 | ||
| 1078 | =head2 Re-Opening Files (dups) | |
| 1079 | ||
| 1080 | (ファイルを再び開く(dup)) | |
| 1081 | ||
| 1082 | =begin original | |
| 1083 | ||
| 1084 | Sometimes you already have a filehandle open, and want to make another | |
| 1085 | handle that's a duplicate of the first one. In the shell, we place an | |
| 1086 | ampersand in front of a file descriptor number when doing redirections. | |
| 1087 | For example, C<< 2>&1 >> makes descriptor 2 (that's STDERR in Perl) | |
| 1088 | be redirected into descriptor 1 (which is usually Perl's STDOUT). | |
| 1089 | The same is essentially true in Perl: a filename that begins with an | |
| 1090 | ampersand is treated instead as a file descriptor if a number, or as a | |
| 1091 | filehandle if a string. | |
| 1092 | ||
| 1093 | =end original | |
| 1094 | ||
| 1095 | 既に開いているファイルハンドルを持っている時に、これを複製して | |
| 1096 | もう一つのハンドルがほしくなる場合がときどきあります。 | |
| 1097 | シェルでは、リダイレクトをするときにファイル記述子番号の前に | |
| 1098 | アンパサンドを置きます。 | |
| 1099 | 例えば C<< 2>&1 >> は、記述子 2 (これは Perl では STDERR) を | |
| 1100 | 記述子 1 (これは Perl では普通は STDOUT) にリダイレクトします。 | |
| 1101 | 同じことは Perl でも基本的には真です: アンパサンドで始まるファイル名は、 | |
| 1102 | それが数値ならファイル記述子、文字列ならファイルハンドルとして | |
| 1103 | 扱われます。 | |
| 1104 | ||
| 1105 | open(SAVEOUT, ">&SAVEERR") || die "couldn't dup SAVEERR: $!"; | |
| 1106 | open(MHCONTEXT, "<&4") || die "couldn't dup fd4: $!"; | |
| 1107 | ||
| 1108 | =begin original | |
| 1109 | ||
| 1110 | That means that if a function is expecting a filename, but you don't | |
| 1111 | want to give it a filename because you already have the file open, you | |
| 1112 | can just pass the filehandle with a leading ampersand. It's best to | |
| 1113 | use a fully qualified handle though, just in case the function happens | |
| 1114 | to be in a different package: | |
| 1115 | ||
| 1116 | =end original | |
| 1117 | ||
| 1118 | これは、もし関数がファイル名を想定しているけれども、既にファイルは | |
| 1119 | 開いているのでファイル名を渡したくない場合、単に先頭にアンパサンドを | |
| 1120 | 付けたファイルハンドルを渡せるということを意味します。 | |
| 1121 | しかし、万が一関数がたまたま違うパッケージだったときのために、完全修飾した | |
| 1122 | ハンドルを渡すのが最善です: | |
| 1123 | ||
| 1124 | somefunction("&main::LOGFILE"); | |
| 1125 | ||
| 1126 | =begin original | |
| 1127 | ||
| 1128 | This way if somefunction() is planning on opening its argument, it can | |
| 1129 | just use the already opened handle. This differs from passing a handle, | |
| 1130 | because with a handle, you don't open the file. Here you have something | |
| 1131 | you can pass to open. | |
| 1132 | ||
| 1133 | =end original | |
| 1134 | ||
| 1135 | この方法により、somefunction() が引数の値を開いた場合、 | |
| 1136 | 単に既に開いているハンドルを使えます。 | |
| 1137 | これはハンドルを渡すのとは違います; なぜならハンドルではファイルを | |
| 1138 | 開かないからです。 | |
| 1139 | こちらでは開くときに指定できるものが指定できます。 | |
| 1140 | ||
| 1141 | =begin original | |
| 1142 | ||
| 1143 | If you have one of those tricky, newfangled I/O objects that the C++ | |
| 1144 | folks are raving about, then this doesn't work because those aren't a | |
| 1145 | proper filehandle in the native Perl sense. You'll have to use fileno() | |
| 1146 | to pull out the proper descriptor number, assuming you can: | |
| 1147 | ||
| 1148 | =end original | |
| 1149 | ||
| 1150 | もし、C++ 民が夢中になっているような巧妙で目新しい I/O オブジェクトの一つを | |
| 1151 | 使っているなら、これらはネイティブな Perl 的に適切なファイルハンドルでは | |
| 1152 | ないので、上述のような方法は動作しません。 | |
| 1153 | 適切な記述子番号を得るために fileno() を使う必要があります; それが出来ると | |
| 1154 | 仮定すれば: | |
| 1155 | ||
| 1156 | use IO::Socket; | |
| 1157 | $handle = IO::Socket::INET->new("www.perl.com:80"); | |
| 1158 | $fd = $handle->fileno; | |
| 1159 | somefunction("&$fd"); # not an indirect function call | |
| 1160 | ||
| 1161 | =begin original | |
| 1162 | ||
| 1163 | It can be easier (and certainly will be faster) just to use real | |
| 1164 | filehandles though: | |
| 1165 | ||
| 1166 | =end original | |
| 1167 | ||
| 1168 | しかし、単に普通のファイルハンドルを使う方が簡単でしょう | |
| 1169 | (そして確実に高速です): | |
| 1170 | ||
| 1171 | use IO::Socket; | |
| 1172 | local *REMOTE = IO::Socket::INET->new("www.perl.com:80"); | |
| 1173 | die "can't connect" unless defined(fileno(REMOTE)); | |
| 1174 | somefunction("&main::REMOTE"); | |
| 1175 | ||
| 1176 | =begin original | |
| 1177 | ||
| 1178 | If the filehandle or descriptor number is preceded not just with a simple | |
| 1179 | "&" but rather with a "&=" combination, then Perl will not create a | |
| 1180 | completely new descriptor opened to the same place using the dup(2) | |
| 1181 | system call. Instead, it will just make something of an alias to the | |
| 1182 | existing one using the fdopen(3S) library call. This is slightly more | |
| 1183 | parsimonious of systems resources, although this is less a concern | |
| 1184 | these days. Here's an example of that: | |
| 1185 | ||
| 1186 | =end original | |
| 1187 | ||
| 1188 | もしファイルハンドルや記述子番号の前にあるのが単なる "&" ではなく "&=" の | |
| 1189 | 組み合わせの場合、Perl は dup(2) システムコールを使って同じ場所で開いた | |
| 1190 | 完全に新しい記述子は作りません。 | |
| 1191 | 代わりに、fdopen(3S) ライブラリコールを使ってすでにある記述子の別名的な | |
| 1192 | ものを作ります。 | |
| 1193 | これはシステムのリソースを少しケチることが出来ますが、最近ではこれは | |
| 1194 | あまり関心を持たれなくなりました。 | |
| 1195 | 以下はこの例です: | |
| 1196 | ||
| 1197 | $fd = $ENV{"MHCONTEXTFD"}; | |
| 1198 | open(MHCONTEXT, "<&=$fd") or die "couldn't fdopen $fd: $!"; | |
| 1199 | ||
| 1200 | =begin original | |
| 1201 | ||
| 1202 | If you're using magic C<< <ARGV> >>, you could even pass in as a | |
| 1203 | command line argument in @ARGV something like C<"<&=$MHCONTEXTFD">, | |
| 1204 | but we've never seen anyone actually do this. | |
| 1205 | ||
| 1206 | =end original | |
| 1207 | ||
| 1208 | もしマジカルな C<< <ARGV> >> を使っているなら、C<"<&=$MHCONTEXTFD"> の | |
| 1209 | ような感じで @ARGV 内のコマンドライン引数として渡すことすら可能ですが、 | |
| 1210 | 実際にこれをしている人を見たことはありません。 | |
| 1211 | ||
| 1212 | =head2 Dispelling the Dweomer | |
| 1213 | ||
| 1214 | (魔法を解く) | |
| 1215 | ||
| 1216 | =begin original | |
| 1217 | ||
| 1218 | Perl is more of a DWIMmer language than something like Java--where DWIM | |
| 1219 | is an acronym for "do what I mean". But this principle sometimes leads | |
| 1220 | to more hidden magic than one knows what to do with. In this way, Perl | |
| 1221 | is also filled with I<dweomer>, an obscure word meaning an enchantment. | |
| 1222 | Sometimes, Perl's DWIMmer is just too much like dweomer for comfort. | |
| 1223 | ||
| 1224 | =end original | |
| 1225 | ||
| 1226 | Perl は、Java のような言語よりも「空気を読む」(DWIM)言語です -- | |
| 1227 | DWIM とは "do what I mean" の略です。 | |
| 1228 | しかし、この原則は時々利用者が知っている以上の隠れた動作をすることが | |
| 1229 | あります。 | |
| 1230 | こんな風に、Perl は (魔法を意味する不明確な単語である) I<dweomer> にも | |
| 1231 | 満ちています。 | |
| 1232 | 時々、Perl の空気の読み方は快適さのために魔法のようになります。 | |
| 1233 | ||
| 1234 | =begin original | |
| 1235 | ||
| 1236 | If magic C<open> is a bit too magical for you, you don't have to turn | |
| 1237 | to C<sysopen>. To open a file with arbitrary weird characters in | |
| 1238 | it, it's necessary to protect any leading and trailing whitespace. | |
| 1239 | Leading whitespace is protected by inserting a C<"./"> in front of a | |
| 1240 | filename that starts with whitespace. Trailing whitespace is protected | |
| 1241 | by appending an ASCII NUL byte (C<"\0">) at the end of the string. | |
| 1242 | ||
| 1243 | =end original | |
| 1244 | ||
| 1245 | もしマジカルな C<open> があなたにとってちょっとマジカルすぎるとしても、 | |
| 1246 | C<sysopen> にまで戻る必要はありません。 | |
| 1247 | ファイル名にどんな変な文字が含まれているファイルでも開くためには、 | |
| 1248 | 先頭と末尾の空白を保護する必要があります。 | |
| 1249 | 先頭の空白は、空白で始まるファイル名の前に C<"./"> を挿入することで | |
| 1250 | 保護します。 | |
| 1251 | 末尾の空白は、文字列の末尾に ASCII NUL バイト (C<"\0">) を | |
| 1252 | 追加することで保護します。 | |
| 1253 | ||
| 1254 | $file =~ s#^(\s)#./$1#; | |
| 1255 | open(FH, "< $file\0") || die "can't open $file: $!"; | |
| 1256 | ||
| 1257 | =begin original | |
| 1258 | ||
| 1259 | This assumes, of course, that your system considers dot the current | |
| 1260 | working directory, slash the directory separator, and disallows ASCII | |
| 1261 | NULs within a valid filename. Most systems follow these conventions, | |
| 1262 | including all POSIX systems as well as proprietary Microsoft systems. | |
| 1263 | The only vaguely popular system that doesn't work this way is the | |
| 1264 | "Classic" Macintosh system, which uses a colon where the rest of us | |
| 1265 | use a slash. Maybe C<sysopen> isn't such a bad idea after all. | |
| 1266 | ||
| 1267 | =end original | |
| 1268 | ||
| 1269 | これはもちろん、あなたのシステムが "." をカレントディレクトリ、 | |
| 1270 | "/" をディレクトリの区切りとして扱い、ASCII NUL をファイル名として | |
| 1271 | 認めていないということを仮定しています。 | |
| 1272 | 全ての POSIX システムとプロプリエタリの Microsoft システムを含む、 | |
| 1273 | ほとんどのシステムはこの慣例に従っています。 | |
| 1274 | これに従わない、一般的に有名な唯一のシステムは | |
| 1275 | "Classic" Macintosh システムです; これは他のシステムが "/" を | |
| 1276 | 使っているところで ":" を使います。 | |
| 1277 | おそらく、とにかく C<sysopen> を使うということはそれほど悪い考えでは | |
| 1278 | ありません。 | |
| 1279 | ||
| 1280 | =begin original | |
| 1281 | ||
| 1282 | If you want to use C<< <ARGV> >> processing in a totally boring | |
| 1283 | and non-magical way, you could do this first: | |
| 1284 | ||
| 1285 | =end original | |
| 1286 | ||
| 1287 | もし、C<< <ARGV> >> の処理を、本当に退屈かつマジカルでない方法で | |
| 1288 | 行いたいなら、まず以下のようにできます: | |
| 1289 | ||
| 1290 | # "Sam sat on the ground and put his head in his hands. | |
| 1291 | # 'I wish I had never come here, and I don't want to see | |
| 1292 | # no more magic,' he said, and fell silent." | |
| 1293 | for (@ARGV) { | |
| 1294 | s#^([^./])#./$1#; | |
| 1295 | $_ .= "\0"; | |
| 1296 | } | |
| 1297 | while (<>) { | |
| 1298 | # now process $_ | |
| 1299 | } | |
| 1300 | ||
| 1301 | =begin original | |
| 1302 | ||
| 1303 | But be warned that users will not appreciate being unable to use "-" | |
| 1304 | to mean standard input, per the standard convention. | |
| 1305 | ||
| 1306 | =end original | |
| 1307 | ||
| 1308 | 但し、ユーザーは、標準入力を意味するために "-" を使うという一般的な | |
| 1309 | 慣習が使えないということを喜ばないだろうということは | |
| 1310 | 警告しておきます。 | |
| 1311 | ||
| 1312 | =head2 Paths as Opens | |
| 1313 | ||
| 1314 | (open にパスを) | |
| 1315 | ||
| 1316 | =begin original | |
| 1317 | ||
| 1318 | You've probably noticed how Perl's C<warn> and C<die> functions can | |
| 1319 | produce messages like: | |
| 1320 | ||
| 1321 | =end original | |
| 1322 | ||
| 1323 | どうやって Perl の C<warn> 関数と C<die> 関数が以下のようなメッセージを | |
| 1324 | 生成するかに気付いたでしょう: | |
| 1325 | ||
| 1326 | Some warning at scriptname line 29, <FH> line 7. | |
| 1327 | ||
| 1328 | =begin original | |
| 1329 | ||
| 1330 | That's because you opened a filehandle FH, and had read in seven records | |
| 1331 | from it. But what was the name of the file, rather than the handle? | |
| 1332 | ||
| 1333 | =end original | |
| 1334 | ||
| 1335 | これは、あなたがファイルハンドル FH を開いて、そこから 7 レコードを | |
| 1336 | 読み込んだからです。 | |
| 1337 | しかし、ハンドルではなく、ファイル名はどうでしょう? | |
| 1338 | ||
| 1339 | =begin original | |
| 1340 | ||
| 1341 | If you aren't running with C<strict refs>, or if you've turned them off | |
| 1342 | temporarily, then all you have to do is this: | |
| 1343 | ||
| 1344 | =end original | |
| 1345 | ||
| 1346 | もし C<strict refs> を有効にしていないか、一時的に無効にしているなら、 | |
| 1347 | する必要があるのは以下のことだけです: | |
| 1348 | ||
| 1349 | open($path, "< $path") || die "can't open $path: $!"; | |
| 1350 | while (<$path>) { | |
| 1351 | # whatever | |
| 1352 | } | |
| 1353 | ||
| 1354 | =begin original | |
| 1355 | ||
| 1356 | Since you're using the pathname of the file as its handle, | |
| 1357 | you'll get warnings more like | |
| 1358 | ||
| 1359 | =end original | |
| 1360 | ||
| 1361 | ファイルのパス名をハンドルとして使っているので、以下のような警告が | |
| 1362 | 出ます | |
| 1363 | ||
| 1364 | Some warning at scriptname line 29, </etc/motd> line 7. | |
| 1365 | ||
| 1366 | =head2 Single Argument Open | |
| 1367 | ||
| 1368 | (1 引数の open) | |
| 1369 | ||
| 1370 | =begin original | |
| 1371 | ||
| 1372 | Remember how we said that Perl's open took two arguments? That was a | |
| 1373 | passive prevarication. You see, it can also take just one argument. | |
| 1374 | If and only if the variable is a global variable, not a lexical, you | |
| 1375 | can pass C<open> just one argument, the filehandle, and it will | |
| 1376 | get the path from the global scalar variable of the same name. | |
| 1377 | ||
| 1378 | =end original | |
| 1379 | ||
| 1380 | Perl の open は 2 引数を取ると言ったことを覚えていますか? | |
| 1381 | これは消極的なごまかしです。 | |
| 1382 | ほら、単に 1 引数を取ることもできます。 | |
| 1383 | 変数がレキシカルではなくグローバルな変数の場合にのみ、C<open> に | |
| 1384 | 1 引数だけ(ファイルハンドル)を渡すことができます; こうすると、 | |
| 1385 | 同じ名前を持つグローバルなスカラ変数からパスを取ります。 | |
| 1386 | ||
| 1387 | $FILE = "/etc/motd"; | |
| 1388 | open FILE or die "can't open $FILE: $!"; | |
| 1389 | while (<FILE>) { | |
| 1390 | # whatever | |
| 1391 | } | |
| 1392 | ||
| 1393 | =begin original | |
| 1394 | ||
| 1395 | Why is this here? Someone has to cater to the hysterical porpoises. | |
| 1396 | It's something that's been in Perl since the very beginning, if not | |
| 1397 | before. | |
| 1398 | ||
| 1399 | =end original | |
| 1400 | ||
| 1401 | どうしてこれはここなんでしょう? | |
| 1402 | 誰かがヒステリックなネズミイルカの要求を満たす必要があります。 | |
| 1403 | これは(遅くとも)非常に初期から Perl にあります。 | |
| 1404 | ||
| 1405 | =head2 Playing with STDIN and STDOUT | |
| 1406 | ||
| 1407 | (STDIN と STDOUT を扱う) | |
| 1408 | ||
| 1409 | =begin original | |
| 1410 | ||
| 1411 | One clever move with STDOUT is to explicitly close it when you're done | |
| 1412 | with the program. | |
| 1413 | ||
| 1414 | =end original | |
| 1415 | ||
| 1416 | STDOUT に関する一つの利口な行動は、プログラムの終了時に | |
| 1417 | 明示的に閉じることです。 | |
| 1418 | ||
| 1419 | END { close(STDOUT) || die "can't close stdout: $!" } | |
| 1420 | ||
| 1421 | =begin original | |
| 1422 | ||
| 1423 | If you don't do this, and your program fills up the disk partition due | |
| 1424 | to a command line redirection, it won't report the error exit with a | |
| 1425 | failure status. | |
| 1426 | ||
| 1427 | =end original | |
| 1428 | ||
| 1429 | これをしないままで、このプログラムがコマンドラインリダイレクトによって | |
| 1430 | ディスクをいっぱいにしてしまっても、失敗状態でエラー終了しません。 | |
| 1431 | ||
| 1432 | =begin original | |
| 1433 | ||
| 1434 | You don't have to accept the STDIN and STDOUT you were given. You are | |
| 1435 | welcome to reopen them if you'd like. | |
| 1436 | ||
| 1437 | =end original | |
| 1438 | ||
| 1439 | 与えられた STDIN と STDOUT を受け入れる必要はありません。 | |
| 1440 | もし望むなら、これらを開き直せます。 | |
| 1441 | ||
| 1442 | open(STDIN, "< datafile") | |
| 1443 | || die "can't open datafile: $!"; | |
| 1444 | ||
| 1445 | open(STDOUT, "> output") | |
| 1446 | || die "can't open output: $!"; | |
| 1447 | ||
| 1448 | =begin original | |
| 1449 | ||
| 1450 | And then these can be accessed directly or passed on to subprocesses. | |
| 1451 | This makes it look as though the program were initially invoked | |
| 1452 | with those redirections from the command line. | |
| 1453 | ||
| 1454 | =end original | |
| 1455 | ||
| 1456 | それからこれらは直接アクセスしたり子プロセスに渡したりできます。 | |
| 1457 | これらは、プログラムの起動時にコマンドラインからリダイレクトが | |
| 1458 | 与えられたかのように動作します。 | |
| 1459 | ||
| 1460 | =begin original | |
| 1461 | ||
| 1462 | It's probably more interesting to connect these to pipes. For example: | |
| 1463 | ||
| 1464 | =end original | |
| 1465 | ||
| 1466 | これらをパイプにつなぐ方がより興味深いでしょう。 | |
| 1467 | 例えば: | |
| 1468 | ||
| 1469 | $pager = $ENV{PAGER} || "(less || more)"; | |
| 1470 | open(STDOUT, "| $pager") | |
| 1471 | || die "can't fork a pager: $!"; | |
| 1472 | ||
| 1473 | =begin original | |
| 1474 | ||
| 1475 | This makes it appear as though your program were called with its stdout | |
| 1476 | already piped into your pager. You can also use this kind of thing | |
| 1477 | in conjunction with an implicit fork to yourself. You might do this | |
| 1478 | if you would rather handle the post processing in your own program, | |
| 1479 | just in a different process: | |
| 1480 | ||
| 1481 | =end original | |
| 1482 | ||
| 1483 | これによって、プログラムの標準出力がが既にページャとパイプで | |
| 1484 | つながれているかのように見えます。 | |
| 1485 | このようなことはまた、自分自身を暗黙に fork したものと結合するためにも | |
| 1486 | 使えます。 | |
| 1487 | 自分自身のプログラムの別のプロセスでで後処理を扱いたい場合、 | |
| 1488 | 以下のようにできます: | |
| 1489 | ||
| 1490 | head(100); | |
| 1491 | while (<>) { | |
| 1492 | print; | |
| 1493 | } | |
| 1494 | ||
| 1495 | sub head { | |
| 1496 | my $lines = shift || 20; | |
| 1497 | return if $pid = open(STDOUT, "|-"); # return if parent | |
| 1498 | die "cannot fork: $!" unless defined $pid; | |
| 1499 | while (<STDIN>) { | |
| 1500 | last if --$lines < 0; | |
| 1501 | print; | |
| 1502 | } | |
| 1503 | exit; | |
| 1504 | } | |
| 1505 | ||
| 1506 | =begin original | |
| 1507 | ||
| 1508 | This technique can be applied to repeatedly push as many filters on your | |
| 1509 | output stream as you wish. | |
| 1510 | ||
| 1511 | =end original | |
| 1512 | ||
| 1513 | このテクニックは、繰り返しプッシュすることで、出力ストリームに好きなだけ | |
| 1514 | 多くのフィルタを適用できます。 | |
| 1515 | ||
| 1516 | =head1 Other I/O Issues | |
| 1517 | ||
| 1518 | (その他の I/O 関連の話題) | |
| 1519 | ||
| 1520 | =begin original | |
| 1521 | ||
| 1522 | These topics aren't really arguments related to C<open> or C<sysopen>, | |
| 1523 | but they do affect what you do with your open files. | |
| 1524 | ||
| 1525 | =end original | |
| 1526 | ||
| 1527 | これらの話題は実際には C<open> や C<sysopen> に関連したものではありませんが、 | |
| 1528 | ファイルを開くときに行うことに影響を与えます。 | |
| 1529 | ||
| 1530 | =head2 Opening Non-File Files | |
| 1531 | ||
| 1532 | (ファイルでないファイルを開く) | |
| 1533 | ||
| 1534 | =begin original | |
| 1535 | ||
| 1536 | When is a file not a file? Well, you could say when it exists but | |
| 1537 | isn't a plain file. We'll check whether it's a symbolic link first, | |
| 1538 | just in case. | |
| 1539 | ||
| 1540 | =end original | |
| 1541 | ||
| 1542 | ファイルがファイルでないときは? | |
| 1543 | えっと、プレーンファイルでないもののとき、と言いたいんですよね。 | |
| 1544 | まず、念のために、それがシンボリックリンクかどうかを調べます。 | |
| 1545 | ||
| 1546 | if (-l $file || ! -f _) { | |
| 1547 | print "$file is not a plain file\n"; | |
| 1548 | } | |
| 1549 | ||
| 1550 | =begin original | |
| 1551 | ||
| 1552 | What other kinds of files are there than, well, files? Directories, | |
| 1553 | symbolic links, named pipes, Unix-domain sockets, and block and character | |
| 1554 | devices. Those are all files, too--just not I<plain> files. This isn't | |
| 1555 | the same issue as being a text file. Not all text files are plain files. | |
| 1556 | Not all plain files are text files. That's why there are separate C<-f> | |
| 1557 | and C<-T> file tests. | |
| 1558 | ||
| 1559 | =end original | |
| 1560 | ||
| 1561 | えーと、ファイルの他にどんな種類のファイルがあるのでしょう? | |
| 1562 | ディレクトリ、シンボリックリンク、名前付きパイプ、Unix ドメインソケット、 | |
| 1563 | キャラクタデバイス、ブロックデバイスです。 | |
| 1564 | これらも全てファイルです -- 単に I<プレーン> ファイルではないと | |
| 1565 | いうだけです。 | |
| 1566 | これはテキストファイルと同じ問題ではありません。 | |
| 1567 | 全てのテキストファイルがプレーンファイルではありません。 | |
| 1568 | 全てのプレーンファイルがテキストファイルではありません。 | |
| 1569 | これが、C<-f> と C<-T> のファイルテストが分離している理由です。 | |
| 1570 | ||
| 1571 | =begin original | |
| 1572 | ||
| 1573 | To open a directory, you should use the C<opendir> function, then | |
| 1574 | process it with C<readdir>, carefully restoring the directory | |
| 1575 | name if necessary: | |
| 1576 | ||
| 1577 | =end original | |
| 1578 | ||
| 1579 | ディレクトリを開くには、C<opendir> 関数を使って、それから | |
| 1580 | C<readdir> で処理します; もし必要なら注意深くディレクトリ名を復元します: | |
| 1581 | ||
| 1582 | opendir(DIR, $dirname) or die "can't opendir $dirname: $!"; | |
| 1583 | while (defined($file = readdir(DIR))) { | |
| 1584 | # do something with "$dirname/$file" | |
| 1585 | 741 | } |
| 1586 | closedir(DIR); | |
| 1587 | 742 | |
| 1588 | 743 | =begin original |
| 1589 | 744 | |
| 1590 | ||
| 745 | Here, we use a second C<open> argument of C<"|-">, signifying that the | |
| 1591 | ||
| 746 | filehandle assigned to C<$cat_fh> should be a write-pipe. We can then | |
| 1592 | ||
| 747 | use it just as we would a write-only ordinary filehandle, including the | |
| 748 | basic function of C<print>-ing data to it. | |
| 1593 | 749 | |
| 1594 | 750 | =end original |
| 1595 | 751 | |
| 1596 | ||
| 752 | ここで、C<open> の 2 番目の引数に C<"|-"> を使います; | |
| 1597 | ||
| 753 | これにより、C<$cat_fh> に代入されるファイルハンドルが書き込み | |
| 1598 | ||
| 754 | パイプであることを示します。 | |
| 1599 | デ | |
| 755 | それから、データを C<print> する基本的な関数を含めて、 | |
| 756 | 書き込み専用の普通のファイルハンドルを使うのと同じようにこれを使えます。 | |
| 1600 | 757 | |
| 1601 | @ARGV = qw(.) unless @ARGV; | |
| 1602 | use File::Find; | |
| 1603 | find sub { print $File::Find::name, -d && '/', "\n" }, @ARGV; | |
| 1604 | ||
| 1605 | 758 | =begin original |
| 1606 | 759 | |
| 1607 | ||
| 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. | |
| 1608 | 768 | |
| 1609 | 769 | =end original |
| 1610 | 770 | |
| 1611 | ||
| 771 | パイプしたいコマンドを指定する 3 番目の引数は、 | |
| 772 | C<cat> の出力を C<< ">" >> 記号を使ってファイル C<numbered.txt> に | |
| 773 | リダイレクトするように指定していることに注意してください。 | |
| 774 | これは最初は少しおかしく見えるかもしれません; | |
| 775 | この同じ記号は、C<open> の 2 番目の引数では全く違うものを意味するからです! | |
| 776 | しかし、ここ 3 番目の引数では、これは単に Perl がパイプを開く | |
| 777 | シェルコマンドの一部であり、Perl 自身はこれに何の特別な意味も与えません。 | |
| 1612 | 778 | |
| 1613 | ||
| 779 | =head2 Expressing the command as a list | |
| 1614 | 780 | |
| 1615 | ||
| 781 | (コマンドをリストとして表現する) | |
| 1616 | 782 | |
| 1617 | As you see, with symbolic links, you can just pretend that it is | |
| 1618 | what it points to. Or, if you want to know I<what> it points to, then | |
| 1619 | C<readlink> is called for: | |
| 1620 | ||
| 1621 | =end original | |
| 1622 | ||
| 1623 | 上述したように、シンボリックリンクの場合、単にそれが指しているもの振りを | |
| 1624 | することができます。 | |
| 1625 | あるいは、もしそれが I<何を> 指しているのかを知りたい場合は、 | |
| 1626 | C<readlink> を呼び出します: | |
| 1627 | ||
| 1628 | if (-l $file) { | |
| 1629 | if (defined($whither = readlink($file))) { | |
| 1630 | print "$file points to $whither\n"; | |
| 1631 | } else { | |
| 1632 | print "$file points nowhere: $!\n"; | |
| 1633 | } | |
| 1634 | } | |
| 1635 | ||
| 1636 | =head2 Opening Named Pipes | |
| 1637 | ||
| 1638 | (名前付きパイプを開く) | |
| 1639 | ||
| 1640 | 783 | =begin original |
| 1641 | 784 | |
| 1642 | ||
| 785 | For opening pipes, Perl offers the option to call C<open> with a list | |
| 1643 | ||
| 786 | comprising the desired command and all its own arguments as separate | |
| 1644 | ||
| 787 | elements, rather than combining them into a single string as in the | |
| 1645 | ||
| 788 | examples above. For instance, we could have phrased the C<open> call in | |
| 1646 | ||
| 789 | the first example like this: | |
| 1647 | 790 | |
| 1648 | 791 | =end original |
| 1649 | 792 | |
| 1650 | ||
| 793 | パイプを開くために、Perl は、目的のコマンドとそれ自身の引数を、 | |
| 1651 | ||
| 794 | 前述の例のように単一の文字列として結合するのではなく、 | |
| 1652 | ||
| 795 | 別個の要素として構成されたリストで C<open> を呼び出すという | |
| 1653 | ||
| 796 | 選択肢を提供しています。 | |
| 1654 | ||
| 797 | 例えば、最初の例の C<open> 呼び出しは次のように書けます: | |
| 1655 | Unix ドメインソケットは同様にやや違うものです; | |
| 1656 | これらは L<perlipc/"Unix-Domain TCP Clients and Servers"> に | |
| 1657 | 記述されています。 | |
| 1658 | 798 | |
| 1659 | ||
| 799 | open(my $sort_fh, '-|', 'sort', '-u', glob('unsorted/*.txt')) | |
| 800 | or die "Couldn't open a pipe into sort: $!"; | |
| 1660 | 801 | |
| 1661 | When it comes to opening devices, it can be easy and it can be tricky. | |
| 1662 | We'll assume that if you're opening up a block device, you know what | |
| 1663 | you're doing. The character devices are more interesting. These are | |
| 1664 | typically used for modems, mice, and some kinds of printers. This is | |
| 1665 | described in L<perlfaq8/"How do I read and write the serial port?"> | |
| 1666 | It's often enough to open them carefully: | |
| 1667 | ||
| 1668 | =end original | |
| 1669 | ||
| 1670 | デバイスを開くときは、簡単にもなりますしトリッキーにもなります。 | |
| 1671 | ブロックデバイスを開こうとしているなら、何をしようとしているのか | |
| 1672 | 分かっていることを仮定します。 | |
| 1673 | キャラクタデバイスはもっと興味深いです。 | |
| 1674 | これらは典型的にはモデム、マウス、ある種のプリンタのために使われます。 | |
| 1675 | これは L<perlfaq8/"How do I read and write the serial port?"> に | |
| 1676 | 記述されています。 | |
| 1677 | しばしば慎重に開くだけで充分です: | |
| 1678 | ||
| 1679 | sysopen(TTYIN, "/dev/ttyS1", O_RDWR | O_NDELAY | O_NOCTTY) | |
| 1680 | # (O_NOCTTY no longer needed on POSIX systems) | |
| 1681 | or die "can't open /dev/ttyS1: $!"; | |
| 1682 | open(TTYOUT, "+>&TTYIN") | |
| 1683 | or die "can't dup TTYIN: $!"; | |
| 1684 | ||
| 1685 | $ofh = select(TTYOUT); $| = 1; select($ofh); | |
| 1686 | ||
| 1687 | print TTYOUT "+++at\015"; | |
| 1688 | $answer = <TTYIN>; | |
| 1689 | ||
| 1690 | 802 | =begin original |
| 1691 | 803 | |
| 1692 | W | |
| 804 | When you call C<open> this way, Perl invokes the given command directly, | |
| 1693 | s | |
| 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. | |
| 1694 | 810 | |
| 1695 | 811 | =end original |
| 1696 | 812 | |
| 1697 | ||
| 813 | この方法で C<open> を呼び出す場合、 | |
| 1698 | ||
| 814 | Perl はシェルをバイパスして指定されたコマンドを直接起動します。 | |
| 815 | シェルはコマンド路の引数リストの中の特殊文字を解釈しようとはしません; | |
| 816 | さもなければ望まない効果を生むことがあります。 | |
| 817 | これはより安全で、C<open> 呼び出しの誤りを減らし、 | |
| 818 | 引数として変数の内容を渡すような場合に有用で、 | |
| 819 | 単に空白を含むファイルを参照する場合にも安全です。 | |
| 1699 | 820 | |
| 1700 | use Fcntl; | |
| 1701 | my $old_flags = fcntl($handle, F_GETFL, 0) | |
| 1702 | or die "can't get flags: $!"; | |
| 1703 | fcntl($handle, F_SETFL, $old_flags | O_NONBLOCK) | |
| 1704 | or die "can't set non blocking: $!"; | |
| 1705 | ||
| 1706 | 821 | =begin original |
| 1707 | 822 | |
| 1708 | ||
| 823 | However, when you I<do> want to pass a meaningful metacharacter to the | |
| 1709 | ||
| 824 | shell, such with the C<"*"> inside that final C<unsorted/*.txt> argument | |
| 1710 | ||
| 825 | here, you can't use this alternate syntax. In this case, we have worked | |
| 1711 | ||
| 826 | around it via Perl's handy C<glob> built-in function, which evaluates | |
| 1712 | ||
| 827 | its argument into a list of filenames ? and we can safely pass that | |
| 1713 | ||
| 828 | resulting list right into C<open>, as shown above. | |
| 1714 | also some high-level modules on CPAN that can help you with these games. | |
| 1715 | Check out Term::ReadKey and Term::ReadLine. | |
| 1716 | 829 | |
| 1717 | 830 | =end original |
| 1718 | 831 | |
| 1719 | ||
| 832 | しかし、シェルに意味のあるメタ文字を I<渡したい>、 | |
| 1720 | ||
| 833 | 例えば最終的な C<unsorted/*.txt> の中の C<"*"> のような場合、 | |
| 1721 | ||
| 834 | この代替文法は使えません。 | |
| 1722 | こ | |
| 835 | この場合、引数をファイル名として評価する Perl の便利な C<glob> 組み込み関数で | |
| 1723 | ||
| 836 | 回避します; そして前述したように、結果のリストを C<open> に安全に | |
| 1724 | ||
| 837 | 渡せます。 | |
| 1725 | 読む必要があります。 | |
| 1726 | これらのものを扱う助けになるような高レベルモジュールも CPAN にあります。 | |
| 1727 | Term::ReadKey と Term::ReadLine を調べてください。 | |
| 1728 | 838 | |
| 1729 | =head2 Opening Sockets | |
| 1730 | ||
| 1731 | (ソケットを開く) | |
| 1732 | ||
| 1733 | 839 | =begin original |
| 1734 | 840 | |
| 1735 | ||
| 841 | Note also that representing piped-command arguments in list form like | |
| 1736 | o | |
| 842 | this doesn't work on every platform. It will work on any Unix-based OS | |
| 1737 | ||
| 843 | that provides a real C<fork> function (e.g. macOS or Linux), as well as | |
| 1738 | ||
| 844 | on Windows when running Perl 5.22 or later. | |
| 1739 | 845 | |
| 1740 | 846 | =end original |
| 1741 | 847 | |
| 1742 | ||
| 848 | また、このようなリスト形式でのパイプコマンド引数表現は、全ての | |
| 1743 | ||
| 849 | プラットフォームで動作するわけではないことに注意してください。 | |
| 1744 | ||
| 850 | 真の C<fork> 関数を提供する Unix ベースの OS (例えば macOS や Linux)、 | |
| 1745 | ||
| 851 | および Perl 5.22 以降の Windows では動作します。 | |
| 1746 | 参照してください。 | |
| 1747 | 以下は例です。 | |
| 1748 | これを実行すると、FH を双方向ファイルハンドルとして使えます。 | |
| 1749 | 852 | |
| 1750 | ||
| 853 | =head1 SEE ALSO | |
| 1751 | local *FH = IO::Socket::INET->new("www.perl.com:80"); | |
| 1752 | 854 | |
| 1753 | 855 | =begin original |
| 1754 | 856 | |
| 1755 | ||
| 857 | The full documentation for L<C<open>|perlfunc/open FILEHANDLE,MODE,EXPR> | |
| 1756 | ||
| 858 | provides a thorough reference to this function, beyond the best-practice | |
| 1757 | ||
| 859 | basics covered here. | |
| 1758 | 860 | |
| 1759 | 861 | =end original |
| 1760 | 862 | |
| 1761 | ||
| 863 | L<C<open>|perlfunc/open FILEHANDLE,MODE,EXPR> の完全な文書は、 | |
| 1762 | ||
| 864 | ここでカバーしているベストプラクティスベースのものを超えて、 | |
| 1763 | ||
| 865 | この関数の完全なリファレンスを提供します。 | |
| 1764 | 866 | |
| 1765 | use LWP::Simple; | |
| 1766 | $doc = get('http://www.cpan.org/'); | |
| 1767 | ||
| 1768 | =head2 Binary Files | |
| 1769 | ||
| 1770 | (バイナリファイル) | |
| 1771 | ||
| 1772 | =begin original | |
| 1773 | ||
| 1774 | On certain legacy systems with what could charitably be called terminally | |
| 1775 | convoluted (some would say broken) I/O models, a file isn't a file--at | |
| 1776 | least, not with respect to the C standard I/O library. On these old | |
| 1777 | systems whose libraries (but not kernels) distinguish between text and | |
| 1778 | binary streams, to get files to behave properly you'll have to bend over | |
| 1779 | backwards to avoid nasty problems. On such infelicitous systems, sockets | |
| 1780 | and pipes are already opened in binary mode, and there is currently no | |
| 1781 | way to turn that off. With files, you have more options. | |
| 1782 | ||
| 1783 | =end original | |
| 1784 | ||
| 1785 | 最終的に (壊れていると言われる) I/O モデルに巻き込まれると寛大にも | |
| 1786 | 呼ばれるある種の古いシステムでは、ファイルはファイルではありません -- | |
| 1787 | 少なくとも C 標準 I/O ライブラリという観点では。 | |
| 1788 | (カーネルではなく)ライブラリがテキストストリームとバイナリストリームを | |
| 1789 | 区別するような古いシステムでは、適切に振る舞うようにファイルを | |
| 1790 | 取得するためには、不愉快な問題を避けるために懸命な努力が必要です。 | |
| 1791 | このような不幸なシステムでは、ソケットとパイプは既にバイナリモードで | |
| 1792 | 開いていて、今のところこれをオフにする方法はありません。 | |
| 1793 | ファイルに対しては、もっと選択肢があります。 | |
| 1794 | ||
| 1795 | =begin original | |
| 1796 | ||
| 1797 | Another option is to use the C<binmode> function on the appropriate | |
| 1798 | handles before doing regular I/O on them: | |
| 1799 | ||
| 1800 | =end original | |
| 1801 | ||
| 1802 | もう一つの選択肢は、通常の I/O を行う前に、適切なファイルハンドルに | |
| 1803 | C<binmode> 関数を使うことです: | |
| 1804 | ||
| 1805 | binmode(STDIN); | |
| 1806 | binmode(STDOUT); | |
| 1807 | while (<STDIN>) { print } | |
| 1808 | ||
| 1809 | =begin original | |
| 1810 | ||
| 1811 | Passing C<sysopen> a non-standard flag option will also open the file in | |
| 1812 | binary mode on those systems that support it. This is the equivalent of | |
| 1813 | opening the file normally, then calling C<binmode> on the handle. | |
| 1814 | ||
| 1815 | =end original | |
| 1816 | ||
| 1817 | C<sysopen> に非標準フラグオプションを渡すことでも、そのような | |
| 1818 | システムでバイナリモードでファイルを開けます。 | |
| 1819 | これは、ファイルを普通に開いてから、ハンドルに対して C<binmode> を | |
| 1820 | 呼び出すのと等価です。 | |
| 1821 | ||
| 1822 | sysopen(BINDAT, "records.data", O_RDWR | O_BINARY) | |
| 1823 | || die "can't open records.data: $!"; | |
| 1824 | ||
| 1825 | =begin original | |
| 1826 | ||
| 1827 | Now you can use C<read> and C<print> on that handle without worrying | |
| 1828 | about the non-standard system I/O library breaking your data. It's not | |
| 1829 | a pretty picture, but then, legacy systems seldom are. CP/M will be | |
| 1830 | with us until the end of days, and after. | |
| 1831 | ||
| 1832 | =end original | |
| 1833 | ||
| 1834 | これで、非標準システム I/O ライブラリがデータを壊す心配なしに | |
| 1835 | ハンドルに対して C<read> と C<print> を使えるようになりました。 | |
| 1836 | これは美しい形ではありませんが、レガシーシステムとは大抵そういうものです。 | |
| 1837 | CP/M は世界が終わるまで(そしてその後も)我々と共にあるでしょう。 | |
| 1838 | ||
| 1839 | =begin original | |
| 1840 | ||
| 1841 | On systems with exotic I/O systems, it turns out that, astonishingly | |
| 1842 | enough, even unbuffered I/O using C<sysread> and C<syswrite> might do | |
| 1843 | sneaky data mutilation behind your back. | |
| 1844 | ||
| 1845 | =end original | |
| 1846 | ||
| 1847 | 風変わりな I/O システムを持つシステムでは、驚いたことに、 | |
| 1848 | C<sysread> や C<syswrite> を使ったバッファリングしない I/O でさえも | |
| 1849 | 背後でこっそりとデータ操作をすることがあります。 | |
| 1850 | ||
| 1851 | while (sysread(WHENCE, $buf, 1024)) { | |
| 1852 | syswrite(WHITHER, $buf, length($buf)); | |
| 1853 | } | |
| 1854 | ||
| 1855 | =begin original | |
| 1856 | ||
| 1857 | Depending on the vicissitudes of your runtime system, even these calls | |
| 1858 | may need C<binmode> or C<O_BINARY> first. Systems known to be free of | |
| 1859 | such difficulties include Unix, the Mac OS, Plan 9, and Inferno. | |
| 1860 | ||
| 1861 | =end original | |
| 1862 | ||
| 1863 | 実行させるシステムの紆余曲折具合によっては、これらのシステムコールですら | |
| 1864 | 最初に C<binmode> や C<O_BINARY> が必要かもしれません。 | |
| 1865 | このような問題がないと分かっているシステムには Unix, Mac OS, Plan 9, | |
| 1866 | Inferno などがあります。 | |
| 1867 | ||
| 1868 | =head2 File Locking | |
| 1869 | ||
| 1870 | (ファイルのロック) | |
| 1871 | ||
| 1872 | =begin original | |
| 1873 | ||
| 1874 | In a multitasking environment, you may need to be careful not to collide | |
| 1875 | with other processes who want to do I/O on the same files as you | |
| 1876 | are working on. You'll often need shared or exclusive locks | |
| 1877 | on files for reading and writing respectively. You might just | |
| 1878 | pretend that only exclusive locks exist. | |
| 1879 | ||
| 1880 | =end original | |
| 1881 | ||
| 1882 | マルチタスク環境では、あなたが触ろうとしているファイルと同じファイルを | |
| 1883 | 他のプロセスが衝突しないように気をつける必要があります。 | |
| 1884 | しばしば、ファイルを読み込みまたは書き込みするために、それぞれ | |
| 1885 | 共有ロックと排他ロックが必要になります。 | |
| 1886 | あるいは、単に排他ロックしかないような振りをするかもしれません。 | |
| 1887 | ||
| 1888 | =begin original | |
| 1889 | ||
| 1890 | Never use the existence of a file C<-e $file> as a locking indication, | |
| 1891 | because there is a race condition between the test for the existence of | |
| 1892 | the file and its creation. It's possible for another process to create | |
| 1893 | a file in the slice of time between your existence check and your attempt | |
| 1894 | to create the file. Atomicity is critical. | |
| 1895 | ||
| 1896 | =end original | |
| 1897 | ||
| 1898 | 決して、ファイルの存在 C<-e $file> をロック指示に使わないでください; | |
| 1899 | なぜならファイルの存在のテストとその作成の間に競合条件があるからです。 | |
| 1900 | 存在チェックとファイル作成のわずかな間に、他のプロセスがファイルを作る | |
| 1901 | 可能性があります。 | |
| 1902 | 原子性は危機的です。 | |
| 1903 | ||
| 1904 | =begin original | |
| 1905 | ||
| 1906 | Perl's most portable locking interface is via the C<flock> function, | |
| 1907 | whose simplicity is emulated on systems that don't directly support it | |
| 1908 | such as SysV or Windows. The underlying semantics may affect how | |
| 1909 | it all works, so you should learn how C<flock> is implemented on your | |
| 1910 | system's port of Perl. | |
| 1911 | ||
| 1912 | =end original | |
| 1913 | ||
| 1914 | Perl でのもっとも移植性のあるロックインターフェースは、 | |
| 1915 | C<flock> 関数によるものです; この単純さは、SysV や Windows のような、 | |
| 1916 | これに直接対応していないシステムでもエミュレートされています。 | |
| 1917 | 基礎となる動作はこれがどのように働くかに影響を与えるので、 | |
| 1918 | あなたが使うシステムの Perl で C<flock> がどのように実装されているかを | |
| 1919 | 学ぶべきです。 | |
| 1920 | ||
| 1921 | =begin original | |
| 1922 | ||
| 1923 | File locking I<does not> lock out another process that would like to | |
| 1924 | do I/O. A file lock only locks out others trying to get a lock, not | |
| 1925 | processes trying to do I/O. Because locks are advisory, if one process | |
| 1926 | uses locking and another doesn't, all bets are off. | |
| 1927 | ||
| 1928 | =end original | |
| 1929 | ||
| 1930 | ファイルロックは、他のプロセスが I/O 操作を行うことからロックするもの | |
| 1931 | I<ではありません>。 | |
| 1932 | ファイルロックは、他のプロセスの I/O 操作をロックするのではなく、他の | |
| 1933 | プロセスがロックを得ようとすることをロックします。 | |
| 1934 | ロックは勧告的なので、あるプロセスがロックを使っていても、他の | |
| 1935 | プロセスがロックを使っていなければ、全ては台無しになります。 | |
| 1936 | ||
| 1937 | =begin original | |
| 1938 | ||
| 1939 | By default, the C<flock> call will block until a lock is granted. | |
| 1940 | A request for a shared lock will be granted as soon as there is no | |
| 1941 | exclusive locker. A request for an exclusive lock will be granted as | |
| 1942 | soon as there is no locker of any kind. Locks are on file descriptors, | |
| 1943 | not file names. You can't lock a file until you open it, and you can't | |
| 1944 | hold on to a lock once the file has been closed. | |
| 1945 | ||
| 1946 | =end original | |
| 1947 | ||
| 1948 | デフォルトでは、C<flock> 呼び出しは、ロックが得られるまでブロックします。 | |
| 1949 | 共有ロック要求は、誰も排他ロックを持っていない状態になれば直ちに | |
| 1950 | 受け入れられます。 | |
| 1951 | 排他ロック要求は、誰もあらゆる種類のロックを守っていない状態になれば | |
| 1952 | 与えられます。 | |
| 1953 | ロックはファイル名に対してではなく、ファイル記述子について与えられます。 | |
| 1954 | ファイルを開かずにファイルをロックすることはできませんし、ファイルを閉じた | |
| 1955 | 後もロックを持ったままにすることもできません。 | |
| 1956 | ||
| 1957 | =begin original | |
| 1958 | ||
| 1959 | Here's how to get a blocking shared lock on a file, typically used | |
| 1960 | for reading: | |
| 1961 | ||
| 1962 | =end original | |
| 1963 | ||
| 1964 | 以下はファイルに対してブロックする共有ロックを得る方法で、 | |
| 1965 | 典型的には読み込み時に使われます: | |
| 1966 | ||
| 1967 | use 5.004; | |
| 1968 | use Fcntl qw(:DEFAULT :flock); | |
| 1969 | open(FH, "< filename") or die "can't open filename: $!"; | |
| 1970 | flock(FH, LOCK_SH) or die "can't lock filename: $!"; | |
| 1971 | # now read from FH | |
| 1972 | ||
| 1973 | =begin original | |
| 1974 | ||
| 1975 | You can get a non-blocking lock by using C<LOCK_NB>. | |
| 1976 | ||
| 1977 | =end original | |
| 1978 | ||
| 1979 | C<LOCK_NB> を使うことでブロックしないロックも得られます。 | |
| 1980 | ||
| 1981 | flock(FH, LOCK_SH | LOCK_NB) | |
| 1982 | or die "can't lock filename: $!"; | |
| 1983 | ||
| 1984 | =begin original | |
| 1985 | ||
| 1986 | This can be useful for producing more user-friendly behaviour by warning | |
| 1987 | if you're going to be blocking: | |
| 1988 | ||
| 1989 | =end original | |
| 1990 | ||
| 1991 | ブロックするときに警告することで、よりユーザーにやさしい振る舞いを | |
| 1992 | することは有用です: | |
| 1993 | ||
| 1994 | use 5.004; | |
| 1995 | use Fcntl qw(:DEFAULT :flock); | |
| 1996 | open(FH, "< filename") or die "can't open filename: $!"; | |
| 1997 | unless (flock(FH, LOCK_SH | LOCK_NB)) { | |
| 1998 | $| = 1; | |
| 1999 | print "Waiting for lock..."; | |
| 2000 | flock(FH, LOCK_SH) or die "can't lock filename: $!"; | |
| 2001 | print "got it.\n" | |
| 2002 | } | |
| 2003 | # now read from FH | |
| 2004 | ||
| 2005 | =begin original | |
| 2006 | ||
| 2007 | To get an exclusive lock, typically used for writing, you have to be | |
| 2008 | careful. We C<sysopen> the file so it can be locked before it gets | |
| 2009 | emptied. You can get a nonblocking version using C<LOCK_EX | LOCK_NB>. | |
| 2010 | ||
| 2011 | =end original | |
| 2012 | ||
| 2013 | (典型的には書き込みのために) 排他ロックを得るためには、慎重になる | |
| 2014 | 必要があります。 | |
| 2015 | 空なる前にロックするために、ファイルを C<sysopen> で開きます。 | |
| 2016 | C<LOCK_EX | LOCK_NB> を使った非ブロック版も得られます。 | |
| 2017 | ||
| 2018 | use 5.004; | |
| 2019 | use Fcntl qw(:DEFAULT :flock); | |
| 2020 | sysopen(FH, "filename", O_WRONLY | O_CREAT) | |
| 2021 | or die "can't open filename: $!"; | |
| 2022 | flock(FH, LOCK_EX) | |
| 2023 | or die "can't lock filename: $!"; | |
| 2024 | truncate(FH, 0) | |
| 2025 | or die "can't truncate filename: $!"; | |
| 2026 | # now write to FH | |
| 2027 | ||
| 2028 | =begin original | |
| 2029 | ||
| 2030 | Finally, due to the uncounted millions who cannot be dissuaded from | |
| 2031 | wasting cycles on useless vanity devices called hit counters, here's | |
| 2032 | how to increment a number in a file safely: | |
| 2033 | ||
| 2034 | =end original | |
| 2035 | ||
| 2036 | 最後に、アクセスカウンタと呼ばれる無駄で空虚な装置のために CPU パワーを | |
| 2037 | 無駄遣いすることから逃れられない無慮数百万のために、 | |
| 2038 | あるファイルの数値を安全に増加させる方法を以下に示します: | |
| 2039 | ||
| 2040 | use Fcntl qw(:DEFAULT :flock); | |
| 2041 | ||
| 2042 | sysopen(FH, "numfile", O_RDWR | O_CREAT) | |
| 2043 | or die "can't open numfile: $!"; | |
| 2044 | # autoflush FH | |
| 2045 | $ofh = select(FH); $| = 1; select ($ofh); | |
| 2046 | flock(FH, LOCK_EX) | |
| 2047 | or die "can't write-lock numfile: $!"; | |
| 2048 | ||
| 2049 | $num = <FH> || 0; | |
| 2050 | seek(FH, 0, 0) | |
| 2051 | or die "can't rewind numfile : $!"; | |
| 2052 | print FH $num+1, "\n" | |
| 2053 | or die "can't write numfile: $!"; | |
| 2054 | ||
| 2055 | truncate(FH, tell(FH)) | |
| 2056 | or die "can't truncate numfile: $!"; | |
| 2057 | close(FH) | |
| 2058 | or die "can't close numfile: $!"; | |
| 2059 | ||
| 2060 | =head2 IO Layers | |
| 2061 | ||
| 2062 | (IO 層) | |
| 2063 | ||
| 2064 | =begin original | |
| 2065 | ||
| 2066 | In Perl 5.8.0 a new I/O framework called "PerlIO" was introduced. | |
| 2067 | This is a new "plumbing" for all the I/O happening in Perl; for the | |
| 2068 | most part everything will work just as it did, but PerlIO also brought | |
| 2069 | in some new features such as the ability to think of I/O as "layers". | |
| 2070 | One I/O layer may in addition to just moving the data also do | |
| 2071 | transformations on the data. Such transformations may include | |
| 2072 | compression and decompression, encryption and decryption, and transforming | |
| 2073 | between various character encodings. | |
| 2074 | ||
| 2075 | =end original | |
| 2076 | ||
| 2077 | Perl 5.8.0 で、"PerlIO" と呼ばれる新しい I/O フレームワークが | |
| 2078 | 導入されました。 | |
| 2079 | これは Perl で発生する全ての I/O のための新しい「配管」です; | |
| 2080 | ほとんど全ての部分では単に今まで通りに動作しますが、 | |
| 2081 | I/O を「層」として考えるための機能のような、新しい要素も導入されています。 | |
| 2082 | ある I/O 層は単にデータを移動させるだけでなく、データを変換するかも知れません。 | |
| 2083 | このような変換には、圧縮と展開、暗号化と復号化、様々な文字エンコーディング間の | |
| 2084 | 変換を含むかも知れません。 | |
| 2085 | ||
| 2086 | =begin original | |
| 2087 | ||
| 2088 | Full discussion about the features of PerlIO is out of scope for this | |
| 2089 | tutorial, but here is how to recognize the layers being used: | |
| 2090 | ||
| 2091 | =end original | |
| 2092 | ||
| 2093 | PerlIO の機能に関する完全な議論はこのチュートリアルの対象外ですが、 | |
| 2094 | 層が使われていることをどうやって認識するかを以下に示します: | |
| 2095 | ||
| 2096 | =over 4 | |
| 2097 | ||
| 2098 | =item * | |
| 2099 | ||
| 2100 | =begin original | |
| 2101 | ||
| 2102 | The three-(or more)-argument form of C<open> is being used and the | |
| 2103 | second argument contains something else in addition to the usual | |
| 2104 | C<< '<' >>, C<< '>' >>, C<< '>>' >>, C<< '|' >> and their variants, | |
| 2105 | for example: | |
| 2106 | ||
| 2107 | =end original | |
| 2108 | ||
| 2109 | 3 (以上) 引数形式の C<open> が使われ、2 番目の引数に通常の | |
| 2110 | C<< '<' >>, C<< '>' >>, C<< '>>' >>, C<< '|' >> およびそのバリエーション | |
| 2111 | 以外の何かが含まれている場合; 例えば: | |
| 2112 | ||
| 2113 | open(my $fh, "<:crlf", $fn); | |
| 2114 | ||
| 2115 | =item * | |
| 2116 | ||
| 2117 | =begin original | |
| 2118 | ||
| 2119 | The two-argument form of C<binmode> is being used, for example | |
| 2120 | ||
| 2121 | =end original | |
| 2122 | ||
| 2123 | 2 引数形式の C<binmode> が使われている場合; 例えば | |
| 2124 | ||
| 2125 | binmode($fh, ":encoding(utf16)"); | |
| 2126 | ||
| 2127 | =back | |
| 2128 | ||
| 2129 | =begin original | |
| 2130 | ||
| 2131 | For more detailed discussion about PerlIO see L<PerlIO>; | |
| 2132 | for more detailed discussion about Unicode and I/O see L<perluniintro>. | |
| 2133 | ||
| 2134 | =end original | |
| 2135 | ||
| 2136 | PerlIO に関するより詳細な議論については L<PerlIO> を参照してください; | |
| 2137 | Unicode と I/O に関するより詳細な議論については L<perluniintro> を | |
| 2138 | 参照してください。 | |
| 2139 | ||
| 2140 | =head1 SEE ALSO | |
| 2141 | ||
| 2142 | =begin original | |
| 2143 | ||
| 2144 | The C<open> and C<sysopen> functions in perlfunc(1); | |
| 2145 | the system open(2), dup(2), fopen(3), and fdopen(3) manpages; | |
| 2146 | the POSIX documentation. | |
| 2147 | ||
| 2148 | =end original | |
| 2149 | ||
| 2150 | perlfunc(1) の C<open> 及び C<sysopen> 関数; | |
| 2151 | システムの open(2), dup(2), fopen(3), fdopen(3) の man ページ; | |
| 2152 | POSIX 文書。 | |
| 2153 | ||
| 2154 | 867 | =head1 AUTHOR and COPYRIGHT |
| 2155 | 868 | |
| 2156 | Copyright 1 | |
| 869 | Copyright 2013 Tom Christiansen; now maintained by Perl5 Porters | |
| 2157 | 870 | |
| 2158 | This documentation is free; you can redistribute it and/or modify it | |
| 871 | This documentation is free; you can redistribute it and/or modify it under | |
| 2159 | ||
| 872 | the same terms as Perl itself. | |
| 2160 | 873 | |
| 2161 | Irrespective of its distribution, all code examples in these files are | |
| 2162 | hereby placed into the public domain. You are permitted and | |
| 2163 | encouraged to use this code in your own programs for fun or for profit | |
| 2164 | as you see fit. A simple comment in the code giving credit would be | |
| 2165 | courteous but is not required. | |
| 2166 | ||
| 2167 | =head1 HISTORY | |
| 2168 | ||
| 2169 | First release: Sat Jan 9 08:09:11 MST 1999 | |
| 2170 | ||
| 2171 | 874 | =begin meta |
| 2172 | 875 | |
| 2173 | Translate: SHIRAK | |
| 876 | Translate: SHIRAKTA Kentaro <argrath@ub32.org> | |
| 2174 | 877 | Status: completed |
| 2175 | 878 | |
| 2176 | 879 | =end meta |