slugger415 has asked for the wisdom of the Perl Monks concerning the following question:

Ok I'm having a dummy moment here. Wanting to use Net::SFTP. I looked here: http://search.cpan.org/~drolsky/Net-SFTP-0.08/lib/Net/SFTP.pm but frankly don't understand how to do it.

Here's some of my code:

my $sftp = Net::SFTP->new($host, %args); print $sftp->ls('.'), $/;

It's clearly connecting successfully, but here's what I get:

HASH(0x30b7de0)HASH(0x30b8098)

I've tried assigning that to a hash, array and variable but can't seem to do anything with it. What am I missing?

Replies are listed 'Best First'.
Re: Using Net::SFTP
by choroba (Cardinal) on Feb 21, 2018 at 22:14 UTC
    What you see is a hash reference, as documented in the very documentation you linked to:
    returns a list of directory entries, each of which is a reference to a hash as described in the previous paragraph.

    Where the previous paragraph says:

    a hash with three keys: filename, the name of the entry in the directory listing; longname, an entry in a "long" listing like ls -l; and a, a Net::SFTP::Attributes object, which contains the file attributes of the entry (atime, mtime, permissions, etc.).

    So, if you want to assign to a hash, you need to dereference first:

    for my $remote_file ($sftp->ls('.')) { my %details = %$remote_file; print $details{filename}, "\n"; }

    To visualize the whole structure, use Data::Dumper:

    use Data::Dumper; for my $remote_file ($sftp->ls('.')) { print Dumper($remote_file); }

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
Re: Using Net::SFTP
by AnomalousMonk (Archbishop) on Feb 21, 2018 at 22:12 UTC
    print $sftp->ls('.'), $/;

    I don't know anything about Net::SFTP, but the doc sez:

    $sftp->ls($remote [, $subref ])

    Fetches a directory listing of $remote.

    If $subref is specified,
    [but you're not specifying a $subref] ... .

    If $subref is not specified, returns a list of directory entries, each of which is a reference to a hash as described in the previous paragraph.
    [emphases added]
    So try something like (untested)
        use Data::Dump qw(dd);
        ...
        my @ra = $sftp->ls('.');
        dd \@ra;
    to see what you're really getting back.

    Update: See also Data::Dump.


    Give a man a fish:  <%-{-{-{-<

Re: Using Net::SFTP
by kcott (Archbishop) on Feb 21, 2018 at 22:32 UTC

    G'day slugger415,

    If you look in the documentation for Net::SFTP, you'll see under $sftp->ls($remote [, $subref ])]:

    "... returns a list of directory entries, each of which is a reference to a hash ..." [my emphasis]

    So you're getting something like this:

    $ perl -E 'my $x = {a=>1}; say $x' HASH(0xad5cb8)

    You can use modules like Data::Dumper (builtin) or Data::Dump (CPAN) to investigate further, e.g.

    $ perl -E 'use Data::Dump; my $x = {a=>1}; dd $x' { a => 1 }

    — Ken

Re: Using Net::SFTP
by thanos1983 (Parson) on Feb 22, 2018 at 12:02 UTC

    Hello slugger415,

    Fellow Monks have provided you with multiple solutions. Just to add one more :):

    #!/usr/bin/perl use strict; use warnings; use Net::SFTP; my $host = '127.0.0.1'; my %args = ( user => 'tinyos', ssh_args => [ port => "22" ] ); my $sftp = Net::SFTP->new($host, %args); my $ls = $sftp->ls('/home/user/Monks'); print "$_->{filename}\n" for (@$ls); __END__ $ perl test.pl benchmark.pl~ Foo sample.pl test.log out.txt sample.pl~ .. .. ..

    Minor note to make here, I am not using password I use ssh keys, I think you should do the same is far far more safe.

    Having said all that why not use Net::SFTP::Foreign? Why I should you use this module? Simply read here Net::SFTP::Foreign Vs. Net::SFTP Vs. Net::SSH2::SFTP.

    Sample of code just as your example:

    #!usr/bin/perl use strict; use warnings; use Net::SFTP::Foreign; my %args = ( host => "127.0.0.1", user => "user", port => "22", # psw => "psw", # uncomment if you are using passwords key_path => "/home/user/.ssh/id_rsa" ); # comment if you are +using passwords my $sftp = Net::SFTP::Foreign->new(%args); $sftp->die_on_error("Unable to establish SFTP connection"); my $ls = $sftp->ls('/home/user/Monks') or die "unable to retrieve directory: ".$sftp->error; print "$_->{filename}\n" for (@$ls); __END__ $ perl test.pl benchmark.pl~ Foo sample.pl test.log out.txt sample.pl~ .. .. ..

    Both examples above are based on my local configurations, so simply update them based on your configurations.

    Hope this helps, BR.

    Seeking for Perl wisdom...on the process of learning...not there...yet!