| 1 | package Scalar::Compare; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use base 'Exporter'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 1 | our $VERSION = '0.01'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 1 | our @EXPORT_OK = qw(scalar_compare); | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | Scalar::Compare - Dynamically use comparison operators | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | my $scalar1 = 'waffle'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | my $scalar2 = 'pirate'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | my $operator = 'ne'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | if (scalar_compare($scalar1, $operator, $scalar2)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | warn "Proceed normally."; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | Simple syntax sugar around Perl's comparison operators that allows you to arbitrarily compare scalars using Perl's comparison operators. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | This is useful, for instance, if you want to support arbitrary comparison criteria in your application without lines and lines of if/else blocks. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | =head1 FUNCTIONS | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | =head2 scalar_compare | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | Given a scalar, an operator, and another scalar, returns the result of that operator on the provided values. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | For instance: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | my $ret = scalar_compare($s1, '==', $s2) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | is equivalent to: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | my $ret = $s1 == $s2; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | sub scalar_compare | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | 28 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
scalar_compare: called CC=14 SCAR=28.6
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | 28 | my ($our_value, $operator, $target_value) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 28 | ▶ | if ($operator eq '==') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 12 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | 4 | return $our_value == $target_value; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | } elsif ($operator eq 'eq') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | 3 | return $our_value eq $target_value; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | } elsif ($operator eq '!=') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | 2 | return $our_value != $target_value; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | } elsif ($operator eq 'ne') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | 2 | return $our_value ne $target_value; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | } elsif ($operator eq '<') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | 3 | return $our_value < $target_value; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | } elsif ($operator eq '<=') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | 3 | return $our_value <= $target_value; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | } elsif ($operator eq '>') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 3 | return $our_value > $target_value; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | } elsif ($operator eq '>=') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | 3 | return $our_value >= $target_value; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | } elsif ($operator eq '=~') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 3 | return $our_value =~ /$target_value/; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | } elsif ($operator eq '!~') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 2 | return $our_value !~ /$target_value/; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | } elsif ($operator eq '<=>') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | 0 | return $our_value <=> $target_value; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | } elsif ($operator eq 'cmp') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | 0 | return $our_value cmp $target_value; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | 0 | ▶ | $operator ||= 'undef'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$operator ||= "undef"
MC/DC0%0/1
$operator ||= "undef"
$operator
Truth table0%0/2
A
A= $operator
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | 0 | die "Unknown comparison operator '$operator'"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | =head1 AUTHORS | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | Bizowie <http://bizowie.com> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | Copyright (C) 2013 Bizowie | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||