[Catalyst] Why is $c undefined?

Lukas Thiemeier spamcatcher at thiemeier.net
Mon Oct 29 19:28:52 GMT 2012


Hi again,

$c is automatically passed to Catalyst-actions  (not methods), but only
if you use Catalyst to do so.

If you call

 $self->someaction;

or

  $c->controller('SomeController')->someaction();

it is not passed. How can catalyst know if you are calling a catalyst
action, or any method on any object? And even if the object is a
catalyst controller, how can catalyst know if it is a action, which
requires the context, or just a helper, which doesn't use the context?

If you want catalyst to pass $c, use $c->forward, $c->detach, $c->visit
and $c->go instead:

  $c->forward("/someaction");

or

  $c->forward($c->controller('MyController')->action_for("someaction"));

or

  $c->forward("/somecontroller/someaction");

Read the main catalyst docs for details. (perldoc Catalyst)

Lukas

On 10/29/2012 08:07 PM, Craig Chant wrote:
> Hi,
> 
>  
> 
> I seem to be unable to work out why $c is never automatically passed to
> any of my models or methods?
> 
>  
> 
> I have in root.pm
> 
>  
> 
> # always runs first!
> 
> sub begin :Private {
> 
>    
> 
>     my ( $self, $c ) = @_;
> 
>  
> 
>     # Authenticate
> 
>     $self->AuthenticateUser();   
> 
>     return 1;
> 
>    
> 
> }
> 
>  
> 
> I then have...
> 
>  
> 
> sub AuthenticateUser {
> 
>    
> 
>     my ( $self, $c ) = @_;   
> 
>     
> 
>     $c->session;
> 
>     if(!$c->model('Members')->LogCheck($c)){
> 
>         $c->uri_for_action('/login/login');
> 
>     }   
> 
>     
> 
> }
> 
>  
> 
> However, $c is undefined and errors, it only works if I pass it $c from
> 'begin'..
> 
>  
> 
>     $self->AuthenticateUser($c);   
> 
>  
> 
>  
> 
> I was under the impression that $c was the context (Catalyst) default
> variable and was always passed to every method / subroutine.
> 
>  
> 
> is this not the case?
> 
>  
> 
> Thanks,
> 
>  
> 
> Craig .
> 
> 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 at 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/




More information about the Catalyst mailing list