skip to Main Content

I’m having a lot of trouble getting my Perl CGI script to render HTML. It keeps outputting HTML as plain text. I even tried explicitly setting the Content-Type with print header("text/html");

Is there anything wrong with the following code? –

use CGI qw(:standard);

# other code

print header("text/html"); 
# also tried just print header;

my $banner = "Some text";
print start_html($banner);
print h3($banner);

print start_form(-method=>"POST");
# HTML form specific code
print end_form;

print end_html;

When I check Elements tab on Chrome’s Developer’s tools, for some reason the entire HTML is wrapped inside a <pre> tag which is in turn inside another HTML document. So the HTML is malformed but I’m unable to understand why –

    <html>

    <head></head>

    <body>
        <pre style="word-wrap: break-word; white-space: pre-wrap;">&lt;!DOCTYPE html
        PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
         "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
    &lt;html xmlns="http://www.w3.org/1999/xhtml" lang="en-US" xml:lang="en-US"&gt;
    &lt;head&gt;
    &lt;title&gt;Some text&lt;/title&gt;
    &lt;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /&gt;
    &lt;/head&gt;
    &lt;body&gt;
    &lt;h3&gt;Some text&lt;/h3&gt;&lt;form method="post" action="/path/script.pl?param1=val1&param2=val2" enctype="multipart/form-data"&gt;

    //form specific code

&lt;/form&gt;
    &lt;/body&gt;
    &lt;/html&gt;</pre>
    </body>

    </html>

Any help would be greatly appreciated.

3

Answers


  1. Try this:

    print(header(-type => 'text/html'));

    at least that worked for me.

    Login or Signup to reply.
  2. Is there anything wrong with the following code? –

    Yes. It doesn’t even compile, for starters.

    $ perl <<'__EOS__'
    use CGI qw(:standard);
    
    // other code
    
    print header("text/html"); // also tried just print header;
    my $banner = "Some text";
    print start_html($banner);
    print h3($banner);
    
    print start_form(-method=>"POST");
    // HTML form specific code
    print end_form;
    
    print end_html;
    __EOS__
    Bareword found where operator expected at - line 3, near "// other"
            (Missing operator before other?)
    Bareword found where operator expected at - line 3, near "other code"
            (Do you need to predeclare other?)
    Bareword found where operator expected at - line 5, near "// also"
            (Missing operator before also?)
    Bareword found where operator expected at - line 11, near "// HTML"
            (Missing operator before HTML?)
    Bareword found where operator expected at - line 11, near "specific code"
            (Do you need to predeclare specific?)
    syntax error at - line 3, near "// other "
    syntax error at - line 5, near "// also tried "
    syntax error at - line 11, near "// HTML form "
    Execution of - aborted due to compilation errors.
    

    But after changing all // to #, we get a perfectly fine program that doesn’t output any PRE elements.

    $ perl <<'__EOS__'
    use CGI qw(:standard);
    
    # other code
    
    print header("text/html"); # also tried just print header;
    my $banner = "Some text";
    print start_html($banner);
    print h3($banner);
    
    print start_form(-method=>"POST");
    # HTML form specific code
    print end_form;
    
    print end_html;
    __EOS__
    Content-Type: text/html; charset=ISO-8859-1
    
    <!DOCTYPE html
            PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
             "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" lang="en-US" xml:lang="en-US">
    <head>
    <title>Some text</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    </head>
    <body>
    <h3>Some text</h3><form method="post" action="http://localhost" enctype="multipart/form-data"></form>
    </body>
    </html>
    
    Login or Signup to reply.
  3. When I run your program, I get the following output:

    Content-Type: text/html; charset=ISO-8859-1
    
    <!DOCTYPE html
        PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
         "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" lang="en-US" xml:lang="en-US">
    <head>
    <title>Some text</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    </head>
    <body>
    <h3>Some text</h3><form method="post" action="http://localhost" enctype="multipart/form-data"></form>
    </body>
    </html>
    

    Which is exactly what I’d expect to see and nothing like what you are seeing. I can see two possibilities to explain this discrepancy:

    1. There is something in the # other code section of your program which is outputting a set of CGI headers (and, perhaps, the <pre> tags as well).
    2. You’re not actually running the code in a CGI environment and there is some unusual web configuration that takes the output from your program and embeds it in another web page.

    An easy way to differentiate between these two cases would be to run your program from the command line and see what output you get. If you get your output, then the problem is somewhere in the # other code, if you get my output, then the problem is in the web server configuration.

    As an aside, I’d highly recommend that you read the section entitled HTML Generation functions should no longer be used in a recent version of the CGI module documentation and the CGI::Alternatives page to see some suggestions for better approaches.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search