Docendo discimus


  • Calendar

    January 2022
    M T W T F S S
  • Archives

  • Recent Posts

  • Bling

Posts Tagged ‘introduction to catalyst’

Top Searches (July)

Posted by brunorc on July 27, 2009

Update: Diego pointed out something very important, so this post had to be significantly updated.

Someone came here searching for catalyst mason variables stash. It brought me to the question: how do I access stash from the Mason view? The answer is: the same way as in TT(Site) view. Mason page is just passed the stash content, so it’s enough to declare variables we want to use in our view.

A small example: let’s put something like this in our controller:

sub uruk_hai :Local :Args(0) {
    my ( $self, $c ) = @_;

    $c->stash->{message} = 'We will feast on manflesh!';

    $c->stash->{template} = 'uruk_hai.html';

Now if we want to use TT(Site), our template will look like this:

<h2>[% message %]</h2>

In Mason it looks like this:

# don't die if there's no message
$message => undef
<h2><% $message %></h2>

Moreover, the Catalyst::View::Mason module declares some useful globals, like $c, $base and $name, so we can choose between Mason and TT without sacrificing anything.

Posted in Catalyst for intimidated | Tagged: , , , , , , , , | 2 Comments »

Catalyst is easy – installation on Windows

Posted by brunorc on July 19, 2009

Perl introduces itself

Strawberry Perl introduces itself on Windows

Some of us use Windows. That’s OK, there are cases when this platform is useful. It can be also useful for Catalyst development. There are at least two solutions:

  • install VMWare, then Ubuntu, then go to the relevant post
  • install Strawberry Perl on Windows, and then install Catalyst

Good news are: if you like Windows, you can use it for Catalyst development (I wouldn’t use it for deployment, though).

First of all – Windows does NOT come with Perl. Really! And the same goes for Python and Ruby. Quite strange… but easy to overcome. There is a nice Perl distribution – Strawberry Perl – prepared especially for Windows. It has nice no-brain installer, so everything you have to do is to go the website and pick the version you like.

Yes, there are two. The only problem with choosing one is that while 5.10 is more modern and has some goodies, it may affect the ease of your Catalyst development – it is known as the “unknown error” bug. The 5.10.1 should be released soon, but at the moment 5.8.9 would be a safe bet.

Anyway, I assume you picked one, downloaded it, installed and you are ready to get Catalyst. It will require some tampering with command line, so get ready.

CPAN shell in action

CPAN shell in action

First open the command line window and run the command perl -V. This will show you all the information about the installed Perl interpreter (see the picture – in my case it is 5.10.0 version, but the 5.8 should be very similar). When you are sure you have Perl installed on your system, go to the Start -> Programs -> Strawberry Perl -> CPAN client. You will get another command line window with CPAN shell running inside. It starts with downloading some bits of necessary information, chewing it and when it’s ready, it welcomes you with cpan > prompt. Now type

install Catalyst::Runtime

and press Enter. As your system is quite empty, you will have to install some packages Catalyst depends on. It takes some time and you may have to answer some questions or confirm some actions now and then. When this operations finishes you will be able to run Catalyst applications on your Windows machine.

CPAN shell asking for dependencies

CPAN shell asking for dependencies

But the point is to develop such applications. For this we need the Catalyst::Devel package, but before we go into this, we have to fight with some minor glitch. The problem is, that Template Toolkit installation breaks on Windows. It breaks only on building the XS module (the pure C backend, which is faster than the Perl one; we can sacrifice it, as on development machine we don’t need to be blazingly fast), and the good news are, that we are asked if we want to use it, and we can say “no”. However the bad news are, it tries to use it during the test phase of the installation, then fails, and then refuses to install.

So, my suggestion is to overcome it that way:

cpan > force install Template

Now we will be able to install it even if tests fail. Use it sparingly, cause most of the time tests should determine if package is ready to be installed, and passing the test suite is a good measure for this readiness. Also remember to answer “no” while being asked for XS – otherwise the build process will break and even the force option won’t help.

Catalyst::Devel asking about "unknown bug"

Catalyst::Devel asking about 'unknown bug'

After that we are ready to finish the installation:

  • install Catalyst::View::TT
  • install Catalyst::Devel – if you decided to use 5.10, remember to answer yes for the “unknown error” question (default is no)
    Update: I checked it on Strawberry Perl (from April 2009) and it is not fixed in this version

Now you are ready to begin your adventure with Catalyst on Windows. You have to remember two things, though:

  • use catalyst.bat instead of for creating your application
  • while using the scripts created by Catalyst, like script/ or script/, you have to specify that they should be run as under Perl interpreter, so you have to use the perl script/
  • don’t use script/ – at the moment there’s no easy support for running Perl webapplications using FastCGI on Windows

That’s all. The 99% of the Catalyst development should be OS agnostic. And every other Perl module you would need can be installed using CPAN shell. Enjoy!

Posted in Catalyst for intimidated | Tagged: , , , , , , , , , , | 10 Comments »

Catalyst is easy – do the wrong thing

Posted by brunorc on July 13, 2009

Someone landed here while searching for the phrase get catalyst object in model. Before I wrote in one of previous entries:

methods of the model don’t have access to the Catalyst context object, because, in fact, they shouldn’t have it

I first searched for the possibilities of doing it. And there are such possibilities, which shouldn’t be surprising, as Catalyst allows one to write all the crap one wants to write – it will just help to write it faster.

So if you really want to use the context object inside your Model (or View), take a look at the ACCEPT_CONTEXT component – and please read the WARNING WARNING WARNING section. In many cases passing some arguments should be sufficient. While being able to do absolutely any crap you can fancy is a nice possibility by itself, it also pays to read the manual of your chainsaw before the first launch. At least you can take advantage of turning the pages with any of your hands.

Having said this, I’d like to point that ACCEPT_CONTEXT even made its way to Catalyst Advent Calendar. It means that sharp tools can be also useful.

P.S. I planned to write about the installation of Catalyst on Windows XP, however it has been postponed (but not forgotten).

Posted in Catalyst for intimidated | Tagged: , , , , , , , , | Leave a Comment »