Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

CGI question

by spencerr1 (Novice)
on Feb 08, 2013 at 16:02 UTC ( #1017847=perlquestion: print w/ replies, xml ) Need Help??
spencerr1 has asked for the wisdom of the Perl Monks concerning the following question:

I'm learning CGI bought a couple of books. I can run scripts from one book but I can't from the other book. I have tried to fix this introductory script but no output.

<html> <head> <title>Animal</title> </head> <body> <form action="cgi-bin/animal.cgi" METHOD=POST> Which animal? <INPUT type="text" name="animal"> <P><INPUT type="submit"> </form> </body> </html>
CGI script #!/usr/local/bin/perl -wT use strict; use CGI ':standard'; my $animal; $animal = param('animal'); print "Content-type: text/html\n\n"; print "Show me the $animal";

Any ideas. Thanks

Comment on CGI question
Select or Download Code
Re: CGI question
by tobyink (Abbot) on Feb 08, 2013 at 16:27 UTC

    Is the path to Perl (/usr/local/bin/perl) correct? Do the web server's logs contain any clues? Can you run the CGI script successfully at the command-line?

    package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name

      Will check that out. Thanks

Re: CGI question
by toolic (Chancellor) on Feb 08, 2013 at 16:28 UTC

      thanks for the website. It's in my notes

Re: CGI question
by 7stud (Deacon) on Feb 08, 2013 at 18:15 UTC

    I can run scripts from one book but I can't from the other book.

    That should be pretty easy to troubleshoot. What are the differences in the html between the two books? Specifically, you should examine the form's action attribute in the html. Any differences in the paths listed there? Can you use one book's html with the other book's working perl script?

    What are the differences in the perl scripts between the two books? Do both books use script names that have the same extension: .cgi v. .pl? Your server may look at a file's extension to decide whether a file is a perl script and should be executed or whether the file should be returned as text.

    What server are you using?

    Did you make both your perl scripts executable? $ chmod a+x prog.pl

    You might need this shebang line:

    #!/usr/local/env perl

    When you are testing any cgi script, you should send errors to the browser by adding this line:

    use CGI::Carp qw(fatalsToBrowser);

    Otherwise, your perl script will fail silently, and you will have to check your server's logs to discover the error.

    What does your server's log file say? Find out where it is located, and look at the last 20 lines or so.

    You should always post what error you got, or if you didn't get an error, you should state what you expected to happen, and what actually happened. Do not say, "My program doesn't work. Help!"

      As a note to the grandparent node, a little bit of emphasis on what 7stud said:

      When you are testing any cgi script, you should send errors to the browser by adding this line: (emphasis added)
      Just make sure it does not go into production with errors to the browser :-)

      --MidLifeXis

      O.K. I'm on it. No sleep till Brooklyn

Re: CGI question
by Khen1950fx (Canon) on Feb 08, 2013 at 21:01 UTC
    How about this?
    #!/usr/bin/perl -T use strict; use warnings; use CGI qw/:standard/; my $q = new CGI; print $q->header; print $q->start_html('Animal'); print <<'END'; <html> <head> <title>Animal</title> </head> <body> <form action="cgi-bin/animal.cgi" METHOD=POST> Which animal? <INPUT type="text" name="animal"> <P><INPUT type="submit"> </form> </body> </html> END print "Show me the <INPUT> ", 'animal', " </INPUT>\n"; print $q->end_html;

      Thanks I will give this a go

Re: CGI question
by 7stud (Deacon) on Feb 09, 2013 at 00:35 UTC
    By the way, perl allows you to test cgi scripts on the command line. Normally in order to run a cgi script, you need to start your server, then load an html page in your browser, then click a button, which is programmed to call your cgi script. But a cgi script essentially just reads some name/value pairs that the server provides, and then the cgi script executes some perl code and prints something. A cgi script has no idea that there is any such a thing as an html form or even that the internet has been invented. Because of that state of affairs, perl can run a cgi script directly, like a regular perl program, if you specify some name=value pairs like this:
    $ perl animal.pl name1=value1 name2=value2

    To figure out what name=value pairs your cgi script is expecting, you just need to examine the code. Your cgi script has this in it:

    $animal = param('animal');

    That line retrieves the value for the name "animal"(which corresponds to the name of an html form element). That means you can run your perl script on the command line if you provide the name animal along with any value that you would have typed into the html form element, e.g.:

    $ perl animal.pl animal=tiger

    And if you hit return after that, you will get this output:

    Content-type: text/html Show me the tiger

    In the normal course of things, that text would be sent to a browser, which knows how to interpret the headers (the lines at the beginning of the output that come before two consecutive newlines), and the browser knows how to display the text following the two newlines. In any case, you can quickly find out if the problem you are having is with the cgi script by running it from the command line. Also, running a perl script from the command line is a good way to see exactly what your cgi script is sending to the browser, which may come in handy as your cgi scripts get more complex.

      Excellent often wondered how to do that. Thanks for the tips

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1017847]
Approved by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (12)
As of 2014-08-01 15:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Who would be the most fun to work for?















    Results (27 votes), past polls