File Coverage

blib/lib/URI/file.pm
Criterion Covered Total %
statement 46 50 92.0
branch 10 18 55.5
condition 14 29 48.2
subroutine 12 13 92.3
pod 7 8 87.5
total 89 118 75.4


line stmt bran cond sub pod time code
1             package URI::file;
2              
3 4     4   394199 use strict;
  4         10  
  4         193  
4 4     4   22 use warnings;
  4         8  
  4         282  
5              
6 4     4   1410 use parent 'URI::_generic';
  4         1120  
  4         31  
7             our $VERSION = '5.34';
8              
9 4     4   387 use URI::Escape qw(uri_unescape);
  4         7  
  4         570  
10              
11             our $DEFAULT_AUTHORITY = "";
12              
13             # Map from $^O values to implementation classes. The Unix
14             # class is the default.
15             our %OS_CLASS = (
16             os2 => "OS2",
17             mac => "Mac",
18             MacOS => "Mac",
19             MSWin32 => "Win32",
20             win32 => "Win32",
21             msdos => "FAT",
22             dos => "FAT",
23             qnx => "QNX",
24             );
25              
26             sub os_class
27             {
28 140   66 140 0 616 my($OS) = shift || $^O;
29              
30 140   100     620 my $class = "URI::file::" . ($OS_CLASS{$OS} || "Unix");
31 4     4   25 no strict 'refs';
  4         7  
  4         2680  
32 140 100       201 unless (%{"$class\::"}) {
  140         779  
33 10         994 eval "require $class";
34 10 50       58 die $@ if $@;
35             }
36 140         730 $class;
37             }
38              
39 2     2 1 9 sub host { uri_unescape(shift->authority(@_)) }
40              
41             sub new
42             {
43 51     51 1 19827 my($class, $path, $os) = @_;
44 51         190 os_class($os)->new($path);
45             }
46              
47             sub new_abs
48             {
49 9     9 1 5245 my $class = shift;
50 9         44 my $file = $class->new(@_);
51 9 100       55 return $file->abs($class->cwd) unless $$file =~ /^file:/;
52 4         26 $file;
53             }
54              
55             sub cwd
56             {
57 6     6 1 362204 my $class = shift;
58 6         45 require Cwd;
59 6         28830 my $cwd = Cwd::cwd();
60 6 50       161 $cwd = VMS::Filespec::unixpath($cwd) if $^O eq 'VMS';
61 6         111 $cwd = $class->new($cwd);
62 6 100       139 $cwd .= "/" unless substr($cwd, -1, 1) eq "/";
63 6         124 $cwd;
64             }
65              
66             sub canonical {
67 46     46 1 62 my $self = shift;
68 46         101 my $other = $self->SUPER::canonical;
69              
70 46         88 my $scheme = $other->scheme;
71 46         118 my $auth = $other->authority;
72 46 50 66     91 return $other if !defined($scheme) && !defined($auth); # relative
73              
74 40 50 66     117 if (!defined($auth) ||
      66        
      33        
      33        
75             $auth eq "" ||
76             lc($auth) eq "localhost" ||
77             (defined($DEFAULT_AUTHORITY) && lc($auth) eq lc($DEFAULT_AUTHORITY))
78             )
79             {
80             # avoid cloning if $auth already match
81 36 0 33     118 if ((defined($auth) || defined($DEFAULT_AUTHORITY)) &&
      0        
      33        
82             (!defined($auth) || !defined($DEFAULT_AUTHORITY) || $auth ne $DEFAULT_AUTHORITY)
83             )
84             {
85 0 0       0 $other = $other->clone if $self == $other;
86 0         0 $other->authority($DEFAULT_AUTHORITY);
87             }
88             }
89              
90 40         81 $other;
91             }
92              
93             sub file
94             {
95 89     89 1 13989 my($self, $os) = @_;
96 89         216 os_class($os)->file($self);
97             }
98              
99             sub dir
100             {
101 0     0 1   my($self, $os) = @_;
102 0           os_class($os)->dir($self);
103             }
104              
105             1;
106              
107             __END__