You are not authenticated, login.
text: sort by
tags: modified
type: chronology
hide / / print
ref: work-0 tags: perl fork read lines external program date: 06-15-2010 18:08 gmt revision:0 [head]

Say you have a program, called from a perl script, that may run for a long time. Get at the program's output as it appears?

Simple - open a pipe to the programs STDOUT. See http://docstore.mik.ua/orelly/perl/prog3/ch16_03.htm Below is an example - I wanted to see the output of programs run, for convenience, from a perl script (didn't want to have to remember - or get wrong - all the command line arguments for each).


$numArgs = $#ARGV + 1;
if($numArgs == 1){
	if($ARGV[0] eq "table"){
		open STATUS, "sudo ./video 0xc1e9 15 4600 4601 0 |";
			print ; 
		close STATUS ; 
	}elsif($ARGV[0] eq "arm"){
		open STATUS, "sudo ./video 0x1ff6 60 4597 4594 4592 |";
			print ; 
		close STATUS ; 
	}else{ print "$ARGV[0] not understood - say arm or table!\n"; 

hide / / print
ref: notes-0 tags: ocaml run external command stdin date: 09-10-2008 19:32 gmt revision:1 [0] [head]

It is not obvious how to run an external command in ocaml & get it's output from stdin. Here is my hack, which simply polls the output of the program until there is nothing left to read. Not very highly tested, but I wanted to share, as I don't think there is an example of the same on pleac

let run_command cmd = 
	let inch = Unix.open_process_in cmd in
	let infd = Unix.descr_of_in_channel inch in
	let buf = String.create 20000 in
	let il = ref 1 in
	let offset = ref 0 in
	while !il > 0 do (
		let inlen = Unix.read infd buf !offset (20000- !offset) in
		il := inlen ; 
		offset := !offset + inlen;
	) done; 
	ignore(Unix.close_process_in inch);  
	if !offset = 0 then "" else String.sub buf 0 !offset

Note: Fixed a nasty string-termination/memory-reuse bug Sept 10 2008