File::ExtAttr::Tie - Tie interface to extended attributes of files


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

Index


Code Index:

NAME

Top

File::ExtAttr::Tie - Tie interface to extended attributes of files

SYNOPSIS

Top

  use File::ExtAttr::Tie;
  use Data::Dumper;

  tie %a,
    "File::ExtAttr::Tie", "/Applications (Mac  OS 9)/Sherlock 2",
    { namespace => 'user' };
  print Dumper \%a;

produces:

  $VAR1 = {
           'com.apple.FinderInfo' => 'APPLfndf!?',
           'com.apple.ResourceFork' => '?p?p5I'
          };

DESCRIPTION

Top

File::ExtAttr::Tie provides access to extended attributes of a file through a tied hash. Creating a new key creates a new extended attribute associated with the file. Modifying the value or removing a key likewise modifies/removes the extended attribute.

Internally this module uses the File::ExtAttr module. So it has the same restrictions as that module in terms of OS support.

METHODS

Top

tie "File::ExtAttr::Tie", $filename, [\%flags]

The flags are the same optional flags as in File::ExtAttr. Any flags given here will be passed to all operations on the tied hash. Only the namespace flag makes sense. The hash will be tied to the default namespace, if no flags are given.

SEE ALSO

Top

File::ExtAttr

AUTHOR

Top

David Leadbeater, http://dgl.cx/contact

Documentation by Richard Dawe, <richdawe@cpan.org>

COPYRIGHT AND LICENSE

Top


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

package File::ExtAttr::Tie;

use strict;
use base qw(Tie::Hash);
use File::ExtAttr qw(:all);

our $VERSION = '0.01';

sub TIEHASH {
  my($class, $file, $flags) = @_;
  my $self = bless { file => $file }, ref $class || $class;
  $self->{flags} = defined($flags) ? $flags : {};
  return $self;
}

sub STORE {
  my($self, $name, $value) = @_;
  return undef unless setfattr($self->{file}, $name, $value, $self->{flags});
  $value;
}

sub FETCH {
  my($self, $name) = @_;
  return getfattr($self->{file}, $name, $self->{flags});
}

sub FIRSTKEY {
  my($self) = @_;
  $self->{each_list} = [listfattr($self->{file}, $self->{flags})];
  shift @{$self->{each_list}};
}

sub NEXTKEY {
  my($self) = @_;
  shift @{$self->{each_list}};
}

sub EXISTS {
  my($self, $name) = @_;
  return getfattr($self->{file}, $name, $self->{flags}) ne undef;
}

sub DELETE {
  my($self, $name) = @_;
  # XXX: Race condition
  my $value = getfattr($self->{file}, $name, $self->{flags});
  return $value if delfattr($self->{file}, $name, $self->{flags});
  undef;
}

sub CLEAR {
  my($self) = @_;
  for(listfattr($self->{file})) {
    delfattr($self->{file}, $_, $self->{flags});
  }
}

#sub SCALAR { }