| 1 | package File::Attributes; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | # please see POD after __END__ | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 7 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 7 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 7 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 7 | our $VERSION = '0.04'; | |||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | # modules that we require | ||||||||||||||||||||||||||||
| 11 | 7 | ▶ | use Module::Pluggable ( search_path => 'File::Attributes', | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 7 | instantiate => 'new' ); | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | # exporting business | ||||||||||||||||||||||||||||
| 15 | 7 | require Exporter; | |||||||||||||||||||||||||||
| 16 | 7 | our @ISA = qw(Exporter); | |||||||||||||||||||||||||||
| 17 | 7 | our @EXPORT = (); | |||||||||||||||||||||||||||
| 18 | 7 | our @EXPORT_OK = qw(set_attribute set_attributes | |||||||||||||||||||||||||||
| 19 | get_attribute get_attributes | ||||||||||||||||||||||||||||
| 20 | unset_attribute unset_attributes | ||||||||||||||||||||||||||||
| 21 | list_attributes); | ||||||||||||||||||||||||||||
| 22 | 7 | our %EXPORT_TAGS = (all => [@EXPORT_OK]); | |||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | # internal variables | ||||||||||||||||||||||||||||
| 25 | 7 | my @modules; # the modules to call, in order | |||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 29 | ▶ | sub _foreach_plugin(@&){ | ||||||||||||||||||||||||||
|
Subroutine
_foreach_plugin: called CC=6 SCAR=18.0
| |||||||||||||||||||||||||||||
| 28 | 29 | my @args = @_; | |||||||||||||||||||||||||||
| 29 | 29 | my $file = $args[0]; | |||||||||||||||||||||||||||
| 30 | 29 | my $code = pop @args; | |||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 29 | ▶ | croak "$file does not exist" if !-e $file; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 28 | foreach my $plugin (@modules){ | |||||||||||||||||||||||||||
| 35 | 28 | ▶ | next if !$plugin->applicable($file); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 36 | 28 | my @result = $code->($plugin, @args); | |||||||||||||||||||||||||||
| 37 | 28 | ▶ | if(@result){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | 18 | ▶ | return @result if wantarray; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 39 | 18 | return $result[0]; | |||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 7 | ▶ | sub set_attribute { | ||||||||||||||||||||||||||
|
Subroutine
set_attribute: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 45 | 7 | ▶ | _foreach_plugin @_, sub { my $p = shift; $p->set(@_) }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 46 | 7 | return; | |||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 1 | ▶ | sub set_attributes { | ||||||||||||||||||||||||||
|
Subroutine
set_attributes: called CC=3 SCAR=11.6
Pod
documented
| |||||||||||||||||||||||||||||
| 50 | 1 | my $file = shift; | |||||||||||||||||||||||||||
| 51 | 1 | my $first = shift; | |||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | # if someone passes a hashref instead, handle that nicely | ||||||||||||||||||||||||||||
| 54 | 1 | my %attributes; | |||||||||||||||||||||||||||
| 55 | 1 | ▶ | if(ref $first){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | 0 | %attributes = %{$first}; | |||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | else { | ||||||||||||||||||||||||||||
| 59 | 1 | %attributes = ($first, @_); | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 1 | foreach my $key (keys %attributes){ | |||||||||||||||||||||||||||
| 63 | 3 | set_attribute($file, $key, $attributes{$key}); | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 11 | ▶ | sub get_attribute { | ||||||||||||||||||||||||||
|
Subroutine
get_attribute: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 68 | 11 | ▶ | return _foreach_plugin @_, sub { my $p = shift; $p->get(@_) }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 3 | ▶ | sub get_attributes { | ||||||||||||||||||||||||||
|
Subroutine
get_attributes: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 72 | 3 | my $file = shift; | |||||||||||||||||||||||||||
| 73 | 3 | my @attributes = list_attributes($file); | |||||||||||||||||||||||||||
| 74 | 3 | my %result; | |||||||||||||||||||||||||||
| 75 | 3 | foreach my $attribute (@attributes){ | |||||||||||||||||||||||||||
| 76 | 5 | $result{$attribute} = get_attribute($file, $attribute); | |||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | 3 | return %result; | |||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 5 | ▶ | sub unset_attribute { | ||||||||||||||||||||||||||
|
Subroutine
unset_attribute: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 82 | _foreach_plugin @_, | ||||||||||||||||||||||||||||
| 83 | 5 | ▶ | sub { my $p = shift; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 84 | 5 | $p->unset(@_); | |||||||||||||||||||||||||||
| 85 | 5 | return; # force unset on all plugins | |||||||||||||||||||||||||||
| 86 | 5 | }; | |||||||||||||||||||||||||||
| 87 | 5 | return; | |||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 1 | ▶ | sub unset_attributes { | ||||||||||||||||||||||||||
|
Subroutine
unset_attributes: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 91 | 1 | my $file = shift; | |||||||||||||||||||||||||||
| 92 | 1 | my @attributes = @_; | |||||||||||||||||||||||||||
| 93 | 1 | foreach my $attribute (@attributes){ | |||||||||||||||||||||||||||
| 94 | 3 | unset_attribute($file, $attribute); | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | 1 | return; | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 6 | ▶ | sub list_attributes { | ||||||||||||||||||||||||||
|
Subroutine
list_attributes: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 100 | 6 | my @result; | |||||||||||||||||||||||||||
| 101 | _foreach_plugin @_, | ||||||||||||||||||||||||||||
| 102 | 5 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 103 | 5 | my $p = shift; | |||||||||||||||||||||||||||
| 104 | 5 | push @result, $p->list(@_); | |||||||||||||||||||||||||||
| 105 | 5 | return; # force examination of all plugins | |||||||||||||||||||||||||||
| 106 | 6 | }; | |||||||||||||||||||||||||||
| 107 | 5 | my %result = map { $_ => 1 } @result; # filter out dupes | |||||||||||||||||||||||||||
| 108 | 5 | return keys %result; | |||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 7 | ▶ | sub _init { | ||||||||||||||||||||||||||
|
Subroutine
_init: called CC=4 SCAR=14.0
| |||||||||||||||||||||||||||||
| 112 | 7 | my $simple; | |||||||||||||||||||||||||||
| 113 | 7 | foreach my $plugin (plugins()){ | |||||||||||||||||||||||||||
| 114 | 14 | eval { | |||||||||||||||||||||||||||
| 115 | 14 | ▶ | push @modules, $plugin | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$plugin->isa("File::Attributes::Base") and $plugin->priority > 0
MC/DC50%1/2
$plugin->isa("File::Attributes::Base") and $plugin->priority > 0
$plugin->isa("File::Attributes::Base")
$plugin->priority > 0
Truth table66%2/3
A and B
A= $plugin->isa("File::Attributes::Base")B= $plugin->priority > 0
| |||||||||||||||||||||||||||||
| 116 | if $plugin->isa('File::Attributes::Base') && | ||||||||||||||||||||||||||||
| 117 | $plugin->priority > 0; | ||||||||||||||||||||||||||||
| 118 | }; | ||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | # sort from highest priority to lowest | ||||||||||||||||||||||||||||
| 122 | 0 | @modules = reverse sort {$a->priority <=> $b->priority} @modules; | |||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 7 | return scalar @modules; | |||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 3 | ▶ | sub _modules { | ||||||||||||||||||||||||||
|
Subroutine
_modules: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 128 | 3 | return map {/(.+)=[A-Z]+/; $1;} @modules; | |||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 7 | return _init(); # returns true if the module can be used | |||||||||||||||||||||||||||
| 132 | __END__ | ||||||||||||||||||||||||||||