Doc changes to Upgrading and PSGI.
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Upgrading.pod
1 =head1 NAME
2
3 Catalyst::Upgrading - Instructions for upgrading to the latest Catalyst
4
5 =head1 Upgrading to Catalyst 5.9
6
7 The major change is that L<Plack>, a toolkit for using the L<PSGI>
8 stack, now replaces most of the subclasses of L<Catalyst::Engine>. If
9 you are using one of the standard subclasses of L<Catalyst::Engine> this
10 should be a straightforward upgrade for you. It was a design goal for
11 this release to preserve as much backwards compatibility as possible.
12 However, since L<Plack> is different from L<Catalyst::Engine>, it is
13 possible that differences exist for edge cases. Therefore, we recommend
14 that care be taken with this upgrade and that testing should be greater
15 than would be the case with a minor point update. Please inform the
16 Catalyst developers of any problems so that we can fix them and
17 incorporate tests.
18
19 It is highly recommended that you become familiar with the L<Plack> ecosystem
20 and documentation. Being able to take advantage of L<Plack> development and
21 middleware is a major bonus to this upgrade. Documentation about how to
22 take advantage of L<Plack::Middleware> by writing your own C<< .psgi >> file
23 is contained in L<Catalyst::PSGI>.
24
25 If you have created a custom subclass of L<Catalyst:Engine>, you will
26 need to convert it to be a subclass of L<Plack::Handler>.
27
28 If you are using the L<Plack> engine, L<Catalyst::Engine::PSGI>, this new
29 release supersedes that code.
30
31 If you are using a subclass of L<Catalyst::Engine> that is aimed at
32 nonstandard or internal/testing uses, such as
33 L<Catalyst::Engine::Embeddable>, you should still be able to continue
34 using that engine.
35
36 Advice for specific subclasses of L<Catalyst::Engine> follows:
37
38 =head2 Upgrading the FastCGI Engine
39
40 No upgrade is needed if your myapp_fastcgi.pl script is already upgraded
41 to use L<Catalyst::Script::FastCGI>.
42
43 =head2 Upgrading the mod_perl / Apache Engines
44
45 The engines that are built upon the various iterations of mod_perl,
46 L<Catalyst::Engine::Apache::MP13> and L<Catalyst::Engine::Apache2::MP20>,
47 should be seamless upgrades and will work using using
48 L<Plack::Handler::Apache1> or L<Plack::Handler::Apache2> as required.
49
50 L<Catalyst::Engine::Apache2::MP19>, however, is no longer supported, as
51 Plack does not support mod_perl version 1.99
52
53 =head2 Upgrading the HTTP Engine
54
55 The default development server that comes with the L<Catalyst> distribution
56 should continue to work as expected with no changes as long as your C<myapp_server>
57 script is upgraded to use L<Catalyst::Script::HTTP>.
58
59 =head2 Upgrading the CGI Engine
60
61 If you were using L<Catalyst::Engine::CGI> there is no upgrade needed if your
62 myapp_cgi.pl script is already upgraded to use L<Catalyst::Script::CGI>.
63
64 =head2 Upgrading the Preforking Engine
65
66 If you were using L<Catalyst::Engine::HTTP::Prefork> then L<Starman>
67 is automatically loaded. You should (at least) change your C<Makefile.PL>
68 to depend on Starman.
69
70 You can regenerate your C<myapp_server.pl> script with C<catalyst.pl>
71 and implement a C<MyApp::Script::Server> class that looks like this:
72
73     package MyApp::Script::Server;
74     use Moose;
75     use namespace::autoclean;
76
77     extends 'CatalystX::Script::Server::Starman';
78
79     1;
80
81 This takes advantage of the new script system, and will add a number of
82 options to the standard server script as extra options are added by
83 Starman.
84
85 More information about these options can be seen at
86 L<CatalystX::Script::Server::Starman/SYNOPSIS>.
87
88 An alternate route to implement this functionality is to write a simple .psgi
89 file for your application, and then use the L<plackup> utility to start the
90 server.
91
92 =head2 Upgrading the PSGI Engine
93
94 If you were using L<Catalyst::Engine::PSGI>, this new release supersedes
95 this engine in supporting L<Plack>. By default the Engine is now always
96 L<Plack>. As a result, you can remove the dependency on
97 L<Catalyst::Engine::PSGI> in your C<Makefile.PL>.
98
99 Applications that were using L<Catalyst::Engine::PSGI>
100 previously should entirely continue to work in this release with no changes.
101
102 However, if you have an C<app.psgi> script, then you no longer need to
103 specify the PSGI engine. Instead, the L<Catalyst> application class now
104 has a new method C<psgi_app> which returns a L<PSGI> compatible coderef
105 which you can wrap in the middleware of your choice.
106
107 Catalyst will use the .psgi for your application if it is located in the C<home>
108 directory of the application.
109
110 For example, if you were using L<Catalyst::Engine::PSGI> in the past, you will
111 have written (or generated) a C<script/myapp.psgi> file similar to this one:
112
113     use Plack::Builder;
114     use MyCatalytApp;
115
116     MyCatalystApp->setup_engine('PSGI');
117
118     builder {
119         enable ... # enable your desired middleware
120         sub { MyCatalystApp->run(@_) };
121     };
122
123 Instead, you now say:
124
125     use Plack::Builder;
126     use MyCatalystApp;
127
128     builder {
129         enable ... #enable your desired middleware
130         MyCatalystApp->psgi_app;
131     };
132
133 In the simplest case:
134
135     MyCatalystApp->setup_engine('PSGI');
136     my $app = sub { MyCatalystApp->run(@_) }
137
138 becomes
139
140     MyCatalystApp->setup_engine('PSGI');
141     my $app = MyCatalystApp->psgi_app(@_);
142
143 B<NOT>:
144
145     my $app = sub { MyCatalystApp->psgi_app(@_) };
146     # If you make ^^ this mistake, your app won't work, and will confuse the hell out of you!
147
148 You can now move C<< script/myapp.psgi >> to C<< myapp.psgi >>, and the built-in
149 Catalyst scripts and your test suite will start using your .psgi file.
150
151 B<NOTE:> If you rename your .psgi file without these modifications, then
152 any tests run via L<Catalyst::Test> will not be compatible with the new
153 release, and will result in the development server starting, rather than
154 the expected test running.
155
156 =head2 Engines which are known to be broken
157
158 The following engines B<DO NOT> work as of Catalyst version 5.9. The
159 core team will be happy to work with the developers and/or users of
160 these engines to help them port to the new Plack/Engine system, but for
161 now, applications which are currently using these engines B<WILL NOT>
162 run without modification to the engine code.
163
164 =over
165
166 =item Catalyst::Engine::Wx
167
168 =item Catalyst::Engine::Zeus
169
170 =item Catalyst::Engine::JobQueue::POE
171
172 =item Catalyst::Engine::XMPP2
173
174 =item Catalyst::Engine::SCGI
175
176 =back
177
178 =head2 Engines with unknown status
179
180 The following engines are untested or have unknown compatibility.
181 Reports are highly encouraged:
182
183 =over
184
185 =item Catalyst::Engine::Mojo
186
187 =item Catalyst::Engine::Server (marked as Deprecated)
188
189 =item Catalyst::Engine::HTTP::POE (marked as Deprecated)
190
191 =back
192
193 =head2 Specifying the engine in the call to ->setup
194
195 XXX FIXME
196
197 =head2 Plack functionality
198
199 See L<Catalyst::PSGI>.
200
201 =head2 Tests in 5.9
202
203 Tests should generally work the same in Catalyst 5.9, but there are
204 some differences.
205
206 Previously, if using L<Catalyst::Test> and doing local requests (against
207 a local server), if the application threw an exception then this
208 exception propagated into the test.
209
210 This behavior has been removed, and now a 500 response will be returned
211 to the test. This change standardizes behavior, so that local test
212 requests behave similarly to remote requests.
213
214 =head1 Upgrading to Catalyst 5.80
215
216 Most applications and plugins should run unaltered on Catalyst 5.80.
217
218 However, a lot of refactoring work has taken place, and several changes have
219 been made which could cause incompatibilities. If your application or plugin
220 is using deprecated code, or relying on side effects, then you could have
221 issues upgrading to this release.
222
223 Most issues found with pre-existing components have been easy to
224 solve. This document provides a complete description of behavior changes
225 which may cause compatibility issues, and of new Catalyst warnings which
226 might be unclear.
227
228 If you think you have found an upgrade-related issue which is not covered in
229 this document, please email the Catalyst list to discuss the problem.
230
231 =head1 Moose features
232
233 =head2 Application class roles
234
235 You can only apply method modifiers after the application's C<< ->setup >>
236 method has been called. This means that modifiers will not work with methods
237 run during the call to C<< ->setup >>.
238
239 See L<Catalyst::Manual::ExtendingCatalyst> for more information about using
240 L<Moose> in your applications.
241
242 =head2 Controller actions in Moose roles
243
244 You can use L<MooseX::MethodAttributes::Role> if you want to declare actions
245 inside Moose roles.
246
247 =head2 Using Moose in Components
248
249 The correct way to use Moose in a component in a both forward and backwards
250 compatible way is:
251
252     package TestApp::Controller::Root;
253     use Moose;
254     BEGIN { extends 'Catalyst::Component' }; # Or ::Controller, or whatever
255
256 See L<Components which inherit from Moose::Object before Catalyst::Component>.
257
258 =head1 Known backwards compatibility breakages
259
260 =head2 Applications in a single file
261
262 Applications must be in their own file, and loaded at compile time. This
263 issue generally only affects the tests of CPAN distributions. Your
264 application will fail if you try to define an application inline in a
265 block, and use plugins which supply a C< new > method, then use that
266 application latter in tests within the same file.
267
268 This is due to the fact that Catalyst is inlining a new method on your
269 application class allowing it to be compatible with Moose. The method
270 used to do this changed in 5.80004 to avoid the possibility of reporting
271 an 'Unknown Error' if your application failed to compile.
272
273 =head2 Issues with Class::C3
274
275 Catalyst 5.80 uses the L<Algorithm::C3> method dispatch order. This is
276 built into Perl 5.10, and comes via L<Class::C3> for Perl 5.8. This
277 replaces L<NEXT> with L<Class::C3::Adopt::NEXT>, forcing all components
278 to resolve methods using C3, rather than the unpredictable dispatch
279 order of L<NEXT>.
280
281 This issue is characterised by your application failing to start due to an
282 error message about having a non-linear @ISA.
283
284 The Catalyst plugin most often causing this is
285 L<Catalyst::Plugin::Session::Store::FastMmap> - if you are using this
286 plugin and see issues, then please upgrade your plugins, as it has been
287 fixed. Note that Makefile.PL in the distribution will warn about known
288 incompatible components.
289
290 This issue can, however, be found in your own application - the only solution is
291 to go through each base class of the class the error was reported against, until
292 you identify the ones in conflict, and resolve them.
293
294 To be able to generate a linear @ISA, the list of superclasses for each
295 class must be resolvable using the C3 algorithm. Unfortunately, when
296 superclasses are being used as mixins (to add functionality used in your class),
297 and with multiple inheritance, it is easy to get this wrong.
298
299 Most common is the case of:
300
301     package Component1; # Note, this is the common case
302     use base qw/Class::Accessor::Fast Class::Data::Inheritable/;
303
304     package Component2; # Accidentally saying it this way causes a failure
305     use base qw/Class::Data::Inheritable Class::Accessor::Fast/;
306
307     package GoesBang;
308     use base qw/Component1 Component2/;
309
310 Any situation like this will cause your application to fail to start.
311
312 For additional documentation about this issue, and how to resolve it, see
313 L<Class::C3::Adopt::NEXT>.
314
315 =head2 Components which inherit from Moose::Object before Catalyst::Component
316
317 Moose components which say:
318
319     package TestApp::Controller::Example;
320     use Moose;
321     extends qw/Moose::Object Catalyst::Component/;
322
323 to use the constructor provided by Moose, while working (if you do some hacks
324 with the C< BUILDARGS > method), will not work with Catalyst 5.80 as
325 C<Catalyst::Component> inherits from C<Moose::Object>, and so C< @ISA > fails
326 to linearize.
327
328 The correct way to use Moose in a component in a both forward and backwards
329 compatible way is:
330
331     package TestApp::Controller::Root;
332     use Moose;
333     BEGIN { extends 'Catalyst::Component' }; # Or ::Controller, or whatever
334
335 Note that the C< extends > declaration needs to occur in a begin block for
336 L<attributes> to operate correctly.
337
338 This way you do not inherit directly from C<Moose::Object>
339 yourself. Having components which do not inherit their constructor from
340 C<Catalyst::Component> is B<unsupported>, and has never been recommended,
341 therefore you're on your own if you're using this technique. You'll need
342 to detect the version of Catalyst your application is running, and deal
343 with it appropriately.
344
345 You also don't get the L<Moose::Object> constructor, and therefore attribute
346 initialization will not work as normally expected. If you want to use Moose
347 attributes, then they need to be made lazy to correctly initialize.
348
349 Note that this only applies if your component needs to maintain component
350 backwards compatibility for Catalyst versions before 5.71001 - in 5.71001
351 attributes work as expected, and the BUILD method is called normally
352 (although BUILDARGS is not).
353
354 If you depend on Catalyst 5.8, then B<all> Moose features work as expected.
355
356 You will also see this issue if you do the following:
357
358     package TestApp::Controller::Example;
359     use Moose;
360     use base 'Catalyst::Controller';
361
362 as C< use base > appends to @ISA.
363
364 =head3 use Moose in MyApp
365
366 Similar to the above, this will also fail:
367
368     package MyApp;
369     use Moose;
370     use Catalyst qw/
371       ConfigLoader
372     /;
373     __PACKAGE__->setup;
374
375 If you need to use Moose in your application class (e.g. for method modifiers
376 etc.) then the correct technique is:
377
378     package MyApp;
379     use Moose;
380     use Catalyst;
381
382     extends 'Catalyst';
383
384     __PACKAGE__->config( name => 'MyApp' );
385     __PACKAGE__->setup(qw/
386         ConfigLoader
387     /);
388
389 =head2 Anonymous closures installed directly into the symbol table
390
391 If you have any code which installs anonymous subroutine references directly
392 into the symbol table, you may encounter breakages. The simplest solution is
393 to use L<Sub::Name> to name the subroutine. Example:
394
395     # Original code, likely to break:
396     my $full_method_name = join('::', $package_name, $method_name);
397     *$full_method_name = sub { ... };
398
399     # Fixed Code
400     use Sub::Name 'subname';
401     my $full_method_name = join('::',$package_name, $method_name);
402     *$full_method_name = subname $full_method_name, sub { ... };
403
404 Additionally, you can take advantage of Catalyst's use of L<Class::MOP> and
405 install the closure using the appropriate metaclass. Example:
406
407     use Class::MOP;
408     my $metaclass = Moose::Meta::Class->initialize($package_name);
409     $metaclass->add_method($method_name => sub { ... });
410
411 =head2 Hooking into application setup
412
413 To execute code during application start-up, the following snippet in MyApp.pm
414 used to work:
415
416     sub setup {
417         my ($class, @args) = @_;
418         $class->NEXT::setup(@args);
419         ... # things to do after the actual setup
420     }
421
422 With Catalyst 5.80 this won't work anymore, because Catalyst no longer
423 uses NEXT.pm for method resolution. The functionality was only ever
424 originally operational as L<NEXT> remembers what methods have already
425 been called, and will not call them again.
426
427 Using this now causes infinite recursion between MyApp::setup and
428 Catalyst::setup, due to other backwards compatibility issues related to how
429 plugin setup works. Moose method modifiers like C<< before|after|around setup
430 => sub { ... }; >> also will not operate correctly on the setup method.
431
432 The right way to do it is this:
433
434     after setup_finalize => sub {
435         ... # things to do after the actual setup
436     };
437
438 The setup_finalize hook was introduced as a way to avoid this issue.
439
440 =head2 Components with a new method which returns false
441
442 Previously, if you had a component which inherited from Catalyst::COMPONENT,
443 but overrode the new method to return false, then your class's configuration
444 would be blessed into a hash on your behalf, and this would be returned from
445 the COMPONENT method.
446
447 This behavior makes no sense, and so has been removed. Implementing your own
448 C< new > method in components is B<highly> discouraged. Instead, you should
449 inherit the new method from Catalyst::Component, and use Moose's BUILD
450 functionality and/or Moose attributes to perform any construction work
451 necessary for your class.
452
453 =head2 __PACKAGE__->mk_accessor('meta');
454
455 Won't work due to a limitation of L<Moose>. This is currently being fixed
456 inside Moose.
457
458 =head2 Class::Data::Inheritable side effects
459
460 Previously, writing to a class data accessor would copy the accessor method
461 down into your package.
462
463 This behavior has been removed. While the class data is still stored
464 per-class, it is stored on the metaclass of the class defining the accessor.
465
466 Therefore anything relying on the side effect of the accessor being copied down
467 will be broken.
468
469 The following test demonstrates the problem:
470
471     {
472         package BaseClass;
473         use base qw/Class::Data::Inheritable/;
474         __PACKAGE__->mk_classdata('foo');
475     }
476
477     {
478         package Child;
479         use base qw/BaseClass/;
480     }
481
482     BaseClass->foo('base class');
483     Child->foo('sub class');
484
485     use Test::More;
486     isnt(BaseClass->can('foo'), Child->can('foo'));
487
488 =head2 Extending Catalyst::Request or other classes in an ad-hoc manner using mk_accessors
489
490 Previously, it was possible to add additional accessors to Catalyst::Request
491 (or other classes) by calling the mk_accessors class method.
492
493 This is no longer supported - users should make a subclass of the class whose
494 behavior they would like to change, rather than globally polluting the
495 Catalyst objects.
496
497 =head2 Confused multiple inheritance with Catalyst::Component::COMPONENT
498
499 Previously, Catalyst's COMPONENT method would delegate to the method on
500 the right hand side, which could then delegate back again with
501 NEXT. This is poor practice, and in addition, makes no sense with C3
502 method dispatch order, and is therefore no longer supported.
503
504 If a COMPONENT method is detected in the inheritance hierarchy to the right
505 hand side of Catalyst::Component::COMPONENT, then the following warning
506 message will be emitted:
507
508     There is a COMPONENT method resolving after Catalyst::Component
509     in ${next_package}.
510
511 The correct fix is to re-arrange your class's inheritance hierarchy so that the
512 COMPONENT method you would like to inherit is the first (left-hand most)
513 COMPONENT method in your @ISA.
514
515 =head1 WARNINGS
516
517 =head2 Actions in your application class
518
519 Having actions in your application class will now emit a warning at application
520 startup as this is deprecated. It is highly recommended that these actions are moved
521 into a MyApp::Controller::Root (as demonstrated by the scaffold application
522 generated by catalyst.pl). 
523
524 This warning, also affects tests. You should move actions in your test,
525 creating a myTest::Controller::Root, like the following example:
526
527     package MyTest::Controller::Root;
528
529     use strict;
530     use warnings;
531
532     use parent 'Catalyst::Controller';
533
534     __PACKAGE__->config(namespace => '');
535
536     sub action : Local {
537         my ( $self, $c ) = @_;
538         $c->do_something; 
539     }
540
541     1;
542
543 =head2 ::[MVC]:: naming scheme
544
545 Having packages called MyApp::[MVC]::XX is deprecated and can no longer be generated
546 by catalyst.pl
547
548 This is still supported, but it is recommended that you rename your application
549 components to Model/View/Controller.
550
551 A warning will be issued at application startup if the ::[MVC]:: naming scheme is
552 in use.
553
554 =head2 Catalyst::Base
555
556 Any code using L<Catalyst::Base> will now emit a warning; this
557 module will be removed in a future release.
558
559 =head2 Methods in Catalyst::Dispatcher
560
561 The following methods in Catalyst::Dispatcher are implementation
562 details, which may change in the 5.8X release series, and therefore their use
563 is highly deprecated.
564
565 =over
566
567 =item tree
568
569 =item dispatch_types
570
571 =item registered_dispatch_types
572
573 =item method_action_class
574
575 =item action_hash
576
577 =item container_hash
578
579 =back
580
581 The first time one of these methods is called, a warning will be emitted:
582
583     Class $class is calling the deprecated method Catalyst::Dispatcher::$public_method_name,
584     this will be removed in Catalyst 5.9
585
586 You should B<NEVER> be calling any of these methods from application code.
587
588 Plugin authors and maintainers whose plugins currently call these methods
589 should change to using the public API, or, if you do not feel the public API
590 adequately supports your use case, please email the development list to
591 discuss what API features you need so that you can be appropriately supported.
592
593 =head2 Class files with names that don't correspond to the packages they define
594
595 In this version of Catalyst, if a component is loaded from disk, but no
596 symbols are defined in that component's name space after it is loaded, this
597 warning will be issued:
598
599     require $class was successful but the package is not defined.
600
601 This is to protect against confusing bugs caused by mistyping package names,
602 and will become a fatal error in a future version.
603
604 Please note that 'inner packages' (via L<Devel::InnerPackage>) are still fully
605 supported; this warning is only issued when component file naming does not map
606 to B<any> of the packages defined within that component.
607
608 =head2 $c->plugin method
609
610 Calling the plugin method is deprecated, and calling it at run time is B<highly
611 deprecated>.
612
613 Instead you are recommended to use L<Catalyst::Model::Adaptor> or similar to
614 compose the functionality you need outside of the main application name space.
615
616 Calling the plugin method will not be supported past Catalyst 5.81.
617
618 =cut
619