The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

Name

Gantry::Docs::QuickStart - Getting your first Gantry app up and running

A Simple Greeting

All you need for a gantry app is a module which uses Gantry and has a method called do_something (usually do_main is a good first choice):

    package HiWorld;
    use strict; use warnings;

    use base 'Gantry';

    sub do_main {
        my $self = shift;

        return "Hello, Rob";
    }

    1;

I'll store this module in /home/myhome/lib/HiWorld.pm. I'll need to set that lib path with a use lib below.

CGI Deployment

To deploy this you need to choose either mod_perl or CGI. First, I'll use CGI -- see below for mod_perl. Create the following script in a servable cgi-bin directory (remember to make sure it is executable):

    #!/usr/bin/perl
    use strict;

    use CGI::Carp qw( fatalsToBrowser);

    use lib '/home/myhome/lib';

    use HiWorld qw{ -Engine=CGI -TemplateEngine=Default };

    my $cgi = Gantry::Engine::CGI->new();

    $cgi->add_location( '/', 'HiWorld' );

    $cgi->dispatch();

Then point your browser to the script.

mod_perl Deployment

To use mod_perl, you must have access to modify the httpd.conf for your web server and to restart that server. Here is the virtual host I created for the greeting application:

    <VirtualHost hiworld.devel.example.com>

        ServerName   hiworld.devel.example.com
        DocumentRoot /home/myhome/html/play
        CustomLog    /home/myhome/logs/combined.log combined
        ErrorLog     /home/myhome/logs/hiworld.err

        Include /home/myhome/html/play/hiworld.conf

    </VirtualHost>

Of course, throws the real config work to hiworld.conf. Mine assumes mod_perl 1.3. Here it is:

    <Perl>
        #!/usr/bin/perl

        use lib '/home/pcrow/srcgantry/play';

        use HiWorld qw{ -Engine=MP13 -TemplateEngine=Default };
    </Perl>

    <Location /hello>
        SetHandler  perl-script
        PerlHandler HiWorld
    </Location>

After restarting the server, I can point my browser to

    http://hiworld.devel.example.com/hello

to see the greeting.

If you are using mod_perl 2, simply change the use statement to:

        use HiWorld qw{ -Engine=MP20 -TemplateEngine=Default };

(i.e. replace MP13 with MP20)

Note that if other Gantry apps are running in the same server instance, they may fight over which template engine to use. Generally, the first app to put in a use statement for its base module chooses the template engine. We don't mind this since we always use TT. Even after you have chosen TT, you can turn it off by adding these statements to the top of your do_* method.

    $self->content_type( 'text/xml' );
    $self->template_disable( 1 );

Further Reading

For a bigger example, see Gantry::Docs::Tutorial which walks through building a simple database-backed app from scratch.

Author

Phil Crow <philcrow2000@yahoo.com>

Copyright and License

Copyright (c) 2006, Phil Crow.

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.6 or, at your option, any later version of Perl 5 you may have available.