Next Los Angeles Perl Mongers 9

The Problems in Writing the Seamstress Table Method

  1. Problem: For table unrolling we accept this:

        gi_tr       => 'iterate',

or this:

        gi_tr       => ['iterate1', 'iterate2']
        use Scalar::Listify;
        my @table_gi_tr = listify $table{gi_tr} ;
        my @table_gi_tr = (ref $table{gi_tr} eq 'ARRAY')
                ? @($table{gi_tr})
                : $table{gi_tr}

Create HTML::Element objects for the sample rows:

        my @iter_node = map {
              $table->{table_node}->look_down($ID, $_)
        }  @table_gi_tr;

2. Problem: when accepting the arrayref, the $seamstress->table() must be able to "cycle" through the sample table rows as long as there is model data. When dealing with one row, we dont need to "cycle", but it doesn't hurt.

        tie $table->{iter_node}, 'Tie::Cycle', \@iter_node;

The Table Method Unleashed

        our ($table_data, $tr_data, $gi_td);
        sub table {
          my ($s, %table) = @_;
          my $table = {};
          $table->{table_node} = $s->look_down($ID, $table{gi_table});
          my @table_gi_tr = listify $table{gi_tr} ;
          my @iter_node = map 
            {
              $table->{table_node}->look_down($ID, $_)
            } @table_gi_tr;
          tie $table->{iter_node}, 'Tie::Cycle', \@iter_node;
          $table->{content}    = $table{content};
          $table->{parent}     = $table->{table_node}->parent;
          $table->{table_node}->detach;
          $_->detach for @iter_node;
          my $add_table;
          while (my $row = $table{tr_data}->($table, $table{table_data})) 
            {
              ++$add_table;
              # wont work:      my $new_iter_node = $table->{iter_node}->clone;
              my $I = $table->{iter_node};
              my $new_iter_node = $I->clone;
              $table{td_data}->($new_iter_node, $row);
              $table->{table_node}->push_content($new_iter_node);
            }
          $table->{parent}->push_content($table->{table_node}) if $add_table;
        }

More Details November 2003