Perl Template Toolkit <% IF %> 条件似乎不起作用

Perl Template Toolkit <% IF %> conditional seems to not be working

下面有 html 的部分,我想在散列 'Season' 具有值时显示该部分。问题是即使 Season 没有值,该部分也会显示,我什至没有将名为 Season 的变量发送到模板,或者即使我将下面的行更改为 <% IF XSeason.size %>

<% IF Season.size %> <!-- season data -------------------------------------->
<div class="container" style="margin-top:20px;">
  <div class="row">
    <h3 class="text-center"> This should only show up if Season hash has value</h3>
    <div class="col-md-12">
      <div class="col-md-10">
    <div class="form-group">
      <table style="width:100%; line-height:40px;">   <!-- Feats (so far) as a table // -->
        <% FOREACH $name in Seasonkeys %>
        <tr>
          <form role="form" action="../alpview" method="POST">
        <td width="5"><% Season$name.stat %></td> 
        <td width="5"><% Season$name.AVG %></td> 
        <input type="hidden" name="chlng_id" value="<% Season$ID.ID %>" />
          </form>
        </tr>   
        <% END %>   
      </table>
    </div>
      </div>
    </div>
  </div>
</div>
<% END %>

更新:这是一个更简单的例子。

<h2>Lets start with an array reference<h2>
<h3>What about fruits array: </h3>
<% FOREACH fruit IN fruits %>
<% fruit %>
<% END %>
<p>array size: </p><% fruits.size %></p>
<p>array dumped: </p><% USE Dumper; Dumper.dump(fruits) %>

输出:

Lets start with an array reference
What about fruits array:
apple bananna orange
array size:

array dumped:

你的说法很容易被推翻:

use 5.014;
use warnings;

use Template qw( );

my $template = '<% IF Season.size %>Test<% END %>';

my $tt = Template->new({
   START_TAG => '<%',
   END_TAG   => '%>',
});

print "No var:         ";
$tt->process($template, { })
   or warn($tt->error());
say "";

print "Empty hash:     ";
$tt->process($template, { Season => { } })
   or warn($tt->error());
say "";

print "Not empty hash: ";
$tt->process($template, { Season => { a => 4 } })
   or warn($tt->error());
say "";

输出:

No var:
Empty hash:
Not empty hash: Test

您的哈希不能为空。

尝试将其添加到 <% IF ... %> 标记之后。

<% Season.size %>
<% USE Dumper; Dumper.dump(Season) %>

至少你会看到 TT 认为变量是什么。