| I'm not sure if I've nailed what the GP was speaking of or what you're looking for but anyway I do think the following is illustrative of the linguistic evolution P6 represents. Earlier this year at perlmonks (perhaps the top resource along with stackoverflow for experienced perl folk answering questions) a poster wanted solutions to a fairly basic operation:[1] > I need a function which will filter a nested hash, removing any fields I'm not interested in. Their test data had a source that began: my $source = {
f1 => 'garbage',
f2 => 'more garbage',
f3 => 'important data',
f4 => {
this => 'sub hash',
is => 'garbage'
},
f5 => {
f6 => 'more important data',
and a filter that began: my $filter = {
f3 => 1,
f5 => {
f6 => 1,
After a few days the person who asked the question summarized the community's P5 answers.[2]The first one listed (slightly trimmed): #! /usr/bin/env perl
use strict;
use warnings;
use Carp;
use Deep::Hash::Utils qw(reach nest deepvalue);
sub hash_filter_recursive {
my $source = shift;
my $filter = shift;
my %output;
foreach ( keys %$filter ) {
if ( exists $source->{$_} ) {
if ( ref $filter->{$_} eq 'HASH' ) {
croak "bad filter: on '$_', expected HASH\n"
unless ( ref $source->{$_} eq 'HASH' );
$output{$_} = hash_filter_recursive( $source->{$_}, $filter->{$_} );
}
else {
$output{$_} = $source->{$_};
}
}
}
return \%output;
}
(This appears to include a modicum of input validation.)Here was my P6 answer, including the call to invoke the routine (which I omitted from the P5):[3] sub infix:<landr> ($l, $r) { $l and $r };
say $filter «landr» $source
Even though this uses a parallel operation which will one day optionally be automatically mapped to multiple cores to run faster, I'm pretty sure this is currently slower than the P5 solutions.And I didn't bother with any validation that it was indeed being passed a nested hash. But it makes a point. While P6 can typically do similar things to P5, it often turns out easier to do many things at a much higher level, which leads to less spaghetti. ---- [1] https://www.perlmonks.org/?node_id=1215517 [2] https://www.perlmonks.org/?node_id=1215736 [3] https://www.perlmonks.org/?node_id=1216274 |
I think the reason the following doesn't work that way is that `and` is too thunky.
(Note that `and` is supposed to be thunky, so that isn't a bug.)---
I personally would write this:
Then the conditions can be code objects, regexes (which are a form of code object in Perl6), or literals. (I tried to get it to work with bare Junctions, but I was unable to get the `«»` part to pass it into the operator.)It does mean that you have to use `True` rather than `1`.