tadman has asked for the wisdom of the Perl Monks concerning the following question:

While reading up on the documentation of base, I came across a reference for fields. What is this module, and can anyone comment on how they've used it effectively, or is it just some sort of experiment that somehow ended up in the base distribution?

Since the links above are probably pointed off into deep space, here's an excerpt from the POD:
DESCRIPTION
The "fields" pragma enables compile-time verified class fields.
# subclassing { package Bar; use base 'Foo'; use fields qw(baz _Bar_private); # not shared with Foo sub new { my $class = shift; my $self = fields::new($class); $self->SUPER::new(); # init base fields $self->{baz} = 10; # init own fields $self->{_Bar_private} = "this is Bar's secret"; return $self; } }

Replies are listed 'Best First'.
Re: use fields
by jmcnamara (Monsignor) on Jun 03, 2002 at 14:01 UTC

    The fields pragma is a way of generating classes based on pseudohashes. As such it should give array access speed combined with hash-like flexibility. It also causes attribute names to be verified at compile time rather than run time, thus avoiding potential errors due to misspelling.

    Since pseudohashes seem to have been a failed experiment the fields pragma may be the only way of guaranteeing this type of behaviour for future releases of perl.

    --
    John.

      Actually, since pseudohashes are being dumped, I would stay away from fields. It seems likely to change or disappear.

        Actually, since pseudohashes are being dumped, I would stay away from fields.

        I think that the intention is to support fields even if pseudohashes are abandoned. From "Programming Perl" 3rd Ed., P257:

        Pseudohashes are a new and relatively experimental feature; as such, the underlying implementation may well change in the future. To protect yourself from such changes, always go through the fields module's documented interface via its phash and new functions

        This is also the migration method proposedby the Perl6 RFC: Pseudo-hashes must die!.

        --
        John.

Re: use fields
by Aristotle (Chancellor) on Jun 03, 2002 at 22:46 UTC
    It was an attempt at coming up with a commonly accepted approach to doing data inheritance akin to that of other languages in Perl. Both the performance was a concern, as well as compile-time interception of typos, much like your garden variety OO language has them. I know a couple modules in the distribution and on CPAN use it but it's yet to become common practice, whether for practical reasons or not.

    Makeshifts last the longest.