多くの行を含むテキスト ファイルがあります。このファイルを行ごとに処理し、行を出力する簡単な OCaml プログラムを書きたいと思います。
このプログラムを作成するために、最初に行数の少ない小さなファイルを作成しました。これにより、プログラムの実行がより速く終了します。
$ wc -l input/master
214745 input/master
$ head -50 input/master > input/small-master
filter.ml
これが私が書いた簡単なボイラープレートプログラムです:
open Core.Std;;
open Printf;;
open Core.In_channel;;
if Array.length Sys.argv >= 2 then begin
let rec process_lines ?ix master_file =
let ix = match ix with
| None -> 0
| Some x -> x
in
match input_line master_file with
| Some line -> (
if ix > 9 then printf "%d == %s\n" ix line;
process_lines ~ix:(ix+1) master_file
)
| None -> close master_file
in
let master_file = create Sys.argv.(1) in
process_lines master_file
end
入力ファイルの場所をコマンド ライン引数として取り、このファイルを読み取るためのファイル ハンドルを作成し、process_lines
このファイル ハンドルを引数として再帰関数を呼び出します。
process_lines
オプションの引数ix
を使用して、ファイルハンドルから行ごとに読み取るときに行番号をカウントします。process_lines は、 から読み取った行をfile_handle
標準出力に出力するだけです。
次に、小さな入力ファイルでプログラムを実行し、出力を Linuxhead
コマンドにパイプすると、すべて正常に動作します。
$ ./filter.native input/small-master |head -2
10 == 1000032|BINCH JAMES G|4|2012-11-13|edgar/data/1000032/0001181431-12-058269.txt
11 == 1000032|BINCH JAMES G|4|2012-12-03|edgar/data/1000032/0001181431-12-061825.txt
そして、より大きなファイルでプログラムを実行すると、壊れたパイプのエラーが表示されます。
$ ./filter.native input/master |head -2
10 == 1000032|BINCH JAMES G|4|2012-11-13|edgar/data/1000032/0001181431-12-058269.txt
11 == 1000032|BINCH JAMES G|4|2012-12-03|edgar/data/1000032/0001181431-12-061825.txt
Fatal error: exception Sys_error("Broken pipe")
Raised by primitive operation at file "pervasives.ml", line 264, characters 2-40
Called from file "printf.ml", line 615, characters 15-25
Called from file "find.ml", line 13, characters 21-48
Called from file "find.ml", line 19, characters 2-27
このような壊れたパイプ エラーは、パイプhead
のライター (この場合は私の OCaml プログラム) が書き込みを完了する前に、パイプのリーダー (この場合はコマンド) が終了すると発生することを学びました。tail
コマンドをリーダーとして使用した場合、そのようなエラーが発生しないのはそのためです。
しかし、ファイルの行数が少ないのに、パイプの破損エラーが発生しなかったのはなぜですか?