package Print; # Print class to print a string use warnings; use strict; use Carp; sub new { my $type = shift; my $class = ref $type || $type; # TEXT is private! my $self = { TEXT => undef, }; my $closure = sub { my $field = shift; if (@_) { $self->{$field} = shift; } return $self->{$field}; }; bless ($closure,$class); return $closure; } # a public accessor to set and get TEXT sub text { &{ $_[0] }("TEXT", @_[1 .. $#_]) } # a public method to print TEXT sub print { my $self = shift; print $self->text . "\n"; } 1;