NAME

Scalar::In - replacement for smartmatch

VERSION

0.001

SYNOPSIS

use Scalar::In;                                   # imports string_in
use Scalar::In 'numeric_in';                      # imports numeric_in
use Scalar::In string_in  => { -as => 'in' };     # imports in
use Scalar::In numeric_in => { -as => 'num_in' }; # imports num_in

EXAMPLE

Inside of this Distribution is a directory named example. Run this *.pl files.

DESCRIPTION

This module was written because the smartmatch operator ~~ was deprecated as experimental.

That module implements some "in" subroutines with smartmatch similar behaviour.

First I tried to delete the obsolete looking numeric_in. In tests I realized that objects with overloaded + working there but string_in expects objects with overloaded "". So there are some special cases for numeric_in. Because of such minimal cases numeric_in is not exported as default.

SUBROUTINES/METHODS

subroutine string_in

"any1" or "any2" can contain 0 or more values. The first string match of "any1" and "any2" will return true. Also the frist match of undef in "any1" and "any2" will return true. All other will return false. In case of a hash or hash reference the keys are used.

$boolean = string_in( [$@%]any1, [$@%]any2 );

Allowed values for $any1:

undef, $string, $numeric, $object, $array_ref, $hash_ref

Allowed values for @any1 or if $any1 is an array reference:

$string, $numeric, $object

Allowed values for $any2:

undef, $string, $numeric, $object, $array_ref, $hash_ref, $regex, $code_ref

Allowed values for @any2 or if $any2 is an array reference:

$string, $numeric, $object, $regex, $code_ref

All given values will be used as string if they are defined.

some examples

true if $string is undef

$boolean = string_in( $string, undef );

true if $string is eq 'string'

$boolean = string_in( $string, 'string' );

true if $string contains abc or def

$boolean = string_in( $string, qr{ abc | def }xms );

true if $string begins with abc

$boolean = string_in(
    $string,
    sub {
        my $str = shift;
        return 0 == index $str, 'abc';
    },
);

true if $object overloads "" and that is eq 'string'. Objects in the 2nd parameter should also overload "".

$boolean = string_in( $object, 'string' );

true if any key in the hash or hash reference will match

$boolean = string_in( $string, $hash_ref );
$boolean = string_in( $string, %hash );

subroutine numeric_in

A given value will be used as numeric if it is defined. Maybe that thows a numeric warning if a string looks not like numeric. The difference to subroutine string_in is, that here is operator == used instead of operator eq.

$boolean = numeric_in( $numeric, undef );
$boolean = numeric_in( $numeric, 123 );
$boolean = numeric_in( $numeric, qr{ 123 | 456 }xms );
$boolean = numeric_in( $numeric, $array_ref );
$boolean = numeric_in( $numeric, @array );
$boolean = numeric_in( $numeric, $hash_ref );
$boolean = numeric_in( $numeric, %hash );

true if $numeric > 1

$boolean = numeric_in(
    $numeric,
    sub {
        my $num = shift;
        return $num > 1;
    },
);

true if $object overloads + and that is == 123.

$boolean = numeric_in( $object, 123 );

Objects that overload + also allowed as 2nd parameter or in a array or array reference.

DIAGNOSTICS

none

CONFIGURATION AND ENVIRONMENT

nothing

DEPENDENCIES

Sub::Exporter

INCOMPATIBILITIES

nothing

BUGS AND LIMITATIONS

nothing

SEE ALSO

smartmatch operator ~~

AUTHOR

Steffen Winkler

LICENSE AND COPYRIGHT

Copyright (c) 2013, Steffen Winkler <steffenw at cpan.org>. All rights reserved.

This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.