[Catalyst] Catalyst::component()

Bernhard Graf catalyst at augensalat.de
Tue Oct 4 23:54:27 CEST 2005


On Tuesday 04 October 2005 18:35, you wrote:
> On Tue, Oct 04, 2005 at 06:00:33PM +0200, Bernhard Graf wrote:
> > It is worse if you read my mail
> > http://lists.rawmode.org/pipermail/catalyst/2005-August/001464.html
>
> To which I replied with a suggest set of changes, and a request for
> comments.
>
> I'm still waiting for you to comment. Maybe you could reply to my
> re-post of the proposal today?

At least I'd like the bugs fixed:
- have a defined() for $c->components->{$name}
- don't return a number but undef (nothing) in scalar context if no
  component matched $name

This is not tested:

sub component {
    my $c = shift;

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

    my $name = shift;
    my $component;

    return $component
	if defined($component = $c->components->{$name});

    for $component ( keys %{ $c->components } ) {
        return $c->components->{$component}
	    if $component =~ /$name/i;
    }
    return;
}

This would be compatible to the docs (which don't mention the return of
a list of all component keys when no arguments provided, though).

As I said I like the compiled regex way with qr(), but more like Wade
suggested in
http://lists.rawmode.org/pipermail/catalyst/2005-October/001798.html

# not tested
sub component_refactored {
    my $c = shift;

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

    my $name = shift;
    my $component;

    if (ref($name) ne 'Regexp') {
	return $component
	    if defined($component = $c->components->{$name});
	return;
    }

    for $component ( keys %{ $c->components } ) {
        return $c->components->{$component}
	    if $component =~ /$name/;
    }
    return;
}


Kind regards
-- 
Bernhard Graf



More information about the Catalyst mailing list