File Coverage

blib/lib/Petal/Utils/If.pm
Criterion Covered Total %
statement 27 28 96.4
branch 6 10 60.0
condition 1 3 33.3
subroutine 7 7 100.0
pod 0 1 0.0
total 41 49 83.6


line stmt bran cond sub pod time code
1             package Petal::Utils::If;
2              
3 4     4   21 use strict;
  4         43  
  4         141  
4 4     4   19 use warnings::register;
  4         8  
  4         442  
5              
6 4     4   20 use Carp;
  4         9  
  4         271  
7              
8 4     4   21 use base qw( Petal::Utils::Base );
  4         9  
  4         345  
9              
10 4     4   22 use constant name => 'if';
  4         6  
  4         232  
11 4     4   19 use constant aliases => qw();
  4         9  
  4         1205  
12              
13             our $VERSION = ((require Petal::Utils), $Petal::Utils::VERSION)[1];
14             our $REVISION = (split(/ /, ' $Revision: 1.2 $ '))[2];
15              
16             sub process {
17 2     2 0 234 my $class = shift;
18 2         4 my $hash = shift;
19 2   33     6 my $args = shift || confess( "'if' expects args of the form 'if: ... then: ... [else: ...]' (got nothing)!" );
20              
21 2         19 my @args = $args =~ /\A(.+?)\sthen:\s+(.+?)(?:\s+else:\s+(.+?))?\z/;
22 2 50       6 confess( "'if' expects arguments of the form: 'if: ... then: ... [else: ...]', not 'if: $args'!" ) unless @args;
23 2 50       7 $args[0] || confess( "1st arg to 'if' should be an expression (got nothing)!" );
24 2 50       4 $args[1] || confess( "2nd arg to 'if' (after then:) should be an expression (got nothing)!" );
25              
26 2 100       8 return $hash->fetch($args[1]) if $hash->fetch($args[0]);
27 1 50       75 return $hash->fetch($args[2]) if $args[2];
28 0           return '';
29             }
30              
31             1;