explain code

I am new to perl, and I am having trouble understanding the following lines of code. can someone explain what is happening after the foreach statement? Also, what I would like is in the template myform statement to be able to insert a value = value returned from query

my $stmt = 'SELECT * FROM table';
my $sth = $dbh->prepare($stmt) or return $self->errstr("prepare: $DBI::errstr: error");
$sth->execute() or return $self->errstr("execute: $DBI::errstr: error");
while( my $row = $sth->fetchrow_hashref ) {
    $value="";
    if ( !$flag ) {
        foreach(qw/flda fldb fldc/) {
            $vl = lc($_);
            $field = '{' . $vl . '}';
            $value = $template{$vl} if lc($ref->{'value'}) eq lc($field);
        }
    }
$template{'myform'} .= qq|<tr><td width="20%">$ref->{'flda'}</td><td width="80%">
<input type="text" class="textbox"  name="$row->{'id'}" value="$value" size="20"></td></tr>|;
elikhaterAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

elikhaterAuthor Commented:
correction of last line in while and first line of template.
           $value = $template{$vl} if lc($row->{'value'}) eq lc($field);
           $template{'myform'} .= qq|<tr><td width="20%">$row->{'flda'}</td><td width="80%">
ozoCommented:
       foreach(qw/flda fldb fldc/) {
            $vl = lc($_);
            $field = '{' . $vl . '}';
            $value = $template{$vl} if lc($ref->{'value'}) eq lc($field);
        }
is the same as
            $vl = lc('flda');
            $field = '{' . $vl . '}';
            $value = $template{$vl} if lc($ref->{'value'}) eq lc($field);
            $vl = lc('fldb');
            $field = '{' . $vl . '}';
            $value = $template{$vl} if lc($ref->{'value'}) eq lc($field);
            $vl = lc('fldc');
            $field = '{' . $vl . '}';
            $value = $template{$vl} if lc($ref->{'value'}) eq lc($field);
which is essentially
            $value = $template{'flda'} if lc($ref->{'value'}) eq '{flda}';
            $value = $template{'fldb'} if lc($ref->{'value'}) eq '{fldb}';
            $value = $template{'fldc'} if lc($ref->{'value'}) eq '{fldc}';
I don't know why not use
            $value = $template{(lc($ref->{'value'})=~/^{(.*)}$/)[0]};
or why put that in the while loop, since $ref->{'value'} doesn't seem to be affected by $row = $sth->fetchrow_hashref

To make $template{myform} show all values returned by the query, you might use something like
while( my $row = $sth->fetchrow_hashref ) {
    $value=join'&',map{"$_=$row->{$_}"}sort keys %$row;
    $template{'myform'} .= qq|<tr><td width="20%">$ref->{'flda'}</td><td width="80%">
<input type="text" class="textbox"  name="$row->{'id'}" value="$value" size="20"></td></tr>|;
}
elikhaterAuthor Commented:
I sort of understand how you derived $value, however, hearing it from you could shed some new light for me. Everything else is ok.
Become a Microsoft Certified Solutions Expert

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

ozoCommented:
Was $ref->{'value'} supposed to be $row->{'value'}?  Is 'value' one of the fields returned by the query?
What are the fields in table, and which fields do you want displayed in $template{myform}?
elikhaterAuthor Commented:
ozo,

I simply want to retrieve one of the columns from the query and utilise it in building the template.
Yes, all references to $ref must be $row. I want to display flda in the template.
Finally, I still do not quite get the objective from executing the foreach within the while. Any thoughts.

Your help is appreciated. Thanks
elikhaterAuthor Commented:
ozo, apologies but I have to leave now. I will look at your answer and respond later.
ozoCommented:
If $ref was supposed to be $row, then it looks like the foreach loop was meant to see whether
$row->{value} returned '{flda}' or '{fldb}' or '{fldc}', and if so, set $value to $template{flda} or $template{fldb} or $template{fldc} repectively.
If I understand what you're saying above, it sounds like you want
while( my $row = $sth->fetchrow_hashref ){
    $value=$row->{value};
    $value = $template{lc $1} if $value =~ /^{(fld[a-c])}$/i;
    $template{myform} .= qq|<tr><td width="20%">$row->{flda}</td><td width="80%"> <input type="text" class="textbox"  name="$row->{id}" value="$value" size="20"></td></tr>|;
}

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
elikhaterAuthor Commented:
That clarified lots of things for me. Thank you
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.