| 1 | # vi: set ts=4 sw=4 ht=4 et : | ||
| 2 | package perl5i::2::SCALAR; | ||
| 3 | use 5.010; | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use warnings; | ||
| 7 | require Carp::Fix::1_25; | ||
| 8 | use perl5i::2::autobox; | ||
| 9 | |||
| 10 | sub title_case { | ||
| 11 | my ($string) = @_; | ||
| 12 | $string =~ s/\b(\w)/\U$1/g; | ||
| 13 | return $string; | ||
| 14 | } | ||
| 15 | |||
| 16 | |||
| 17 | sub center { | ||
| 18 | my ($string, $size, $char) = @_; | ||
| 19 | Carp::Fix::1_25::carp("Use of uninitialized value for size in center()") if !defined $size; | ||
| 20 | $size //= 0; | ||
| 21 | $char //= ' '; | ||
| 22 | |||
| 23 | if (length $char > 1) { | ||
| 24 | my $bad = $char; | ||
| 25 | $char = substr $char, 0, 1; | ||
| 26 | Carp::Fix::1_25::carp("'$bad' is longer than one character, using '$char' instead"); | ||
| 27 | } | ||
| 28 | |||
| 29 | my $len = length $string; | ||
| 30 | |||
| 31 | return $string if $size <= $len; | ||
| 32 | |||
| 33 | my $padlen = $size - $len; | ||
| 34 | |||
| 35 | # pad right with half the remaining characters | ||
| 36 | my $rpad = int( $padlen / 2 ); | ||
| 37 | |||
| 38 | # bias the left padding to one more space, if $size - $len is odd | ||
| 39 | my $lpad = $padlen - $rpad; | ||
| 40 | |||
| 41 | return $char x $lpad . $string . $char x $rpad; | ||
| 42 | } | ||
| 43 | |||
| 44 | |||
| 45 | sub ltrim { | ||
| 46 | my ($string,$trim_charset) = @_; | ||
| 47 | $trim_charset = '\s' unless defined $trim_charset; | ||
| 48 | my $re = qr/^[$trim_charset]*/; | ||
| 49 | $string =~ s/$re//; | ||
| 50 | return $string; | ||
| 51 | } | ||
| 52 | |||
| 53 | |||
| 54 | sub rtrim { | ||
| 55 | my ($string,$trim_charset) = @_; | ||
| 56 | $trim_charset = '\s' unless defined $trim_charset; | ||
| 57 | my $re = qr/[$trim_charset]*$/; | ||
| 58 | $string =~ s/$re//; | ||
| 59 | return $string; | ||
| 60 | } | ||
| 61 | |||
| 62 | |||
| 63 | sub trim { | ||
| 64 | my $charset = $_[1]; | ||
| 65 | |||
| 66 | return rtrim(ltrim($_[0], $charset), $charset); | ||
| 67 | } | ||
| 68 | |||
| 69 | |||
| 70 | sub wrap { | ||
| 71 | my ($string, %args) = @_; | ||
| 72 | |||
| 73 | my $width = $args{width} // 76; | ||
| 74 | my $separator = $args{separator} // "\n"; | ||
| 75 | |||
| 76 | return $string if $width <= 0; | ||
| 77 | |||
| 78 | require Text::Wrap; | ||
| 79 | local $Text::Wrap::separator = $separator; | ||
| 80 | local $Text::Wrap::columns = $width; | ||
| 81 | |||
| 82 | return Text::Wrap::wrap('', '', $string); | ||
| 83 | |||
| 84 | } | ||
| 85 | |||
| 86 | |||
| 87 | # Always reverse the scalar, not a single element list. | ||
| 88 | sub reverse { | ||
| 89 | return scalar CORE::reverse $_[0]; | ||
| 90 | } | ||
| 91 | |||
| 92 | |||
| 93 | # Compatiblity wrappers. Remove in 3.0. | ||
| 94 | sub untaint { | ||
| 95 | return $_[0]->mo->untaint; | ||
| 96 | } | ||
| 97 | |||
| 98 | sub taint { | ||
| 99 | return $_[0]->mo->taint; | ||
| 100 | } | ||
| 101 | |||
| 102 | sub is_tainted { | ||
| 103 | return $_[0]->mo->is_tainted; | ||
| 104 | } | ||
| 105 | |||
| 106 | |||
| 107 | sub require { | ||
| 108 | my $error = do { | ||
| 109 | # Don't let them leak out or get reset | ||
| 110 | local($!,$@); | ||
| 111 | return $_[0] if eval { require $_[0]->module2path }; | ||
| 112 | $@; | ||
| 113 | }; | ||
| 114 | |||
| 115 | my($pack, $file, $line) = caller; | ||
| 116 | $error =~ s{ at .*? line .*?\.\n$}{ at $file line $line.\n}; | ||
| 117 | die $error; | ||
| 118 | } | ||
| 119 | |||
| 120 | |||
| 121 | sub alias { | ||
| 122 | Carp::Fix::1_25::croak(<<ERROR) if !ref $_[0]; | ||
| 123 | Due to limitations in autoboxing, scalars cannot be aliased. | ||
| 124 | Sorry. Use a scalar reference instead. | ||
| 125 | ERROR | ||
| 126 | |||
| 127 | goto &perl5i::2::UNIVERSAL::alias; | ||
| 128 | } | ||
| 129 | |||
| 130 | |||
| 131 | require POSIX; | ||
| 132 | *ceil = \&POSIX::ceil; | ||
| 133 | *floor = \&POSIX::floor; | ||
| 134 | *round_up = \&ceil; | ||
| 135 | *round_down = \&floor; | ||
| 136 | sub round { | ||
| 137 | return 0 if $_[0] == 0; | ||
| 138 | |||
| 139 | if( $_[0]->is_positive ) { | ||
| 140 | abs($_[0] - int($_[0])) < 0.5 ? round_down($_[0]) | ||
| 141 | : round_up($_[0]) | ||
| 142 | } | ||
| 143 | else { | ||
| 144 | abs($_[0] - int($_[0])) < 0.5 ? round_up($_[0]) | ||
| 145 | : round_down($_[0]) | ||
| 146 | } | ||
| 147 | } | ||
| 148 | |||
| 149 | require Scalar::Util; | ||
| 150 | *is_number = \&Scalar::Util::looks_like_number; | ||
| 151 | sub is_positive { $_[0]->is_number && $_[0] > 0 } | ||
| 152 | sub is_negative { $_[0]->is_number && $_[0] < 0 } | ||
| 153 | sub is_integer { | ||
| 154 | return 0 if !$_[0]->is_number; | ||
| 155 | return $_[0] =~ m{ ^[+-]? \d+ $}x; | ||
| 156 | } | ||
| 157 | *is_int = \&is_integer; | ||
| 158 | sub is_even { $_[0]->is_integer && !($_[0] % 2) } | ||
| 159 | sub is_odd { $_[0]->is_integer && ($_[0] % 2) } | ||
| 160 | sub is_decimal { | ||
| 161 | return 0 if !$_[0]->is_number; | ||
| 162 | |||
| 163 | # Fast and reliable way to spot most decimals | ||
| 164 | return 1 if ((int($_[0]) - $_[0]) != 0); | ||
| 165 | |||
| 166 | # Final gate for tricky things like 1.0, 1. and .0 | ||
| 167 | return $_[0] =~ m{^ [+-]? (?: \d+\.\d* | \.\d+ ) $}x; | ||
| 168 | } | ||
| 169 | |||
| 170 | sub group_digits { | ||
| 171 | my($self, $opts) = @_; | ||
| 172 | |||
| 173 | state $defaults = { | ||
| 174 | thousands_sep => ",", | ||
| 175 | grouping => 3, | ||
| 176 | decimal_point => ".", | ||
| 177 | }; | ||
| 178 | |||
| 179 | my $is_money = $opts->{currency}; | ||
| 180 | my $sep = $opts->{separator} // (_get_from_locale("thousands_sep", $is_money) | ||
| 181 | || $defaults->{thousands_sep}); | ||
| 182 | my $grouping = $opts->{grouping} // (_get_grouping($is_money) | ||
| 183 | || $defaults->{grouping}); | ||
| 184 | my $decimal_point = $opts->{decimal_point} // (_get_from_locale("decimal_point", $is_money) | ||
| 185 | || $defaults->{decimal_point}); | ||
| 186 | return $self if $grouping == 0; | ||
| 187 | |||
| 188 | my($integer, $decimal) = split m{\.}, $self, 2; | ||
| 189 | |||
| 190 | $integer = CORE::reverse $integer; | ||
| 191 | $integer =~ s/(\d{$grouping})(?=\d)(?!\d*\.)/$1$sep/g; | ||
| 192 | $integer = CORE::reverse $integer; | ||
| 193 | |||
| 194 | my $number = $integer; | ||
| 195 | $number .= $decimal_point . $decimal if defined $decimal; | ||
| 196 | |||
| 197 | return $number; | ||
| 198 | } | ||
| 199 | |||
| 200 | sub commify { | ||
| 201 | my($self, $args) = @_; | ||
| 202 | |||
| 203 | state $defaults = { | ||
| 204 | separator => ",", | ||
| 205 | grouping => 3, | ||
| 206 | decimal_point => ".", | ||
| 207 | }; | ||
| 208 | |||
| 209 | my $opts = $defaults->merge($args); | ||
| 210 | |||
| 211 | return $self->group_digits( $opts ); | ||
| 212 | } | ||
| 213 | |||
| 214 | |||
| 215 | sub _get_lconv { | ||
| 216 | return eval { | ||
| 217 | require POSIX; | ||
| 218 | POSIX::localeconv(); | ||
| 219 | } || {}; | ||
| 220 | } | ||
| 221 | |||
| 222 | sub _get_grouping { | ||
| 223 | my $is_money = shift; | ||
| 224 | my $key = $is_money ? "mon_grouping" : "grouping"; | ||
| 225 | |||
| 226 | my $lconv = _get_lconv; | ||
| 227 | |||
| 228 | if( $lconv->{$key} ) { | ||
| 229 | return (unpack("C*", $lconv->{$key}))[0]; | ||
| 230 | } | ||
| 231 | else { | ||
| 232 | return; | ||
| 233 | } | ||
| 234 | } | ||
| 235 | |||
| 236 | sub _get_from_locale { | ||
| 237 | my($key, $is_money) = @_; | ||
| 238 | $key = "mon_$key" if $is_money; | ||
| 239 | |||
| 240 | my $lconv = _get_lconv; | ||
| 241 | return $lconv->{$key}; | ||
| 242 | } | ||
| 243 | |||
| 244 | |||
| 245 | sub path2module { | ||
| 246 | my $path = shift; | ||
| 247 | |||
| 248 | my($vol, $dirs, $file) = File::Spec->splitpath($path); | ||
| 249 | my @dirs = grep length, File::Spec->splitdir($dirs); | ||
| 250 | |||
| 251 | Carp::Fix::1_25::croak("'$path' does not look like a Perl module path") | ||
| 252 | if $file !~ m{\.pm$} or File::Spec->file_name_is_absolute($path); | ||
| 253 | |||
| 254 | $file =~ s{\.pm$}{}; | ||
| 255 | |||
| 256 | my $module = join "::", @dirs, $file; | ||
| 257 | Carp::Fix::1_25::croak("'$module' is not a valid module name") unless $module->is_module_name; | ||
| 258 | |||
| 259 | return $module; | ||
| 260 | } | ||
| 261 | |||
| 262 | |||
| 263 | sub is_module_name { | ||
| 264 | my $name = shift; | ||
| 265 | |||
| 266 | return 0 unless defined($name); | ||
| 267 | |||
| 268 | return 0 unless $name =~ qr{\A | ||
| 269 | [[:alpha:]_] # Must start with an alpha or _ | ||
| 270 | [[:word:]]* # Then any number of alpha numerics or _ | ||
| 271 | (?: :: [[:word:]]+ )* # Then optional ::word's | ||
| 272 | \z | ||
| 273 | }x; | ||
| 274 | |||
| 275 | return 1; | ||
| 276 | } | ||
| 277 | |||
| 278 | |||
| 279 | sub module2path { | ||
| 280 | my $module = shift; | ||
| 281 | |||
| 282 | Carp::Fix::1_25::croak("'$module' is not a valid module name") unless $module->is_module_name; | ||
| 283 | |||
| 284 | my @parts = split /::/, $module; | ||
| 285 | $parts[-1] .= ".pm"; | ||
| 286 | |||
| 287 | return join "/", @parts; | ||
| 288 | } | ||
| 289 | |||
| 290 | |||
| 291 | # On the first call, load Path::Tiny and replace our path() with | ||
| 292 | # Path::Tiny::path() so we don't try to load it again. | ||
| 293 | # Shaves about 1/6 off the call time. | ||
| 294 | sub path { | ||
| 295 | require Path::Tiny; | ||
| 296 | no warnings 'redefine'; | ||
| 297 | *path = \&Path::Tiny::path; | ||
| 298 | goto &Path::Tiny::path; | ||
| 299 | } | ||
| 300 | |||
| 301 | 1; |