AcmeなるモジュールをCPANに求めて

tag perl acme

(firstly written in Japanese, later in English)

こんにちは。makamakaです。
書く人がいないので埋め草的な記事です。Acme大全2010の使い回しです。

CPANでも割とユニークだと私が感じるモジュールを紹介しましょう。十分Acmeなモノです。
Text::CSV::UniqueColumns。CSVファイルから重複しない値を持ったカラムを探すモジュールです。

# ヘッダを持ったCSVファイル
#------- foo.csv ------
col1,col2,col3,col4
1,a,1,a
2,b,1,b
3,a,2,c
4,a,2,d
#-----------------------

use strict;
use Text::CSV::UniqueColumns;

$csv  = Text::CSV::UniqueColumns->new( 'foo.csv' );

print $csv->checkUniq('col1'); # => 1

print $csv->getUniqCols();     # => col1,col4

で、まあ、Text::CSVを使って値を調べたりするのかなと思ってソースを見たのですよ。そしたら

sub getUniqCols {
(中略)
    my $sCmd = "cut -f$iField -d , $self->{'_file'} | sed s/' '//g | wc -l;";
    $sCmd .= "cut -f$iField -d ,  $self->{'_file'} | sed s/' '//g | sort | uniq | wc -l";
    my ($iCount1, $iCount2) = split("\n",`$sCmd`);

あー……

sub checkUniq {
(中略)
    my $sCmd = "paste  -d , $sPasteFiles > pasteOutput ";
    push (@cleanup, 'pasteOutput');
    $sPasteOutput = `$sCmd`;
    my $iCount1 =  `cat pasteOutput | sed s/' '//g | wc -l`;
    my $iCount2 =  `cat pasteOutput | sed s/' '//g | sort | uniq |  wc -l`;


あー……

sub cleanUp {
    foreach my $sFile (@cleanup){
        `rm -f $sFile`;
    }
}

↑そこはかとなく不安感を醸し出すコード


はい、なんだか不思議な気持ちになりましたね? 十分Acmeなモノです。

というわけで、このお正月はAcme的なモノを求めて夢の世界に……





* ----------------------------------------------------
* English version
* ----------------------------------------------------
TITLE: The Acme-Quest of Unknown CPAN

Hello everyone. I'm makamaka.
As no one writes articles, I worte.

I introduce a module felt as a unique module on CPAN. Enough Acme.
Text::CSV::UniqueColumns.
It finds columns which have unique values.

# a csv file which has its header.
#------- foo.csv ------
col1,col2,col3,col4
1,a,1,a
2,b,1,b
3,a,2,c
4,a,2,d
#-----------------------

use strict;
use Text::CSV::UniqueColumns;

$csv  = Text::CSV::UniqueColumns->new( 'foo.csv' );

print $csv->checkUniq('col1'); # => 1

print $csv->getUniqCols();     # => col1,col4

Firstly I thought using Text::CSV.
And I look at the source.

sub getUniqCols {
...
    my $sCmd = "cut -f$iField -d , $self->{'_file'} | sed s/' '//g | wc -l;";
    $sCmd .= "cut -f$iField -d ,  $self->{'_file'} | sed s/' '//g | sort | uniq | wc -l";
    my ($iCount1, $iCount2) = split("\n",`$sCmd`);


Ah....

sub checkUniq {
...
    my $sCmd = "paste  -d , $sPasteFiles > pasteOutput ";
    push (@cleanup, 'pasteOutput');
    $sPasteOutput = `$sCmd`;
    my $iCount1 =  `cat pasteOutput | sed s/' '//g | wc -l`;
    my $iCount2 =  `cat pasteOutput | sed s/' '//g | sort | uniq |  wc -l`;


Ah....

sub cleanUp {
    foreach my $sFile (@cleanup){
        `rm -f $sFile`;
    }
}

↑somehow anxious code. Enough Acme.


Well, in this winter vacation, would you like to travel to dream land for Acme...