Perl programming related question.

Dave Cross davorg at gmail.com
Fri Aug 17 13:39:40 UTC 2012


On 16 August 2012 16:28, Lázaro Morales <lazaro at frioclima.com.cu> wrote:
> Hello folks,
>
> This question is a bit off-topic but the Fedora Community is awesome. This
> is the question, How can I from this string:
>
>    my $url = "http://somesite.org/somefile.zip";    # Example URL
>
> obtain only this sub-string:
>
>   my $file = "somefile.zip";

Why bother wrestiling with complex regular expressions when someone
else has already done the work? Perl has a URI module ("yum install
perl-URI") which understands how URIs (and URLs) work. I'd recommend
using that.

  use URI;

  my $url = URI->new('http://somesite.org/somefile.zip');
  say $url->path; # prints "/somefile.zip"

Then there's the File::Basename module (included with the Perl
distribution) for dealing with file paths.

  use File::Basename;

  say basename($url->path); # prints somefile.zip

Perl programming gets a lot easier if you use the tools that are already there.

Cheers.

Dave...

-- 
Dave Cross :: dave at dave.org.uk
http://dave.org.uk/
@davorg


More information about the users mailing list