Read quicker than a line per time from a file handle

Discussion in 'Perl' started by Carlo Filippini, Oct 2, 2003.

  1. 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
    Carlo Filippini, Oct 2, 2003
    #1
    1. Advertising

  2. Carlo Filippini

    Boyd Guest

    Carlo Filippini wrote:
    > 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
    Boyd, Oct 2, 2003
    #2
    1. Advertising

  3. Carlo Filippini

    Kris Wempa Guest

    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

    "Carlo Filippini" <> wrote in message
    news:...
    > 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
    Kris Wempa, Oct 2, 2003
    #3
  4. Carlo Filippini

    Boyd Guest

    Carlo Filippini wrote:
    > 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
    Boyd, Oct 2, 2003
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tarren
    Replies:
    2
    Views:
    10,217
    Brock Allen
    Sep 29, 2005
  2. Ben_
    Replies:
    12
    Views:
    625
    xarax
    Nov 26, 2003
  3. Hugo
    Replies:
    10
    Views:
    1,289
    Matt Humphrey
    Oct 18, 2004
  4. Orgil
    Replies:
    0
    Views:
    87
    Orgil
    May 21, 2007
  5. Orgil
    Replies:
    0
    Views:
    96
    Orgil
    May 21, 2007
Loading...

Share This Page