Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

How to print list using Net::SFTP::Foreign

by TCM (Acolyte)
on Oct 02, 2013 at 18:26 UTC ( #1056662=perlquestion: print w/replies, xml ) Need Help??
TCM has asked for the wisdom of the Perl Monks concerning the following question:

I have a script that connects to a remote sftp server and is then suppose to print a listing of all the files in the remote directory. However, I get the error message "Argument "\x{2e}" isn't numeric in hash element..". Can anyone tell from my code what I am doing incorrectly? I am on a FreeBSD v 5.3 R1 machine running Perl 5.8.5 (build for i386-freebsd-64int). I don't have much experience with Perl and would appreciate any help offered. My code is below and it errors out on the line "$filename=$file->{filename}". Quick note: The script does call another script ( that contains subroutines that I use often, but that shouldn't have anything to do with the actual error (at least I don't think it would). Thank you for your time!
#!/usr/bin/perl -w $base_dir = substr($0,0,rindex($0,"/")); chop($hostname = `hostname`); # include shared subroutines do "$base_dir/"; $sftphost = ""; $sftpuser = "username"; $sftppass = "password"; $remotedir = "TO REMOTE"; ($scriptname = "$0") =~ s/.*\///; # begin log string $outSTR = log_divider("BEGIN: (" . localtime() . ")"); $outSTR .= "$hostname : $scriptname - $subject\n"; # create string with name of log file based on $scriptname $logfile= substr($scriptname,0,rindex($scriptname,".")) . ".log"; $log_dir= "$base_dir/log"; # Set up first log entry $outSTR .= log_divider("Begin Test Connection Script"); print $outSTR; log_it($log_dir,$logfile,$outSTR); # Connect to remote SFTP server $sftp = sftpf_connect($sftphost,$sftpuser,$sftppass); # List remote directory contents @$remotefiles=$sftp->ls($remotedir); # Loop @$remotefiles and print each filename foreach (@$remotefiles){ $file=$_; $filename=$file->{filename}; if($filename ne "." && $filename ne ".."){ print"the filename is $filename"; } } #Disconnect from remote server sftpf_disconnect($sftp); # Write to log file $outSTR .= log_divider("End Test Connection Script"); print $outSTR; log_it($log_dir,$logfile,$outSTR); print $outSTR;

Replies are listed 'Best First'.
Re: How to print list using Net::SFTP::Foreign
by keszler (Priest) on Oct 03, 2013 at 02:49 UTC
    Adding use strict; at the beginning of your script (and fixing everything it subsequently complains about) would likely have pointed you directly at the problem.

    According to Net::SFTP::Foreign, $sftp->ls($remote, %opts):

    Returns a reference to a list of entries. Every entry is a reference to a hash with three keys: ...
    Thus, your
    # List remote directory contents @$remotefiles=$sftp->ls($remotedir);
    should not have the @ in front of $remotefiles.
      Thank you for the reply keszler, it worked. Thank you also for the user strict information. I really appreciate you taking the time to help me out!

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1056662]
Approved by marto
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2017-01-17 01:35 GMT
Find Nodes?
    Voting Booth?
    Do you watch meteor showers?

    Results (151 votes). Check out past polls.