summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYorhel <git@yorhel.nl>2017-12-09 16:35:04 +0100
committerYorhel <git@yorhel.nl>2017-12-09 16:35:08 +0100
commitaff3a2174c919da117ae41386dca825910775a36 (patch)
treeec046222b4fd552809be7c8b44b47c8972c2820d
parentba6da0074d4bd7c5e4a557eed2212daeff473148 (diff)
Add a default-exported tuwf() DSL-like function
Another inspiration from Dancer2. While Dancer2 exports a whole bunch of such functions, TUWF's functionality is already mostly contained in a single object, so a single function will do nicely.
-rwxr-xr-xexamples/singlefile.pl7
-rw-r--r--lib/TUWF.pm7
-rw-r--r--lib/TUWF.pod16
3 files changed, 25 insertions, 5 deletions
diff --git a/examples/singlefile.pl b/examples/singlefile.pl
index 267a0ed..226127f 100755
--- a/examples/singlefile.pl
+++ b/examples/singlefile.pl
@@ -71,15 +71,14 @@ sub subpage {
$self->resHeader('Content-Type' => 'text/plain; charset=UTF-8');
lit $uri;
lit "\n";
- lit $self->capture(1);
+ lit tuwf->capture(1);
lit "\n";
- lit $self->capture('capturename');
+ lit tuwf->capture('capturename');
}
sub echoapi {
- my $self = shift;
- $self->resJSON($self->reqJSON);
+ tuwf->resJSON(tuwf->reqJSON);
}
diff --git a/lib/TUWF.pm b/lib/TUWF.pm
index 109f19c..4ea7e71 100644
--- a/lib/TUWF.pm
+++ b/lib/TUWF.pm
@@ -76,9 +76,11 @@ my @handlers;
sub import {
my $self = shift;
my $pack = caller();
+ # Always export 'tuwf'. This can still be excluded with a '!tuwf' in @_
+ my @arg = ('tuwf', @_);
# import requested functions from TUWF submodules
- croak $@ if @_ && !eval "package $pack; import TUWF::func \@_; 1";
+ croak $@ if !eval "package $pack; import TUWF::func \@arg; 1";
}
@@ -224,6 +226,9 @@ our @EXPORT_OK = (
@TUWF::XML::EXPORT_OK
);
our %EXPORT_TAGS = %TUWF::XML::EXPORT_TAGS;
+our @EXPORT = ('tuwf');
+
+sub tuwf() { $TUWF::OBJ }
diff --git a/lib/TUWF.pod b/lib/TUWF.pod
index d269a16..e66f338 100644
--- a/lib/TUWF.pod
+++ b/lib/TUWF.pod
@@ -210,6 +210,22 @@ approach does not suffer from this problem and is therefore recommended.
+=head1 EXPORTED FUNCTIONS
+
+By default, TUWF exports a single function.
+
+=head2 tuwf()
+
+Returns the global TUWF object. This allows for convenient DSL-like access to
+methods in the TUWF object:
+
+ # Get the client IP:
+ my $remote_ip = tuwf->reqIP;
+
+ # Send a 404 response:
+ tuwf->resNotFound;
+
+
=head1 GLOBAL FUNCTIONS