File: Globby.pm

package info (click to toggle)
libmime-tools-perl 5.515-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,612 kB
  • sloc: perl: 6,349; makefile: 8
file content (32 lines) | stat: -rw-r--r-- 674 bytes parent folder | download | duplicates (16)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package Globby;

# More-portable but limited glob().  It does what we need.

use Exporter;
use vars qw(@ISA @EXPORT);

@ISA    = qw(Exporter);
@EXPORT = qw(globby);

sub globby {
    my $globpath = shift;    # full glob
    my %r = ('.'=>'\.',
	     '*'=>'.*',
	     '?'=>'.');

    # Get directory and regexp:
    my ($dir, $glob) = ($globpath =~ m{^(.*?)/?([^/]+\Z)});
    defined($dir) or $dir = '';
    my $re = $glob; $re =~ s{([\.\*\?])}{$r{$1}}g;

    # Get files which match pattern:
    my @f;
    if (opendir DIR, $dir) {
	@f = grep /^$re\Z/, sort(readdir(DIR));
	closedir DIR;
    }

    # Return as full paths:
    map { ($dir ne '') ? "$dir/$_" : $_}  @f;
}
1;