Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

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

by amasidlover (Sexton)
on Jan 17, 2005 at 14:50 UTC ( [id://422742]=note: print w/replies, xml ) Need Help??


in reply to How do I extract a file name from a path string

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 (Chancellor) 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/filename.pl'; my ($vol, $path, $filename) = splitpath($fname); # $filename now contains 'filename.pl'
      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?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://422742]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2024-04-25 15:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found