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

DESCRIPTION

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

KNOWN BUGS

154       There is a bug in perl5.6.0 with UV's that are >= 1<<31. This will show
155       up as tests 8 and 9 of dualvar.t failing
156
158       Copyright (c) 1997-2005 Graham Barr <gbarr@pobox.com>. All rights
159       reserved.  This program is free software; you can redistribute it
160       and/or modify it under the same terms as Perl itself.
161
162       Except weaken and isweak which are
163
164       Copyright (c) 1999 Tuomas J. Lukka <lukka@iki.fi>. All rights reserved.
165       This program is free software; you can redistribute it and/or modify it
166       under the same terms as perl itself.
167

BLATANT PLUG

169       The weaken and isweak subroutines in this module and the patch to the
170       core Perl were written in connection  with the APress book `Tuomas J.
171       Lukka's Definitive Guide to Object-Oriented Programming in Perl', to
172       avoid explaining why certain things would have to be done in cumbersome
173       ways.
174
175
176
177perl v5.8.8                       2001-09-21                 Scalar::Util(3pm)
Impressum