in reply to rearrange lines in file

Another way to go about this is to read through the file once to create an index:
my @index; do {push @index, tell FIN} while <FIN>; pop @index;
(That pop is necessary to delete the value that points one byte past the end of the file.)

Then you can read lines from the file in any order you want, using seek with elements of @index, and print them to the output file in sequence. For example, to read and print the 5th line from the input file:

seek FIN, $index[4], 0; print FOUT scalar <FIN>;
(The scalar is necessary to keep <FIN> from being evaluated in print's default list context, which would read and print the rest of the input file instead of just one line.)