Commit | Line | Data |
f4a2945e |
1 | # Scalar::Util.pm |
2 | # |
3 | # Copyright (c) 1997-2000 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. |
6 | |
7 | package Scalar::Util; |
8 | |
9 | require Exporter; |
10 | require List::Util; # List::Util loads the XS |
11 | |
12 | $VERSION = $VERSION = $List::Util::VERSION; |
13 | @ISA = qw(Exporter); |
14 | @EXPORT_OK = qw(blessed dualvar reftype weaken isweak tainted readonly); |
15 | |
16 | sub export_fail { |
17 | if (grep { /^(weaken|isweak)$/ } @_ ) { |
18 | require Carp; |
19 | Carp::croak("Weak references are not implemented in the version of perl"); |
20 | } |
21 | if (grep { /^dualvar$/ } @_ ) { |
22 | require Carp; |
23 | Carp::croak("dualvar is only avaliable with the XS version"); |
24 | } |
25 | |
26 | @_; |
27 | } |
28 | |
f4a2945e |
29 | 1; |
30 | |
31 | __END__ |
32 | |
33 | =head1 NAME |
34 | |
35 | Scalar::Util - A selection of general-utility scalar subroutines |
36 | |
37 | =head1 SYNOPSIS |
38 | |
39 | use Scalar::Util qw(blessed dualvar reftype weaken isweak); |
40 | |
41 | =head1 DESCRIPTION |
42 | |
43 | C<Scalar::Util> contains a selection of subroutines that people have |
44 | expressed would be nice to have in the perl core, but the usage would |
45 | not really be high enough to warrant the use of a keyword, and the size |
46 | so small such that being individual extensions would be wasteful. |
47 | |
48 | By default C<Scalar::Util> does not export any subroutines. The |
49 | subroutines defined are |
50 | |
51 | =over 4 |
52 | |
53 | =item blessed EXPR |
54 | |
55 | If EXPR evaluates to a blessed reference the name of the package |
56 | that it is blessed into is returned. Otherwise C<undef> is returned. |
57 | |
58 | =item dualvar NUM, STRING |
59 | |
60 | Returns a scalar that has the value NUM in a numeric context and the |
61 | value STRING in a string context. |
62 | |
63 | $foo = dualvar 10, "Hello"; |
64 | $num = $foo + 2; # 12 |
65 | $str = $foo . " world"; # Hello world |
66 | |
67 | =item isweak EXPR |
68 | |
69 | If EXPR is a scalar which is a weak reference the result is true. |
70 | |
71 | =item reftype EXPR |
72 | |
73 | If EXPR evaluates to a reference the type of the variable referenced |
74 | is returned. Otherwise C<undef> is returned. |
75 | |
76 | =item weaken REF |
77 | |
78 | REF will be turned into a weak reference. This means that it will not |
79 | hold a reference count on the object it references. Also when the reference |
80 | count on that object reaches zero, REF will be set to undef. |
81 | |
82 | This is useful for keeping copies of references , but you don't want to |
83 | prevent the object being DESTROY-ed at it's usual time. |
84 | |
85 | =back |
86 | |
87 | =head1 COPYRIGHT |
88 | |
89 | Copyright (c) 1997-2000 Graham Barr <gbarr@pobox.com>. All rights reserved. |
90 | This program is free software; you can redistribute it and/or modify it |
91 | under the same terms as Perl itself. |
92 | |
93 | except weaken and isweak which are |
94 | |
95 | Copyright (c) 1999 Tuomas J. Lukka <lukka@iki.fi>. All rights reserved. |
96 | This program is free software; you can redistribute it and/or modify it |
97 | under the same terms as perl itself. |
98 | |
99 | =head1 BLATANT PLUG |
100 | |
101 | The weaken and isweak subroutines in this module and the patch to the core Perl |
102 | were written in connection with the APress book `Tuomas J. Lukka's Definitive |
103 | Guide to Object-Oriented Programming in Perl', to avoid explaining why certain |
104 | things would have to be done in cumbersome ways. |
105 | |
106 | =cut |