Hi Dana,

On Tuesday 11 May 2010 17:14:51 Dana Hudes wrote:
> Klaus,
> Thanks for contributing to CPAN
> 
> On the question of callbacks: This is Perl, there's more than way to do it
> (whatever 'it' is ). That said the use of callbacks is very Perlish.
> Indeed Perl itself uses callbacks in it's builtins: look at sort(), the
> comparison function is a callback.

sort() uses a callback because it's a one-time operation. You normally are not 
interested in having the array as partially sorted. However, sometimes 
implementing an iterative interface makes a lot of sense.

> 
> It is also true that while callbacks provide a clean interface (instead of
> overloading a generic object method, for example) you are adding another
> function call to processing. But instead of having a clean function call
> you have substituted calling an iterator from your object. This is
> actually more costly.

More costly? Did you benchmark it? A callback-based interface also has an 
implicit loop, and, with an iterative interface, you can inline the block 
dealing with the iterated stuff:

[code]
while (my $token = $self->get_next_token())
{
        # Do something with $token.
}
[/code]
 
An iterative interface also has the advantage of being capable of being 
interrupted in the middle more easily, and often more capable of 
instantiation.

Jonathan Rockway noted that one can use coroutines / continuations to convert 
a callback-based interface into an iterative one, but in Perl 5, it involves 
using the Coro.pm module which introduces many low-level hacks, is another 
dependency, may complicate the code and will also make it harder to read.

I have done some work on File-Find-Object ( 
http://www.shlomifish.org/open-source/projects/File-Find-Object/ ), because it 
was an iterative alternative to File::Find, which could also be instantiated, 
as well as got rid of most of the other File::Find ugliness in its interface. 
File::Next is also similarly iterative (though it cannot be instantiated).

> 
> Look at XML::Simple. You get back a data structure and deal with it
> yourself using native Perl iteration (foreach). You can then do whatever
> you want: write to a file, find the piece you want or do some
> tranformation and turn it back into XML. 

The problem with XML-Simple in the context of this discussion is that it reads 
the entire file into memory and then lets you handle the entire tree in 
memory, which may be very memory consuming as XML can get very large. While 
this approach (as also done by the XML-LibXML's DOM handling) is often valid, 
it is not a full substitute to the XML-Reader (or XML::LibXML::Reader, etc.) 
approach of iterating through the file event-by-event.

There are other problems with XML-Simple, and I have a policy against ever 
recommending it or helping people with their XML-Simple problems, but they are 
tangential to this discussion. 

Regards,

        Shlomi Fish

-- 
-----------------------------------------------------------------
Shlomi Fish       http://www.shlomifish.org/
What does "Zionism" mean? - http://shlom.in/def-zionism

God considered inflicting XSLT as the tenth plague of Egypt, but then
decided against it because he thought it would be too evil.

Please reply to list if it's a mailing list post - http://shlom.in/reply .

Reply via email to