It's a shame that you did not tell us what you already have tried, and what went wrong with your attempt. You are also missing some important information: how are the output files supposed to look? Should they contain the delimiter lines? Or should they contain everything, including module and endmodule?

Since the text blocks seem not to contain any blank lines, here is a very lazy way to solve the problem you are having.

#!/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 f +ile!"; print $out $outfile->{content}; }

This solution uses the "paragraph mode" - this line: $/ = ""; makes sure that the iterating over the contents of the file is done not by line, as it usually is, but by whole paragraphs. If you uncomment the Data::Dumper line, you will see that the data from your input file is parsed into an array of hash references, in a way that gives you the information about the contents of the output file and its name.

Good luck with your problem.

- Luke


In reply to Re: how to split file with some pattern by blindluke
in thread how to split file with some pattern by herman4016

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.