Elegant has asked for the wisdom of the Perl Monks concerning the following question:

I'm currently using the following code snippet:
my($parent, $dir, $suffix) = fileparse($item, qr/\.[mM][kK][vV]/);
Unfortunately, this fails every single time $item contains a dash (-), IE: "L:\My-Folder". It promptly ends giving a parser error saying the document is empty.

Replies are listed 'Best First'.
Re: fileparse() Issue (File::Basename)
by Athanasius (Archbishop) on Sep 18, 2014 at 06:26 UTC

    Works fine for me:

    #! perl use strict; use warnings; use File::Basename; my $item = 'L:\My-Folder.mkv'; my($filename, $dirs, $suffix) = fileparse($item, qr/\.[mM][kK][vV]/); print "filename: $filename\n"; print "directories: $dirs\n"; print "suffix: $suffix\n";

    Output:

    16:23 >perl 1018_SoPW.pl filename: My-Folder directories: L:\ suffix: .mkv 16:23 >

    I think you’ll need to provide a minimal but complete, working script together with your test data and the output you are seeing.

    From the backslash in the path I assume you’re on Windows?

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Re: FileParse Issue
by Corion (Patriarch) on Sep 18, 2014 at 06:48 UTC

    You have basically the same problem as in Parameters and Dashes. Maybe the problem is not in your code but in your data?

    Have you made really, really certain that the name is My-Folder and not My—Folder or My&ldash;Folder? If the dash is not a minus sign but a &ldash; or &mdash; weird things might happen.

    So far, you haven't shown a self-contained program that exhibits the problem you describe. Please help us help you better by showing a short, self-contained program that takes no input data and produces the results you describe.

Re: FileParse Issue
by jellisii2 (Hermit) on Sep 18, 2014 at 13:42 UTC
    File::Spec handles a LOOOOOOT of this stuff for you...
    use strict; use warnings; use File::Spec; my $item = 'L:\My-Folder\foo.txt'; my($vol, $path, $file) = File::Spec->splitpath($item); print "Vol = $vol, path = $path, file = $file";
    Teamed with File::Find, it becomes exceptionally useful.