1Catalyst::Utils(3) User Contributed Perl Documentation Catalyst::Utils(3)
2
3
4
6 Catalyst::Utils - The Catalyst Utils
7
9 See Catalyst.
10
12 Catalyst Utilities.
13
15 appprefix($class)
16 MyApp::Foo becomes myapp_foo
17
18 class2appclass($class);
19 MyApp::Controller::Foo::Bar becomes MyApp
20 My::App::Controller::Foo::Bar becomes My::App
21
22 class2classprefix($class);
23 MyApp::Controller::Foo::Bar becomes MyApp::Controller
24 My::App::Controller::Foo::Bar becomes My::App::Controller
25
26 class2classsuffix($class);
27 MyApp::Controller::Foo::Bar becomes Controller::Foo::Bar
28
29 class2env($class);
30 Returns the environment name for class.
31
32 MyApp becomes MYAPP
33 My::App becomes MY_APP
34
35 class2prefix( $class, $case );
36 Returns the uri prefix for a class. If case is false the prefix is
37 converted to lowercase.
38
39 My::App::Controller::Foo::Bar becomes foo/bar
40
41 class2tempdir( $class [, $create ] );
42 Returns a tempdir for a class. If create is true it will try to create
43 the path.
44
45 My::App becomes /tmp/my/app
46 My::App::Controller::Foo::Bar becomes /tmp/my/app/c/foo/bar
47
48 home($class)
49 Returns home directory for given class.
50
51 dist_indicator_file_list
52 Returns a list of files which can be tested to check if you're inside a
53 CPAN distribution which is not yet installed.
54
55 These are:
56
57 Makefile.PL
58 Build.PL
59 dist.ini
60 cpanfile
61
62 prefix($class, $name);
63 Returns a prefixed action.
64
65 MyApp::Controller::Foo::Bar, yada becomes foo/bar/yada
66
67 request($uri)
68 Returns an HTTP::Request object for a uri.
69
70 ensure_class_loaded($class_name, \%opts)
71 Loads the class unless it already has been loaded.
72
73 If $opts{ignore_loaded} is true always tries the require whether the
74 package already exists or not. Only pass this if you're either (a) sure
75 you know the file exists on disk or (b) have code to catch the file not
76 found exception that will result if it doesn't.
77
78 merge_hashes($hashref, $hashref)
79 Base code to recursively merge two hashes together with right-hand
80 precedence.
81
82 env_value($class, $key)
83 Checks for and returns an environment value. For instance, if $key is
84 'home', then this method will check for and return the first value it
85 finds, looking at $ENV{MYAPP_HOME} and $ENV{CATALYST_HOME}.
86
87 term_width
88 Try to guess terminal width to use with formatting of debug output
89
90 All you need to get this work, is:
91
92 1) Install Term::Size::Any, or
93
94 2) Export $COLUMNS from your shell.
95
96 (Warning to bash users: 'echo $COLUMNS' may be showing you the bash
97 variable, not $ENV{COLUMNS}. 'export COLUMNS=$COLUMNS' and you should
98 see that 'env' now lists COLUMNS.)
99
100 As last resort, default value of 80 chars will be used.
101
102 Calling "term_width" with a true value will cause it to be
103 recalculated; you can use this to cause it to get recalculated when
104 your terminal is resized like this
105
106 $SIG{WINCH} = sub { Catalyst::Utils::term_width(1) };
107
108 resolve_namespace
109 Method which adds the namespace for plugins and actions.
110
111 __PACKAGE__->setup(qw(MyPlugin));
112
113 # will load Catalyst::Plugin::MyPlugin
114
115 build_middleware (@args)
116 Internal application that converts a single middleware definition (see
117 "psgi_middleware" in Catalyst) into an actual instance of middleware.
118
119 apply_registered_middleware ($psgi)
120 Given a $psgi reference, wrap all the "registered_middlewares" in
121 Catalyst around it and return the wrapped version.
122
123 This exists to deal with the fact Catalyst registered middleware can be
124 either an object with a wrap method or a coderef.
125
126 inject_component
127 Used to add components at runtime:
128
129 into The Catalyst package to inject into (e.g. My::App)
130 component The component package to inject
131 traits (Optional) ArrayRef of L<Moose::Role>s that the component should consume.
132 as An optional moniker to use as the package name for the derived component
133
134 For example:
135
136 Catalyst::Utils::inject_component( into => My::App, component => Other::App::Controller::Apple )
137
138 The above will create 'My::App::Controller::Other::App::Controller::Apple'
139
140 Catalyst::Utils::inject_component( into => My::App, component => Other::App::Controller::Apple, as => Apple )
141
142 The above will create 'My::App::Controller::Apple'
143
144 Catalyst::Utils::inject_component( into => $myapp, component => 'MyRootV', as => 'Controller::Root' );
145
146 Will inject Controller, Model, and View components into your Catalyst
147 application at setup (run)time. It does this by creating a new package
148 on-the-fly, having that package extend the given component, and then
149 having Catalyst setup the new component (via $app->setup_component).
150
151 NOTE: This is basically a core version of CatalystX::InjectComponent.
152 If you were using that you can now use this safely instead. Going
153 forward changes required to make this work will be synchronized with
154 the core method.
155
156 NOTE: The 'traits' option is unique to the Catalyst::Utils version of
157 this feature.
158
159 NOTE: These injected components really need to be a Catalyst::Component
160 and a Moose based class.
161
163 Utility functions to make it easier to work with PSGI applications
164 under Catalyst
165
166 env_at_path_prefix
167 Localize $env under the current controller path prefix:
168
169 package MyApp::Controller::User;
170
171 use Catalyst::Utils;
172
173 use base 'Catalyst::Controller';
174
175 sub name :Local {
176 my ($self, $c) = @_;
177 my $env = $c->Catalyst::Utils::env_at_path_prefix;
178 }
179
180 Assuming you have a request like GET /user/name:
181
182 In the example case $env will have PATH_INFO of '/name' instead of
183 '/user/name' and SCRIPT_NAME will now be '/user'.
184
185 env_at_action
186 Localize $env under the current action namespace.
187
188 package MyApp::Controller::User;
189
190 use Catalyst::Utils;
191
192 use base 'Catalyst::Controller';
193
194 sub name :Local {
195 my ($self, $c) = @_;
196 my $env = $c->Catalyst::Utils::env_at_action;
197 }
198
199 Assuming you have a request like GET /user/name:
200
201 In the example case $env will have PATH_INFO of '/' instead of
202 '/user/name' and SCRIPT_NAME will now be '/user/name'.
203
204 Alternatively, assuming you have a request like GET /user/name/foo:
205
206 In this example case $env will have PATH_INFO of '/foo' instead of
207 '/user/name/foo' and SCRIPT_NAME will now be '/user/name'.
208
209 This is probably a common case where you want to mount a PSGI
210 application under an action but let the Args fall through to the PSGI
211 app.
212
213 env_at_request_uri
214 Localize $env under the current request URI:
215
216 package MyApp::Controller::User;
217
218 use Catalyst::Utils;
219
220 use base 'Catalyst::Controller';
221
222 sub name :Local Args(1) {
223 my ($self, $c, $id) = @_;
224 my $env = $c->Catalyst::Utils::env_at_request_uri
225 }
226
227 Assuming you have a request like GET /user/name/hello:
228
229 In the example case $env will have PATH_INFO of '/' instead of
230 '/user/name' and SCRIPT_NAME will now be '/user/name/hello'.
231
233 Catalyst Contributors, see Catalyst.pm
234
236 This library is free software. You can redistribute it and/or modify it
237 under the same terms as Perl itself.
238
239
240
241perl v5.30.1 2020-01-29 Catalyst::Utils(3)