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

Hello,

I have been trying to insert some file information (filename and type) into a DB and thought the fileparse function was ideal.

I'm using simple filenames, 59 alphanumeric and _ chars, the dot and a three or two char alphanumeric extension.

my $row = ./filename.ext; ($req_file, $dir, $ext) = fileparse($row, '(?<=\.)[^.]*');

As described in the manual the regexp splits off the suffix. I don't want the dot in the suffix but because of how fileparse works, when I use this regexp, I get the dot at the end of the filename. I don't want it there either! :)

I have spent my entire morning reading the regexp info on perldoc.org and haven't seen anything to help. Is there a regular expression that I can use to get both $req_file and $ext without the dot?

For now I am just running $req_file through substr as it is a known length.

Replies are listed 'Best First'.
Re: How can I get rid of the full stop using fileparse and regex?
by choroba (Cardinal) on Mar 24, 2015 at 15:06 UTC
    The dot must go somewhere (the file name or the suffix), so you can roundtrip the original path back with
    join $req_file, $dir, $ext

    As the documentation says:

    You are guaranteed that $dirs . $filename . $suffix will denote the same location as the original $path.
    If you want to remove the dot, you don't need to know the length of the string:
    #! /usr/bin/perl use warnings; use strict; use File::Basename; for my $row('./filename.ext', '../filename', 'filename.s1.s2') { my ($filename, $dirs, $suffix) = fileparse($row, qr/\..*/); substr $suffix, 0, 1, q(); # Remove the dot. print "$_\t" for $filename, $dirs, $suffix; print "\n"; }

    Update: Fixed to handle various suffixes. Change the regex to qr/\.[^.]*/ if you only want s2 as the suffix of filename.s1.s2.

    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

      I actually did exactly that with my substr right after writing my post. Apparently I am having a day where I want to do everything the difficult way! I need someone to shout KISS at me.

      Thanks for responding to what was a bit of a daft question. Now at least I understand why the fileparse function works the way it does.

Re: How can I get rid of the full stop using fileparse and regex?
by graff (Chancellor) on Mar 25, 2015 at 01:33 UTC
    If you're certain that the file names are always as simple as you described (one dot per name), then you can use fileparse without an extension arg; this leaves the extension attached in the return value, which you can then split on period:
    use File::Basename; for ( qw{foo/bar.ext ../moo/mar.ble} ) { my ( $name, $ext ) = split( /\./, fileparse( $_ )); print "$_ => $name and $ext\n"; }