Loading ...
Sorry, an error occurred while loading the content.

Re: [PBML] how to close SFTP connection?

Expand Messages
  • Jeff Shu
    ... wrote: Hello Charles: Thanks for the explanation. I will use undef $sftp; in my ftp_send function.
    Message 1 of 3 , Sep 6 8:05 PM
    • 0 Attachment
      --- In perl-beginner@yahoogroups.com, "Charles K. Clarkson"
      <cclarkson@...> wrote:
      Hello Charles:

      Thanks for the explanation. I will use undef $sftp; in my ftp_send
      function.

      > There is no concept for logging off or for closing the SFTP
      > connection in the Net::SFTP docs or in the source code. Also, there is
      > no DESTROY method in Net::SFTP or the underlying Net::SSH::Perl object
      > so letting the $sftp object go out of scope may not work, but it's
      > usually a good idea to limit the scope of all variables in a subroutine.
      >
      >
      >
      > : sub ftp_send() {
      > : my($file) = @_;
      > : $sftp = Net::SFTP->new('xxx', user=>'zzz', password=>'yyy',
      > : debug=>1);
      > : $sftp->put("$homedir", "$remote");
      > : #close the SFTP connection here or exit SFTP environment
      > : }
      >
      > sub ftp_send() {
      >
      > my( $file, $homedir, $remote ) = @_;
      >
      > my $sftp = Net::SFTP->new(
      > 'host',
      > user => 'zzz',
      > password => 'yyy',
      > debug => 1
      > );
      >
      > $sftp->put( $homedir, $remote );
      >
      > # When this sub routine ends the SFTP connection should close
      > # as $sftp goes out of scope. If this sub routine is not ending
      > # here, I could use "undef $sftp;" to destroy the object.
      > }
      >
      > BTW:
      >
      > * Don't quote variables needlessly. "$remote" is not always the
      > same as $remote. See What's wrong with always quoting "$vars"?
      > in Perlfaq4.
      >
      > * Perlsub recommends that new perl programmers not use the "&"
      > prefix on subroutines. &ftp_send($filename) is better called
      > with ftp_send($filename) until you know why they are different.
      >
      >
      >
      > HTH,
      >
      > Charles K. Clarkson
      > --
      > Mobile Homes Specialist
      > Free Market Advocate
      > Web Programmer
      >
      > 254 968-8328
      >
      > http://www.clarksonenergyhomes.com/wordpress/about/
      >
      > Don't tread on my bandwidth. Trim your posts.
      >
    Your message has been successfully submitted and would be delivered to recipients shortly.