/usr/share/perl5/Data/Miscellany.pm is in libdata-miscellany-perl 1.100850-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 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 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 | use 5.008;
use strict;
use warnings;
package Data::Miscellany;
our $VERSION = '1.100850';
# ABSTRACT: Collection of miscellaneous subroutines
use Exporter qw(import);
our %EXPORT_TAGS = (
util => [
qw/
set_push flex_grep flatten is_deeply eq_array eq_hash
is_defined value_of str_value_of class_map trim
/
],
);
our @EXPORT_OK = @{ $EXPORT_TAGS{all} = [ map { @$_ } values %EXPORT_TAGS ] };
# Like push, but only pushes the item(s) onto the list indicated by the list
# ref (first param) if the list doesn't already contain it.
# Originally, I used Storable::freeze to see whether two structures where the
# same, but this didn't work for all cases, so I switched to is_deeply().
sub set_push (\@@) {
my ($list, @items) = @_;
ITEM:
for my $item (@items) {
for my $el (@$list) {
next ITEM if is_deeply($item, $el);
}
push @$list, $item;
}
}
sub flatten {
ref $_[0] eq 'ARRAY' ? @{ $_[0] }
: defined $_[0] ? @_
: ();
}
# Start of code adapted from Test::More
#
# In set_push and other places within the framework, we need to compare
# structures deeply, so here are the relevant methods copied from Test::More
# with the test-specific code removed.
sub is_deeply {
my ($this, $that) = @_;
return _deep_check($this, $that) if ref $this && ref $that;
return $this eq $that if defined $this && defined $that;
# undef only matches undef and nothing else
return !defined $this && !defined $that;
}
sub _deep_check {
my ($e1, $e2) = @_;
# Quiet uninitialized value warnings when comparing undefs.
local $^W = 0;
return 1 if $e1 eq $e2;
return eq_array($e1, $e2)
if UNIVERSAL::isa($e1, 'ARRAY') && UNIVERSAL::isa($e2, 'ARRAY');
return eq_hash($e1, $e2)
if UNIVERSAL::isa($e1, 'HASH') && UNIVERSAL::isa($e2, 'HASH');
return _deep_check($$e1, $$e2)
if UNIVERSAL::isa($e1, 'REF') && UNIVERSAL::isa($e2, 'REF');
return _deep_check($$e1, $$e2)
if UNIVERSAL::isa($e1, 'SCALAR') && UNIVERSAL::isa($e2, 'SCALAR');
return 0;
}
sub eq_array {
my ($a1, $a2) = @_;
return 1 if $a1 eq $a2;
return 0 unless $#$a1 == $#$a2;
for (0 .. $#$a1) {
return 0 unless is_deeply($a1->[$_], $a2->[$_]);
}
return 1;
}
sub eq_hash {
my ($a1, $a2) = @_;
return 1 if $a1 eq $a2;
return 0 unless keys %$a1 == keys %$a2;
foreach my $k (keys %$a1) {
return 0 unless exists $a2->{$k};
return 0 unless is_deeply($a1->{$k}, $a2->{$k});
}
return 1;
}
# End of code adapted from Test::More
# Handle value objects as well as normal scalars
sub is_defined ($) {
my $value = shift;
# restrict the method call to objects of type Class::Value, because we
# want to avoid deep recursion that could happen if is_defined() is
# imported into a package and then someone else calls is_defined() on an
# object of that package.
ref($value)
&& UNIVERSAL::isa($value, 'Class::Value')
&& UNIVERSAL::can($value, 'is_defined')
? $value->is_defined
: defined($value);
}
sub value_of ($) {
my $value = shift;
# Explicitly return undef unless the value is_defined, because it could
# still be a value object, in which case the value we want isn't the value
# object itself, but 'undef'
is_defined $value ? "$value" : undef;
}
sub str_value_of ($) {
my $value = shift;
is_defined $value ? "$value" : '';
}
sub flex_grep {
my $wanted = shift;
return grep { $_ eq $wanted }
map { flatten($_) } @_;
}
sub class_map {
my ($class, $map, $seen) = @_;
# circularities
$seen ||= {};
# so we can pass an object as well as a class name:
$class = ref $class if ref $class;
return if $seen->{$class}++;
my $val = $map->{$class};
return $val if defined $val;
# If there's no direct mapping for an exception class, check its
# superclasses. Assumes that the classes are loaded, of course.
no strict 'refs';
for my $super (@{"$class\::ISA"}) {
my $found = class_map($super, $map, $seen);
# we will return UNIVERSAL if everything fails - so skip it.
return $found if defined $found && $found ne $map->{UNIVERSAL};
}
return $map->{UNIVERSAL};
}
sub trim {
my $s = shift;
$s =~ s/^\s+//;
$s =~ s/\s+$//;
$s;
}
1;
__END__
=pod
=head1 NAME
Data::Miscellany - Collection of miscellaneous subroutines
=head1 VERSION
version 1.100850
=head1 SYNOPSIS
use Data::Miscellany qw/set_push flex_grep/;
my @foo = (1, 2, 3, 4);
set_push @foo, 3, 1, 5, 1, 6;
# @foo is now (1, 2, 3, 4, 5, 6);
flex_grep('foo', [ qw/foo bar baz/ ]); # true
flex_grep('foo', [ qw/bar baz flurble/ ]); # false
flex_grep('foo', 1..4, 'flurble', [ qw/foo bar baz/ ]); # true
flex_grep('foo', 1..4, [ [ 'foo' ] ], [ qw/bar baz/ ]); # false
=head1 DESCRIPTION
This is a collection of miscellaneous subroutines useful in wide but varying
scenarios; a catch-all module for things that don't obviously belong anywhere
else. Obviously what's useful differs from person to person, but this
particular collection should be useful in object-oriented frameworks, such as
L<Class::Scaffold> and L<Data::Conveyor>.
=head1 FUNCTIONS
=head2 set_push(ARRAY, LIST)
Like C<push()>, but only pushes the item(s) onto the list indicated by the
list or list ref (the first argument) if the list doesn't already contain it.
Example:
@foo = (1, 2, 3, 4);
set_push @foo, 3, 1, 5, 1, 6;
# @foo is now (1, 2, 3, 4, 5, 6)
=head2 flatten()
If the first argument is an array reference, it returns the dereferenced
array. If the first argument is undefined (or there are no arguments), it
returns the empty list. Otherwise the argument list is returned as is.
=head2 flex_grep(SCALAR, LIST)
Like C<grep()>, but compares the first argument to each flattened (see
C<flatten()>) version of each element of the list.
Examples:
flex_grep('foo', [ qw/foo bar baz/ ]) # true
flex_grep('foo', [ qw/bar baz flurble/ ]) # false
flex_grep('foo', 1..4, 'flurble', [ qw/foo bar baz/ ]) # true
flex_grep('foo', 1..4, [ [ 'foo' ] ], [ qw/bar baz/ ]) # false
=head2 is_deeply()
Like L<Test::More>'s C<is_deeply()> except that this version respects
stringification overloads. If a package overloads stringification, it means
that it specifies how it wants to be compared. Recent versions of
L<Test::More> break this behaviour, so here is a working version of
C<is_deeply()>. This subroutine only does the comparison; there are no test
diagnostics or results recorded or printed anywhere.
=head2 eq_array()
Like L<Test::More>'s C<eq_array()> except that this version respects
stringification overloads. If a package overloads stringification, it means
that it specifies how it wants to be compared. Recent versions of
L<Test::More> break this behaviour, so here is a working version of
C<eq_array()>. This subroutine only does the comparison; there are no test
diagnostics or results recorded or printed anywhere.
=head2 eq_hash()
Like L<Test::More>'s C<eq_hash()> except that this version respects
stringification overloads. If a package overloads stringification, it means
that it specifies how it wants to be compared. Recent versions of
L<Test::More> break this behaviour, so here is a working version of
C<eq_hash()>. This subroutine only does the comparison; there are no test
diagnostics or results recorded or printed anywhere.
=head2 is_defined(SCALAR)
A kind of C<defined()> that is aware of L<Class::Value>, which has its own
views of what is a defined value and what isn't. The issue arose since
L<Class::Value> objects are supposed to be used transparently, mixed with
normal scalar values. However, it is not possible to overload "definedness",
and C<defined()> used on a value object always returns true since the
object reference certainly exists. However, what we want to know is
whether the value encapsulated by the value object is defined.
Additionally, each value class can have its own ideas of when its
encapsulated value is defined. Therefore, L<Class::Value> has an
C<is_defined()> method.
This subroutine checks whether its argument is a value object and if so, calls
the value object's C<is_defined()> method. Otherwise, the normal C<defined()>
is used.
=head2 value_of(SCALAR)
Stringifies its argument, but returns undefined values (per C<is_defined()>)
as C<undef>.
=head2 str_value_of(SCALAR)
Stringifies its argument, but returns undefined values (per C<is_defined()>)
as the empty string.
=head2 class_map(SCALAR, HASH)
Takes an object or class name as the first argument (if it's an object, the
class name used will be the package name the object is blessed into).
Takes a hash whose keys are class names as the second argument. The hash
values are completely arbitrary.
Looks up the given class name in the hash and returns the corresponding value.
If no such hash key is found, the class hierarchy for the given class name is
traversed depth-first and checked against the hash keys in turn. The first
value found is returned.
If no key is found, a special key, C<UNIVERSAL> is used.
As an example of how this might be used, consider a hierarchy of exception
classes. When evaluating each exception, we want to know how severe this
exception is, so we define constants for C<RC_OK> (meaning it's informational
only), C<RC_ERROR> (meaning some sort of action should be taken) and
C<RC_INTERNAL_ERROR> (meaning something has gone badly wrong and we might halt
processing). In the following table assume that if you have names like
C<Foo::Bar> and C<Foo::Bar::Baz>, then the latter subclasses the former.
%map = (
'UNIVERSAL' => RC_INTERNAL_ERROR,
'My::Exception::Business' => RC_ERROR,
'My::Exception::Internal' => RC_INTERNAL_ERROR,
'My::Exception::Business::ValueNormalized' => RC_OK,
);
Assuming that C<My::Exception::Business::IllegalValue> exists and that it
subclasses C<My::Exception::Business>, here are some outcomes:
class_map('My::Exception::Business::IllegalValue', \%map) # RC_ERROR
class_map('My::Exception::Business::ValueNormalzed', \%map) # RC_OK
=head2 trim(STRING)
Trims off whitespace at the beginning and end of the string and returns the
trimmed string.
=head1 INSTALLATION
See perlmodinstall for information and options on installing Perl modules.
=head1 BUGS AND LIMITATIONS
No bugs have been reported.
Please report any bugs or feature requests through the web interface at
L<http://rt.cpan.org/Public/Dist/Display.html?Name=Data-Miscellany>.
=head1 AVAILABILITY
The latest version of this module is available from the Comprehensive Perl
Archive Network (CPAN). Visit L<http://www.perl.com/CPAN/> to find a CPAN
site near you, or see
L<http://search.cpan.org/dist/Data-Miscellany/>.
The development version lives at
L<http://github.com/hanekomu/Data-Miscellany/>.
Instead of sending patches, please fork this project using the standard git
and github infrastructure.
=head1 AUTHOR
Marcel Gruenauer <marcel@cpan.org>
=head1 COPYRIGHT AND LICENSE
This software is copyright (c) 2004 by Marcel Gruenauer.
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
|