http://qs321.pair.com?node_id=539277


in reply to Re: Your favorite objects NOT of the hashref phylum (array w/ consts)
in thread Your favorite objects NOT of the hashref phylum

For compile time catching of typos in attrs when using hashes as the internal representation use the fields module.

In perl 5.8 it's based on pseudohashes, which are a bit like mapping strings into arrays, except they use hash syntax. But everyone knows that pseudohashes suck ;-)

In perl 5.9 fields uses Hash::Util and locks the hash for runtime checks, and also performs the same compile time checks, so you get the benefits of fields without the insanity of pseudohashes.

The way you use it is:

use fields qw/attr1 attr2/; sub new { # ... my __PACKAGE__ $self = fields::new($pkg); # construct using fields +::new $self->{attr1} = "foo"; # ok $self->{attttr2} = "bar"; # compile time error } sub some_method { my __PACKAGE__ $self = shift; # use __PACKAGE__ or any class name +to tell perl what class $self is $self->{foo}; # because we told it to make compile time checks bas +ed on __PACKAGE__'s fields this will be checked at compile time }
But this still doesn't let you encapsulate the properties in subroutines that you can override.
-nuffin
zz zZ Z Z #!perl
  • Comment on Re^2: Your favorite objects NOT of the hashref phylum (array w/ consts)
  • Download Code