View on
MetaCPAN is shutting down
For details read Perl NOC. After June 25th this page will redirect to
Jonathan Rockway > MooseX-FileAttribute-0.02 > MooseX::FileAttribute



Annotate this POD

View/Report Bugs
Module Version: 0.02   Source   Latest Release: MooseX-FileAttribute-0.03


MooseX::FileAttribute - sugar for classes that have file or directory attributes


Instead of has, use has_file or has_directory to create attributes that hold a file or 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.

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.


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.



Jonathan Rockway <>


Copyright (c) 2009 Jonathan Rockway.

This module is Free software. You can redistribute it under the same terms as Perl itself.

syntax highlighting: