You want to copy a file and randomly reorder its lines.
Read all lines into an array, shuffle the array using List::Util's shuffle function, and write the shuffled lines back out:
use List::Util qw(shuffle); while (<INPUT>) { push(@lines, $_); } @lines = shuffle(@lines); foreach (@reordered) { print OUTPUT $_; }
The easiest approach is to read all lines into memory and shuffle them there. Because you don't know where lines start in the file, you can't just shuffle a list of line numbers and then extract lines in the order they'll appear in the shuffled file. Even if you did know the byte offsets of the start of each line, it would probably still be slower because you'd be seeking around in the file instead of sequentially reading it from start to finish.
If you have a version of Perl older than v5.8, you can download the List::Util module from CPAN.
The documentation for the standard List::Util module; Recipe 2.6; Recipe 2.7; Recipe 4.18