1Scalar::Util(3pm)      Perl Programmers Reference Guide      Scalar::Util(3pm)
2
3
4

NAME

6       Scalar::Util - A selection of general-utility scalar subroutines
7

SYNOPSIS

9           use Scalar::Util qw(blessed dualvar isweak readonly refaddr reftype tainted
10                               weaken isvstring looks_like_number set_prototype);
11                               # and other useful utils appearing below
12

DESCRIPTION

14       "Scalar::Util" contains a selection of subroutines that people have
15       expressed would be nice to have in the perl core, but the usage would
16       not really be high enough to warrant the use of a keyword, and the size
17       so small such that being individual extensions would be wasteful.
18
19       By default "Scalar::Util" does not export any subroutines. The
20       subroutines defined are
21
22       blessed EXPR
23           If EXPR evaluates to a blessed reference the name of the package
24           that it is blessed into is returned. Otherwise "undef" is returned.
25
26              $scalar = "foo";
27              $class  = blessed $scalar;           # undef
28
29              $ref    = [];
30              $class  = blessed $ref;              # undef
31
32              $obj    = bless [], "Foo";
33              $class  = blessed $obj;              # "Foo"
34
35       dualvar NUM, STRING
36           Returns a scalar that has the value NUM in a numeric context and
37           the value STRING in a string context.
38
39               $foo = dualvar 10, "Hello";
40               $num = $foo + 2;                    # 12
41               $str = $foo . " world";             # Hello world
42
43       isvstring EXPR
44           If EXPR is a scalar which was coded as a vstring the result is
45           true.
46
47               $vs   = v49.46.48;
48               $fmt  = isvstring($vs) ? "%vd" : "%s"; #true
49               printf($fmt,$vs);
50
51       isweak EXPR
52           If EXPR is a scalar which is a weak reference the result is true.
53
54               $ref  = \$foo;
55               $weak = isweak($ref);               # false
56               weaken($ref);
57               $weak = isweak($ref);               # true
58
59           NOTE: Copying a weak reference creates a normal, strong, reference.
60
61               $copy = $ref;
62               $weak = isweak($copy);              # false
63
64       looks_like_number EXPR
65           Returns true if perl thinks EXPR is a number. See
66           "looks_like_number" in perlapi.
67
68       openhandle FH
69           Returns FH if FH may be used as a filehandle and is open, or FH is
70           a tied handle. Otherwise "undef" is returned.
71
72               $fh = openhandle(*STDIN);           # \*STDIN
73               $fh = openhandle(\*STDIN);          # \*STDIN
74               $fh = openhandle(*NOTOPEN);         # undef
75               $fh = openhandle("scalar");         # undef
76
77       readonly SCALAR
78           Returns true if SCALAR is readonly.
79
80               sub foo { readonly($_[0]) }
81
82               $readonly = foo($bar);              # false
83               $readonly = foo(0);                 # true
84
85       refaddr EXPR
86           If EXPR evaluates to a reference the internal memory address of the
87           referenced value is returned. Otherwise "undef" is returned.
88
89               $addr = refaddr "string";           # undef
90               $addr = refaddr \$var;              # eg 12345678
91               $addr = refaddr [];                 # eg 23456784
92
93               $obj  = bless {}, "Foo";
94               $addr = refaddr $obj;               # eg 88123488
95
96       reftype EXPR
97           If EXPR evaluates to a reference the type of the variable
98           referenced is returned. Otherwise "undef" is returned.
99
100               $type = reftype "string";           # undef
101               $type = reftype \$var;              # SCALAR
102               $type = reftype [];                 # ARRAY
103
104               $obj  = bless {}, "Foo";
105               $type = reftype $obj;               # HASH
106
107       set_prototype CODEREF, PROTOTYPE
108           Sets the prototype of the given function, or deletes it if
109           PROTOTYPE is undef. Returns the CODEREF.
110
111               set_prototype \&foo, '$$';
112
113       tainted EXPR
114           Return true if the result of EXPR is tainted
115
116               $taint = tainted("constant");       # false
117               $taint = tainted($ENV{PWD});        # true if running under -T
118
119       weaken REF
120           REF will be turned into a weak reference. This means that it will
121           not hold a reference count on the object it references. Also when
122           the reference count on that object reaches zero, REF will be set to
123           undef.
124
125           This is useful for keeping copies of references , but you don't
126           want to prevent the object being DESTROY-ed at its usual time.
127
128               {
129                 my $var;
130                 $ref = \$var;
131                 weaken($ref);                     # Make $ref a weak reference
132               }
133               # $ref is now undef
134
135           Note that if you take a copy of a scalar with a weakened reference,
136           the copy will be a strong reference.
137
138               my $var;
139               my $foo = \$var;
140               weaken($foo);                       # Make $foo a weak reference
141               my $bar = $foo;                     # $bar is now a strong reference
142
143           This may be less obvious in other situations, such as "grep()", for
144           instance when grepping through a list of weakened references to
145           objects that may have been destroyed already:
146
147               @object = grep { defined } @object;
148
149           This will indeed remove all references to destroyed objects, but
150           the remaining references to objects will be strong, causing the
151           remaining objects to never be destroyed because there is now always
152           a strong reference to them in the @object array.
153

DIAGNOSTICS

155       Module use may give one of the following errors during import.
156
157       Weak references are not implemented in the version of perl
158           The version of perl that you are using does not implement weak
159           references, to use "isweak" or "weaken" you will need to use a
160           newer release of perl.
161
162       Vstrings are not implemented in the version of perl
163           The version of perl that you are using does not implement Vstrings,
164           to use "isvstring" you will need to use a newer release of perl.
165
166       "NAME" is only available with the XS version of Scalar::Util
167           "Scalar::Util" contains both perl and C implementations of many of
168           its functions so that those without access to a C compiler may
169           still use it. However some of the functions are only available when
170           a C compiler was available to compile the XS version of the
171           extension.
172
173           At present that list is: weaken, isweak, dualvar, isvstring,
174           set_prototype
175

KNOWN BUGS

177       There is a bug in perl5.6.0 with UV's that are >= 1<<31. This will show
178       up as tests 8 and 9 of dualvar.t failing
179

SEE ALSO

181       List::Util
182
184       Copyright (c) 1997-2007 Graham Barr <gbarr@pobox.com>. All rights
185       reserved.  This program is free software; you can redistribute it
186       and/or modify it under the same terms as Perl itself.
187
188       Except weaken and isweak which are
189
190       Copyright (c) 1999 Tuomas J. Lukka <lukka@iki.fi>. All rights reserved.
191       This program is free software; you can redistribute it and/or modify it
192       under the same terms as perl itself.
193
194
195
196perl v5.12.4                      2011-06-07                 Scalar::Util(3pm)
Impressum