1# Copyright (c) 1997-2007 Graham Barr <gbarr@pobox.com>. All rights reserved. 2# This program is free software; you can redistribute it and/or 3# modify it under the same terms as Perl itself. 4# 5# Maintained since 2013 by Paul Evans <leonerd@leonerd.org.uk> 6 7package Scalar::Util; 8 9use strict; 10use warnings; 11require Exporter; 12 13our @ISA = qw(Exporter); 14our @EXPORT_OK = qw( 15 blessed refaddr reftype weaken unweaken isweak 16 17 dualvar isdual isvstring looks_like_number openhandle readonly set_prototype 18 tainted 19); 20our $VERSION = "1.55"; 21$VERSION =~ tr/_//d; 22 23require List::Util; # List::Util loads the XS 24List::Util->VERSION( $VERSION ); # Ensure we got the right XS version (RT#100863) 25 26our @EXPORT_FAIL; 27 28unless (defined &weaken) { 29 push @EXPORT_FAIL, qw(weaken); 30} 31unless (defined &isweak) { 32 push @EXPORT_FAIL, qw(isweak isvstring); 33} 34unless (defined &isvstring) { 35 push @EXPORT_FAIL, qw(isvstring); 36} 37 38sub export_fail { 39 if (grep { /^(?:weaken|isweak)$/ } @_ ) { 40 require Carp; 41 Carp::croak("Weak references are not implemented in the version of perl"); 42 } 43 44 if (grep { /^isvstring$/ } @_ ) { 45 require Carp; 46 Carp::croak("Vstrings are not implemented in the version of perl"); 47 } 48 49 @_; 50} 51 52# set_prototype has been moved to Sub::Util with a different interface 53sub set_prototype(&$) 54{ 55 my ( $code, $proto ) = @_; 56 return Sub::Util::set_prototype( $proto, $code ); 57} 58 591; 60 61__END__ 62 63=head1 NAME 64 65Scalar::Util - A selection of general-utility scalar subroutines 66 67=head1 SYNOPSIS 68 69 use Scalar::Util qw(blessed dualvar isdual readonly refaddr reftype 70 tainted weaken isweak isvstring looks_like_number 71 set_prototype); 72 # and other useful utils appearing below 73 74=head1 DESCRIPTION 75 76C<Scalar::Util> contains a selection of subroutines that people have expressed 77would be nice to have in the perl core, but the usage would not really be high 78enough to warrant the use of a keyword, and the size would be so small that 79being individual extensions would be wasteful. 80 81By default C<Scalar::Util> does not export any subroutines. 82 83=cut 84 85=head1 FUNCTIONS FOR REFERENCES 86 87The following functions all perform some useful activity on reference values. 88 89=head2 blessed 90 91 my $pkg = blessed( $ref ); 92 93If C<$ref> is a blessed reference, the name of the package that it is blessed 94into is returned. Otherwise C<undef> is returned. 95 96 $scalar = "foo"; 97 $class = blessed $scalar; # undef 98 99 $ref = []; 100 $class = blessed $ref; # undef 101 102 $obj = bless [], "Foo"; 103 $class = blessed $obj; # "Foo" 104 105Take care when using this function simply as a truth test (such as in 106C<if(blessed $ref)...>) because the package name C<"0"> is defined yet false. 107 108=head2 refaddr 109 110 my $addr = refaddr( $ref ); 111 112If C<$ref> is reference, the internal memory address of the referenced value is 113returned as a plain integer. Otherwise C<undef> is returned. 114 115 $addr = refaddr "string"; # undef 116 $addr = refaddr \$var; # eg 12345678 117 $addr = refaddr []; # eg 23456784 118 119 $obj = bless {}, "Foo"; 120 $addr = refaddr $obj; # eg 88123488 121 122=head2 reftype 123 124 my $type = reftype( $ref ); 125 126If C<$ref> is a reference, the basic Perl type of the variable referenced is 127returned as a plain string (such as C<ARRAY> or C<HASH>). Otherwise C<undef> 128is returned. 129 130 $type = reftype "string"; # undef 131 $type = reftype \$var; # SCALAR 132 $type = reftype []; # ARRAY 133 134 $obj = bless {}, "Foo"; 135 $type = reftype $obj; # HASH 136 137Note that for internal reasons, all precompiled regexps (C<qr/.../>) are 138blessed references; thus C<ref()> returns the package name string C<"Regexp"> 139on these but C<reftype()> will return the underlying C structure type of 140C<"REGEXP"> in all capitals. 141 142=head2 weaken 143 144 weaken( $ref ); 145 146The lvalue C<$ref> will be turned into a weak reference. This means that it 147will not hold a reference count on the object it references. Also, when the 148reference count on that object reaches zero, the reference will be set to 149undef. This function mutates the lvalue passed as its argument and returns no 150value. 151 152This is useful for keeping copies of references, but you don't want to prevent 153the object being DESTROY-ed at its usual time. 154 155 { 156 my $var; 157 $ref = \$var; 158 weaken($ref); # Make $ref a weak reference 159 } 160 # $ref is now undef 161 162Note that if you take a copy of a scalar with a weakened reference, the copy 163will be a strong reference. 164 165 my $var; 166 my $foo = \$var; 167 weaken($foo); # Make $foo a weak reference 168 my $bar = $foo; # $bar is now a strong reference 169 170This may be less obvious in other situations, such as C<grep()>, for instance 171when grepping through a list of weakened references to objects that may have 172been destroyed already: 173 174 @object = grep { defined } @object; 175 176This will indeed remove all references to destroyed objects, but the remaining 177references to objects will be strong, causing the remaining objects to never be 178destroyed because there is now always a strong reference to them in the @object 179array. 180 181=head2 unweaken 182 183 unweaken( $ref ); 184 185I<Since version 1.36.> 186 187The lvalue C<REF> will be turned from a weak reference back into a normal 188(strong) reference again. This function mutates the lvalue passed as its 189argument and returns no value. This undoes the action performed by 190L</weaken>. 191 192This function is slightly neater and more convenient than the 193otherwise-equivalent code 194 195 my $tmp = $REF; 196 undef $REF; 197 $REF = $tmp; 198 199(because in particular, simply assigning a weak reference back to itself does 200not work to unweaken it; C<$REF = $REF> does not work). 201 202=head2 isweak 203 204 my $weak = isweak( $ref ); 205 206Returns true if C<$ref> is a weak reference. 207 208 $ref = \$foo; 209 $weak = isweak($ref); # false 210 weaken($ref); 211 $weak = isweak($ref); # true 212 213B<NOTE>: Copying a weak reference creates a normal, strong, reference. 214 215 $copy = $ref; 216 $weak = isweak($copy); # false 217 218=head1 OTHER FUNCTIONS 219 220=head2 dualvar 221 222 my $var = dualvar( $num, $string ); 223 224Returns a scalar that has the value C<$num> in a numeric context and the value 225C<$string> in a string context. 226 227 $foo = dualvar 10, "Hello"; 228 $num = $foo + 2; # 12 229 $str = $foo . " world"; # Hello world 230 231=head2 isdual 232 233 my $dual = isdual( $var ); 234 235I<Since version 1.26.> 236 237If C<$var> is a scalar that has both numeric and string values, the result is 238true. 239 240 $foo = dualvar 86, "Nix"; 241 $dual = isdual($foo); # true 242 243Note that a scalar can be made to have both string and numeric content through 244numeric operations: 245 246 $foo = "10"; 247 $dual = isdual($foo); # false 248 $bar = $foo + 0; 249 $dual = isdual($foo); # true 250 251Note that although C<$!> appears to be a dual-valued variable, it is 252actually implemented as a magical variable inside the interpreter: 253 254 $! = 1; 255 print("$!\n"); # "Operation not permitted" 256 $dual = isdual($!); # false 257 258You can capture its numeric and string content using: 259 260 $err = dualvar $!, $!; 261 $dual = isdual($err); # true 262 263=head2 isvstring 264 265 my $vstring = isvstring( $var ); 266 267If C<$var> is a scalar which was coded as a vstring, the result is true. 268 269 $vs = v49.46.48; 270 $fmt = isvstring($vs) ? "%vd" : "%s"; #true 271 printf($fmt,$vs); 272 273=head2 looks_like_number 274 275 my $isnum = looks_like_number( $var ); 276 277Returns true if perl thinks C<$var> is a number. See 278L<perlapi/looks_like_number>. 279 280=head2 openhandle 281 282 my $fh = openhandle( $fh ); 283 284Returns C<$fh> itself, if C<$fh> may be used as a filehandle and is open, or if 285it is a tied handle. Otherwise C<undef> is returned. 286 287 $fh = openhandle(*STDIN); # \*STDIN 288 $fh = openhandle(\*STDIN); # \*STDIN 289 $fh = openhandle(*NOTOPEN); # undef 290 $fh = openhandle("scalar"); # undef 291 292=head2 readonly 293 294 my $ro = readonly( $var ); 295 296Returns true if C<$var> is readonly. 297 298 sub foo { readonly($_[0]) } 299 300 $readonly = foo($bar); # false 301 $readonly = foo(0); # true 302 303=head2 set_prototype 304 305 my $code = set_prototype( $code, $prototype ); 306 307Sets the prototype of the function given by the C<$code> reference, or deletes 308it if C<$prototype> is C<undef>. Returns the C<$code> reference itself. 309 310 set_prototype \&foo, '$$'; 311 312=head2 tainted 313 314 my $t = tainted( $var ); 315 316Return true if C<$var> is tainted. 317 318 $taint = tainted("constant"); # false 319 $taint = tainted($ENV{PWD}); # true if running under -T 320 321=head1 DIAGNOSTICS 322 323Module use may give one of the following errors during import. 324 325=over 326 327=item Weak references are not implemented in the version of perl 328 329The version of perl that you are using does not implement weak references, to 330use L</isweak> or L</weaken> you will need to use a newer release of perl. 331 332=item Vstrings are not implemented in the version of perl 333 334The version of perl that you are using does not implement Vstrings, to use 335L</isvstring> you will need to use a newer release of perl. 336 337=back 338 339=head1 KNOWN BUGS 340 341There is a bug in perl5.6.0 with UV's that are >= 1<<31. This will 342show up as tests 8 and 9 of dualvar.t failing 343 344=head1 SEE ALSO 345 346L<List::Util> 347 348=head1 COPYRIGHT 349 350Copyright (c) 1997-2007 Graham Barr <gbarr@pobox.com>. All rights reserved. 351This program is free software; you can redistribute it and/or modify it 352under the same terms as Perl itself. 353 354Additionally L</weaken> and L</isweak> which are 355 356Copyright (c) 1999 Tuomas J. Lukka <lukka@iki.fi>. All rights reserved. 357This program is free software; you can redistribute it and/or modify it 358under the same terms as perl itself. 359 360Copyright (C) 2004, 2008 Matthijs van Duin. All rights reserved. 361Copyright (C) 2014 cPanel Inc. All rights reserved. 362This program is free software; you can redistribute it and/or modify 363it under the same terms as Perl itself. 364 365=cut 366