in reply to when $$s =~ m/\G.../gc is too verbose
Not that I like to generalize things, because I usually end up un-generalizing them a few months later (stupid shifting requirements!), but it seems easier than playing with symbol table manipulations just to save a few keystrokes to me... am I missing something?
I'm thinking of something roughly along these lines... completely untested and possibly wrong code is below. ;-)
# make a table of regular expression patterns my %table = ( qr/(\d+)/ => 'INT', qr/([A-Z]\w*)/ => 'ID', .... # more tokens here ); my ($parser) = shift; my $s = $parser->YYData->{INPUT}; my @matches; # any matches found by our re go in here foreach my $re ( keys %table ) { # for each regexp, check to see if it matches, and # put all the captured values in @matches if it does @matches = ( $$s =~ m/\G$re/gc ); # return the appropriate token, and captures... return( $table{$re}, @matches) if (@matches); } # end search for a token match # token not found... put error handling here ...
--
Ytrew
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: when $$s =~ m/\G.../gc is too verbose
by stefp (Vicar) on Feb 03, 2006 at 00:00 UTC |