1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
|
package Catmandu::Util::Path;
use Catmandu::Sane;
our $VERSION = '1.2024';
use Catmandu::Util qw(is_value is_string require_package);
use namespace::clean;
use Exporter qw(import);
our @EXPORT_OK = qw(
looks_like_path
as_path
);
our %EXPORT_TAGS = (all => \@EXPORT_OK,);
sub looks_like_path { # TODO only recognizes Catmandu::Path::simple
my ($path) = @_;
is_string($path) && $path =~ /^\$[\.\/]/ ? 1 : 0;
}
sub as_path {
my ($path, $path_type) = @_;
if (is_value($path)) {
$path_type //= 'simple';
state $class_cache = {};
my $class = $class_cache->{$path_type}
||= require_package($path_type, 'Catmandu::Path');
$class->new(path => $path);
}
else {
$path;
}
}
1;
__END__
=pod
=head1 NAME
Catmandu::Util::Path - Path related utility functions
=head1 FUNCTIONS
=over 4
=item looks_like_path($str)
Returns 1 if the given string is a path, 0 otherwise. Only recognizes
L<Catmandu::Path::simple> paths prefixed with a dollar sign at the moment.
looks_like_path("$.foo.bar.$append")
# => 1
looks_like_path("waffles")
# => 0
=item as_path($str, $type)
Helper function that returns a L<Catmandu::Path> instance for the given path
string. The optional C<$type> argument gives preliminary support for
alternative path implementations and defaults to 'simple'.
as_path("$.foo.bar.$append")
# is equivalent to
Catmandu::Path::simple->new(path => "$.foo.bar.$append");
=back
=cut
|