Buffering piped output of a script to another script...

This is a multipart message in MIME format.
--===============0192331505==
Content-Type: multipart/alternative;
boundary="=_alternative 0072FB14852574B2_="

This is a multipart message in MIME format.
--=_alternative 0072FB14852574B2_=
Content-Type: text/plain; charset="US-ASCII"

Is it possible to buffer the data coming in ($_) based on size rather than
waiting for a newline?

Here is a test scenario:

#script1.pl
while(1){
print "Sent: " . localtime() . "\n";
}

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

#script2.pl:
print "Received: $_";

Running the above scripts using: "perl script1.pl | perl -n script2.pl"
works fine (as expected). However I wonder if it is possible to remove
the "\n" from the print statement in script1.pl and let script2.pl handle
the buffering, by this I mean process the print "Received: $_"; line only
when $_ contains x number of characters, bytes, etc...

Note: Target platform is Win32.

Thanks,
Brett Carroll
--=_alternative 0072FB14852574B2_=
Content-Type: text/html; charset="US-ASCII"


<br><font size=2 face="sans-serif">Is it possible to buffer the data coming
in ($_) based on size rather than waiting for a newline?</font>
<br>
<br><font size=2 face="sans-serif">Here is a test scenario:</font>
<br>
<br><font size=2 face="sans-serif">#script1.pl</font>
<br><font size=2 face="sans-serif">while(1){</font>
<br><font size=2 face="sans-serif">print "Sent: " . localtime()
.. "\n";</font>
<br><font size=2 face="sans-serif">}</font>
<br>
<br><font size=2 face="sans-serif">------------------------------</font>
<br>
<br><font size=2 face="sans-serif">#script2.pl:</font>
<br><font size=2 face="sans-serif">print "Received: $_";</font>
<br>
<br><font size=2 face="sans-serif">Running the above scripts using: "perl
script1.pl | perl -n script2.pl" works fine (as expected).  However
I wonder if it is possible to remove the "\n" from the print
statement in script1.pl and let script2.pl handle the buffering, by this
I mean process the print "Received: $_"; line only when $_ contains
x number of characters, bytes, etc...</font>
<br>
<br><font size=2 face="sans-serif">Note: Target platform is Win32.</font>
<br>
<br><font size=2 face="sans-serif">Thanks,<br>
Brett Carroll</font>
--=_alternative 0072FB14852574B2_=--

--===============0192331505==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
ActivePerl mailing list
ActivePerl [at] listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
--===============0192331505==--
Brett.Carroll [ Mi, 27 August 2008 22:55 ] [ ID #1965871 ]

Re: Buffering piped output of a script to another script...

Brett.Carroll [at] bpd.treas.gov wrote:
>
> Is it possible to buffer the data coming in ($_) based on size rather
> than waiting for a newline?
>
> Here is a test scenario:
>
> #script1.pl
> while(1){
> print "Sent: " . localtime() . "\n";
> }

$| = 1;
while (1) {
print "Sent: " . localtime ();
sleep 1;
}

> ------------------------------
>
> #script2.pl:
> print "Received: $_";

This might work for you, but you really need to know where to put the
newlines other than checking for Sent:

print "Received: ";
while (1) {
my $buf;
sysread STDIN, $buf, 128;
print $buf;
print "\nReceived: " if $buf =~ /^Sent/;
}

> Running the above scripts using: "perl script1.pl | perl -n script2.pl"
> works fine (as expected). However I wonder if it is possible to remove
> the "\n" from the print statement in script1.pl and let script2.pl
> handle the buffering, by this I mean process the print "Received: $_";
> line only when $_ contains x number of characters, bytes, etc...
>
> Note: Target platform is Win32.
_______________________________________________
ActivePerl mailing list
ActivePerl [at] listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Bill Luebkert [ Do, 28 August 2008 02:47 ] [ ID #1966039 ]
Perl » gmane.comp.lang.perl.active-perl » Buffering piped output of a script to another script...

Vorheriges Thema: ActiveState releases ActivePerl 5.8.8.824 and ActivePerl 5.10.0.1004
Nächstes Thema: help with Win32::TieRegistry