Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

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\@"; 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 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"; }

Replies are listed 'Best First'.
Re: Not getting List of files from Net::SFTP::Foreign
by syphilis (Chancellor) 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;
Re: Not getting List of files from Net::SFTP::Foreign
by kcott (Chancellor) 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?

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2018-06-23 03:39 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (125 votes). Check out past polls.