Jonathan has asked for the wisdom of the Perl Monks concerning the following question:

I am currently rewriting my Korn shell scripts in perl (who said an old dog can't learn new tricks) and would like to know the best way to re-write ksh co-process handling.
For example how could the following example be best re-written?
# Open co-process co_process_program |& while read -p output do if ((input_required == true)) then input="just do some work"; print -p $input; fi if [[ $output = +(*done*) ]] then input="do something else"; print -p $input; fi if [[ $output = +(*finished*) ]] then input="time to go"; print -p $input; break; fi done

Replies are listed 'Best First'.
Re: co-processes
by ZZamboni (Curate) on May 05, 2000 at 20:32 UTC
    I'm not sure what you mean by "co-process handling". It seems to me that you are executing a command and reading its output. Is that it? In that case, one way of doing it with perl would be:
    open(CMD, "co_process_program |") or die "Error: $!\n"; while(<CMD>) { # each line of input will be in $_ } close(CMD);
RE: co-processes
by Jonathan (Curate) on May 07, 2000 at 06:32 UTC
    No, theres a lot more to it than that. The example was a very stripped down version.
    Perhaps a better one is to consider an interactive SQL program. Your script wants to run some sql, get back the number of rows processed and then decide whether the transaction should be commited or rollbacked. (Note this is only an example - anyone who mentions DBI will be shot ;-)