#!/usr/bin/perl use warnings; use strict; use Data::Dumper; my $file = 'soc.v'; open(my $fh, "<", $file) or die "Cannot open the file!"; my @data; $/ = ""; while (my $paragraph = <$fh>) { $paragraph =~ m/^\s*module\s*([A-Za-z]+)/; push @data, { filename => $1, content => $paragraph }; } #use Data::Dumper; print Dumper \@data; for my $outfile (@data) { open(my $out, ">", $outfile->{filename}) or die "Cannot open the file!"; print $out $outfile->{content}; }