Calling subroutines
Hi, I have a script for a cgi form that covers about 20 pages, and want to name a subroutine to handle each page like this: page1, page2, page3, etc. Once the script figures out which page it should go to, I dont want to have to do this: if ($page == 1) { page1() } if ($page == 2) { page2() } if ($page == 3) { page3() } . . . I would like to call the subroutine with one statement, something like this: $page = $in-param('page'); page$page() but the compiler doesn't seem to substitute the variable $page before figuring out the name of the subroutine and it gives me an error. I thought about loading the subroutine referencees into an array, but run into the same problem. Is there a way to do this? Or is there a better way for the beginning part of the script to play traffic cop and direct it to the right page? __ Do you Yahoo!? Yahoo! Small Business - Try our new resources site! http://smallbusiness.yahoo.com/resources/ -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Calling subroutines
Denzil Kruse wrote: Hi, I have a script for a cgi form that covers about 20 pages, and want to name a subroutine to handle each page like this: page1, page2, page3, etc. Once the script figures out which page it should go to, I dont want to have to do this: if ($page == 1) { page1() } if ($page == 2) { page2() } if ($page == 3) { page3() } . . . I would like to call the subroutine with one statement, something like this: $page = $in-param('page'); page$page() but the compiler doesn't seem to substitute the variable $page before figuring out the name of the subroutine and it gives me an error. I thought about loading the subroutine referencees into an array, but run into the same problem. Is there a way to do this? Or is there a better way for the beginning part of the script to play traffic cop and direct it to the right page? Have you considered the CGI::Application module? It works essentially as you describe but has a good following, is likely better tested, and may provide a little more support structure. http://search.cpan.org/~markstos/CGI-Application-3.31/lib/CGI/Application.pm In any case the array method you describe should work, can you show us the code you have tried? You may just not be dereferencing your sub correctly. You might also consider a hash and drop the numeric (and confusing names) unless there really is an order to the pages. http://danconia.org -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Calling subroutines
--- Wiggins d'Anconia [EMAIL PROTECTED] wrote: Denzil Kruse wrote: Hi, I have a script for a cgi form that covers about 20 pages, and want to name a subroutine to handle each page like this: page1, page2, page3, etc. Once the script figures out which page it should go to, I dont want to have to do this: if ($page == 1) { page1() } if ($page == 2) { page2() } if ($page == 3) { page3() } . . . I would like to call the subroutine with one statement, something like this: $page = $in-param('page'); page$page() but the compiler doesn't seem to substitute the variable $page before figuring out the name of the subroutine and it gives me an error. I thought about loading the subroutine referencees into an array, but run into the same problem. Is there a way to do this? Or is there a better way for the beginning part of the script to play traffic cop and direct it to the right page? Have you considered the CGI::Application module? It works essentially as you describe but has a good following, is likely better tested, and may provide a little more support structure. http://search.cpan.org/~markstos/CGI-Application-3.31/lib/CGI/Application.pm I took a quick look at it and looks pretty interesting. But, I fooled around with my above code found out that if I put some curly brackets in the right place, I think it works: $page = $in-param('page'); {page$page}() Thanks for the info! __ Do you Yahoo!? Yahoo! Small Business - Try our new resources site! http://smallbusiness.yahoo.com/resources/ -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Calling subroutines
--- Denzil Kruse [EMAIL PROTECTED] wrote: Once the script figures out which page it should go to, I dont want to have to do this: if ($page == 1) { page1() } if ($page == 2) { page2() } if ($page == 3) { page3() } As mentioned previously, CGI::Application is a good choice for this sort of problem, but you can also use a dispatch table. In this case, assuming we're using a hash: my %dispatch = ( new= \new, edit = \edit, delete = \delete, ); my $action = $cgi-param('action'); if (my $action = $dispatch{$action}) { $action-(@some_args); } else { # die or go to a default page } Solutions like this is generally easy to understand (particular when using named actions). Cheers, Ovid -- If this message is a response to a question on a mailing list, please send follow up questions to the list. Web Programming with Perl -- http://users.easystreet.com/ovid/cgi_course/ -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Calling subroutines
Denzil == Denzil Kruse [EMAIL PROTECTED] writes: Denzil But, I fooled around with my above code found out that Denzil if I put some curly brackets in the right place, I Denzil think it works: Denzil $page = $in-param('page'); Denzil {page$page}() You really really *really* don't want to do that. Please pay attention to the proper solutions provided elsewhere. For one, your example will fail on use strict, which is what every program larger than 10 lines should use. And your exact example is what it tries to rule out. -- Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095 merlyn@stonehenge.com URL:http://www.stonehenge.com/merlyn/ Perl/Unix/security consulting, Technical writing, Comedy, etc. etc. See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training! -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Subroutines
Hi Everyone, I am writing a program in which I am connecting to an oracle database. I would like to put the environment variables and the connection routine into a separate subroutine, so I dont have to keep re-copying the code. Whats the best way to go about this? Thanks, Theresa Theresa M. Mullin Programmer/Analyst Administrative Computing Northern Essex Community College 100 Elliott Way Haverhill, MA 01830 (978) 556-3757 [EMAIL PROTECTED]
RE: Subroutines
Theresa, Paul Duboise in his book Perl and MySQL puts all connection schemes in a library. Would that work for you? -Original Message- From: Theresa Mullin [mailto:[EMAIL PROTECTED]] Sent: Monday, July 15, 2002 2:31 PM To: [EMAIL PROTECTED] Subject: Subroutines Hi Everyone, I am writing a program in which I am connecting to an oracle database. I would like to put the environment variables and the connection routine into a separate subroutine, so I don't have to keep re-copying the code. What's the best way to go about this? Thanks, Theresa Theresa M. Mullin Programmer/Analyst Administrative Computing Northern Essex Community College 100 Elliott Way Haverhill, MA 01830 (978) 556-3757 [EMAIL PROTECTED] -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]