2

Where should I place functions like, for example, sum_it_all() in Catalyst project?

It's not a model, that's nothing about data, it's not a controller because it doesn't ask the web-request. It's just a simple function and I want it to be accessible in all my controllers.

Now I use Model/Utils.pm and $c->model("utils")->sum_it_all(), but it seems to be really ridiculous.

tereško
  • 58,060
  • 25
  • 98
  • 150
Emb
  • 23
  • 2
  • I don't know Catalyst, so this may not be any help, but in symfony, there is a top-level lib which contains lib/models and lib/forms, so I just put this sort of thing in lib directly. – Colin Fine Apr 20 '11 at 13:20
  • When I first met Catalyst, I did exactly what you did (creating a Model). Models are very lightweight in Catalyst, so it's not a totally broken idea. However, it does make much more sense to just create a module outside of the catalyst namespace, as Sid Burn wrote. – Csongor Fagyal Jul 31 '12 at 00:53

4 Answers4

4

If you need this functions in a Catalyst Controller, just embed it in the controller where you need it. If you need the same function in several Controller. Create a new Module that contains all your functions. If your Project is named "Foo", then create for example "Foo::Helpers".

In every controller where you need some functions from your helper just import them "use Foo::Helper qw(sum)"

Look at Sub::Exporter for exporting functions.

David Raab
  • 4,433
  • 22
  • 40
  • If I did this, what folder should the module be in? – gideon Dec 22 '12 at 04:39
  • From your main project root. Place the modul in `lib/Foo/Helpers.pm` – David Raab Dec 23 '12 at 11:58
  • Thanks @Sid So then I should add a `use Foo::Helpers;" on the top and use it like a perl package right? (As opposed to a `$c->models("Foo"..` ) – gideon Dec 23 '12 at 12:00
  • The file you created is just a normal Perl Module and starts with `package Foo::Helpers` and so on. Other Catalyst Controllers or other Modules in your project then can just include the module with `use Foo::Helpers qw(sum)`. If you want to import a function you need to setup "Exporting" in Foo::Helpers. Use Sub::Exporter for this. – David Raab Dec 23 '12 at 12:03
3

If it's nothing Catalyst specific, just use it how you would outside the Catalyst context. I'd recommend Sub-Exporter.

The $ctx->model(...) is meant for accessing a layer (::Model::) that is basically a "glue" between Catalyst and your business/model logic. If you don't need any glue (automatic configuration and component inflation for easier access is a common use-case), you can abstract it away as in every Perl application.

phaylon
  • 1,914
  • 14
  • 13
0

I have set of functions in Utils.pm Helper package, through catalyst I want to access all the methods using $c->utils accessor.

For Eg:

package Utils

sub method1 {
}

sub method2 {
}

In catalyst, I would like to call method1 using $c->utils->method1(<params>) or $c->utils->method2(<params>)

Please let me know the best way to accomplish this.

sheeju
  • 56
  • 4
  • 2
    hmm ... answer or question? If the latter, please post a ... [question](http://stackoverflow.com/questions/ask) :-) – kleopatra Sep 13 '13 at 07:57
0

I recommend you simply add that function and other useful ones as a Catalyst Plugin and you can access it using the syntax $c->sum_it_all() (see sample plugin below)

========Example custom Plugin====

package Catalyst::Plugin::HelpUtils;
use strict;
use warnings;
our $VERSION = '1.0';

=head1 NAME

Catalyst::Plugin::HelpUtils


=head1 SYNOPSIS

    use Catalyst qw/
    Helputils
    /;

    To use any of the utilities in this plugin e.g:
    $c->sum_it_all()    


=cut

sub sum_it_all{
    my @items = @_;
    my $result = 0;
    foreach(@items) {
        $result += $_;
    }
    return $result;
}


1;
Sotiris
  • 38,986
  • 11
  • 53
  • 85
  • 2
    I don't recommend this. Often it ends up that you write a lot of code that is only usable in Catalyst and nowhere else. Even a "sum" function fits this. Because why should somebody write a sum function that is only usable in a Catalyst Controller? Only write a Catalyst plugin if it so specific that it don't make sense in any other context. – David Raab May 31 '11 at 22:48