Debian Codegraph @

upstream/debian-unstable/man3/Scalar::Util.3perl

raw
1 .\" -*- mode: troff; coding: utf-8 -*-
2 .\" Automatically generated by Pod::Man 5.0102 (Pod::Simple 3.45)
3 .\"
4 .\" Standard preamble:
5 .\" ========================================================================
6 .de Sp \" Vertical space (when we can't use .PP)
7 .if t .sp .5v
8 .if n .sp
9 ..
10 .de Vb \" Begin verbatim text
11 .ft CW
12 .nf
13 .ne \\$1
14 ..
15 .de Ve \" End verbatim text
16 .ft R
17 .fi
18 ..
19 .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>.
20 .ie n \{\
21 . ds C` ""
22 . ds C' ""
23 'br\}
24 .el\{\
25 . ds C`
26 . ds C'
27 'br\}
28 .\"
29 .\" Escape single quotes in literal strings from groff's Unicode transform.
30 .ie \n(.g .ds Aq \(aq
31 .el .ds Aq '
32 .\"
33 .\" If the F register is >0, we'll generate index entries on stderr for
34 .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
35 .\" entries marked with X<> in POD. Of course, you'll have to process the
36 .\" output yourself in some meaningful fashion.
37 .\"
38 .\" Avoid warning from groff about undefined register 'F'.
39 .de IX
40 ..
41 .nr rF 0
42 .if \n(.g .if rF .nr rF 1
43 .if (\n(rF:(\n(.g==0)) \{\
44 . if \nF \{\
45 . de IX
46 . tm Index:\\$1\t\\n%\t"\\$2"
47 ..
48 . if !\nF==2 \{\
49 . nr % 0
50 . nr F 2
51 . \}
52 . \}
53 .\}
54 .rr rF
55 .\" ========================================================================
56 .\"
57 .IX Title "Scalar::Util 3perl"
58 .TH Scalar::Util 3perl 2025-11-16 "perl v5.40.1" "Perl Programmers Reference Guide"
59 .\" For nroff, turn off justification. Always turn off hyphenation; it makes
60 .\" way too many mistakes in technical documents.
61 .if n .ad l
62 .nh
63 .SH NAME
64 Scalar::Util \- A selection of general\-utility scalar subroutines
65 .SH SYNOPSIS
66 .IX Header "SYNOPSIS"
67 .Vb 4
68 \& use Scalar::Util qw(blessed dualvar isdual readonly refaddr reftype
69 \& tainted weaken isweak isvstring looks_like_number
70 \& set_prototype);
71 \& # and other useful utils appearing below
72 .Ve
73 .SH DESCRIPTION
74 .IX Header "DESCRIPTION"
75 \&\f(CW\*(C`Scalar::Util\*(C'\fR 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 would be so small that
78 being individual extensions would be wasteful.
79 .PP
80 By default \f(CW\*(C`Scalar::Util\*(C'\fR does not export any subroutines.
81 .ie n .SS "Core Perl ""builtin"" Functions"
82 .el .SS "Core Perl \f(CWbuiltin\fP Functions"
83 .IX Subsection "Core Perl builtin Functions"
84 Many functions in this module have served as the inspiration for a new
85 experimental facility in recent versions of Perl. From various development
86 versions, starting at 5.35.7, equivalent functions to many of these utilities
87 are available in the \f(CW\*(C`builtin::\*(C'\fR package.
88 .PP
89 .Vb 1
90 \& use Scalar::Util qw(blessed);
91 \&
92 \& $class = blessed $obj;
93 \&
94 \& $class = builtin::blessed $obj; # equivalent
95 .Ve
96 .PP
97 For more information, see the documentation on builtin.
98 .SH "FUNCTIONS FOR REFERENCES"
99 .IX Header "FUNCTIONS FOR REFERENCES"
100 The following functions all perform some useful activity on reference values.
101 .SS blessed
102 .IX Subsection "blessed"
103 .Vb 1
104 \& my $pkg = blessed( $ref );
105 .Ve
106 .PP
107 If \f(CW$ref\fR is a blessed reference, the name of the package that it is blessed
108 into is returned. Otherwise \f(CW\*(C`undef\*(C'\fR is returned.
109 .PP
110 .Vb 2
111 \& $scalar = "foo";
112 \& $class = blessed $scalar; # undef
113 \&
114 \& $ref = [];
115 \& $class = blessed $ref; # undef
116 \&
117 \& $obj = bless [], "Foo";
118 \& $class = blessed $obj; # "Foo"
119 .Ve
120 .PP
121 Take care when using this function simply as a truth test (such as in
122 \&\f(CW\*(C`if(blessed $ref)...\*(C'\fR) because the package name \f(CW"0"\fR is defined yet false.
123 .PP
124 \&\fISince Perl version 5.35.7\fR an equivalent function is available as
125 \&\f(CW\*(C`builtin::blessed\*(C'\fR.
126 .SS refaddr
127 .IX Subsection "refaddr"
128 .Vb 1
129 \& my $addr = refaddr( $ref );
130 .Ve
131 .PP
132 If \f(CW$ref\fR is reference, the internal memory address of the referenced value is
133 returned as a plain integer. Otherwise \f(CW\*(C`undef\*(C'\fR is returned.
134 .PP
135 .Vb 3
136 \& $addr = refaddr "string"; # undef
137 \& $addr = refaddr \e$var; # eg 12345678
138 \& $addr = refaddr []; # eg 23456784
139 \&
140 \& $obj = bless {}, "Foo";
141 \& $addr = refaddr $obj; # eg 88123488
142 .Ve
143 .PP
144 \&\fISince Perl version 5.35.7\fR an equivalent function is available as
145 \&\f(CW\*(C`builtin::refaddr\*(C'\fR.
146 .SS reftype
147 .IX Subsection "reftype"
148 .Vb 1
149 \& my $type = reftype( $ref );
150 .Ve
151 .PP
152 If \f(CW$ref\fR is a reference, the basic Perl type of the variable referenced is
153 returned as a plain string (such as \f(CW\*(C`ARRAY\*(C'\fR or \f(CW\*(C`HASH\*(C'\fR). Otherwise \f(CW\*(C`undef\*(C'\fR
154 is returned.
155 .PP
156 .Vb 3
157 \& $type = reftype "string"; # undef
158 \& $type = reftype \e$var; # SCALAR
159 \& $type = reftype []; # ARRAY
160 \&
161 \& $obj = bless {}, "Foo";
162 \& $type = reftype $obj; # HASH
163 .Ve
164 .PP
165 Note that for internal reasons, all precompiled regexps (\f(CW\*(C`qr/.../\*(C'\fR) are
166 blessed references; thus \f(CWref()\fR returns the package name string \f(CW"Regexp"\fR
167 on these but \f(CWreftype()\fR will return the underlying C structure type of
168 \&\f(CW"REGEXP"\fR in all capitals.
169 .PP
170 \&\fISince Perl version 5.35.7\fR an equivalent function is available as
171 \&\f(CW\*(C`builtin::reftype\*(C'\fR.
172 .SS weaken
173 .IX Subsection "weaken"
174 .Vb 1
175 \& weaken( $ref );
176 .Ve
177 .PP
178 The lvalue \f(CW$ref\fR will be turned into a weak reference. This means that it
179 will not hold a reference count on the object it references. Also, when the
180 reference count on that object reaches zero, the reference will be set to
181 undef. This function mutates the lvalue passed as its argument and returns no
182 value.
183 .PP
184 This is useful for keeping copies of references, but you don't want to prevent
185 the object being DESTROY-ed at its usual time.
186 .PP
187 .Vb 6
188 \& {
189 \& my $var;
190 \& $ref = \e$var;
191 \& weaken($ref); # Make $ref a weak reference
192 \& }
193 \& # $ref is now undef
194 .Ve
195 .PP
196 Note that if you take a copy of a scalar with a weakened reference, the copy
197 will be a strong reference.
198 .PP
199 .Vb 4
200 \& my $var;
201 \& my $foo = \e$var;
202 \& weaken($foo); # Make $foo a weak reference
203 \& my $bar = $foo; # $bar is now a strong reference
204 .Ve
205 .PP
206 This may be less obvious in other situations, such as \f(CWgrep()\fR, for instance
207 when grepping through a list of weakened references to objects that may have
208 been destroyed already:
209 .PP
210 .Vb 1
211 \& @object = grep { defined } @object;
212 .Ve
213 .PP
214 This will indeed remove all references to destroyed objects, but the remaining
215 references to objects will be strong, causing the remaining objects to never be
216 destroyed because there is now always a strong reference to them in the \f(CW@object\fR
217 array.
218 .PP
219 \&\fISince Perl version 5.35.7\fR an equivalent function is available as
220 \&\f(CW\*(C`builtin::weaken\*(C'\fR.
221 .SS unweaken
222 .IX Subsection "unweaken"
223 .Vb 1
224 \& unweaken( $ref );
225 .Ve
226 .PP
227 \&\fISince version 1.36.\fR
228 .PP
229 The lvalue \f(CW\*(C`REF\*(C'\fR will be turned from a weak reference back into a normal
230 (strong) reference again. This function mutates the lvalue passed as its
231 argument and returns no value. This undoes the action performed by
232 "weaken".
233 .PP
234 This function is slightly neater and more convenient than the
235 otherwise-equivalent code
236 .PP
237 .Vb 3
238 \& my $tmp = $REF;
239 \& undef $REF;
240 \& $REF = $tmp;
241 .Ve
242 .PP
243 (because in particular, simply assigning a weak reference back to itself does
244 not work to unweaken it; \f(CW\*(C`$REF = $REF\*(C'\fR does not work).
245 .PP
246 \&\fISince Perl version 5.35.7\fR an equivalent function is available as
247 \&\f(CW\*(C`builtin::unweaken\*(C'\fR.
248 .SS isweak
249 .IX Subsection "isweak"
250 .Vb 1
251 \& my $weak = isweak( $ref );
252 .Ve
253 .PP
254 Returns true if \f(CW$ref\fR is a weak reference.
255 .PP
256 .Vb 4
257 \& $ref = \e$foo;
258 \& $weak = isweak($ref); # false
259 \& weaken($ref);
260 \& $weak = isweak($ref); # true
261 .Ve
262 .PP
263 \&\fBNOTE\fR: Copying a weak reference creates a normal, strong, reference.
264 .PP
265 .Vb 2
266 \& $copy = $ref;
267 \& $weak = isweak($copy); # false
268 .Ve
269 .PP
270 \&\fISince Perl version 5.35.7\fR an equivalent function is available as
271 \&\f(CW\*(C`builtin::is_weak\*(C'\fR.
272 .SH "OTHER FUNCTIONS"
273 .IX Header "OTHER FUNCTIONS"
274 .SS dualvar
275 .IX Subsection "dualvar"
276 .Vb 1
277 \& my $var = dualvar( $num, $string );
278 .Ve
279 .PP
280 Returns a scalar that has the value \f(CW$num\fR in a numeric context and the value
281 \&\f(CW$string\fR in a string context.
282 .PP
283 .Vb 3
284 \& $foo = dualvar 10, "Hello";
285 \& $num = $foo + 2; # 12
286 \& $str = $foo . " world"; # Hello world
287 .Ve
288 .SS isdual
289 .IX Subsection "isdual"
290 .Vb 1
291 \& my $dual = isdual( $var );
292 .Ve
293 .PP
294 \&\fISince version 1.26.\fR
295 .PP
296 If \f(CW$var\fR is a scalar that has both numeric and string values, the result is
297 true.
298 .PP
299 .Vb 2
300 \& $foo = dualvar 86, "Nix";
301 \& $dual = isdual($foo); # true
302 .Ve
303 .PP
304 Note that a scalar can be made to have both string and numeric content through
305 standard operations:
306 .PP
307 .Vb 4
308 \& $foo = "10";
309 \& $dual = isdual($foo); # false
310 \& $bar = $foo + 0;
311 \& $dual = isdual($foo); # true
312 .Ve
313 .PP
314 The \f(CW$!\fR variable is commonly dual-valued, though it is also magical in other
315 ways:
316 .PP
317 .Vb 3
318 \& $! = 1;
319 \& $dual = isdual($!); # true
320 \& print("$!\en"); # "Operation not permitted"
321 .Ve
322 .PP
323 \&\fBCAUTION\fR: This function is not as useful as it may seem. Dualvars are not a
324 distinct concept in Perl, but a standard internal construct of all scalar
325 values. Almost any value could be considered as a dualvar by this function
326 through the course of normal operations.
327 .SS isvstring
328 .IX Subsection "isvstring"
329 .Vb 1
330 \& my $vstring = isvstring( $var );
331 .Ve
332 .PP
333 If \f(CW$var\fR is a scalar which was coded as a vstring, the result is true.
334 .PP
335 .Vb 3
336 \& $vs = v49.46.48;
337 \& $fmt = isvstring($vs) ? "%vd" : "%s"; #true
338 \& printf($fmt,$vs);
339 .Ve
340 .SS looks_like_number
341 .IX Subsection "looks_like_number"
342 .Vb 1
343 \& my $isnum = looks_like_number( $var );
344 .Ve
345 .PP
346 Returns true if perl thinks \f(CW$var\fR is a number. See
347 "looks_like_number" in perlapi.
348 .SS openhandle
349 .IX Subsection "openhandle"
350 .Vb 1
351 \& my $fh = openhandle( $fh );
352 .Ve
353 .PP
354 Returns \f(CW$fh\fR itself, if \f(CW$fh\fR may be used as a filehandle and is open, or if
355 it is a tied handle. Otherwise \f(CW\*(C`undef\*(C'\fR is returned.
356 .PP
357 .Vb 4
358 \& $fh = openhandle(*STDIN); # \e*STDIN
359 \& $fh = openhandle(\e*STDIN); # \e*STDIN
360 \& $fh = openhandle(*NOTOPEN); # undef
361 \& $fh = openhandle("scalar"); # undef
362 .Ve
363 .SS readonly
364 .IX Subsection "readonly"
365 .Vb 1
366 \& my $ro = readonly( $var );
367 .Ve
368 .PP
369 Returns true if \f(CW$var\fR is readonly.
370 .PP
371 .Vb 1
372 \& sub foo { readonly($_[0]) }
373 \&
374 \& $readonly = foo($bar); # false
375 \& $readonly = foo(0); # true
376 .Ve
377 .SS set_prototype
378 .IX Subsection "set_prototype"
379 .Vb 1
380 \& my $code = set_prototype( $code, $prototype );
381 .Ve
382 .PP
383 Sets the prototype of the function given by the \f(CW$code\fR reference, or deletes
384 it if \f(CW$prototype\fR is \f(CW\*(C`undef\*(C'\fR. Returns the \f(CW$code\fR reference itself.
385 .PP
386 .Vb 1
387 \& set_prototype \e&foo, \*(Aq$$\*(Aq;
388 .Ve
389 .SS tainted
390 .IX Subsection "tainted"
391 .Vb 1
392 \& my $t = tainted( $var );
393 .Ve
394 .PP
395 Return true if \f(CW$var\fR is tainted.
396 .PP
397 .Vb 2
398 \& $taint = tainted("constant"); # false
399 \& $taint = tainted($ENV{PWD}); # true if running under \-T
400 .Ve
401 .SH DIAGNOSTICS
402 .IX Header "DIAGNOSTICS"
403 Module use may give one of the following errors during import.
404 .IP "Vstrings are not implemented in this version of perl" 4
405 .IX Item "Vstrings are not implemented in this version of perl"
406 The version of perl that you are using does not implement Vstrings, to use
407 "isvstring" you will need to use a newer release of perl.
408 .SH "KNOWN BUGS"
409 .IX Header "KNOWN BUGS"
410 There is a bug in perl5.6.0 with UV's that are >= 1<<31. This will
411 show up as tests 8 and 9 of dualvar.t failing
412 .SH "SEE ALSO"
413 .IX Header "SEE ALSO"
414 List::Util
415 .SH COPYRIGHT
416 .IX Header "COPYRIGHT"
417 Copyright (c) 1997\-2007 Graham Barr <[email protected]>. All rights reserved.
418 This program is free software; you can redistribute it and/or modify it
419 under the same terms as Perl itself.
420 .PP
421 Additionally "weaken" and "isweak" which are
422 .PP
423 Copyright (c) 1999 Tuomas J. Lukka <[email protected]>. All rights reserved.
424 This program is free software; you can redistribute it and/or modify it
425 under the same terms as perl itself.
426 .PP
427 Copyright (C) 2004, 2008 Matthijs van Duin. All rights reserved.
428 Copyright (C) 2014 cPanel Inc. All rights reserved.
429 This program is free software; you can redistribute it and/or modify
430 it under the same terms as Perl itself.