/usr/share/perl5/NetSDS/Util/Struct.pm is in libnetsds-util-perl 1.044-2.
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 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 | package NetSDS::Util::Struct;
########################################################################
# Misc Struct routines
#
########################################################################
=head1 NAME
NetSDS::Util::Struct - data structure convertors
=head1 SYNOPSIS
use NetSDS::Util::Struct;
...
my $str = dump_to_row($some_structure);
=head1 DESCRIPTION
NetSDS::Util::Struct module contains different utilities for data structures processing.
=cut
use 5.8.0;
use warnings 'all';
use strict;
use base 'Exporter';
use version; our $VERSION = "1.044";
our @EXPORT = qw(
dump_to_string
dump_to_row
arrays_to_hash
to_array
merge_hash
);
use Scalar::Util qw(
blessed
reftype
);
#***********************************************************************
=head1 EXPORTED METHODS
=over
=item B<dump_to_string(...)>
Returns cleaned dump to scalar.
=cut
#-----------------------------------------------------------------------
sub dump_to_string {
my $dmp = Data::Dumper->new( ( scalar(@_) > 1 ) ? [ \@_ ] : \@_, ['DUMP'] );
$dmp->Terse(0);
$dmp->Deepcopy(0);
$dmp->Sortkeys(1);
$dmp->Quotekeys(0);
$dmp->Indent(1);
$dmp->Pair(': ');
$dmp->Bless('obj');
return $dmp->Dump();
}
#***********************************************************************
=item B<dump_to_row(...)>
Returns cleaned dump to scalar.
=cut
#-----------------------------------------------------------------------
sub dump_to_row {
my $str = dump_to_string(@_);
if ( $str =~ s/^\s*\$DUMP\s+=\s+[{\[]\s+//s ) {
$str =~ s/\s+[}\]];\s+$//s;
} else {
$str =~ s/^\s*\$DUMP\s+=\s+//s;
$str =~ s/\s;\s+$//s;
}
$str =~ s/\$DUMP/\$/g;
$str =~ s/\s+/ /g;
$str =~ s/\\'/'/g;
$str =~ s/\\undef/undef/g;
$str =~ s/\\(\d)/$1/g;
return $str;
}
#***********************************************************************
=item B<to_array($data)>
=cut
#-----------------------------------------------------------------------
sub to_array {
my ($data) = @_;
if ( is_ref_array($data) ) {
return $data;
} elsif ( is_ref_hash($data) ) {
return [ keys %{$data} ];
} elsif ( defined($data) ) {
return [$data];
} else {
return $data;
}
}
#***********************************************************************
=item B<arrays_to_hash($keys_ref, $values_ref)> - translate arrays to hash
Parameters: references to keys array and values array
Return: hash
If @$keys_ref is longer than @$values_ref - rest of keys filled with
C<undef> values.
If @$keys_ref is shorter than @$values_ref - rest of values are discarded.
If any of parameters isn't array reference then C<undef> will return.
Example:
my %h = array2hash(['fruit','animal'], ['apple','horse']);
Result should be a hash:
(
fruit => 'apple',
animal => 'horse'
)
=cut
#-----------------------------------------------------------------------
sub arrays_to_hash {
my ( $keys_ref, $values_ref ) = @_;
return undef unless ( is_ref_array($keys_ref) and is_ref_array($values_ref) );
my %h = ();
for ( my $i = 0 ; $i < scalar(@$keys_ref) ; $i++ ) {
$h{ $keys_ref->[$i] } = defined( $values_ref->[$i] ) ? $values_ref->[$i] : undef;
}
return %h;
}
#***********************************************************************
=item B<merge_hash($target, $source)> - merge two hashes
Parameters: references to target and source hashes.
This method adds source hash to target one and return value as a result.
=cut
#-----------------------------------------------------------------------
sub merge_hash {
my ( $trg, $src ) = @_;
while ( my ( $key, $val ) = each( %{$src} ) ) {
if ( is_ref_hash($val) and is_ref_hash( $trg->{$key} ) ) {
merge_hash( $trg->{$key}, $val );
} else {
$trg->{$key} = $val;
}
}
return $trg;
}
#**************************************************************************
1;
__END__
=back
=head1 EXAMPLES
None
=head1 BUGS
Unknown yet
=head1 TODO
None
=head1 SEE ALSO
None
=head1 AUTHORS
Valentyn Solomko <pere@pere.org.ua>
=cut
|