Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Unwanted line (CGI question)

by xiaoyafeng (Deacon)
on Oct 21, 2008 at 09:56 UTC ( [id://718432]=perlquestion: print w/replies, xml ) Need Help??

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

Hi monks!!

As my last nodenewbie questions for web in perl[OT], I've studied Web development these day. After reading a great number of document intensively and hearing some monks' suggestionsRe: newbie questions for web in perl[OT], I decide to make use of CGI::Application to build my web.

It seems all OK so far except one minor thing:

Every page output by CGI will add one line 'Content-Type: text/html; charset=ISO-8859-1'.

yesterday, I ask for it on chatterbox. some guru say there is a redundant statement: header; in my script. But I double check it, the answer is no. Below is my test snippet:

index.html
<!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-U +S"> <head> <title>login</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1 +" /> </head> <body> <form action = "action.cgi" method = "POST"> <p> name <input type = 'text' name = "name" value = ""> </p +> <p> passwd <input type = 'password' name = "password" value = +""> </p> <p> <input type = 'submit' name = 'submit' value = "submit"> + </p> </form> </body> </html>
action.cgi
#! C:\perl\bin\perl.exe use WebApp; my $webapp = WebApp->new(); $webapp->run();
WebApp.pm
use strict; use warnings; package WebApp; use base qw(CGI::Application); use Template; use CGI::Application::Plugin::TT; sub setup { my $c = shift; $c->start_mode('mode1'); $c->run_modes( 'mode1' => 'do_stuff'); } sub do_stuff { my $c = shift; my $h = $c->query(); my $name = $h->param('name'); my %params = ( name => $name, ); $c->tt_process('template.tt', \%params); } 1;
template.tt
<html> <head> <title>Untitled Document</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1 +" /> </head> <body> [% name %] </body> </html>

Sadly, the wield line is still there!

I suspect my platform (M$ + IIS6.0) caused it without any reasons. Anybody could tell me whether my guess is right or not. I hope perl gurus could enlighten me.

Thanks in advance!!

BTW, I found many questions about CGI on Seekers of Perl Wisdom.(Unfortunately, I can't found the answer to my question) It appears CGI is big, dark and mysterious forest to newbie. ;)

UPDATE

Thanks for everybody replied! I've found use NPH SCRIPTS could solve this question perfectly!


I am trying to improve my English skills, if you see a mistake please feel free to reply or /msg me a correction

Replies are listed 'Best First'.
Re: Unwanted line (CGI question)
by derby (Abbot) on Oct 21, 2008 at 10:37 UTC

    By default, CGI::Application uses CGI which will output the header 'content-type' by default:

    #!/usr/bin/perl use CGI qw/:standard/; print header, start_html('A Simple Example'), end_html;
    produces:
    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>A Simple Example</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> </head> <body> </body> </html>
    Normally, for web pages serving html, you want/need that line. Why do you think it's weird? What problems is it causing you?

    -derby
      Normally, for web pages serving html, you want/need that line. Why do you think it's weird? What problems is it causing you?

      Content-Type: text/html; charset=ISO-8859-1 is http header. IMHO, that means it's invisible to client. That's why I think it's weird.


      I am trying to improve my English skills, if you see a mistake please feel free to reply or /msg me a correction

        Right ... depending on the client. Are you seeing that line in the browser?

        -derby
Re: Unwanted line (CGI question)
by ggvaidya (Pilgrim) on Oct 21, 2008 at 11:46 UTC
    Hi Yafeng,

    What do you get if you execute action.cgi from the command line? I don't have CGI::Application set up here, so I can't try it out myself; but I'd love to help figure out what's going on.

    cheers,
    Gaurav
      Hi Gaurav,

      Below is the output:

      C:\CGI_test>perl action.cgi Content-Type: text/html; charset=ISO-8859-1 <html> <head> <title>Untitled Document</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1 +" /> </head> <body> </body> </html>
      Thanks for your help!

      I am trying to improve my English skills, if you see a mistake please feel free to reply or /msg me a correction
        I wouldn't worry too much about this. It's an instruction to the browser telling it what character set is being used to construct the page, in this case the 191 character Latin alphabet. Wikipedia will tell you more about this. You can also run a search for Meta tags in HTML, which will provide you with a bit more information on what they are and why they are present.
        That's what you should get. If you run your script as a CGI script, it is necessary for that line to be there. It won't appear in the browser. If it does, you're not running your script as a CGI script, so you have a server configuration issue.
        What ikegami said is correct. Running the script from the command line will display the Content-Type line. When you run the script in a web browser that line is used by the browser to know what type of content it should display, i.e. text/html, and the rest of the output renders in this format. So everything is working alright :) You can alter the Content-Type (using the header_add or header_props methods of CGI::Application) to other kinds, e.g. application/pdf or text/csv or whatever you like, and your web browser will react accordingly, i.e. either render the output in the browser, or ask you to save the file, or launch the application used to view that kind of file. Because the default Content-Type is text/html your web browser displays the subsequent output as a web page. Here's one page that lists a lot of Content Types (also known as MIME Types): http://www.w3schools.com/media/media_mimeref.asp
Re: Unwanted line (CGI question)
by Anonymous Monk on Oct 21, 2008 at 16:05 UTC

    If you're seeing it in the browser, it's something that happens with ActiveState Perl and IIS. On my server, it happens when I use perlis.dll.

    Read the section of the CGI docs that talks about using NPH scripts. I've found that setting NPH make the problem go away.

Re: Unwanted line (CGI question)
by ig (Vicar) on Oct 21, 2008 at 19:28 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://718432]
Approved by moritz
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (7)
As of 2024-04-26 08:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found