Note: When key names start with an underscore, you should quote the key name as it may lead to issues with strict pragma.

I took a look at Class::Struct. My quick guess is that it takes each key you add in the struct method and auto-generates a setter and getter.

Code

$element_value = $obj->s; # element value $obj->s('new value');

In order for your object to work like this, you would need to create the setter and getters for each key. This can be done by the AUTOLOAD to create the getter/setter on the fly. Otherwise, it's pretty monotonous. For what it's worth, to save time, headaches, and use proven well tested code - use Moose.

This doesn't work when the caller is a subclass. If I have class B that inherits from class A, $self will be B, but caller will be A.

Since the subclass is inheriting from the parent, all the methods belong to the subclass, except those methods that are overridden. To access the parents versions, you must use SUPER. Even calling SUPER, as the TacoBell new method does, it still reports the caller as TacoBell.

I don't think I follow on the base class needing to know its subclasses.

When TacoBell inherits from foobar, TacoBell extends foobar but its identity is still TacoBell. When you issue, use parent fooBar, this uses fooBar to load its methods into TacoBell's namespace/package, and then pushes fooBar onto @ISA. @ISA is used as a method lookup table, that sets a lookup precedence for method calling. When a method is requested, it searches TacoBell, in case of any overrides, and then fooBar.

Perl doesn't really support "Private" anything. Its all Public. We are using a hack to simulate Private functions. Python has the same issue, but decided to remove any methods from its lookup tables that begin with an underscore. However, this can be bypassed very easily, because even though it may appear to be private, its still public.

In Perl and Python, private is done by convention only. It's up to the programmer to respect this.

Now in Perl 6, there has been requests to add 'private' which will do exactly as you expect. So this may find it's way into Perl 5 as well.

As for Moose, there is an extension that does private and protected modes. But, they are still remain public.

Experienced Perl programmers have a lot of respect for each others logic and implementations. They know the limitations of the language and respect it and make it work. Just because objects can be accessed, or jail-breaked, or whatever, a majority if not all try to follow the best practice guidelines.