Read quicker than a line per time from a file handle

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Carlo Filippini

    Read quicker than a line per time from a file handle

    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
  • Boyd

    #2
    Re: Read quicker than a line per time from a file handle

    Carlo Filippini wrote:[color=blue]
    > 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[/color]
    I sent answer directly to Carlo since my IPS's news server quit posting.
    Boyd


    Comment

    • Boyd

      #3
      Re: Read quicker than a line per time from a file handle

      Carlo Filippini wrote:[color=blue]
      > 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 ++;
      >
      > }[/color]
      [color=blue]
      >
      > Thanks
      > Carlo[/color]

      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


      Comment

      • Kris Wempa

        #4
        Re: Read quicker than a line per time from a file handle

        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" <carlo.filippin i@t-mobile.co.uk> wrote in message
        news:b0b268d8.0 310020501.261eb 45f@posting.goo gle.com...[color=blue]
        > 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[/color]


        Comment

        Working...