[Catalyst] create controller helper

Tomas Doran bobtfish at bobtfish.net
Sat Jan 9 15:26:14 GMT 2010


On 9 Jan 2010, at 11:35, Kiffin Gish wrote:

> Yes you're right, I overlooked the presence namespace::autoclean at  
> the
> top.
>
> Checked out the documentation but don't quite understand the  
> difference
> with namespace::clean. Is the latter the same as auto except then more
> explicit in that only named -except methods aren't cleaned out?

namespace::clean removes any symbols from your package you haven't  
explicitly specified at the end of your package, and ergo is ordering  
dependent..

E.g.

package My::Example;
use Test::More;
use Acme::UNIVERSAL::cannot;
use Foo qw/foo/;
use Bar qw/bar/;
use namespace::clean;
use Quux qw/quux/;

ok __PACKAGE__->can('foo');
ok __PACKAGE__->can('bar');
ok __PACKAGE__->can('baz');

1;
# Cleaning happens at end of scope, i.e. lib/My/Example.pm finishes

package main;
use My::Example;
use Test::More;
use Acme::UNIVERSAL::cannot;

ok My::Example->can't('foo');
ok My::Example->can't('bar');
ok My::Example->can('quux');

done_testing;

Note that the 'quux' symbol is not removed ever :)

namespace::autoclean uses a different mechanism to work out what to  
clean - rather than being ordering related, at the end of your  
package, it goes through the symbol table and finds all code symbols  
in your package. It then gets the method list from Moose and works out  
the set of symbols in your package which Moose doesn't consider a  
method - if they're not methods, then they're imported functions...  
The remaining list of imported functions is then removed..

Does this explanation make sense at all? Any chance you could make a  
doc patch for namespace::clean and/or namespace::autoclean to make  
this more clear?

> Also, when use Moose is used, is it no longer necessary to end the pm
> module with a "1;"?

It is never necessary to end a .pm with 1;

You end with _any true value_. So you can end your package with:

42;
__PACKAGE__;
'ILIKECAKE';
bless {}, 'FooBar';

or anything else you'd like to which is a true value.

The make_immutable method on Class::MOP::Class returns the newly  
immutable metaclass instance.. Which is a true value :)

Cheers
t0m




More information about the Catalyst mailing list