• Howdy! Welcome to our community of more than 130.000 members devoted to web hosting. This is a great place to get special offers from web hosts and post your own requests or ads. To start posting sign up here. Cheers! /Peo, FreeWebSpace.net
managed wordpress hosting

help with perl

The stuff after the ? is stored in an environment variable called query_string. To store what's in the query_string to a variable, you can do this:

$variable=$ENV{'QUERY_STRING'};

By using an IF statement along with the query_string, you can go to a corresponding subroutine. Something like this:

$query=$ENV{'QUERY_STRING'};
if ($query eq "main") {
&main;
}
if ($query eq "contact") {
&contact;
}

-Ryan Palmer
 
Is it possible that you could use &{$ENV{'QUERY_STRING'})??

Don't know I have never tried it but I do know that you are able to define names of variables by simply doing ${$nam_value}

That way you may find you can implement it in callings subroutines etc. Probably a pretty bad idea though considering that it will Internal 500 if you call a non existant sub.


Anyways.

See ya

STuart Low
thedude@perlboy.org
 
if u do it with

$query=$ENV{'QUERY_STRING'};
if ($query eq "main") {
&main;
}
if ($query eq "contact") {
&contact;
}

how would u make it so if u called sumthing that wasn't there u could redirect it to anotha sub routine like an error subroutine

dont mind me im a moron when it comes to this
 
A simple way is to do a series of elseif statements such as:

$query=$ENV{'QUERY_STRING'};
if ($query eq "main")
&main;
elsif ($query eq "contact")
&contact;
elsif ($query eq "bob")
&something;
else
&default_error_message;

You can even do a case statement if that makes you feel better :)

If you want to just check if its empty..easy...

if($query eq "") ...

Hope this helps...
 
I think the best way to learn is to learn by example...

So here is a fully working piece of code that works by itself..just put it in a file of its own... :


------start code------

#!/usr/local/bin/perl

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

$query = $ENV{'QUERY_STRING'};

if ($query eq "test") {
print "test";
}
elsif ($query eq "tester") {
print "oknow";
}
elsif ($query eq "main") {
print "main";
}
elsif ($query eq "contact") {
print "contact";
}
elsif ($query eq "bob") {
print "bob";
}
else {
print "error page";
}

------end code------

**** Make sure that you CHMOD the file to be 755 and that the extension is either .cgi or .pl and it should work. You can then modify it to fit your needs...
 
Tip of advice

LawTown Junky:

I'm not sure what you're using this script for, but keep in mind that if you're doing this to display html pages do not put all the HTML code in the perl script because it has to completely read the script to execute. So use require to open the file and print it.

I made this mistake before and it slowed the site down really bad.
 
the first example given by cds looks more like java/javascript than perl.. all perl if-else statements use brackets unless trailing a single statement.. thats why it gave 500 errors

#!/usr/bin/perl

$go = $ENV{QUERY_STRING};

print "Content-type: text/html\n\n";
if ($go eq "contact") {
&contact ();
}
elsif ($go eq "downloads") {
&downloads ();
}
else {
&main ();
}

sub main {
print qq~<html>
<body>
this is the main page
</body>
</html>~;
}

sub contact {
print qq~<html>
<body>
this is the contact page
</body>
</html>~;
}

sub downloads {
print qq~<html>
<body>
this is the download page
</body>
</html>~;
}

and just in case you put a @ sign or a ~ in between the qq~'s and ~;'s .. remember to put a \ in front of it.. there are a few more.. if you add some weird characters in there.. and get an error.. just put a backslash in front of it
 
and if you want to do it nick's way..

#!/usr/bin/perl

$pwd = `pwd`;
chomp $pwd;
$file = $ENV{QUERY_STRING};

unless (-e "$pwd/$file.txt") { $file = "main"; }

print "Content-type: text/html\n\n";
open FILE, "<$pwd/$file.txt";
while (<FILE>) { print; }
close FILE;

put the html of any file you want in "file.txt"...

ie: if they type ........blah.cgi?home

make a home.txt with the html content for the home page

of course you'll be doing more with whatever program you got in mind.. but as far as this code is.. it is extremely pointless.. because its like instead of making someone type "http://yourdomain.com/home.html", you make them type.. "http://yourdomain.com/file.cgi?home"

if they type a page that doesn't exist.. it'll go to the home
 
im jus messin around tryin to learn some stuff.
and the otha day i wondered how you did that so i decided to find out. thanx for u guys help
 
ok i got it to work with this :
#!/usr/bin/perl

$go = $ENV{QUERY_STRING};

print "Content-type: text/html\n\n";
if ($go eq "contact") {
&contact ();
}
elsif ($go eq "downloads") {
&downloads ();
}
else {
&main ();
}

sub main {
print qq~<html>
<body>
this is the main page
</body>
</html>~;
}

sub contact {
print qq~<html>
<body>
this is the contact page
</body>
</html>~;
}

sub downloads {
print qq~<html>
<body>
this is the download page
</body>
</html>~;
}

and yes it is extremly slow but now i know how to do it so i can add it in my stupid things i attempt to make.
 
Back
Top