in reply to Net::SSH2::SFTP stat function

Read the docs for the Perl builtin stat regarding the "mode" field.
use Fcntl ':mode'; my $mode = ...; if (S_ISDIR($mode)) { # directory ... } elsif (S_ISLNK($mode)) { # link ... } elsif (...
Also, instead of Net::SSH2, you should try using Net::SFTP::Foreign that has methods to mirror full file systems trees:
use Net::SFTP::Foreign; my $sftp = Net::SFTP::Foreign->new($host); $sftp->error and die "unable to connect to remote host: ".$sftp->error +; $sftp->rget($remote_dir, $local_dir);

Replies are listed 'Best First'.
Re^2: Net::SSH2::SFTP stat function
by james_ (Novice) on Oct 02, 2008 at 12:29 UTC
    Thanks for the suggestion! I did as you suggested:

    use Fcntl ':mode'; my $mode = ...; if (S_ISDIR($mode)) { # directory ... }

    But still no joy. It simply doesn't enter the "if" block, even though I'm doing a stat on a directory. I suspect it's got something to do with the fact that the files (and dirs) are on a remote host. Anyway, reading the docs on the builtin stat has shown me how to get the permissions, i.e.

    my $ssh2 = Net::SSH2->new(); my $sftp = $ssh2->sftp(); my %fstat = $sftp->stat("${testdir}"); my $perm = $fstat{"mode"} & 07777;

    But is there no way to get the file type in the same way, i.e. using an & operator on the mode?

    Many thanks..

      yes, the S_IF* constants from Fcntl tell you the meaning of the remaining bits:
      S_IFREG => 32768 S_IFDIR => 16384 S_IFLNK => 40960 S_IFBLK => 24576 S_IFCHR => 8192 S_IFIFO => 4096 S_IFSOCK => 49152
      Regarding the compatibility of the different SSH/SFTP modules availables, read this post: Re^3: package that can handle ftp, sftp, http etc ?.
        Thanks, using the S_IF* constants works fine!