output asked headers in the order they were asked; avoid header name spoofing by...
[hband-tools.git] / user-tools / glob
blob71b5e244f2bc469f1d517324d56836e7000c7664
1 #!/usr/bin/env perl
3 =pod
5 =head1 NAME
7 glob - Expand shell-wildcard patterns
9 =head1 SYNOPSIS
11 glob [I<OPTIONS>] [--] I<PATTERN> [I<PATTERN> [I<PATTERN> [...]]]
13 =head1 DESCRIPTION
15 Expand I<PATTERN> as shell-wildcard patterns and output matching filenames.
16 Output all matched file names once and sorted alphabetically.
18 =head1 OPTIONS
20 =over 4
22 =item -0
24 Output filenames as NULL byte temrinated strings.
26 =item -f
28 Fail if can not read a directory.
29 See B<GLOB_ERR> in File::Glob(3perl).
31 =item -i
33 Match case-insensitively.
34 Default is case-sensitive.
36 =item -b
38 Support curly bracket expansion.
39 See B<GLOB_BRACE> in File::Glob(3perl).
41 =back
43 =head1 LIMITATIONS
45 Uses perl(1)'s B<bsd_glob> function from File::Glob(3perl),
47 =head1 SEE ALSO
49 File::Glob(3perl), perldoc(1): glob
51 =cut
54 use Data::Dumper;
55 use Getopt::Long qw/:config no_ignore_case no_bundling no_getopt_compat no_auto_abbrev require_order/;
56 use Pod::Usage;
57 use File::Glob ':bsd_glob';
58 no if ($] >= 5.018), 'warnings' => 'experimental::smartmatch';
60 $OptNullTerminated = 0;
61 $OptGlobFlags = GLOB_QUOTE | GLOB_TILDE;
63 GetOptions(
64 '0' => \$OptNullTerminated,
65 'f' => sub { $OptGlobFlags |= GLOB_ERR; },
66 'i' => sub { $OptGlobFlags |= GLOB_NOCASE; },
67 'b' => sub { $OptGlobFlags |= GLOB_BRACE; },
68 'help' => sub { pod2usage(-exitval=>0, -verbose=>99); },
69 '<>' => sub { unshift @ARGV, @_[0]; die '!FINISH'; },
70 ) or pod2usage(-exitval=>2, -verbose=>99);
73 %Files = ();
75 for my $pattern (@ARGV)
77 my @matches = bsd_glob($pattern, $OptGlobFlags);
78 if(GLOB_ERROR)
80 die "$0: $!: $pattern\n";
82 %Files = (%Files, map {$_=>1} @matches);
85 $\ = "\n";
86 $\ = chr 0 if $OptNullTerminated;
87 print "$_" for sort keys %Files;