2

Is there anyway to get access to $c without passing it around? I've got a third party library that runs through CGI::Application that I bootstrap with Catalyst::Controller::WrapCGI. What's the best way to get $c into the code being called so I can prepare to migrate it to Catalyst?

If it doesn't make it accessible, how do I get access to $c in an application context?

Evan Carroll
  • 78,363
  • 46
  • 261
  • 468

1 Answers1

2

No

At least as far as I can tell, but this blog entry in the Catalyst Advent Calendar gives you an idea of how to accomplish it. They just make a global and an accessor, and if it works.. it works?

our $__ACTIVE_CTX;
sub ctx { $__ACTIVE_CTX }

around 'dispatch' => sub {
  my ($orig, $c, @args) = @_;
  local $__ACTIVE_CTX = $c;
  $c->$orig(@args)
};

But, before you roll your own, check out this module CatalystX::GlobalContext which solves just this problem. Copied from synopsis:

package MyApp::Controller::Root;

use CatalystX::GlobalContext ();

sub auto : Private {
    my ($self, $c) = @_;
    CatalystX::GlobalContext->set_context($c);
    1;
}

package Some::Other::Module;

use CatalystX::GlobalContext '$c';

...
do stuff with $c
...
Evan Carroll
  • 78,363
  • 46
  • 261
  • 468