[Pdx-pm] I think that I'm trying to make things too hard.

Michael G Schwern schwern at gmail.com
Wed Oct 25 20:19:24 PDT 2006

benh wrote:
> So I'm building a small site for a client. Durring the process I
> started playing with the idea that every page is a module, there are
> some that just needed to be text, others that needed to do things.
> Though, things didn't go as easily as I would have liked, and I ended
> up going about things the longer way to make it all go. But I was
> wondering if there was a cleaner/better way of doing this.
> The crux of things is that I am attempting to use a var to call these
> module/pages. So I was hopeing for something along the lines of:
> my $action=CGI::param('action');
> $data->{'body'}=local::$action::generate_page_data();
> problem is that I get a 'bad name after local::' error.

no strict 'refs';
$data->{body} = &{"local::$action::generate_page_data"};

> so in the end I just ditched things and built subs in page and called
> them... it works but its not as elegant as I would like it to be....
> so I'm tossing this out to the group. How would you have solved this
> kinda problem? Is my thinking off? I think that it's an idea that I
> want to take another stab at but just looking for some input.

You are much better off either A) using one of the many, many modules on CPAN for this.  CGI::Application for example or B) something like...

%action2page = (
	blarg   => sub { return "<B>I AM BLARG!</B>" },
        welcome => sub { return "Welcome to BLARGH!!!<br>" },
        thingy  => \&generate_thingy

my $action = CGI->param('action');
$data->{body} = $action2page{$action}->();

No namespace munging.  No possibility of arbitrary code execution.

Error handling is left as an exercise for the reader.

More information about the Pdx-pm-list mailing list