Gossamer Forum
Home : General : Perl Programming :

parse_url

Quote Reply
parse_url
 
Hi,

I'm looking for a perl snippet that can do what the host part of parse_url (PHP) does! Who can help me out?

Code:

parse_url -- Parse a URL and return its components
http://www.php.net/manual/en/function.parse-url.php

Example:

$referer = parse_url(http://www.domain.com/dir/subdir/page.html);
$referer = $referer[host];

$referer will print: www.domain.com

Currently I'm using the following snippet... But it isn't parse url ;-)

Code:

my $referer = "http://www.domain.com/dir/subdir/page.html"
$referer =~ s|^http://([^/]+).*|$1|i;

Greeting,

cK
Quote Reply
Re: [cK] parse_url In reply to
Try this on for size :) ....I'm not saying it's perfect as it's after midnight and I just whipped this up, but give it a try.

Code:
sub parse_url {
#---------------------------------------
# Pass in a URL. Hashref is returned.

my $url = shift;
my (@parts) =~ $url =~ m|^([^:]+)://([^/]+)(?:(/.+?/?)([^/]+)?)?$|;
my (%slice) = ();
@slice{qw/protocol domain path file/} = @parts;
return \%slice;
}

You'd call it like:

Code:
my $parts = parse_url("http://www.aol.com/foo/bar/index.html");

Then you can do stuff like...

Code:
my $domain = $parts->{domain};
Quote Reply
Re: [Paul] parse_url In reply to
Cool !!! Angelic

Only I'm getting no output at all.... Am I doing something stupid? Tested it like:

Code:
#!/usr/bin/perl

use CGI::Carp qw(fatalsToBrowser);

sub parse_url {
#---------------------------------------
# Pass in a URL. Hashref is returned.

my $url = shift;
my (@parts) =~ $url =~ m|^([^:]+)://([^/]+)(?:(/.+?/?)([^/]+)?)?$|;
my (%slice) = ();
@slice{qw/protocol domain path file/} = @parts;
return \%slice;
}

my $url = parse_url("http://www.aol.com/foo/bar/index.html");

print "Content-type:text/html\n\n";
print "$url->{protocol}<br>";
print "$url->{domain}<br>";
print "$url->{path}<br>";
print "$url->{file}<br>";

To be honest: only the protocol and domain part is *really* important, the rest is more handy and cool.
Quote Reply
Re: [cK] parse_url In reply to
Whoops there's a typo.

Change:

my (@parts) =~

to...

my (@parts) =