I would format it slightly, and then just read from the bottum up. Yes map and grep can be scary, but normaly reading them isn't hard (just figuring out how/when to use them is.

sub _file_containing { my($self, $id) = @_; opendir(my $dir, $self->{update_dir}) or return; my @files = map { "$self->{update_dir}/$_" } sort { $a <=> $b } grep { /^\d+$/ && $_ <= $id } readdir $dir; return $files[-1]; }

So now start at the bottom of the statment and read up. readdir $dir takes all the files in the directory $self->{update_dir} and pipes them into the grep above it. grep { /^\d+$/ && $_ <= $id } is a filter that only lets files that look like a number and are less than $id through. sort { $a <=> $b } sorts the files using a numeric sort (instead of cmp that would do an alphanumeric). map  { "$self->{update_dir}/$_" } takes that list and adds the actual directory to the front of each element. The map in this case is kinda pointless because you then return only the last element, however the map means that you don't have to check if there was indeed a last element in the list to return. If you replaced the last return with return $self->{update_dir} || '/' || $files[-1]; then you would wrongly return the directory itself when there where no matching files.


___________
Eric Hodges

In reply to Re: How do I un-map this code? by eric256
in thread How do I un-map this code? by Plankton

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.