/usr/share/perl5/Ref/Util.pm is in libref-util-perl 0.203-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 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 | package Ref::Util;
# ABSTRACT: Utility functions for checking references
$Ref::Util::VERSION = '0.203';
use strict;
use warnings;
use Exporter 5.57 'import';
{
my $impl = $ENV{PERL_REF_UTIL_IMPLEMENTATION}
|| our $IMPLEMENTATION
|| 'XS';
if ($impl ne 'PP' && eval { require Ref::Util::XS; 1 }) {
_install_aliases('Ref::Util::XS');
}
else {
require Ref::Util::PP;
_install_aliases('Ref::Util::PP');
}
}
sub _install_aliases {
my ($package) = @_;
no warnings 'once';
no strict 'refs';
our %EXPORT_TAGS = %{"${package}::EXPORT_TAGS"};
our @EXPORT_OK = @{"${package}::EXPORT_OK"};
*$_ = \&{"${package}::$_"} for '_using_custom_ops', @EXPORT_OK;
}
1;
__END__
=pod
=encoding UTF-8
=head1 NAME
Ref::Util - Utility functions for checking references
=head1 VERSION
version 0.203
=head1 SYNOPSIS
use Ref::Util qw( is_plain_arrayref is_plain_hashref );
if ( is_plain_arrayref( $something ) ) {
print for @{ $something };
} elsif ( is_plain_hashref( $something ) ) {
print for sort values %{ $something };
}
=head1 DESCRIPTION
Ref::Util introduces several functions to help identify references in a
B<smarter> (and usually faster) way. In short:
# conventional approach # with Ref::Util
ref( $foo ) eq 'ARRAY' is_plain_arrayref( $foo )
use Scalar::Util qw( reftype );
reftype( $foo ) eq 'ARRAY' is_arrayref( $foo )
The difference:
=over 4
=item * No comparison against a string constant
When you call C<ref>, you stringify the reference and then compare it
to some string constant (like C<ARRAY> or C<HASH>). Not just awkward,
it's brittle since you can mispell the string.
If you use L<Scalar::Util>'s C<reftype>, you still compare it as a
string:
if ( reftype($foo) eq 'ARRAY' ) { ... }
=item * Supports blessed variables
B<Note:> In future versions, the idea is to make the default functions
use the B<plain> variation, which means explicitly non-blessed references.
If you want to explicitly check for B<blessed> references, you should use
the C<is_blessed_*> functions. There will be an C<is_any_*> variation
which will act like the current main functions - not caring whether it's
blessed or not.
When calling C<ref>, you receive either the reference type (B<SCALAR>,
B<ARRAY>, B<HASH>, etc.) or the package it's blessed into.
When calling C<is_arrayref> (et. al.), you check the variable flags,
so even if it's blessed, you know what type of variable is blessed.
my $foo = bless {}, 'PKG';
ref($foo) eq 'HASH'; # fails
use Ref::Util 'is_hashref';
my $foo = bless {}, 'PKG';
is_hashref($foo); # works
On the other hand, in some situations it might be better to specifically
exclude blessed references. The rationale for that might be that merely
because some object happens to be implemented using a hash doesn't mean it's
necessarily correct to treat it as a hash. For these situations, you can use
C<is_plain_hashref> and friends, which have the same performance benefits as
C<is_hashref>.
There is also a family of functions with names like C<is_blessed_hashref>;
these return true for blessed object instances that are implemented using
the relevant underlying type.
=item * Supports tied variables and magic
Tied variables (used in L<Readonly>, for example) are supported.
use Ref::Util qw<is_plain_hashref>;
use Readonly;
Readonly::Scalar my $rh2 => { a => { b => 2 } };
is_plain_hashref($rh2); # success
L<Ref::Util> added support for this in 0.100. Prior to this version
the test would fail.
=item * Ignores overloading
These functions ignore overloaded operators and simply check the
variable type. Overloading will likely not ever be supported, since I
deem it problematic and confusing.
Overloading makes your variables opaque containers and hides away
B<what> they are and instead require you to figure out B<how> to use
them. This leads to code that has to test different abilities (in
C<eval>, so it doesn't crash) and to interfaces that get around what
a person thought you would do with a variable. This would have been
alright, except there is no clear way of introspecting it.
=item * Ignores subtle types:
The following types, provided by L<Scalar::Util>'s C<reftype>, are
not supported:
=over 4
=item * C<VSTRING>
This is a C<PVMG> ("normal" variable) with a flag set for VSTRINGs.
Since this is not a reference, it is not supported.
=item * C<LVALUE>
A variable that delegates to another scalar. Since this is not a
reference, it is not supported.
=item * C<INVLIST>
I couldn't find documentation for this type.
=back
Support might be added, if a good reason arises.
=item * Usually fast
When possible, Ref::Util uses L<Ref::Util::XS> as its implementation. (If
you don't have a C compiler available, it uses a pure Perl fallback that has
all the other advantages of Ref::Util, but isn't as fast.)
In fact, Ref::Util::XS has two alternative implementations available
internally, depending on the features supported by the version of Perl
you're using. For Perls that supports custom OPs, we actually add an OP
(which is faster); for other Perls, the implementation that simply calls an
XS function (which is still faster than the pure-Perl equivalent).
See below for L<benchmark results|/"BENCHMARKS">.
=back
=head1 EXPORT
Nothing is exported by default. You can ask for specific subroutines
(described below) or ask for all subroutines at once:
use Ref::Util qw<is_scalarref is_arrayref is_hashref ...>;
# or
use Ref::Util ':all';
=head1 SUBROUTINES
=head2 is_ref($ref)
Check for a reference to anything.
is_ref([]);
=head2 is_scalarref($ref)
Check for a scalar reference.
is_scalarref(\"hello");
is_scalarref(\30);
is_scalarref(\$value);
Note that, even though a reference is itself a type of scalar value, a
reference to another reference is not treated as a scalar reference:
!is_scalarref(\\1);
The rationale for this is two-fold. First, callers that want to decide how
to handle inputs based on their reference type will usually want to treat a
ref-ref and a scalar-ref differently. Secondly, this more closely matches
the behavior of the C<ref> built-in and of L<Scalar::Util/reftype>, which
report a ref-ref as C<REF> rather than C<SCALAR>.
=head2 is_arrayref($ref)
Check for an array reference.
is_arrayref([]);
=head2 is_hashref($ref)
Check for a hash reference.
is_hashref({});
=head2 is_coderef($ref)
Check for a code reference.
is_coderef( sub {} );
=head2 is_regexpref($ref)
Check for a regular expression (regex, regexp) reference.
is_regexpref( qr// );
=head2 is_globref($ref)
Check for a glob reference.
is_globref( \*STDIN );
=head2 is_formatref($ref)
Check for a format reference.
# set up format in STDOUT
format STDOUT =
.
# now we can test it
is_formatref( *main::STDOUT{'FORMAT'} );
This function is not available in Perl 5.6 and will trigger a
C<croak()>.
=head2 is_ioref($ref)
Check for an IO reference.
is_ioref( *STDOUT{IO} );
=head2 is_refref($ref)
Check for a reference to a reference.
is_refref( \[] ); # reference to array reference
=head2 is_plain_scalarref($ref)
Check for an unblessed scalar reference.
is_plain_scalarref(\"hello");
is_plain_scalarref(\30);
is_plain_scalarref(\$value);
=head2 is_plain_ref($ref)
Check for an unblessed reference to anything.
is_plain_ref([]);
=head2 is_plain_arrayref($ref)
Check for an unblessed array reference.
is_plain_arrayref([]);
=head2 is_plain_hashref($ref)
Check for an unblessed hash reference.
is_plain_hashref({});
=head2 is_plain_coderef($ref)
Check for an unblessed code reference.
is_plain_coderef( sub {} );
=head2 is_plain_globref($ref)
Check for an unblessed glob reference.
is_plain_globref( \*STDIN );
=head2 is_plain_formatref($ref)
Check for an unblessed format reference.
# set up format in STDOUT
format STDOUT =
.
# now we can test it
is_plain_formatref(bless *main::STDOUT{'FORMAT'} );
=head2 is_plain_refref($ref)
Check for an unblessed reference to a reference.
is_plain_refref( \[] ); # reference to array reference
=head2 is_blessed_scalarref($ref)
Check for a blessed scalar reference.
is_blessed_scalarref(bless \$value);
=head2 is_blessed_ref($ref)
Check for a blessed reference to anything.
is_blessed_ref(bless [], $class);
=head2 is_blessed_arrayref($ref)
Check for a blessed array reference.
is_blessed_arrayref(bless [], $class);
=head2 is_blessed_hashref($ref)
Check for a blessed hash reference.
is_blessed_hashref(bless {}, $class);
=head2 is_blessed_coderef($ref)
Check for a blessed code reference.
is_blessed_coderef( bless sub {}, $class );
=head2 is_blessed_globref($ref)
Check for a blessed glob reference.
is_blessed_globref( bless \*STDIN, $class );
=head2 is_blessed_formatref($ref)
Check for a blessed format reference.
# set up format for FH
format FH =
.
# now we can test it
is_blessed_formatref(bless *FH{'FORMAT'}, $class );
=head2 is_blessed_refref($ref)
Check for a blessed reference to a reference.
is_blessed_refref( bless \[], $class ); # reference to array reference
=head1 BENCHMARKS
Here is a benchmark comparing similar checks.
my $bench = Dumbbench->new(
target_rel_precision => 0.005,
initial_runs => 20,
);
my $amount = 1e7;
my $ref = [];
$bench->add_instances(
Dumbbench::Instance::PerlSub->new(
name => 'Ref::Util::is_plain_arrayref (CustomOP)',
code => sub {
Ref::Util::is_plain_arrayref($ref) for ( 1 .. $amount )
},
),
Dumbbench::Instance::PerlSub->new(
name => 'ref(), reftype(), !blessed()',
code => sub {
ref $ref
&& Scalar::Util::reftype($ref) eq 'ARRAY'
&& !Scalar::Util::blessed($ref)
for ( 1 .. $amount );
},
),
Dumbbench::Instance::PerlSub->new(
name => 'ref()',
code => sub { ref($ref) eq 'ARRAY' for ( 1 .. $amount ) },
),
Dumbbench::Instance::PerlSub->new(
name => 'Data::Util::is_array_ref',
code => sub { is_array_ref($ref) for ( 1 .. $amount ) },
),
);
The results:
ref(): 5.335e+00 +/- 1.8e-02 (0.3%)
ref(), reftype(), !blessed(): 1.5545e+01 +/- 3.1e-02 (0.2%)
Ref::Util::is_plain_arrayref (CustomOP): 2.7951e+00 +/- 6.2e-03 (0.2%)
Data::Util::is_array_ref: 5.9074e+00 +/- 7.5e-03 (0.1%)
(Rounded run time per iteration)
A benchmark against L<Data::Util>:
Ref::Util::is_plain_arrayref: 3.47157e-01 +/- 6.8e-05 (0.0%)
Data::Util::is_array_ref: 6.7562e-01 +/- 7.5e-04 (0.1%)
=head1 SEE ALSO
=over 4
=item * L<Params::Classify>
=item * L<Scalar::Util>
=item * L<Data::Util>
=back
=head1 THANKS
The following people have been invaluable in their feedback and support.
=over 4
=item * Yves Orton
=item * Steffen Müller
=item * Jarkko Hietaniemi
=item * Mattia Barbon
=item * Zefram
=item * Tony Cook
=item * Sergey Aleynikov
=back
=head1 AUTHORS
=over 4
=item * Aaron Crane
=item * Vikentiy Fesunov
=item * Sawyer X
=item * Gonzalo Diethelm
=item * p5pclub
=back
=head1 LICENSE
This software is made available under the MIT Licence as stated in the
accompanying LICENSE file.
=head1 AUTHORS
=over 4
=item *
Sawyer X <xsawyerx@cpan.org>
=item *
Aaron Crane <arc@cpan.org>
=item *
Vikenty Fesunov <vyf@cpan.org>
=item *
Gonzalo Diethelm <gonzus@cpan.org>
=item *
Karen Etheridge <ether@cpan.org>
=back
=head1 COPYRIGHT AND LICENSE
This software is Copyright (c) 2017 by Sawyer X.
This is free software, licensed under:
The MIT (X11) License
=cut
|