Skip to content

Instantly share code, notes, and snippets.

@mattak
Created May 23, 2013 05:35
Show Gist options
  • Save mattak/5632948 to your computer and use it in GitHub Desktop.
Save mattak/5632948 to your computer and use it in GitHub Desktop.
#!/usr/bin/env perl
use strict;
use warnings;
use utf8;
use 5.010;
use Encode;
use Path::Class;
use Text::CSV_XS;
my $csv = Text::CSV_XS->new({ binary => 1 });
my $from_stdin = ($#ARGV != 0);
sub getline_as_stdin {
my $code = shift;
while(my $line = <STDIN>) {
chomp($line);
$line = decode('utf-8', $line);
$code->($line);
}
}
sub getline_as_file {
my ($file, $code) = @_;
my $io = file($file)->openr;
while (my $line = $io->getline) {
chomp($line);
$line = decode('utf-8', $line);
$code->($line);
}
$io->close;
}
my $code = sub {
my $line = shift;
my @fields = split /\t/, $line;
$csv->print(*STDOUT, [ map { utf8::upgrade( my $x = $_ ); $x } @fields ] );
print "\n";
};
if ($from_stdin) {
getline_as_stdin($code);
}
else {
getline_as_file($ARGV[0], $code);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment