3 # Copyright (c) 1997-2006 Graham Barr <gbarr@pobox.com>. All rights reserved.
4 # This program is free software; you can redistribute it and/or
5 # modify it under the same terms as Perl itself.
10 use vars qw(@ISA @EXPORT_OK $VERSION);
12 require List::Util; # List::Util loads the XS
15 @EXPORT_OK = qw(blessed dualvar reftype weaken isweak tainted readonly openhandle refaddr isvstring looks_like_number set_prototype);
17 $VERSION = eval $VERSION;
20 if (grep { /^(weaken|isweak)$/ } @_ ) {
22 Carp::croak("Weak references are not implemented in the version of perl");
24 if (grep { /^(isvstring)$/ } @_ ) {
26 Carp::croak("Vstrings are not implemented in the version of perl");
28 if (grep { /^(dualvar|set_prototype)$/ } @_ ) {
30 Carp::croak("$1 is only avaliable with the XS version");
38 my $rt = reftype($fh) || '';
40 return defined(fileno($fh)) ? $fh : undef
43 if (reftype(\$fh) eq 'GLOB') { # handle openhandle(*DATA)
46 elsif ($rt ne 'GLOB') {
50 (tied(*$fh) or defined(fileno($fh)))
54 eval <<'ESQ' unless defined &dualvar;
56 use vars qw(@EXPORT_FAIL);
57 push @EXPORT_FAIL, qw(weaken isweak dualvar isvstring set_prototype);
59 # The code beyond here is only used if the XS is not installed
61 # Hope nobody defines a sub by this name
62 sub UNIVERSAL::a_sub_not_likely_to_be_here { ref($_[0]) }
65 local($@, $SIG{__DIE__}, $SIG{__WARN__});
67 ? eval { $_[0]->a_sub_not_likely_to_be_here }
72 my $pkg = ref($_[0]) or return undef;
74 bless $_[0], 'Scalar::Util::Fake';
80 my $i = do { local $^W; hex $1 };
81 bless $_[0], $pkg if defined $pkg;
86 local($@, $SIG{__DIE__}, $SIG{__WARN__});
90 length($t = ref($r)) or return undef;
92 # This eval will fail if the reference is not blessed
93 eval { $r->a_sub_not_likely_to_be_here; 1 }
96 # we have a GLOB or an IO. Stringify a GLOB gives it's name
98 $q =~ /^\*/ ? "GLOB" : "IO";
101 # OK, if we don't have a GLOB what parts of
102 # a glob will it populate.
103 # NOTE: A glob always has a SCALAR
105 defined *glob{ARRAY} && "ARRAY"
106 or defined *glob{HASH} && "HASH"
107 or defined *glob{CODE} && "CODE"
108 or length(ref(${$r})) ? "REF" : "SCALAR";
115 local($@, $SIG{__DIE__}, $SIG{__WARN__});
117 eval { kill 0 * $_[0] };
122 return 0 if tied($_[0]) || (ref(\($_[0])) ne "SCALAR");
124 local($@, $SIG{__DIE__}, $SIG{__WARN__});
127 !eval { $_[0] = $tmp; 1 };
130 sub looks_like_number {
133 # checks from perlfaq4
134 return 0 if !defined($_) or ref($_);
135 return 1 if (/^[+-]?\d+$/); # is a +/- integer
136 return 1 if (/^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/); # a C float
137 return 1 if ($] >= 5.008 and /^(Inf(inity)?|NaN)$/i) or ($] >= 5.006001 and /^Inf$/i);
150 Scalar::Util - A selection of general-utility scalar subroutines
154 use Scalar::Util qw(blessed dualvar isweak readonly refaddr reftype tainted
155 weaken isvstring looks_like_number set_prototype);
159 C<Scalar::Util> contains a selection of subroutines that people have
160 expressed would be nice to have in the perl core, but the usage would
161 not really be high enough to warrant the use of a keyword, and the size
162 so small such that being individual extensions would be wasteful.
164 By default C<Scalar::Util> does not export any subroutines. The
165 subroutines defined are
171 If EXPR evaluates to a blessed reference the name of the package
172 that it is blessed into is returned. Otherwise C<undef> is returned.
175 $class = blessed $scalar; # undef
178 $class = blessed $ref; # undef
180 $obj = bless [], "Foo";
181 $class = blessed $obj; # "Foo"
183 =item dualvar NUM, STRING
185 Returns a scalar that has the value NUM in a numeric context and the
186 value STRING in a string context.
188 $foo = dualvar 10, "Hello";
189 $num = $foo + 2; # 12
190 $str = $foo . " world"; # Hello world
194 If EXPR is a scalar which was coded as a vstring the result is true.
197 $fmt = isvstring($vs) ? "%vd" : "%s"; #true
202 If EXPR is a scalar which is a weak reference the result is true.
205 $weak = isweak($ref); # false
207 $weak = isweak($ref); # true
209 B<NOTE>: Copying a weak reference creates a normal, strong, reference.
212 $weak = isweak($ref); # false
214 =item looks_like_number EXPR
216 Returns true if perl thinks EXPR is a number. See
217 L<perlapi/looks_like_number>.
221 Returns FH if FH may be used as a filehandle and is open, or FH is a tied
222 handle. Otherwise C<undef> is returned.
224 $fh = openhandle(*STDIN); # \*STDIN
225 $fh = openhandle(\*STDIN); # \*STDIN
226 $fh = openhandle(*NOTOPEN); # undef
227 $fh = openhandle("scalar"); # undef
229 =item readonly SCALAR
231 Returns true if SCALAR is readonly.
233 sub foo { readonly($_[0]) }
235 $readonly = foo($bar); # false
236 $readonly = foo(0); # true
240 If EXPR evaluates to a reference the internal memory address of
241 the referenced value is returned. Otherwise C<undef> is returned.
243 $addr = refaddr "string"; # undef
244 $addr = refaddr \$var; # eg 12345678
245 $addr = refaddr []; # eg 23456784
247 $obj = bless {}, "Foo";
248 $addr = refaddr $obj; # eg 88123488
252 If EXPR evaluates to a reference the type of the variable referenced
253 is returned. Otherwise C<undef> is returned.
255 $type = reftype "string"; # undef
256 $type = reftype \$var; # SCALAR
257 $type = reftype []; # ARRAY
259 $obj = bless {}, "Foo";
260 $type = reftype $obj; # HASH
262 =item set_prototype CODEREF, PROTOTYPE
264 Sets the prototype of the given function, or deletes it if PROTOTYPE is
265 undef. Returns the CODEREF.
267 set_prototype \&foo, '$$';
271 Return true if the result of EXPR is tainted
273 $taint = tainted("constant"); # false
274 $taint = tainted($ENV{PWD}); # true if running under -T
278 REF will be turned into a weak reference. This means that it will not
279 hold a reference count on the object it references. Also when the reference
280 count on that object reaches zero, REF will be set to undef.
282 This is useful for keeping copies of references , but you don't want to
283 prevent the object being DESTROY-ed at its usual time.
288 weaken($ref); # Make $ref a weak reference
292 Note that if you take a copy of a scalar with a weakened reference,
293 the copy will be a strong reference.
297 weaken($foo); # Make $foo a weak reference
298 my $bar = $foo; # $bar is now a strong reference
300 This may be less obvious in other situations, such as C<grep()>, for instance
301 when grepping through a list of weakened references to objects that may have
302 been destroyed already:
304 @object = grep { defined } @object;
306 This will indeed remove all references to destroyed objects, but the remaining
307 references to objects will be strong, causing the remaining objects to never
308 be destroyed because there is now always a strong reference to them in the
315 There is a bug in perl5.6.0 with UV's that are >= 1<<31. This will
316 show up as tests 8 and 9 of dualvar.t failing
324 Copyright (c) 1997-2006 Graham Barr <gbarr@pobox.com>. All rights reserved.
325 This program is free software; you can redistribute it and/or modify it
326 under the same terms as Perl itself.
328 Except weaken and isweak which are
330 Copyright (c) 1999 Tuomas J. Lukka <lukka@iki.fi>. All rights reserved.
331 This program is free software; you can redistribute it and/or modify it
332 under the same terms as perl itself.