1
# vi: set ts=4 sw=4 ht=4 et :
2
package perl5i::2::SCALAR;
8
use perl5i::2::autobox;
12
$string =~ s/\b(\w)/\U$1/g;
18
my ($string, $size, $char) = @_;
19
Carp::carp("Use of uninitialized value for size in center()") if !defined $size;
23
if (length $char > 1) {
25
$char = substr $char, 0, 1;
26
Carp::carp("'$bad' is longer than one character, using '$char' instead");
29
my $len = length $string;
31
return $string if $size <= $len;
33
my $padlen = $size - $len;
35
# pad right with half the remaining characters
36
my $rpad = int( $padlen / 2 );
38
# bias the left padding to one more space, if $size - $len is odd
39
my $lpad = $padlen - $rpad;
41
return $char x $lpad . $string . $char x $rpad;
46
my ($string,$trim_charset) = @_;
47
$trim_charset = '\s' unless defined $trim_charset;
48
my $re = qr/^[$trim_charset]*/;
55
my ($string,$trim_charset) = @_;
56
$trim_charset = '\s' unless defined $trim_charset;
57
my $re = qr/[$trim_charset]*$/;
66
return rtrim(ltrim($_[0], $charset), $charset);
71
my ($string, %args) = @_;
73
my $width = $args{width} // 76;
74
my $separator = $args{separator} // "\n";
76
return $string if $width <= 0;
79
local $Text::Wrap::separator = $separator;
80
local $Text::Wrap::columns = $width;
82
return Text::Wrap::wrap('', '', $string);
87
# untaint the scalar itself, not the reference
89
return $_[0]->mo->untaint if ref $_[0];
92
Taint::Util::untaint($_[0]);
97
# untaint the scalar itself, not the reference
99
return $_[0]->mo->taint if ref $_[0];
102
Taint::Util::taint($_[0]);
106
# Could use the version in Meta but this removes the need to check
110
return ref $_[0] ? Taint::Util::tainted(${$_[0]}) : Taint::Util::tainted($_[0]);
116
# Don't let them leak out or get reset
118
return $_[0] if eval { require $_[0]->module2path };
122
my($pack, $file, $line) = caller;
123
$error =~ s{ at .*? line .*?\.\n$}{ at $file line $line.\n};
129
Carp::croak(<<ERROR) if !ref $_[0];
130
Due to limitations in autoboxing, scalars cannot be aliased.
131
Sorry. Use a scalar reference instead.
134
goto &perl5i::2::UNIVERSAL::alias;
139
*ceil = \&POSIX::ceil;
140
*floor = \&POSIX::floor;
142
*round_down = \&floor;
144
return 0 if $_[0] == 0;
146
if( $_[0]->is_positive ) {
147
abs($_[0] - int($_[0])) < 0.5 ? round_down($_[0])
151
abs($_[0] - int($_[0])) < 0.5 ? round_up($_[0])
156
require Scalar::Util;
157
*is_number = \&Scalar::Util::looks_like_number;
158
sub is_positive { $_[0]->is_number && $_[0] > 0 }
159
sub is_negative { $_[0]->is_number && $_[0] < 0 }
161
return 0 if !$_[0]->is_number;
162
return $_[0] =~ m{ ^[+-]? \d+ $}x;
164
*is_int = \&is_integer;
166
return 0 if !$_[0]->is_number;
168
# Fast and reliable way to spot most decimals
169
return 1 if ((int($_[0]) - $_[0]) != 0);
171
# Final gate for tricky things like 1.0, 1. and .0
172
return $_[0] =~ m{^ [+-]? (?: \d+\.\d* | \.\d+ ) $}x;
179
my($vol, $dirs, $file) = File::Spec->splitpath($path);
180
my @dirs = grep length, File::Spec->splitdir($dirs);
182
Carp::croak("'$path' does not look like a Perl module path")
183
if $file !~ m{\.pm$} or File::Spec->file_name_is_absolute($path);
187
return join "::", @dirs, $file;
194
my @parts = split /::/, $module;
197
return join "/", @parts;