Search code examples
perlglob

Perl sort corrupts bsd_glob results


This is really simple case:

use feature qw/say/;
use File::Glob qw/bsd_glob/;

# got many wav files
say foreach bsd_glob "*.wav";

# got "*.wav" as only result
say foreach sort bsd_glob "*.wav";
say foreach sort bsd_glob("*.wav");

Why sort keyword has affect on bsd_glob funtion, and make it behaves like not find the files?


Solution

  • You use unintentionally using the

    sort SUBNAME LIST
    

    syntax, telling sort to use bsd_glob as the compare function.

    You could explicitly specify the compare function

    sort { $a cmp $b } bsd_glob "*.wav"
    

    You could refrain from omitting the parens around the operands and arguments.

     sort(bsd_glob(*.wav"))
    

    Omitting parens around the operands and arguments can lead to weird errors.