in reply to SFTP Foreign Issues using $sftp->ls

print "\nH1. Attempt to list files "; foreach $file ( @{$ls1}, @{$ls2}, @{$ls3}, @{$ls4} ) { print "\nH1. $file "; }

$sftp->ls() returns a reference to an array of hashes. You are printing the reference to the hash. Try the example given in the documentation for Net::SFTP::Foreign and let us know how that works. It is printing the 'filename' value in the returned hash. If you leave out the directory name it will list the contents of the current working directory.

my $ls = $sftp->ls('/home/foo') or die "unable to retrieve directory: ".$sftp->error; print "$_->{filename}\n" for (@$ls);

Update: Also I just noticed you are using glob style wildcards inside a regular expression for your ls function call. That won't work as you expect for a regular expression.

$pat4 = $envment . "REQ_*.req.copied"; ... $ls4 = $sftp->ls('/', names_only => 1, wanted=>qr/$pat4/);

In your $pat4 regular expression it is looking for 0 or more copies of '_' followed by any single character ( '.' ) followed by req then any single character, etc. Why not start with a small test of just printing all files then try to get the regular expression working.

Replies are listed 'Best First'.
Re^2: SFTP Foreign Issues using $sftp->ls
by salva (Canon) on Mar 01, 2016 at 19:47 UTC
    Actually the module has a glob method:
    my @files = $sftp->glob($envment."{REP_*.dat,REQ_*.report,REQ_*.report +.*,REQ_*.req.copied}", names_only => 1);
    Every call to glob retrieves the full remote directory so it is better to use a single glob covering all the file names if possible.
      Actually the module has a glob method:

      Yes I know but the title of the OP was about ls() and the OP is using glob style wildcards in a regex in the ls(). I don't feel like rewriting a long convoluted script when the OP needs to try a few simple things and get something working.

      And the glob method returns the same style reference to an array of hashes. The OP was incorrectly dereferencing the data structure returned from ls which I pointed out and you did not. Do you think they would do it correctly for glob or find or something else?

        And the glob method returns the same style reference to an array of hashes

        No. glob returns a list.

        The OP was incorrectly dereferencing the data structure

        No. He is already using the names_only feature that makes the method return just the file names instead of hashes.

      This works as I expected from my previous attempts. Thanks.

      my @files = $sftp->glob($envment."{REP_*.dat,REQ_*.report,REQ_*.repor +t +.*,REQ_*.req.copied}", names_only => 1);