The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Data::Clean::Base - Base class for Data::Clean::*

VERSION

This document describes version 0.32 of Data::Clean::Base (from Perl distribution Data-Clean-JSON), released on 2016-03-30.

METHODS

new(%opts) => $obj

Create a new instance.

Options specify what to do with problematic data. Option keys are either reference types (like HASH, ARRAY, SCALAR) or class names (like Foo::Bar), or -obj (to match all kinds of objects, a.k.a. blessed references), -circular (to match circular references), -ref (to refer to any kind of references, used to process references not handled by other options). Option values are arrayrefs, the first element of the array is command name, to specify what to do with the reference/class. The rest are command arguments.

Note that arrayrefs and hashrefs are always walked into, so it's not trapped by -ref.

Option keys that start with ! are special: !recurse_object (bool) which can be set to true to to recurse into objects if they are hash- or array-based. By default objects are not recursed into. Note that if you enable this option, object options (like Foo::Bar or -obj) won't work for hash- and array-based objects because they will be recursed instead.

Default for %opts: -ref => 'stringify'.

Available commands:

  • ['stringify']

    This will stringify a reference like {} to something like HASH(0x135f998).

  • ['replace_with_ref']

    This will replace a reference like {} with HASH.

  • ['replace_with_str', STR]

    This will replace a reference like {} with STR.

  • ['call_method' => STR]

    This will call a method named STR and use its return as the replacement. For example: DateTime->from_epoch(epoch=>1000) when processed with [call_method => 'epoch'] will become 1000.

  • ['call_func', STR]

    This will call a function named STR with value as argument and use its return as the replacement.

  • ['one_or_zero']

    This will perform $val ? 1:0.

  • ['deref_scalar']

    This will replace a scalar reference like \1 with 1.

  • ['unbless']

    This will perform unblessing using Function::Fallback::CoreOrPP::unbless(). Should be done only for objects (-obj).

  • ['code', STR]

    This will replace with STR treated as Perl code.

  • ['clone', INT]

    This command is useful if you have circular references and want to expand/copy them. For example:

     my $def_opts = { opt1 => 'default', opt2 => 0 };
     my $users    = { alice => $def_opts, bob => $def_opts, charlie => $def_opts };

    $users contains three references to the same data structure. With the default behaviour of -circular => [replace_with_str => 'CIRCULAR'] the cleaned data structure will be:

     { alice   => { opt1 => 'default', opt2 => 0 },
       bob     => 'CIRCULAR',
       charlie => 'CIRCULAR' }

    But with -circular => ['clone'] option, the data structure will be cleaned to become (the $def_opts is cloned):

     { alice   => { opt1 => 'default', opt2 => 0 },
       bob     => { opt1 => 'default', opt2 => 0 },
       charlie => { opt1 => 'default', opt2 => 0 }, }

    The command argument specifies the number of references to clone as a limit (the default is 50), since a cyclical structure can lead to infinite cloning. Above this limit, the circular references will be replaced with a string "CIRCULAR". For example:

     my $a = [1]; push @$a, $a;

    With -circular => ['clone', 2] the data will be cleaned as:

     [1, [1, [1, "CIRCULAR"]]]

    With -circular => ['clone', 3] the data will be cleaned as:

     [1, [1, [1, [1, "CIRCULAR"]]]]

$obj->clean_in_place($data) => $cleaned

Clean $data. Modify data in-place.

$obj->clone_and_clean($data) => $cleaned

Clean $data. Clone $data first.

ENVIRONMENT

  • LOG_CLEANSER_CODE => BOOL (default: 0)

    Can be enabled if you want to see the generated cleanser code. It is logged at level trace.

  • LINENUM => BOOL (default: 1)

    When logging cleanser code, whether to give line numbers.

HOMEPAGE

Please visit the project's homepage at https://metacpan.org/release/Data-Clean-JSON.

SOURCE

Source repository is at https://github.com/sharyanto/perl-Data-Clean-JSON.

BUGS

Please report any bugs or feature requests on the bugtracker website https://rt.cpan.org/Public/Dist/Display.html?Name=Data-Clean-JSON

When submitting a bug or request, please include a test-file or a patch to an existing test-file that illustrates the bug or desired feature.

AUTHOR

perlancar <perlancar@cpan.org>

COPYRIGHT AND LICENSE

This software is copyright (c) 2016 by perlancar@cpan.org.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.