3 # Copyright (c) 1997-2005 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 require List::Util; # List::Util loads the XS
13 @EXPORT_OK = qw(blessed dualvar reftype weaken isweak tainted readonly openhandle refaddr isvstring looks_like_number set_prototype);
15 $VERSION = eval $VERSION;
18 if (grep { /^(weaken|isweak)$/ } @_ ) {
20 Carp::croak("Weak references are not implemented in the version of perl");
22 if (grep { /^(isvstring)$/ } @_ ) {
24 Carp::croak("Vstrings are not implemented in the version of perl");
26 if (grep { /^(dualvar|set_prototype)$/ } @_ ) {
28 Carp::croak("$1 is only avaliable with the XS version");
36 my $rt = reftype($fh) || '';
38 return defined(fileno($fh)) ? $fh : undef
41 if (reftype(\$fh) eq 'GLOB') { # handle openhandle(*DATA)
44 elsif ($rt ne 'GLOB') {
48 (tied(*$fh) or defined(fileno($fh)))
52 eval <<'ESQ' unless defined &dualvar;
54 push @EXPORT_FAIL, qw(weaken isweak dualvar isvstring set_prototype);
56 # The code beyond here is only used if the XS is not installed
58 # Hope nobody defines a sub by this name
59 sub UNIVERSAL::a_sub_not_likely_to_be_here { ref($_[0]) }
62 local($@, $SIG{__DIE__}, $SIG{__WARN__});
64 ? eval { $_[0]->a_sub_not_likely_to_be_here }
69 my $pkg = ref($_[0]) or return undef;
70 bless $_[0], 'Scalar::Util::Fake';
77 local($@, $SIG{__DIE__}, $SIG{__WARN__});
81 length($t = ref($r)) or return undef;
83 # This eval will fail if the reference is not blessed
84 eval { $r->a_sub_not_likely_to_be_here; 1 }
87 # we have a GLOB or an IO. Stringify a GLOB gives it's name
89 $q =~ /^\*/ ? "GLOB" : "IO";
92 # OK, if we don't have a GLOB what parts of
93 # a glob will it populate.
94 # NOTE: A glob always has a SCALAR
96 defined *glob{ARRAY} && "ARRAY"
97 or defined *glob{HASH} && "HASH"
98 or defined *glob{CODE} && "CODE"
99 or length(ref(${$r})) ? "REF" : "SCALAR";
106 local($@, $SIG{__DIE__}, $SIG{__WARN__});
108 eval { kill 0 * $_[0] };
113 return 0 if tied($_[0]) || (ref(\($_[0])) ne "SCALAR");
115 local($@, $SIG{__DIE__}, $SIG{__WARN__});
118 !eval { $_[0] = $tmp; 1 };
121 sub looks_like_number {
124 # checks from perlfaq4
125 return $] < 5.009002 unless defined;
126 return 1 if (/^[+-]?\d+$/); # is a +/- integer
127 return 1 if (/^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/); # a C float
128 return 1 if ($] >= 5.008 and /^(Inf(inity)?|NaN)$/i) or ($] >= 5.006001 and /^Inf$/i);
141 Scalar::Util - A selection of general-utility scalar subroutines
145 use Scalar::Util qw(blessed dualvar isweak readonly refaddr reftype tainted weaken isvstring looks_like_number set_prototype);
149 C<Scalar::Util> contains a selection of subroutines that people have
150 expressed would be nice to have in the perl core, but the usage would
151 not really be high enough to warrant the use of a keyword, and the size
152 so small such that being individual extensions would be wasteful.
154 By default C<Scalar::Util> does not export any subroutines. The
155 subroutines defined are
161 If EXPR evaluates to a blessed reference the name of the package
162 that it is blessed into is returned. Otherwise C<undef> is returned.
165 $class = blessed $scalar; # undef
168 $class = blessed $ref; # undef
170 $obj = bless [], "Foo";
171 $class = blessed $obj; # "Foo"
173 =item dualvar NUM, STRING
175 Returns a scalar that has the value NUM in a numeric context and the
176 value STRING in a string context.
178 $foo = dualvar 10, "Hello";
179 $num = $foo + 2; # 12
180 $str = $foo . " world"; # Hello world
184 If EXPR is a scalar which was coded as a vstring the result is true.
187 $fmt = isvstring($vs) ? "%vd" : "%s"; #true
192 If EXPR is a scalar which is a weak reference the result is true.
195 $weak = isweak($ref); # false
197 $weak = isweak($ref); # true
199 =item looks_like_number EXPR
201 Returns true if perl thinks EXPR is a number. See
202 L<perlapi/looks_like_number>.
206 Returns FH if FH may be used as a filehandle and is open, or FH is a tied
207 handle. Otherwise C<undef> is returned.
209 $fh = openhandle(*STDIN); # \*STDIN
210 $fh = openhandle(\*STDIN); # \*STDIN
211 $fh = openhandle(*NOTOPEN); # undef
212 $fh = openhandle("scalar"); # undef
214 =item readonly SCALAR
216 Returns true if SCALAR is readonly.
218 sub foo { readonly($_[0]) }
220 $readonly = foo($bar); # false
221 $readonly = foo(0); # true
225 If EXPR evaluates to a reference the internal memory address of
226 the referenced value is returned. Otherwise C<undef> is returned.
228 $addr = refaddr "string"; # undef
229 $addr = refaddr \$var; # eg 12345678
230 $addr = refaddr []; # eg 23456784
232 $obj = bless {}, "Foo";
233 $addr = refaddr $obj; # eg 88123488
237 If EXPR evaluates to a reference the type of the variable referenced
238 is returned. Otherwise C<undef> is returned.
240 $type = reftype "string"; # undef
241 $type = reftype \$var; # SCALAR
242 $type = reftype []; # ARRAY
244 $obj = bless {}, "Foo";
245 $type = reftype $obj; # HASH
247 =item set_prototype CODEREF, PROTOTYPE
249 Sets the prototype of the given function, or deletes it if PROTOTYPE is
250 undef. Returns the CODEREF.
252 set_prototype \&foo, '$$';
256 Return true if the result of EXPR is tainted
258 $taint = tainted("constant"); # false
259 $taint = tainted($ENV{PWD}); # true if running under -T
263 REF will be turned into a weak reference. This means that it will not
264 hold a reference count on the object it references. Also when the reference
265 count on that object reaches zero, REF will be set to undef.
267 This is useful for keeping copies of references , but you don't want to
268 prevent the object being DESTROY-ed at its usual time.
273 weaken($ref); # Make $ref a weak reference
277 Note that if you take a copy of a scalar with a weakened reference,
278 the copy will be a strong reference.
282 weaken($foo); # Make $foo a weak reference
283 my $bar = $foo; # $bar is now a strong reference
285 This may be less obvious in other situations, such as C<grep()>, for instance
286 when grepping through a list of weakened references to objects that may have
287 been destroyed already:
289 @object = grep { defined } @object;
291 This will indeed remove all references to destroyed objects, but the remaining
292 references to objects will be strong, causing the remaining objects to never
293 be destroyed because there is now always a strong reference to them in the
300 There is a bug in perl5.6.0 with UV's that are >= 1<<31. This will
301 show up as tests 8 and 9 of dualvar.t failing
305 Copyright (c) 1997-2005 Graham Barr <gbarr@pobox.com>. All rights reserved.
306 This program is free software; you can redistribute it and/or modify it
307 under the same terms as Perl itself.
309 Except weaken and isweak which are
311 Copyright (c) 1999 Tuomas J. Lukka <lukka@iki.fi>. All rights reserved.
312 This program is free software; you can redistribute it and/or modify it
313 under the same terms as perl itself.
317 The weaken and isweak subroutines in this module and the patch to the core Perl
318 were written in connection with the APress book `Tuomas J. Lukka's Definitive
319 Guide to Object-Oriented Programming in Perl', to avoid explaining why certain
320 things would have to be done in cumbersome ways.