Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

HTML::Template Modules and Zeros

by Angel (Friar)
on Apr 21, 2003 at 22:31 UTC ( [id://252143]=perlquestion: print w/replies, xml ) Need Help??

Angel has asked for the wisdom of the Perl Monks concerning the following question:

Dear Fellow Monks,

Back when I was doing simple subtitution s/var_name/$var_name I used a pad function to take the results from a database that were zero but perl insisted were blank scalars and print them as "0"

I am now using HTML::Template and using a Module to query the data my attempts have failed to get "0" on the output. Any ideas I am sure its simple but I have missed something here. And I'd rather not put the padding in the main module

# pad adds leading zeros to a string ### FROM THE MODULE THAT QUERIES THE DATA #### sub pad( ) { my $output; my ($string, $length)=@_; if( $string eq "" ) { $output = "0"; } else { my $padlength=$length-length($string); $output = "0"x$padlength.$string; } return $output; } sub get_number_prereg_adult( $ ) { my $data; my $query; my $self = shift; my $dbh = $self->{dbh}; my $sqlQuery = "SELECT COUNT(*) FROM PREREGDATA WHERE GroupID = $self->{'group_id'} AND Category = \'1\'"; $query = $dbh->prepare( $sqlQuery ); $query->execute() || die $dbh->errstr; if( $data = $query->fetchrow_array() ) { $self->{error_type} = ""; $self->{error_string} = ""; return( pad( $data, 1 ) ); } else { $self->{error_type} = "variable returned no value"; $self->{error_string} = "variable returned no value"; return( undef ); } } ### FROM THE MODULE THAT USES HTML TEMPLATE #set template page $template = HTML::Template->new( filename=>"update_group_informatio +n_template.htm" ); $template->param( number_pre_reg_adult_variable_string => $qu +ery_module->get_number_prereg_adult );

Replies are listed 'Best First'.
Re: HTML::Template Modules and Zeros
by dws (Chancellor) on Apr 22, 2003 at 00:19 UTC
    Any ideas? I am sure its simple but I have missed something here.

    When in doubt, write a short test.

    use HTML::Template; # version 2.6 my @template = <DATA>; my $template = HTML::Template->new(arrayref => \@template); $template->param(ZERO => 0); $template->param(ONE => 1); print $template->output(); __DATA__ Zero = <TMPL_VAR ZERO> One = <TMPL_VAR ONE>
    produces the expected result
    Zero = 0 One = 1
    You might have run into a version-dependent bug, or you might have a problem upstream. What happens when you try to replicate the problem with a small example?


    Update: jeffa points out that   my $template = HTML::Template->new(filehandle => *DATA); is another way to go.

Re: HTML::Template Modules and Zeros
by dragonchild (Archbishop) on Apr 21, 2003 at 22:39 UTC
    This may not mean much, but:
    if( $data = $query->fetchrow_array() )
    is suspect to me. It will always, in this case, give you zero or 1. I suspect you want:
    if( ($data) = $query->fetchrow_array() )
    That may not fix your problem, but it is a bug.

    ------
    We are the carpenters and bricklayers of the Information Age.

    Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

    Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

(z) Re: HTML::Template Modules and Zeros
by zigdon (Deacon) on Apr 22, 2003 at 17:20 UTC
    Is it possible that your output template has something like this?
    <TMPL_IF number_pre_reg_adult_variable_string> the output is <TMPL_VAR number_pre_reg_adult_variable_string> </TMPL_IF>
    If so, the block won't print because the variable is evalurating to 0!

    -- zigdon

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://252143]
Approved by dorko
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (6)
As of 2024-04-25 11:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found