For Programmers: Free Programming Magazines  


Home > Archive > PERL CGI Beginners > March 2005 > Calling subroutines









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Calling subroutines
Denzil Kruse

2005-03-21, 8:55 pm

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/
Wiggins d'Anconia

2005-03-21, 8:55 pm



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/CG.../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
Denzil Kruse

2005-03-21, 8:55 pm


--- Wiggins d'Anconia <wiggins@danconia.org> wrote:
>
>
> Denzil Kruse wrote:
> 20
> each
> go
> about
> but
> way
> traffic
>
> 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/CG.../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/
Ovid

2005-03-21, 8:55 pm

--- Denzil Kruse <denzilphp@yahoo.com> 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/
Randal L. Schwartz

2005-03-22, 3:55 am

>>>>> "Denzil" == Denzil Kruse <denzilphp@yahoo.com> 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!
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com