I am working on a project which at one point gets a list of files from an ftp server. At that point it either returns an arrayref of files OR if an optional regex reference
See nparen in Regexp::Parser.
use strictures;
use Carp qw(carp);
use Regexp::Parser qw();
my $parser = Regexp::Parser->new;
sub filter_files {
my ($files, $pattern) = @_;
my @files = @$files;
return \@files unless $pattern;
carp sprintf('Could not inspect regex "%s": %s (%d)',
$pattern, $parser->errmsg, $parser->errnum)
unless $parser->regex($pattern);
my %versions;
@files = map {
if (my ($capture) = $_ =~ $pattern) {
$parser->nparen
? push @{ $versions{$capture} }, $_
: $_
} else {
()
}
} @files;
carp 'Could not find any matching files' unless @files;
return (scalar keys %versions)
? \%versions
: \@files;
}
Another possibility to avoid inspecting the pattern is to simply rely on the value of $capture
. It will be 1
(Perl true value) in the case of a successful match without capture. You can distinguish it from the occasional capture returning 1
because that one lack the IV
flag.