| 1 | package File::Attributes::Recursive; | ||
| 2 | |||
| 3 | use warnings; | ||
| 4 | use strict; | ||
| 5 | |||
| 6 | our $VERSION = '0.02'; | ||
| 7 | require Exporter; | ||
| 8 | |||
| 9 | our @ISA = qw(Exporter); | ||
| 10 | our @EXPORT_OK = qw(get_attribute_recursively get_attributes_recursively | ||
| 11 | list_attributes_recursively); | ||
| 12 | |||
| 13 | our %EXPORT_TAGS = (all => \@EXPORT_OK); | ||
| 14 | |||
| 15 | use File::Attributes qw(get_attribute list_attributes); | ||
| 16 | use Path::Class; | ||
| 17 | use Cwd qw(abs_path); | ||
| 18 | use Carp; | ||
| 19 | |||
| 20 | sub get_attribute_recursively { | ||
| 21 | my $file = shift; | ||
| 22 | my $top = shift; | ||
| 23 | my $attribute = shift; | ||
| 24 | |||
| 25 | if(!defined $attribute){ | ||
| 26 | $attribute = $top; | ||
| 27 | $top = '/'; | ||
| 28 | } | ||
| 29 | |||
| 30 | $file = file($file)->absolute; | ||
| 31 | $top = dir($top)->absolute; | ||
| 32 | |||
| 33 | if(!$top->subsumes($file)){ | ||
| 34 | croak "get_attribute_recursively: filename ($file) must ". | ||
| 35 | "contain top ($top)"; | ||
| 36 | } | ||
| 37 | |||
| 38 | my $result; | ||
| 39 | while($top->subsumes($file)){ | ||
| 40 | eval { | ||
| 41 | $result = get_attribute($file, $attribute); | ||
| 42 | }; | ||
| 43 | |||
| 44 | last if defined $result; | ||
| 45 | |||
| 46 | $file = $file->parent; | ||
| 47 | } | ||
| 48 | |||
| 49 | return $result; | ||
| 50 | } | ||
| 51 | |||
| 52 | sub get_attributes_recursively { | ||
| 53 | my $file = shift; | ||
| 54 | my $top = shift; | ||
| 55 | |||
| 56 | $top = '/' if !defined $top; | ||
| 57 | |||
| 58 | $file = file($file)->absolute; | ||
| 59 | $top = dir($top)->absolute; | ||
| 60 | |||
| 61 | if(!$top->subsumes($file)){ | ||
| 62 | croak "get_attributes_recursively: filename ($file) must ". | ||
| 63 | "contain top ($top)"; | ||
| 64 | } | ||
| 65 | |||
| 66 | my %result; | ||
| 67 | while($top->subsumes($file)){ | ||
| 68 | my @attributes = list_attributes($file); | ||
| 69 | |||
| 70 | foreach my $attribute (@attributes){ | ||
| 71 | next if exists $result{$attribute}; | ||
| 72 | eval { | ||
| 73 | $result{$attribute} = get_attribute($file, $attribute); | ||
| 74 | }; | ||
| 75 | } | ||
| 76 | |||
| 77 | $file = $file->parent; | ||
| 78 | } | ||
| 79 | |||
| 80 | return %result; | ||
| 81 | } | ||
| 82 | |||
| 83 | sub list_attributes_recursively { | ||
| 84 | my $file = shift; | ||
| 85 | my $top = shift; | ||
| 86 | |||
| 87 | $top = '/' if !defined $top; | ||
| 88 | |||
| 89 | $file = file($file)->absolute; | ||
| 90 | $top = dir($top)->absolute; | ||
| 91 | |||
| 92 | if(!$top->subsumes($file)){ | ||
| 93 | croak "get_attributes_recursively: filename ($file) must ". | ||
| 94 | "contain top ($top)"; | ||
| 95 | } | ||
| 96 | |||
| 97 | my %results; | ||
| 98 | while($top->subsumes($file)){ | ||
| 99 | eval { | ||
| 100 | my @subresults = list_attributes($file); | ||
| 101 | @results{@subresults} = @subresults; | ||
| 102 | }; | ||
| 103 | $file = $file->parent; | ||
| 104 | } | ||
| 105 | |||
| 106 | return keys %results; | ||
| 107 | } | ||
| 108 | |||
| 109 | __END__ |