Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Code review

by BillKSmith (Monsignor)
on Mar 07, 2024 at 19:46 UTC ( [id://11158113]=note: print w/replies, xml ) Need Help??


in reply to Code review

I propose replacing the three lines of the OP (shown in sub OP) with the three lines (shown in sub TEST). The test code around them demonstrates that they produce the same result in each of the three cases of interest. The new code is about the same length and probably less efficient, but I find it much easier to understand.
use strict; use warnings; use Test::More; my $y = 5; my %Y; my @cases = ( [[], 'no @ARGs'], # 1 [[1,2,3], 'no matches'], # undef [[5,2,5], '2 matches'], # 2 ); plan tests=>3; foreach my $case (@cases) { @ARGV = @{$case->[0]}; %Y = (); my $Y_OP = OP(); @ARGV = @{$case->[0]}; %Y = (); my $Y_TEST = TEST(); is($Y_TEST, $Y_OP, $case->[1]); } sub OP { if(@ARGV) { foreach (@ARGV) { $Y{$y}++,next if $y eq $_; } } else { $Y{$y}++ } return $Y{$y}; } sub TEST{ my $number_of_matches = grep { $_ eq $y } @ARGV; $number_of_matches = undef if $number_of_matches == 0; $Y{$y} = (@ARGV != 0) ? $number_of_matches : 1; return $Y{$y}; }

OUTPUT:

1..3 ok 1 - no @ARGs ok 2 - no matches ok 3 - 2 matches
Bill

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11158113]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (5)
As of 2026-04-21 13:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.