From e496f621262e74133d9870cc197217552ad10185 Mon Sep 17 00:00:00 2001 From: Daniel Friesel Date: Mon, 4 Jul 2011 09:37:36 +0200 Subject: Raps2.pm: Make file_to_hash return a hashref (consistency with pw_load) --- Changelog | 1 + bin/raps2 | 10 +++++----- lib/App/Raps2.pm | 32 ++++++++++++++++---------------- t/29-app-raps2.t | 2 +- 4 files changed, 23 insertions(+), 22 deletions(-) diff --git a/Changelog b/Changelog index 9a49db0..3306369 100644 --- a/Changelog +++ b/Changelog @@ -5,6 +5,7 @@ git HEAD * Rename pw_add t pw_save * Rename pw_get to pw_load * pw_load now also returns the salt + * file_to_hash now returns a hashref instead of a hash App::Raps2 0.4 - Sun Jul 03 2011 diff --git a/bin/raps2 b/bin/raps2 index e05a7d5..689ecd2 100755 --- a/bin/raps2 +++ b/bin/raps2 @@ -141,8 +141,8 @@ sub cmd_info { exit 2; } - my %key = $raps2->file_to_hash($pwfile); - $raps2->ui->output( [ 'URL', $key{url} ], [ 'Login', $key{login} ], ); + my $key = $raps2->file_to_hash($pwfile); + $raps2->ui->output( [ 'URL', $key->{url} ], [ 'Login', $key->{login} ], ); return; } @@ -151,11 +151,11 @@ sub cmd_list { my @files = read_dir( data_home('raps2') ); for my $file ( sort @files ) { - my %key = $raps2->file_to_hash( data_files("raps2/${file}") ); + my $key = $raps2->file_to_hash( data_files("raps2/${file}") ); $raps2->ui->list( [ 'Account', $file ], - [ 'Login', $key{login} ], - [ 'URL', $key{url} ], + [ 'Login', $key->{login} ], + [ 'URL', $key->{url} ], ); } diff --git a/lib/App/Raps2.pm b/lib/App/Raps2.pm index 61bbaee..faf74d9 100644 --- a/lib/App/Raps2.pm +++ b/lib/App/Raps2.pm @@ -36,7 +36,7 @@ sub new { sub file_to_hash { my ( $self, $file ) = @_; - my %ret; + my $ret; for my $line ( slurp($file) ) { my ( $key, $value ) = split( qr{ \s+ }x, $line ); @@ -45,9 +45,9 @@ sub file_to_hash { next; } - $ret{$key} = $value; + $ret->{$key} = $value; } - return %ret; + return $ret; } sub sanity_check { @@ -101,10 +101,10 @@ sub create_config { sub load_config { my ($self) = @_; - my %cfg = $self->file_to_hash( $self->{xdg_conf} . '/password' ); - $self->{master_hash} = $cfg{hash}; - $self->{master_salt} = $cfg{salt}; - $self->{default}->{cost} //= $cfg{cost}; + my $cfg = $self->file_to_hash( $self->{xdg_conf} . '/password' ); + $self->{master_hash} = $cfg->{hash}; + $self->{master_salt} = $cfg->{salt}; + $self->{default}->{cost} //= $cfg->{cost}; return; } @@ -159,16 +159,16 @@ sub pw_load { $data{file} //= $self->{xdg_data} . "/$data{name}"; - my %key = $self->file_to_hash( $data{file} ); + my $key = $self->file_to_hash( $data{file} ); return { - url => $key{url}, - login => $key{login}, - password => $self->pw->decrypt( $key{hash}, $key{salt} ), - salt => $key{salt}, + url => $key->{url}, + login => $key->{login}, + password => $self->pw->decrypt( $key->{hash}, $key->{salt} ), + salt => $key->{salt}, extra => ( - $key{extra} - ? $self->pw->decrypt( $key{extra}, $key{salt} ) + $key->{extra} + ? $self->pw->decrypt( $key->{extra}, $key->{salt} ) : undef ), }; @@ -216,8 +216,8 @@ B of key setup, passed on to App::Raps2::Password(3pm). =item $raps2->file_to_hash(I<$file>) -Reads $file (lines with key/value separated by whitespace) and returns a hash -with its key/value pairs. +Reads $file (lines with key/value separated by whitespace) and returns a +hashref with its key/value pairs. =item $raps2->get_master_password() diff --git a/t/29-app-raps2.t b/t/29-app-raps2.t index ddea7fa..53aa51d 100644 --- a/t/29-app-raps2.t +++ b/t/29-app-raps2.t @@ -24,7 +24,7 @@ isa_ok($r2, 'App::Raps2'); isa_ok($r2->ui(), 'App::Raps2::UI'); is_deeply( - { $r2->file_to_hash('t/in/hash') }, + $r2->file_to_hash('t/in/hash'), { key => 'value', otherkey => 'othervalue' }, 'file_to_hash works', ); -- cgit v1.2.3