3 ms·
Sometimes Perl works really well. For example, just the other day I wrote a tokenizer in Perl after attempting to do the same in other languages, doesn't it loo
by zzzcpan 13y ago
Sometimes Perl works really well. For example, just the other day I wrote a tokenizer in Perl after attempting to do the same in other languages, doesn't it look pretty?
sub tokenize {
while ($_[0] =~ m!
(?<whitespace> [\x20\x09]+ ) |
(?<lf> [\x0a] ) |
(?<cr> [\x0d] ) |
(?<ident> [A-Za-z_]+[A-Za-z0-9_]* ) |
(?<float> [0-9]*\.[0-9]+ ) |
(?<float> [0-9]+\.[0-9]* ) |
(?<int> [0-9]+ ) |
# ...
(?<unknown> . )
!gsx) {
my ($k, $v) = each %+;
# $k: token, $v: data
# pos($_[0]): current offset
# ...
}
}
- Blahah 13y agonice... it looks quite pretty, but would be much prettier (but very similar) in Ruby :P