Plankton has asked for the
wisdom of the Perl Monks concerning the following question:

Dear Monks,

I have also tried to avoid the use of map because I really can never understand what it is doing therefore assume that other people that might have to read my code might also find it difficult to understand if they ever had to maintain it.

Unfortunately I am the one maintaining the code of some really smart guy that thinks map, and unlesses and other crappy obfuscations make for maintainable code. Please forgive my rant ... anyways here's the bit of code I would
to un map so I can add some debug to it.

I understand that back in the day structured programming it was a good idea to put an '_' in front of function if you where writing libraries so you wouldn't clutter the namespace of people using your libraries. But when I see it in circumstances like object orientated code is being used this practice of
_my_great_subroutine only points out the pretensiousness of the doofus that wrote the code!

I have also tried to avoid the use of map because I really can never understand what it is doing

I translate this statement as "I'm scared, so I refuse to address my fear." If you really don't want to address your fear, stop now. I plan on getting you to understand map so that you never have to worry again.

map, grep, and sort all operate the exact same way. They take a list of stuff and return a list of stuff.

sort takes the list and sorts it, returning back every item that was in the original list, but possibly in a different place.

grep takes the list and applies a function to each item. If the function returns true, that item is in the result list. If the function returns false, that item is skipped. The items that make it are in the same order as they were originally.

map takes the list and applies a function to each item. It takes the return value from the function and puts it into the result list. The items are returned in the same order.

Now, the biggest problem people usually have with map and grep (though, funnily enough, not sort) is that you have to read them from right to left. This is in direct opposition to how you read everything else (which is from left to right). Perl 6 will provide a mechanism where you can have map, grep, and other list operators that can be read from left to right. It will look something like:

Thanks that is a little more readable. I can't see how adding map to the mess would make it more readable. And as far as '_' meaning private ... I guess that would be private by convention since Perl doesn't really have any private methods or members, but conventions are only good if to tell people about them and this particular convention isn't documented anywhere.
It might more sense to let people on that this method is private like so ...

And as far as '_' meaning private ... I guess that would be private by convention since Perl doesn't really have any private methods or members, but conventions are only good if to tell people about them and this particular convention isn't documented anywhere.

On the contrary, this widely used naming convention is documented in the core Perl docs, the Camel book, and in many other places:

From Perl style: "You can use a leading underscore to indicate that a variable or function should not be used outside the package that defined it."

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.

Ah, no. The code in the OP is looking for a file name that matches some criteria (the maximum numerical file less than or equal to some id). Your code is looking for files with contents that match some criteria (contains some string). So the original function does seem poorly named. But if you really do hate want to avoid perl that much, you could (in ksh):