File::Attributes - Manipulate file metadata


File-Attributes documentation Contained in the File-Attributes distribution.

Index


Code Index:

NAME

Top

File::Attributes - Manipulate file metadata

VERSION

Top

Version 0.04

SYNOPSIS

Top

    use File::Attributes qw(set_attribute list_attributes get_all_attributes);

    my $file = 'foo.txt';
    set_attribute($file, type     => 'text/plain');
    set_attribute($file, encoding => 'utf8');

    my @attributes = list_attributes($file);
    #  @attributes = qw(type encoding)

    %attributes = get_attributes($file);
    #  $attributes{type} will be 'text/plain'
    #  $attributes{foo}  will be undefined.

DETAILS

Top

File::Attributes is a wrapper around modules in the File::Attributes hierarchy. If you use this module directly (instead of one of the aforementioned decendants), then your attribute manipulations will Just Work, regardless of the underlying filesystem.

Module::Pluggable is used to find all File::Attributes:: modules that inherit from File::Attributes::Base and that are applicable on your system. If it finds one, it uses that. If not, it uses File::Attributes::Simple, which is bundled with this module and works everywhere.

As of version 0.04, plugins are now set up per-file, not per-system. This means that if you have File::Attributes::Extended installed, extended attributes will be used where available, but Simple attributes will be used on files where extended attributes don't work (a FAT filesytem on a Linux machine, for example). Existing simple attributes will be read even if extended attributes are available, but writes will affect only the extended attributes.

This means that you can switch to a better attribute plugin at any time, without losing any old data!

EXPORT

Top

None, by default. Specify the functions that you'd like to use as arguments to the module. If you want everything, specify :all.

FUNCTIONS

Top

All functions throw an exception on error.

get_attribute($file, $attribute)

Returns the value of attribute $attribute on file $file. If $attribute doesn't exist, returns undefined.

set_attribute($file, $attribute => $value)

Sets attribute $<attribute> on file $file to $value.

get_attributes($file)

Returns a hash of all attributes on $file.

set_attributes($file, %hash)

Sets the attributes named by the keys of %hash to the value contained in %hash. Note that this operation is not atomic -- if setting an individual attribute fails, the attributes on $file may not be the same as before set_attributes was called!

unset_attribute($file, $attribute)

Removes the attribute $attribute from $file.

unset_attributes($file, $attribute0, [$attribute1, ...])

Removes each attribute ($attribute0, $attribute1, ...) from $file.

list_attributes($file)

Returns a list of the attributes on $file. Equivalent to (but faster than) keys get_attributes($file).

AUTHOR

Top

Jonathan Rockway, <jrockway at cpan.org>

BUGS

Top

Please report any bugs or feature requests to RT at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=File-Attributes.

COPYRIGHT & LICENSE

Top


File-Attributes documentation Contained in the File-Attributes distribution.
package File::Attributes;

# please see POD after __END__

use warnings;
use strict;
use Carp;
our $VERSION = '0.04';

# modules that we require
use Module::Pluggable ( search_path => 'File::Attributes',
		        instantiate => 'new' );

# exporting business
require Exporter;
our @ISA = qw(Exporter);
our @EXPORT = ();
our @EXPORT_OK   = qw(set_attribute   set_attributes
		      get_attribute   get_attributes
		      unset_attribute unset_attributes
		      list_attributes);
our %EXPORT_TAGS = (all => [@EXPORT_OK]);

# internal variables
my @modules; # the modules to call, in order

sub _foreach_plugin(@&){
    my @args = @_;
    my $file = $args[0];
    my $code = pop @args;

    croak "$file does not exist" if !-e $file;

    foreach my $plugin (@modules){
	next if !$plugin->applicable($file);
	my @result = $code->($plugin, @args);
	if(@result){
	    return @result if wantarray;
	    return $result[0];
	}
    }
}

sub set_attribute {
    _foreach_plugin @_, sub { my $p = shift; $p->set(@_) };
    return;
}

sub set_attributes {
    my $file  = shift;
    my $first = shift;

    # if someone passes a hashref instead, handle that nicely
    my %attributes;
    if(ref $first){
	%attributes = %{$first};
    }
    else {
	%attributes = ($first, @_);
    }    
    
    foreach my $key (keys %attributes){
	set_attribute($file, $key, $attributes{$key});
    }
}

sub get_attribute {
    return _foreach_plugin @_, sub { my $p = shift; $p->get(@_) };
}

sub get_attributes {
    my $file = shift;
    my @attributes = list_attributes($file);
    my %result;
    foreach my $attribute (@attributes){
	$result{$attribute} = get_attribute($file, $attribute);
    }
    return %result;
}

sub unset_attribute {
    _foreach_plugin @_, 
      sub { my $p = shift; 
	    $p->unset(@_); 
	    return; # force unset on all plugins
	};
    return;
}

sub unset_attributes {
    my $file = shift;
    my @attributes = @_;
    foreach my $attribute (@attributes){
	unset_attribute($file, $attribute);
    }
    return;
}

sub list_attributes {
    my @result;
    _foreach_plugin @_, 
      sub { 
	  my $p = shift;
	  push @result, $p->list(@_);
	  return; # force examination of all plugins
      };
    my %result = map { $_ => 1 } @result; # filter out dupes
    return keys %result;
}

sub _init {
    my $simple;
    foreach my $plugin (plugins()){
	eval {
	    push @modules, $plugin
	      if $plugin->isa('File::Attributes::Base') && 
		$plugin->priority > 0;
	};
    }

    # sort from highest priority to lowest
    @modules = reverse sort {$a->priority <=> $b->priority} @modules;
    
    return scalar @modules;
}

sub _modules {
    return map {/(.+)=[A-Z]+/; $1;} @modules;
}

return _init(); # returns true if the module can be used
__END__

1; # End of File::Attributes