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:

   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.

Bugs may be submitted through the RT bug tracker (or

I am also usually active on irc, as 'ether' at


Jonathan Rockway <>


  • Karen Etheridge <>

  • Jonathan Rockway <>

  • Ken Crowell <>


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.