You can do this without reverse or chop, with a one-liner:
my $URL = "http://www.mysite.com/images/uploads/1050084447_4.jpg";
# retrieves the image name from the URL
my $image_name = (split /\//, $URL)[-1];
print "$image_name\n";
Which prints:
1050084447_4.jpg
| [reply] [d/l] [select] |
my ($file) = $url =~ m!([^/]*)$!;
-sauoq
"My two cents aren't worth a dime.";
| [reply] [d/l] |
works like a charm!
thanks guys!
| [reply] |
If you need to do more with URL's than just picking out the filename, take a look at The URI module, or more specifically: URI::URL.
Arjen | [reply] |
I'm going to strongly second aragorn's statement, and go further. You NEED to use the URI modules, in particular, URI::Split for the uri_split() function.
Here's the problem: What if your database is screwed up and gives you back "http://www.mysite.com" as the result? Well, with the aforementioned solutions, your imagename will be "www.mysite.com", when it should be undef. Better is something along the lines of:
use URI::Split qw(uri_split);
use File::Spec;
my $url= get_url_from_database();
my ($scheme, $auth, $path, $query, $frag) = uri_split($url);
die "No path in URI ($url) from database!" unless $path;
my ($volume, $directory, $filename) = File::Spec->split_path($path);
die "No filename in path ($path) from URI!" unless $filename;
------
We are the carpenters and bricklayers of the Information Age.
The idea is a little like C++ templates, except not quite so brain-meltingly complicated. -- TheDamian, Exegesis 6
... strings and arrays will suffice. As they are easily available as native data types in any sane language, ... - blokhead, speaking on evolutionary algorithms
Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.
| [reply] [d/l] |
$url =~ s|.*/||;
Real simple.
------------
:Wq
Not an editor command: Wq
| [reply] [d/l] |