Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re^6: Perl sftp behavior when there are no files in the share

by Bindo (Acolyte)
on Mar 27, 2013 at 15:29 UTC ( #1025744=note: print w/replies, xml ) Need Help??


in reply to Re^5: Perl sftp behavior when there are no files in the share
in thread Perl sftp behavior when there are no files in the share

Thank you very much again sir. Mr soonix beleive it or not Im a beginner in the world of perl. I too saw the context callback and I couldnt understand a thing. In fact net:sftp foreign is the firs ever cspan module I used in my scripting. Could you please be good enough to modify my code without complicating it too much (cos im a beginner :) so that I could grasp the idea of it? Please sir.

  • Comment on Re^6: Perl sftp behavior when there are no files in the share

Replies are listed 'Best First'.
Re^7: Perl sftp behavior when there are no files in the share
by soonix (Monsignor) on Mar 27, 2013 at 18:41 UTC

    This is untested, because I haven't yet figured out how to use cpan with the perl interpreter on my smartphone :-)

    First you need the callback routine, like this:

    sub show_progress { my($sftp, $data, $offset, $size) = @_; print "downloaded $offset of $size bytes\r"; }
    The \r positions the cursor to the beginning of the line, so that the next print overwrites it.

    Then you change

    $sftp->get($REM_FILE)
    to
    $sftp->get($REM_FILE, callback => \&show_progress)
    That should do the trick...

      Thank you again sir. I tried but it gives me this error.

      Odd number of elements in hash assignment at /usr/lib/perl5/site_perl/ +5.10.0/Net/SFTP/Foreign.pm line 1432. Invalid option 'CODE(0xa53608)' or bad combination of options at ./gtp +_logs.pl line 66

      Any thoughts sir? Also what would be the easiest way to get what I want without using CSPAN stuff? As in that simple progresss shown when you download the file manually? Many thanks in advance sir.

        It seems there is a small error in the code by soonix. The Net::SFTP::Foreign documentation says that ->get takes the local name of the file as second parameter and the rest are options. So if you want to display the progress, you'll also have to provide the local name, for example like this:

        ... use File::Basename; my $local_name= basename( $REM_FILE ); warn "Saving '$REM_FILE' to '$local_name'"; $sftp->get($REM_FILE, $local_name, callback => \&show_progress) ...

        This is actually two replies in one. First, thanks (and ++) to Corion for pointing out the mistake in my solution above.

        Now Bindo, your question for a solution without CPAN... I think doing SFTP in pure Perl, while possible, would be difficult and error-prone. If you just don't want any additional CPAN modules, that's another question:

        As far as I see, your $REM_FILE already is a file name without directory, so you don't need the basename function and module, and can simply

        $sftp->get($REM_FILE, $REM_FILE, callback => \&show_progress)
        Perhaps that already does what you want.

        Update: second parameter in get corrected

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1025744]
help
Chatterbox?
talexb admires the shimmering pool after an enjoyable hour-long All Hands meeting spent listening with one ear and reading Perlmonks with the other .. ear.
[talexb]: Used all my votes during that meeting .. been a while since I've done that. Now, back to work. Good localtime, monks!
[Corion]: talexb: You enjoyed the All Hands meeting in the pool?!
[Corion]: I understand "surfing" in the pool ;)
[talexb]: Corion I admired the pool .. I wasn't .. soaking in it. ;)
[talexb]: Plus, I've been awy from PM for about three weeks .. needed a bit of catch-up time.

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (12)
As of 2017-07-20 15:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I came, I saw, I ...
























    Results (304 votes). Check out past polls.