X-Git-Url: https://git.dogcows.com/gitweb?p=chaz%2Fp5-File-KDBX;a=blobdiff_plain;f=lib%2FFile%2FKDBX%2FUtil.pm;h=a09d2863d9177d69f902140f2159c0083bf50bc0;hp=b1795a71c111d1144995dc7e82d7769f47e018fa;hb=331a54019664704eb4a10186cb4abd7a2a722f30;hpb=c98fc7d0294e641cf8844306808333bdec4fea2f diff --git a/lib/File/KDBX/Util.pm b/lib/File/KDBX/Util.pm index b1795a7..a09d286 100644 --- a/lib/File/KDBX/Util.pm +++ b/lib/File/KDBX/Util.pm @@ -20,12 +20,12 @@ use namespace::clean -except => 'import'; our $VERSION = '999.999'; # VERSION our %EXPORT_TAGS = ( - assert => [qw(assert_64bit)], + assert => [qw(DEBUG assert assert_64bit)], 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)], - debug => [qw(dumper)], + debug => [qw(DEBUG dumper)], fork => [qw(can_fork)], function => [qw(memoize recurse_limit)], empty => [qw(empty nonempty)], @@ -33,7 +33,7 @@ our %EXPORT_TAGS = ( gzip => [qw(gzip gunzip)], io => [qw(is_readable is_writable read_all)], load => [qw(load_optional load_xs try_load_optional)], - search => [qw(query search search_limited simple_expression_query)], + search => [qw(query search simple_expression_query)], text => [qw(snakify trim)], uuid => [qw(format_uuid generate_uuid is_uuid uuid UUID_NULL)], uri => [qw(split_url uri_escape_utf8 uri_unescape_utf8)], @@ -42,6 +42,15 @@ our %EXPORT_TAGS = ( $EXPORT_TAGS{all} = [map { @$_ } values %EXPORT_TAGS]; our @EXPORT_OK = @{$EXPORT_TAGS{all}}; +BEGIN { + my $debug = $ENV{DEBUG}; + $debug = looks_like_number($debug) ? (0 + $debug) : ($debug ? 1 : 0); + *DEBUG = $debug == 1 ? sub() { 1 } : + $debug == 2 ? sub() { 2 } : + $debug == 3 ? sub() { 3 } : + $debug == 4 ? sub() { 4 } : sub() { 0 }; +} + my %OPS = ( 'eq' => 2, # binary 'ne' => 2, @@ -117,6 +126,32 @@ sub load_xs { } } +=func assert + + assert { ... }; + +Write an executable comment. Only executed if C is set in the environment. + +=cut + +sub assert(&) { ## no critic (ProhibitSubroutinePrototypes) + return if !DEBUG; + my $code = shift; + return if $code->(); + + (undef, my $file, my $line) = caller; + $file =~ s!([^/\\]+)$!$1!; + my $assertion = ''; + if (try_load_optional('B::Deparse')) { + my $deparse = B::Deparse->new(qw{-P -x9}); + $assertion = $deparse->coderef2text($code); + $assertion =~ s/^\{(?:\s*(?:package[^;]+|use[^;]+);)*\s*(.*?);\s*\}$/$1/s; + $assertion =~ s/\s+/ /gs; + $assertion = ": $assertion"; + } + die "$0: $file:$line: Assertion failed$assertion\n"; +} + =func assert_64bit assert_64bit(); @@ -590,8 +625,9 @@ sub load_optional { for my $module (@_) { eval { load $module }; if (my $err = $@) { - warn $err if $ENV{DEBUG}; - throw "Missing dependency: Please install $module to use this feature.\n", module => $module; + throw "Missing dependency: Please install $module to use this feature.\n", + module => $module, + error => $err; } } return wantarray ? @_ : $_[0]; @@ -729,33 +765,6 @@ sub search { return \@match; } -=for Pod::Coverage search_limited - -=cut - -sub search_limited { - my $list = shift; - my $query = shift; - my $limit = shift // 1; - - if (is_coderef($query) && !@_) { - # already a query - } - elsif (is_scalarref($query)) { - $query = simple_expression_query($$query, @_); - } - else { - $query = query($query, @_); - } - - my @match; - for my $item (@$list) { - push @match, $item if $query->($item); - last if $limit <= @match; - } - return \@match; -} - =func simple_expression_query $query = simple_expression_query($expression, @fields); @@ -921,7 +930,7 @@ sub try_load_optional { for my $module (@_) { eval { load $module }; if (my $err = $@) { - warn $err if $ENV{DEBUG}; + warn $err if 3 <= DEBUG; return; } }