This is fairly easy to do using Perl's flip-flop operator:
#!/usr/bin/perl # Open File - Read File Contents Then Modify & save contents use warnings; use strict; # Specify name of file my $data_file = 'sample.pm'; # Name of temp file 1 my $prefile1 = '/tmp/123456.beg'; # Name of temp file 2 my $prefile2 = '/tmp/123456.end'; # Open File and read it all in to rawdata open OUTFILE1, '>', $prefile1 or die "Could not open $prefile1. <br> $ +!"; open OUTFILE2, '>', $prefile2 or die "Could not open $prefile2. <br> $ +!"; open SAMPLE, '<', $data_file or die "Could not open $data_file. <br> $ +!"; my @rawdata; while ( <SAMPLE> ) { if ( 1 .. /# __START_CONFIG__/i ) { print OUTFILE1; next; } if ( /# __END_CONFIG__/i .. eof ) { print OUTFILE2; next; } push @rawdata, $_; } close SAMPLE; close OUTFILE1; close OUTFILE2;
By the way, you can't exclusively lock a file opened readonly. And you should import the flock constants from the Fcntl module instead of using numerical literals.

In reply to Re: Splitting up file by Anonymous Monk
in thread Splitting up file by hmag

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.