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