MooseX::Types::Path::Tiny - Path::Tiny types and coercions for Moose |
MooseX::Types::Path::Tiny - Path::Tiny types and coercions for Moose
version 0.011
### specification of type constraint with coercion
package Foo;
use Moose; use MooseX::Types::Path::Tiny qw/Path Paths AbsPath/;
has filename => ( is => 'ro', isa => Path, coerce => 1, );
has directory => ( is => 'ro', isa => AbsPath, coerce => 1, );
has filenames => ( is => 'ro', isa => Paths, coerce => 1, );
### usage in code
Foo->new( filename => 'foo.txt' ); # coerced to Path::Tiny Foo->new( directory => '.' ); # coerced to path('.')->absolute Foo->new( filenames => [qw/bar.txt baz.txt/] ); # coerced to ArrayRef[Path::Tiny]
This module provides the Path::Tiny manpage types for Moose. It handles two important types of coercion:
It also can check to ensure that files or directories exist.
This module uses the MooseX::Types manpage to define the following subtypes.
Path
ensures an attribute is a the Path::Tiny manpage object. Strings and
objects with overloaded stringification may be coerced.
AbsPath
is a subtype of Path
(above), but coerces to an absolute path.
These are just like Path
and AbsPath
, except they check -f
to ensure
the file actually exists on the filesystem.
These are just like Path
and AbsPath
, except they check -d
to ensure
the directory actually exists on the filesystem.
These are arrayrefs of Path
and AbsPath
, and include coercions from
arrayrefs of strings.
Path
just ensures you have a the Path::Tiny manpage object.
File
and Dir
check the filesystem. Don't use them unless that's really
what you want.
Be careful if you pass in a the File::Temp manpage object. Because the argument is stringified during coercion into a the Path::Tiny manpage object, no reference to the original the File::Temp manpage argument is held. Be sure to hold an external reference to it to avoid immediate cleanup of the temporary file or directory at the end of the enclosing scope.
A better approach is to use the Path::Tiny manpage's own tempfile
or tempdir
constructors, which hold the reference for you.
Foo->new( filename => Path::Tiny->tempfile );
David Golden <dagolden@cpan.org>
This software is Copyright (c) 2013 by David Golden.
This is free software, licensed under:
The Apache License, Version 2.0, January 2004
MooseX::Types::Path::Tiny - Path::Tiny types and coercions for Moose |