MimeInfo.pm

filter: Statementstmt 91.5% 205 / 224 Branchbran 65.1% 125 / 192 Conditioncond 51.7% 15 / 29 MC/DCmcdc 33.3% 6 / 18 Subroutinesub 100% 24 / 24 Podpod 100% 13 / 13 totaltotal 77.6% 388 / 500 CC 111 SCAR 28.9
CC
111
Cov
78%
default
44.7
_hash_globs
25.0
inodetype
19.8
CRAP 17.9
« MimeInfo/Applications.pm ↑ Summary MimeInfo/Magic.pm »
1 package File::MimeInfo;
2  
3 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 9 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 9 ▶use Fcntl 'SEEK_SET';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 9 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 9 ▶use File::BaseDir qw/data_files/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 9 require Exporter;
10  
11 9 our @ISA = qw(Exporter);
12 9 our @EXPORT = qw(mimetype);
13 9 our @EXPORT_OK = qw(extensions describe globs inodetype mimetype_canon mimetype_isa);
14 9 our $VERSION = '0.37';
15 9 our $DEBUG;
16  
17 9 our ($_hashed, $_hashed_aliases, $_hashed_subclasses, $_has_mimeinfo_database);
18 9 our (@globs, %literal, %extension, %mime2ext, %aliases, %subclasses);
19 9 our ($LANG, @DIRS);
20 # @globs = [ [ 'glob', qr//, $mime_string ], ... ]
21 # %literal contains literal matches
22 # %extension contains extensions (globs matching /^\*(\.\w)+$/ )
23 # %mime2ext is used for looking up extension by mime type
24 # %aliases contains the aliases table
25 # %subclasses contains the subclasses table
26 # $LANG can be used to set a default language for the comments
27 # @DIRS can be used to specify custom database directories
28  
29 1 ▶sub new { bless \$VERSION, shift } # what else is there to bless ?
Subroutine new: called CC=1 SCAR=0.0
Pod documented
30  
31 21 ▶sub mimetype {
Subroutine mimetype: called CC=4 SCAR=13.9
Pod documented
32 21 my $file = pop;
33 21 ▶ croak 'subroutine "mimetype" needs a filename as argument' unless defined $file;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file 0 21 21
34 return
35 21 ▶ inodetype($file) ||
Condition100%6/6
inodetype($file) || globs($file) || default($file)
l!l&&r!l&&!r
1551
inodetype($file) or globs($file)
l!l&&r!l&&!r
2136
MC/DC100%3/3
inodetype($file) || globs($file) || default($file)
inodetype($file) globs($file) default($file)
Truth table100%4/4
A or B || C
A= inodetype($file)B= globs($file)C= default($file)
ABCresult
1XX1
01X1
0011
0000
36 globs($file) ||
37 default($file);
38 }
39  
40 29 ▶sub inodetype {
Subroutine inodetype: called CC=14 SCAR=29.9
Pod documented
41 29 my $file = pop;
42 29 ▶ print STDERR "> Checking inode type\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $File::MimeInfo::DEBUG 0 29 29
43 29 ▶ lstat $file or return undef;
Branch: 1 branch
BranchTrueFalseTotal
lstat $file or return undef 16 13 29
44 16 ▶ return undef if -f _;
Branch: 1 branch
BranchTrueFalseTotal
if -f _ 14 2 16
45 2 ▶ my $t = (-l $file) ? 'inode/symlink' : # Win32 does not like '_' here
Branch: 6 branches
BranchTrueFalseTotal
-l $file ? : 1 1 2
-d _ ? : 1 0 1
-p _ ? : 0 0 0
-c _ ? : 0 0 0
-b _ ? : 0 0 0
-S _ ? : 0 0 0
46 (-d _) ? 'inode/directory' :
47 (-p _) ? 'inode/fifo' :
48 (-c _) ? 'inode/chardevice' :
49 (-b _) ? 'inode/blockdevice' :
50 (-S _) ? 'inode/socket' : '' ;
51 2 ▶ if ($t eq 'inode/directory') { # compare devices to detect mount-points
Branch: 1 branch
BranchTrueFalseTotal
if ($t eq 'inode/directory') { } 1 1 2
52 1 my $dev = (stat _)[0]; # device of the node under investigation
53 1 $file = File::Spec->rel2abs($file); # get full path
54 1 my @dirs = File::Spec->splitdir($file);
55 1 $file = File::Spec->catfile(@dirs); # removes trailing '/' or equivalent
56 1 ▶ return $t if -l $file; # parent can be on other dev for links
Branch: 1 branch
BranchTrueFalseTotal
if -l $file 0 1 1
57 1 pop @dirs;
58 1 my $dir = File::Spec->catdir(@dirs); # parent dir
59 1 ▶ $t = 'inode/mount-point' unless (stat $dir)[0] == $dev; # compare devices
Branch: 1 branch
BranchTrueFalseTotal
unless (stat $dir)[0] == $dev 0 1 1
60 1 return $t;
61 }
62 1 ▶ else { return $t ? $t : undef }
Branch: 1 branch
BranchTrueFalseTotal
$t ? : 1 0 1
63 }
64  
65 25 ▶sub globs {
Subroutine globs: called CC=15 SCAR=27.4
Pod documented
66 25 my $file = pop;
67 25 ▶ croak 'subroutine "globs" needs a filename as argument' unless defined $file;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file 0 25 25
68 25 ▶ rehash() unless $_hashed;
Branch: 1 branch
BranchTrueFalseTotal
unless $File::MimeInfo::_hashed 1 24 25
69 25 (undef, undef, $file) = File::Spec->splitpath($file); # remove path
70 25 ▶ print STDERR "> Checking globs for basename '$file'\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $File::MimeInfo::DEBUG 0 25 25
71  
72 25 ▶ return $literal{$file} if exists $literal{$file};
Branch: 1 branch
BranchTrueFalseTotal
if exists $File::MimeInfo::literal{$file} 1 24 25
73  
74 24 ▶ if ($file =~ /\.(\w+(\.\w+)*)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file =~ /\.(\w+(\.\w+)*)$/) 11 13 24
75 11 my @ext = split /\./, $1;
76 11 ▶ while (@ext) {
Branch: 1 branch
BranchTrueFalseTotal
if (@ext) 16 1 17
77 16 my $ext = join('.', @ext);
78 16 ▶ print STDERR "> Checking for extension '.$ext'\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $File::MimeInfo::DEBUG 0 16 16
79 16 ▶ carp "WARNING: wantarray behaviour of globs() will change in the future.\n" if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 0 16 16
80 return wantarray
81 ? ($extension{$ext}, $ext)
82 : $extension{$ext}
83 16 ▶ if exists $extension{$ext};
Branch: 2 branches
BranchTrueFalseTotal
if exists $File::MimeInfo::extension{$ext} 10 6 16
wantarray ? : 0 10 10
84 6 shift @ext;
85 }
86 }
87  
88 14 for (@globs) {
89 14 ▶ next unless $file =~ $_->[1];
Branch: 1 branch
BranchTrueFalseTotal
unless $file =~ /$_->[1]/ 12 2 14
90 2 ▶ print STDERR "> This file name matches \"$_->[0]\"\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $File::MimeInfo::DEBUG 0 2 2
91 2 return $_->[2];
92 }
93  
94 12 ▶ return globs(lc $file) if $file =~ /[A-Z]/; # recurs
Branch: 1 branch
BranchTrueFalseTotal
if $file =~ /[A-Z]/ 1 11 12
95 11 return undef;
96 }
97  
98 8 ▶sub default {
Subroutine default: called CC=19 SCAR=38.0
Pod documented
99 8 my $file = pop;
100 8 ▶ croak 'subroutine "default" needs a filename as argument' unless defined $file;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file 0 8 8
101  
102 8 my $line;
103 8 ▶ unless (ref $file) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $file) { } 0 8 8
if (do { ref $file eq 'Path::Tiny' }) { } 0 0 0
104 8 ▶ return undef unless -f $file;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $file 1 7 8
105 7 ▶ print STDERR "> File exists, trying default method\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $File::MimeInfo::DEBUG 0 7 7
106 7 ▶ return 'text/plain' if -z $file;
Branch: 1 branch
BranchTrueFalseTotal
if -z $file 1 6 7
107  
108 6 ▶ open FILE, '<', $file or return undef;
Branch: 1 branch
BranchTrueFalseTotal
open FILE, "<", $file or return undef 6 0 6
109 6 ▶ binmode FILE, ':utf8' unless $] < 5.008;
Branch: 1 branch
BranchTrueFalseTotal
unless $] < "5.008" 6 0 6
110 6 read FILE, $line, 32;
111 6 close FILE;
112 }
113 elsif (ref $file eq 'Path::Tiny') {
114 0 ▶ return undef unless $file->exists;
Branch: 1 branch
BranchTrueFalseTotal
unless $file->exists 0 0 0
115 0 ▶ print STDERR "> File is Path::Tiny object and exists, "
Branch: 1 branch
BranchTrueFalseTotal
if $File::MimeInfo::DEBUG 0 0 0
116 . "trying default method\n" if $DEBUG;
117 0 ▶ open my $fh, '<', $file or return undef;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $file or return undef 0 0 0
118 0 ▶ binmode $fh, ':utf8' unless $] < 5.008;
Branch: 1 branch
BranchTrueFalseTotal
unless $] < "5.008" 0 0 0
119 0 read $fh, $line, 32;
120 0 close $fh;
121 }
122 else {
123 0 ▶ print STDERR "> Trying default method on object\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $File::MimeInfo::DEBUG 0 0 0
124  
125 0 $file->seek(0, SEEK_SET);
126 0 $file->read($line, 32);
127 }
128  
129 {
130 9 ▶ no warnings; # warnings can be thrown when input not ascii
Subroutine BEGIN: called CC=1 SCAR=0.0
131 6 ▶ if ($] < 5.008 or ! utf8::valid($line)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($] < 5.008 or not utf8::valid($line)) { } 2 4 6
Condition66%2/3
$] < "5.008" or not utf8::valid($line)
l!l&&r!l&&!r
024
MC/DC50%1/2
$] < "5.008" or not utf8::valid($line)
$] < "5.008" not utf8::valid($line)
Truth table66%2/3
A or not(B)
A= $] < "5.008"B= not utf8::valid($line)
ABresult
1X1
011
000
132 9 ▶ use bytes; # avoid invalid utf8 chars
Subroutine BEGIN: called CC=1 SCAR=0.0
133 2 $line =~ s/\s//g; # \m, \n and \t are also control chars
134 2 ▶ return 'text/plain' unless $line =~ /[\x00-\x1F\x7F]/;
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ /[\x00-\x1F\x7F]/ 0 2 2
135 }
136 else {
137 # use perl to do something intelligent for ascii & utf8
138 4 ▶ return 'text/plain' unless $line =~ /[^[:print:]\s]/;
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ /[^[:print:]\s]/ 3 1 4
139 }
140 }
141 3 ▶ print STDERR "> First 10 bytes of the file contain control chars\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $File::MimeInfo::DEBUG 0 3 3
142 3 return 'application/octet-stream';
143 }
144  
145 5 ▶sub rehash {
Subroutine rehash: called CC=6 SCAR=18.0
Pod documented
146 5 (@globs, %literal, %extension, %mime2ext) = (); # clear all data
147 5 local $_; # limit scope of $_ ... :S
148 my @globfiles = @DIRS
149 5 ▶ ? ( grep {-e $_ && -r $_} map "$_/globs", @DIRS )
Branch: 1 branch
BranchTrueFalseTotal
@File::MimeInfo::DIRS ? : 1 4 5
Condition33%1/3
-e $_ && -r $_
!ll&&!rl&&r
001
MC/DC0%0/2
-e $_ && -r $_
-e $_ -r $_
Truth table33%1/3
A && B
A= -e $_B= -r $_
ABresult
0X0
100
111
150 : ( reverse data_files('mime/globs') );
151 5 ▶ if (@globfiles) {
Branch: 1 branch
BranchTrueFalseTotal
if (@globfiles) { } 3 2 5
152 3 $_has_mimeinfo_database = 1;
153 } else {
154 2 carp "WARNING: You don't seem to have a mime-info database. " .
155 "The shared-mime-info package is available from http://freedesktop.org/";
156 }
157 5 my @done;
158 5 for my $file (@globfiles) {
159 5 ▶ next if grep {$file eq $_} @done;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$file eq $_;} @done 2 3 5
160 3 _hash_globs($file);
161 3 push @done, $file;
162 }
163 5 $_hashed = 1;
164 }
165  
166 3 ▶sub _hash_globs {
Subroutine _hash_globs: called CC=16 SCAR=32.2
167 3 my $file = shift;
168 3 ▶ open GLOB, '<', $file or croak "Could not open file '$file' for reading" ;
Branch: 1 branch
BranchTrueFalseTotal
open GLOB, "<", $file or croak("Could not open file '${file}' for reading") 3 0 3
169 3 ▶ binmode GLOB, ':utf8' unless $] < 5.008;
Branch: 1 branch
BranchTrueFalseTotal
unless $] < "5.008" 3 0 3
170 3 my ($string, $glob);
171 3 my %seen_extension;
172 3 ▶ while (<GLOB>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline GLOB)) 33 3 36
173 33 ▶ next if /^\s*#/ or ! /\S/; # skip comments and empty lines
Branch: 1 branch
BranchTrueFalseTotal
if /^\s*#/ or not /\S/ 3 30 33
Condition66%2/3
/^\s*#/ or not /\S/
l!l&&r!l&&!r
3030
MC/DC50%1/2
/^\s*#/ or not /\S/
/^\s*#/ not /\S/
Truth table66%2/3
A or not(B)
A= /^\s*#/B= not /\S/
ABresult
1X1
011
000
174 30 chomp;
175 30 ($string, $glob) = split /:/, $_, 2;
176 30 ▶ unless ($glob =~ /[\?\*\[]/) { $literal{$glob} = $string }
Branch: 2 branches
BranchTrueFalseTotal
if ($glob =~ /[\?\*\[]/) { } 27 3 30
if (do { $glob =~ /^\*\.(\w+(\.\w+)*)$/ }) { } 24 3 27
177 elsif ($glob =~ /^\*\.(\w+(\.\w+)*)$/) {
178 24 ▶ next if $seen_extension{$1}++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen_extension{$1}++ 3 21 24
179 21 ▶ if (exists $extension{$1} && $extension{$1} ne $string) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $File::MimeInfo::extension{$1} and $File::MimeInfo::extension{$1} ne $string) 0 21 21
Condition33%1/3
exists $File::MimeInfo::extension{$1} and $File::MimeInfo::extension{$1} ne $string
!ll&&!rl&&r
2100
MC/DC0%0/2
exists $File::MimeInfo::extension{$1} and $File::MimeInfo::extension{$1} ne $string
exists $File::MimeInfo::extension{$1} $File::MimeInfo::extension{$1} ne $string
Truth table33%1/3
A and B
A= exists $File::MimeInfo::extension{$1}B= $File::MimeInfo::extension{$1} ne $string
ABresult
0X0
100
111
180 0 my $old = $extension{$1};
181 0 ▶ if (defined $mime2ext{$old}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $File::MimeInfo::mime2ext{$old}) 0 0 0
182 0 @{$mime2ext{$old}} = grep { $_ ne $1 } @{$mime2ext{$old}};
183 0 ▶ delete $mime2ext{$old} unless @{$mime2ext{$old}};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$File::MimeInfo::mime2ext{$old};} 0 0 0
184 }
185 }
186 21 $extension{$1} = $string;
187 21 ▶ $mime2ext{$string} = [] if !defined($mime2ext{$string});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $File::MimeInfo::mime2ext{$string} 18 3 21
188 21 push @{$mime2ext{$string}}, $1
189 21 ▶ unless grep { $_ eq $1 } @{$mime2ext{$string}};
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ eq $1;} @{$File::MimeInfo::mime2ext{$string};} 21 0 21
190 3 } else { unshift @globs, [$glob, _glob_to_regexp($glob), $string] }
191 }
192 3 ▶ close GLOB or croak "Could not open file '$file' for reading" ;
Branch: 1 branch
BranchTrueFalseTotal
close GLOB or croak("Could not open file '${file}' for reading") 3 0 3
193 }
194  
195 7 ▶sub _glob_to_regexp {
Subroutine _glob_to_regexp: called CC=1 SCAR=0.0
196 7 my $glob = shift;
197 7 $glob =~ s/\./\\./g;
198 7 $glob =~ s/([?*])/.$1/g;
199 7 $glob =~ s/([^\w\/\\\.\?\*\[\]])/\\$1/g;
200 7 qr/^$glob$/;
201 }
202  
203 2 ▶sub has_mimeinfo_database {
Subroutine has_mimeinfo_database: called CC=2 SCAR=7.2
Pod documented
204 2 ▶ rehash() if (!$_hashed);
Branch: 1 branch
BranchTrueFalseTotal
unless $File::MimeInfo::_hashed 2 0 2
205 2 return $_has_mimeinfo_database;
206 }
207  
208 2 ▶sub extensions {
Subroutine extensions: called CC=6 SCAR=18.2
Pod documented
209 2 my $mimet = mimetype_canon(pop @_);
210 2 ▶ rehash() unless $_hashed;
Branch: 1 branch
BranchTrueFalseTotal
unless $File::MimeInfo::_hashed 1 1 2
211 2 ▶ my $ref = $mime2ext{$mimet} if exists $mime2ext{$mimet};
Branch: 1 branch
BranchTrueFalseTotal
if exists $File::MimeInfo::mime2ext{$mimet} 2 0 2
212 2 ▶ return $ref ? @{$ref} : undef if wantarray;
Branch: 2 branches
BranchTrueFalseTotal
if wantarray 1 1 2
$ref ? : 1 0 1
213 1 ▶ return $ref ? @{$ref}[0] : '';
Branch: 1 branch
BranchTrueFalseTotal
$ref ? : 1 0 1
214 }
215  
216 2 ▶sub describe {
Subroutine describe: called CC=14 SCAR=29.3
Pod documented
217 2 ▶ shift if ref $_[0];
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] 0 2 2
218 2 my ($mt, $lang) = @_;
219 2 ▶ croak 'subroutine "describe" needs a mimetype as argument' unless $mt;
Branch: 1 branch
BranchTrueFalseTotal
unless $mt 0 2 2
220 2 $mt = mimetype_canon($mt);
221 2 ▶ $lang = $LANG unless defined $lang;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $lang 2 0 2
222 2 ▶ my $att = $lang ? qq{xml:lang="$lang"} : '';
Branch: 1 branch
BranchTrueFalseTotal
$lang ? : 1 1 2
223 2 my $desc;
224 my @descfiles = @DIRS
225 0 ▶ ? ( grep {-e $_ && -r $_} map "$_/$mt.xml", @DIRS )
Branch: 1 branch
BranchTrueFalseTotal
@File::MimeInfo::DIRS ? : 0 2 2
Condition0%0/3
-e $_ && -r $_
!ll&&!rl&&r
000
MC/DC0%0/2
-e $_ && -r $_
-e $_ -r $_
Truth table0%0/3
A && B
A= -e $_B= -r $_
ABresult
0X0
100
111
226 : ( reverse data_files('mime', split '/', "$mt.xml") ) ;
227 2 for my $file (@descfiles) {
228 2 $desc = ''; # if a file was found, return at least empty string
229 2 ▶ open XML, '<', $file or croak "Could not open file '$file' for reading";
Branch: 1 branch
BranchTrueFalseTotal
open XML, "<", $file or croak("Could not open file '${file}' for reading") 2 0 2
230 2 ▶ binmode XML, ':utf8' unless $] < 5.008;
Branch: 1 branch
BranchTrueFalseTotal
unless $] < "5.008" 2 0 2
231 2 ▶ while (<XML>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline XML)) 20 0 20
232 20 ▶ next unless m!<comment\s*$att>(.*?)</comment>!;
Branch: 1 branch
BranchTrueFalseTotal
unless m[<comment\s*$att>(.*?)</comment>] 18 2 20
233 2 $desc = $1;
234 2 last;
235 }
236 2 ▶ close XML or croak "Could not open file '$file' for reading";
Branch: 1 branch
BranchTrueFalseTotal
close XML or croak("Could not open file '${file}' for reading") 2 0 2
237 2 ▶ last if $desc;
Branch: 1 branch
BranchTrueFalseTotal
if $desc 2 0 2
238 }
239 2 return $desc;
240 }
241  
242 16 ▶sub mimetype_canon {
Subroutine mimetype_canon: called CC=4 SCAR=13.9
Pod documented
243 16 my $mimet = pop;
244 16 ▶ croak 'mimetype_canon needs argument' unless defined $mimet;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $mimet 0 16 16
245 16 ▶ rehash_aliases() unless $_hashed_aliases;
Branch: 1 branch
BranchTrueFalseTotal
unless $File::MimeInfo::_hashed_aliases 2 14 16
246 16 ▶ return exists($aliases{$mimet}) ? $aliases{$mimet} : $mimet;
Branch: 1 branch
BranchTrueFalseTotal
exists $File::MimeInfo::aliases{$mimet} ? : 1 15 16
247 }
248  
249 2 ▶sub rehash_aliases {
Subroutine rehash_aliases: called CC=1 SCAR=0.0
Pod documented
250 2 %aliases = _read_map_files('aliases');
251 2 $_hashed_aliases++;
252 }
253  
254 3 ▶sub _read_map_files {
Subroutine _read_map_files: called CC=12 SCAR=25.9
255 3 my ($name, $list) = @_;
256 my @files = @DIRS
257 0 ▶ ? ( grep {-e $_ && -r $_} map "$_/$name", @DIRS )
Branch: 1 branch
BranchTrueFalseTotal
@File::MimeInfo::DIRS ? : 0 3 3
Condition0%0/3
-e $_ && -r $_
!ll&&!rl&&r
000
MC/DC0%0/2
-e $_ && -r $_
-e $_ -r $_
Truth table0%0/3
A && B
A= -e $_B= -r $_
ABresult
0X0
100
111
258 : ( reverse data_files("mime/$name") );
259 3 my (@done, %map);
260 3 for my $file (@files) {
261 6 ▶ next if grep {$_ eq $file} @done;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ eq $file;} @done 3 3 6
262 3 ▶ open MAP, '<', $file or croak "Could not open file '$file' for reading";
Branch: 1 branch
BranchTrueFalseTotal
open MAP, "<", $file or croak("Could not open file '${file}' for reading") 3 0 3
263 3 ▶ binmode MAP, ':utf8' unless $] < 5.008;
Branch: 1 branch
BranchTrueFalseTotal
unless $] < "5.008" 3 0 3
264 3 ▶ while (my $line = <MAP>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline MAP)) 6 3 9
265 6 ▶ next unless $line =~ m/\S/; # skip empty lines
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ /\S/ 0 6 6
266 6 ▶ next if $line =~ m/^\s*#/; # skip comment lines
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^\s*#/ 0 6 6
267 6 chomp $line;
268 6 my ($k, $v) = split m/\s+/, $line, 2;
269 6 ▶ if ($list) {
Branch: 1 branch
BranchTrueFalseTotal
if ($list) { } 2 4 6
270 2 ▶ $map{$k} = [] unless $map{$k};
Branch: 1 branch
BranchTrueFalseTotal
unless $map{$k} 1 1 2
271 2 push @{$map{$k}}, $v;
272 }
273 4 else { $map{$k} = $v }
274 }
275 3 close MAP;
276 3 push @done, $file;
277 }
278 3 return %map;
279 }
280  
281 5 ▶sub mimetype_isa {
Subroutine mimetype_isa: called CC=10 SCAR=23.0
Pod documented
282 5 ▶ my $parent = pop || croak 'mimetype_isa needs argument';
Condition50%1/2
pop() || croak('mimetype_isa needs argument')
l!l
50
MC/DC0%0/1
pop() || croak('mimetype_isa needs argument')
pop()
Truth table50%1/2
A
A= pop()
Aresult
11
00
283 5 my $mimet = pop;
284 5 ▶ if (ref $mimet or ! defined $mimet) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $mimet or not defined $mimet) { } 2 3 5
Condition66%2/3
ref $mimet or not defined $mimet
l!l&&r!l&&!r
023
MC/DC50%1/2
ref $mimet or not defined $mimet
ref $mimet not defined $mimet
Truth table66%2/3
A or not(B)
A= ref $mimetB= not defined $mimet
ABresult
1X1
011
000
285 2 $mimet = mimetype_canon($parent);
286 2 undef $parent;
287 }
288 else {
289 3 $mimet = mimetype_canon($mimet);
290 3 $parent = mimetype_canon($parent);
291 }
292 5 ▶ rehash_subclasses() unless $_hashed_subclasses;
Branch: 1 branch
BranchTrueFalseTotal
unless $File::MimeInfo::_hashed_subclasses 1 4 5
293  
294 5 my @subc;
295 5 ▶ push @subc, 'inode/directory' if $mimet eq 'inode/mount-point';
Branch: 1 branch
BranchTrueFalseTotal
if $mimet eq "inode/mount-point" 1 4 5
296 5 ▶ push @subc, @{$subclasses{$mimet}} if exists $subclasses{$mimet};
Branch: 1 branch
BranchTrueFalseTotal
if exists $File::MimeInfo::subclasses{$mimet} 2 3 5
297 5 ▶ push @subc, 'text/plain' if $mimet =~ m#^text/#;
Branch: 1 branch
BranchTrueFalseTotal
if $mimet =~ m[^text/] 2 3 5
298 5 ▶ push @subc, 'application/octet-stream' unless $mimet =~ m#^inode/#;
Branch: 1 branch
BranchTrueFalseTotal
unless $mimet =~ m[^inode/] 4 1 5
299  
300 5 ▶ return $parent ? scalar(grep {$_ eq $parent} @subc) : @subc;
Branch: 1 branch
BranchTrueFalseTotal
$parent ? : 3 2 5
301 }
302  
303 1 ▶sub rehash_subclasses {
Subroutine rehash_subclasses: called CC=1 SCAR=0.0
Pod documented
304 1 %subclasses = _read_map_files('subclasses', 'LIST');
305 1 $_hashed_subclasses++;
306 }
307  
308 9 1;
309  
310 __END__
« MimeInfo/Applications.pm ↑ Summary MimeInfo/Magic.pm »