File Coverage

blib/lib/Repl/Spec/Args/FixedArg.pm
Criterion Covered Total %
statement 27 29 93.1
branch 3 4 75.0
condition 6 14 42.8
subroutine 5 6 83.3
pod 0 3 0.0
total 41 56 73.2


line stmt bran cond sub pod time code
1             package Repl::Spec::Args::FixedArg;
2            
3 1     1   8456 use strict;
  1         3  
  1         255  
4 1     1   7 use warnings;
  1         1  
  1         40  
5 1     1   5 use Carp;
  1         2  
  1         806  
6            
7             # Parameters:
8             # - A typespec.
9             sub new
10             {
11 2     2 0 16 my $invocant = shift;
12 2   33     34 my $class = ref($invocant) || $invocant;
13 2   50     11 my $typespec = shift || die "TypeSpec expected.";
14            
15 2         4 my $self= {};
16 2         4 $self->{TYPESPEC} = $typespec;
17 2         15 $self->{SPECNAME} = sprintf("fixed: %s", $typespec->name());
18 2         8 return bless $self, $class;
19             }
20            
21             sub specname()
22             {
23 0     0 0 0 my $self = shift;
24 0         0 return $self->{SPECNAME};
25             }
26            
27             # Parameters:
28             # - An argument list (ref to array).
29             # - a position.
30             # - A context!
31             sub guard
32             {
33 9     9 0 12 my $self = shift;
34 9   50     24 my $args = shift || die "Argument list expected.";
35 9   50     20 my $pos = shift || die "Position expected.";
36 9   50     35 my $ctx = shift || die "Context expected";
37            
38 9 50 33     51 croak sprintf("The required argument at position %d, type %s missing.", $pos, $self->{SPECNAME}) if($pos < 0 || $pos >= scalar(@$args));
39 9         23 my $typespec = $self->{TYPESPEC};
40 9         9 my $result;
41 9         11 eval {$result = $typespec->guard($args->[$pos], $ctx)};
  9         40  
42 9 100       341 croak sprintf("The required argument at position %d: %s", $pos, $@) if $@;
43 8         27 return $result;
44             }
45            
46             1;