Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Re: Calling different methods based on a CGI parameter

by wfsp (Abbot)
on May 21, 2008 at 11:46 UTC ( #687753=note: print w/replies, xml ) Need Help??

in reply to Calling different methods based on a CGI parameter

A dispatch table is often used for this. It helps separate your input data from your code and helps trap invalid data. It works under strict and warnings and is prettier than a string of if/elses. :-)
#!/usr/bin/perl use strict; use warnings; my %dispatch = ( table1 => \&_upload_table1, table2 => \&_upload_table1, ); my $tablename = cgi->param('tablename'); die qq{bad param\n} unless exists $dispatch{$tablename}; $dispatch{$tablename}->(); sub _upload_table1 { } sub _upload_table2 { }
update: <cough> added the call to the sub :-)

Replies are listed 'Best First'.
Re^2: Calling different methods based on a CGI parameter
by palette (Scribe) on May 21, 2008 at 12:23 UTC
    Thankyou, but I was more interested to find out the way to build the dispatch hash in a dynamic way using an array of table names.
      I had a feeling you'd say that. :-)

      You would be generating code based on user input (data from outside). Most monks will argue quite strenuously that this is a bad idea. Really.

      You will be tempted to turn off strict and warnings, you will probably have to turn off taint mode too.

      Your code should decide which sub to run, not the the user. Have a look at the Tutorials on web programing (particularly Security in CGI Programming) and, please, take another look at dispatch tables.

      update: added link

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://687753]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2023-10-03 13:05 GMT
Find Nodes?
    Voting Booth?

    No recent polls found