Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Novice trying to find my way

by mortalhero (Novice)
on Sep 16, 2009 at 20:35 UTC ( [id://795706]=perlquestion: print w/replies, xml ) Need Help??

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

I've decided to try and learn perl. My first goal is to simply send a number that a visitor enters into my web page to a perl script, and then send a number back to the same web page. I cannot find a simple answer to this anywhere.

I’m trying to use Frontpage to handle the HTML code and organize everything. I’ve installed iiss for Windows XP and ActivePerl. I was successful running simple perl scripts from the Command Prompt such as… print “hello world\n”;

Also, I was successful with getting the following to appear in a browser;

print "Content-Type: text/html\n\n"; print "<HTML>\n"; print "<HEAD>\n"; print "<TITLE>Hello World</TITLE>\n"; print "</HEAD>\n"; print "<BODY>\n"; print "<H4>Hello World</H4>\n"; print "<P>\n"; print "Your IP Address is $ENV{REMOTE_ADDR}.\n"; print "<P>"; print "<H5>Have a nice day</H5>\n"; print "</BODY>\n"; print "</HTML>\n";

But when I create a simple browser page, mypage.htm, in FrontPage and try to run the following and get it to appear in mypage.htm, it opens the Command Prompt and dumps the page’s html code to it;

print "Content-Type: text/html\n\n"; open HTML, "c:/mypage.htm" or die $!; while( <HTML> ) { print; } close HTML;

I guess my question is, what am I doing wrong? How can I simply get something to appear, where I want it, in my browser from perl?

Thanks, Dave

Replies are listed 'Best First'.
Re: Novice trying to find my way
by Sewi (Friar) on Sep 16, 2009 at 22:06 UTC
    In case of any error (expecially the errors you don't find easily), print is your friend.
    There are three default I/O channels:
  • STDIN (Standard input) where user input at the command line or usually POST - data gets delivered
  • STDOUT (Standard output) is where every print defaults to, the is usually the content send to the browser
  • STDERR (Standard error) for error messages which usually go to an error logfile
  • If you add "debugging prints", which are just prints which help you to understand what your program is doing where and when, first be sure that you'll see them.
    Use your first (running) program and add a line:
    print STDERR "Hello error world\n";
    Now look at the error logfile of your webserver and you should see the line "Hello error world". If you don't see it, your webserver doesn't work like the Apache "default" and we need to redirect the debug messages. Try this as your first script line (after #!/usr/bin/perl or anything else which may be needed to start Perl for this script):
    open STDERR,'>>C:\perl_error.txt';
    Now you should get a file called C:\perl_error.txt with your test debug line. I don't use Perl on Windows myself, so I can't test it myself, you may need to replace the filename by "C:\\perl_error.txt" or "C:/perl_error.txt". Please try it out until you get the debug message.

    Now you are sure that you could read messages from your program. If you had to include a open STDERR - line, copy it to the non-working script.
    Insert ikegami's hint here: Check your error log (where you just got the test debug line "Hello error world") for any open errors for mypage.htm.
    If you don't see any errors, it's a good idea to copy the data to the error logfile in addition to the existing print:

    print STDERR $_;
    You could also add other prints to STDERR to see what your script does and in what order.

    PS: You should look at the CGI module once you start things like HTTP parameter parsing and building complete websites by-script, but it's too complex to start playing around with Perl.

    (For the Perl experts: I know of select and other things which could make some of the above statements wrong, but please remember that this comment is written for a Perl novice who doesn't need to care about such things currently.)

      Thanks, Where can I find my server's error logfile?

Re: Novice trying to find my way
by ikegami (Patriarch) on Sep 16, 2009 at 21:11 UTC
    Did you check the error log to see if the open died?
Re: Novice trying to find my way
by Anonymous Monk on Sep 16, 2009 at 22:09 UTC
Re: Novice trying to find my way
by graff (Chancellor) on Sep 17, 2009 at 04:38 UTC
    When you say:

    I was successful with getting the following to appear in a browser;

    Do you mean that the browser showed the entire script exactly as you posted it (including all the repetitions of "print" and all the quotes and dollar signs and "ENV{REMOTE_ADDR}")? Or do you mean that the browser actually showed "Hello World" as the title, and as a heading, and you saw an actual IP address (presumably 127.0.0.1)?

    If the former, it means you don't yet have your IIS web server configured to actually execute perl cgi scripts. I don't really know about configuring IIS (I suppose you have to navigate a bunch of dialog windows and menus and crap like that), but in general, e.g. using Apache, there's a config file that you edit, and somewhere in there is a section about designating the directory where cgi scripts will be kept. (Don't they have Apache servers for windows?)

    You want to make sure that only a specific directory (or small number of specific directories), which you manage and protect very carefully, are designated for storing executable cgi scripts. Then you put your cgi script in that directory, and type the url that corresponds to this path and script file into the browser, so when the server gets that url request, it runs the script, and sends the script output (the html data) back to the browser.

    My apologies if you know all this already -- it wasn't clear from the OP how far along (or behind) you are.

Re: Novice trying to find my way
by ww (Archbishop) on Sep 16, 2009 at 21:47 UTC

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (7)
As of 2024-04-16 09:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found