diff --git a/lib/WebGUI/Utility.pm b/lib/WebGUI/Utility.pm index b393ce415..f0b93e2aa 100644 --- a/lib/WebGUI/Utility.pm +++ b/lib/WebGUI/Utility.pm @@ -23,7 +23,7 @@ use Net::Subnets; our @ISA = qw(Exporter); our @EXPORT = qw(&isBetween &makeTabSafe &makeArrayTabSafe &randomizeHash &commify &randomizeArray &isInSubnet - &formatBytes &sortHashDescending &sortHash &isIn &makeCommaSafe &makeArrayCommaSafe &randint &round + &formatBytes &sortHashDescending &sortHash &isIn &makeCommaSafe &makeArrayCommaSafe &randint &round &scalarEquals ); @@ -376,6 +376,32 @@ sub round { #------------------------------------------------------------------- +=head2 scalarEquals ( $a, $b, .. ) + +Checks an arbitrary number of scalars for strict equality. + +Lets perl do all the work for us, via clever use of hash keys. + +Credit goes to davido on perlmonks: http://www.perlmonks.org/?node_id=525349 + +Be aware that this may actually be more strict than you want. See Utility.t +for the full list of scalars that are considered equal and not equal. + +=cut + +sub scalarEquals { + # False when < 2 defined args + return 0 if ( grep { defined($_) } @_ ) < 2; + + # Use args as hash keys, all equal if we end up with only 1 key + no warnings qw/uninitialized/; + my %gadget; + @gadget{@_} = (); + return scalar keys %gadget == 1 ? 1 : 0; +} + +#------------------------------------------------------------------- + =head2 sortHash ( hash ) Sorts a hash by its values. Returns a Tie::IxHash. You must assign this to diff --git a/t/Utility.t b/t/Utility.t index 95a764a26..5d98bc9a4 100644 --- a/t/Utility.t +++ b/t/Utility.t @@ -17,7 +17,7 @@ use Tie::IxHash; use WebGUI::Test; use WebGUI::Session; -use Test::More tests => 46; # increment this value for each test you create +use Test::More tests => 57; # increment this value for each test you create use Test::Deep; my $session = WebGUI::Test->session; @@ -125,6 +125,41 @@ is(WebGUI::Utility::round(47.6, 0), 48, 'round() - rounds up, too'); is_deeply([keys %hash3], [qw/a d b c e/], 'sortHashDescending'); } +##################################################################### +# +# scalarEquals +# +##################################################################### +{ + my %eq = ( + 0 => 0, + "0" => "0", + 0.1 => 0.1, + "0.1" => "0.1", + "0 but true" => "0 but true", + "string" => "string", + ); + while (my($a, $b) = each %eq) { + ok(WebGUI::Utility::scalarEquals($a, $b), "scalarEquals($a, $b) truthy"); + } + + my %ne = ( + 0 => "0", + "0.0" => "0", + "0.1" => "0.10", + "0" => "0 but true", + "1" => "0 but true", + 0 => "0 but true", + 1 => "0 but true", + ); + while (my($a, $b) = each %ne) { + ok(!WebGUI::Utility::scalarEquals($a, $b), "scalarEquals($a, $b) falsy"); + } + ok(!WebGUI::Utility::scalarEquals(), "scalarEquals() falsy when no args"); + ok(!WebGUI::Utility::scalarEquals(1), "falsy for 1 arg"); + ok(!WebGUI::Utility::scalarEquals(1, undef, 1), "falsy for 3 args"); +} + # isInSubnets is(WebGUI::Utility::isInSubnet('192.168.0.1', []), 0, 'isInSubnet: comparing against an empty array ref'); is(WebGUI::Utility::isInSubnet('192.168.0.1', ['192.168.0.1/32']), 1, 'isInSubnet: comparing against an exact match');