MooseX::FileAttribute - Sugar for classes that have file or directory attributes
version 0.03
Instead of has, use has_file or has_directory to create attributes that hold a file or directory:
has
has_file
has_directory
package Class; use Moose; use MooseX::FileAttribute; has_file 'foo' => ( documentation => 'path to the foo file', must_exist => 1, required => 1, ); has_directory 'bar' => ( required => 1, ); sub BUILD { use autodie 'mkdir'; mkdir $self->bar unless -d $self->bar; }
Then use the class like you'd use any Moose class:
my $c = Class->new( foo => '/quux/bar/foo', bar => '/quux/bar/' ); my $fh = $c->foo->openr; # string initarg promoted to Path::Class::File attribute while( my $line = <$fh> ) { ... }
I write a lot of classes that take files or directories on the command-line. This results in a lot of boilerplate, usually:
package Class; use Moose; use MooseX::Types::Path::Class qw(File); has 'foo' => ( is => 'ro', isa => File, coerce => 1, required => 1, );
This module lets you save yourself some typing in this case:
has_file 'foo' => ( required => 1 );
These are exactly equivalent. has_directory does the same thing that has_file does, but with a Dir constraint.
Dir
This module also defines two additional type constraints to ensure that the specified file or directory exists and is a file or directory. You can use these constraints instead of the defaults by passing must_exist => 1 to the has_* function.
must_exist => 1
has_*
The ExistingFile constraint will accept named pipes, ttys, directories, etc., as files, as long as what's named exists on disk. The ExistingDir constraint is more strict, only allowing directories.
Bugs may be submitted through the RT bug tracker (or bug-MooseX-FileAttribute@rt.cpan.org).
I am also usually active on irc, as 'ether' at irc.perl.org.
irc.perl.org
Jonathan Rockway <jrockway@cpan.org>
Karen Etheridge <ether@cpan.org>
Jonathan Rockway <jon@jrock.us>
Ken Crowell <ken@oeuftete.com>
This software is copyright (c) 2009 by Jonathan Rockway.
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 MooseX::FileAttribute, copy and paste the appropriate command in to your terminal.
cpanm
cpanm MooseX::FileAttribute
CPAN shell
perl -MCPAN -e shell install MooseX::FileAttribute
For more information on module installation, please visit the detailed CPAN module installation guide.