perl and CGI

Discussion in 'Perl Misc' started by Vit, Jun 9, 2009.

  1. Vit

    Vit Guest

    Hi All,

    I'm new to the perl language....

    I'm trying to understand how this language work and I hav develop the
    following code:

    in the webpage:
    <form action="\cgi-bin\my_email.cgi" method="post"> 
    <input type="submit" name="Submit" value="Submit" style="width:
    93px" />

    in the cgi-bin directori the "my_email.cgi" file:
    #!/usr/bin/perl
    print "Content-type: text/html\n\n";
    print "Hello, world!\n";

    I'm using this file just to test.... and the result is that is not
    working....

    when I click to the "submit" button, I get the following message:

    Server error!
    The server encountered an internal error and was unable to complete
    your request.

    Error message:
    Premature end of script headers: my_email.cgi

    If you think this is a server error, please contact the webmaster.

    Error 500
    Tue Jun 9 14:11:00 2009
    Apache/2.0.55 (Ubuntu) mod_ldap_userdir/1.1.11 PHP/4.4.2-1.1
    mod_vhost_ldap/1.0.0

    what can I do?? do you know what can I cahe to find out the issue???

    thanks

    Vit
     
    Vit, Jun 9, 2009
    #1
    1. Advertisements

  2. Purists will likely point out that "perl" (lower case) is the
    interpreter, while "Perl" (upper case) is the name of the programming
    language that is interpreted by perl.
    If you want to learn programming in Perl then I _STRONGLY_ suggest to
    start with good old plain Perl programs and not to add the complexity
    and problems of CGI, web servers, and the WWW on top of learning the
    language.
    And this is a perfect example of why mixing everything together is a bad
    idea. This error message is _not_ a Perl error message.
    Your program is perfectly fine. When run from the command line it will
    print those intended 3 lines and then terminate without any error
    message.
    That means your problem lies in a different area, in your case probably
    in a misunderstanding of the CGI protocol.
    This infamous error message is listed in the Perl FAQ, see
    perldoc -q 500
    for details.

    jue
     
    Jürgen Exner, Jun 9, 2009
    #2
    1. Advertisements

  3. Vit

    Vit Guest

    And this is a perfect example of why mixing everything together is a bad
    thank's for the suggestion....

    I was just trying to figure out how perl and the web server works..

    I have already developed a perl file to manage a web form (send the
    forms field to an email address)...

    and the file seems to be ok..

    but when I run it on the server it gives me the same error...

    so I was wondering what was wrong and how can I debug the issue....

    any suggestion to find out the problem??

    thanks

    Vit
     
    Vit, Jun 9, 2009
    #3
  4. As I mentioned before, you DO NOT have an issue with Perl. Had you
    written your program in C or Pascal or Basic or Fortran you would still
    get the same error message.
    Your issue is with the Common Gateway Interface. Therefore asking in a
    NG that deals with CGI will probably address your problem more
    accurately. Did you read the FAQ I pointed out? It contains several
    pointers to more information.

    jue
     
    Jürgen Exner, Jun 9, 2009
    #4
  5. Jürgen gave you a suggestion. If you run

    perldoc -q 500

    at the command line, you get an entry in the Perl FAQ that mentions
    various possible reasons for the error message. The document can also be
    found on the web:

    http://perldoc.perl.org/perlfaq9.html

    Since the program looks good, let me still give you a few hints:

    - Is the path to perl correct?
    - Did you upload the program in ASCII / TEXT mode?
    - Is the program executable? (0755 works for most web servers)
     
    Gunnar Hjalmarsson, Jun 9, 2009
    #5
  6. Any suggestions of such a NG?
     
    Gunnar Hjalmarsson, Jun 9, 2009
    #6
  7. <form action="/cgi-bin/my_email.cgi" method="post">

    ....
    The content you send and the content type you specify conflict:

    #!/usr/bin/perl

    use strict;
    use warnings;

    $| = 1;

    use CGI qw( -no_xhtml );

    my $cgi = CGI->new;

    print $cgi->header('text/html'),
    $cgi->start_html(),
    $cgi->p('Hello World!'),
    $cgi->end_html();

    __END__

    As was pointed out, perldoc -q 500

    Sinan

    --
    A. Sinan Unur <>
    (remove .invalid and reverse each component for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://www.rehabitation.com/clpmisc/
     
    A. Sinan Unur, Jun 9, 2009
    #7
  8. Vit

    ccc31807 Guest

    Follow these three steps and this will provide a foundation:
    1. Study (and I do mean STUDY) Learning Perl by Randal Schwartz
    2. Study CGI/Perl by Diane Zak
    3. Study the CGI documentation at the Apache Foundation website
    (apache.org) with particular reference to httpd.conf (assuming that
    you are using httpd to serve your your CGI scripts)

    (optional) 4. I have found MySQL and Perl for the Web by Paul Dubois
    valuable for actually building useful applications, and I strongly
    recommend that you consider studying that book. It's not an entry
    level book and you need to have a firm grasp of steps 1 through 3
    before you can understand it.

    CC.
     
    ccc31807, Jun 9, 2009
    #8
  9. Sorry for asking stupid and even off-topic, but: while true that
    shouldn't cause a server error, should it?

    jue
     
    Jürgen Exner, Jun 9, 2009
    #9
  10. I don't do CGI and am not interested in it, therefore I have no
    information about the validity of any of the following NGs, which a
    quick search for CGI in the NG directory of my provider returned:

    comp.infosystems.www.authoring.cgi
    perl.beginners.cgi

    In other languages:
    de.comm.infosystems.www.authoring.cgi
    de.comp.lang.perl.cgi
    it.comp.www.cgi
    as well as several subgroups fido7.ru.cgi.* including fido7.ru.cgi.perl
    in Russian

    jue
     
    Jürgen Exner, Jun 9, 2009
    #10
  11. Vit

    J. Gleixner Guest

    Change that to "/cgi-bin/my_email.cgi". Also, check in your
    Web server's error log.
     
    J. Gleixner, Jun 9, 2009
    #11
  12. Vit

    Nathan Keel Guest

    All of the CGI groups are dead (and I mean literally dead, not just low
    activity). They used to be pretty busy, but died off about 5 years
    ago. There's not a lot to get Perl working for CGI, mainly just the
    header output, permissions set properly and a few other minor things,
    so I'm not surprised CGI specific groups are no more.
     
    Nathan Keel, Jun 9, 2009
    #12
  13. That's what I suspected. The natural follow-up question is: Why on earth
    are you so quick to reply to CGI related questions??
    Dead since long.
    NNTP interface at nntp.perl.org to the mailing list beginners-cgi
    http://lists.cpan.org/showlist.cgi?name=beginners-cgi
    (which actually is alive)
     
    Gunnar Hjalmarsson, Jun 9, 2009
    #13
  14. 3.5. Find out where your web server keeps the log files (especially the
    error log) and READ THEM!
    hp
     
    Peter J. Holzer, Jun 9, 2009
    #14
  15. Indeed not. The server does not do HTML validation, that's the work of
    the HTML parser on the client.

    Possible causes for the error seen by the OP:

    - Permissions on the CGI script are wrong.

    - Webserver is looking in another directory for CGI scripts.

    - I saw the OP using backslashes to specify the path in the URL. I don't
    know if that's going to work with Apache (the server output mentions
    it's Ubuntu, so I assume Apache as httpd).

    - Perl is not in /usr/bin on the webserver.

    Mart
     
    Mart van de Wege, Jun 9, 2009
    #15
  16. Vit

    Guy Guest

    Hi All,

    I'm new to the perl language....

    I'm trying to understand how this language work and I hav develop the
    following code:

    in the webpage:
    <form action="\cgi-bin\my_email.cgi" method="post">
    <input type="submit" name="Submit" value="Submit" style="width:
    93px" />

    in the cgi-bin directori the "my_email.cgi" file:
    #!/usr/bin/perl
    print "Content-type: text/html\n\n";
    print "Hello, world!\n";

    I'm using this file just to test.... and the result is that is not
    working....

    when I click to the "submit" button, I get the following message:

    Server error!
    The server encountered an internal error and was unable to complete
    your request.

    Error message:
    Premature end of script headers: my_email.cgi

    If you think this is a server error, please contact the webmaster.

    Error 500
    Tue Jun 9 14:11:00 2009
    Apache/2.0.55 (Ubuntu) mod_ldap_userdir/1.1.11 PHP/4.4.2-1.1
    mod_vhost_ldap/1.0.0

    what can I do?? do you know what can I cahe to find out the issue???

    thanks

    Vit

    -----------------------------
    Did you solve your problem?

    1. Make sure you upload your Perl script to the correct folder on your web
    server, which I think would be something like /cgi-bin. Otherwise you may
    get a 404 error, saying it can't find the file.

    2. Once on the server, ensure that you change the file's properties to
    executable, otherwise, I think you'll get error 403, something about
    refusing to display web page.

    3. I get the 500 Internal Server Error when I have an error in my code. The
    following script for example works fine:
    #!/usr/bin/perl
    $command= $];
    $title = "Perl Version";
    print "Content-type: text/html\n\n";
    print "<html><head><title>$title</title></head><body>";
    print "<h1>$title</h1>\n";
    print "Perl version : ".$command;
    print "<HR> \n";
    system 'perl -v';

    It displays the following -- yes I know it would be great if I had Perl 10
    :(

    Perl Version
    Perl version : 5.006001
    -----------------------
    This is perl, v5.6.1 built for i686-linux Copyright 1987-2001, Larry Wall
    Perl may be copied only under the terms of either the Artistic License or
    the GNU General Public License, which may be found in the Perl 5 source kit.
    Complete documentation for Perl, including FAQ lists, should be found on
    this system using `man perl' or `perldoc perl'. If you have access to the
    Internet, point your browser at http://www.perl.com/, the Perl Home Page.

    If however, I forget a semi colon like:
    #!/usr/bin/perl
    $command= $] # <--- Forgot Semicolon
    $title = "Perl Version";
    print "Content-type: text/html\n\n";
    print "<html><head><title>$title</title></head><body>";
    print "<h1>$title</h1>\n";
    print "Perl version : ".$command;
    print "<HR> \n";
    system 'perl -v';

    I get the following:
    500 Internal Server Error
    The server encountered an internal error or misconfiguration and was unable
    to complete your request.
    Please contact the server administrator, or webmaster and inform them of the
    time the error occurred, and anything you might have done that may have
    caused the error.
    More information about this error may be available in the server error log.

    4. You can also access your perl scripts directly instead of submitting a
    form, for example, type the following in your address bar, and also use
    /cgi-bin/my_email.cgi instead of \cgi-bin\my_email.cgi like Sinan said.
    http://yourserver/cgi-bin/my_email.cgi

    5. On a side note, I use the .pl extension instead of .cgi, but I may be a
    minority? I think I even read that it was better to not use any extension
    claiming that people don't have to know that the script is Perl or whatever?

    6. Your print code looks ok, so perhaps the trouble is in your (pound bang)
    line? Like you, mine is:
    #!/usr/bin/perl
    But perhaps you require it like this: (user) instead of (usr)???
    #!/user/bin/perl

    7. Are you sure your system supports Perl? If so, check if there are some
    default Perl scripts in the /cgi-bin folder and check if they work, and if
    they do, start with them.

    Finally, I'm quite new to this group and Perl so I expect to be corrected
    here!

    PS. I recently learned that instead of uploading my scripts to test them, I
    could install Strawberry Perl on my Windows XP and test my scripts locally.
    I upload them to the server only once the bugs are out.

    Cheers,
    Guy D.
     
    Guy, Jun 10, 2009
    #16

  17. [ snip quoted text with no quote marks ]



    [ snip original text, but how can anybody tell that it is not quoted? ]


    Please learn the proper way of composing a followup.
     
    Tad J McClellan, Jun 10, 2009
    #17

  18. There is no Perl 10.

    There isn't even a Perl 6 (just yet).

    It would be great if you had Perl 5.10.
     
    Tad J McClellan, Jun 10, 2009
    #18
  19. Vit

    Vit Guest

    Thanks for the answer!!! really appreciate!!!

    I have solve the issue!!! was the permission on the .pl file (I set
    utp the permission on the falder, but not on the file...

    now I'm trying to debug my file.... it works (no error in the
    browser!!!), but it is not doing what I'd like to...

    #!/usr/bin/perl

    &send_email;

    sub send_email
    {
    $to = "vit\@domain.com";
    open(MAIL, "|/usr/sbin/sendmail");

    print MAIL "From: bob\@domain.com\n";
    print MAIL "To: $to\n";
    print MAIL "Subject: Web Enqiry\n\n";

    close (MAIL);
    }

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

    it doesn't give me an error, but I dont receive any mail....

    any suggestion???

    thanks

    Vit
     
    Vit, Jun 10, 2009
    #19
  20. use strict;
    use warnings;
    use CGI;
    use CGI::Carp qw(fatalsToBrowser);


    Always check for success/failure when opening a file handle.
    Use lexical file-handles and the three-argument open.
    (assuming your ancient perl supports them)
    Aren't you supposed to provide a message body terminated by a full-stop
    on a line by itself?
    I imagine sendmail was waiting for a message body?
    Don't promise HTML if you are not willing to keep your promise.
    That isn't HTML.
    Ask perl to tell you of any errors! (see comment above).
    Make sure errors are written somewhere you are looking.
    See above `use CGI::Carp qw(fatalsToBrowser);`
    Back in the days of Perl 4 I used
    BEGIN {
    open(STDERR ,'>&STDOUT')
    || die "Can't redir stderr to out: $!\n";
    $| = 1; # Make it unbuffered
    print "Content-type: text/html\n\n";
    }
    This is more in keeping with your rather quaint and archaic coding style
    but I can't imagine your Perl installation could be so backward as to
    lack a better way to redirect errors to the browser.
    Ask Perl to tell you why.
    See above.

    I'd make more use of modules for CGI and for email.
     
    RedGrittyBrick, Jun 10, 2009
    #20
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.