Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@gardejo
Created October 15, 2009 16:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gardejo/211069 to your computer and use it in GitHub Desktop.
Save gardejo/211069 to your computer and use it in GitHub Desktop.
sample code snippet to explain one-way dependeded attributes
#!/usr/local/bin/perl
# sample code snippet to explain one-way dependeded attributes
# see http://blog.eorzea.asia/2009/10/post_72.html
use strict;
use warnings;
use utf8;
{
package MyApp::Authentication;
# Thanks, Goro Fuji (gfx) san!
# ( http://b.hatena.ne.jp/gfx/20091016#bookmark-16746813 )
# use Moose;
use Any::Moose;
use Digest;
use namespace::clean -except => [qw(meta)];
has 'password' => (
is => 'rw',
isa => 'Str',
trigger => sub {
$_[0]->_clear_hashed_password;
},
);
has 'hashed_password' => (
is => 'ro',
isa => 'Str',
init_arg => undef,
lazy_build => 1,
clearer => '_clear_hashed_password',
);
sub _build_hashed_password {
my $digest = Digest->new('SHA-256');
$digest->add($_[0]->password);
return $digest->hexdigest;
}
__PACKAGE__->meta->make_immutable;
}
{
use Digest;
use Encode;
use Test::More;
my $password = 'foobar';
my $digest = Digest->new('SHA-256');
$digest->add($password);
my $hashed_password = $digest->hexdigest;
my $auth = MyApp::Authentication->new;
$auth->password($password);
is $auth->password, $password
=> encode_utf8('平文パスワード');
is $auth->hashed_password, $hashed_password
=> encode_utf8('ハッシュ化パスワード');
done_testing;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment