Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

There should be no need to add delays in between outputting the characters.  As I tried to explain, the OS takes care of storing echo's output until it has been read. It shouldn't matter if the consuming side takes a while to get done; the OS will happily keep the data in the fridge...

If you still wanted to do what you were trying above, you'd need to add a pair of parentheses around the echos and sleeps (aka subshell), so the collective output would be piped into phylib.  As you have it, only the last echo "r" would be piped.

Compare the following  (I replaced your phylib with a bit of Perl code that also reads 3 chars):

$ echo abc | perl -e'for (1..3) {print "Q$_ -> "; read STDIN,$r,1; pri +nt "$r\n"; sleep 2}' Q1 -> a Q2 -> b Q3 -> c

Here, the reading side is slow, but as you can see, every answer still ends up where expected.

The net effect of putting the delays on the other side (in between the echos), will superficially behave the same, but the difference is that the reading end will now have to wait until data becomes available:

$ (echo -n a; sleep 2; echo -n b; sleep 2; echo -n c) | perl -e'for (1 +..3) {print "Q$_ -> "; read STDIN,$r,1; print "$r\n";}' Q1 -> a Q2 -> b Q3 -> c

I.e., in both cases, there will be 2 sec intervals between outputting Q/A's.

As for phylib aborting after the first input... are you sure it reads char-by-char, or does it maybe read lines (i.e. characters up until a newline)? In the latter case, you'd need to have echo output \n in between characters, or else the entire "rYr" would be consumed by the first read...

P.S.: echo -e interpolates escapes (like "\n"), and echo -n suppresses outputting of a trailing newline (which you don't want in case the program is reading char-by-char).   (Update: at least that's what the bash builtin echo, and /bin/echo on Linux does — unfortunately, different versions of echo are notoriously known for their incompatibilities wrt behavior and options...)


In reply to Re^5: How to execute external programs from perl script by Anonyrnous Monk
in thread How to execute external programs from perl script by chak9988

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2024-04-20 02:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found