1package Ref::Util::PP; 2$Ref::Util::PP::VERSION = '0.204'; 3# ABSTRACT: pure-Perl version of Ref::Util 4 5use strict; 6use warnings; 7use Carp (); 8use Scalar::Util (); 9use Exporter 5.57 'import'; 10 11use constant _FORMAT_REFS_WORK => ("$]" >= 5.007); 12use constant _RX_NEEDS_MAGIC => (Scalar::Util::reftype(qr/^/) ne 'REGEXP'); 13 14our %EXPORT_TAGS = ( 'all' => [qw< 15 is_ref 16 is_scalarref 17 is_arrayref 18 is_hashref 19 is_coderef 20 is_regexpref 21 is_globref 22 is_formatref 23 is_ioref 24 is_refref 25 26 is_plain_ref 27 is_plain_scalarref 28 is_plain_arrayref 29 is_plain_hashref 30 is_plain_coderef 31 is_plain_globref 32 is_plain_formatref 33 is_plain_refref 34 35 is_blessed_ref 36 is_blessed_scalarref 37 is_blessed_arrayref 38 is_blessed_hashref 39 is_blessed_coderef 40 is_blessed_globref 41 is_blessed_formatref 42 is_blessed_refref 43>] ); 44our @EXPORT = (); 45our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); 46 47sub _using_custom_ops () { 0 } 48 49if (_RX_NEEDS_MAGIC) { 50 require B; 51 *_is_regexp = sub { 52 no warnings 'uninitialized'; 53 return 0 if ref($_[0]) eq ''; 54 my $o = B::svref_2object($_[0]) or return 0; 55 return 0 if Scalar::Util::blessed($o) ne 'B::PVMG'; 56 57 my $m = $o->MAGIC; 58 while ($m) { 59 return 1 if $m->TYPE eq 'r'; 60 $m = $m->MOREMAGIC; 61 } 62 63 return 0; 64 }; 65} 66 67# ---- 68# -- is_* 69# ---- 70 71sub is_ref($) { length ref $_[0] } 72 73sub is_scalarref($) { 74 no warnings 'uninitialized'; 75 Carp::croak("Too many arguments for is_scalarref") if @_ > 1; 76 my $reftype = Scalar::Util::reftype( $_[0] ); 77 ( $reftype eq 'SCALAR' || $reftype eq 'VSTRING' ) 78 && (!_RX_NEEDS_MAGIC || !_is_regexp($_[0])); 79} 80 81sub is_arrayref($) { 82 no warnings 'uninitialized'; 83 Carp::croak("Too many arguments for is_arrayref") if @_ > 1; 84 Scalar::Util::reftype( $_[0] ) eq 'ARRAY'; 85} 86 87sub is_hashref($) { 88 no warnings 'uninitialized'; 89 Carp::croak("Too many arguments for is_hashref") if @_ > 1; 90 Scalar::Util::reftype( $_[0] ) eq 'HASH'; 91} 92 93sub is_coderef($) { 94 no warnings 'uninitialized'; 95 Carp::croak("Too many arguments for is_coderef") if @_ > 1; 96 Scalar::Util::reftype( $_[0] ) eq 'CODE'; 97} 98 99sub is_regexpref($) { 100 no warnings 'uninitialized'; 101 Carp::croak("Too many arguments for is_regexpref") if @_ > 1; 102 _RX_NEEDS_MAGIC ? _is_regexp( $_[0] ) 103 : re::is_regexp( $_[0] ); 104} 105 106sub is_globref($) { 107 no warnings 'uninitialized'; 108 Carp::croak("Too many arguments for is_globref") if @_ > 1; 109 Scalar::Util::reftype( $_[0] ) eq 'GLOB'; 110} 111 112sub is_formatref($) { 113 _FORMAT_REFS_WORK 114 or 115 Carp::croak("is_formatref() isn't available on Perl 5.6.x and under"); 116 117 no warnings 'uninitialized'; 118 Carp::croak("Too many arguments for is_formatref") if @_ > 1; 119 Scalar::Util::reftype( $_[0] ) eq 'FORMAT'; 120} 121 122sub is_ioref($) { 123 no warnings 'uninitialized'; 124 Carp::croak("Too many arguments for is_ioref") if @_ > 1; 125 Scalar::Util::reftype( $_[0] ) eq 'IO'; 126} 127 128sub is_refref($) { 129 no warnings 'uninitialized'; 130 Carp::croak("Too many arguments for is_refref") if @_ > 1; 131 Scalar::Util::reftype( $_[0] ) eq 'REF'; 132} 133 134# ---- 135# -- is_plain_* 136# ---- 137 138sub is_plain_ref($) { 139 Carp::croak("Too many arguments for is_plain_ref") if @_ > 1; 140 ref $_[0] && !Scalar::Util::blessed( $_[0] ); 141} 142 143sub is_plain_scalarref($) { 144 Carp::croak("Too many arguments for is_plain_scalarref") if @_ > 1; 145 !defined Scalar::Util::blessed( $_[0] ) 146 && ( ref( $_[0] ) eq 'SCALAR' || ref( $_[0] ) eq 'VSTRING' ); 147} 148 149sub is_plain_arrayref($) { 150 Carp::croak("Too many arguments for is_plain_arrayref") if @_ > 1; 151 !defined Scalar::Util::blessed( $_[0] ) 152 && ref( $_[0] ) eq 'ARRAY'; 153} 154 155sub is_plain_hashref($) { 156 Carp::croak("Too many arguments for is_plain_hashref") if @_ > 1; 157 !defined Scalar::Util::blessed( $_[0] ) 158 && ref( $_[0] ) eq 'HASH'; 159} 160 161sub is_plain_coderef($) { 162 Carp::croak("Too many arguments for is_plain_coderef") if @_ > 1; 163 !defined Scalar::Util::blessed( $_[0] ) 164 && ref( $_[0] ) eq 'CODE'; 165} 166 167sub is_plain_globref($) { 168 Carp::croak("Too many arguments for is_plain_globref") if @_ > 1; 169 !defined Scalar::Util::blessed( $_[0] ) 170 && ref( $_[0] ) eq 'GLOB'; 171} 172 173sub is_plain_formatref($) { 174 _FORMAT_REFS_WORK 175 or 176 Carp::croak("is_plain_formatref() isn't available on Perl 5.6.x and under"); 177 178 Carp::croak("Too many arguments for is_plain_formatref") if @_ > 1; 179 !defined Scalar::Util::blessed( $_[0] ) 180 && ref( $_[0] ) eq 'FORMAT'; 181} 182 183sub is_plain_refref($) { 184 Carp::croak("Too many arguments for is_plain_refref") if @_ > 1; 185 !defined Scalar::Util::blessed( $_[0] ) 186 && ref( $_[0] ) eq 'REF'; 187} 188 189# ---- 190# -- is_blessed_* 191# ---- 192 193sub is_blessed_ref($) { 194 Carp::croak("Too many arguments for is_blessed_ref") if @_ > 1; 195 defined Scalar::Util::blessed( $_[0] ); 196} 197 198sub is_blessed_scalarref($) { 199 Carp::croak("Too many arguments for is_blessed_scalarref") if @_ > 1; 200 my $reftype = Scalar::Util::reftype( $_[0] ); 201 defined Scalar::Util::blessed( $_[0] ) 202 && ($reftype eq 'SCALAR' || $reftype eq 'VSTRING') 203 && (!_RX_NEEDS_MAGIC || !_is_regexp( $_[0] )); 204} 205 206sub is_blessed_arrayref($) { 207 Carp::croak("Too many arguments for is_blessed_arrayref") if @_ > 1; 208 defined Scalar::Util::blessed( $_[0] ) 209 && Scalar::Util::reftype( $_[0] ) eq 'ARRAY'; 210} 211 212sub is_blessed_hashref($) { 213 Carp::croak("Too many arguments for is_blessed_hashref") if @_ > 1; 214 defined Scalar::Util::blessed( $_[0] ) 215 && Scalar::Util::reftype( $_[0] ) eq 'HASH'; 216} 217 218sub is_blessed_coderef($) { 219 Carp::croak("Too many arguments for is_blessed_coderef") if @_ > 1; 220 defined Scalar::Util::blessed( $_[0] ) 221 && Scalar::Util::reftype( $_[0] ) eq 'CODE'; 222} 223 224sub is_blessed_globref($) { 225 Carp::croak("Too many arguments for is_blessed_globref") if @_ > 1; 226 defined Scalar::Util::blessed( $_[0] ) 227 && Scalar::Util::reftype( $_[0] ) eq 'GLOB'; 228} 229 230sub is_blessed_formatref($) { 231 _FORMAT_REFS_WORK 232 or 233 Carp::croak("is_blessed_formatref() isn't available on Perl 5.6.x and under"); 234 235 Carp::croak("Too many arguments for is_blessed_formatref") if @_ > 1; 236 defined Scalar::Util::blessed( $_[0] ) 237 && Scalar::Util::reftype( $_[0] ) eq 'FORMAT'; 238} 239 240sub is_blessed_refref($) { 241 Carp::croak("Too many arguments for is_blessed_refref") if @_ > 1; 242 defined Scalar::Util::blessed( $_[0] ) 243 && Scalar::Util::reftype( $_[0] ) eq 'REF'; 244} 245 2461; 247 248__END__ 249 250=pod 251 252=encoding UTF-8 253 254=head1 NAME 255 256Ref::Util::PP - pure-Perl version of Ref::Util 257 258=head1 VERSION 259 260version 0.204 261 262=head1 SYNOPSIS 263 264 use Ref::Util; 265 266=head1 DESCRIPTION 267 268This module provides a pure-Perl implementation of the functions in 269L<Ref::Util>. 270 271Ref::Util:PP will be used automatically if Ref::Util is installed on a 272system with no C compiler, but you can force its usage by setting either 273C<$Ref::Util::IMPLEMENTATION> or the C<PERL_REF_UTIL_IMPLEMENTATION> 274environment variable to C<PP>. 275 276=head1 AUTHORS 277 278=over 4 279 280=item * 281 282Sawyer X <xsawyerx@cpan.org> 283 284=item * 285 286Aaron Crane <arc@cpan.org> 287 288=item * 289 290Vikenty Fesunov <vyf@cpan.org> 291 292=item * 293 294Gonzalo Diethelm <gonzus@cpan.org> 295 296=item * 297 298Karen Etheridge <ether@cpan.org> 299 300=back 301 302=head1 COPYRIGHT AND LICENSE 303 304This software is Copyright (c) 2017 by Sawyer X. 305 306This is free software, licensed under: 307 308 The MIT (X11) License 309 310=cut 311