/usr/share/perl5/SHARYANTO/Data/Util.pm is in libsharyanto-utils-perl 0.53-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 | package SHARYANTO::Data::Util;
use 5.010;
use strict;
use warnings;
#use experimental 'smartmatch';
require Exporter;
our @ISA = qw(Exporter);
our @EXPORT_OK = qw(clone_circular_refs);
our $VERSION = '0.53'; # VERSION
our %SPEC;
$SPEC{clone_circular_refs} = {
v => 1.1,
summary => 'Remove circular references by deep-copying them',
description => <<'_',
For example, this data:
$x = [1];
$data = [$x, 2, $x];
contains circular references by referring to `$x` twice. After
`clone_circular_refs`, data will become:
$data = [$x, 2, [1]];
that is, the subsequent circular references will be deep-copied. This makes it
safe to transport to JSON, for example.
Sometimes it doesn't work, for example:
$data = [1];
push @$data, $data;
Cloning will still create circular references.
This function modifies the data structure in-place, and return true for success
and false upon failure.
_
args_as => 'array',
args => {
data => {
schema => "any",
pos => 0,
req => 1,
},
},
result_naked => 1,
};
sub clone_circular_refs {
require Data::Structure::Util;
require Data::Clone;
my ($data) = @_;
my %refs;
my $doit;
$doit = sub {
my $x = shift;
my $r = ref($x);
return if !$r;
if ($r eq 'ARRAY') {
for (@$x) {
next unless ref($_);
if ($refs{"$_"}++) {
$_ = Data::Clone::clone($_);
} else {
$doit->($_);
}
}
} elsif ($r eq 'HASH') {
for (keys %$x) {
next unless ref($x->{$_});
if ($refs{"$x->{$_}"}++) {
$x->{$_} = Data::Clone::clone($x->{$_});
} else {
$doit->($_);
}
}
}
};
$doit->($data);
!Data::Structure::Util::has_circular_ref($data);
}
1;
# ABSTRACT: Data utilities
__END__
=pod
=encoding utf-8
=head1 NAME
SHARYANTO::Data::Util - Data utilities
=head1 VERSION
version 0.53
=head1 SYNOPSIS
=head1 DESCRIPTION
=head1 FUNCTIONS
None are exported by default, but they are exportable.
None are exported by default, but they are exportable.
=head2 clone_circular_refs(@args) -> any
For example, this data:
$x = [1];
$data = [$x, 2, $x];
contains circular references by referring to C<$x> twice. After
C<clone_circular_refs>, data will become:
$data = [$x, 2, [1]];
that is, the subsequent circular references will be deep-copied. This makes it
safe to transport to JSON, for example.
Sometimes it doesn't work, for example:
$data = [1];
push @$data, $data;
Cloning will still create circular references.
This function modifies the data structure in-place, and return true for success
and false upon failure.
Arguments ('*' denotes required arguments):
=over 4
=item * B<data>* => I<any>
=back
Return value:
=head1 SEE ALSO
To check for circular references, try C<has_circular_ref> from
L<Data::Structure::Util>. There is also L<Devel::Cycle> albeit far slower.
=head1 AUTHOR
Steven Haryanto <stevenharyanto@gmail.com>
=head1 COPYRIGHT AND LICENSE
This software is copyright (c) 2013 by Steven Haryanto.
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
=cut
|