in reply to Re^3: Fields pragma - the poor man's OO framework?
in thread Fields pragma - the poor man's OO framework?

'static' in this context means 'checked at compile time' rather than 'restricted to a given set of values'.

However if restriction to given values is wanted, I'd suggest Hash::Utils as it is core in 5.8 and above (and it does the restriction natively rather than using tie).

package Foo; use strict; use Hash::Util qw( lock_keys ); my @attrs = qw( name alias race ); sub new { my $class = shift; my %args = @_; my $self = bless {}, $class; lock_keys(%$self, @attrs); foreach my $k ( @attrs ) { if ( exists $args{$k} ) { $self->{$k} = $args{$k}; } } return $self; } package main; my $c = Foo->new(name => 'Aragorn', alias => 'Strider', race => 'Human +'); print "name: $c->{name}\n"; print "job: $c->{job}\n";
output:
$ perl test.pl name: Aragorn Attempt to access disallowed key 'job' in a restricted hash at test.pl + line 26.
As it happens, fields in 5.10 is implemented using Hash::Utils' restriction mechanism.

Replies are listed 'Best First'.
Re^5: Fields pragma - the poor man's OO framework?
by dragonchild (Archbishop) on Jul 07, 2008 at 12:58 UTC
    You cannot check all hash accesses at compile time. Therefore, fields must do many things at runtime. Therefore, it must have a runtime component. I suggested using tie. lock_keys() is a perfectly good other solution. tie has the added benefit of being able to do other things than just restricting keynames. YMMV.

    My criteria for good software:
    1. Does it work?
    2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?

      As far as I can tell, 'fields' performs checks both at compile time (most common case, if used carefully) and at run-time (thanks to Hash::Util, as explained by Arunbear).

      I just noticed an issue (bug?) in the 5.10 implementation. I'll post it in a top-level subthread, so that any wandering Googler gets a chance to see it.