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

Dear Master Monks,

Would you rewrite:

function fileExtension($file) { $pos = strrpos($file,"."); if ($pos == "0") return false; else { $ext = strtolower(substr($file,$pos)); return $ext; } }
As:
sub fileExtension { my $file = shift; my $pos = index($file,"."); if ($pos == "0") { return undef; } else { my $ext = lc(substr($file,$pos)); return $ext; } }
Or (as per the Cookbook, File::Basename):
sub extension { my $path = shift; my $ext = (fileparse($path,'\.[^.]*'))[2]; $ext =~ s/^\.//; return $ext; }

I think the Cookbook is the best.
Gavin.

Walking the road to enlightenment... I found a penguin and a camel on the way.....
Fancy a yourname@perl.me.uk? Just ask!!!

Replies are listed 'Best First'.
Re: Rewrite a PHP fileExtension function
by Cody Pendant (Prior) on Sep 09, 2005 at 06:56 UTC
    sub fileExtension { my $file = shift; my $pos = index($file,"."); if ($pos == "0") { return undef; } else { my $ext = lc(substr($file,$pos)); return $ext; } } my $filename = 'test.test.txt'; print fileExtension($filename);
    prints ".test.txt". So I don't think I'd recommend that one...


    ($_='kkvvttuu bbooppuuiiffss qqffssmm iibbddllffss')
    =~y~b-v~a-z~s; print

      For what it's worth, this bug is not present in the PHP version. It uses strrpos, which would be rindex in Perl-speak.

      Yeah, wouldn't work with a file.tar.gz

      Walking the road to enlightenment... I found a penguin and a camel on the way.....
      Fancy a yourname@perl.me.uk? Just ask!!!
Re: Rewrite a PHP fileExtension function
by Tanktalus (Canon) on Sep 09, 2005 at 14:17 UTC

    The cookbook/File::Basename is definitely the way to go. The other two (including the PHP one) have bugs. Well, the same bug: you're lower-casing the extention, which means you're going to end up with a different extention than the original file. That'll make it quite painful to put the filename parts back together again to reach the original file.

    "txt" ne "TxT"

    If you need the lowercased version of the extention, for example in trying to guess at the mime type, you should do that outside of a function of "extention". Perhaps in the function named "guess_mimetype" it would make sense.

Re: Rewrite a PHP fileExtension function
by sasikumar (Monk) on Sep 09, 2005 at 06:21 UTC
    Hi

    It depends when to use. If i speak to a monk i use the cook book style. But if i need to teach PERL to a new commer to programming i would use the other way. As usual PERL gives you the flexibility to solve a problem in 100 different ways and we use it depending on the need.
    Thanks
    SasiKumar
Re: Rewrite a PHP fileExtension function
by revdiablo (Prior) on Sep 09, 2005 at 16:30 UTC

    The cookbook version is better, but I'm not sure I'd like to load File::Basename just for this. I'd probably end up writing it as:

    sub extension { my $path = shift; my $ext = (split /\./, $path)[-1]; return $ext; }

    Update: and since we all know this is going to devolve into Golf sooner or later, here's a shortened version of that:

    sub extension { (split/\./,$_[0])[-1]; }