Showing posts with label Catalyst. Show all posts
Showing posts with label Catalyst. Show all posts

Saturday, 30 April 2011

Catalyst in the Cloud

A lot of buzz was generated recently in the Perl community after DotCloud, a big PaaS player, introduced its brand new Perl stack, letting developers deploy modern Perl web applications into the "Cloud" without having to worry about the underlying system at all.

DotCloud already provides a nice Dancer example, and sri was diligent enough to provide a Mojolicious example as soon as he got his hands on an account. So when I got my own invitation to the service, I wondered how Catalyst would stand to the challenge.

After going through the whole thing in just a couple of minutes, all I can say is that:

If you were ever scared or concerned of using Catalyst due to its dependencies and how hard it would be to deploy... you're out of excuses =)


So, without further ado, here's the Catalyst DotCloud deploy process!

Install the "dotcloud" app in your local machine:

$ sudo apt-get install python-setuptools
$ sudo easy_install dotcloud

Create a DotCloud namespace (I called it "catalyst")

$ dotcloud create catalyst

Deploy a Perl stack into that namespace:

$ dotcloud deploy -t perl catalyst.www

Create a sample Catalyst app (skip if you already have one) and enter its directory:

$ catalyst.pl CatalystCloud
$ cd CatalystCloud

DotCloud uses the "static" dir for static files, so let's create a symbolic link there for Catalyst's root/static, or wherever else you placed your static files. There are probably other ways to customize this with DotCloud's standard configuration and its nginx server, but I just joined and haven't fiddled with any docs yet :)

$ ln -s root/static static

Add PSGI support for your Catalyst app. We could try the built-in FastCGI run script, but the recommended way is PSGI. To do that, simply install Catalyst::Engine::PSGI and create the new helper for your app, naming it "app.psgi" in your app's base directory:

$ cpan Catalyst::Engine::PSGI (or cpanm, or whatever you like)
$ script/catalystcloud_create.pl PSGI
$ ln -s script/catalystcloud.psgi app.psgi

The "app.psgi" file needs to know how to find your app, so add a use lib 'lib'; statement to it, right after "use warnings". The final file should look something like this (replacing "CatalystCloud" with your app's name, of course):

#!/usr/bin/env perl
use strict;
use warnings;
use lib 'lib';
use CatalystCloud;
CatalystCloud->setup_engine('PSGI');
my $app = sub { CatalystCloud->run(@_) };

Remember to add the new dependency to your app's Makefile.PL, so DotCloud knows what's required when it deploys:

requires 'Catalyst::Engine::PSGI';

Your application is ready; let's push it to DotCloud:

$ dotcloud push catalyst.www .

Now sit back and watch as DotCloud smoothly walks through all of Catalyst's dependencies and deploys your app to the Web. Sweet!



Once you start working on your application, remember to keep your Makefile.PL up-to-date, adding any and all dependencies to it (like Catalyst::View::TT and Catalyst::Model::DBIC::Schema). But you already do that, don't you?

If you run into trouble, give "dotcloud logs catalyst.www" a try (again, replacing the catalyst.www namespace with your app's) and browse through you application's logs in real time. If you left the -Debug flag on, you can even see Catalyst's messages on the fly.

Many thanks are in order to the ever-awesome Miyagawa and the whole DotCloud team for making deploying Perl applications such a breeze =)

Have fun!!

Monday, 18 January 2010

Better output testing HTML and other long strings

If you ever wrote or ran a website test in Perl, you probably used Rafaël Garcia-Suarez's wonderful Test::LongString, even if you never heard of it before. Not only is it used by lots of CPAN modules, it is the foundation of Test::WWW::Mechanize test functions, with flavors for Catalyst, PSGI, CGIApp and several others. But it doesn't have to be HTML content testing - any string output can be tested with its awesome functions.

Maybe Test::LongString's most common function is contains_string(), which you may know as Test::WWW::Mechanize's $mech->content_contains() method. When it fails, the failing test output shows the original string and the text it was trying to find:


# searched: "foo bar"
# can't find: "baz"


Problem is, when the original string is too long, you get only the beginning of it. This happens a lot when you're trying to find content inside HTML, like:


# searched: "<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Trans"...
# can't find: "some random content"


Such output is only barely useful, and I got fed up of having to edit a test file manually, add diag() calls to see the complete output, then ack for the wanted string to see what went wrong. So I made a small patch to Test::LongString, which Rafaël promptly accepted (yup, he rocks, but we all know that ;-).

Now, whenever such test fails, you'll get two extra lines:


# searched: "<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Trans"...
# can't find: "some random content"
# LCSS: "ome random content"
# LCSS context: "d="content" class="foo">Some random content!</div>"


"LCSS" stands for Longest Common SubString, meaning you get whatever matched the most inside the original content (note that LCSS is not the same as LCS, or Longest Common Subsequence, which would go for a non-sequential match as seen in diff-like outputs). And, of course, "LCSS context" means the surroundings of the LCSS string just found.

Now, just by looking at our example, we know exactly why the test failed, and we are free to fix either the test suite or the application.

Know what's best? You get all that for free, no need to change a single line in your tests. Just update Test::LongString to 0.12 or later and enjoy! :-)

Monday, 25 May 2009

Catalyst/Mojo Plugins for Padre now in 8 languages!

Catalyst is an amazing web framework, but requires a little learning effort before you get to do all the cool stuff. Mojo still needs some work (specially documentation-wise) but it's catching up as a solid simple-yet-powerful alternative for web development, specially if you're Perl begginer. As you may know, both of them have Padre plugins to further easen your developing process. To make it even easier, the new versions of Padre::Plugin::Mojolicious (0.02) and Padre::Plugin::Catalyst (0.03) now have localization support, so menu and messages can be displayed in your native tongue! Problem is, I don't know what your native tongue is ;)

Right now, they're already translated in these languages (besides original english, of course):
Is your native tongue not listed? Then please add support for it! You can find both plugins on Padre's svn repository. Let me know if you need any help creating a .po file from messages.pot. Also, since both Cat and Mojo plugins share almost the same messages, if you translate one of them, the other should be a quick and easy copy/paste process. Heck, I can even do it for you if you're too lazy ;)

Many thanks to all translators! Also, many thanks to jq, who did an excellent job adding translation support for plugins on Padre, and to szabgab++ for the impressive effort on enabling translation for all plugins under Padre's svn repo. Now we could really use your help translating not only these, but all of Padre's plugins!

Tuesday, 14 April 2009

Padre + Catalyst

For a while now, I've been contributing (or at least trying) to Padre - The Perl IDE. Although it still lacks some nice IDE features, it's really amazing to see how much has already been done in so little time. In fact, Padre releases are almost weekly, with Changelogs filled with active development and bug tracking. Szabgab++ managed to create an active and exciting community around the project, and I'm really glad to be a part of it.

One of Padre's cool features is plugins. Jq++, Alias++ and everyone else did an amazing job with the plugin API, so it's getting easier and easier to extend Padre's functionality. In fact, there are already over 20 plugins available on CPAN and many more to come. Recently, I created one myself, hoping it would improve the Padre experience and help developers of web applications, specially beginners: Padre::Plugin::Catalyst.

Installing the plugin should be fairly simple on the cpan shell

cpan> install Padre::Plugin::Catalyst


Of course, it has both Padre and Catalyst::Devel as dependencies. After installation, fire up Padre and go to the Plugins->Plugin Manager menu option.

The Catalyst plugin should show up. Select it and click on the "Enable" button so its status turns green (enabled), as shown in the image above.

That's it! The plugin and all its features can be accessed through its own menu at Plugins->Catalyst:


Let's browse through some of the options:

New Catalyst Application



This option will create a new Catalyst application on the specified folder. You can mark the "short names" checkbox to use "M", "V" and "C" folder names instead of the recommended "Model", "View" and "Controller" names. After clicking on the "OK" button, the plugin will create a skeleton program for you and ask if you want to open the created "Root.pm" controller so you can start programming right away.

Create New -> Model


This option lets you create a new model for your application. Here, you can specify your Model's name, type, and any additional parameters needed. "Type" is a list with all helper models available on your system, for you to choose from. It defaults to mst++'s DBIC::Schema (if available), as it's considered the ORM technology of choice for Catalyst.

Create New -> View


Similarly, this option will let you create a new View component for your application. "Type" defaults to the highly popular Template Toolkit helper, whenever available.

Create New -> Controller


With this option, you are able to quickly create a skeleton for any controllers your application may have. The default "Type" is no type at all, but, if you do use Controller helpers, you can also specify any additional parameters for it.

Start/Stop Web Server


At any point, should you want to test-drive your web application, you can click on this option to call the yourapp_server.pl script (where "yourapp" is the name of your Catalyst application, of course), a stand-alone development web server. All the output produced by the server will be displayed right on Padre's Output window (very useful for debugging), and it will ask if you want to open your web browser to actually view your application running.

Catalyst Online References

This option has quick shortcuts to some handy developer's information links available on the Internet:


That's about it :)

Future plans include letting the developer choose its favorite types, script updating features, and much more. If you want to help, you can checkout the current code from Padre's svn repository, or give me some feedback on any bugs or feature requests via Padre's Tickets or CPAN's RT.

Let me know what you think!