Totally untested.
use strict; use warnings; BEGIN { package Handle::ReadProgress; use Symbol qw( gensym ); use Tie::Handle qw( ); our @ISA = 'Tie::Handle'; sub wrap { my $outer_fh = gensym(); tie *$outer_fh, @_; return $outer_fh; } sub new { my ($class, $inner_fh, $callback) = @_; return bless({ inner_fh => $inner_fh, callback => $callback, progress => 0, }, $class); } sub READ { my ($self) = @_; my $bytes_read = read($self->{inner_fh}, $_[1], $_[2], $_[3]); if (defined($bytes_read)) { my $progress = ( $self->{progress} += $bytes_read ); $self->{callback}->($outer_fh, $progress); } return $bytes_read; } sub READLINE { my ($self) = @_; my $line = readline($self->{inner_fh}); if (defined($line)) { my $progress = ( $self->{progress} += lenght($line) ); $self->{callback}->($outer_fh, $progress); } return $line; } sub CLOSE { close $_[0]{inner_fh} } sub BINMODE { binmode $_[0]{inner_fh}, $_[1] } sub EOF { eof $_[0]{inner_fh} } } use IO::Handle qw( ); { open(my $fh, "<", $fileToRead) or die; binmode $fh; my $ftp = Net::FTP->new("ftp.example.com"); $ftp->login('username', 'password'); $ftp->binary(); my $size = -s $fh; $fh = Handle::ReadProgress->wrap($fh, sub { my ($fh, $progress) = @_; printf("\r%d%%", int($progress / $size) * 100); STDOUT->flush(); }); print('0%'); STDOUT->flush(); $ftp->store($fh, $fileToWrite); print("\rdone.\n"); }

Update: Fixed missing TIEHANDLE (by adding new).


In reply to Re: Track file upload progress by ikegami
in thread Track file upload progress by PerlRob

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.