Web::S...Filter(3pm)

πŸ“¦
version 0.38 Β· 6 versions
, in debian bookworm, debian trixie, ubuntu noble, ubuntu resolute, ubuntu stonking

✨ Web::Scraper::Filter β€” the base class glow-up ✨

okay bestie, so this is the manual page for Web::Scraper::Filter (which shows up in your terminal as Web::S...Filter(3pm) because, like, the header truncates it β€” tragic, I know πŸ’€). It's a User Contributed Perl Documentation page and it is lowkey the blueprint for making your own text filters in Web::Scraper. Let's get into it 🫢


🌸 NAME 🌸

Web::Scraper::Filter β€” the base class for Web::Scraper filters. the origin story, the blueprint, the main character πŸ‘‘


πŸŽ€ SYNOPSIS πŸŽ€

here's the vibe, verbatim, no notes:

package Web::Scraper::Filter::YAML;
use base qw( Web::Scraper::Filter );
use YAML ();

sub filter {
    my($self, $value) = @_;
    YAML::Load($value);
}

1;

use Web::Scraper;

my $scraper = scraper {
    process ".yaml-code", data => [ 'TEXT', 'YAML' ];
};

like… a whole custom filter in, what, ten lines? she ate and left no crumbs πŸ’…


πŸ¦‹ DESCRIPTION πŸ¦‹

Web::Scraper::Filter is a base class for text filters in Web::Scraper. You wanna build your own text filter? You subclass this module. That's it. That's the whole assignment. ✨

There are two ways to create and use your custom filter:

  1. If you name your filter Web::Scraper::Filter::Something, you just call:
process $exp, $key => [ 'TEXT', 'Something' ];
  1. If you declare your filter under your own namespace, like 'MyApp::Filter::Foo', then you say:
process $exp, $key => [ 'TEXT', '+MyApp::Filter::Foo' ];

(see that little +? that's the "not a core filter, bestie, look in my namespace" signal 🫢)


πŸ’œ inline filters, because who has time for a whole class πŸ’œ

You can also inline your filter function or regexp without creating a filter class at all:

process $exp, $key => [ 'TEXT', sub { s/foo/bar/ } ];

process $exp, $key => [ 'TEXT', qr/Price: (\d+)/ ];
process $exp, $key => [ 'TEXT', qr/(?<name>\w+): (?<value>\w+)/ ];

yes, named captures included. we love a glow-up. 😭

⚠️ okay important, listen up: this function munges $_ and returns the count of replacement. Filter code special cases if the return value of the callback is number and $_ value is updated. that's the rule, that's the lore, don't fight it πŸ’€

🌈 stacking, because more is more 🌈

You can, of course, stack filters like:

process $exp, $key => [ '@href', 'Foo', '+MyApp::Filter::Bar', \&baz ];

filter, filter, filter, coderef β€” a whole little relay race and honestly? iconic. πŸƒβ€β™€οΈπŸ’¨


πŸ‘‘ AUTHOR πŸ‘‘

Tatsuhiko Miyagawa β€” the legend, the moment, the whole reason we're here 🫢


and that's the page, bestie!! this gem of a doc came to you via perl v5.34.0, dated 2022-06-28 β€” so if you're reading this years later, she's vintage and still slaying. bye pookie, go write a filter πŸ’–βœ¨πŸŽ€

πŸ“¦ where this page comes from