From 963983123e5166b3e8bead9ed8d7d6ac175c1cb1 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Fri, 4 Dec 2015 00:41:41 -0500 Subject: use the same password api as Spreadsheet::ParseExcel --- lib/Spreadsheet/ParseXLSX.pm | 48 +++++++++++++++++--------------------------- t/encryption.t | 5 +++-- 2 files changed, 21 insertions(+), 32 deletions(-) diff --git a/lib/Spreadsheet/ParseXLSX.pm b/lib/Spreadsheet/ParseXLSX.pm index 4c446c9..d750e9b 100644 --- a/lib/Spreadsheet/ParseXLSX.pm +++ b/lib/Spreadsheet/ParseXLSX.pm @@ -33,24 +33,27 @@ This module is an adaptor for L that reads XLSX files. =cut -=method new +=method new(%opts) -Returns a new parser instance. Takes no parameters. +Returns a new parser instance. Takes a hash of parameters: + +=over 4 + +=item Password + +Password to use for decrypting encrypted files. + +=back =cut sub new { - my $self = bless {}, shift; - my ($param) = @_; + my $class = shift; + my (%args) = @_; + + my $self = bless {}, $class; + $self->{Password} = $args{Password} if defined $args{Password}; - if (ref($param) eq 'HASH') { - if (exists($param->{password})) { - $self->{password} = $param->{password}; - } - if (exists($param->{formatter})) { - $self->{formatter} = $param->{formatter}; - } - } return $self; } @@ -65,27 +68,12 @@ The C<$formatter> argument is an optional formatter class as described in Lnew; my $signature = ''; my $tempfile; - - if (ref($param1) eq 'HASH') { - $formatter = $param1->{formatter}; - $password = $param1->{password}; - } else { - $formatter = $param1; - $password = $param2; - } - - $formatter = $formatter || $self->{formatter}; - $password = $password || $self->{password}; - - my $workbook = Spreadsheet::ParseExcel::Workbook->new; - if (openhandle($file)) { if (ref($file) eq 'GLOB') { read($file, $signature, 2); @@ -106,7 +94,7 @@ sub parse { } if ($signature eq "\xd0\xcf") { - $tempfile = $file = Spreadsheet::ParseXLSX::decryptor->open($file, $password); + $tempfile = $file = Spreadsheet::ParseXLSX::decryptor->open($file, $self->{Password}); } eval { diff --git a/t/encryption.t b/t/encryption.t index cbd913e..b2d0cca 100644 --- a/t/encryption.t +++ b/t/encryption.t @@ -5,8 +5,8 @@ use Test::More; use Spreadsheet::ParseXLSX; -my $parser = Spreadsheet::ParseXLSX->new(); -my $workbook = $parser->parse("t/data/encryption-agile-123q.xlsx", {password => '123q'}); +my $parser = Spreadsheet::ParseXLSX->new(Password => '123q'); +my $workbook = $parser->parse("t/data/encryption-agile-123q.xlsx"); my $worksheet; my $cell; @@ -19,6 +19,7 @@ ok(defined($cell) && $cell->value() eq 'abcdefgABCDEFG'); open FH, "t/data/encryption-standard-default-password.xlsx"; +$parser = Spreadsheet::ParseXLSX->new(Password => ''); $workbook = $parser->parse(\*FH); ok(defined($workbook)); -- cgit v1.2.3-54-g00ecf