#! perl -slw use strict; package Message; use constant CLASS => 0; use constant SELF => 0; use constant TEXT => 1; use constant TS => 1; use constant ATTRIBUTES => qw/_id _timestamp text/; BEGIN { no strict 'refs'; for my $property (ATTRIBUTES){ *$property = sub : lvalue { $_[SELF]->{$property}; } } } { my $nextID = 0; sub new { my $self = {}; @$self{'_id', '_timestamp', 'text'} = ($nextID++, time(), $_[TEXT] || 'Unassigned!'); return bless $self, $_[CLASS]; } } sub ID { $_[SELF]->_id; } sub timeStamp { $_[SELF]->_timestamp } sub setTS { $_[SELF]->_timestamp = $_[TS] if $_[TS] and $_[TS] =~ /^\d+/; } sub stringify{ sprintf '<' . __PACKAGE__ . '>' . $/ . '' . $/ . '' . $/ . '%s' . $/ . '' . $/, map{ $_[SELF]->$_ } ATTRIBUTES; } package main; my $msg = new Message 'A message'; print $msg->stringify(); $msg->text = 'This is the new message text'; print $msg->timeStamp; $msg->setTS( time()-1000 ); print $msg->stringify(); my $msg2 = new Message; print $msg2->stringify(); $msg2->text = 'Yet another test message'; print $msg2->stringify(); __END__ C:\test>lvalue A message 1041200593 This is the new message text Unassigned! Yet another test message