1package URI::Split; 2 3use strict; 4use warnings; 5 6our $VERSION = '5.10'; 7 8use Exporter 5.57 'import'; 9our @EXPORT_OK = qw(uri_split uri_join); 10 11use URI::Escape (); 12 13sub uri_split { 14 return $_[0] =~ m,(?:([^:/?#]+):)?(?://([^/?#]*))?([^?#]*)(?:\?([^#]*))?(?:#(.*))?,; 15} 16 17sub uri_join { 18 my($scheme, $auth, $path, $query, $frag) = @_; 19 my $uri = defined($scheme) ? "$scheme:" : ""; 20 $path = "" unless defined $path; 21 if (defined $auth) { 22 $auth =~ s,([/?\#]), URI::Escape::escape_char($1),eg; 23 $uri .= "//$auth"; 24 $path = "/$path" if length($path) && $path !~ m,^/,; 25 } 26 elsif ($path =~ m,^//,) { 27 $uri .= "//"; # XXX force empty auth 28 } 29 unless (length $uri) { 30 $path =~ s,(:), URI::Escape::escape_char($1),e while $path =~ m,^[^:/?\#]+:,; 31 } 32 $path =~ s,([?\#]), URI::Escape::escape_char($1),eg; 33 $uri .= $path; 34 if (defined $query) { 35 $query =~ s,(\#), URI::Escape::escape_char($1),eg; 36 $uri .= "?$query"; 37 } 38 $uri .= "#$frag" if defined $frag; 39 $uri; 40} 41 421; 43 44__END__ 45 46=head1 NAME 47 48URI::Split - Parse and compose URI strings 49 50=head1 SYNOPSIS 51 52 use URI::Split qw(uri_split uri_join); 53 ($scheme, $auth, $path, $query, $frag) = uri_split($uri); 54 $uri = uri_join($scheme, $auth, $path, $query, $frag); 55 56=head1 DESCRIPTION 57 58Provides functions to parse and compose URI 59strings. The following functions are provided: 60 61=over 62 63=item ($scheme, $auth, $path, $query, $frag) = uri_split($uri) 64 65Breaks up a URI string into its component 66parts. An C<undef> value is returned for those parts that are not 67present. The $path part is always present (but can be the empty 68string) and is thus never returned as C<undef>. 69 70No sensible value is returned if this function is called in a scalar 71context. 72 73=item $uri = uri_join($scheme, $auth, $path, $query, $frag) 74 75Puts together a URI string from its parts. 76Missing parts are signaled by passing C<undef> for the corresponding 77argument. 78 79Minimal escaping is applied to parts that contain reserved chars 80that would confuse a parser. For instance, any occurrence of '?' or '#' 81in $path is always escaped, as it would otherwise be parsed back 82as a query or fragment. 83 84=back 85 86=head1 SEE ALSO 87 88L<URI>, L<URI::Escape> 89 90=head1 COPYRIGHT 91 92Copyright 2003, Gisle Aas 93 94This library is free software; you can redistribute it and/or 95modify it under the same terms as Perl itself. 96 97=cut 98