Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You need to get a good Perl book which will help you learn how to code Perl in a more <em>modern</em> style. I recommend <a href="http://rads.stackoverflow.com/amzn/click/1449303587" rel="nofollow">Learning Perl</a>. This should teach you some techniques in coding.</p> <p>For example, always use <code>use strict;</code> and <code>use warnings;</code>. These two modules will catch about 99% of all coding errors.</p> <p>Also, the standard way of doing an error check is to use the <code>or die</code> style syntax. This is a much more natural method than setting a variable, and then checking the variable, and if there is an error, then going to a subroutine:</p> <pre><code>$stfp-&gt;ls or die qq(Cannot access remote machine ) . HOST; </code></pre> <p>It would also be nice if you indented too. Makes following your code easier.</p> <p>Perl has a standard that variables should be all lowercase, and the newest standard is to use underscores to help separate out variable names. Constants should be all uppercase.</p> <p>You should also look at the module's complete documentation. For example, there's an <code>autodie</code> option when you create a <code>Net::SFTP::Foreign</code> object that will kill your <code>sftp</code> session on any failure. This simplifies coding because you don't have to check for errors.</p> <p>You also need to know that <code>Net::SFTP::Foreign-&gt;ls</code> returns a <em>reference</em> to an array. Thus, you need to dereference that array. You also have to know that each entry in this <em>array reference</em> is actually a reference to a hash, and that the file name is under the <code>filename</code> key of that hash reference.</p> <p>And one more tiny fact the document mentions: This program uses the Unix/Linux installed <code>ssh</code> client. If you don't have that <code>ssh</code> client installed, this just won't work.</p> <p>You may want to look at the Perl Tutorial on <a href="http://perldoc.perl.org/perlretut.html" rel="nofollow">References</a></p> <p>I've rewritten your program to take advantage of <code>autodie</code> and to use a bit more modern syntax. Normally, I would test this, but unfortunately, I don't have Net::SFTP::Foreign installed, and I don't have a remote computer I can use for testing:</p> <pre><code>#! /usr/bin/env perl use strict; use warnings; use feature qw(say); use autodie; use Net::SFTP::Foreign; use constant { HOST =&gt; "sftp.elsvier.com", REMOTE_DIR =&gt; "sample/host", LOCAL_DIR =&gt; "sample/local", USER_NAME =&gt; "username", PASSWORD =&gt; "password", DEBUG =&gt; "0", }; my $sftp; $sftp = Net::SFTP::Foreign-&gt;new ( HOST, timeout =&gt; 240, user =&gt; USER_NAME, password =&gt; PASSWORD, autodie =&gt; 1, ); # # Fetch Files # $sftp-&gt;rget( REMOTE_DIR, LOCAL_DIR ); # # Delete Files # $sftp-&gt;setcwd( REMOTE_DIR ); my @files = @{ $sftp-&gt;ls }; #Returns a reference to an array of hashes for my $file ( @files ) { $sftp-&gt;remove( $file-&gt;{filename} ); #Reference to a hash } say "Deleted files successfully"; $sftp-&gt;disconnect; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload