perlancar

NAME

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

VERSION

This document describes version 0.24 of Data::Clean::Base (from Perl distribution Data-Clean-JSON), released on 2015-03-26.

METHODS

new(%opts) => $obj

Create a new instance.

Options specify what to do with problematic data. Option keys are either reference types or class names, or -obj (to refer to objects, a.k.a. blessed references), -circular (to refer to circular references), -ref (to refer to 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.

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']

    This will call a method 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', STR]

    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/perlancar/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) 2015 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.