in reply to Dissecting a complete pathname to a file

I'm pretty sure pure regular expressions isn't really the best answer here.

If you wish to do things like expand the ./, have a look at Cwd (e.g., $full = abs_path($file)). It has all that magic built in. For finding the test.txt (assuming / is the only magic character in your paths), you probably just want something like my $short = $1 if $file =~ m/([^\\]+)\z/;.

UPDATE: I wasn't trying to complete a programming assignment, I was just giving general suggestions... My suggestion was that filling out the fullpath is probably not the job of a regular expression. Clearly, File::Spec (below) is a cleaner solution anyway, and that doesn't involve regular expressions at all. I'm also pretty sure Cwd is a Core perl module, so I'm not sure where you're going with that.

UPDATE2: Ahh, I see what you mean, my mistake.

-Paul

Replies are listed 'Best First'.
Re^2: Dissecting a complete pathname to a file
by ikegami (Patriarch) on Feb 28, 2007 at 00:03 UTC

    That returns the file. What about the second requirement to capture the directory?

    use Cwd qw( abs_path ); my $full = abs_path($file_path); my ($dir, $file) = $full =~ /^(.*)\\(.*)/; # Wrong for C:\File my ($dir, $file) = $full =~ /^(.*\\)(.*)/; # Ugly for C:\Dir\File

    A regular expression is not the way to go here. As seen below, there are better (reliable, tested, portable) options in Core Perl.

Re^2: Dissecting a complete pathname to a file
by ikegami (Patriarch) on Feb 28, 2007 at 03:50 UTC

    I'm also pretty sure Cwd is a Core perl module, so I'm not sure where you're going with that.

    I didn't say Cwd was wrong. I said you only answered half the question, and that your method for answering the first half can't be expanded to answer the second. I just added two lines to the top of the snippet for clarity.