I would skip a regex solution and use substr and rindex:
my $url = qq|/directory/directory/some-file.html|; print substr($url, 0, rindex($url,'/')),"\n";

UPDATE:

I just saw tye mention:

(split('/',$url))[-1]
in the chatterbox. Negative indexing++.

Sheer boredom has settled in me today, so i have decided to run some benchmarks. You did mention 'efficient' after all.

use strict; use Benchmark; use File::Basename; my $url = qq|/directory/directory/some-file.html|; timethese(500000, { 'substr' => sub { return substr($url, 0, rindex($url,'/')) }, 'split' => sub { return (split('/',$url))[-1] }, 'module' => sub { return basename($url) }, }); =for benchmark_results Benchmark: timing 500000 iterations of module, split, substr... module: 33 wallclock secs (33.03 usr + 0.10 sys = 33.13 CPU) split: 5 wallclock secs ( 5.44 usr + -0.01 sys = 5.43 CPU) substr: 1 wallclock secs ( 1.74 usr + 0.00 sys = 1.74 CPU) (linux kernal 2.2.14-5.0, dual 450MHz) =cut
So what does this mean? Just because the substr method is faster is not a reason to pick it over File::Basename.

The main reason is portability. Since we are dealing with URL's, we are guaranteed that forward slashes will be used, but if you are dealing with directory paths, the substr method will not portable between Unix platforms and Windows platforms.

And i bet the reason you can't use modules is not because of a work restriction - this was a homework question wasn't it? Doh! Oh well.

Jeff

R-R-R--R-R-R--R-R-R--R-R-R--R-R-R--
L-L--L-L--L-L--L-L--L-L--L-L--L-L--

In reply to (jeffa) Re: URL Help by jeffa
in thread Removing filename from directory paths (was: URL Help) by vbrtrmn

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.