in reply to Re^3: parsing the results of the subroutine in real time
in thread parsing the results of the subroutine in real time

Hi, Maybe i was not clear so....my apologies... Lets say i have the code lines below: &executable(in,out); &process_executable(out); The script will run first the executable subrotuine and when the process finishes it will execute the process_executable subrotine which takes the output of the executable. What i want its to know how can i simultaneously start the executable subrotuine and not wait for the output but instead read the output as it is written....something like: &executable(in,out) && &process_executable(out);
  • Comment on Re^4: parsing the results of the subroutine in real time

Replies are listed 'Best First'.
Re^5: parsing the results of the subroutine in real time
by BrowserUk (Patriarch) on Apr 04, 2008 at 23:35 UTC

    Something like?:

    my $pid = open my $out, 'theExe infile | tee outfile' or die $!; while( <$out> ) { processOutputLine( $_ ); }

    You can omit the tee if you don't need a copy of the output in a file afterwards. Or you could just print a copy from within the while loop if you do.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re^5: parsing the results of the subroutine in real time
by mr_mischief (Monsignor) on Apr 04, 2008 at 23:05 UTC
    You have several options, then.

    You can use two separate programs. With those, you can use an anonymous pipe on the command line. You could also use a named pipe or a Unix socket. You could even use Berkeley sockets (the canonical implementation of a TCP/IP API).

    You can use threads or fork a new process and use a producer/consumer model. You can then use a pipe, sockets, or shared memory. In the case of threads you also have other options.

    You could have one program open another via a piped open call or use IPC::Open2 (or IPC::Open3, if needed).

    An alternative is to process your input in chunks and to process your output in chunks, then put your subroutines in a loop. Here's one example of that: while ( <> ) { output( input( $_ ) ) } It really matters what type of data you're dealing with whether or not this would work.