PageRenderTime 40ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/dist/base/lib/fields.pm

https://github.com/dougwilson/perl
Perl | 327 lines | 251 code | 56 blank | 20 comment | 33 complexity | 61dbfe535244b7ca7c40cdbaa7bbdf59 MD5 | raw file
  1. package fields;
  2. require 5.005;
  3. use strict;
  4. no strict 'refs';
  5. unless( eval q{require warnings::register; warnings::register->import; 1} ) {
  6. *warnings::warnif = sub {
  7. require Carp;
  8. Carp::carp(@_);
  9. }
  10. }
  11. use vars qw(%attr $VERSION);
  12. $VERSION = '2.16';
  13. # constant.pm is slow
  14. sub PUBLIC () { 2**0 }
  15. sub PRIVATE () { 2**1 }
  16. sub INHERITED () { 2**2 }
  17. sub PROTECTED () { 2**3 }
  18. # The %attr hash holds the attributes of the currently assigned fields
  19. # per class. The hash is indexed by class names and the hash value is
  20. # an array reference. The first element in the array is the lowest field
  21. # number not belonging to a base class. The remaining elements' indices
  22. # are the field numbers. The values are integer bit masks, or undef
  23. # in the case of base class private fields (which occupy a slot but are
  24. # otherwise irrelevant to the class).
  25. sub import {
  26. my $class = shift;
  27. return unless @_;
  28. my $package = caller(0);
  29. # avoid possible typo warnings
  30. %{"$package\::FIELDS"} = () unless %{"$package\::FIELDS"};
  31. my $fields = \%{"$package\::FIELDS"};
  32. my $fattr = ($attr{$package} ||= [1]);
  33. my $next = @$fattr;
  34. # Quiet pseudo-hash deprecation warning for uses of fields::new.
  35. bless \%{"$package\::FIELDS"}, 'pseudohash';
  36. if ($next > $fattr->[0]
  37. and ($fields->{$_[0]} || 0) >= $fattr->[0])
  38. {
  39. # There are already fields not belonging to base classes.
  40. # Looks like a possible module reload...
  41. $next = $fattr->[0];
  42. }
  43. foreach my $f (@_) {
  44. my $fno = $fields->{$f};
  45. # Allow the module to be reloaded so long as field positions
  46. # have not changed.
  47. if ($fno and $fno != $next) {
  48. require Carp;
  49. if ($fno < $fattr->[0]) {
  50. if ($] < 5.006001) {
  51. warn("Hides field '$f' in base class") if $^W;
  52. } else {
  53. warnings::warnif("Hides field '$f' in base class") ;
  54. }
  55. } else {
  56. Carp::croak("Field name '$f' already in use");
  57. }
  58. }
  59. $fields->{$f} = $next;
  60. $fattr->[$next] = ($f =~ /^_/) ? PRIVATE : PUBLIC;
  61. $next += 1;
  62. }
  63. if (@$fattr > $next) {
  64. # Well, we gave them the benefit of the doubt by guessing the
  65. # module was reloaded, but they appear to be declaring fields
  66. # in more than one place. We can't be sure (without some extra
  67. # bookkeeping) that the rest of the fields will be declared or
  68. # have the same positions, so punt.
  69. require Carp;
  70. Carp::croak ("Reloaded module must declare all fields at once");
  71. }
  72. }
  73. sub inherit {
  74. require base;
  75. goto &base::inherit_fields;
  76. }
  77. sub _dump # sometimes useful for debugging
  78. {
  79. for my $pkg (sort keys %attr) {
  80. print "\n$pkg";
  81. if (@{"$pkg\::ISA"}) {
  82. print " (", join(", ", @{"$pkg\::ISA"}), ")";
  83. }
  84. print "\n";
  85. my $fields = \%{"$pkg\::FIELDS"};
  86. for my $f (sort {$fields->{$a} <=> $fields->{$b}} keys %$fields) {
  87. my $no = $fields->{$f};
  88. print " $no: $f";
  89. my $fattr = $attr{$pkg}[$no];
  90. if (defined $fattr) {
  91. my @a;
  92. push(@a, "public") if $fattr & PUBLIC;
  93. push(@a, "private") if $fattr & PRIVATE;
  94. push(@a, "inherited") if $fattr & INHERITED;
  95. print "\t(", join(", ", @a), ")";
  96. }
  97. print "\n";
  98. }
  99. }
  100. }
  101. if ($] < 5.009) {
  102. *new = sub {
  103. my $class = shift;
  104. $class = ref $class if ref $class;
  105. return bless [\%{$class . "::FIELDS"}], $class;
  106. }
  107. } else {
  108. *new = sub {
  109. my $class = shift;
  110. $class = ref $class if ref $class;
  111. require Hash::Util;
  112. my $self = bless {}, $class;
  113. # The lock_keys() prototype won't work since we require Hash::Util :(
  114. &Hash::Util::lock_keys(\%$self, _accessible_keys($class));
  115. return $self;
  116. }
  117. }
  118. sub _accessible_keys {
  119. my ($class) = @_;
  120. return (
  121. keys %{$class.'::FIELDS'},
  122. map(_accessible_keys($_), @{$class.'::ISA'}),
  123. );
  124. }
  125. sub phash {
  126. die "Pseudo-hashes have been removed from Perl" if $] >= 5.009;
  127. my $h;
  128. my $v;
  129. if (@_) {
  130. if (ref $_[0] eq 'ARRAY') {
  131. my $a = shift;
  132. @$h{@$a} = 1 .. @$a;
  133. if (@_) {
  134. $v = shift;
  135. unless (! @_ and ref $v eq 'ARRAY') {
  136. require Carp;
  137. Carp::croak ("Expected at most two array refs\n");
  138. }
  139. }
  140. }
  141. else {
  142. if (@_ % 2) {
  143. require Carp;
  144. Carp::croak ("Odd number of elements initializing pseudo-hash\n");
  145. }
  146. my $i = 0;
  147. @$h{grep ++$i % 2, @_} = 1 .. @_ / 2;
  148. $i = 0;
  149. $v = [grep $i++ % 2, @_];
  150. }
  151. }
  152. else {
  153. $h = {};
  154. $v = [];
  155. }
  156. [ $h, @$v ];
  157. }
  158. 1;
  159. __END__
  160. =head1 NAME
  161. fields - compile-time class fields
  162. =head1 SYNOPSIS
  163. {
  164. package Foo;
  165. use fields qw(foo bar _Foo_private);
  166. sub new {
  167. my Foo $self = shift;
  168. unless (ref $self) {
  169. $self = fields::new($self);
  170. $self->{_Foo_private} = "this is Foo's secret";
  171. }
  172. $self->{foo} = 10;
  173. $self->{bar} = 20;
  174. return $self;
  175. }
  176. }
  177. my $var = Foo->new;
  178. $var->{foo} = 42;
  179. # this will generate an error
  180. $var->{zap} = 42;
  181. # subclassing
  182. {
  183. package Bar;
  184. use base 'Foo';
  185. use fields qw(baz _Bar_private); # not shared with Foo
  186. sub new {
  187. my $class = shift;
  188. my $self = fields::new($class);
  189. $self->SUPER::new(); # init base fields
  190. $self->{baz} = 10; # init own fields
  191. $self->{_Bar_private} = "this is Bar's secret";
  192. return $self;
  193. }
  194. }
  195. =head1 DESCRIPTION
  196. The C<fields> pragma enables compile-time verified class fields.
  197. NOTE: The current implementation keeps the declared fields in the %FIELDS
  198. hash of the calling package, but this may change in future versions.
  199. Do B<not> update the %FIELDS hash directly, because it must be created
  200. at compile-time for it to be fully useful, as is done by this pragma.
  201. B<Only valid for perl before 5.9.0:>
  202. If a typed lexical variable holding a reference is used to access a
  203. hash element and a package with the same name as the type has
  204. declared class fields using this pragma, then the operation is
  205. turned into an array access at compile time.
  206. The related C<base> pragma will combine fields from base classes and any
  207. fields declared using the C<fields> pragma. This enables field
  208. inheritance to work properly.
  209. Field names that start with an underscore character are made private to
  210. the class and are not visible to subclasses. Inherited fields can be
  211. overridden but will generate a warning if used together with the C<-w>
  212. switch.
  213. B<Only valid for perls before 5.9.0:>
  214. The effect of all this is that you can have objects with named
  215. fields which are as compact and as fast arrays to access. This only
  216. works as long as the objects are accessed through properly typed
  217. variables. If the objects are not typed, access is only checked at
  218. run time.
  219. The following functions are supported:
  220. =over 4
  221. =item new
  222. B< perl before 5.9.0: > fields::new() creates and blesses a
  223. pseudo-hash comprised of the fields declared using the C<fields>
  224. pragma into the specified class.
  225. B< perl 5.9.0 and higher: > fields::new() creates and blesses a
  226. restricted-hash comprised of the fields declared using the C<fields>
  227. pragma into the specified class.
  228. This function is usable with or without pseudo-hashes. It is the
  229. recommended way to construct a fields-based object.
  230. This makes it possible to write a constructor like this:
  231. package Critter::Sounds;
  232. use fields qw(cat dog bird);
  233. sub new {
  234. my $self = shift;
  235. $self = fields::new($self) unless ref $self;
  236. $self->{cat} = 'meow'; # scalar element
  237. @$self{'dog','bird'} = ('bark','tweet'); # slice
  238. return $self;
  239. }
  240. =item phash
  241. B< before perl 5.9.0: >
  242. fields::phash() can be used to create and initialize a plain (unblessed)
  243. pseudo-hash. This function should always be used instead of creating
  244. pseudo-hashes directly.
  245. If the first argument is a reference to an array, the pseudo-hash will
  246. be created with keys from that array. If a second argument is supplied,
  247. it must also be a reference to an array whose elements will be used as
  248. the values. If the second array contains less elements than the first,
  249. the trailing elements of the pseudo-hash will not be initialized.
  250. This makes it particularly useful for creating a pseudo-hash from
  251. subroutine arguments:
  252. sub dogtag {
  253. my $tag = fields::phash([qw(name rank ser_num)], [@_]);
  254. }
  255. fields::phash() also accepts a list of key-value pairs that will
  256. be used to construct the pseudo hash. Examples:
  257. my $tag = fields::phash(name => "Joe",
  258. rank => "captain",
  259. ser_num => 42);
  260. my $pseudohash = fields::phash(%args);
  261. B< perl 5.9.0 and higher: >
  262. Pseudo-hashes have been removed from Perl as of 5.10. Consider using
  263. restricted hashes or fields::new() instead. Using fields::phash()
  264. will cause an error.
  265. =back
  266. =head1 SEE ALSO
  267. L<base>
  268. =cut