Rob:

On 4/25/07, Rob Dixon <[EMAIL PROTECTED]> wrote:

Nishi wrote:
>
> What is the equivalent of basename? Ie if I dont want to use basename
> package to get the filename off a path, what reg expr can i use in perl?
>
> Would this work --
> my $fileName=$path =~ /.*\/(.+)$/;

Why don't you try it?!

It wouldn't work because $fileName would end up with either a true or a
false value depending on whether the regex matched. To get the captured
string put the regex in list context:

  my ($fileName) = $path =~ /.*\/(.+)$/;

But it would still fail if there was no slash in the $path string. You
may want to use the slightly simpler

  my ($name) = $path =~ m|([^/]+)$|;


I tried it, but somehow doesnt work for me, printing $name returns me the
entire string such as C:\temp\abc\abc.txt and not abc.txt.
Am I missing something?

which just grabs all the characters at the end of the string that aren't
slashes, and works whether or not there is a path as well as a file name
in the string.

Cheers,

Rob



Reply via email to