in reply to Event Handling in CGI
... probably through a hash ...
Yep, you've got it. Here's one way to go about it. The following snippet assumes that the action to act upon is passed via the 'q' parameter (ie: index.pl?q=members):
#!/usr/bin/perl -w use strict; use CGI qw/:standard/; sub main { print header(), start_html('Welcome'), p( strong('Welcome to my website!') ), end_html(); } sub login { print header(), start_html('Member Login'), p( strong('Member Login') ), p('Login below:'), end_html(); } sub members { print header(), start_html('I am a member-only page!'), p( strong('Members Page') ), end_html(); } sub _invalid { print header(), start_html('Invalid Function'), p( strong('Invalid Script Function') ), p('That fucntion is not valid!'), end_html(); } my $actions = { main => \&main, login => \&login, members => \&members, _invalid => \&_invalid }; my $q = param('q') || 'main'; $actions->{ exists $actions->{$q} ? $q : '_invalid' }->();
If the above content is missing any vital points or you feel that any of the information is misleading, incorrect or irrelevant, please feel free to downvote the post. At the same time, please reply to this node or /msg me to inform me as to what is wrong with the post, so that I may update the node to the best of my ability.
|
|---|