[Catalyst] Catalyst::component()

Bernhard Graf catalyst at augensalat.de
Thu Oct 6 23:10:42 CEST 2005

Matt S Trout wrote:

> A lot of people already use the $c->comp('Foo') idiom and rely on the
> regex match; I'm just making it more likely to return what they
> expected.

Well, that is the actual question:
What did most people expect this method to do?
In my case it did something other than I expected and not because I
didn't RTFM.

And I really don't want methods that save me five bytes of typing, but
require reading hundreds of lines about all their magic internals.

> Plus if you really need to be explicit-or-fail, you can always do
> $c->components->{'MyApp::C::Foo'};

of course... :-/

> > say $c->comp(qr/foo$/i) if I want to build my own pitfalls. ;*)
> I suspect this actually works now; it'd be nice to have it fall
> straight through though, which is what I was asking for.

You mean (please excuse if I don't co the whole project for three lines)?

sub component {
    my $c = shift;

    if (@_) {

        my $name = shift;

	if (ref($name) ne 'Regexp') {	# <==
            my $appclass = ref $c || $c;

            my @names = (
                $name, "${appclass}::${name}",
                map { "${appclass}::${_}::${name}" } qw/M V C/

            foreach my $try (@names) {

                if ( exists $c->components->{$try} ) {

                    return $c->components->{$try};
	}				# <==

        foreach my $component ( keys %{ $c->components } ) {

            return $c->components->{$component} if $component =~ /$name/i;
	return;	# <== or `return undef´ whatever you prefer

    return sort keys %{ $c->components };

Kind regards
Bernhard Graf

More information about the Catalyst mailing list