X-Git-Url: https://git.dogcows.com/gitweb?a=blobdiff_plain;f=lib%2FFile%2FKDBX%2FUtil.pm;h=f1b997619fee6783b4a9e349b83f5434436b308e;hb=1d0a10e989a4d0487aa13cf4f56e533d3795469d;hp=3141c3fe969c35369482d84bd85c911a87b81647;hpb=4919a87027c9f0501a636fbea0cbd2a6510afb38;p=chaz%2Fp5-File-KDBX diff --git a/lib/File/KDBX/Util.pm b/lib/File/KDBX/Util.pm index 3141c3f..f1b9976 100644 --- a/lib/File/KDBX/Util.pm +++ b/lib/File/KDBX/Util.pm @@ -21,7 +21,7 @@ our $VERSION = '999.999'; # VERSION our %EXPORT_TAGS = ( assert => [qw(assert_64bit)], - class => [qw(extends has)], + class => [qw(extends has list_attributes)], clone => [qw(clone clone_nomagic)], coercion => [qw(to_bool to_number to_string to_time to_tristate to_uuid)], crypt => [qw(pad_pkcs7)], @@ -35,7 +35,7 @@ our %EXPORT_TAGS = ( load => [qw(load_optional load_xs try_load_optional)], search => [qw(query search search_limited simple_expression_query)], text => [qw(snakify trim)], - uuid => [qw(format_uuid generate_uuid is_uuid uuid)], + uuid => [qw(format_uuid generate_uuid is_uuid uuid UUID_NULL)], uri => [qw(split_url uri_escape_utf8 uri_unescape_utf8)], ); @@ -85,6 +85,7 @@ my %OP_NEG = ( '=~' => '!~', '!~' => '=~', ); +my %ATTRIBUTES; =func load_xs @@ -397,54 +398,43 @@ sub has { my $name = shift; my %args = @_ % 2 == 1 ? (default => shift, @_) : @_; + my ($package, $file, $line) = caller; + my $d = $args{default}; - my $default = is_arrayref($d) ? sub { [%$d] } : is_hashref($d) ? sub { +{%$d} } : $d; + my $default = is_arrayref($d) ? sub { [@$d] } : is_hashref($d) ? sub { +{%$d} } : $d; my $coerce = $args{coerce}; my $is = $args{is} || 'rw'; - my $has_default = is_coderef $default; - my $has_coerce = is_coderef $coerce; + my $store = $args{store}; + ($store, $name) = split(/\./, $name, 2) if $name =~ /\./; + push @{$ATTRIBUTES{$package} //= []}, $name; - my $caller = caller; - no strict 'refs'; ## no critic (ProhibitNoStrict) - if (my $store = $args{store}) { - *{"${caller}::${name}"} = $is eq 'ro' && $has_default ? sub { - $_[0]->$store->{$name} //= scalar $default->($_[0]); - } : $is eq 'ro' ? sub { - $_[0]->$store->{$name} //= $default; - } : $has_default && $has_coerce ? sub { - $#_ ? $_[0]->$store->{$name} = scalar $coerce->($_[1]) - : $_[0]->$store->{$name} //= scalar $default->($_[0]); - } : $has_default ? sub { - $#_ ? $_[0]->$store->{$name} = $_[1] - : $_[0]->$store->{$name} //= scalar $default->($_[0]); - } : $has_coerce ? sub { - $#_ ? $_[0]->$store->{$name} = scalar $coerce->($_[1]) - : $_[0]->$store->{$name} //= $default; - } : sub { - $#_ ? $_[0]->$store->{$name} = $_[1] - : $_[0]->$store->{$name} //= $default; - }; - } - else { - *{"${caller}::${name}"} = $is eq 'ro' && $has_default ? sub { - $_[0]->{$name} //= scalar $default->($_[0]); - } : $is eq 'ro' ? sub { - $_[0]->{$name} //= $default; - } : $has_default && $has_coerce ? sub { - $#_ ? $_[0]->{$name} = scalar $coerce->($_[1]) - : $_[0]->{$name} //= scalar $default->($_[0]); - } : $has_default ? sub { - $#_ ? $_[0]->{$name} = $_[1] - : $_[0]->{$name} //= scalar $default->($_[0]); - } : $has_coerce ? sub { - $#_ ? $_[0]->{$name} = scalar $coerce->($_[1]) - : $_[0]->{$name} //= $default; - } : sub { - $#_ ? $_[0]->{$name} = $_[1] - : ($_[0]->{$name} //= $default); - }; + my $store_code = ''; + $store_code = qq{->$store} if $store; + my $member = qq{\$_[0]$store_code\->{'$name'}}; + + my $default_code = is_coderef $default ? q{scalar $default->($_[0])} + : defined $default ? q{$default} + : q{undef}; + my $get = qq{$member //= $default_code;}; + + my $set = ''; + if ($is eq 'rw') { + $set = is_coderef $coerce ? qq{$member = scalar \$coerce->(\@_[1..\$#_]) if \$#_;} + : defined $coerce ? qq{$member = do { local @_ = (\@_[1..\$#_]); $coerce } if \$#_;} + : qq{$member = \$_[1] if \$#_;}; } + + $line -= 4; + my $code = <