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