The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

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)

capture temporarily redirects the output done in $block and 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 capture function or set_request.

If it is called outside a capture function it will just forward everything to the o method of set_request.

form ($block, $callback)

This function will generate a html form for you, which you can fill with your own input elements. The $callback will 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. The form function 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 form method of the AnyEvent::HTTPD object.

entry ($ref, %args)

This function will output a text input form field via the o function which will set the scalar reference to the value of the text field when the form is submitted.

See also the form function above for an example.

The %args hash can contain the keys size and maxlength to set the size and the maximum length of the entry.

submit ($label)

This function will output a submit button with the label $label.

js (@strs)

This function will output the @strs appended enclosed in a HTML script tag for javascript.

See also the o function.

js_ajaxobj_func ($funcname)

This function will output javascript compatibility cruft code to get a XMLHttpRequest object. The javascript function $funcname will 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);
   JS

The first argument of the response_cb is the XMLHttpRequest object and the second the responseText of the finished request.

This does exactly the same as the link method of AnyEvent::HTTPD::Request just uses the current request as object and prints out the link via the o function.

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 js function like this:

   js ($AnyEvent::HTTPD::Appgets::JSON_JS);