Read quicker than a line per time from a file handle

  • Thread starter Carlo Filippini
  • Start date
C

Carlo Filippini

Hi
I try to measure how fast an ftp download is going on the
fly. I do something like:

$|=1;
my $count =0;
open (CMD, "ftp.script 2>&1 |") or die "Can't execute: $!";
while (<CMD>){
print "Result: $_ \n";
if ($_ =~ /\#/) {
$count ++;

}

The problem is that the 'ftp.script' returns me the hashes, but all
toghter. So basically $count is only 1 after execution. In other words
the output that I get is:
Result: Connected to... bla bla
Result: Downloading ...
Result: ######################################################
Result: Download complete

What I want is:
Result: Connected to... bla bla
Result: Downloading ...
Result: #
Result: #
Result: #
Result: #
Result: #
Result: #
Result: #
....
Result: Download complete


I have tried $|=1; but it does not help. The original script does
write the hashes one by one to STDOUT. Is there any way to force
'ftp.script' to pass me the hashes one by one? I use a windows
machine.

Thanks
Carlo
 
B

Boyd

Carlo said:
Hi
I try to measure how fast an ftp download is going on the
fly. I do something like:

$|=1;
my $count =0;
open (CMD, "ftp.script 2>&1 |") or die "Can't execute: $!";
while (<CMD>){
print "Result: $_ \n";
if ($_ =~ /\#/) {
$count ++;

}

The problem is that the 'ftp.script' returns me the hashes, but all
toghter. So basically $count is only 1 after execution. In other words
the output that I get is:
Result: Connected to... bla bla
Result: Downloading ...
Result: ######################################################
Result: Download complete

What I want is:
Result: Connected to... bla bla
Result: Downloading ...
Result: #
Result: #
Result: #
Result: #
Result: #
Result: #
Result: #
....
Result: Download complete


I have tried $|=1; but it does not help. The original script does
write the hashes one by one to STDOUT. Is there any way to force
'ftp.script' to pass me the hashes one by one? I use a windows
machine.

Thanks
Carlo
I sent answer directly to Carlo since my IPS's news server quit posting.
Boyd
 
K

Kris Wempa

Why don't you try using the read() function and only read 1 char at a time ?
This is how CGI programs read from STDIN. I hope that helps.

Kris
 
B

Boyd

Carlo said:
Hi
I try to measure how fast an ftp download is going on the
fly. I do something like:

$|=1;
my $count =0;
open (CMD, "ftp.script 2>&1 |") or die "Can't execute: $!";
while (<CMD>){
print "Result: $_ \n";
if ($_ =~ /\#/) {
$count ++;

}
Thanks
Carlo

I suspect the ftp.script output is not putting end-of-line characters at
the end of lines, so you may have to use the perl read command instead
of <CMD>. For example, you put (reference: perldoc -f read)
read CMD,$char,1
if you want to read one character at a time into the scalar $char. And
then you can test for '#' as you were doing. So you can try (I haven't
tested this):
$|=1;
my $count =0;
my $char;
open (CMD, "ftp.script 2>&1 |") or die "Can't execute: $!";
print "Result: ";
while (read CMD,$char,1){
print $char;
if ($char =~ /\#/) {
$count ++;
print "\nResult:";
}
if ($char =~ /\r|\n/) {
print "\nResult: "
} # for Windows, this may give extra blank lines
}

Boyd
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,280
Latest member
BGBBrock56

Latest Threads

Top