blib/lib/HTML/Location.pm

filter: Statementstmt 98.5% 66 / 67 Branchbran 64.2% 18 / 28 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 100% 19 / 19 Podpod 100% 7 / 7 totaltotal 85.5% 112 / 131 CC 17 SCAR 12.4
CC
17
Cov
85%
new
7.6
param
5.3
__eq
4.0
CRAP 3.4
↑ Summary
1 package HTML::Location;
2  
3 =pod
4  
5 =head1 NAME
6  
7 HTML::Location - Working with disk to URI file mappings (deprecated: see URI::ToDisk)
8  
9 =head1 STATUS
10  
11 As correctly noted by several users, C<HTML::Location> is a really stupid
12 name for this module. I apologise, I was new to the whole CPAN game at the
13 time I first wrote it.
14  
15 B<This module has been relocated> to L<URI::ToDisk>. This module will
16 remain indefinately for back-compatibility, but should otherwise be
17 B<considered deprecated>.
18  
19 Please convert your code to the otherwise identical L<URI::ToDisk> at
20 your leisure.
21  
22 =head1 SYNOPSIS
23  
24 # We have a directory on disk that is accessible via a web server
25 my $authors = HTML::Location->new( '/var/www/AUTHORS', 'http://ali.as/AUTHORS' );
26
27 # We know where a particular generated file needs to go
28 my $about = $authors->catfile( 'A', 'AD', 'ADAMK', 'about.html' );
29
30 # Save the file to disk
31 my $file = $about->path;
32 open( FILE, ">$file" ) or die "open: $!";
33 print FILE, $content;
34 close FILE;
35
36 # Show the user where to see the file
37 my $uri = $about->uri;
38 print "Author information is at $uri\n";
39  
40 =head1 DESCRIPTION
41  
42 In several process relating to working with the web, we may need to keep
43 track of an area of disk that maps to a particular URL. From this location,
44 we should be able to derived both a filesystem path and URL for any given
45 directory or file under this location that we might need to work with.
46  
47 =head2 Implementation
48  
49 Internally each C<HTML::Location> object contains both a filesystem path,
50 which is altered using L<File::Spec>, and a L<URI> object. When making a
51 change, the path section of the URI is altered using <File::Spec::Unix>.
52  
53 =head2 Method Calling Conventions
54  
55 The main functional methods, such as C<catdir> and C<catfile>, do B<not>
56 modify the original object, instead returning a new object containing the
57 new location.
58  
59 This means that it should be used in a somewhat similar way to L<File::Spec>.
60  
61 # The File::Spec way
62 my $path = '/some/path';
63 $path = File::Spec->catfile( $path, 'some', 'file.txt' );
64
65 # The HTML::Location way
66 my $location = HTML::Location->new( '/some/path', 'http://foo.com/blah' );
67 $location = $location->catfile( 'some', 'file.txt' );
68  
69 OK, well it's not exactly THAT close, but you get the idea. It also allows you
70 to do method chaining, which is basically
71  
72 HTML::Location->new( '/foo', 'http://foo.com/' )->catfile( 'bar.txt' )->uri
73  
74 Which may seem a little trivial now, but I expect it to get more useful later.
75 It also means you can do things like this.
76  
77 my $base = HTML::Location->new( '/my/cache', 'http://foo.com/' );
78 foreach my $path ( @some_files ) {
79 my $file = $base->catfile( $path );
80 print $file->path . ': ' . $file->uri . "\n";
81 }
82  
83 In the above example, you don't have to be continuously cloning the location,
84 because all that stuff happens internally as needed.
85  
86 =head1 METHODS
87  
88 =cut
89  
90 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
91 2 ▶use Clone ();
Subroutine BEGIN: called CC=1 SCAR=0.0
92 2 ▶use URI ();
Subroutine BEGIN: called CC=1 SCAR=0.0
93 2 ▶use File::Spec ();
Subroutine BEGIN: called CC=1 SCAR=0.0
94 2 ▶use File::Spec::Unix ();
Subroutine BEGIN: called CC=1 SCAR=0.0
95  
96 # Overload stringification to the string form of the URL.
97 2 ▶use overload 'bool' => sub () { 1 };
Subroutine BEGIN: called CC=1 SCAR=0.0
98 2 ▶use overload '""' => 'uri';
Subroutine BEGIN: called CC=1 SCAR=0.0
99 2 ▶use overload 'eq' => '__eq';
Subroutine BEGIN: called CC=1 SCAR=0.0
100  
101 2 ▶use vars qw{$VERSION @ISA};
Subroutine BEGIN: called CC=1 SCAR=0.0
102 2 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
103 2 $VERSION = '1.03';
104 2 @ISA = 'Clone';
105 }
106  
107  
108  
109  
110  
111 #####################################################################
112 # Constructors
113  
114 =pod
115  
116 =head2 new $path, $http_url
117  
118 The C<new> constructor takes as argument a filesystem path and a http(s)
119 URL. Both are required, and the method will return C<undef> is either is
120 illegal. The URL is not required to have protocol, host or port sections,
121 and as such allows for host-relative URL to be used.
122  
123 Returns a new C<HTML::Location> object on success, or C<undef> on failure.
124  
125 =cut
126  
127 6 ▶sub new {
Subroutine new: called CC=6 SCAR=20.3
Pod documented
128 6 my $class = shift;
129  
130 # Get the base file system path
131 6 ▶ my $path = File::Spec->canonpath(shift) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $path = "File::Spec"->canonpath(shift()) or return undef 6 0 6
132  
133 # Get the base URI. We only accept HTTP(s) URLs
134 6 ▶ return undef unless defined $_[0] and ! ref $_[0];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0] and not ref $_[0] 0 6 6
Condition33%1/3
defined $_[0] and not ref $_[0]
!ll&&!rl&&r
006
MC/DC0%0/2
defined $_[0] and not ref $_[0]
defined $_[0] not ref $_[0]
Truth table33%1/3
A and not(B)
A= defined $_[0]B= not ref $_[0]
ABresult
0X0
100
111
135 6 ▶ my $URI = URI->new( shift, 'http' ) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $URI = "URI"->new(shift(), "http") or return undef 6 0 6
136 6 ▶ $URI->path( '/' ) unless length $URI->path;
Branch: 1 branch
BranchTrueFalseTotal
unless length $URI->path 6 0 6
137  
138 # Create the object
139 6 bless { path => $path, URI => $URI }, $class;
140 }
141  
142 =pod
143  
144 =head2 param $various
145  
146 C<param> is provided as a mechanism for higher order modules to flexibly
147 accept HTML::Location's as parameters. In this case, it accepts either
148 an existing HTML::Location object, two arguments ($path, $http_url), or
149 a reference to an array containing the same two arguments.
150  
151 Returns a HTML::Location if possible, or C<undef> if one cannot be provided.
152  
153 =cut
154  
155 3 ▶sub param {
Subroutine param: called CC=5 SCAR=16.7
Pod documented
156 3 my $class = shift;
157 3 ▶ return shift if UNIVERSAL::isa(ref $_[0], 'HTML::Location');
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::isa(ref $_[0], "HTML::Location") 1 2 3
158 2 ▶ return HTML::Location->new(@_) if @_ == 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 2 1 1 2
159 1 ▶ if ( ref $_[0] eq 'ARRAY' and @{$_[0]} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq "ARRAY" and @{$_[0];}) 1 0 1
Condition33%1/3
ref $_[0] eq "ARRAY" and @{$_[0];}
!ll&&!rl&&r
001
MC/DC0%0/2
ref $_[0] eq "ARRAY" and @{$_[0];}
ref $_[0] eq "ARRAY" @{$_[0];}
Truth table33%1/3
A and B
A= ref $_[0] eq "ARRAY"B= @{$_[0];}
ABresult
0X0
100
111
160 1 return HTML::Location->new(@{$_[0]});
161 }
162 0 undef;
163 }
164  
165  
166  
167  
168  
169 #####################################################################
170 # Accessors
171  
172 =pod
173  
174 =head2 uri
175  
176 The C<uri> method gets and returns the current URI of the location, in
177 string form.
178  
179 =cut
180  
181 62 ▶sub uri {
Subroutine uri: called CC=1 SCAR=0.0
Pod documented
182 62 $_[0]->{URI}->as_string;
183 }
184  
185 =pod
186  
187 =head2 URI
188  
189 The capitalised C<URI> method gets and returns a copy of the raw L<URI>,
190 held internally by the location. Note that only a copy is returned, and
191 as such as safe to further modify yourself without effecting the location.
192  
193 =cut
194  
195 32 ▶sub URI {
Subroutine URI: called CC=1 SCAR=0.0
Pod documented
196 32 Clone::clone $_[0]->{URI};
197 }
198  
199 =pod
200  
201 =head2 path
202  
203 The C<path> method returns the filesystem path componant of the location.
204  
205 =cut
206  
207 44 ▶sub path { $_[0]->{path} }
Subroutine path: called CC=1 SCAR=0.0
Pod documented
208  
209  
210  
211  
212  
213 #####################################################################
214 # Manipulate Locations
215  
216 =pod
217  
218 =head2 catdir 'dir', 'dir', ...
219  
220 A L<File::Spec> workalike, the C<catdir> method acts in the same way as for
221 L<File::Spec>, modifying both componants of the location. The C<catdir> method
222 returns a B<new> HTML::Location object representing the new location, or
223 C<undef> on error.
224  
225 =cut
226  
227 1 ▶sub catdir {
Subroutine catdir: called CC=3 SCAR=11.1
Pod documented
228 1 my $self = shift;
229 1 my @args = @_;
230  
231 # Alter the URI and local paths
232 1 ▶ my $new_uri = File::Spec::Unix->catdir( $self->{URI}->path, @args ) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $new_uri = "File::Spec::Unix"->catdir($self->{"URI"}->path, @args) or return undef 1 0 1
233 1 ▶ my $new_path = File::Spec->catdir( $self->{path}, @args ) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $new_path = "File::Spec"->catdir($self->{"path"}, @args) or return undef 1 0 1
234  
235 # Clone and set the new values
236 1 my $changed = $self->clone;
237 1 $changed->{URI}->path( $new_uri );
238 1 $changed->{path} = $new_path;
239  
240 1 $changed;
241 }
242  
243 =pod
244  
245 =head2 catfile [ 'dir', ..., ] $file
246  
247 Like C<catdir>, the C<catfile> method acts in the same was as for
248 L<File::Spec>, and returns a new HTML::Location object representing
249 the file, or C<undef> on error.
250  
251 =cut
252  
253 1 ▶sub catfile {
Subroutine catfile: called CC=3 SCAR=11.1
Pod documented
254 1 my $self = shift;
255 1 my @args = @_;
256  
257 # Alter the URI and local paths
258 1 ▶ my $uri = File::Spec::Unix->catfile( $self->{URI}->path, @args ) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $uri = "File::Spec::Unix"->catfile($self->{"URI"}->path, @args) or return undef 1 0 1
259 1 ▶ my $fs = File::Spec->catfile( $self->{path}, @args ) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $fs = "File::Spec"->catfile($self->{"path"}, @args) or return undef 1 0 1
260  
261 # Set both and return
262 1 my $changed = $self->clone;
263 1 $changed->{URI}->path( $uri );
264 1 $changed->{path} = $fs;
265  
266 1 $changed;
267 }
268  
269  
270  
271  
272  
273 #####################################################################
274 # Additional Overload Methods
275  
276 14 ▶sub __eq {
Subroutine __eq: called CC=4 SCAR=13.9
277 14 ▶ my $left = UNIVERSAL::isa(ref $_[0], 'HTML::Location') ? shift : return '';
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa(ref $_[0], 'HTML::Location') ? : 14 0 14
278 14 ▶ my $right = UNIVERSAL::isa(ref $_[0], 'HTML::Location') ? shift : return '';
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa(ref $_[0], 'HTML::Location') ? : 10 4 14
279 10 ▶ ($left->path eq $right->path) and ($left->uri eq $right->uri);
Branch: 1 branch
BranchTrueFalseTotal
$left->path eq $right->path and $left->uri eq $right->uri 7 3 10
280 }
281  
282  
283  
284  
285  
286 #####################################################################
287 # Coercion Support
288  
289 1 ▶sub __as_URI { shift->URI }
Subroutine __as_URI: called CC=1 SCAR=0.0
290  
291 2 1;
292  
293 =pod
294  
295 =head1 TO DO
296  
297 Add more File::Spec-y methods as needed. Ask if you need one.
298  
299 =head1 SUPPORT
300  
301 Bugs should be reported via the CPAN bug tracker at
302  
303 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=HTML-Location>
304  
305 For other issues, or commercial enhancement or support, contact the author.
306  
307 =head1 AUTHOR
308  
309 Adam Kennedy E<lt>adamk@cpan.orgE<gt>
310  
311 =head1 COPYRIGHT
312  
313 Copyright 2003 - 2008 Adam Kennedy.
314  
315 This program is free software; you can redistribute
316 it and/or modify it under the same terms as Perl itself.
317  
318 The full text of the license can be found in the
319 LICENSE file included with this module.
320  
321 =cut
↑ Summary