Config::Any::XML - Load XML config files


Config-Any documentation Contained in the Config-Any distribution.

Index


Code Index:

NAME

Top

Config::Any::XML - Load XML config files

DESCRIPTION

Top

Loads XML files. Example:

    <config>
        <name>TestApp</name>
        <component name="Controller::Foo">
            <foo>bar</foo>
        </component>
        <model name="Baz">
            <qux>xyzzy</qux>
        </model>
    </config>

METHODS

Top

extensions( )

return an array of valid extensions (xml).

load( $file )

Attempts to load $file as an XML file.

requires_all_of( )

Specifies that this module requires XML::Simple in order to work.

CAVEATS

Top

Strict Mode

If, by some chance, XML::Simple has already been loaded with the strict flag turned on, then you will likely get errors as warnings will become fatal exceptions and certain arguments to XMLin() will no longer be optional.

See XML::Simple's strict mode documentation for more information.

AUTHORS

Top

Brian Cassidy <bricas@cpan.org>

Joel Bernstein <rataxis@cpan.org>

COPYRIGHT AND LICENSE

Top

SEE ALSO

Top

* Catalyst
* Config::Any
* XML::Simple

Config-Any documentation Contained in the Config-Any distribution.
package Config::Any::XML;

use strict;
use warnings;

use base 'Config::Any::Base';

sub extensions {
    return qw( xml );
}

sub load {
    my $class = shift;
    my $file  = shift;
    my $args  = shift || {};

    require XML::Simple;
    my $config = XML::Simple::XMLin(
        $file,
        ForceArray => [ qw( component model view controller ) ],
        %$args
    );

    return $class->_coerce( $config );
}

sub _coerce {
    # coerce the XML-parsed config into the correct format
    my $class  = shift;
    my $config = shift;
    my $out;
    for my $k ( keys %$config ) {
        my $ref = $config->{ $k };
        my $name = ref $ref eq 'HASH' ? delete $ref->{ name } : undef;
        if ( defined $name ) {
            $out->{ $k }->{ $name } = $ref;
        }
        else {
            $out->{ $k } = $ref;
        }
    }
    $out;
}

sub requires_all_of { 'XML::Simple' }

1;