On 8/27/07, salva <[EMAIL PROTECTED]> wrote:
> On Aug 24, 2:57 pm, [EMAIL PROTECTED] (Marian Bednar) wrote:
> > Hi all,
> >
> > I am a newbie in this forum and Perl too ;-)
> >
> > I am trying writing script transfering files using module
> > Net::SFTP::Foreign.
> >
> > I need to retrieve from remote directory only names of files (not
> > directories, links,etc.).
> >
>
> 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}-
> >perm) });
>
> 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‐
           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‐
       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‐
       graph.

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to