From: Chris W. <ch...@cw...> - 2001-11-09 04:40:10
|
For some reason this crossed my mind recently and I was curious how easily I could make an object read-only in SPOPS. Very easily, it turns out :-) ---------------------------------------- package My::ReadOnly; # $Id: ReadOnly.pm,v 1.1 2001/11/09 04:36:49 lachoy Exp $ use strict; use SPOPS qw( _w DEBUG ); use SPOPS::ClassFactory qw( OK ); sub behavior_factory { my ( $class ) = @_; DEBUG && _w( 1, "Installing read-only persistence methods for ($class)" ); return { read_code => \&generate_persistence_methods }; } sub generate_persistence_methods { my ( $class ) = @_; DEBUG && _w( 1, "Generating read-only save() and remove() for ($class)" ); no strict 'refs'; *{ "${class}::save" } = sub { warn ref $_[0], " is read-only; no changes allowed\n" }; *{ "${class}::remove" } = sub { warn ref $_[0], " is read-only; no changes allowed\n" }; return OK; } 1; ---------------------------------------- To use, put 'My::ReadOnly' in the 'rules_from' of your object configuration. Cool :-) This will be in the eg/ directory of the next SPOPS release. Chris -- Chris Winters (ch...@cw...) Building enterprise-capable snack solutions since 1988. |