NAME
AnyEvent::HTTPD::Appgets - Some utility functions for web applications
EXPORTS
This module mostly exports these functions:
- set_request ($ref)
-
This function sets the current request the output is appended to for the response.
Use it eg. like this:
$httpd->reg_cb ( _ => sub { my ($httpd, $req) = @_; set_request ($req); o "<html><body><h1>test</h1></body></html>"; $req->respond; } ); - capture ($block)
-
capturetemporarily redirects the output done in$blockand returns it.This function should be called with a block as argument like this:
my $r = capture { o ("<html><body>Hi</body></html>") }The return value will be simply the concationated output as it would be sent to the callback or appended to the reference given to
set_output. - o (@strs)
-
This function will append all arguments it gets and append that to the current output context, which is either set by the
capturefunction orset_request.If it is called outside a
capturefunction it will just forward everything to theomethod ofset_request. - form ($block, $callback)
-
This function will generate a html form for you, which you can fill with your own input elements. The
$callbackwill be called when the next request is handled and if the form was submitted. It will be executed before any of your content callbacks are run. Theformfunction has a special prototype which allows this syntax:my $new_element; form { entry (\$new_element); o '<input type="submit" value="append"/>' } sub { push @list, $new_element; };This function is just a convenience wrapper around the
formmethod of the AnyEvent::HTTPD object. - entry ($ref)
-
This function will output a text input form field via the
ofunction which will set the scalar reference to the value of the text field when the form is submitted.See also the
formfunction above for an example. - submit ($label)
-
This function will output a submit button with the label
$label. - js (@strs)
-
This function will output the
@strsappended enclosed in a HTML script tag for javascript.See also the
ofunction. - js_ajaxobj_func ($funcname)
-
This function will output javascript compatibility cruft code to get a XMLHttpRequest object. The javascript function
$funcnamewill be declared and can be called in javascript code with the content callback as first argument:js_ajaxobj_func 'newxhreq'; js (<<'JS'); function response_cb (xh, textcontent) { ... } var xh = newxhreq (response_cb); xh.open ("GET", "/", true) xh.send (null); JSThe first argument of the
response_cbis the XMLHttpRequest object and the second the responseText of the finished request. - link ($label, $callback, $newurl)
-
This does exactly the same as the
linkmethod of AnyEvent::HTTPD::Request just uses the current request as object and prints out the link via theofunction.
VARIABLES
- $AnyEvent::HTTPD::Appgets::JSON_JS
-
This variable contains the javascript source of the JSON serializer and deserializer described in http://www.JSON.org/js.html.
You can use this in your application by for example output it via the
jsfunction like this:js ($AnyEvent::HTTPD::Appgets::JSON_JS);