Badger::Class for Introspection
package Your::Forager; use Badger::Class base => 'Badger::Base', import => 'class'; our $FOO = 10; sub init { my ($self, $config) = @_; $self->{ foo } = $config->{ foo } || $self->class->var('FOO'); return $self; }
Note: Just Works™ with subclasses
Thus Spake Andy:
You can also call the class()
subroutine as a method against your objects.
When Perl calls an object method it implicitly passes the object reference as
the first argument. So $self->class
is the same as class($self)
. We get
back a Badger::Class
object for whatever kind of package $self
is blessed
into. The var()
method then returns the value of a package variable in that
package. The above code is more long-winded than simply writing $FOO
but
it has the advantage of working correctly when you subclass the package.
This may not be what you want, of course, in which case $FOO
works just
fine.