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

I think you have some bad information on how Perl programs run. I also think you have some bad information on how input and output work. You might want to read up a bit on how those things happen, and you might want to share that information with whoever told you those things.

The first set of Perl code here, for example, autoflushes the output buffer after every print, does a substitution, prints, sleeps for a second, then starts with the next line. The second instance of perl just prints everything it gets from STDIN as soon as it gets it.

perl -ne '$|++; s/print/say/; print $_; sleep 1;' ffi/test.input | per +l -pe ''

On Linux you actually have some OS-supported options like Unix sockets, named pipes, and such. You can open a file for writing by one process and for reading by another, and the data goes from the one to the other.

Perl fully handles reading and writing lines of output in other than batch-style modes, though, as it's a general purpose programming language.

Replies are listed 'Best First'.
Re^4: parsing the results of the subroutine in real time
by doar4forum (Initiate) on Apr 04, 2008 at 22:43 UTC
    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);

      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.
      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.