summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYorhel <git@yorhel.nl>2018-01-31 13:15:18 +0100
committerYorhel <git@yorhel.nl>2018-01-31 13:15:18 +0100
commit284c8bb2fd1b98056ae1519cd7e6a8caa656089f (patch)
tree33226da8f4525148b6b8d64e257546add2c98b0f
parentb39346c1d705194c19a77429bf4c6ca6393ea515 (diff)
Add import_modules setting
-rw-r--r--lib/TUWF.pm16
-rw-r--r--lib/TUWF.pod67
2 files changed, 60 insertions, 23 deletions
diff --git a/lib/TUWF.pm b/lib/TUWF.pm
index 30bca08..1e8fb0a 100644
--- a/lib/TUWF.pm
+++ b/lib/TUWF.pm
@@ -24,6 +24,7 @@ our $OBJ = bless {
after => [],
},
# defaults
+ import_modules => 1,
mail_from => '<noreply-yawf@blicky.net>',
mail_sendmail => '/usr/sbin/sendmail',
max_post_body => 10*1024*1024, # 10MB
@@ -108,7 +109,7 @@ sub set {
sub run {
# load the database module if requested
- $OBJ->_load_module('TUWF::DB') if $OBJ->{_TUWF}{db_login};
+ $OBJ->_load_module('TUWF::DB', 1) if $OBJ->{_TUWF}{db_login};
# install a warning handler to write to the log file
$SIG{__WARN__} = sub { $TUWF::OBJ->log($_) for @_; };
@@ -194,7 +195,7 @@ sub hook ($&) {
# Load modules
sub load {
- $OBJ->_load_module($_) for (@_);
+ $OBJ->_load_module($_, $OBJ->{import_modules}) for (@_);
}
# Load modules, recursively
@@ -204,7 +205,7 @@ sub load_recursive {
$rec = sub {
my($d, $f, $m) = @_;
for my $s (glob "\"$d/$f/*\"") {
- $OBJ->_load_module("${m}::$1") if -f $s && $s =~ /([^\/]+)\.pm$/;
+ $OBJ->_load_module("${m}::$1", $OBJ->{import_modules}) if -f $s && $s =~ /([^\/]+)\.pm$/;
$rec->($d, "$f/$1", "${m}::$1") if -d $s && $s =~ /([^\/]+)$/;
}
};
@@ -212,7 +213,7 @@ sub load_recursive {
(my $f = $m) =~ s/::/\//g;
my $d = (grep +(-d "$_/$f" or -s "$_/$f.pm"), @INC)[0];
croak "No module or submodules of '$m' found" if !$d;
- $OBJ->_load_module($m) if -s "$d/$f.pm";
+ $OBJ->_load_module($m, $OBJ->{import_modules}) if -s "$d/$f.pm";
$rec->($d, $f, $m) if -d "$d/$f";
}
}
@@ -316,8 +317,11 @@ our @CARP_NOT = ('TUWF');
sub _load_module {
- my($self, $module) = @_;
- Carp::croak $@ if !eval "use $module; 1";
+ my($self, $module, $import) = @_;
+ my $r = $import
+ ? eval "use $module; 1"
+ : eval "require $module; 1";
+ Carp::croak $@ if !$r;
}
diff --git a/lib/TUWF.pod b/lib/TUWF.pod
index 4b733b3..9dca171 100644
--- a/lib/TUWF.pod
+++ b/lib/TUWF.pod
@@ -142,20 +142,19 @@ directly. Even though it is an "instance" of C<TUWF::Object>, you are
encouraged to use it as if it is the main object for your website: You can use
it to store global configuration settings and other shared data.
-All modules loaded using C<TUWF::load()> and its recursive counterpart can
-export functions; These functions are automatically imported in the
-C<TUWF::Object> namespace and can be used as methods of the TUWF object. This
-allows for an easy method to split the functionality of your website among
-different functions and files, without having to constantly load and import
-your utility modules in each file that uses them.
-
-Of course, with all exported functions being imported into a single namespace,
-this does call for some function naming conventions to avoid name conflicts and
-other confusing issues. The main TUWF methods use camelCase and are often
-prefixed with a short identifier to indicate to which module or section they
-belong. For example, the L<TUWF::Request|TUWF::Request> methods all start with
-C<req> and L<TUWF::Response|TUWF::Response> with C<res>. It is a good idea to
-adopt this style when you write your own methods.
+All modules loaded using C<TUWF::load()> and its recursive counterpart can add
+their own methods to the TUWF Object. This makes it easy to split the
+functionality of your website among different functions and files, without
+having to constantly load and import your utility modules in each file that
+uses them.
+
+Of course, with all these methods being imported into a single namespace, this
+does call for some method naming conventions to avoid name conflicts and other
+confusing issues. The main TUWF methods use camelCase and are often prefixed
+with a short identifier to indicate to which module or section they belong. For
+example, the L<TUWF::Request|TUWF::Request> methods all start with C<req> and
+L<TUWF::Response|TUWF::Response> with C<res>. It is a good idea to adopt this
+style when you write your own methods.
Be warned that the data in the TUWF object may or may not persist among
multiple requests, depending on whether your script is running in CGI, FastCGI
@@ -299,9 +298,9 @@ This replaces the L<post_request_handler|/post_request_handler> setting.
=head2 TUWF::load(@modules)
-Loads the listed module names and imports their exported functions to the
-C<TUWF::Object> namespace. The modules must be available in any subdirectory in
-C<@INC>.
+Loads the listed module names and optionally imports their exported functions
+to the C<TUWF::Object> namespace (see the L</import_modules> setting). The
+modules must be available in any subdirectory in C<@INC>.
# make sure the website modules are available from @INC
use lib 'mylib';
@@ -485,6 +484,40 @@ only have it generate a friendly response.
Port to listen on when running the standalone HTTP server. This defaults to the
C<TUWF_HTTP_SERVER_PORT> environment variable, or 3000 if it is not set.
+=item import_modules
+
+This setting controls whether C<TUWF::load()> and C<TUWF::load_recursive()>
+will import all public functions into the C<TUWF::Object> namespace.
+
+For example, with C<import_modules> enabled, a module can add a
+C<tuwf->htmlFramework()> method as follows:
+
+ package My::Package;
+ use TUWF;
+ use Exporter 'import';
+
+ our @EXPORT = ('htmlFramework');
+ sub htmlFramework {
+ # ..
+ }
+
+The same can still be done with C<import_modules> set to a false value, but it
+will work slightly differently:
+
+ package My::Package;
+ use TUWF;
+
+ sub TUWF::Object::htmlFramework {
+ # ..
+ }
+ # Or:
+ *TUWF::Object::htmlFramework = sub {
+ # ..
+ };
+
+This setting defaults to true, but that's only for historical reasons. The
+latter style is recommended for new projects.
+
=item logfile
To enable logging, set this to a string that indicates the path to your log