[Petal] While loops

Jean-Michel Hiver jhiver at mkdoc.com
Tue Feb 4 11:51:08 GMT 2003


On Tue 04-Feb-2003 at 01:26:26AM +0100, Mark Holland wrote:
> Hi,
> 
> I'm not sure if the following is at all possible -I'm trying to write a 
> while loop in petal using a database cursor.
> 
> In perl it would be something like this:
> 
> while (my $obj = $cursor->next) {
>    print $obj->some_value();
> }

I think it might be another good use for Petal recursive abilities...

File cursor.xml
===============

  <block petal:omit-tag="string:1"
         petal:define="next object/cursor/next"
         petal:condition="next"
  >
  
    <span petal:replace="next/some_value">Foo</span>
    <xi:xinclude href="./cursor.xml" />
    
  </block>

You need to make sure you do set $Petal::MAX_INCLUDES to something high
enough.


> I know that I could read all the objects into an array and then just 
> pass that and loop through it, but if I can save myself the extra step 
> and make my code neater I would like to.

If you don't like the 'recursion' option and if you could know the size
of the result set in advance then you could construct a tied array,
define the methods FETCHSIZE() and FETCH() and die on any other method.

Basically:

  sub FETCHSIZE { return shift->{size} }

  sub FETCH ($index) { return $shift->cursor->next() }

  sub AUTOLOAD { die 'arrrgh' }

  sub DESTROY {}


When you've done that you could write a 'cursor:' modifier which would
return a reference to this tied array. Then you could write:

  <li petal:repeat="cursor: object/cursor">
    ...
  </li>


> Any ideas? Am I missing something very basic for which I will be made
> to feel stupid? ;-)

None of the above is very basic, and it's true that I haven't really
built Petal with cursors in mind.


Cheers,
-- 
Building a better web - http://www.mkdoc.com/
---------------------------------------------
Jean-Michel Hiver
jhiver at mkdoc.com  - +44 (0)114 255 8097
Homepage: http://www.webmatrix.net/



More information about the Petal mailing list