use strict; use warnings; use feature 'state', 'say'; use Test::More; use JSON; sub calc_rev { state %price_by_qty = ( 1 => 15, 3 => 10, 6 => 8, 10 => 5, ); my $qty = shift; return 0 if ! $qty; for my $level ( sort { $b <=> $a } keys %price_by_qty ) { return $qty * $price_by_qty{ $level } if $qty >= $level; } } # # mock getting an aref of rows for this test file my $json = do { local $/; }; my $rows = decode_json( $json ); # my $revenue = 0; for my $row ( @{ $rows } ) { $revenue += calc_rev($row->{total}); } is( $revenue, 123, 'Revenue is 123. Mwahahahaa!'); done_testing; __DATA__ [ {"total":3}, {"total":6}, {"total":1}, {"total":2} ]