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);
		my %config = $conf->getall;

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

=item cprint(@array)

lib/App/CCSV.pm  view on Meta::CPAN

=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



( run in 0.691 second using v1.01-cache-2.11-cpan-8f98c5d2c55 )