#!/usr/bin/env perl
our
(
$mydir
,
$myname
);
BEGIN {
my
$location
= (-l $0) ? abs_path($0) : $0;
$location
=~ /(.*?)([^\/]+?)_?\z/s or
die
"?"
;
(
$mydir
,
$myname
) = ($1, $2);
}
sub
usage {
print
"error: @_\n"
if
@_
;
print
"usage:
$myname
mappingsfile.pl
Read mail addresses from stdin,
map
using the mapping function
returned in the
'map'
field of the hashtable returned as the
last
value from mappingsfile.pl
Options:
--
sort
-result
sort
according to result value from the
mapping function, using the function in the
'cmp'
field of the configuration hashtable as the
comparison function (
default
: case sensitive
string search).
";
exit
1;
}
our
$verbose
= 0;
my
$opt_sort_result
;
GetOptions(
"verbose"
=> \
$verbose
,
"help"
=>
sub
{usage},
"sort-result"
=> \
$opt_sort_result
,
) or
exit
1;
usage
unless
@ARGV
== 1;
my
(
$mappingfile
) =
@ARGV
;
use
FP::Ops
qw(cut_method unary_operator)
;
my
$config
=
require
$mappingfile
;
ref
(
$config
) eq
"HASH"
or usage
"invalid mappingfile, does not return a hash ref"
;
my
(
$mapfn
,
$cmp
) =
@$config
{
"map"
,
"cmp"
};
my
$in
= glob_to_fh(
*STDIN
);
my
$out
= glob_to_fh(
*STDOUT
);
my
$lines
= fh_to_lines
$in
;
my
$mapped
=
$lines
->
map
(
sub
{
my
(
$line
) =
@_
;
chomp
$line
;
&$mapfn
(
$line
)
}
);
my
$result
=
do
{
my
$l
=
$mapped
->filter(unary_operator
'defined'
);
$opt_sort_result
?
$l
->
sort
(
$cmp
) :
$l
};
$result
->for_each(cut_method
$out
,
"xprintln"
);
$out
->xclose;