Re: How do I output an array twice via pipe instead of only once?





On Jan 30, 6:00 pm, "Paul Lalli" <mri...@xxxxxxxxx> wrote:
On Jan 30, 12:55 pm, "Paul Lalli" <mri...@xxxxxxxxx> wrote:

On Jan 30, 12:13 pm, junk2j...@xxxxxxxxxxx wrote:
I have a script that outputs an array which gets used as input in a
second script. The problem is that if I want to send over a second
array, my script sends them both over at the same time. It seems like
perl is trying to be too efficient. Here are the simplified scripts:

#!/usr/bin/perl
#
# a.pl
#

open(OUTPUT,"|b.pl");

@array1=("A","B");
print OUTPUT @array1;

@array2=("C","D");
print OUTPUT @array2;

---------------------------------------------------

#!/usr/bin/perl
#
# b.pl
#

@input=<STDIN>;

print "here is the inputted array: @input \n";

-----------------------------------------------------

What the script displays is:
here is the inputted array: ABCD

-----------------------------------------------------

But what I want is:
here is the inputted array: AB
here is the inputted array: CD

You've got your problem backwards. It is not the fault of what's
being sent, but of what's being read. When you use the standard
<STDIN> operation, you are telling Perl to read until the next
newline. You did not send any newlines in the first send, so Perl
keeps waiting until it does receive a newline (or it receives end-of-
file, which happens when a.pl closes, thereby closing the pipe to
b.pl).

Either change your read to only read two characters at a time (see
perldoc -f read), or change your printing to send newlines after each
send.

Taking another look at what you said you wanted, the above is only
half of the answer. You're evaluating <STDIN> in list context,
meaning that it's going to read the entire stream, at once, into an
array. It waits for end-of-file regardless. The individual elements
of the array will be the newline-terminated strings sent by the first
program. If you want the actual message of "here is the inputted
array" to be printed once for each send, you need to make multiple
reads, usually in a loop. Change a.pl to print newlines after each
send:

@array1=("A","B");
print OUTPUT @array1, "\n";
@array2=("C","D");
print OUTPUT @array2, "\n";

and change b.pl to continuously read and print until there is no more
data:

while (my $input = <STDIN> ) {
print "Inputted string: $input";

}

For completeness I should mention that, on a POSIX-like OS, so long as
each print() is printing less than POSIX::PIPE_BUF characters you
could put OUTPUT into autoflush mode and then use sysread() (with a
buffer that's larger than the largest record you expect) in b.pl.

This is because (as long as you never write more than PIPE_BUF
characters at a time) it is legitimate under POSIX treat a FIFO as a
packet stream rather than a byte stream.

I still think that printing newlines is the better solution most of
the time. The packet stream buisness really comes into it's own when
there are multiple writers on the FIFO.

I do not know how FIFOs behave on non-POSIX-like OSs but I suspect
this behaviour is not portable because I get...

C:\>perl -MPOSIX -e" print POSIX::PIPE_BUF"
Your vendor has not defined POSIX macro PIPE_BUF, used at -e line 1

.



Relevant Pages

  • Re: How do I output an array twice via pipe instead of only once?
    ... array, my script sends them both over at the same time. ... print OUTPUT @array1; ... You did not send any newlines in the first send, ...
    (perl.beginners)
  • Re: Logon script - function array and select case not working
    ... this all works well, except, the function i am using for the rules in the control script causes alot of querrys to AD. as there are alot of groups. ... objTSout.writeline retrv ... So if you think that this will assign an array value to the variable, how do you think the case select statement is going go compare this array value with the literal string values such as "group name here"? ... However, by not assigning ANY value to checkgrp in the function, you are guaranteeing that, should the function ever exit, it will return no information. ...
    (microsoft.public.scripting.vbscript)
  • Re: string retrieval issue
    ... Chicago Bears|NFC North ... not writing the third element back to the array). ... You didn't include it in your script. ... Fear is the mind-killer. ...
    (comp.lang.perl.misc)
  • Re: even rows for checkbox forms with no splitting of boxes from values
    ... 'mod' on the array length with a denominator equal to the width of the row, ... State saving really depends on what the script is already doing. ... So I usually roll my own checkboxes. ... #this is to create an array with 25-50 strings 2-10 in length ...
    (comp.infosystems.www.authoring.cgi)
  • Re: settimeout needs alert() ???
    ... function slider { ... and use script to replace the src and title attributes. ... are downloaded completely. ... The usual strategy is to load all of the images in to an array of image ...
    (comp.lang.javascript)