Someone would typically write everything what you have shown in a Catalyst::Controller. Now you must remember that a Catalyst Controller exists to do your URL mapping. Sure, it is possible to import a lot of functions into the controller, but when Catalyst itself import a log
function, how do you use this function for URL mapping?
For example sub log : Local { ... }
. Shortly that would not be possible, or it will be more complex then it should be. A Controller have nearly no Functions, so that you don't need to remember a lot of functions and don't have any conflicts.
Its the same reason why Perl itself choose to have special characters in special variables. Like $/
, $_
, $]
and so on. Sure they could also use $INPUT_RECORD_SEPARATOR
or $RS
as the default, but then you need to knew them, and it probably can conflict with your code if you don't knew all special variables.
Another reason is that your additional features you call on $c
have some context. For example you can enable or disable logging with $c->log->disable('warn', 'error')
or just enable them. This context is correctly passed into deeper controller. And they are not global, you can set them on every request to another state.
Another reasons is, that extra functionality that you use can and sometimes need to read the configuration file or other things. Using a object that you pass around for every request (Every $c
is special for every request) and can modified for every request gives your extension the possibility to request information from your application or to handle a state for a specific request.
But if you still don't want this, you are not forced to use $c
. For example you can just load Log::Log4Perl manually, and use a special config for it, and not use $c->log
at all. Or you can import a lot of functions by yourself. But i think the default to not pollute the namespace and give you the possibility to do something special for every request is a good default.
And at least there is no rule that you must use $c
. For example, i myself use DateTime directly and create new objects and don't use Catalyst::Plugin::DateTime that allows me to do $c->datetime
. And i don't see any benefit of doing the last. Just because there exists a lot of plugins that extends $c
, does not mean they are useful or you must use them.