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.
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
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
}
#!/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
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
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