develooper Front page | perl.perl6.users | Postings from May 2021

File::Find using a junction with exclude

Thread Previous | Thread Next
From:
Joseph Brenner
Date:
May 20, 2021 23:52
Subject:
File::Find using a junction with exclude
Message ID:
CAFfgvXV_+bob=AXq9m2DzvJA=WHp==4AeJXHnJv7x178o2KLuA@mail.gmail.com
The documentation for File::Find says that the exclude argument
is applied via a smart-match, so I thought that I could use a
junction, like so:

   use File::Find
   my @files = find( dir => $loc, type => 'file', exclude => any(
@exclude_pats ) );

But instead of getting an array of file names (either strings or
IO objects would be fine), that returns an any junction of IO
objects, built-up from a peculiar set of hits.

Does that make sense to anyone?

Here's some code that demos the problem:

use v6;
use File::Find;

## create some files to find
my $loc = "/tmp/monster_island";
mkdir( $loc );
chdir( $loc );
my @monsters = < godzilla mothera rhodan >;
for @monsters -> $name {
    $name.IO.spurt("The $name attacks!");
}

## without exclude, we find all 3 files
my @files_all = find( dir => $loc, type => 'file' );
say @files_all.elems;     # 3

## with a handcrafted regex we find only 2, skipping mothera as expected
my @files_trimmed = find( dir => $loc, type => 'file', exclude =>
rx/<|w>[mothera|camel]$/ );
say @files_trimmed.elems; # 2

## Trying to do the same with an any junction doesn't work:
my @exclude = ( rx/<|w>mothera$/, rx/<|w>camel$/ );
my @files = find( dir => $loc, type => 'file', exclude => any(@exclude) );
say @files;
# [any(("/home/doom/tmp/monster_island/godzilla".IO
"/home/doom/tmp/monster_island/rhodan".IO),
("/home/doom/tmp/monster_island/godzilla".IO
"/home/doom/tmp/monster_island/mothera".IO
"/home/doom/tmp/monster_island/rhodan".IO))]
##
## (1) we end up with a single "any" junction in the first element
## (2) there are five hits, two redundant rhodan and godzillas, plus
one mothera slips through (?)

## but note that this works:
for @monsters {
    .say unless $_ ~~ any(@exclude)
}
# godzilla
# rhodan



raku --version
Welcome to 𝐑𝐚𝐤𝐮𝐝𝐨™ v2020.10.
Implementing the 𝐑𝐚𝐤𝐮™ programming language v6.d.
Built on MoarVM version 2020.10.

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About