http://www.perlmonks.org?node_id=101322


in reply to (tye)Re: File::Find bummers on an NFS volume.
in thread File::Find bummers on an NFS volume.

How would it transfer to the user's code if it didn't callback? File::Recurse only populates a hash with filenames keyed by directory; it doesn't call anything.

I think a callback is natural; however, there might also be an interface that populates hashes or arrays.

Replies are listed 'Best First'.
(tye)Re2: File::Find bummers on an NFS volume.
by tye (Sage) on Aug 01, 2001 at 22:12 UTC

    Perhaps I was thinking of some module other than File::Recurse. Darn.

    How would it transfer to the user's code if it didn't callback?
    Iterators, of course.

    For any readers who aren't familiar with the concept: I find bikeNomad's question similar to "But how can we let the user run a section of code once for each line of an input file without requiring them to write callbacks?"

    Perl is full of iterators. File globbing, line reading, regex matching, and iterating over an array or other list are all common examples of things that are often done in Perl using interators. And this is because callbacks suck. (:

            - tye (but my friends call me "Tye")
      I don't see much of a difference between these idioms in Perl:
      # iterator sort { $a cmp $b } @array; # callback sub myFunc { $a cmp $b } sort myFunc @array;
      It's interesting you mention this. My Algorithm-Diff module uses callbacks, in part because the original module by Dominus did, and in part because that seemed like a good thing to do. There are, in fact, about 6 callbacks that could be specified. A couple are there to handle comparison and hashing of user-defined data types (since everything in Perl isn't an object). The rest of them are there so that user code can be run while traversing the input sequences. This turns out to be pretty useful.

      I'm re-doing this module in Ruby, and am re-considering the interface in the process.

      I ended up with the various diff functions in a class; if someone wants to override comparison behavior, they just re-define a method or two (Ruby lets you do this on a per-object basis; for the equivalent in Perl you can use my Class::Prototyped module). I wonder if that would be a better interface in Perl. You'd just override the methods you cared about:

      package MyDiff; @MyDiff::ISA = 'Algorithm::Diff'; sub keyGen { ... } sub compare { ... } sub match { ... } sub skip_a { ... } package main; my $diff = MyDiff->new(); $diff->traverse_sequences(\@a1, \@a2);

      Do you think that this is a better idiom than explicit callbacks?