#!usr/bin/perl use strict; use warnings; =pod Use this script as the input file to be cut up. We'll put the generated files into a throw away directory that is a sub-directory to the directory we are running the script from. This script creates the split files and the rejoined text. The rejoined text doesn't get saved, but is compared to the original script as a check that everything worked. =cut my $subDir = './delme'; # Create a throw away sub-directory for the test. Wrapped in an eval because # we don't care if it fails (probably because the dir already exists). eval {mkdir $subDir}; seek DATA, 0, 0; # Set DATA to the start of this file my $origText = do {local $/; }; # Slurp the script text to check against seek DATA, 0, 0; # Back to the start again # Create the split files my $fileNum = 0; while (!eof DATA) { my $fileLines; $fileLines .= for 1 .. 2; last if !defined $fileLines; ++$fileNum; open my $outFile, '>', "$subDir/outFile$fileNum.txt"; print $outFile $fileLines; close $outFile; } # Join the files back up again my $joinedText; $fileNum = 1; while (open my $fileIn, '<', "$subDir/outFile$fileNum.txt") { $joinedText .= do {local $/; <$fileIn>}; # Slurp the file ++$fileNum; } print "Saved and Loaded OK\n" if $joinedText = $origText; __DATA__