I think it was just dragonchild mis-remembering the name of the design pattern - what you're after isn't a facade.

Filters/Pipelines are such a common idiom I have to believe that somebody has documented it as a design pattern somewhere... possibly in a PLoP proceeding. However, I don't have an academic library available to check :-)

I think there's a little confusion because the terms "filtering" and "pipelining" are more often used for applying a series of transformations to something - rather than a series of predicates that exclude objects. From your post I think you're after the latter.

A more concrete example of what you, in particular, are attempting would help. As somebody else already pointed out grep does what you want in simple cases.

If you need to build objects that can apply specific filters, something like this:

#! /usr/bin/perl package Sieve; use strict; use warnings; sub new { bless [], shift }; sub add_predicate { my $self = shift; push @$self, shift; } sub ok { my ($self, $item) = @_; $_->($item) || return(0) foreach @$self; return(1); }; sub sieve { my $self = shift; grep {$self->ok($_)} @_; };

allows you to do:

my $o = Sieve->new; $o->add_predicate( sub { $_[0] > 5 } ); $o->add_predicate( sub { $_[0] % 2 } ); my @original = (1..15); my @sieved = $o->sieve( @original ); print "original: @original\n"; print "odd numbers > 5 are: @sieved\n"; __END__ producing original: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 odd numbers > 5 are: 7 9 11 13 15

If you need more - you'll have to give up a bit more detail ;-)


In reply to Re^4: Filter objects? by adrianh
in thread Filter objects? by matth

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.