in reply to Encapsulation without methods
You might want to take a look at lvalued subroutines. Not only do you keep the encapsulation of using methods, but you can treat them like scalars as well.
package Test; sub new { my ($class, $foo, $bar) = @_; return bless { foo => $foo } => ref($class) || $class; } sub foo : lvalue { $_[0]->{foo} } package main; my $test = Test->new("foo"); print $test->foo, "\n"; $test->foo = "bar"; print $test->foo, "\n"; $test->foo =~ s/bar/baz/g; print $test->foo, "\n";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Encapsulation without methods
by Roy Johnson (Monsignor) on Jun 22, 2004 at 03:45 UTC | |
by stvn (Monsignor) on Jun 22, 2004 at 04:33 UTC |