+ Reply to Thread
Results 1 to 3 of 3

Thread: Perl References

  1. #1
    xav0989's Avatar
    xav0989 is offline Community Public Relation xav0989 is just really nice
    Join Date
    Jul 2008
    Location
    ifk
    Posts
    4,410

    Perl References

    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}.
    Xavier L | Community Public Relations Manager (Free Hosting Support)
    █ Yes, my position is too cool to even exist!
    How am I helping? Rate this post by clicking the icon below! (this is even better than "liking" a post)
    Terms of Service | Acceptable Use Policy | x10Hosting Wiki

  2. #2
    misson is offline Community Advocate misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,391

    Re: Perl References

    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 by misson; 02-28-2010 at 08:13 PM.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

  3. #3
    xav0989's Avatar
    xav0989 is offline Community Public Relation xav0989 is just really nice
    Join Date
    Jul 2008
    Location
    ifk
    Posts
    4,410

    Re: Perl References

    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 by xav0989; 03-01-2010 at 10:10 AM.
    Xavier L | Community Public Relations Manager (Free Hosting Support)
    █ Yes, my position is too cool to even exist!
    How am I helping? Rate this post by clicking the icon below! (this is even better than "liking" a post)
    Terms of Service | Acceptable Use Policy | x10Hosting Wiki

+ Reply to Thread

Similar Threads

  1. Perl?
    By pzelnip in forum Programming Help
    Replies: 4
    Last Post: 05-29-2009, 01:10 AM
  2. Perl bug
    By kdmqforforum in forum Free Hosting
    Replies: 1
    Last Post: 05-13-2009, 05:42 AM
  3. Eblah Perl/Perl Mods
    By vol7ron in forum Free Hosting
    Replies: 3
    Last Post: 04-08-2008, 06:50 PM
  4. New! Looking for good online references
    By nbanerje in forum Introductions
    Replies: 1
    Last Post: 12-17-2007, 09:18 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers