[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: regex quickie


On 26 Apr 2007, at 18:32, Jonathan Vanasco wrote:
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' );

Yeah, but that only works if all the sites use q= to prefix the query - and have to maintain the site names in two places - once in the RE and once in the despatch table.

It'd be better to parse the query parameters into a hash and pass that to the handlers - it's the per site handlers that should know how to extract the query.

IMO of course :)

--
Andy Armstrong, hexten.net


Mail converted by mhonarc 2.6.15
This archive provided courtesy of JSW4.NET, Internet Hosting Services for Small Business.