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   291648 use strict;
  4         8  
  4         126  
4 4     4   15 use warnings;
  4         7  
  4         225  
5              
6 4     4   973 use parent 'URI::_generic';
  4         744  
  4         18  
7             our $VERSION = '5.35';
8              
9 4     4   255 use URI::Escape qw(uri_unescape);
  4         6  
  4         405  
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 589 my($OS) = shift || $^O;
29              
30 140   100     579 my $class = "URI::file::" . ($OS_CLASS{$OS} || "Unix");
31 4     4   20 no strict 'refs';
  4         5  
  4         2123  
32 140 100       199 unless (%{"$class\::"}) {
  140         776  
33 10         728 eval "require $class";
34 10 50       49 die $@ if $@;
35             }
36 140         675 $class;
37             }
38              
39 2     2 1 6 sub host { uri_unescape(shift->authority(@_)) }
40              
41             sub new
42             {
43 51     51 1 15803 my($class, $path, $os) = @_;
44 51         189 os_class($os)->new($path);
45             }
46              
47             sub new_abs
48             {
49 9     9 1 4007 my $class = shift;
50 9         50 my $file = $class->new(@_);
51 9 100       77 return $file->abs($class->cwd) unless $$file =~ /^file:/;
52 4         29 $file;
53             }
54              
55             sub cwd
56             {
57 6     6 1 344079 my $class = shift;
58 6         56 require Cwd;
59 6         24383 my $cwd = Cwd::cwd();
60 6 50       167 $cwd = VMS::Filespec::unixpath($cwd) if $^O eq 'VMS';
61 6         134 $cwd = $class->new($cwd);
62 6 100       170 $cwd .= "/" unless substr($cwd, -1, 1) eq "/";
63 6         156 $cwd;
64             }
65              
66             sub canonical {
67 46     46 1 46 my $self = shift;
68 46         114 my $other = $self->SUPER::canonical;
69              
70 46         105 my $scheme = $other->scheme;
71 46         91 my $auth = $other->authority;
72 46 50 66     83 return $other if !defined($scheme) && !defined($auth); # relative
73              
74 40 50 66     106 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     111 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         71 $other;
91             }
92              
93             sub file
94             {
95 89     89 1 9785 my($self, $os) = @_;
96 89         199 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__