perl-modperl mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jonathan Vanasco <jvana...@2xlp.com>
Subject Re: regex quickie
Date Thu, 26 Apr 2007 17:32:05 GMT

On Apr 26, 2007, at 9:15 AM, Andy Armstrong wrote:
>     my %despatch = (
>         'yahoo'  => sub { print "Yahoo!\n"; },
>         'google' => sub { print "Google!\n"; },
>     );
>
>     my $match = join('|', map {quotemeta} keys %despatch);
>     my $re = qr/($match)/i;       # or whatever
>
>     print "Using: $re\n";
>     for my $t ('I like yahoo', 'and also google') {
>         $despatch{lc($1)}->() if $t =~ $re;
>     }

completely off topic....

i'd keep the dispatch essentially the  same, but change the regex to  
something more like this:

	my %despatch = (
	  'yahoo' => sub { print "\n Yahoo! $_[0]\n"; },
	  'google' => sub { print "\n Google! $_[0]\n"; },
	 );

	sub dispatcher
	{
		my	( $string )= @_;
		my @parts= $string =~ qr/^(?:http:\/\/)?(?:www\.)?(google|yahoo|msn) 
\.com(?:\/\?q=([^&]*))?/i;
		if ( scalar @parts ) {
			my	( $domain , $q )= @parts;
			$despatch{lc($domain)}->($q);
		}
	}

	dispatcher( 'google.com' );
	dispatcher( 'google.com/?q=test' );


still very dirtry -- i prefer putting refs in the  dispatch, or class  
method names.
but that shows you a quick way to catch the q var and pass it to your  
function using Andy's example :)



// Jonathan Vanasco

| - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  
- - - - - - - - - - - - - - - -
| FindMeOn.com - The cure for Multiple Web Personality Disorder
| Web Identity Management and 3D Social Networking
| - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  
- - - - - - - - - - - - - - - -
| RoadSound.com - Tools For Bands, Stuff For Fans
| Collaborative Online Management And Syndication Tools
| - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  
- - - - - - - - - - - - - - - -



Mime
View raw message