Tried a whole bunch of things, not all worked, but currently at about 20X faster on 231MB fake file (perl v5.32.0).
#!/usr/bin/perl
use strict; # https://perlmonks.org/?node_id=11128141
use warnings;
use Time::HiRes qw( time );
my $string = do { local(@ARGV, $/) = '50k.foo'; <> };
my $start = time;
parse_v( $string );
printf "seconds %.3f for length %d file\n", time - $start, length $str
+ing;
sub parse_v
{
local $_ = shift;
my $name;
while( 1 )
{
if(/\G (?: (?!endmodule\b|module\b) \S+ \s [^;]* ; |
(?<!\N) endmodule \b) \s* /gcx) { }
elsif(/\G (?<!\N) module \s+ (\S+?) \s* \( [^)]* \) \s* ; \s* /gcx
+)
{ $name = $1 }
else { /\G \z/gcx ? last : die "ERROR: unknown syntax at @{[pos($_
+)]}\n" }
}
print "LAST MODULE (Perl $]): $name\n";
}
For double negative fans, (?<!\N) means "not preceded by not a newline".