in reply to Re: Organising lots of simple regexes
in thread Organising lots of simple regexes

Thanks all for your ideas. As an alternate way of writing this, so I don't have to have lots of subroutine refs in a big hash structure, I'm now thinking of doing something like this:</code>

use strict; package Our::Redirects; sub www_theirsite_com { my $url = shift; # and transform url here however } # allow for alt domain names *www_theirsite_co_uk = \&www_theirsite_com; package main; use URI; while ( <> ) { my $uri = URI->new($_) or die "Can't parse URI"; my $func = lc( $uri->host() ); $func =~ tr/.-/_/; if ( defined &{ "Our::Redirects::$func" } ) { &{ \&{ "Our::Redirects::$func" } }($_); } }

This seems to me to have the advantages of a hash-type construct - i.e. straight-thru mapping - but slightly sugary syntax, particularly for some of the cases which aren't quite as simple as the ones I suggested. Does this seem like a reasonable way to go?

Thanks again

Replies are listed 'Best First'.
Re: Re: Re: Organising lots of simple regexes
by hardburn (Abbot) on Jan 27, 2004 at 16:14 UTC

    That's reasonable. I would change the code to call the subroutine as Our::Redirects->$func (the subroutines would have to be changed to something like sub www_theirsite_com { my ($class, $url) = @_; . . . }. Also, I would pass the URI object instead of its string form. Lastly, wrap the call in an eval to get rid of the conditional (such as eval { Our::Redirects->$func($uri) };).

    ----
    I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
    -- Schemer

    : () { :|:& };:

    Note: All code is untested, unless otherwise stated