in reply to What makes File::Find's interface so commonly hated

Another thing I have noticed is that people seem to have an aversion to File::Find's callback interface. There is a perceived loss of control. In some cases, there is a real loss of control, but usually it is mostly perception. Consider a case like the following, though (this is a made up interface, but it's just a demonstration):

while (my $found = $finder->next) { print $found->filename, "\n"; last if $found->pathname =~ $end_condition; next if $found->pathname =~ $skip_condition; process($found); }

This wouldn't be quite so nice with a callback interface, even though there's no reason it isn't possible.

Replies are listed 'Best First'.
Re^2: What makes File::Find's interface so commonly hated
by Aristotle (Chancellor) on Jul 16, 2006 at 19:02 UTC

    There are other issues. F.ex., with a GUI app, the callback interface is a problem as you need to be able to keep the app responsive while scanning potentially huge directory trees. Yes, it’s possible even with the callback interface, but it’s a right pain. The natural way to do this would be to launch an iterator and then collect results whenever the UI is idle; File::Find forces you to instead poll the UI for events within your wanted function, which breaks separation of concerns and doesn’t work in all scenarios.

    Makeshifts last the longest.

Re^2: What makes File::Find's interface so commonly hated
by ikegami (Patriarch) on Jul 16, 2006 at 19:35 UTC
    Not so. I use a module whose callback returns 0 or 1 for last or next respectively.
    sub callback { my ($found) = @_; print $found->filename, "\n"; return 0 if $found->pathname =~ $end_condition; return 1 if $found->pathname =~ $skip_condition; process($found); return 1; }

    Constants would improve readability.

      As I said, this is certainly possible with a callback interface. What you've shown is a way to do that, but it's not using the standard next and last operators, so it's not quite as easy to follow or understand.

      I should probably also note that I don't have a big problem with callback interfaces generally, nor with File::Find specifically. But I can certainly say in this case, one alternative is slightly nicer than the other.