ConfigReader

 view release on metacpan or  search on metacpan

Values.pm  view on Meta::CPAN


sub ignore          { my $s = shift;  $s->{'spec'}->ignore(@_); }


=head2 C<directives()>

Returns an array of the configuration directive names.

=cut

sub directives      { my $s = shift;  $s->{'spec'}->directives(@_); }


=head2 C<value($directive, [$whence])>

Returns the value of the configuration directive $directive.

=cut

sub value {
    my ($self, $directive, $whence) = @_;

    unless (defined $whence) {
        my ($package, $filename, $line) = caller;
        $whence = "at $filename line $line";
    }

    my $spec = $self->{'spec'};
    my $values = $self->{'values'};

    return $spec->value($directive, $values, $whence);
}


=head2 C<define_accessors([$package, [@names]])>

Creates subroutines in the caller's package to access configuration
values.  For example, if one of the configuration directives is named
"Input_File", you can do:

    $config->define_accessors();
    ...

    open(IN, Input_File());

The names of the created subroutines is returned in an array.  If
you'd like to export the accessor subroutines, you can say:

    push @EXPORT, $config->define_accessors();

You can specify the package in which to create the subroutines with the
optional $package argument.  You may also specify which configuration
directives to create accessor subroutines for.  By default,
subroutines will be created for all the directives.

=cut

sub define_accessors {
    my ($self, $package, @names) = @_;
    @names = $self->directives() unless @names;
    $package = (caller)[0] unless defined $package;

    my $name;
    foreach $name (@names) {
        $self->_define_accessor($name, $package);
    }
    @names;
}

sub _define_accessor {
    my ($self, $name, $package) = @_;
    $package = (caller)[0] unless defined $package;
    
    no strict 'refs';
    *{ $package . "::" . $name } = $self->_make_accessor($name);
    return $name;
}

sub _make_accessor {
    my ($self, $name) = @_;
    return sub {
        my ($package, $filename, $line) = caller;
        $self->value($name, "at $filename line $line")
    };
}

=head1 IMPLEMENTATION METHODS

The following methods will probably be called by a subclass
implementing a reader for a particular style of configuration files.

=head2 new( [$spec] )

The static method new() creates and returns a new ConfigReader::Values
object.

Unless the optional $spec argument is present, a new
ConfigReader::Spec object will be created to store the configuration
specification.  The directive(), required(), ignore(), value(), and
directive() methods described above are passed through to the spec
object.

By setting $spec, you can use a different class (perhaps a subclass)
to store the specification.

You can also set $spec if you want to use one specification for
multiple sets of values.  Files like /etc/termcap describe a
configuration for multiple objects (terminals, in this case), but use
the same directives to describe each object.

=cut

sub new {
    my ($class, $spec) = @_;
    $spec = new ConfigReader::Spec unless defined $spec;
    my $self = {spec   => $spec,
                values => {}};
    return bless $self, $class;
}


=head2 C<values()>

Returns the hash ref which actually stores the configuration directive
values.  The key of the hash ref is the directive name.

=cut

sub values {
    my ($self) = @_;
    return $self->{'values'};
}



( run in 1.534 second using v1.01-cache-2.11-cpan-5511b514fd6 )