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

Hi,

I am very new to Perl and XML so please forgive me if I sound like a noob.

In my .pl I am using XML::Simple, Data::Dumper, and HTML::Template to try to display info from an xml file to an html template. I have read and reread the CPAN articles for all the mods I am using, but still can't figure this out.

My XML looks like this:
<document> 'employee' =>{ 'department' => [ 'Operations Hardware', 'Corporate Marketing', 'Legal', 'Human Resources', 'Finance' ] } </document>
To get this in perl I have:
#!/perl #use strict; use lib 'C:/xampp/perl/site/lib'; use HTML::Template; $ENV{XML_SIMPLE_PREFERRED_PARSER} = 'XML::Parser';<br< use XML::Simple; use Data::Dumper; my $template = HTML::Template->new(filename => 'file'); my $xml = XMLin('file', ForceArray => ['option']); my @deptartment = @{ $xml->{employee}->{department} }; $template->param(DEPT => @deptartment); print $template->output;
and in my .tmpl I have:
<+font size="4">Department: </+font> <+ul><+li><select name="dept"> <TMPL_LOOP DEPT> <option value="<TMPL_VAR EMP_DEPT>"><TMPL_VAR EMP_DEPT></option> + </TMPL_LOOP> </select></+li> </+ul>

I'm not getting any errors, but the drop-down list in the html that gets displayed is empty. I have a feeling that I'm either (1) not getting the data from the .xml correctly, or (2) not giving the .tmpl the data in the correct format.

Please help!

Replies are listed 'Best First'.
Re: Problems getting info from .xml to display with HTML::Template
by moritz (Cardinal) on Jul 16, 2008 at 14:38 UTC
    $template->param(DEPT => @deptartment);

    Even if everything else were correct, this is certainly wrong, since HTML::Template expects an array reference, so use $template->param(DEPT => \@deptartment) instead.

    (1) not getting the data from the .xml correctly, or

    You can easily check that with Data::Dumper. Actually you can be sure that your data structure is wrong, because there is no nested XML structure in your input.

    Please also use use strict; use warnings; in your script to prevent you from doing stupid mistakes.

      XML now:
      <document> <position> <departments> <department name = "Nothing Selected" /> <department name = "Pricing and Shopping Systems" /> <department name = "Commercial" /> <department name = "Operations Hardware" /> <department name = "Corporate Marketing" /> <department name = "Legal" /> <department name = "Human Resources" /> <department name = "Finance" /> </departments> </position> </document>
      Perl now:
      #!/perl use strict; use warnings; use lib 'C:/xampp/perl/site/lib'; use HTML::Template; $ENV{XML_SIMPLE_PREFERRED_PARSER} = 'XML::Parser'; use XML::Simple; use Data::Dumper; my $template = HTML::Template->new(filename => 'file'); my $xml = XMLin('file', ForceArray => ['departments']); my @department = @{ $xml->{position}->{departments} }; $template->param(DEPT => \@deptartment); print $template->output;
      With strict on I get error: Global symbol "@department" requires explicit package name.

      With strict off I get no errors, but nothing shows up on my template.

      Using Dumper, my xml data looks right but displays something like

      $VAR1=[ {department => { 'Finance' => {}, 'Human Resources' => {} } ]
      I'm thinking maybe the empty curly braces have something to do with why it's not actually displaying anything?
        my @department ..
        ...
        $template->param(DEPT => \@deptartment);
        ...

        With strict on I get error: Global symbol "@department" requires explicit package name.

        Do you notice anything? removing strict only hides the error.

        And I think you'll be more successful if you structure your XML to contain the data in the tags:

        <document> <position> <departments> <department>Nothing Selected</department> <department>Pricing and Shopping Systems</department> ... </departments> </position> </document>

        But then again I didn't test it.