/usr/share/perl5/Object/InsideOut/Util.pm is in libobject-insideout-perl 4.02-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 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 | package Object::InsideOut::Util; {
require 5.006;
use strict;
use warnings;
our $VERSION = '4.02';
$VERSION = eval $VERSION;
use Object::InsideOut::Metadata 4.02;
### Module Initialization ###
BEGIN {
# 1. Install our own 'no-op' version of Internals::SvREADONLY for Perl < 5.8
if (! Internals->can('SvREADONLY')) {
*Internals::SvREADONLY = sub (\$;$) { return; };
}
# Import 'share' and 'bless' if threads::shared
if ($threads::shared::threads_shared) {
import threads::shared;
}
}
# 2. Export requested subroutines
sub import
{
my $class = shift; # Not used
# Exportable subroutines
my %EXPORT_OK;
@EXPORT_OK{qw(create_object hash_re is_it make_shared shared_copy)} = undef;
# Handle entries in the import list
my $caller = caller();
my %meta;
while (my $sym = shift) {
if (exists($EXPORT_OK{lc($sym)})) {
# Export subroutine name
no strict 'refs';
*{$caller.'::'.$sym} = \&{lc($sym)};
$meta{$sym}{'hidden'} = 1;
} else {
OIO::Code->die(
'message' => "Symbol '$sym' is not exported by Object::InsideOut::Util",
'Info' => 'Exportable symbols: ' . join(' ', keys(%EXPORT_OK)),
'ignore_package' => 'Object::InsideOut::Util');
}
}
if (%meta) {
add_meta($caller, \%meta);
}
}
### Subroutines ###
# Returns a blessed (optional), readonly (Perl 5.8) anonymous scalar reference
# containing either:
# the value returned by a user-specified subroutine; or
# a user-supplied scalar
sub create_object
{
my ($class, $id) = @_;
# Create the object from an anonymous scalar reference
my $obj = \do{ my $scalar; };
# Set the scalar equal to ...
if (my $ref_type = ref($id)) {
if ($ref_type eq 'CODE') {
# ... the value returned by the user-specified subroutine
local $SIG{__DIE__} = 'OIO::trap';
$$obj = $id->($class);
} else {
# Complain if something other than code ref
OIO::Args->die(
'message' => q/2nd argument to create_object() is not a code ref or scalar/,
'Usage' => 'create_object($class, $scalar) or create_object($class, $code_ref, ...)',
'ignore_package' => 'Object::InsideOut::Util');
}
} else {
# ... the user-supplied scalar
$$obj = $id;
}
# Bless the object into the specified class (optional)
if ($class) {
bless($obj, $class);
}
# Make the object 'readonly' (Perl 5.8)
Internals::SvREADONLY($$obj, 1) if ($] >= 5.008003);
# Done - return the object
return ($obj);
}
# Make a thread-shared version of a complex data structure or object
sub make_shared
{
my $in = shift;
my $cloned = shift || {};
# If not sharing or already thread-shared, then just return the input
if (! ref($in) ||
! $threads::threads ||
! $threads::shared::threads_shared ||
threads::shared::_id($in))
{
return ($in);
}
# Check for previously cloned references
# (this takes care of circular refs as well)
my $addr = Scalar::Util::refaddr($in);
if (exists($cloned->{$addr})) {
# Return the already existing clone
return $cloned->{$addr};
}
# Make copies of array, hash and scalar refs
my $out;
my $ref_type = Scalar::Util::reftype($in);
# Copy an array ref
if ($ref_type eq 'ARRAY') {
# Make empty shared array ref
$out = &threads::shared::share([]);
# Add to clone checking hash
$cloned->{$addr} = $out;
# Recursively copy and add contents
push(@$out, map { make_shared($_, $cloned) } @$in);
}
# Copy a hash ref
elsif ($ref_type eq 'HASH') {
# Make empty shared hash ref
$out = &threads::shared::share({});
# Add to clone checking hash
$cloned->{$addr} = $out;
# Recursively copy and add contents
foreach my $key (keys(%{$in})) {
$out->{$key} = make_shared($in->{$key}, $cloned);
}
}
# Copy a scalar ref
elsif ($ref_type eq 'SCALAR') {
$out = \do{ my $scalar = $$in; };
threads::shared::share($out);
# Add to clone checking hash
$cloned->{$addr} = $out;
}
# Copy of a ref of a ref
elsif ($ref_type eq 'REF') {
# Special handling for $x = \$x
if ($addr == Scalar::Util::refaddr($$in)) {
$out = \$out;
threads::shared::share($out);
$cloned->{$addr} = $out;
} else {
my $tmp;
$out = \$tmp;
threads::shared::share($out);
# Add to clone checking hash
$cloned->{$addr} = $out;
# Recursively copy and add contents
$tmp = make_shared($$in, $cloned);
}
} else {
# Just return anything else
# NOTE: This will end up generating an error
return ($in);
}
# Return blessed copy, if applicable
if (my $class = Scalar::Util::blessed($in)) {
bless($out, $class);
}
# Clone READONLY flag
if ($ref_type eq 'SCALAR') {
if (Internals::SvREADONLY($$in)) {
Internals::SvREADONLY($$out, 1) if ($] >= 5.008003);
}
}
if (Internals::SvREADONLY($in)) {
Internals::SvREADONLY($out, 1) if ($] >= 5.008003);
}
# Return clone
return ($out);
}
# Make a copy of a complex data structure or object.
# If thread-sharing, then make the copy thread-shared.
sub shared_copy
{
return (($threads::shared::threads_shared) ? clone_shared(@_) : clone(@_));
}
# Recursively make a copy of a complex data structure or object that is
# thread-shared
sub clone_shared
{
my $in = shift;
my $cloned = shift || {};
# Just return the item if not a ref or if it's an object
return $in if (! ref($in) || Scalar::Util::blessed($in));
# Check for previously cloned references
# (this takes care of circular refs as well)
my $addr = Scalar::Util::refaddr($in);
if (exists($cloned->{$addr})) {
# Return the already existing clone
return $cloned->{$addr};
}
# Make copies of array, hash and scalar refs
my $out;
my $ref_type = Scalar::Util::reftype($in);
# Copy an array ref
if ($ref_type eq 'ARRAY') {
# Make empty shared array ref
$out = &threads::shared::share([]);
# Add to clone checking hash
$cloned->{$addr} = $out;
# Recursively copy and add contents
push(@$out, map { clone_shared($_, $cloned) } @$in);
}
# Copy a hash ref
elsif ($ref_type eq 'HASH') {
# Make empty shared hash ref
$out = &threads::shared::share({});
# Add to clone checking hash
$cloned->{$addr} = $out;
# Recursively copy and add contents
foreach my $key (keys(%{$in})) {
$out->{$key} = clone_shared($in->{$key}, $cloned);
}
}
# Copy a scalar ref
elsif ($ref_type eq 'SCALAR') {
$out = \do{ my $scalar = $$in; };
threads::shared::share($out);
# Add to clone checking hash
$cloned->{$addr} = $out;
}
# Copy of a ref of a ref
elsif ($ref_type eq 'REF') {
# Special handling for $x = \$x
if ($addr == Scalar::Util::refaddr($$in)) {
$out = \$out;
threads::shared::share($out);
$cloned->{$addr} = $out;
} else {
my $tmp;
$out = \$tmp;
threads::shared::share($out);
# Add to clone checking hash
$cloned->{$addr} = $out;
# Recursively copy and add contents
$tmp = clone_shared($$in, $cloned);
}
} else {
# Just return anything else
# NOTE: This will end up generating an error
return ($in);
}
# Return blessed copy, if applicable
if (my $class = Scalar::Util::blessed($in)) {
bless($out, $class);
}
# Clone READONLY flag
if ($ref_type eq 'SCALAR') {
if (Internals::SvREADONLY($$in)) {
Internals::SvREADONLY($$out, 1) if ($] >= 5.008003);
}
}
if (Internals::SvREADONLY($in)) {
Internals::SvREADONLY($out, 1) if ($] >= 5.008003);
}
# Return clone
return ($out);
}
# Recursively make a copy of a complex data structure or object
sub clone
{
my $in = shift;
my $cloned = shift || {};
# Just return the item if not a ref or if it's an object
return $in if (! ref($in) || Scalar::Util::blessed($in));
# Check for previously cloned references
# (this takes care of circular refs as well)
my $addr = Scalar::Util::refaddr($in);
if (exists($cloned->{$addr})) {
# Return the already existing clone
return $cloned->{$addr};
}
# Make copies of array, hash and scalar refs
my $out;
my $ref_type = Scalar::Util::reftype($in);
# Copy an array ref
if ($ref_type eq 'ARRAY') {
# Make empty shared array ref
$out = [];
# Add to clone checking hash
$cloned->{$addr} = $out;
# Recursively copy and add contents
push(@$out, map { clone($_, $cloned) } @$in);
}
# Copy a hash ref
elsif ($ref_type eq 'HASH') {
# Make empty shared hash ref
$out = {};
# Add to clone checking hash
$cloned->{$addr} = $out;
# Recursively copy and add contents
foreach my $key (keys(%{$in})) {
$out->{$key} = clone($in->{$key}, $cloned);
}
}
# Copy a scalar ref
elsif ($ref_type eq 'SCALAR') {
$out = \do{ my $scalar = $$in; };
# Add to clone checking hash
$cloned->{$addr} = $out;
}
# Copy of a ref of a ref
elsif ($ref_type eq 'REF') {
# Special handling for $x = \$x
if ($addr == Scalar::Util::refaddr($$in)) {
$out = \$out;
$cloned->{$addr} = $out;
} else {
my $tmp;
$out = \$tmp;
# Add to clone checking hash
$cloned->{$addr} = $out;
# Recursively copy and add contents
$tmp = clone($$in, $cloned);
}
} else {
# Just return anything else
# NOTE: This will end up generating an error
return ($in);
}
# Clone READONLY flag
if ($ref_type eq 'SCALAR') {
if (Internals::SvREADONLY($$in)) {
Internals::SvREADONLY($$out, 1) if ($] >= 5.008003);
}
}
if (Internals::SvREADONLY($in)) {
Internals::SvREADONLY($out, 1) if ($] >= 5.008003);
}
# Return clone
return ($out);
}
# Access hash value using regex
sub hash_re
{
my $hash = $_[0]; # Hash ref to search through
my $re = $_[1]; # Regex to match keys against
foreach (keys(%{$hash})) {
if (/$re/) {
return ($hash->{$_}, $_) if wantarray();
return ($hash->{$_});
}
}
return;
}
# Checks if a scalar is a specified type
sub is_it
{
my ($thing, $what) = @_;
return ((Scalar::Util::blessed($thing))
? $thing->isa($what)
: (ref($thing) eq $what));
}
} # End of package's lexical scope
1;
|