perl-embperl mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Angus Lees" <gusl...@gmail.com>
Subject Re: Object Arrays & Dynamic Tables
Date Sun, 10 Jun 2007 21:22:28 GMT
On 6/5/07, Gavin Spomer <spomerg@cwu.edu> wrote:
>
> Hi Folks,
>
> I have an array of "facet" objects called @facets. A "facet" object has a
> method called facet_name. I would like to output this using dynamic tables,
> instead of having to do an Embperl foreach loop. The following Embperl
> foreach loop works:
>
>    <table>
>    [$ foreach $facet (@facets) $]
>       <tr>
>          <td>[+ $facet->facet_name +]</td>
>       </tr>
>    [$ endforeach $]
>    </table>
>
> However when I try using an Embperl dynamic table (as desired) like the
> following:
>
>    <table>
>       <tr>
>          <td>[+ $facets[$row]->facet_name +]</td>
>       </tr>
>    </table>
>
> I get an error:
>
>    [1381]ERR: 24: Error in Perl code: Can't call method "facet_name" on an
> undefined value at /usr/local/mylibrary/index.epl line 17.
>
> Oddly enough, the following works (using a literal value of zero instead
> of $row):
>
>    <table>
>       <tr>
>          <td>[+ $facets[0]->facet_name +]</td>
>       </tr>
>    </table>
>
> And also this works too, even though it isn't the output I'm looking for:
> (It's not calling facet_name)
>
>    <table>
>       <tr>
>          <td>[+ $facets[$row] +]</td>
>       </tr>
>    </table>
>
> What am I doing wrong? It seems to me like something funky is going on
> with $row.


Thats how $row works.  It keeps repeating the expression until it evaluates
to undef.  This works great for $array[$row], but in your case the last row
is undef->facet_name which gives that error.

Try [+ $facets[$row] and $facets[$row]->facet_name +]  or else switch to
(possibly nested) array/hash structures rather than method calls.

-- 
- Gus

Mime
View raw message