Subject: | Filter output test is wrong |
Despite of what's stated in documentation (test for defined), filter
output is tested just to be true. That leads to some weird cases, like
%cat Data/Printer/Filter/Z.pm
package Data::Printer::Filter::Z;
use Data::Printer::Filter;
filter 'SCALAR', sub {
my ($ref, $properties) = @_;
my $val = $$ref;
return $val + 1;
};
1;%
perl -e 'use Data::Printer {filters => {-external => [ 'Z' ]}}; $a=[-2,
-1, 0]; p $a'
\ [
[0] -1,
[1] ,
[2] 1
]
Furthermore, defining a filter that overrides default type
(SCALAR/HASH/ARRAY) doesn't allow default fallback when filter doesn't
return anything (contrary to class filters call). Patch for the 1st is
attached, but 2nd - i prefer it to be corrected too, but don't know what
should be filter expected behavior.
Subject: | data-printer.filter-def.diff |
--- lib/Data/Printer.pm
+++ lib/Data/Printer.pm
@@ -248,7 +248,7 @@
# filter item (if user set a filter for it)
if ( exists $p->{filters}->{$ref} ) {
foreach my $filter ( @{ $p->{filters}->{$ref} } ) {
- if ( my $result = $filter->($item, $p) ) {
+ if ( defined(my $result = $filter->($item, $p)) ) {
$string .= $result;
last;
}
@@ -456,7 +456,7 @@
my $visited = 0;
if ( exists $p->{filters}->{'-class'} ) {
foreach my $filter ( @{ $p->{filters}->{'-class'} } ) {
- if ( my $result = $filter->($item, $p) ) {
+ if ( defined(my $result = $filter->($item, $p)) ) {
$string .= $result;
$visited = 1;
last;