Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Doing an 'ls' in SFTP:Foreign

by StarkRavingCalm (Sexton)
on Feb 15, 2013 at 15:42 UTC ( #1018899=perlquestion: print w/ replies, xml ) Need Help??
StarkRavingCalm has asked for the wisdom of the Perl Monks concerning the following question:

Ok, I have some code that is working perfectly in Net:SFTP. However, I needed to switch from Net:SFTP to Net:SFTP:Foreign to trap errors. But now the intial 'ls' I am putting into a hash complains with: Not a HASH reference at ./test-perl-sftp-get.pl line 103 ( which is the 'next if $entry->{filename} =~ /^\./;' )

I am guessing the attributes are different from SFTP to SFTP:Foreign

What would the SFTP:Foreign equivalent of this be:?

my %file; foreach my $entry ( $sftp->ls('/home/ftptest/inbound') ) { next if $entry->{filename} =~ /^\./; my $size = (split(' ', $entry->{longname}))[4]; $file{$entry->{filename}} = $size; } print Dumper \%file;

Thanks in advance!

Comment on Doing an 'ls' in SFTP:Foreign
Download Code
Re: Doing an 'ls' in SFTP:Foreign
by salva (Monsignor) on Feb 15, 2013 at 16:19 UTC
    ls returns an array reference:
    $l = $sftp->ls(...) or die "ls failed: ".$sftp->error; for my $e (@$l) { ... }

      Why does this work flawlessly in Net:SFTP but not Net:SFTP:Foreign?

        I haven't used these packages, but the man page for Net::SFTP says ls returns a list of entries, while Net::SFTP::Foreign says it returns a reference to a list of entries.

        fnord

        Different modules, different APIs.

        Retrospectively, having ls return an array reference, was probably a bad idea, bad it is too late to change it now.

        It works flawlessly in both libraries. It just works differently.

      Maybe salva knows how to format that line to work with Foreign?

      Ok, I am ALMOST there... I have the 'ls' going to a array and getting close to what I would expect. Currently, it is ignoring hidden files and giving filenames. What I need is to get the size of each file without all the atime, mtime, perms etc... that appears to be default in SFTP-Foreign.

      NOTE: I am only using 'names_only' as a proof of concept in case that negates any file size arguments I would put in there.

      Does anyone know how to get the file size as an argument in the string I am using?

      my @newarray = @{ $sftp->ls('/home/ftptest/inbound', names_only => 1, +no_wanted => qr/^\./ )};
        Just loop over the returned values generating a new list with the fields you want to keep:
        my $ls = $sftp->ls('/home/ftptest/inbound', no_wanted => qr/^\./ ); my @names_and_sizes = map { { filename => $_->{filename}, size => $_-> +{a}->size } } @$ls; # or... my %size = map { $_->{filename} => $_->{a}->size } @$ls;
Re: Doing an 'ls' in SFTP:Foreign
by Illuminatus (Curate) on Feb 15, 2013 at 16:20 UTC
    Are you using the autodie param when creating the object? If the ls fails in your example, the loop may still execute, but it obviously won't give you a hash entry.

    fnord

      not using autodie

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1018899]
Front-paged by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (17)
As of 2014-07-29 17:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (223 votes), past polls