Saturday, April 6, 2019

Perl: strange behavior of fileparse() getting the suffix


When we supply a suffix list to the function fileparse(), it behaves differently depending on the order of the suffixes in the list.

    use File::Basename;
   
    $fullname = '/tmp/mydir/myfile.txt.bak';
    @suffixlist = ('.txt', '.bak');
   
    ($name,$path,$suffix) = fileparse($fullname,@suffixlist);
   
    print "name: ", $name, "\n";
    print "suffix: ", $suffix, "\n";

   
The output of the above code is:
    name: myfile.txt
    suffix: .bak


If we just simply switch the places of the 2 suffixes in the list to ('.bak', 'txt'), the output is different:
    use File::Basename;
   
    $fullname = '/tmp/mydir/myfile.txt.bak';
    @suffixlist = ('.bak', '.txt');
   
    ($name,$path,$suffix) = fileparse($fullname,@suffixlist);
   
    print "name: ", $name, "\n";
    print "suffix: ", $suffix, "\n";


The output of the new code is:
    name: myfile
    suffix: .txt.bak


Is this a bug?

No comments:

 
Get This <