alwynpan has asked for the wisdom of the Perl Monks concerning the following question:

Hi, Second day in the Perl world, still a lot to learn. I am reading the document for Catalyst, I am not quite sure what does the variable $c do in this method.
sub hello : Global { my ( $self, $c ) = @_; $c->stash->{template} = 'hello.tt'; } sub end : Private { my ( $self, $c ) = @_; $c->forward( $c->view('TT') ); }
I understand what $self is, but I can't find any information about $c. Please help. Thank you.

Replies are listed 'Best First'.
Re: What is the $c variable?
by Athanasius (Archbishop) on Nov 01, 2016 at 03:55 UTC
      Hi Athanasius, Thank you for your help. You are correct, it is actually explained in a later section of the document on CPAN. I found the intro document for Catalyst was not quite well organized as it introduced many new things without any explanation or reference. And the explanation was provided in a later chapter which makes the beginners (at least myself) quite lost. Regards, Alwyn
        If this is your second day in the Perl world, I would suggest that trying to work with the Catalyst framework is probably premature. I think that you should nail down the basics of Perl before going into Catalyst or any other similar large-scale framework. Otherwise, you're going to make the learning curve much steeper than it needs to be.

        IMHO, you should grab a good tutorial or introductory book on Perl (e.g. Learning Perl), get a good understanding on the basic concepts and syntax, spend a couple of weeks on that, and come back to Catalyst afterwards.

        Update: fixed a typo: s/leaning/learning/;

Re: What is the $c variable?
by AnomalousMonk (Archbishop) on Nov 01, 2016 at 04:06 UTC

    In the quoted code,  $c is a lexical (or my) variable initialized from the second item of the argument list of the  hello() and  end() functions — actually, from the code it seems more appropriate to call them "methods" rather than functions. Again from the context of the code, I'd guess the  $c variable in both cases is an object reference. (Update: Please see perlobj.)

    The precise name is not important. It could as well have been  $xyzzy or  $some_name_that_has_meaning_for_me just as long as the naming was consistent throughout the function/method.


    Give a man a fish:  <%-{-{-{-<

Re: What is the $c variable?
by Your Mother (Archbishop) on Nov 01, 2016 at 13:15 UTC

    $c is standard shorthand for the application context in Catalyst. As AnomalousMonk said, the actual variable name is arbitrary, just as $self is but it's also a convention. In the case of your sample code, $self is the Controller object.

    During a regular request cycle, in which all controllers participate, $c will have $c->request loaded and you can start writing a $c->response if you like (they both have shorthand too, $c->req and $c->res). In cases where the request has not been processed, like the base class of any Catalyst application or in pre-request configuration code, this convention is often used to disambiguate from the request cycle context: $ctx.

    And I think you can start to see why Laurent_R's and my cautions about Catalyst as a first trip to Perlland are good advice. The journey will probably be more torturous than edifying. You have to know HTTP and Perl pretty well, including object oriented Perl, before you can even absorb the answers you'll get to your questions. And you haven't hit Models in Catalyst yet. Almost all the examples in the docs and most real apps use DBIx::Class which also has a steep and dissimilar learning curve.

    Don't get me wrong. Catalyst is fantastic; as is DBIC. It's just not starter fare.