| 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 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 38 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 39 | 1 | ▶ | use Carp qw/confess croak/; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 40 | 1 | ▶ | use overload '""' => 'toString'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 41 | |||||||||||
| 42 | 1 | ▶ | use Exporter qw/import/; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 43 | |||||||||||
| 44 | 1 | ▶ | use version; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 45 | 1 | 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 | 13 | ▶ | sub new{ | ||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 58 | 13 | my($class, $var) = @_; | |||||||||
| 59 | |||||||||||
| 60 | 13 | my $self = { | |||||||||
| 61 | var => $var, | ||||||||||
| 62 | }; | ||||||||||
| 63 | |||||||||||
| 64 | 13 | bless($self, $class); | |||||||||
| 65 | |||||||||||
| 66 | 13 | 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 | 13 | ▶ | sub unwrap{ | ||||||||
|
Subroutine
unwrap: called CC=2 SCAR=7.6
Pod
documented
| |||||||||||
| 81 | 13 | my($self) = @_; | |||||||||
| 82 | |||||||||||
| 83 | 13 | ▶ | if(!defined($$self{var})){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 84 | 0 | croak("Variable not defined"); | |||||||||
| 85 | } | ||||||||||
| 86 | |||||||||||
| 87 | 13 | 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 | 0 | ▶ | sub expect{ | ||||||||
|
Subroutine
expect: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 104 | 0 | my($self, $msg) = @_; | |||||||||
| 105 | |||||||||||
| 106 | 0 | ▶ | if(!defined($$self{var})){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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 | 0 | ▶ | sub toString{ | ||||||||
|
Subroutine
toString: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||
| 132 | 0 | my($self) = @_; | |||||||||
| 133 | |||||||||||
| 134 | 0 | my $var = $$self{var}; | |||||||||
| 135 | 0 | ▶ | if(!defined($var)){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 136 | 0 | $var = "UNDEFINED"; | |||||||||
| 137 | } | ||||||||||
| 138 | 0 | ▶ | if(length($var) > 30){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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 | 1; | |||||||||
| 145 | |||||||||||