line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Option::Option; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
=pod |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
=head1 NAME |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
Option::Option |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
=head1 SYNOPSIS |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
Provides objects that can hold results that can be unwrapped |
12
|
|
|
|
|
|
|
similar to Rust |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
use Option::Option; |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
my $option = Option::Option->new("something"); |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
# This croaks: |
19
|
|
|
|
|
|
|
print $var; |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
# This works |
22
|
|
|
|
|
|
|
my $var = $option->unwrap(); |
23
|
|
|
|
|
|
|
print $var; |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
# This also works and has a helpful error message |
26
|
|
|
|
|
|
|
my $var = $option->expect("get my something"); |
27
|
|
|
|
|
|
|
print $var; |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
=head1 AUTHOR |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
Lee Katz |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
=head1 METHODS |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
=cut |
36
|
|
|
|
|
|
|
|
37
|
1
|
|
|
1
|
|
1558
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
54
|
|
38
|
1
|
|
|
1
|
|
9
|
use warnings; |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
54
|
|
39
|
1
|
|
|
1
|
|
9
|
use Carp qw/confess croak/; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
111
|
|
40
|
1
|
|
|
1
|
|
26
|
use overload '""' => 'toString'; |
|
1
|
|
|
|
|
6
|
|
|
1
|
|
|
|
|
20
|
|
41
|
|
|
|
|
|
|
|
42
|
1
|
|
|
1
|
|
125
|
use Exporter qw/import/; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
76
|
|
43
|
|
|
|
|
|
|
|
44
|
1
|
|
|
1
|
|
802
|
use version; |
|
1
|
|
|
|
|
3025
|
|
|
1
|
|
|
|
|
5
|
|
45
|
|
|
|
|
|
|
our $VERSION = version->declare("0.1"); |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
=pod |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
=over |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
=item new() |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
Creates a new object with a variable |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
=cut |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
sub new{ |
58
|
13
|
|
|
13
|
1
|
7078
|
my($class, $var) = @_; |
59
|
|
|
|
|
|
|
|
60
|
13
|
|
|
|
|
30
|
my $self = { |
61
|
|
|
|
|
|
|
var => $var, |
62
|
|
|
|
|
|
|
}; |
63
|
|
|
|
|
|
|
|
64
|
13
|
|
|
|
|
21
|
bless($self, $class); |
65
|
|
|
|
|
|
|
|
66
|
13
|
|
|
|
|
22
|
return $self; |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
=pod |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
=item unwrap() |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
Checks if the variable is defined and if it is, returns it. |
74
|
|
|
|
|
|
|
If not defined, croaks. |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
=back |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
=cut |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
sub unwrap{ |
81
|
13
|
|
|
13
|
1
|
33
|
my($self) = @_; |
82
|
|
|
|
|
|
|
|
83
|
13
|
50
|
|
|
|
49
|
if(!defined($$self{var})){ |
84
|
0
|
|
|
|
|
0
|
croak("Variable not defined"); |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
|
87
|
13
|
|
|
|
|
21
|
return $$self{var}; |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
=pod |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
=over |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
=item expect($msg) |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
Checks if the variable is defined and if it is, returns it. |
97
|
|
|
|
|
|
|
If not defined, croaks with error message. |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
=back |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
=cut |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
sub expect{ |
104
|
0
|
|
|
0
|
1
|
|
my($self, $msg) = @_; |
105
|
|
|
|
|
|
|
|
106
|
0
|
0
|
|
|
|
|
if(!defined($$self{var})){ |
107
|
0
|
|
|
|
|
|
croak("Variable not defined: $msg"); |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
|
110
|
0
|
|
|
|
|
|
return $$self{var}; |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
=pod |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
=over |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
=item toString() |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
Dies with an error message, describing that the object was attempted to be used |
120
|
|
|
|
|
|
|
in a scalar context without unwrapping. |
121
|
|
|
|
|
|
|
This subroutine is not meant to be used directly. |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
my $var = Option::Option->new("something"); |
124
|
|
|
|
|
|
|
my $concat = $var . " wicked this way comes"; |
125
|
|
|
|
|
|
|
# dies with error message during the concatenation |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
=back |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
=cut |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
sub toString{ |
132
|
0
|
|
|
0
|
1
|
|
my($self) = @_; |
133
|
|
|
|
|
|
|
|
134
|
0
|
|
|
|
|
|
my $var = $$self{var}; |
135
|
0
|
0
|
|
|
|
|
if(!defined($var)){ |
136
|
0
|
|
|
|
|
|
$var = "UNDEFINED"; |
137
|
|
|
|
|
|
|
} |
138
|
0
|
0
|
|
|
|
|
if(length($var) > 30){ |
139
|
0
|
|
|
|
|
|
$var = substr($var, 0, 27)."..."; |
140
|
|
|
|
|
|
|
} |
141
|
|
|
|
|
|
|
|
142
|
0
|
|
|
|
|
|
confess("Attempt to use a ".ref($self)." object whose value is `$var`"); |
143
|
|
|
|
|
|
|
} |
144
|
|
|
|
|
|
|
1; |
145
|
|
|
|
|
|
|
|