Forum Moderators: coopster & phranque

Message Too Old, No Replies

How to Display Different Page Depending on String

How to Display Different Page Depending on String

         

lindajames

11:52 am on May 6, 2003 (gmt 0)

10+ Year Member



Hi,

I need to create a perl script that displays different things depending on the string.

for example if i call the script like this:

script.cgi?action=error

it should print my error message etc....

can anyone help me do this?

Any help would be much appreciated.

Cheers
Linda

dingman

7:31 pm on May 6, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Details vary depending on what language you write the script in, but it's pretty simple in concept. in PHP it might look like:


if (file_exists('/path/prefix/' . $_GET['action'] . '.inc'))
{
include('/path/prefix/' . $_GET['action'] . '.inc');
}
else
{
echo "Sorry, I don't know any action called {$_GET['action']}.";
}

Perl should be similar, except that I don't know off the top of my head how to get the value of a query-string variable in Perl.

Of course, sometimes you want to do something fancier than that. It really depends what you're trying to do whether a simple include() will do the trick.

lindajames

7:36 pm on May 6, 2003 (gmt 0)

10+ Year Member



Oh dumb me! i forgot to mention that i need to know how to do this in perl not php.

basically, i need to have 4 different pages that would have the same header and footer. So instead of creating 4 independent .cgi files i wanted to call each page by a string name i.e. script.cgi?action=page1 etc.

any help would be much appreciated.

cheers
linda

ShawnR

11:12 pm on May 6, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



#!/usr/bin/perl

use CGI ':standard';

$requested_action = param('action');

# Now $requested_action is set to "error"

Shawn

lindajames

11:29 pm on May 6, 2003 (gmt 0)

10+ Year Member



Thanx Shawn, but I dint quiet understand it, how do i add more than one query strings?

thanx for your help

cheers
linda

ShawnR

11:54 pm on May 6, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Do you mean you want to pass the script more than one parameter? Or do you mean 'How do I do an 'if then else' construct in perl?

pass the script more than one parameter
==========================================
script.cgi?action=error&username=linda

#!/usr/bin/perl
use CGI ':standard';

$requested_action = param('action');
$user = param('username');

if - then - else in perl
=========================
script.cgi?action=error&username=linda
script.cgi?action=all+OK&username=linda

$requested_action = param('action');

if ($requested_action == 'error') {
# print stuff
} elsif ($requested_action == 'allOK') {
# print some other stuff
} else {
# print default page
}

lindajames

7:12 pm on May 7, 2003 (gmt 0)

10+ Year Member



I tried the following:

#!/usr/bin/perl

$action = param('action');

if ($action == 'info') {
print "Content-Type: text/html\n\n";
print "information";

} elsif ($action == 'contact') {
print "Content-Type: text/html\n\n";
print "contact";

} else {
print "Content-Type: text/html\n\n";
print "Error:";
}

but it doesnt seem to work

any suggestions?

cheers
linda

lindajames

9:43 pm on May 7, 2003 (gmt 0)

10+ Year Member



Ive done it like this and it works but i can only seem to call one string using this method and thats "?action=info" if i call the script like that it displays &info; otherwise displays &error;

I really need to know how i can add more than just one string so that i can call other pages instead of just info

#!/usr/bin/perl

use CGI;
my $query = new CGI;
my $action = $query->param('action');

if ($action == 'info') {
&info;
} else {
&error;
}

sub info {
print "Content-Type: text/html\n\n";
print "Information:";
}

sub error {
print "Content-Type: text/html\n\n";
print "Error:";
}

any help would be very much appreciated

cheers
linda

lindajames

10:10 pm on May 7, 2003 (gmt 0)

10+ Year Member



DOESNT MATTER,

I DID IT!

andreasfriedrich

10:28 pm on May 7, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You are looking for a switch statement in Perl [perl.com]. There is none since there are lots of ways to build your own switch construct:


foreach (param('action')) {
/nick/ and do {
nick();
last [perldoc.com];
};
/aaron/ and do {
aaron();
last [perldoc.com];
};
/jesse/ and do {
jesse();
last [perldoc.com];
};
/stevie/ and do {
stevie();
last [perldoc.com];
};
&error;
}

This approach uses a loop that will be run only once, since the list to iterate over just contains the value of the action parameter. Its value gets assigned to the special variable $_ which is then tested against all the regular expressions. If a RE matches then the second part of the and statement gets evaluated. The last [perldoc.com] statement exists the loop immediately after that. As a default &error is called since Perl [perl.com] will fall through to it if no last [perldoc.com] gets called.

Another way would be to build a table that maps action strings to functions:


my %functions = (
aaron => \&aaron,
nick => \&nick,
jesse => \&jesse,
stevie => \&stevie,
);
my $action = param('action');
if (exists [perldoc.com] $functions{$action}) {
$functions{$action}->();
} else {
&error;
}

If an entry for a certain action string exists in the table the function gets called. Otherwise the error subroutine is run.

HTH Andreas

ShawnR

10:58 pm on May 7, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



DOESNT MATTER,
I DID IT!

So what was the problem? What you had in msg#7 looked good to me.

You are looking for a switch statement in Perl. There is none since there are lots of ways to build your own switch construct

I'm a great fan of perl, but I think that is a poor excuse ;) .

lindajames

12:33 pm on May 8, 2003 (gmt 0)

10+ Year Member



this is how i did it:

use CGI;

my $query = new CGI;
my $action = $query->param('action');

if ($action eq 'info') { info(); exit; }
if ($action eq 'contact') { contact(); exit; }
if ($cpaction eq 'help') { help(); exit; }
else { &error; }

ShawnR

1:13 pm on May 8, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



OOPS, sorry! The problem was the == (for numeric comparison) instead of the eq (for string comparison).

Shawn