16/17

10. Inside out classes (Chapter 15)

  • (One of the recommendations was not to make variables part of your interface. I did that just the other day. Ooops.)

  • Instead of:

        sub new {
    	my ($class, ...) = @_;
    	... 
    	return bless {}, $class;
        }
        sub get_foo {
    	my ($self) = shift;
    	return $self->{foo};
        }
  • Do this:

        {
    	my %foo_of;
    	sub new {
    	    my ($class, ...) = @_;
    	    ...
    	    return bless \do {my $anon_scalar}, $class;
    	}
    	sub get_foo {
    	    my ($self) = shift;
    	    return $foo_of{$self};
    	}
        }
  • Magically, you have encapsulation, and all that.