summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorYorhel <git@yorhel.nl>2008-10-23 11:41:03 +0200
committerYorhel <git@yorhel.nl>2008-10-23 11:41:03 +0200
commit47589fd17c115c6f4453b0c37b6748f184868ec0 (patch)
treeb404748176f686d9e5fcf8bbc354d7c1c96810b8 /lib
parentbd456811f70a406c2af4e7dceafb377308d1cca5 (diff)
Results of the URL pattern match will be used as arguments to the handlers
...and added a nice example using this feature, XML output and a database function.
Diffstat (limited to 'lib')
-rw-r--r--lib/VNDB/DB/Misc.pm19
-rw-r--r--lib/VNDB/Handler/Example.pm28
-rw-r--r--lib/YAWF.pm4
3 files changed, 46 insertions, 5 deletions
diff --git a/lib/VNDB/DB/Misc.pm b/lib/VNDB/DB/Misc.pm
index f173cf9..b83a66d 100644
--- a/lib/VNDB/DB/Misc.pm
+++ b/lib/VNDB/DB/Misc.pm
@@ -3,9 +3,22 @@ package VNDB::DB::Misc;
use strict;
use warnings;
+use Exporter 'import';
+
+our @EXPORT = ('dbVNInfo');
+
+
+# small example showing how execute an SQL query and return the results
+sub dbVNInfo {
+ my($s, $id) = @_;
+ return $s->dbRow(q|
+ SELECT vr.id, vr.title, vr.original
+ FROM vn v
+ JOIN vn_rev vr ON vr.id = v.latest
+ WHERE v.id = ?
+ LIMIT 1|,
+ $id);
+}
-#sub import {
-# print "Import called\n";
-#}
1;
diff --git a/lib/VNDB/Handler/Example.pm b/lib/VNDB/Handler/Example.pm
index 764c61d..0fdc788 100644
--- a/lib/VNDB/Handler/Example.pm
+++ b/lib/VNDB/Handler/Example.pm
@@ -3,13 +3,14 @@ package VNDB::Handler::Example;
use strict;
use warnings;
-use YAWF ':html';
+use YAWF ':html', ':xml';
YAWF::register(
qr/envdump/, \&envdump,
qr/error/, \&error,
qr/html/, \&htmlexample,
+ qr{v([1-9]\d*)/xml}, \&vnxml,
);
@@ -57,5 +58,30 @@ sub htmlexample {
}
+# this function will get a number as argument, this number was parsed
+# from the pattern match above (/v+/xml, we get the + here as argument)
+sub vnxml {
+ my($self, $id) = @_;
+
+ # Let's actually serve XML as text/xml
+ $self->resHeader('Content-Type' => 'text/xml');
+
+ # fetch some information about that VN,
+ # defined in VNDB::DB::Misc
+ my $v = $self->dbVNInfo($id);
+
+ # no results found, return a 404
+ return 404 if !$v->{id};
+
+ # XML output
+ xml;
+ tag 'vn', id => $id;
+ tag 'title', $v->{title};
+ tag 'original', $v->{original};
+ end;
+}
+
+
1;
+
diff --git a/lib/YAWF.pm b/lib/YAWF.pm
index 0872ea9..c3864f1 100644
--- a/lib/YAWF.pm
+++ b/lib/YAWF.pm
@@ -146,15 +146,17 @@ sub handle_request {
my $loc = $self->reqPath;
study $loc;
my $han = $self->{_YAWF}{error_404_handler};
+ my @args;
for (@handlers ? 0..$#handlers/2 : ()) {
if($loc =~ /^$handlers[$_*2]$/) {
+ @args = map { substr $loc, $-[$_], $+[$_]-$-[$_] } 1..$#- if $#-;
$han = $handlers[$_*2+1];
last;
}
}
# execute handler
- my $ret = $han->($self);
+ my $ret = $han->($self, @args);
# give 404 page if the handler returned 404...
if($ret && $ret eq '404') {