January 2014 Archives

Tie::File don't while(<>)

Consider the common case where you want to take a text file and walk it doing various operations such as stripping whitespace, looking for duplicates, sorting or other common operations. I was recently asked to work on a program where part of the process is 'cleaning' uploaded files before doing other things with it. This process was over 40 lines of subs opening old and new filehandles, doing the standard while(<>) {} and then renaming new file to old file. If you find yourself wanting to do something you would normally do on an array, but on a file and often in place, consider Tie::File before while()ing away at it. The documentation has many examples and there are several examples on the web including at Perl Monks. In this case I was able to whittle this down to this:

tie my @file, 'Tie::File', $file;
@file = grep !$seen{$_}++,
            map s/$RE{ws}{crop}//g,
            map lc, @file;

untie @file;

If you're working with CSV or other delimited files, consider Tie::Array::CSV


About Jesse Shy

user-pic Perl pays for my travel addiction.