outputting the results of something.pl

Discussion in 'Perl Misc' started by jim jowski, Apr 17, 2005.

  1. jim jowski

    jim jowski Guest

    I've written an html form that will pass three values to a script called
    'something.pl' which lives in my cgi-bin directory. When something.pl
    is executed; I get the following error message:

    [Sun Apr 17 08:24:37 2005] [error] [client 127.0.0.1] malformed header
    from script. Bad header=<html><head><title>the title</:
    /var/www/cgi-bin/something.pl

    'something.pl' is nothing more than:

    #!/usr/bin/perl

    $dollar0 = @ARGV[0];
    $dollar1 = @ARGV[1];
    $dollar2 = @ARGV[2];

    print "<html><head><title>the title</title></head>";
    print "<body><p>dollar0 = $dollar0\n";
    print "<p>\tdollar1 = $dollar1\n";
    print "<p>\t\tdollar2 = $dollar2\n";
    print "</body></html>";

    and the html form that calls 'something.pl' is:

    <html><head><title>input</title></head>
    <body><form method="post" action="/cgi-bin/something.pl">
    <P>value 0: <input type="text" name="v0" size=10></p>
    <P>value 1: <input type="text" name="v1" size=10></p>
    <P>value 2: <input type="text" name="v2" size=10></p>
    <P><input type="submit" name="submit" value="calculate"></P>
    </form>
    </body>
    </html>

    I know that I can do all this with PHP but I have too much time invested
    in perl code that I can clone to migrate. Once I have this simple
    output problem solved, I can hopefully do the rest of the project this week.

    Thanks,
    Jim
    jim jowski, Apr 17, 2005
    #1
    1. Advertising

  2. jim jowski

    Brian Wakem Guest

    jim jowski wrote:

    > I've written an html form that will pass three values to a script called
    > 'something.pl' which lives in my cgi-bin directory. When something.pl
    > is executed; I get the following error message:
    >
    > [Sun Apr 17 08:24:37 2005] [error] [client 127.0.0.1] malformed header
    > from script. Bad header=<html><head><title>the title</:
    > /var/www/cgi-bin/something.pl



    You need to print a content type header first.

    print "Content-type: text/html\n\n";



    --
    Brian Wakem
    Brian Wakem, Apr 17, 2005
    #2
    1. Advertising

  3. jim jowski <> wrote:

    > I get the following error message:
    >
    > [Sun Apr 17 08:24:37 2005] [error] [client 127.0.0.1] malformed header
    > from script. Bad header=<html><head><title>the title</:
    > /var/www/cgi-bin/something.pl


    > #!/usr/bin/perl
    >
    > $dollar0 = @ARGV[0];

    ^
    ^

    You should always enable warnings when developing Perl code!

    CGI programs do not normally get command line arguments.

    What info are you expected to get into $dollar0?

    You should use the CGI.pm module to get access to the form values.



    Output a proper header before the body:

    print "Content-Type: text/html\n\n";

    The CGI.pm module can write a proper header for you too.


    > print "<html><head><title>the title</title></head>";



    That is not a proper header, so you get a message about an
    improper header.


    > I know that I can do all this with PHP



    So you already know how to print a proper header in PHP?


    > Once I have this simple
    > output problem solved,



    But you don't know how to print a proper header in Perl?

    You use Perl's print() function to make output.



    Have you seen Perl's Frequently Asked Questions about CGI programming?

    perldoc -q CGI

    Where can I learn about CGI or Web programming in Perl?

    What is the correct form of response from a CGI script?

    My CGI script runs from the command line but not the browser. (500
    Server Error)

    How can I get better error messages from a CGI program?

    How do I make sure users can't enter values into a form that cause my
    CGI script to do bad things?

    How do I decode a CGI form?


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Apr 17, 2005
    #3
  4. jim jowski

    John Guest

    Maybe you need to backquote or escape-out the slashes in your literal

    instead of:

    <title>the title</title>

    maybe:

    <title>the title<\/title>
    John, Apr 17, 2005
    #4
  5. jim jowski

    jim jowski Guest

    Tad McClellan wrote:
    > jim jowski <> wrote:
    >
    >
    >>I get the following error message:
    >>
    >>[Sun Apr 17 08:24:37 2005] [error] [client 127.0.0.1] malformed header
    >>from script. Bad header=<html><head><title>the title</:
    >>/var/www/cgi-bin/something.pl

    >
    >
    >>#!/usr/bin/perl
    >>
    >>$dollar0 = @ARGV[0];

    >
    > ^
    > ^
    >
    > You should always enable warnings when developing Perl code!
    >
    > CGI programs do not normally get command line arguments.
    >
    > What info are you expected to get into $dollar0?
    >
    > You should use the CGI.pm module to get access to the form values.
    >
    >
    >
    > Output a proper header before the body:
    >
    > print "Content-Type: text/html\n\n";
    >
    > The CGI.pm module can write a proper header for you too.
    >
    >
    >
    >>print "<html><head><title>the title</title></head>";

    >
    >
    >
    > That is not a proper header, so you get a message about an
    > improper header.
    >
    >
    >
    >>I know that I can do all this with PHP

    >
    >
    >
    > So you already know how to print a proper header in PHP?
    >
    >
    >
    >>Once I have this simple
    >>output problem solved,

    >
    >
    >
    > But you don't know how to print a proper header in Perl?
    >
    > You use Perl's print() function to make output.
    >
    >
    >
    > Have you seen Perl's Frequently Asked Questions about CGI programming?
    >
    > perldoc -q CGI
    >
    > Where can I learn about CGI or Web programming in Perl?
    >
    > What is the correct form of response from a CGI script?
    >
    > My CGI script runs from the command line but not the browser. (500
    > Server Error)
    >
    > How can I get better error messages from a CGI program?
    >
    > How do I make sure users can't enter values into a form that cause my
    > CGI script to do bad things?
    >
    > How do I decode a CGI form?
    >
    >


    I'll check "perldoc -q CGI" and try to get smart. :^)

    Actually, the problem is one of making the form available and passing
    the POSTings to a script that hides its source code. After I initially
    posted the question, I found the FAQ reference to:

    print "Content-Type: text/html\n\n";

    and was able to output an HTML page but no $dollar0 - $dollar2 inputs.
    At least I'm heading down the right path regarding getting the output
    but now passing the input is the problem.

    Thanks again,
    Jim
    jim jowski, Apr 17, 2005
    #5
  6. jim jowski

    jim jowski Guest

    jim jowski wrote:
    > I've written an html form that will pass three values to a script called
    > 'something.pl' which lives in my cgi-bin directory. When something.pl
    > is executed; I get the following error message:
    >
    > [Sun Apr 17 08:24:37 2005] [error] [client 127.0.0.1] malformed header
    > from script. Bad header=<html><head><title>the title</:
    > /var/www/cgi-bin/something.pl
    >
    > 'something.pl' is nothing more than:
    >
    > #!/usr/bin/perl
    >
    > $dollar0 = @ARGV[0];
    > $dollar1 = @ARGV[1];
    > $dollar2 = @ARGV[2];
    >
    > print "<html><head><title>the title</title></head>";
    > print "<body><p>dollar0 = $dollar0\n";
    > print "<p>\tdollar1 = $dollar1\n";
    > print "<p>\t\tdollar2 = $dollar2\n";
    > print "</body></html>";
    >
    > and the html form that calls 'something.pl' is:
    >
    > <html><head><title>input</title></head>
    > <body><form method="post" action="/cgi-bin/something.pl">
    > <P>value 0: <input type="text" name="v0" size=10></p>
    > <P>value 1: <input type="text" name="v1" size=10></p>
    > <P>value 2: <input type="text" name="v2" size=10></p>
    > <P><input type="submit" name="submit" value="calculate"></P>
    > </form>
    > </body>
    > </html>
    >
    > I know that I can do all this with PHP but I have too much time invested
    > in perl code that I can clone to migrate. Once I have this simple
    > output problem solved, I can hopefully do the rest of the project this
    > week.
    >
    > Thanks,
    > Jim


    I was able to get the output of 'something.pl' to display by changing it to:

    #!/usr/bin/perl

    use CGI;
    $query = new CGI;
    @names = $query->param; #the names of the passed parameters
    $dollar0 = $query->param('v0');
    $dollar1 = $query->param('v1');
    $dollar2 = $query->param('v2');

    print "Content-type: text/html\n\n";
    print "<HTML><HEAD><TITLE>the TITLE</TITLE></HEAD>\n\n<BODY>";
    print "<p>dollar0 = $dollar0\n";
    print "<p>\tdollar1 = $dollar1\n";
    print "<p>\t\tdollar2 = $dollar2\n";
    print "</BODY></HTML>";

    This brought in the passed parameter values by using $query-> and the
    "Content-..." line with two newlines made the header legal. (Good thing
    too because the programming police were on their way...)

    Thanks again to all, especially Tad McClellan, for the gentle guidance
    in the right direction. Maybe these snippets will be useful to others
    "when" the same question arises.

    Jim
    jim jowski, Apr 17, 2005
    #6
  7. John wrote:
    > Maybe you need to backquote or escape-out the slashes in your literal
    >
    > instead of:
    >
    > <title>the title</title>
    >
    > maybe:
    >
    > <title>the title<\/title>


    He would if he was doing:

    print qq/<title>the title<\/title>/;

    Instead of:

    print "<title>the title</title>";

    Slashes are not special in double quoted strings.



    John
    --
    use Perl;
    program
    fulfillment
    John W. Krahn, Apr 17, 2005
    #7
  8. jim jowski <> wrote:
    > Tad McClellan wrote:
    >> jim jowski <> wrote:


    >>>$dollar0 = @ARGV[0];



    >> CGI programs do not normally get command line arguments.


    >> You should use the CGI.pm module to get access to the form values.



    > Actually, the problem is one of making the form available



    > At least I'm heading down the right path regarding getting the output
    > but now passing the input is the problem.



    CGI programs do not normally get command line arguments.

    You should use the CGI.pm module to get access to the form values.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Apr 17, 2005
    #8
  9. On Sun, 17 Apr 2005, jim jowski wrote:

    [excessive quotage snipped]

    > I was able to get the output of 'something.pl' to display by
    > changing it to:
    >
    > #!/usr/bin/perl


    A pity that you offer indirect thanks to Tad, while apparently
    ignoring his oft-repeated advice...[1]

    > use CGI;
    > $query = new CGI;


    wibble

    > @names = $query->param; #the names of the passed parameters
    > $dollar0 = $query->param('v0');
    > $dollar1 = $query->param('v1');
    > $dollar2 = $query->param('v2');
    >
    > print "Content-type: text/html\n\n";
    > print "<HTML><HEAD><TITLE>the TITLE</TITLE></HEAD>\n\n<BODY>";


    Talk about getting a dog and barking for yourself...

    CGI.pm is happy to generate this correctly for you (in xhtml or html
    flavours at your option).

    Omitting the appropriate charset specification from the Content-type:
    header can have some unpleasant consequences when handling forms
    input. It's also a security exposure according to CA-2000-02 (a
    rather complex bag of stuff, but the advice in this regard was clear
    enough).

    > "Content-..." line with two newlines made the header legal.


    Well, yes, as far as *that* went.

    > Thanks again to all, especially Tad McClellan, for the gentle
    > guidance in the right direction.


    [1] But you still didn't take the advice that's churned out here
    countless times a day, to ask Perl for all the help it can give.
    Which means using strict, and (at least during development) using
    warnings. And in the case of CGI one should make taint checking (-T)
    one's default.
    Alan J. Flavell, Apr 17, 2005
    #9
  10. jim jowski

    Joe Smith Guest

    John wrote:
    > Maybe you need to backquote or escape-out the slashes in your literal


    No, jim's problem has nothing to do with slashes.
    Joe Smith, Apr 18, 2005
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Guest
    Replies:
    4
    Views:
    477
    Guest
    Oct 13, 2004
  2. Pekka Järvinen
    Replies:
    2
    Views:
    664
    Richard Tobin
    Apr 29, 2008
  3. Jenny Purcell

    Outputting results as html in a file

    Jenny Purcell, Oct 2, 2007, in forum: Ruby
    Replies:
    1
    Views:
    119
    Jesús Gabriel y Galán
    Oct 2, 2007
  4. Replies:
    4
    Views:
    218
    Tad McClellan
    Jun 1, 2007
  5. Replies:
    9
    Views:
    159
Loading...

Share This Page