Spreadsheet-Read

 view release on metacpan or  search on metacpan

Read.pm  view on Meta::CPAN

    wrap    => 0,
    merged  => 0,
    hidden  => 0,
    locked  => 0,
    enc     => "utf-8", # $ENV{LC_ALL} // $ENV{LANG} // ...
    formula => undef,
    );

# Helper functions

sub _dump {
    my ($label, $ref) = @_;
    if ($can{dmp}) {
	print STDERR Data::Peek::DDumper ({ $label => $ref });
	}
    else {
	print STDERR Data::Dumper->Dump ([$ref], [$label]);
	}
    my @c = caller (1);
    print STDERR "<<- $c[1]:$c[2]|$c[3]\n";
    } # _dump

sub _parser {
    my $type = shift		or  return "";
    if ($type =~ m/::/ and my @p = grep { $_->[1] eq $type } @parsers) {
	my $format = $p[0][0];
	$ENV{"SPREADSHEET_READ_\U$format"} = $type;
	eval "local \$_; require $type";
	$@ and croak ("Forced backend $type for $format fails to load:\n$@");
	$can{$format} = $type;
	$type = $format;
	}
    $type = lc $type;
    my $ods = $can{ods} ? "ods" : "sxc";
    # Aliases and fullnames
    $type eq "excel"		and return "xls";
    $type eq "excel2007"	and return "xlsx";
    $type eq "xlsm"		and return "xlsx";
    $type eq "oo"		and return $ods;
#   $type eq "sxc"		and return $ods;
    $type eq "openoffice"	and return $ods;
    $type eq "libreoffice"	and return $ods;
    $type eq "perl"		and return "prl";
    $type eq "scalc"		and return "sc";
    $type eq "squirrelcalc"	and return "sc";
    return exists $can{$type} ? $type : "";
    } # _parser

sub new {
    my $class = shift;
    my $r = ReadData (@_);
    unless ($r) {
	@_ and return;	# new with arguments failed to open resource
	$r = [{
	    parsers	=> [],
	    error	=> undef,
	    sheets	=> 0,
	    sheet	=> { },
	    }];
	}
    bless $r => $class;
    } # new

sub parsers {
    ref $_[0] eq __PACKAGE__ and shift;
    my @c;
    for (sort { $a->[0] cmp $b->[0] || $a->[1] cmp $b->[1] }
         grep { $_->[0] !~ m{^(?:dmp|ios|!.*)$} }
         @parsers) {
	my ($typ, $mod, $min) = @$_;
	eval "local \$_; require $mod";
	my $vsn = $@ ? "-" : eval { $mod->VERSION };
	push @c => {
	    ext => $typ,
	    mod => $mod,
	    min => $min,
	    vsn => $vsn,
	    def => $can{$typ} eq $mod ? "*" : "",
	    };
	}
    @c;
    } # parsers

# Spreadsheet::Read::parses ("csv") or die "Cannot parse CSV"
sub parses {
    ref $_[0] eq __PACKAGE__ and shift;

    my $type = shift or
	return sort grep { !m/^(?:dmp|ios)/ && $can{$_} !~ m{^!} }
	    keys %can;

    $type = _parser ($type) or return 0;
    if ($can{$type} =~ m/^!\s*(.*)/) {
	$@ = $1;
	return 0;
	}
    return $can{$type} || 0;
    } # parses

sub sheets {
    my $ctrl = shift->[0];
    wantarray or return $ctrl->{sheets};

    my $s = $ctrl->{sheet} or return (); # No labels defined
    sort { $s->{$a} <=> $s->{$b} } keys %$s;
    } # sheets

# col2label (4) => "D"
sub col2label {
    ref $_[0] eq __PACKAGE__ and shift;
    my $c = shift;
    defined $c && $c > 0 or return "";
    my $cell = "";
    while ($c) {
	use integer;

	substr $cell, 0, 0, chr (--$c % 26 + ord "A");
	$c /= 26;
	}
    $cell;
    } # col2label

 view all matches for this distribution
 view release on metacpan -  search on metacpan

( run in 2.836 seconds using v1.00-cache-2.02-grep-82fe00e-cpan-c30982ac1bc3 )