| 1 |
NAME |
| 2 |
URI::ToDisk - An object for mapping a URI to an on-disk storage |
| 3 |
directory |
| 4 |
|
| 5 |
SYNOPSIS |
| 6 |
# We have a directory on disk that is accessible via a web server |
| 7 |
my $authors = URI::ToDisk->new( '/var/www/AUTHORS', 'http://ali.as/AUTHORS' ); |
| 8 |
|
| 9 |
# We know where a particular generated file needs to go |
| 10 |
my $about = $authors->catfile( 'A', 'AD', 'ADAMK', 'about.html' ); |
| 11 |
|
| 12 |
# Save the file to disk |
| 13 |
my $file = $about->path; |
| 14 |
open( FILE, ">$file" ) or die "open: $!"; |
| 15 |
print FILE, $content; |
| 16 |
close FILE; |
| 17 |
|
| 18 |
# Show the user where to see the file |
| 19 |
my $uri = $about->uri; |
| 20 |
print "Author information is at $uri\n"; |
| 21 |
|
| 22 |
DESCRIPTION |
| 23 |
In several process relating to working with the web, we may need to keep |
| 24 |
track of an area of disk that maps to a particular URL. From this |
| 25 |
location, we should be able to derived both a filesystem path and URL |
| 26 |
for any given directory or file under this location that we might need |
| 27 |
to work with. |
| 28 |
|
| 29 |
Implementation |
| 30 |
Internally each "URI::ToDisk" object contains both a filesystem path, |
| 31 |
which is altered using File::Spec, and a URI object. When making a |
| 32 |
change, the path section of the URI is altered using <File::Spec::Unix>. |
| 33 |
|
| 34 |
Method Calling Conventions |
| 35 |
The main functional methods, such as "catdir" and "catfile", do not |
| 36 |
modify the original object, instead returning a new object containing |
| 37 |
the new location. |
| 38 |
|
| 39 |
This means that it should be used in a somewhat similar way to |
| 40 |
File::Spec. |
| 41 |
|
| 42 |
# The File::Spec way |
| 43 |
my $path = '/some/path'; |
| 44 |
$path = File::Spec->catfile( $path, 'some', 'file.txt' ); |
| 45 |
|
| 46 |
# The URI::ToDisk way |
| 47 |
my $location = URI::ToDisk->new( '/some/path', 'http://foo.com/blah' ); |
| 48 |
$location = $location->catfile( 'some', 'file.txt' ); |
| 49 |
|
| 50 |
OK, well it's not exactly THAT close, but you get the idea. It also |
| 51 |
allows you to do method chaining, which is basically |
| 52 |
|
| 53 |
URI::ToDisk->new( '/foo', 'http://foo.com/' )->catfile( 'bar.txt' )->uri |
| 54 |
|
| 55 |
Which may seem a little trivial now, but I expect it to get more useful |
| 56 |
later. It also means you can do things like this. |
| 57 |
|
| 58 |
my $base = URI::ToDisk->new( '/my/cache', 'http://foo.com/' ); |
| 59 |
foreach my $path ( @some_files ) { |
| 60 |
my $file = $base->catfile( $path ); |
| 61 |
print $file->path . ': ' . $file->uri . "\n"; |
| 62 |
} |
| 63 |
|
| 64 |
In the above example, you don't have to be continuously cloning the |
| 65 |
location, because all that stuff happens internally as needed. |
| 66 |
|
| 67 |
METHODS |
| 68 |
new $path, $http_url |
| 69 |
The "new" constructor takes as argument a filesystem path and a http(s) |
| 70 |
URL. Both are required, and the method will return "undef" is either is |
| 71 |
illegal. The URL is not required to have protocol, host or port |
| 72 |
sections, and as such allows for host-relative URL to be used. |
| 73 |
|
| 74 |
Returns a new "URI::ToDisk" object on success, or "undef" on failure. |
| 75 |
|
| 76 |
param $various |
| 77 |
"param" is provided as a mechanism for higher order modules to flexibly |
| 78 |
accept URI::ToDisk's as parameters. In this case, it accepts either an |
| 79 |
existing URI::ToDisk object, two arguments ($path, $http_url), or a |
| 80 |
reference to an array containing the same two arguments. |
| 81 |
|
| 82 |
Returns a URI::ToDisk if possible, or "undef" if one cannot be provided. |
| 83 |
|
| 84 |
uri |
| 85 |
The "uri" method gets and returns the current URI of the location, in |
| 86 |
string form. |
| 87 |
|
| 88 |
URI |
| 89 |
The capitalised "URI" method gets and returns a copy of the raw URI, |
| 90 |
held internally by the location. Note that only a copy is returned, and |
| 91 |
as such as safe to further modify yourself without effecting the |
| 92 |
location. |
| 93 |
|
| 94 |
path |
| 95 |
The "path" method returns the filesystem path componant of the location. |
| 96 |
|
| 97 |
catdir 'dir', 'dir', ... |
| 98 |
A File::Spec workalike, the "catdir" method acts in the same way as for |
| 99 |
File::Spec, modifying both componants of the location. The "catdir" |
| 100 |
method returns a new URI::ToDisk object representing the new location, |
| 101 |
or "undef" on error. |
| 102 |
|
| 103 |
catfile [ 'dir', ..., ] $file |
| 104 |
Like "catdir", the "catfile" method acts in the same was as for |
| 105 |
File::Spec, and returns a new URI::ToDisk object representing the file, |
| 106 |
or "undef" on error. |
| 107 |
|
| 108 |
TO DO |
| 109 |
Add more File::Spec-y methods as needed. Ask if you need one. |
| 110 |
|
| 111 |
SUPPORT |
| 112 |
Bugs should be reported via the CPAN bug tracker at |
| 113 |
|
| 114 |
<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=URI-ToDisk> |
| 115 |
|
| 116 |
For other issues, or commercial enhancement or support, contact the |
| 117 |
author. |
| 118 |
|
| 119 |
AUTHORS |
| 120 |
Adam Kennedy <http://ali.as/>, [email protected] |
| 121 |
|
| 122 |
COPYRIGHT |
| 123 |
Copyright (c) 2003 - 2006 Adam Kennedy. |
| 124 |
|
| 125 |
This program is free software; you can redistribute it and/or modify it |
| 126 |
under the same terms as Perl itself. |
| 127 |
|
| 128 |
The full text of the license can be found in the LICENSE file included |
| 129 |
with this module. |
| 130 |
|