On 2010.08.11 00:15, MySelf rdtan.net wrote:

> On 10/08/10 4:49, Dermot wrote:

>> I think you might be getting a bit confused here. TT requires
>> references for it's parameters variable, so yes array, hash references
>> (or any reference that returns a list) but that is different from how
>> you retrieve data from the DBI. You turned that data into a hash of
>> hash references when you did this:
>>
> You're absolutely correct about I'm confused. I can't differentiate
> what's array & array references, hash & hash references.

Understanding references is imho one of the most important aspects of
being able to use Perl effectively, and minimize headache.

> At one point when I try to pass multiple row of array (query from DBI
> using $sth->fetchrow_array) from perl to TT, it some how does not
> work.

That's because iirc TT accepts a hashref as the data, and you must stuff
your arrayrefs within it.

Here is an example I've cobbled together quickly (using an email template):

my ( @notices, @renewals );

if ( $blah ) {
    # push a new (anonymous) hash reference onto the array
    push @notices, { username => $username, hours => $hours_balance, };
}
else {
    push @renewals, { username => $username, hours => $hours_balance, };
}

# create a hash for the template data, and insert
# a reference to both of the arrays within it

my %tmpl_data;

$tmpl_data{ notices }    = \...@notices;
$tmpl_data{ renewals }   = \...@renewals;

my $msg     = MIME::Lite::TT->new(

                  From        => $from,
                  To          => $to,
                  Subject     => $subject,
                  Template    => $tmpl,
                  TmplParams  => \%tmpl_data, # create ref of hash
              );

__END__

The data structure looks like this when printed with Dumper. It is a
hash reference that has two keys, who's values contain an array
reference. Each of the two array references point to an array that
contains a hash reference as each element. The data within these hrefs
are what are used in the foreach loops within the template itself.

Note that the below output contains two loops. I refer to this output
whenever I find myself having an issue generating output from within a
template loop:

acct-dev: ISP % sudo ./b.pl

$VAR1 = {
          'renewals' => [
                          {
                            'hours' => '8.84 hours remaining',
                            'username' => 'xxx'
                          },
                          {
                            'hours' => '5.51 hours remaining',
                            'username' => 'xxx'
                          },
                        ],
          'notices' => [
                         {
                           'hours' => '13.17 hours remaining',
                           'username' => 'xxx'
                         },
                         {
                           'hours' => '10 hours remaining',
                           'username' => 'xxx'
                         },
                        ]
        };

__END__

The resulting template itself:

Clients sent Notices:

[% FOREACH item IN notices %]
[%item.username%]: [%item.hours%]
[% END %]

Clients sent Renewals:

[% FOREACH item IN renewals %]
[%item.username%]: [%item.hours%]
[% END %]

> Until I stumble on this working codes from Perl Mongers and turn
> it into hash, then only it works. I was hoping someone over here can
> show some codes on how to use $sth->fetchrow_arrayref to pass arrays
> (or array references?) to TT.

I believe what you want to do is fetch the data from the db as a hashref
instead, and push the rows into an array to generate your loop topology.
untested... I haven't used DBI in quite a long time:

my @loop_array;

push @loop_array, $sth->fetchrow_hashref();

my %top_level_tmpl_data;

$top_level_tmpl_data{ db_data } = \...@loop_array;

Then, you can either create an explicit reference to the top level hash
like this:

my $tmpl_data_ref = \%top_level_tmpl_data;

...or just skip that step and create the ref as you pass the data into TT.

I hope this is what you were after. Note that I prefer to use references
directly as opposed to creating array and hash first, but I thought that
it would be better in this case if I explicitly did things the long way.

Cheers,

Steve

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to