wrote:
> Hi and TIA.
>
> Following is a snippet of code which uses HTML::TableContentParser to
> pull table data out of an HTML page. I've gotten it to work, but, I'd
> like to better understand why/how (particularly the long array/hash
> references). I've read the Perllol and Perlref documents, but would
> really appreciate a plain language walk-through. Thanks.
>
> my $cServerInventoryURL = '...';
> my $cServerInventoryHTML = get $cServerInventoryURL || die "Couldn't
> get $cServerInventoryURL";
>
> $p = HTML::TableContentParser->new(); # This seems to assign
> a reference handle for the parser object
> $tables = $p->parse($cServerInventoryHTML); # This assigns a
> reference to an array containing all the tables in the HTML
> $t=$tables->[5]; # This
OK - so $t now contains a reference to a hash, $tables containing an
arrayref (a reference to an array). Try using Data:

umper to print it out.
use Data:

umper;
print Dumper $t;
This will be your greatest aid in understanding the structure.
> appears to assign a reference to the 6th table object in the HTML page
>
> ##### HERE ARE THE REFERENCES IN QUESTION. THEY WORK, I JUST AM NOT
> CLEAR WHY #####
> for $r (@{$t->{rows}}) {
The hashref pointed to by $t contains an element "rows", which is itself
an arrayref. We need to deference the reference to be able to use most
array operators on it. "for" in this context (also "foreach") sets $r to
be a reference to each element of the arrayref in turn.
> $cell2=$r->{cells}[1]{data}; #Cell 2 contains server name
> $cell9=$r->{cells}[8]{data}; #Cell 9 contains environment list
This syntax is syntactic sugar: written out longhand it would read
$cell2 = $r->{cells}->[1]->{data};
So: $r is a hashref containing an element "cells", that is itself an
arrayref. ->[1] dereferences the second element of this arrayref, giving
a hashref with an element "data", which gives a scalar value.
You need to be running under
use strict;
use warnings;
Check out the docs - for example perlstyle or perlfaq3 - for why.
Mark