You can also define your constants in "YourApp/Constants.pl", and "use"
YourApp::Constants wherever you need them.

YourApp/Constants.pm:
  use constant { FOO => 1 };
  1;

Controller.pm:

  use YourApp::Constants;

  # and in some method
  ...
  my $global_foo = FOO;

There might be a better way, but this works...

For handling dates, I recommend "DateTime", available on cpan:

Another reason to use DBIC:
DBIx::Class::Inflatecolumn::DateTime automatically transforms dates
stored in a database into a DateTime object, which can be used like this:

my $uk_string = $datetime->dmy('/');
my $us_string = $datetime->ymd('-');

But DateTime is not related to DBIC, DBIC just makes using it easy.

Use it if you have to work with dates a lot. It has lots of methods for
outputting dates, times or both in different formats, and it allows
datetime-math...


On 10/30/2012 03:39 PM, Craig Chant wrote:
> Well I opted for putting my globals (methods and constants) in MyApp.pm
> 
> It's working grand with $c->myMethod or $c->MY_CONSTANT
> 
> I use to have them working as a bareword within my application , but 
> $c->MY_CONSTANT is just as easy!
> 
> Many thanks for all the input and help, it really is appreciated.
> 
> -----Original Message-----
> From: Lukas Thiemeier [mailto:spamcatc...@thiemeier.net]
> Sent: 30 October 2012 13:56
> To: catalyst@lists.scsys.co.uk
> Subject: Re: [Catalyst] Global 'helper' methods
> 
> Hi Craig,
> 
> Writing helpers in your main App.pm and using roles is not either-or.
> 
> Roles are one possibility to modify your classes in a reusable way.
> 
> If your helpers are really global, which means that they are used by all, or 
> almost all your controllers, I would put them into your main application 
> class.
> 
> If you have more than one application which make use of the same
> helpers: Put them into a role:
> 
> YourHelperRole.pm:
> 
>   package YourHelperRole;
>   use Moose::Role;
> 
>   has "some_attribute" => ( ... ); # if you need attributes
> 
>   sub helpermethod1{
>         my ($self, $other, $args) = @_;
>         do_something();
>   }
> 
>   sub helpermethod2{
>         my ($self, $other, $args) = @_;
>         do_something_different();
>   }
> 
>   no Moose::Role; # or use namespace::autoclean or MooseX::MarkAsMethod
>   1;
> 
> App.pm:
> 
>   use Catalyst qw( ... );
>   extends "Catalyst";
>   with qw/YourHelperRole/;
> 
>   ...
> 
> In my opinion, extending your main App.pm with roles is only useful if you 
> need the same helpers in different locations, too.
> If you only need them for that single application,  avoid the overhead 
> related to using roles.
> 
> If you have helpers which are only needed by some controllers, create a role 
> for your controllers. You can not only implement helpers in you roles, you 
> can also create method-modifiers, which change the way your methods behave.
> 
> Here is an example, which writes sth to the log, after the "delete"
> method was called:
> 
> YourLogRole.pm:
> 
>   package YourLogRole;
> 
>   use Moose::Role;
>   requires qw/delete/; #die if the consuming class has no "delete"
> 
>   after "delete" => sub{
>         my ($self, $c) = @_;
>         $c->log->debug("DELETE called");
>   };
> 
>   no Moose::Role;
>   1;
> 
> Controller1.pm: (has the helpers, but no logging)
> 
>   package Controller1;
>   use Moose;
>   extends "Catalyst::Controller";
>   with qw/
>         YourHelperRole
>   /;
> 
>   sub someaction :Local {
>         my ($self, $c) =@_;
>         $self->helpermethod1();
>   }
> 
>   ...
> 
> Controller2.pm: (has helpers AND logging)
> 
>   package Controller2;
>   use Moose;
>   extends "Catalyst::Controller";
>   with qw/
>         YourLogRole
>         YourHelperRole
>   /;
> 
>   sub delete :Local :Args(1) { ... }
> 
> Controller3.pm (has logging, but no helpers)
> 
>   package Controller3;
>   use Moose;
>   extends "Catalyst::Controller";
>   with qw/
>         YourLogRole
>   /;
> 
>   sub delete :Local :Args(1) { ... }
> 
> Roles are very powerful tools. I use them a lot.
> Read the Moose docs for more information.
> 
> If you don't want to learn Moose, roles and so on now, just remember:
> 
> 1. write sth in a role
> 2. consume the role in a moose class, using "with"
> 3. your roles methods and attributes will be available in the consuming class
> 
> Lukas
> 
> On 10/30/2012 02:03 PM, Craig Chant wrote:
>> Hi,
>>
>>
>>
>> Please could you advise the best way of having a global 'helper' class
>> that has all commonly used methods in it accessible for any part of
>> the catalyst app.
>>
>>
>>
>> I found this thread
>> http://stackoverflow.com/questions/11941836/catalyst-global-subroutine
>> s
>>
>>
>>
>> With one indicating it's ok to put them in the main MyApp.pm and
>> another saying to use Moose & Roles.
>>
>>
>>
>> What is considered the correct way and could you provide an example of
>> how I create this helper class and bolt it to the Catalyst application.
>>
>>
>>
>> Many thanks,
>>
>>
>>
>> */Craig Chant/*
>>
>> I.T. Manager
>>
>> Description: cid:image001.png@01CD5F4A.17E848D0
>>
>> Main Line            01903 602664
>>
>> Direct Line           01903 227753
>>
>> Visit our website http://www.homeloanpartnership.com
>>
>> *HomeLoan Partnership have been named the Best Mortgage Network, 2012,
>> at the myintroducer.com Industry Awards*
>>
>>
>>
>> This Email and any attachments contain confidential information and is
>> intended solely for the individual to whom it is addressed. If this
>> Email has been misdirected, please notify the author as soon as
>> possible. If you are not the intended recipient you must not disclose,
>> distribute, copy, print or rely on any of the information contained,
>> and all copies must be deleted immediately. Whilst we take reasonable
>> steps to try to identify any software viruses, any attachments to this
>> e-mail may nevertheless contain viruses, which our anti-virus software
>> has failed to identify. You should therefore carry out your own
>> anti-virus checks before opening any documents. HomeLoan Partnership
>> will not accept any liability for damage caused by computer viruses
>> emanating from any attachment or other document supplied with this e-mail.
>> HomeLoan Partnership reserves the right to monitor and archive all
>> e-mail communications through its network. No representative or
>> employee of HomeLoan Partnership has the authority to enter into any
>> contract on behalf of HomeLoan Partnership by email. HomeLoan
>> Partnership is a trading name of H L Partnership Limited, registered
>> in England and Wales with Registration Number 5011722. Registered
>> office: 26-34 Old Street, London, EC1V 9QQ. H L Partnership Limited is
>> authorised and regulated by the Financial Services Authority.
>>
>>
>> _______________________________________________
>> List: Catalyst@lists.scsys.co.uk
>> Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
>> Searchable archive:
>> http://www.mail-archive.com/catalyst@lists.scsys.co.uk/
>> Dev site: http://dev.catalyst.perl.org/
> 
> 
> _______________________________________________
> List: Catalyst@lists.scsys.co.uk
> Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
> Searchable archive: http://www.mail-archive.com/catalyst@lists.scsys.co.uk/
> Dev site: http://dev.catalyst.perl.org/
> This Email and any attachments contain confidential information and is 
> intended solely for the individual to whom it is addressed. If this Email has 
> been misdirected, please notify the author as soon as possible. If you are 
> not the intended recipient you must not disclose, distribute, copy, print or 
> rely on any of the information contained, and all copies must be deleted 
> immediately. Whilst we take reasonable steps to try to identify any software 
> viruses, any attachments to this e-mail may nevertheless contain viruses, 
> which our anti-virus software has failed to identify. You should therefore 
> carry out your own anti-virus checks before opening any documents. HomeLoan 
> Partnership will not accept any liability for damage caused by computer 
> viruses emanating from any attachment or other document supplied with this 
> e-mail. HomeLoan Partnership reserves the right to monitor and archive all 
> e-mail communications through its network. No representative or employee of 
> HomeLoan Partn
 ership ha
s the authority to enter into any contract on behalf of HomeLoan Partnership by 
email. HomeLoan Partnership is a trading name of H L Partnership Limited, 
registered in England and Wales with Registration Number 5011722. Registered 
office: 26-34 Old Street, London, EC1V 9QQ. H L Partnership Limited is 
authorised and regulated by the Financial Services Authority.
> 
> _______________________________________________
> List: Catalyst@lists.scsys.co.uk
> Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
> Searchable archive: http://www.mail-archive.com/catalyst@lists.scsys.co.uk/
> Dev site: http://dev.catalyst.perl.org/


_______________________________________________
List: Catalyst@lists.scsys.co.uk
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst@lists.scsys.co.uk/
Dev site: http://dev.catalyst.perl.org/

Reply via email to