Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ajax applications with perl backend - how to?

There are already questions about the Perl+AJAX, like here, here or here and several others. They're more than 2 years old and I was hoping for some new stuff.

The questions are:

  • What is the most preferred method today making AJAX apps with a Perl backend?
  • Are there some active and commonly used Perl modules that help build AJAX based applications?

Something, for the usual workflow:

 if clicked this button (or changed this field.. etc),
 POST these data to the server,
 read the JSON answer,
 and update this/these DIV(s) in a DOM... etc.

This question is can be classified as vague, but I'm really lost and need help with this: what is the most common way making AJAX apps in the Perl world, TODAY.

Looking for a helper module that help me build the browser-side javascript.

I found these:

  • https://metacpan.org/pod/OpenThought
  • https://metacpan.org/pod/HTML::AjaxTags
  • https://metacpan.org/pod/CGI::Ajax (but this is CGI based and IMHO not the best for Plack world)

These modules have not been updated for several years. Are they stable and in use? Or are they deprecated and is there some better way? (for the modern Perl technologies - like Plack).

UPDATE

As I read answers, I'm thinking that the main problem is probably in my English. I don't know how to express myself right.

I know Perl. Maybe I'm not an expert, but I wrote several thousand lines of code. I know Dancer, and already write some apps in Mojo...::Lite. Know JSON{::XS} and I know how AJAX works.

Now (for some reason) I prefer using Mason2, with Mason::Plugin::RouterSimple and several other CPAN modules and Moose. Catalyst, Jifty are too big for my needs.

Back to the question:

My favorite JS framework is jQuery, I'm using it in several projects for modal windows, or accordions, tabs etc.

BUT

My main problem is exactly in Sismetic's answer. I don't want to write JavaScript. Don't like it. (Don't know it very well, and hate every language where I must write something like: var arr = new Array(); instead of my @arr)

So, looking for a solution, how I can minimize (or in the ideal world - totally eliminate) the need of writing the JavaScript code. Don't want write into my templates

$('.clickableButton').click(function(e) {
      .... etc... etc..
)}

but something like:

 $ajax->make_button( -onchange=>$url, -updatedom=>'#thisdiv", some_special_button_description_in_perl );
 $tohead .= $ajax->gen_libs();
 $tohtml .= $ajax->gen_html();
 $jsdocready .= $ajax->gen_jsinitcode();

An in my templates will output only $tohead in the head part (so include jQuery), $tohtml will come into body, and $jsdocready will come into the end of body as JavaScript init code.

Offcourse, the above is an very stupid example, but hopefully shows what I mean. Simply: The ideal solution was (probably does not exists) is totally eliminate the need writing JavaScript, only Perl code what will generate the needed JS.

Therefore I mentioned the above modules, especially https://metacpan.org/pod/OpenThought, because these really help minimize writing JavaScript. The problem is - these have not updated for 2 years. ;( And unfortunately - I don't know any others.

like image 630
kobame Avatar asked May 30 '11 12:05

kobame


4 Answers

The reason you're not getting answers isn't just the vagueness of the question. The problem space is very wide and has a many angles of attack.

Let's clarify that the "x" in Ajax shouldn't be taken to mean XML anymore. JSON is obviously more natural and doesn't suffer from nearly as many problems so all my advice will point toward it.

What hobbs said already is right on. You do not want to mess with the server-side code much at all but adopt a framework. This is not because dealing with Ajax is hard in Perl; it's trivial. It's because the problem space gets messy quickly and you'll end up repeating your code in endless minor variations. So–

Perl/Server-side

Any of these will make you happy eventually. They all have a learning curve. There are other options but these are the Best™.

  • Catalyst (Catalyst::View::JSON)
  • Dancer (Dancer::Serializer::JSON)
  • Mojolicious (prefer Mojolicious for HTML5/Websockets)

Deployment SHOULD be Plack/PSGI based.

Take the time to really learn the core of Perl's Ajax handling: JSON(::XS) so you know what the views in the various frameworks do under the covers.

JavaScript/Client-side

This is essentially an embarrassment of riches at this point.

  • jQuery
    • Many Perl hackers like this kit; it seems to hit the same sweet spot Perl does. I adore jQuery.
  • Dojo
    • I'm not a fan — they had the worst documentation possible in early versions and broke compatibility while deleting what little docs used to exist — but it's well-liked in its current version.
  • MochiKit
  • MooTools
  • YUI
  • ExtJS
    • This, now distant, fork from YUI is the 800lb gorilla of client-side JS. I personally dislike it because of its lack of graceful degradation, et cetera, but it is highly regarded and very sharp looking out of the box.

I personally dislike and can't recommend prototype and though I've never used it I also chose not to put script.aculo.us on the list.

There are plenty of other amazing specialty kits out there too; e.g., Modernizr. When you are looking into JS, consider how important standards compliance and forward-looking features like CSS3, HTML5, extended event handling like multi-touch are to what you'll do. Good luck and have fun.

Update: Possibly of further interest

  • Jemplate – templating in JavaScript
    • Catalyst::View::Jemplate
  • Jifty – YAWF
like image 173
Ashley Avatar answered Nov 06 '22 22:11

Ashley


Looking for "ajax" isn't really what you need. Just use a web framework of your choice that has good facilities for serialization, working with Accept headers, etc. For example Catalyst and Catalyst::Action::REST, or Dancer. Don't write Perl that writes Javascript, it will only make you sad.

like image 42
hobbs Avatar answered Nov 06 '22 23:11

hobbs


I use CGI::Application as my base framework and CGI::Application::Plugin::JSON to return JSON data to jQuery.

like image 4
Raoul Avatar answered Nov 06 '22 22:11

Raoul


If you want to generate HTML code with a Perl module, I would recommend CGI.pm:

...
use strict;
use warnings;
#CGI is shipped along perl, I think
use CGI;

my $CGI = CGI->new();

my $return_string = '';
#From CGI documentation on CPAN
#http://search.cpan.org/~markstos/CGI.pm-3.55/lib/CGI.pm
$return_string .= $CGI->header;
$return_string .= $CGI->start_html('hello world');
$return_string .= $CGI->h1('hello world');
$return_string .= $CGI->button(-name    => 'button_name',
                               -value   => 'Click Me!',
                               #Javascript event if needed
                               -onClick => "do_something()"
                              );
$return_string .= $CGI->end_html;
print $return_string;

Or(as I dont like the latter method) you could just write it on perl(generating it yourself manually):

use strict;
use warnings;

#Needed header, otherwise will return error
print "Content-type: text/html\n\n"; 

#return random number from 0 to 100
my $random_number = int(rand(101));

my $HTML_generated_string = qq|
<html>
 <head>
  <title>HTML generated manually with perl</title>
 </head>
 <body>
  <h1>Hello world</h1>
  Bla bla bla Heres the random number $random_number
 </body>
</html>
|;

print $HTML_generated_string;

Other than that, I dont know any extra modules to do it. I normally do it by hand, or write a template(with CGI::Application).

like image 1
Sismetic Avatar answered Nov 06 '22 23:11

Sismetic