If you want to avoid regexes, you could use the URI module's path method in combination with the File::Spec::Unix module's splitpath function to get the filename. For example:
#!/usr/bin/perl -w
use strict;
use File::Spec::Unix;
use URI;
#
# url: http://www.made_up_name.com/somepath/script.php/image.jpg?
+p=1&q=2#name
#
# $u->scheme: http
# $u->userinfo: (undef)
# $u->host: www.made_up_name.com
# $u->port: 80
# $u->path: /somepath/script.php/image.jpg
# $u->query: p=1&q=2
# $u->fragment: name
#
my $url = join(
'://', 'http',
join(
'/',
'www.made_up_name.com',
'somepath',
'script.php',
join( '?',
'image.jpg',
join( '#', join( '&', 'p=1', 'q=2' ), 'name' ) )
)
);
my $u = URI->new($url);
#
# $u->path: /somepath/script.php/image.jpg
#
# $volume: ''
# $directories: /somepath/script.php/
# $file: image.jpg
#
( my $volume, my $directories, my $file ) =
File::Spec::Unix->splitpath( $u->path );
printf
<< "URI_PARTS", $url, ( defined( $u->scheme ) ? $u->scheme : '(undef
+)' ), ( defined( $u->userinfo ) ? $u->userinfo : '(undef)' ), ( defin
+ed( $u->host ) ? $u->host : '(undef)' ), ( defined( $u->port ) ? $u->
+port : '(undef)' ), ( defined( $u->path ) ? $u->path : '(undef)' ), (
+ defined( $u->query ) ? $u->query : '(undef)' ), ( defined( $u->fragm
+ent ) ? $u->fragment : '(undef)' );
URI PARTS
URL: %s
SCHEME: %s
USERINFO: %s
HOST: %s
PORT: %s
PATH: %s
QUERY: %s
FRAGMENT: %s
URI_PARTS
printf
<< "PATH_PARTS", ( defined($u->path) ? $u->path : '(undef)' ), ( def
+ined($volume) ? $volume : '(undef)' ), ( defined($directories) ? $dir
+ectories : '(undef)' ), ( defined($file) ? $file : '(undef)' );
PATH FRAGMENTS
PATH: %s
VOLUME: %s
DIRECTORIES: %s
FILE: %s
PATH_PARTS
Another way to look at it, at least. Hope that helps. |