Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] GC and file descriptors
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: David Fox <david.fox@l...>
Subject: Re: [Caml-list] GC and file descriptors
I might start with this

let for_process_output f cmd =
   let r = Unix.open_process_in cmd in
   let result = f r in
   Unix.close_process_in r;
   result

then

let tput x = for_process_output (fun r -> input r buf 0 8) ("tput " ^ x)

at which point you start thinking about exception handling...

Dustin Sallings wrote:

>
>     I keep ending up with somewhat awkward structures to close files 
> I've opened.  Does the GC take care of any of this type of thing?
>
> I.e., could the following:
>
> let tput x =
>     let r = Unix.open_process_in ("tput " ^ x)
>     and buf = String.create 8 in
>     let len = input r buf 0 8 in
>     close_in r;
>     String.sub buf 0 len
> ;;
>
> be written like this:
>
> let tput x =
>     let buf = String.create 8 in
>     String.sub buf 0 (input (Unix.open_process_in ("tput " ^ x)) buf 0 8)
> ;;
>
> safely?
>

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners