Debian Codegraph @

upstream/mageia-cauldron/man3pm/Scalar::Util.3pm

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