URI::Split - Parse and compose URI strings


URI documentation Contained in the URI distribution.

Index


Code Index:

NAME

Top

URI::Split - Parse and compose URI strings

SYNOPSIS

Top

 use URI::Split qw(uri_split uri_join);
 ($scheme, $auth, $path, $query, $frag) = uri_split($uri);
 $uri = uri_join($scheme, $auth, $path, $query, $frag);

DESCRIPTION

Top

Provides functions to parse and compose URI strings. The following functions are provided:

($scheme, $auth, $path, $query, $frag) = uri_split($uri)

Breaks up a URI string into its component parts. An undef value is returned for those parts that are not present. The $path part is always present (but can be the empty string) and is thus never returned as undef.

No sensible value is returned if this function is called in a scalar context.

$uri = uri_join($scheme, $auth, $path, $query, $frag)

Puts together a URI string from its parts. Missing parts are signaled by passing undef for the corresponding argument.

Minimal escaping is applied to parts that contain reserved chars that would confuse a parser. For instance, any occurrence of '?' or '#' in $path is always escaped, as it would otherwise be parsed back as a query or fragment.

SEE ALSO

Top

URI, URI::Escape

COPYRIGHT

Top


URI documentation Contained in the URI distribution.

package URI::Split;

use strict;

use vars qw(@ISA @EXPORT_OK);
require Exporter;
@ISA = qw(Exporter);
@EXPORT_OK = qw(uri_split uri_join);

use URI::Escape ();

sub uri_split {
     return $_[0] =~ m,(?:([^:/?#]+):)?(?://([^/?#]*))?([^?#]*)(?:\?([^#]*))?(?:#(.*))?,;
}

sub uri_join {
    my($scheme, $auth, $path, $query, $frag) = @_;
    my $uri = defined($scheme) ? "$scheme:" : "";
    $path = "" unless defined $path;
    if (defined $auth) {
	$auth =~ s,([/?\#]), URI::Escape::escape_char($1),eg;
	$uri .= "//$auth";
	$path = "/$path" if length($path) && $path !~ m,^/,;
    }
    elsif ($path =~ m,^//,) {
	$uri .= "//";  # XXX force empty auth
    }
    unless (length $uri) {
	$path =~ s,(:), URI::Escape::escape_char($1),e while $path =~ m,^[^:/?\#]+:,;
    }
    $path =~ s,([?\#]), URI::Escape::escape_char($1),eg;
    $uri .= $path;
    if (defined $query) {
	$query =~ s,(\#), URI::Escape::escape_char($1),eg;
	$uri .= "?$query";
    }
    $uri .= "#$frag" if defined $frag;
    $uri;
}

1;

__END__