The idea is to refactor the portions of your code that can be shared by A.cgi and B.cgi. Consider the following:
my $data = 'pretend this is a complex data structure';
my $is_web_browser = 1;
if ($is_web_browser) {
print qq(<span color="red">$data</span>\n);
}
else {
print qq(\e[0;31m$data\e[0m\n);
}
For this example, sometimes we want to decorate our data one way for one kind of client and another way for another kind of client. Rather than use one file for producing/publishing the data to be displayed as well as decorating that for a Unix command line AND a CGI "web page" ... why not refactor into one module that any number of formatters can consume?
file: SomeModule.pm
package SomeModule;
use strict;
use warnings;
sub get_data {
return 'pretend this is a complex data structure';
}
1;
file: app.cgi
use strict;
use warnings;
use SomeModule;
my $data = SomeModule::get_data();
print qq(<span color="red">$data</span>\n);
file: app.pl
use strict;
use warnings;
use SomeModule;
my $data = SomeModule::get_data();
print qq(\e[0;31m$data\e[0m\n);
This is a very simple and distilled example, but hopefully you see how different apps can reuse the same modules without actually executing each other as a script.
|