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.
5 # Maintained since 2013 by Paul Evans <leonerd@leonerd.org.uk>
12 our @ISA = qw(Exporter);
14 blessed refaddr reftype weaken unweaken isweak
16 dualvar isdual isvstring looks_like_number openhandle readonly set_prototype
19 our $VERSION = "1.42_02";
20 $VERSION = eval $VERSION;
22 require List::Util; # List::Util loads the XS
23 List::Util->VERSION( $VERSION ); # Ensure we got the right XS version (RT#100863)
27 unless (defined &weaken) {
28 push @EXPORT_FAIL, qw(weaken);
30 unless (defined &isweak) {
31 push @EXPORT_FAIL, qw(isweak isvstring);
33 unless (defined &isvstring) {
34 push @EXPORT_FAIL, qw(isvstring);
38 if (grep { /^(?:weaken|isweak)$/ } @_ ) {
40 Carp::croak("Weak references are not implemented in the version of perl");
43 if (grep { /^isvstring$/ } @_ ) {
45 Carp::croak("Vstrings are not implemented in the version of perl");
51 # set_prototype has been moved to Sub::Util with a different interface
54 my ( $code, $proto ) = @_;
55 return Sub::Util::set_prototype( $proto, $code );
64 Scalar::Util - A selection of general-utility scalar subroutines
68 use Scalar::Util qw(blessed dualvar isdual readonly refaddr reftype
69 tainted weaken isweak isvstring looks_like_number
71 # and other useful utils appearing below
75 C<Scalar::Util> contains a selection of subroutines that people have expressed
76 would be nice to have in the perl core, but the usage would not really be high
77 enough to warrant the use of a keyword, and the size so small such that being
78 individual extensions would be wasteful.
80 By default C<Scalar::Util> does not export any subroutines.
84 =head1 FUNCTIONS FOR REFERENCES
86 The following functions all perform some useful activity on reference values.
90 my $pkg = blessed( $ref );
92 If C<$ref> is a blessed reference the name of the package that it is blessed
93 into is returned. Otherwise C<undef> is returned.
96 $class = blessed $scalar; # undef
99 $class = blessed $ref; # undef
101 $obj = bless [], "Foo";
102 $class = blessed $obj; # "Foo"
104 Take care when using this function simply as a truth test (such as in
105 C<if(blessed $ref)...>) because the package name C<"0"> is defined yet false.
109 my $addr = refaddr( $ref );
111 If C<$ref> is reference the internal memory address of the referenced value is
112 returned as a plain integer. Otherwise C<undef> is returned.
114 $addr = refaddr "string"; # undef
115 $addr = refaddr \$var; # eg 12345678
116 $addr = refaddr []; # eg 23456784
118 $obj = bless {}, "Foo";
119 $addr = refaddr $obj; # eg 88123488
123 my $type = reftype( $ref );
125 If C<$ref> is a reference the basic Perl type of the variable referenced is
126 returned as a plain string (such as C<ARRAY> or C<HASH>). Otherwise C<undef>
129 $type = reftype "string"; # undef
130 $type = reftype \$var; # SCALAR
131 $type = reftype []; # ARRAY
133 $obj = bless {}, "Foo";
134 $type = reftype $obj; # HASH
140 The lvalue C<$ref> will be turned into a weak reference. This means that it
141 will not hold a reference count on the object it references. Also when the
142 reference count on that object reaches zero, the reference will be set to
143 undef. This function mutates the lvalue passed as its argument and returns no
146 This is useful for keeping copies of references, but you don't want to prevent
147 the object being DESTROY-ed at its usual time.
152 weaken($ref); # Make $ref a weak reference
156 Note that if you take a copy of a scalar with a weakened reference, the copy
157 will be a strong reference.
161 weaken($foo); # Make $foo a weak reference
162 my $bar = $foo; # $bar is now a strong reference
164 This may be less obvious in other situations, such as C<grep()>, for instance
165 when grepping through a list of weakened references to objects that may have
166 been destroyed already:
168 @object = grep { defined } @object;
170 This will indeed remove all references to destroyed objects, but the remaining
171 references to objects will be strong, causing the remaining objects to never be
172 destroyed because there is now always a strong reference to them in the @object
179 I<Since version 1.36.>
181 The lvalue C<REF> will be turned from a weak reference back into a normal
182 (strong) reference again. This function mutates the lvalue passed as its
183 argument and returns no value. This undoes the action performed by
186 This function is slightly neater and more convenient than the
187 otherwise-equivalent code
193 (because in particular, simply assigning a weak reference back to itself does
194 not work to unweaken it; C<$REF = $REF> does not work).
198 my $weak = isweak( $ref );
200 Returns true if C<$ref> is a weak reference.
203 $weak = isweak($ref); # false
205 $weak = isweak($ref); # true
207 B<NOTE>: Copying a weak reference creates a normal, strong, reference.
210 $weak = isweak($copy); # false
212 =head1 OTHER FUNCTIONS
216 my $var = dualvar( $num, $string );
218 Returns a scalar that has the value C<$num> in a numeric context and the value
219 C<$string> in a string context.
221 $foo = dualvar 10, "Hello";
222 $num = $foo + 2; # 12
223 $str = $foo . " world"; # Hello world
227 my $dual = isdual( $var );
229 I<Since version 1.26.>
231 If C<$var> is a scalar that has both numeric and string values, the result is
234 $foo = dualvar 86, "Nix";
235 $dual = isdual($foo); # true
237 Note that a scalar can be made to have both string and numeric content through
241 $dual = isdual($foo); # false
243 $dual = isdual($foo); # true
245 Note that although C<$!> appears to be dual-valued variable, it is actually
246 implemented using a tied scalar:
249 print("$!\n"); # "Operation not permitted"
250 $dual = isdual($!); # false
252 You can capture its numeric and string content using:
254 $err = dualvar $!, $!;
255 $dual = isdual($err); # true
259 my $vstring = isvstring( $var );
261 If C<$var> is a scalar which was coded as a vstring the result is true.
264 $fmt = isvstring($vs) ? "%vd" : "%s"; #true
267 =head2 looks_like_number
269 my $isnum = looks_like_number( $var );
271 Returns true if perl thinks C<$var> is a number. See
272 L<perlapi/looks_like_number>.
276 my $fh = openhandle( $fh );
278 Returns C<$fh> itself if C<$fh> may be used as a filehandle and is open, or is
279 is a tied handle. Otherwise C<undef> is returned.
281 $fh = openhandle(*STDIN); # \*STDIN
282 $fh = openhandle(\*STDIN); # \*STDIN
283 $fh = openhandle(*NOTOPEN); # undef
284 $fh = openhandle("scalar"); # undef
288 my $ro = readonly( $var );
290 Returns true if C<$var> is readonly.
292 sub foo { readonly($_[0]) }
294 $readonly = foo($bar); # false
295 $readonly = foo(0); # true
299 my $code = set_prototype( $code, $prototype );
301 Sets the prototype of the function given by the C<$code> reference, or deletes
302 it if C<$prototype> is C<undef>. Returns the C<$code> reference itself.
304 set_prototype \&foo, '$$';
308 my $t = tainted( $var );
310 Return true if C<$var> is tainted.
312 $taint = tainted("constant"); # false
313 $taint = tainted($ENV{PWD}); # true if running under -T
317 Module use may give one of the following errors during import.
321 =item Weak references are not implemented in the version of perl
323 The version of perl that you are using does not implement weak references, to
324 use L</isweak> or L</weaken> you will need to use a newer release of perl.
326 =item Vstrings are not implemented in the version of perl
328 The version of perl that you are using does not implement Vstrings, to use
329 L</isvstring> you will need to use a newer release of perl.
331 =item C<NAME> is only available with the XS version of Scalar::Util
333 C<Scalar::Util> contains both perl and C implementations of many of its
334 functions so that those without access to a C compiler may still use it.
335 However some of the functions are only available when a C compiler was
336 available to compile the XS version of the extension.
338 At present that list is: weaken, isweak, dualvar, isvstring, set_prototype
344 There is a bug in perl5.6.0 with UV's that are >= 1<<31. This will
345 show up as tests 8 and 9 of dualvar.t failing
353 Copyright (c) 1997-2007 Graham Barr <gbarr@pobox.com>. All rights reserved.
354 This program is free software; you can redistribute it and/or modify it
355 under the same terms as Perl itself.
357 Additionally L</weaken> and L</isweak> which are
359 Copyright (c) 1999 Tuomas J. Lukka <lukka@iki.fi>. All rights reserved.
360 This program is free software; you can redistribute it and/or modify it
361 under the same terms as perl itself.
363 Copyright (C) 2004, 2008 Matthijs van Duin. All rights reserved.
364 Copyright (C) 2014 cPanel Inc. All rights reserved.
365 This program is free software; you can redistribute it and/or modify
366 it under the same terms as Perl itself.