Path::Resolver - go from "file" names to things
version 3.100452
Path::Resolver is a set of libraries for resolving virtual file paths into entities that may be found at those paths. Here's a trivial example:
use Path::Resolver::Resolver::FileSystem; # Create a resolver that looks at the filesystem, starting in /etc my $fs = Path::Resolver::Resolver::FileSystem->new({ root => '/etc' }); my $file = $fs->entity_at('/postfix/main.cf');
Assuming it exists, this will return an object representing the file /etc/postfix/main.cf. Using the code above, $file would be a Path::Resolver::SimpleEntity object, which has a content method. We could print the contents of the file to screen like this:
$file
Path::Resolver::SimpleEntity
content
print $file->content;
Path::Resolver lets you use a simple, familiar notation for accessing all kinds of hierarchical data. It's also distributed with resolvers that act as multiplexers for other resolvers. Since all resolvers share one mechanism for addressing content, they can easily be mixed and matched. Since resolvers know what kind of object they'll return, and can be fitted with translators, it's easy to ensure that all your multiplexed resolvers will resolve names to the same kind of object.
For example, we could overlay two search paths like this:
my $resolver = Path::Resolver::Resolver::Mux::Ordered->new({ resolvers => [ Path::Resolver::Resolver::FileSystem->new({ root => './config' }), Path::Resolver::Resolver::Archive::Tar->new({ archive => 'config.tgz' }), ], }); $resolver->entity_at('/foo/bar.txt');
This will return an entity representing ./config/foo/bar.txt if it exists. If it doesn't, it will look for foo/bar.txt in the contents of the archive. If that's found, an entity will be returned. Finally, if neither is found, it will return false.
Alternately, you could multiplex based on path:
my $resolver = Path::Resolver::Resolver::Mux::Prefix->new({ config => Path::Resolver::Resolver::FileSystem->new({ root => '/etc/my-app', }), template => Path::Resolver::Resolver::Mux::Ordered->new({ Path::Resolver::Resolver::DistDir->new({ module => 'MyApp' }), Path::Resolver::Resolver::DataSection->new({ module => 'My::Framework' }), }), });
The path /config/main.cf would be looked for on disk as /etc/my-app/main.cf. The path /template/main.html would be looked for first as main.html in the sharedir for MyApp and failing that in the DATA section of My::Framework.
If you want to read about how to write a resolver, look at Path::Resolver::Role::Resolver.
If you want to read about the interfaces to the existing resolvers look at their documentation:
Path::Resolver::Resolver::AnyDist
Path::Resolver::Resolver::Archive::Tar
Path::Resolver::Resolver::DataSection
Path::Resolver::Resolver::DistDir
Path::Resolver::Resolver::FileSystem
Path::Resolver::Resolver::Hash
Path::Resolver::Resolver::Mux::Ordered
Path::Resolver::Resolver::Mux::Prefix
Ricardo Signes <rjbs@cpan.org>
This software is copyright (c) 2011 by Ricardo Signes.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
To install Path::Resolver, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Path::Resolver
CPAN shell
perl -MCPAN -e shell install Path::Resolver
For more information on module installation, please visit the detailed CPAN module installation guide.