The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Path::Resolver - go from "file" names to things

VERSION

version 3.092200

DESCRIPTION

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_ref method. We could print the contents of the file to screen like this:

  my $content_ref = $file->content_ref;
  print $$content_ref;

WHAT'S THE POINT?

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.

WHERE DO I GO NEXT?

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:

AUTHOR

  Ricardo Signes <rjbs@cpan.org>

COPYRIGHT AND LICENSE

This software is copyright (c) 2009 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.