package Business::CreditCard::Obscure; use strict; use warnings; sub new { my ($pack,%in) = @_; my $hash = {}; $hash->{'cardnum'} = defined($in{'cardnum'}) ? $in{'cardnum'} : undef; $hash->{'head'} = defined($in{'head'}) ? $in{'head'} : 0; $hash->{'tail'} = defined($in{'tail'}) ? -$in{'tail'} : -4; $hash->{'replacement'} = defined($in{'replacement'}) ? $in{'replacement'} : '*'; return bless $hash, $pack; } sub obscure { my ($check,%in) = @_; my $self = ($check eq __PACKAGE__) ? $check->new(%in) : $check; $self->{'cardnum'} = $in{'cardnum'} if ($in{'cardnum'}); $self->{'head'} = $in{'head'} if ($in{'head'}); $self->{'tail'} = - $in{'tail'} if ($in{'tail'}); $self->{'replacement'} = $in{'replacement'} if ($in{'replacement'}); return "cardnum not found" unless (defined($self->{'cardnum'})); return "head not found" unless (defined($self->{'head'})); return "tail not found" unless (defined($self->{'tail'})); return "replacement not found" unless (defined($self->{'replacement'})); my $cardnum = $self->{'cardnum'}; substr( $cardnum, $self->{'head'}, $self->{'tail'} ) =~ s/./$self->{'replacement'}/g; return $self->{'obscured'} = $cardnum; } 1;