App-CCSV
view release on metacpan or search on metacpan
lib/App/CCSV.pm view on Meta::CPAN
{
my $name = shift;
_init(@_);
__PACKAGE__->export_to_level(1,1,qw(csay cprint parse $csv));
tie *ARGV, 'TieCSV',$csv;
}
sub _init
{
my $out = length($_[-1] || '') > 1 && $#_ > 0 ? pop : ''; # last argument length > 1 means csvout config section if more than 1 argument
my ($quote,$sep,$escape,$eol) = @_;
#print join "|", ($quote, $sep, $escape, $eol,"\n");
my $cfgfile = $ENV{CCSVCONF} || $ENV{HOME} || '~';
$cfgfile = $cfgfile . '/.CCSVConf' unless $ENV{CCSVCONF};
if (length($quote || '') <= 1) # length > 1 specifies config section
{
$csv = Text::CSV_XS->new( {
quote_char => $quote || '"',
sep_char => $sep || ',',
escape_char => $escape || '"',
eol => $eol || '',
binary => 1,
} );
} else
{
$csv = Text::CSV_XS->new(); # config later from cfgfile
}
if (-r $cfgfile)
{
my $conf = new Config::General($cfgfile);
lib/App/CCSV.pm view on Meta::CPAN
=head1 EXPORTS
The array C<@f>, which will hold the field values of the CSV, the functions C<cprint()>, C<csay()>, C<parse()>, and the L<Text::CSV_XS> objects C<$csv> and C<$csvout> will be exported by default.
C<$csv> will be initialized with your config for the CSV input, C<$csvout> for the CSV output. In case you haven't specified any configs, C<$csv> and C<$csvout> will be initialized as this:
Text::CSV_XS->new( {
quote_char => '"',
sep_char => ',',
escape_char '"',
eol => '',
binary => 1,
} );
Note that these are slightly different from the L<Text::CSV_XS> defaults.
=head1 FUNCTIONS
=over 4
lib/App/CCSV.pm view on Meta::CPAN
like C<cprint()>, with an added newline at the end (actually, the value of C<$/> is printed at the end of the line).
=item parse($string)
parses C<$string> according to the current input CSV configuration, and returns the fields as an array. Note that you will hardly need that function, as you will have the fields of the current line in C<@f> automatically - this function is here "jus...
=back
=head1 CONFIGURATION
You can configure CSV quote char, separator, escape char and end of line char by passing them directly to the module, like this:
:~$ perl -MApp::CCSV=<quote char>,<sep char>,<escape char>,<eol char> -ne 'print @f' < some.csv
Note that these are really limited to one character. More than one character would mean a config section in the config file (more about the config file in a moment).
If you don't specify any options, the defaults shown under "EXPORTS" will be used.
Any of the characters can be skipped, in which case the defaults will be used. For instance, setting only the escape char to for example "@" will look like this:
:~$ perl -MApp::CCSV=,,@ -ne 'print @f' < some.csv
=head1 CONFIG FILE
App::CCSV expects a config file in any format L<Config::General> understands.
There are two ways of setting a config file. Firstly, APP::CCSV will look for a config file in your environment variable C<$ENV{CCSVCONF}>.
Secondly, if this variable is not set, App:CCSV will look for a file called .CCSVConf in your home directory. If there is none, it falls back to defaults. An example .CCSVConf file is included in the examples directory of this distribution.
( run in 1.336 second using v1.01-cache-2.11-cpan-c21f80fb71c )