Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Not getting List of files from Net::SFTP::Foreign

by sanPerl (Friar)
on Sep 03, 2013 at 03:42 UTC ( #1052004=perlquestion: print w/ replies, xml ) Need Help??
sanPerl has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks, I am failing to get list of files from following code
use strict; use lib '/myperllib'; use Net::SFTP::Foreign; my $host="root\@10.111.11.11"; my $password="password"; my $sftp = Net::SFTP::Foreign->new($host, fs_encoding => 'latin1'); my @ls = map { $_->{filename} } $sftp->ls('/root/mydirs'); foreach my $myfile (@ls) { print "File: $myfile\n"; }
I am Not able to understand the error Not a HASH reference at xx.pl line 7.. Also I am touching Perl code after two years and it seems I have lost the touch (Feels great to work on it again :-)). Can someone help me please ?

Update:I got the answer from some other thread. This code works fine as per thread Doing an 'ls' in SFTP:Foreign
my $ls = $sftp->ls('/root/mydirs', no_wanted => qr/^\./ ); my %size = map { $_->{filename} => $_->{a}->size } @$ls; foreach my $filename (sort keys %size) { print "$filename : $size{$filename}\n"; }

Comment on Not getting List of files from Net::SFTP::Foreign
Select or Download Code
Re: Not getting List of files from Net::SFTP::Foreign
by syphilis (Canon) on Sep 03, 2013 at 04:19 UTC
    You haven't checked to see whether the Net::SFTP::Foreign->new(...) call succeeded. If it fails, then $sftp won't be a hash reference.
    You don't appear to have passed the password on. Do you need to ?

    The documentation recommends using $ftp->error() to check that the new() call has succeeded:
    $sftp = Net::SFTP::Foreign->new($host); $sftp->error and die "unable to connect to remote host: " . $sftp->err +or;
    Cheers,
    Rob
Re: Not getting List of files from Net::SFTP::Foreign
by kcott (Abbot) on Sep 03, 2013 at 05:23 UTC

    G'day sanPerl,

    According to the Net::SFTP::Foreign documentation, the ls method reurns an arrayref of hashrefs not an array of hashrefs.

    If you read the documentation, you'll find it not only provides the correct syntax for what you've presented here, but also a way to do without the map altogether.

    -- Ken

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1052004]
Approved by Jim
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (8)
As of 2014-12-25 19:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (162 votes), past polls