Perl References

Discussion in 'Scripts, 3rd Party Apps, and Programming' started by xav0989, Feb 28, 2010.

  1. xav0989

    xav0989 Community Public Relation Community Support

    Messages:
    4,467
    Likes Received:
    95
    Trophy Points:
    0
    I started a small project in perl recently, a survey generator. Basically is loads the survey from a XML file and generates the interface from the XML. However, I am having problems with the hashrefs. I would greatly appreciate if someone could help me out on this.
    Code:
    #!/usr/bin/perl -w
    use HTML::Template;
    use CGI;
    use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
    use XML::Simple;
    use Data::Dumper;
    
    ########################
    #  MUST EDIT FOLOWING  #
    ########################
    # path to the data directory
    my $dataDir = "PATH/TO/WHERE/THE/SURVEYS/ARE/";
    
    ########################
    #      DO NOT EDIT     #
    ########################
    
    my $query  = new CGI;
    my $template = HTML::Template->new(filename => 'survey.tmpl', path => $dataDir);
    my $XML = new XML::Simple;
    
    my $survey = $query->param('survey');
    my $list = $XML->XMLin($dataDir . "surveys/list.xml", KeyAttr => "id");
    
    if (exists $list->{survey}->{$survey}) {
        my $surveyMetaData = $list->{survey}->{$survey};
    } else {
        $template->param(TITLE => 'ERROR', CONTENT => 'ERROR (0x0): Undefined survey ID.');
        print "Content-Type: text/html\n\n", $template->output;
        exit;
    }
    
    my $surveyData = $XML->XMLin($dataDir . "surveys/" . $surveyMetaData->{file} . ".xml", KeyAttr => "id");
    
    print "Content-Type: text/html\n\n", $template->output;
    basically, the $surveyMetaData is being set to an empty hash, when I want it to reference (or hold) the value of $list->{survey}->{$survey}.
     
  2. misson

    misson Community Paragon Community Support

    Messages:
    2,572
    Likes Received:
    72
    Trophy Points:
    48
    There's more going on than what's in the code. An inconsequential alteration (removing the HTML templates, so I can be lazy and not install HTML::Template) works.
    Code:
    #!/usr/bin/perl -w
    #use HTML::Template;
    use CGI;
    use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
    use XML::Simple;
    use Data::Dumper;
    
    ########################
    #  MUST EDIT FOLOWING  #
    ########################
    # path to the data directory
    my $dataDir = $ENV{'DOCUMENT_ROOT'} . "/survey/";
    
    ########################
    #      DO NOT EDIT     #
    ########################
    
    my $query  = new CGI;
    #my $template = HTML::Template->new(filename => 'survey.tmpl', path => $dataDir);
    my $XML = new XML::Simple;
    
    my $survey = $query->param('survey') || 'foo';
    my $list = $XML->XMLin($dataDir . "/list.xml", KeyAttr => "id");
    
    print <<EOS ;
    Content-Type: text/html
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
      <head>
        <title></title>
        <style type="text/css">
        pre {
          padding: 0.5em;
          border: 1px solid black;
        }
        </style>
      </head>
    
      <body>
    EOS
    
    if (exists $list->{survey}->{$survey}) {
        my $surveyMetaData = $list->{survey}->{$survey};
    
        my $surveyData = $XML->XMLin($dataDir . "/" . $surveyMetaData->{file} . ".xml", KeyAttr => "id");
    
        print 'survey.xml:<pre>', Dumper($list), '</pre>';
    
        print 'survey meta data:<pre>', Dumper($surveyMetaData), '</pre>';
    
        print "survey data (from <a href='/survey/$surveyMetaData->{file}.xml'>$surveyMetaData->{file}.xml</a>):<pre>", Dumper($surveyData), '</pre>';
    } else {
        #$template->param(TITLE => 'ERROR', CONTENT => 'ERROR (0x0): Undefined survey ID.');
        #print "Content-Type: text/html\n\n", $template->output;
        print "<p>Error: undefined survey '$survey'.</p>";
    }
    
    print <<EOS ;
      </body>
    </html>
    EOS
    
    Check that the elusive <survey> element in list.xml isn't missing any child elements or attributes.
     
    Last edited: Feb 28, 2010
  3. xav0989

    xav0989 Community Public Relation Community Support

    Messages:
    4,467
    Likes Received:
    95
    Trophy Points:
    0
    Thanks, misson. I will test that out as well.
    Here is the code of my list.xml file:
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <surveys>
    	<survey id="1" name="Testing" file="survey_1" results="survey_1" />
    	<survey id="2" name="Second Testing" file="survey_1" results="survey_1" />
    </surveys>
    EDIT. I think I figured it out. It was because I initialized the $surveyMetaData inside the if block. The reference was only available inside the if block. If I initialize it outside the if block but set is inside, it works. Thanks misson, you opened my eyes :p (damn, I can't give you credits nor reputation... :( )
     
    Last edited: Mar 1, 2010

Share This Page