vendion has asked for the wisdom of the Perl Monks concerning the following question:

Hello monks, I am trying to use Net::SFTP to grab a couple of files off a remote server and show the progress in percent downloaded, but what my script displays is incorrect.

Here is basically what I am trying to due:

#!/usr/bin/perl use strict; use warnings; use Net::SFTP; my %args = ( user => 'me', password => 'xxxxxxxxxx' ); my $sftp = Net::SFTP->new( 'ftp.example.com', %args ) || die 'Could no +t connect to the server: ' . $! . "\n"; $sftp->get( 'file1.txt', 'file1.txt', \&callback ) || die 'Could not f +etch remote file: ' . $! . "\n"; sub callback { my ( $sftp, $data, $offset, $size ) = @_; my $percent = ($offset/$size)*100; print $percent . ' '; return; }

The output of the print is:

0 409600/333487 819200/333487 1228800/333487 1638400/333487 2048000/333487 2457600/333487 409600/47641 3276800/333487 3686400/333487 4096000/333487 409600/30317 4915200/333487 5324800/333487 819200/47641 6144000/333487 6553600/333487 6963200/333487 7372800/333487 7782400/333487 8192000/333487 1228800/47641 819200/30317 9420800/333487 9830400/333487 10240000/333487 10649600/333487 11059200/333487 1638400/47641 11878400/333487 12288000/333487 12697600/333487 13107200/333487 1228800/30317 13926400/333487 2048000/47641 14745600/333487 15155200/333487 15564800/333487 15974400/333487 16384000/333487 16793600/333487 2457600/47641 17612800/333487 1638400/30317 18432000/333487 18841600/333487 19251200/333487 19660800/333487 2867200/47641 20480000/333487 20889600/333487 21299200/333487 21708800/333487 22118400/333487 2048000/30317 3276800/47641 23347200/333487 23756800/333487 24166400/333487 24576000/333487 409600/5467 25395200/333487 3686400/47641 26214400/333487 26624000/333487 2457600/30317 27443200/333487 27852800/333487 28262400/333487 4096000/47641 409600/4697 29491200/333487 29900800/333487 30310400/333487 30720000/333487 31129600/333487 409600/4331 31948800/333487 32358400/333487 32768000/333487 33177600/333487
The offset is getting multiplied by 100 but the division isn't happening

Replies are listed 'Best First'.
Re: Strange math issue with Net::SFTP (Pari)
by tye (Sage) on May 29, 2012 at 16:36 UTC

      So if the root of my problem is caused by Math::Pari then I should be able to convert it to a number and then calculate the percentage. I had trying messing with Math::Pari some the other day but had no such luck... I will keep trying.

        Ok so after changing my callback sub to this

        sub callback { my ( $sftp, $data, $offset, $size ) = @_; use Math::Pari qw( :DEFAULT PARI pari2num ); print 'offset is a: '; print ref $offset; print "\n"; $offset = pari2num($offset); print 'now offset is a: '; print ref $offset; print "\n"; print 'size is a: '; print ref $size; print "\n"; $size = pari2num($size); print 'now size is a: '; print ref $size; print "\n"; my $percent = ($offset/$size) * 100; print "percent=$percent\n"; return; }

        Things now start to make sense to me in the output:
        offset is a: now offset is a: offset=0 size is a: Math::Pari size is a: size=666974 percent=0 offset is a: now offset is a: offset=8192 size is a: Math::Pari size is a: size=666974 percent=1.22823378422547 offset is a: now offset is a: offset=16384 size is a: Math::Pari size is a: size=666974 percent=2.45646756845094

Re: Strange math issue with Net::SFTP
by salva (Canon) on May 29, 2012 at 18:36 UTC
Re: Strange math issue with Net::SFTP
by toolic (Bishop) on May 29, 2012 at 15:23 UTC
    For debug purposes, replace your sub with:
    sub callback { my ( $sftp, $data, $offset, $size ) = @_; my $percent = ($offset/$size)*100; print "offset=$offset\n"; print "size=$size\n"; print "percent=$percent\n"; }
    Then post your results.

      After making the change to my callback sub here is the output:


      Side note anyone know why the readmore tag does not preserve newlines like the code tag?

        Side note anyone know why the readmore tag does not preserve newlines like the code tag?
        readmore is just for hiding. You can nest code tags inside readmore:
        <readmore> <code> </code> </readmore>
Re: Strange math issue with Net::SFTP
by zentara (Cardinal) on May 29, 2012 at 20:34 UTC