in reply to Re^2: CGI::Application same function code
in thread CGI::Application same function code

You can use either a function name, or a code reference. (Trust me - my name is in there for providing a patch to this area.) By giving an anonymous sub here, I'm creating a code reference which gets passed in. Then C::A will call it something like: $self->$function(). This means that the first (and only) parameter is the C::A object, which above I shift off to use to call _runmode.

You can send in anything else that is visible at the point you're setting up run modes. There is not really a lot of point to doing so in this case, however, since you can just as easily put all of that information into $self->param() and everyone else would have access to those as well.

As for getting the CGI, DBI, and other objects/data, I don't think there is a better way than what you're already doing. What you're looking for is a templating mechanism (on CPAN, Source Filters - I've never used source filters, but just a casual read around here for the last month has shown a number of concerns around using them - they can be quite dangerous). This is not part of standard programming languages - in C/C++, you would need to use the preprocessor. In Perl, a source filter. Maybe something along the lines of a macro that you put in your code, "RUNMODE_SETUP;", which your source filter could catch and insert the real setup code. It does seem a bit overkill to me, and you do have to be careful, but it may work.