2

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?

jiandingzhe
  • 1,881
  • 15
  • 35

2 Answers2

3

When you say

sort bsd_glob $pattern;

then the bsd_glob is used as a sorting (comparison) function, with the $pattern tnen being the list to sort (parens used around the pattern notwithstanding).

The quickest workaround is

sort +bsd_glob $pattern;

where the unary + makes the following (bsd_glob) be treated as an expression, in which case it cannot be a sorting function. Then the default { $a cmp $b } sorting is used, as intended.

Or, use another set of parens around the list to be sorted (bsd_glob(...)). See this post for details, and the links in it.

zdim
  • 64,580
  • 5
  • 52
  • 81
2

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.

ysth
  • 96,171
  • 6
  • 121
  • 214
ikegami
  • 367,544
  • 15
  • 269
  • 518