Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Answer: How do I extract a file name from a path string

by amasidlover (Sexton)
on Jan 17, 2005 at 14:50 UTC ( #422742=categorized answer: print w/replies, xml ) Need Help??

Q&A > strings > How do I extract a file name from a path string - Answer contributed by amasidlover

This works for both DOS and UNIX path separators and when there is no path info.
( $dir, $file ) = m/(.*)[\\\/](.+)/ ? ( $1, $2 ) : ( undef, $_ );

Replies are listed 'Best First'.
Re: Answer: How do I extract a file name from a path string
by talexb (Canon) on Jan 17, 2005 at 22:47 UTC

    I'm not sure why I would want to use that code fragment and not File::Spec::Functions.

    Alex / talexb / Toronto

    "Groklaw is the open-source mentality applied to legal research" ~ Linus Torvalds

      I wholeheartedly agree:

      use File::Spec::Functions; my $fname = '/home/legato/'; my ($vol, $path, $filename) = splitpath($fname); # $filename now contains ''
      A pattern match will not be as platform-aware (or as readable, incidentally) or as thorough as the above code, which is not significantly longer.

      Anima Legato
      .oO all things connect through the motion of the mind

Log In?

What's my password?
Create A New User
Corion looks at TheDamians new modules. It seems he's written another Perl parser in a regexp and uses it for Keyword::Declare ...
[Discipulus]: from txt to array, from array to polemic, from polemic to flame?
[Corion]: Discipulus++ # profound insight
[Corion]: :-D

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (9)
As of 2017-06-23 07:56 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (536 votes). Check out past polls.