perl help to split big verilog file into smaller ones for each module

Hi
I have a big verilog file with multiple modules. Each module begin with the code word 'module <module-name>(ports,...)'
and end with the
'endmodule' keyword.
Could you please suggest the best way to split each of these modules into multiple files?

Thank you for the help.

Example of the file:
-----begine file------
module mod1 (port1, port2);
input port1;
output port2;

buf (port2, port1);
endmodule

module mod2 (port1, port2, port3);
input port1, port3;
output port2;

and (port2, port1, port3);
endmodule
----end file-----
I wanted each module above mod1 & mod2 into two files. with corresponding module name as the names for the smaller files
eg. mod1.v & mod2.v are the new filenames of the split files

Hi
Could anybody help me? I got this below code but its not working:

#!/usr/bin/perl
#
use strict;

my ($line, $nr);

my $thebigfile = "intxt.txt"; # input file location
#my $log = "$\.v"; # output files basename

# open input file
open(INFILE, "<$thebigfile");

foreach $line (<INFILE>) { # for each line
if( $line =~ /^module[" "]+[0-9]+[a-z]+\($/ ) {
$nr = $line;
$nr =~ s/^module//;
$nr =~ s/\(//;
close(OUTFILE);
open(OUTFILE, ">$nr\.v");
}
print OUTFILE $line;
}

close OUTFILE;
close INFILE;

nawk '$1 == "module" { close(file);file= $2 ".v" } {print > file}' myFile

I don't have nawk so tried awk but got following error:
123 temp > awk '$1 == "module" { close(file);file= $2 ".v" } {print > file}' intxt.txt
awk: (FILENAME=intxt.txt FNR=1) fatal: expression for `>' redirection has null string value

Anything you could suggest?
thank you for the prompt help.

You probably have line(s) before the first "module" line.

awk '$1 == "module" { file= $2 ".v" } file != ""{print > file} $1 == "endmodule" {close(file); file=""}' intxt.txt

Thanks this one works better but what if my module line is like
module 1a_ext(a, b);
I just want the output filename to be
1a_ext.v
but its putting
1a_ext(a,.v
So how do we specify the output filename as the string between module and '(' ?
Thank you .

awk -F '[ (]' '$1 == "module" { file= $2 ".v" } file != ""{print > file} $1 == "endmodule" {close(file); file=""}' intxt.txt

Thank you very much . That works on a 600MB file in couple of minutes !!
I greatly appreciate your prompt help.