You might do well to read How do I post a question effectively? and Writeup Formatting Tips. In particular, you should be wrapping code in <code> tags and try to make sure published code is runnable as written. You are clearly using modules but have not specified what modules you are using.

Based upon your syntax, I am gussing you are using the Net::SFTP module. From the module's documentation:

$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, permissions, 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 paragraph.

You can therefore recover the -l result by changing your key from filename to longname. Your sorting options take a little more work, since you need the complete list to sort it. I would suggest you modify your subroutine to return an array ref with the longname and mtime, and then use sort with a custom function on the returned list.

Update: As SilasTheMonk points out below, following this approach requires a closure to cache search results rather than the simple return I describe above.

Update 2: Having reread the above, the return value from an ordinary call to $sftp->ls contains all the information you need to recover your desired result (untested).

my @contents = $sftp->ls($RemoteBaseDirectory . $RemoteOutgoingDirecto +ry); my @sorted = sort {$b->{a}->mtime <=> $b->{a}->mtime} @contents; print $sorted[0]->{longname};

If the above fails, you need to look at the format of the mtime return to fix the compare.


In reply to Re: Perl SFTP question? by kennethk
in thread Perl SFTP question? by goochalba

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.