The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

File::Sort - Sort a file or merge sort multiple files.

SYNOPSIS

  use File::Sort qw(sortFile);
  sortFile({
    I=>[qw(file1_new file2_new)],
    O=>'filex_new',
    V=>1,Y=>1000,TF=>50,M=>1,U=>1,R=>1,N=>1,T=>'/tmp'
  });

  sortFile('file1','file1_new',1,1000);

DESCRIPTION

WARNING: This is MUCH SLOWER than using sort(1) that comes with most Unix boxes. This was developed primarily because some perls (specifically, MacPerl) do not have access to potentially infinite amounts of memory (thus they cannot necessarily slurp in a text file of several megabytes), nor does everyone have access to sort(1).

Here are some benchmarks that might be of interest (Power Mac 7100/66 with MkLinux DR2.1, but results were similar on an Ultra SPARC 1 running Solaris 2.5.1). The file was a mail file around 6MB. Note that once was with a CHUNK value of 200,000 lines, which was more than the whole file contained; Unix systems can get away with something like that because of VM, while Mac OS systems cannot. So inevitably you will get much better performance with large files on Unix than you will on Mac OS. C'est la vie.

  use File::Sort qw(sortFile);
  use Benchmark;
  timethese(10,{
    1=>q+`sort -o $ARGV[0].1 $ARGV[0]`+,
    2=>q+open(F,$ARGV[0]);open(F1,">$ARGV[0].4");@f=<F>;print F1 sort @f+
    3=>q+sortFile({I=>$ARGV[0],O=>"$ARGV[0].2",Y=>200000})+,
    4=>q+sortFile({I=>$ARGV[0],O=>"$ARGV[0].3"})+,
  })

  Benchmark: timing 10 iterations of 1, 2, 3...
    1: 161 secs ( 0.01 usr  0.03 sys + 105.47 cusr 34.37 csys = 139.88 cpu)
    2: 262 secs (215.18 usr 21.60 sys = 236.78 cpu)
    3: 781 secs (670.78 usr 48.45 sys = 719.23 cpu)
    4: 13239 secs (12981.68 usr 79.65 sys = 13061.33 cpu)

That all having been noted, there are plans to have this module use sort(1) if it is available.

WARNING Part Deux: This module is subject to change in every way, including the fact that it exists.

There are two primary syntaxes:

  sortFile(INFILE, OUTFILE [, VERBOSE, CHUNK]);

This will sort INFILE to OUTFILE. The OUTFILE can be the same as the INFILE, but it is required. VERBOSE is off by default. CHUNK is how many lines to deal with at a time (as opposed to how much memory to deal with at a time, like sort(1); this might change). We hope to gain some more intelligence for sort in the future.

  sortFile({
    I=>INFILE, O=>OUTFILE, V=>VERBOSE, 
    Y=>CHUNK, TF=>FILE_LIMIT, 
    M=>MERGE_ONLY, U=>UNIQUE_ONLY, 
    R=>REVERSE, N=>NUMERIC, T=>TEMP_DIR
  });

This time, FILEIN can be a filename or an reference to an array of filenames. If MERGE_ONLY is true, then File::Sort will assume the files on input are already sorted. UNIQUE_ONLY, if true, only outputs unique lines, removing all others. TEMP_DIR gives a location for temporary directory. A default will try to be ascertained if it none is given, finally reverting to the directory of the outfile if none is given. FILE_LIMIT is the system's limit to how many files can be opened at once. A default value is given in the module.

Note that if INFILE does not have a linebreak terminating the last line, a native linebreak character will be added to it.

EXPORT

Exports sortFile() on request.

RETURN VALUE

Currently, sortFile() returns nothing. Any ideas on this are welcome.

BUGS

None! :) I plan on making CHUNK and FILE_LIMIT more intelligent somehow, and on allowing more ordering options besides just regular, numeric and reverse.

Also, I will have the module use sort(1) if it is available.

AUTHOR

Chris Nandor <pudge@pobox.com> http://pudge.net/

Copyright (c) 1998 Chris Nandor. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

HISTORY

v0.11 (04 January 1998)

More cleanup; fixed special case of no linebreak on last line; wrote test suite; fixed warning for redefined subs (sort1 and sort2).

v0.10 (03 January 1998)

Some cleanup; made it not subject to system file limitations; separated many parts out into separate functions.

v0.03 (23 December 1997)

Added reverse and numeric sorting options.

v0.02 (19 December 1997)

Added unique and merge-only options.

v0.01 (18 December 1997)

First release.

VERSION

Version 0.11 (03 January 1998)

SEE ALSO

perl(1), sort(1).