For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > August 2007 > Re: Get names of files ONLY using module Net::SFTP::Foreign









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: Get names of files ONLY using module Net::SFTP::Foreign
Chas Owens

2007-08-27, 8:48 am

On 8/27/07, salva <sfandino@gmail.com> wrote:
> On Aug 24, 2:57 pm, bed...@nbs.sk (Marian Bednar) wrote:
>
> The permission flags for the remote object are available on the
> Attributes object on the 'a' slot
>
> You can use the S_IS* functions from Fcntl to check whether those flag
> are for a particular kind of object, for instance S_ISREG for files
> (similar to -f):
>
> Also, the ls method from Net::SFTP::Foreign allows you to read all the
> directory entries in one go, without requiring you to open the dir and
> looping with readdir:
>
> use strict;
> use warnings;
> use Net::SFTP::Foreign;
> use Fcntl ':mode';
>
> my $remote_dir = "/tmp";
> my $sftp = Net::SFTP::Foreign->new("host");
> $sftp->error and print "SSH connection failed: " . $sftp->error .
> "\n";
>
> my @files = $sftp->ls($remote_dir,
> wanted => sub { S_ISREG($_[1]->{a}-
>
> print "$_->{filename}\n" for @files;


Be careful, this is Net::SFTP::Foreign, not Net::SFTP. They are very
similar, but there are differences. For instance the ls method
returns an arrayref, not a list in Net::SFTP::Foreign. This means
your code should be

my @files = @{
$sftp->ls($remote_dir, wanted => sub { S_ISREG($_[1]->{a}->perm)})
};

from perldoc Net::SFTP::Foreign
$sftp->ls($remote, %opts)
Fetches a directory listing of the remote directory $remote. If
$remote is not given, the remote current working directory is
listed.

Returns a reference to a list of entries. Every entry is a refer$B!>(B
ence to a hash with three keys: "filename", the name of the entry;
"longname", an entry in a "long" listing like "ls -l"; and "a", a
Net::SFTP::Foreign::Attributes object containing file atime, mtime,
permissions and size.

from perldoc Net::SFTP
$sftp->ls($remote [, $subref ])

Fetches a directory listing of $remote.

If $subref is specified, for each entry in the directory, $subref will
be called and given a reference to 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, permis$B!>(B
sions, etc.).

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 para$B!>(B
graph.
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com