HTML::Template->param() : You gave me an odd number of parameters to param()!

Discussion in 'Perl Misc' started by Dave, Apr 25, 2011.

  1. Dave

    Dave Guest

    Hi,

    I'm using Perl 5.10.1. I'm trying to fill in an HTML::Template, which
    consists of

    <TMPL_LOOP NAME=TESTS>
    <tr><td><a href="<TMPL_VAR NAME=ABSOLUTE_PATH>"><TMPL_VAR
    NAME=FILE_SHORT_NAME></a></td></tr>
    </TMPL_LOOP>


    but I'm getting an error when I try and do the following ...

    my @loop_arr;
    foreach (@test_files) {
    my $test_file = $_;
    my %params_hash;
    $params_hash{'absolute_path'} = $test_file;
    $params_hash{'file_short_name'} = $test_file;
    push(@loop_arr, %params_hash);
    }
    # open the html template
    my $template = HTML::Template->new(filename => '/opt/scripts/selenium/
    report_suite.templ');
    $template->param(TESTS => @loop_arr);

    The error is complaining about the last line. I tried changing
    "@loop_arr" to "$loop_arr" but that gave some undefined variable
    errors. Could someone point out the error(s) of my ways?

    Thanks, - Dave
    Dave, Apr 25, 2011
    #1
    1. Advertising

  2. Dave

    J. Gleixner Guest

    Re: HTML::Template->param() : You gave me an odd number of parametersto param()!

    Dave wrote:
    > Hi,
    >
    > I'm using Perl 5.10.1. I'm trying to fill in an HTML::Template, which
    > consists of
    >
    > <TMPL_LOOP NAME=TESTS>
    > <tr><td><a href="<TMPL_VAR NAME=ABSOLUTE_PATH>"><TMPL_VAR
    > NAME=FILE_SHORT_NAME></a></td></tr>
    > </TMPL_LOOP>
    >
    >
    > but I'm getting an error when I try and do the following ...
    >
    > my @loop_arr;
    > foreach (@test_files) {
    > my $test_file = $_;
    > my %params_hash;
    > $params_hash{'absolute_path'} = $test_file;
    > $params_hash{'file_short_name'} = $test_file;
    > push(@loop_arr, %params_hash);

    push( @loop_arr, \%params_hash );

    > }
    > # open the html template
    > my $template = HTML::Template->new(filename => '/opt/scripts/selenium/
    > report_suite.templ');
    > $template->param(TESTS => @loop_arr);


    $template->process(TESTS => \@loop_arr);
    >
    > The error is complaining about the last line. I tried changing
    > "@loop_arr" to "$loop_arr" but that gave some undefined variable
    > errors. Could someone point out the error(s) of my ways?


    Give the documentation a try.
    J. Gleixner, Apr 25, 2011
    #2
    1. Advertising

  3. Dave

    Guest

    On Mon, 25 Apr 2011 13:46:24 -0700 (PDT), Dave <> wrote:

    >Hi,
    >
    >I'm using Perl 5.10.1. I'm trying to fill in an HTML::Template, which
    >consists of
    >
    ><TMPL_LOOP NAME=TESTS>
    > <tr><td><a href="<TMPL_VAR NAME=ABSOLUTE_PATH>"><TMPL_VAR
    >NAME=FILE_SHORT_NAME></a></td></tr>
    ></TMPL_LOOP>
    >
    >
    >but I'm getting an error when I try and do the following ...
    >
    >my @loop_arr;
    >foreach (@test_files) {
    > my $test_file = $_;
    > my %params_hash;
    > $params_hash{'absolute_path'} = $test_file;
    > $params_hash{'file_short_name'} = $test_file;
    > push(@loop_arr, %params_hash);
    >}
    ># open the html template
    >my $template = HTML::Template->new(filename => '/opt/scripts/selenium/
    >report_suite.templ');
    >$template->param(TESTS => @loop_arr);
    >

    maybe => [@loop_arr]

    since => imply's a hash element, which can't be an array,
    but can be a reference to an array.

    -sln
    , Apr 25, 2011
    #3
  4. Dave

    John Bokma Guest

    writes:

    > On Mon, 25 Apr 2011 13:46:24 -0700 (PDT), Dave <> wrote:


    >>$template->param(TESTS => @loop_arr);
    >>

    > maybe => [@loop_arr]
    >
    > since => imply's a hash element, which can't be an array,
    > but can be a reference to an array.


    In which case you might want to use => \@loop_arr instead. Correct me if
    I am wrong but you're now copying @loop_arr into an empty hash you
    created a reference too. Sometimes that's what you want (for example if
    you want to keep a copy), but in this case most likely not.

    --
    John Bokma j3b

    Blog: http://johnbokma.com/ Facebook: http://www.facebook.com/j.j.j.bokma
    Freelance Perl & Python Development: http://castleamber.com/
    John Bokma, Apr 26, 2011
    #4
  5. Dave

    Guest

    On Mon, 25 Apr 2011 18:37:38 -0500, John Bokma <> wrote:

    > writes:
    >
    >> On Mon, 25 Apr 2011 13:46:24 -0700 (PDT), Dave <> wrote:

    >
    >>>$template->param(TESTS => @loop_arr);
    >>>

    >> maybe => [@loop_arr]
    >>
    >> since => imply's a hash element, which can't be an array,
    >> but can be a reference to an array.

    >
    >In which case you might want to use => \@loop_arr instead. Correct me if
    >I am wrong but you're now copying @loop_arr into an empty hash you
    >created a reference too. Sometimes that's what you want (for example if
    >you want to keep a copy), but in this case most likely not.


    It was hard to tell what he wanted to do. Looked like he wanted to copy
    the array to this place, therefore the new array. Otherwise, it appears
    if he doesen't know what [] is, he isin't going to know what \@ar is.
    I stopped guessing what's in the minds of people.

    -sln
    , Apr 26, 2011
    #5
  6. Dave

    John Bokma Guest

    Tad McClellan <> writes:

    > John Bokma <> wrote:
    >> writes:
    >>
    >>> On Mon, 25 Apr 2011 13:46:24 -0700 (PDT), Dave <> wrote:

    >>
    >>>>$template->param(TESTS => @loop_arr);
    >>>>
    >>> maybe => [@loop_arr]
    >>>
    >>> since => imply's a hash element, which can't be an array,
    >>> but can be a reference to an array.

    >>
    >> In which case you might want to use => \@loop_arr instead. Correct me if
    >> I am wrong but you're now copying @loop_arr into an empty hash you

    > ^^^^
    > ^^^^
    > s/hash/array/;


    Thanks Tad, indeed that should have read array :)

    --
    John Bokma j3b

    Blog: http://johnbokma.com/ Facebook: http://www.facebook.com/j.j.j.bokma
    Freelance Perl & Python Development: http://castleamber.com/
    John Bokma, Apr 26, 2011
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. gaurav1234
    Replies:
    0
    Views:
    408
    gaurav1234
    Jul 7, 2010
  2. Luan
    Replies:
    2
    Views:
    137
    Wilson Bilkovich
    Nov 29, 2006
  3. Replies:
    5
    Views:
    100
  4. Replies:
    10
    Views:
    161
    J├╝rgen Exner
    Aug 28, 2005
  5. PerlFAQ Server
    Replies:
    19
    Views:
    223
    John Bokma
    Apr 28, 2011
Loading...

Share This Page