3 # Copyright (c) 1997-2001 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
12 our @ISA = qw(Exporter);
13 our @EXPORT_OK = qw(blessed dualvar reftype weaken isweak tainted readonly openhandle refaddr isvstring);
14 our $VERSION = $List::Util::VERSION;
18 my $rt = reftype($fh) || '';
20 return defined(fileno($fh)) ? $fh : undef
23 if (reftype(\$fh) eq 'GLOB') { # handle openhandle(*DATA)
26 elsif ($rt ne 'GLOB') {
30 (tied(*$fh) or defined(fileno($fh)))
40 Scalar::Util - A selection of general-utility scalar subroutines
44 use Scalar::Util qw(blessed dualvar isweak readonly refaddr reftype tainted weaken);
48 C<Scalar::Util> contains a selection of subroutines that people have
49 expressed would be nice to have in the perl core, but the usage would
50 not really be high enough to warrant the use of a keyword, and the size
51 so small such that being individual extensions would be wasteful.
53 By default C<Scalar::Util> does not export any subroutines. The
54 subroutines defined are
60 If EXPR evaluates to a blessed reference the name of the package
61 that it is blessed into is returned. Otherwise C<undef> is returned.
64 $class = blessed $scalar; # undef
67 $class = blessed $ref; # undef
69 $obj = bless [], "Foo";
70 $class = blessed $obj; # "Foo"
72 =item dualvar NUM, STRING
74 Returns a scalar that has the value NUM in a numeric context and the
75 value STRING in a string context.
77 $foo = dualvar 10, "Hello";
79 $str = $foo . " world"; # Hello world
83 If EXPR is a scalar which was coded as a vstring the result is true.
86 $fmt = isvstring($vs) ? "%vd" : "%s"; #true
91 If EXPR is a scalar which is a weak reference the result is true.
94 $weak = isweak($ref); # false
96 $weak = isweak($ref); # true
100 Returns FH if FH may be used as a filehandle and is open, or FH is a tied
101 handle. Otherwise C<undef> is returned.
103 $fh = openhandle(*STDIN); # \*STDIN
104 $fh = openhandle(\*STDIN); # \*STDIN
105 $fh = openhandle(*NOTOPEN); # undef
106 $fh = openhandle("scalar"); # undef
108 =item readonly SCALAR
110 Returns true if SCALAR is readonly.
112 sub foo { readonly($_[0]) }
114 $readonly = foo($bar); # false
115 $readonly = foo(0); # true
119 If EXPR evaluates to a reference the internal memory address of
120 the referenced value is returned. Otherwise C<undef> is returned.
122 $addr = refaddr "string"; # undef
123 $addr = refaddr \$var; # eg 12345678
124 $addr = refaddr []; # eg 23456784
126 $obj = bless {}, "Foo";
127 $addr = refaddr $obj; # eg 88123488
131 If EXPR evaluates to a reference the type of the variable referenced
132 is returned. Otherwise C<undef> is returned.
134 $type = reftype "string"; # undef
135 $type = reftype \$var; # SCALAR
136 $type = reftype []; # ARRAY
138 $obj = bless {}, "Foo";
139 $type = reftype $obj; # HASH
143 Return true if the result of EXPR is tainted
145 $taint = tainted("constant"); # false
146 $taint = tainted($ENV{PWD}); # true if running under -T
150 REF will be turned into a weak reference. This means that it will not
151 hold a reference count on the object it references. Also when the reference
152 count on that object reaches zero, REF will be set to undef.
154 This is useful for keeping copies of references , but you don't want to
155 prevent the object being DESTROY-ed at its usual time.
160 weaken($ref); # Make $ref a weak reference
168 There is a bug in perl5.6.0 with UV's that are >= 1<<31. This will
169 show up as tests 8 and 9 of dualvar.t failing
173 Copyright (c) 1997-2001 Graham Barr <gbarr@pobox.com>. All rights reserved.
174 This program is free software; you can redistribute it and/or modify it
175 under the same terms as Perl itself.
177 Except weaken and isweak which are
179 Copyright (c) 1999 Tuomas J. Lukka <lukka@iki.fi>. All rights reserved.
180 This program is free software; you can redistribute it and/or modify it
181 under the same terms as perl itself.
185 The weaken and isweak subroutines in this module and the patch to the core Perl
186 were written in connection with the APress book `Tuomas J. Lukka's Definitive
187 Guide to Object-Oriented Programming in Perl', to avoid explaining why certain
188 things would have to be done in cumbersome ways.