| 1 | package Data::SIprefixes::nano; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use bignum; | ||
| 6 | use base 'Error::Helper'; | ||
| 7 | |||
| 8 | =head1 NAME | ||
| 9 | |||
| 10 | Data::SIprefixes::nano - This provides micro matching for Data::SIprefixes. | ||
| 11 | |||
| 12 | =head1 VERSION | ||
| 13 | |||
| 14 | Version 0.0.0 | ||
| 15 | |||
| 16 | =cut | ||
| 17 | |||
| 18 | our $VERSION = '0.0.0'; | ||
| 19 | |||
| 20 | =head1 SYNOPSIS | ||
| 21 | |||
| 22 | use Data::SIprefixes::nano; | ||
| 23 | |||
| 24 | my $prefix=Data::SIprefixes::nano->new; | ||
| 25 | |||
| 26 | my $origMeasure='nanometer'; | ||
| 27 | |||
| 28 | my $measure=$prefix->longMatch( $origMeasure ); | ||
| 29 | my $long; | ||
| 30 | if ( $prefix->error ){ | ||
| 31 | warn('error:'.$prefix->error.': '.$prefix->errorString); | ||
| 32 | }elseif( ! defined( $measure ) ){ | ||
| 33 | |||
| 34 | $measure=$prefix->shortMatch( $origMeasure ); | ||
| 35 | |||
| 36 | }else{ | ||
| 37 | $long=1; | ||
| 38 | } | ||
| 39 | |||
| 40 | =head1 METHODS | ||
| 41 | |||
| 42 | =head2 new | ||
| 43 | |||
| 44 | This initiates the object. | ||
| 45 | |||
| 46 | my $prefix=$Data::SIprefixes::nano->new; | ||
| 47 | |||
| 48 | =cut | ||
| 49 | |||
| 50 | sub new { | ||
| 51 | my $string=$_[1]; | ||
| 52 | |||
| 53 | my $self={ | ||
| 54 | perror=>undef, | ||
| 55 | error=>undef, | ||
| 56 | errorString=>'', | ||
| 57 | fromBase=>1000, | ||
| 58 | toBase=>1000, | ||
| 59 | }; | ||
| 60 | bless $self; | ||
| 61 | |||
| 62 | $self->{fromBase} **= 3; | ||
| 63 | $self->{toBase} **= -3; | ||
| 64 | |||
| 65 | return $self; | ||
| 66 | } | ||
| 67 | |||
| 68 | =head2 fromBase | ||
| 69 | |||
| 70 | Returns the number needed to to multiple it by to get from the unprefixed | ||
| 71 | measure to the prefixed measure. | ||
| 72 | |||
| 73 | my $fromBase=$prefix->fromBase; | ||
| 74 | |||
| 75 | =cut | ||
| 76 | |||
| 77 | sub fromBase{ | ||
| 78 | my $self=$_[0]; | ||
| 79 | |||
| 80 | if ( ! $self->errorblank ){ | ||
| 81 | $self->warnString('Failed to blank the previous error'); | ||
| 82 | } | ||
| 83 | |||
| 84 | return $self->fromBase; | ||
| 85 | } | ||
| 86 | |||
| 87 | =head2 longMatch | ||
| 88 | |||
| 89 | Matches long SI prefixed measures. | ||
| 90 | |||
| 91 | A match returns the measure with out the SI prefix, which will be '' | ||
| 92 | if no measure is specified. | ||
| 93 | |||
| 94 | my $measure=$prefix->longMatch( $origMeasure ); | ||
| 95 | if ( $prefix->error ){ | ||
| 96 | warn('error:'.$foo->error.': '.$foo->errorString); | ||
| 97 | } | ||
| 98 | |||
| 99 | =cut | ||
| 100 | |||
| 101 | sub longMatch{ | ||
| 102 | my $self=$_[0]; | ||
| 103 | my $measure=$_[1]; | ||
| 104 | |||
| 105 | if ( ! $self->errorblank ){ | ||
| 106 | $self->warnString('Failed to blank the previous error'); | ||
| 107 | } | ||
| 108 | |||
| 109 | if ( ! defined( $measure ) ){ | ||
| 110 | $self->{error}=1; | ||
| 111 | $self->{errorString}='No measure defined'; | ||
| 112 | $self->warn; | ||
| 113 | return undef; | ||
| 114 | } | ||
| 115 | |||
| 116 | if ( $measure=~/^nano/ ){ | ||
| 117 | my $origMeasure=$measure; | ||
| 118 | $measure=~s/^nano//g; | ||
| 119 | |||
| 120 | if ( $measure =~ /^ / ){ | ||
| 121 | $self->{error}=2; | ||
| 122 | $self->{errorString}='Space found after prefix, /^milli/, in "'.$origMeasure.'"' ; | ||
| 123 | $self->warn; | ||
| 124 | return undef; | ||
| 125 | } | ||
| 126 | |||
| 127 | return $measure; | ||
| 128 | } | ||
| 129 | |||
| 130 | return undef; | ||
| 131 | } | ||
| 132 | |||
| 133 | =head2 shortMatch | ||
| 134 | |||
| 135 | Matches short SI prefixed measures. | ||
| 136 | |||
| 137 | A match returns the measure with out the SI prefix, which will be '' | ||
| 138 | if no measure is specified. | ||
| 139 | |||
| 140 | my $measure=$prefix->longMatch( $origMeasure ); | ||
| 141 | if ( $prefix->error ){ | ||
| 142 | warn('error:'.$foo->error.': '.$foo->errorString); | ||
| 143 | } | ||
| 144 | |||
| 145 | =cut | ||
| 146 | |||
| 147 | sub shortMatch{ | ||
| 148 | my $self=$_[0]; | ||
| 149 | my $measure=$_[1]; | ||
| 150 | |||
| 151 | if ( ! $self->errorblank ){ | ||
| 152 | $self->warnString('Failed to blank the previous error'); | ||
| 153 | } | ||
| 154 | |||
| 155 | if ( ! defined( $measure ) ){ | ||
| 156 | $self->{error}=1; | ||
| 157 | $self->{errorString}='No measure defined'; | ||
| 158 | $self->warn; | ||
| 159 | return undef; | ||
| 160 | } | ||
| 161 | |||
| 162 | # avoids possible collisions | ||
| 163 | if ( $measure eq 'newton' ){ | ||
| 164 | return $measure; | ||
| 165 | } | ||
| 166 | |||
| 167 | if ( $measure=~/^n/ ){ | ||
| 168 | my $origMeasure=$measure; | ||
| 169 | $measure=~s/^n//g; | ||
| 170 | |||
| 171 | if ( $measure =~ /^ / ){ | ||
| 172 | $self->{error}=2; | ||
| 173 | $self->{errorString}='Space found after prefix, /^n/, in "'.$origMeasure.'"' ; | ||
| 174 | $self->warn; | ||
| 175 | return undef; | ||
| 176 | } | ||
| 177 | |||
| 178 | return $measure; | ||
| 179 | } | ||
| 180 | |||
| 181 | return undef; | ||
| 182 | } | ||
| 183 | |||
| 184 | =head2 symbol | ||
| 185 | |||
| 186 | This returns the symbol for the prefix. | ||
| 187 | |||
| 188 | my $symbol=$prefix->symbol; | ||
| 189 | |||
| 190 | =cut | ||
| 191 | |||
| 192 | sub symbol{ | ||
| 193 | return 'n'; | ||
| 194 | } | ||
| 195 | |||
| 196 | =head2 toBase | ||
| 197 | |||
| 198 | Returns the number needed to to multiple it by to get from the prefixed measure | ||
| 199 | number to the unprefixed measure. | ||
| 200 | |||
| 201 | my $toBase=$prefix->toBase; | ||
| 202 | |||
| 203 | =cut | ||
| 204 | |||
| 205 | sub toBase{ | ||
| 206 | my $self=$_[0]; | ||
| 207 | |||
| 208 | if ( ! $self->errorblank ){ | ||
| 209 | $self->warnString('Failed to blank the previous error'); | ||
| 210 | } | ||
| 211 | |||
| 212 | return $self->toBase; | ||
| 213 | } | ||
| 214 | |||
| 215 | =head1 ERROR CODES | ||
| 216 | |||
| 217 | =head2 1 | ||
| 218 | |||
| 219 | Nothing passed for a measure. | ||
| 220 | |||
| 221 | =head2 2 | ||
| 222 | |||
| 223 | Space found after prefix. | ||
| 224 | |||
| 225 | =head1 AUTHOR | ||
| 226 | |||
| 227 | Zane C. Bowers-Hadley, C<< <vvelox at vvelox.net> >> | ||
| 228 | |||
| 229 | =head1 BUGS | ||
| 230 | |||
| 231 | Please report any bugs or feature requests to C<bug-data-siprefixes at rt.cpan.org>, or through | ||
| 232 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Data-SIprefixes>. I will be notified, and then you'll | ||
| 233 | automatically be notified of progress on your bug as I make changes. | ||
| 234 | |||
| 235 | |||
| 236 | |||
| 237 | |||
| 238 | =head1 SUPPORT | ||
| 239 | |||
| 240 | You can find documentation for this module with the perldoc command. | ||
| 241 | |||
| 242 | perldoc Data::SIprefixes::nano | ||
| 243 | |||
| 244 | |||
| 245 | You can also look for information at: | ||
| 246 | |||
| 247 | =over 4 | ||
| 248 | |||
| 249 | =item * RT: CPAN's request tracker (report bugs here) | ||
| 250 | |||
| 251 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Data-SIprefixes> | ||
| 252 | |||
| 253 | =item * AnnoCPAN: Annotated CPAN documentation | ||
| 254 | |||
| 255 | L<http://annocpan.org/dist/Data-SIprefixes> | ||
| 256 | |||
| 257 | =item * CPAN Ratings | ||
| 258 | |||
| 259 | L<http://cpanratings.perl.org/d/Data-SIprefixes> | ||
| 260 | |||
| 261 | =item * Search CPAN | ||
| 262 | |||
| 263 | L<http://search.cpan.org/dist/Data-SIprefixes/> | ||
| 264 | |||
| 265 | =back | ||
| 266 | |||
| 267 | |||
| 268 | =head1 ACKNOWLEDGEMENTS | ||
| 269 | |||
| 270 | |||
| 271 | =head1 LICENSE AND COPYRIGHT | ||
| 272 | |||
| 273 | Copyright 2012 Zane C. Bowers-Hadley. | ||
| 274 | |||
| 275 | This program is free software; you can redistribute it and/or modify it | ||
| 276 | under the terms of either: the GNU General Public License as published | ||
| 277 | by the Free Software Foundation; or the Artistic License. | ||
| 278 | |||
| 279 | See http://dev.perl.org/licenses/ for more information. | ||
| 280 | |||
| 281 | |||
| 282 | =cut | ||
| 283 | |||
| 284 | 1; # End of Data::SIprefixes::nano |