App-CCSV

 view release on metacpan or  search on metacpan

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

package App::CCSV;

use strict;
use Data::Dumper;
use App::CCSV::TieCSV;
use Text::CSV_XS;
use Config::General;
require Exporter;
our @ISA = qw(Exporter);

our $csv;
our $csvout;

our @EXPORT = qw(cprint csay parse $csv);

our $VERSION = 0.02;

sub import 
{
	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;
		# CSV:
		if (!@_ || ($#_ == 0 and $quote eq '')) # no override(s) from command line
		{
			_confme($csv,$config{CCSV}) if $config{CCSV};
		} elsif (length($quote) > 1) # config section
		{
				die "no config section with name <$quote> could be found in $cfgfile\n" unless $config{CCSV}->{names}->{$quote};
				_confme($csv,$config{CCSV}->{names}->{$quote});
		}

		_confme($csv,$config{CCSV}->{overrideall}) if $config{CCSV}->{overrideall}; 
		# different CSVOUT?
		if ($out) # get from config section
		{
			 die "no config section with name <$out> could be found in $cfgfile\n" unless $config{CCSV}->{names}->{$out};



( run in 2.671 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )