Forum Moderators: coopster & phranque

Message Too Old, No Replies

New to CGI programming ~confused~

         

MasterBoo

3:23 pm on Aug 21, 2005 (gmt 0)

10+ Year Member



I need to send a parameter to the CGI page and then redirect it to a different page according to the parameter received.
How do I send parameters? How do I recevie them? How do I redirect? :P
Thanks a lot :P

MasterBoo

3:27 pm on Aug 21, 2005 (gmt 0)

10+ Year Member



Do I...
1. Send the parameters using the GET method as usual, with the question mark (.....?param=value)?
2. Get the param using $myParam = $cgi->param('paramname');?
3. Use the following commands to redirect
$self->header_type('redirect');
$self->header_props(-url => "http://www.blahblah.com");?

Thanks :S

KevinADC

5:12 pm on Aug 21, 2005 (gmt 0)

10+ Year Member



have you actually tried anything yet? Are you still having problems or still confused?

You should use CGI.pm to fetch the query string data and do the redirction, and yes, you must use a? after the URI and then a name/value pair:

query string:

www.site.com?name=value

perl script:


[3]
#!perl
use CGI qw/:standard/;
my $query = param('name');
print redirect('http://www.website.com/$query');
[/3]

I put $query on the and of the URL assuming you wanted to use it in the redirection, if not, just remove it.

MasterBoo

6:30 am on Aug 22, 2005 (gmt 0)

10+ Year Member



Ok, got it all except for one thing-
You should use CGI.pm to fetch the query string data and do the redirction
.
Is there more than one file extension for CGI Perl scripts? I mean, I saw .cgi, .pl, and now .pm? :o
What's up with that, which one should I use? :S
Thanks :)

drhowarddrfine

12:50 pm on Aug 22, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



.cgi is for CGI scripts written in any language.
.pl is a perl script.
But you can use 'nothing at all' because the shebang line (#!) will tell the server to use the perl compiler.
.pm is for 'perl modules' which have a different format and use but are perl code nonetheless. Essentially, .pm files are a collection of subroutines (libraries) to be called by perl programs.

KevinADC

7:41 pm on Aug 22, 2005 (gmt 0)

10+ Year Member



drhowarddrfine answered the question very well.

CGI or cgi is actually a protocol, Common Gateway Interface, which tells computers how to share data over the internet. The .cgi extension became synonymous with web based perl scripts back in the day and has managed to stick to this very day.

.pl is generally a perl script
.pm is generally a perl module (a library)

you can use .cgi or .pl for the most part anywhere or anytime, just depends on what you prefer. save the .pm extension only for modules as they are treated a little differently by perl during compile time when compiled with the "use" operator. Which is why you can write:

use CGI; (no quotes)

and perl will look for that module in the special array @INC

but must write:

require "path/to/CGI.pm";

rocknbil

6:16 am on Aug 29, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



MasterBoo - Perhaps you should start with a google for perl tutorials, it's really easy to learn and simple input-output is one of the first places most of them will start, followed by a mailer script and all sorts of fun things.

When you submit a form from the web or send a query string to a script

test.cgi?my_name=rocknbil&you=boo

It comes to your script in a encoded form. That is, spaces are turned into numeric ASCII equivalents, and in the case of file uploads, even binary data can be included in this data stream.

The first thing that happens when this is sent to your script is that this data must be read in and parsed- turn the spaces back to spaces and split the name/value pairs into an associated key/value pairs

my_name "rocknbil"
you "boo"

or read in the binary data into a file. You can code a read/parse routine into your script yourself, or do what they are describing here, use a module that already has functions written for the task you need to do.

The module CGI.pm manages read/parse, file upload, generates forms, and has many other functions that deal with web in- and out. It's pretty difficult to find an installation of perl these days that doesn't have this module installed, so read the documentation on CGI.pm and throw the use CGI.pm line into a few scripts and see what happens.

BUT you will learn so much more if you hit the tut's and write a couple read/parse routines yourself, you'll understand the process better and it will only take 10 or so lines of code.

adwatson

3:35 pm on Sep 12, 2005 (gmt 0)

10+ Year Member



It's probably helpful to note that POST (i.e. from forms, etc) parameters are accessed in perl the same way as the GET params in the examples above.