Data-Miscellany

 view release on metacpan or  search on metacpan

lib/Data/Miscellany.pm  view on Meta::CPAN

=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



( run in 2.037 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )