A
Average_Joe
Hello Newsgroup.
Just soliciting some general opinions.
What would you say is the most sexy, easy to use iterator?
My favorite is to return an object supporting a next() method, (when next()
returns references or lists.) or a getline() method for lines of text. Boring,
but effective.
A close second is to return a code reference:
$code = someFunction();
while( $entry = $code->() ){
}
Easy, uncluttered, simple to do. $code->() can accept arguments if needed,
returning hashes and lists are sane.
I've been thinking about overloading <> so it becomes:
This is listed as "iteration" in overload, I'm left with the notion that it's
supposed to be the standard approach, yet I've never seen it in use for
returning references and lists.
while($entry = <$object>){
# huh?
}
This looks "dirty" to me, except maybe if $entry is plain-text, but where
$entry is a reference to something just seems to weird. One nice thing, I guess
is that you can have it both ways. <$object> OR $object->next() are the same
method.
One quirk, is that if next() is supposed to return a list. Like:
while( @record = <$object>) { ... }
I suppose you could do that, but one would think it should return ALL the
records, if each record is a list, it's kind of confusing.
What about:
while($object){
++$object; # The "iterator".
%rec = %{$object}; # The current value.
$rec = ${$object}; # Same thing.
}
Does that seem sane? To me it seems "more sane", except that you can't always
do --$object (Like if $object were reading from standard input)
Also seems too involved, have to implement 4 different functions instead
of a single 'next()' (and you have to keep track of a "current entry")
Finally, (more serious question I guess) what do you do about callbacks?
Say you use a module that uses a collector style callback. Maybe it performs a
search or something and calls your code reference for each hit.
You wanted to use it as part of your module, but you want your module to return
an iterator. How would you create a callback that can exit and then return
where it left off? Short of altering the module, the only way I can think of is
to build a whole list or use some sort of thread/fork.
Opinions?
Jamie
Just soliciting some general opinions.
What would you say is the most sexy, easy to use iterator?
My favorite is to return an object supporting a next() method, (when next()
returns references or lists.) or a getline() method for lines of text. Boring,
but effective.
A close second is to return a code reference:
$code = someFunction();
while( $entry = $code->() ){
}
Easy, uncluttered, simple to do. $code->() can accept arguments if needed,
returning hashes and lists are sane.
I've been thinking about overloading <> so it becomes:
This is listed as "iteration" in overload, I'm left with the notion that it's
supposed to be the standard approach, yet I've never seen it in use for
returning references and lists.
while($entry = <$object>){
# huh?
}
This looks "dirty" to me, except maybe if $entry is plain-text, but where
$entry is a reference to something just seems to weird. One nice thing, I guess
is that you can have it both ways. <$object> OR $object->next() are the same
method.
One quirk, is that if next() is supposed to return a list. Like:
while( @record = <$object>) { ... }
I suppose you could do that, but one would think it should return ALL the
records, if each record is a list, it's kind of confusing.
What about:
while($object){
++$object; # The "iterator".
%rec = %{$object}; # The current value.
$rec = ${$object}; # Same thing.
}
Does that seem sane? To me it seems "more sane", except that you can't always
do --$object (Like if $object were reading from standard input)
Also seems too involved, have to implement 4 different functions instead
of a single 'next()' (and you have to keep track of a "current entry")
Finally, (more serious question I guess) what do you do about callbacks?
Say you use a module that uses a collector style callback. Maybe it performs a
search or something and calls your code reference for each hit.
You wanted to use it as part of your module, but you want your module to return
an iterator. How would you create a callback that can exit and then return
where it left off? Short of altering the module, the only way I can think of is
to build a whole list or use some sort of thread/fork.
Opinions?
Jamie