XML::Grove::Sub(3pm)

πŸ“¦
version 0.46alpha Β· 4 versions
, in debian bookworm, debian trixie, ubuntu noble, ubuntu resolute, ubuntu stonking

πŸ’– XML::Grove::Sub πŸ’–

okay bestie, so this is XML::Grove::Sub β€” a lil Perl module (from the User Contributed Perl Documentation collection, no less) that runs a filter sub over a grove. yes. that's the whole personality. and it's iconic for it πŸ’…


✨ NAME ✨

XML::Grove::Sub β€” run a filter sub over a grove πŸ¦‹


🌸 SYNOPSIS 🌸

use XML::Grove::Sub;

# Using filter method on XML::Grove::Document or XML::Grove::Element:
@results = $grove_object->filter(\&sub [, ...]);

# Using an XML::Grove::Sub instance:
$filterer = XML::Grove::Sub->new();
@results = $grove_object->accept($filterer, \&sub [, ...]);

so you can either hit up the filter method straight on your XML::Grove::Document or XML::Grove::Element, or you can make your own XML::Grove::Sub instance and roll with accept. both are valid, both are cute, pick your fighter 🫢


πŸ’œ DESCRIPTION πŸ’œ

XML::Grove::Sub executes a sub β€” the filter β€” over every single object in a grove, and then hands you back a list of all the return values from that sub. no cap, all of them, collected together.

The way it works is: the sub gets called with the grove object as its first parameter, and then the rest of the arguments from your call to filter() or accept() get passed along too. so whatever extra args you were carrying, they get forwarded straight through to the filter. periodt ✨


πŸŽ€ EXAMPLE πŸŽ€

okay so here's a filter that returns a list of all foo or bar elements that have an attribute widget-no beginning with A or B. she's specific, she's picky, she knows what she wants:

@results = $grove_obj->filter(sub {
    my $obj = shift;

    if ($obj->isa('XML::Grove::Element')
        && (($obj->{Name} eq 'foo')
            || ($obj->{Name} eq 'bar'))
        && ($obj->{Attributes}{'widget-no'} =~ /^[AB]/)) {
        return ($obj);
    }
    return ();
});

see how it checks isa('XML::Grove::Element') first? and then the {Name} field? and then digs into {Attributes}{'widget-no'} with a regex? chef's kiss, that's the vibe check passing πŸ”₯ and if nothing matches, it just returns () β€” an empty list, no drama, no notes.


πŸ‘‘ AUTHOR πŸ‘‘

Ken MacLeod, ken@bitsko.slc.ut.us

like, actual legend behavior. we love him. πŸ’–


🌈 SEE ALSO 🌈

go read those too bestie, they're all connected, it's a whole universe, iykyk πŸ’«


and that's the whole page!! this lil doc came to you as part of perl v5.36.0, dated 2022-11-29, served up fresh from the User Contributed Perl Documentation. thank you for coming to my ted talk, mwah πŸ’‹πŸ’–βœ¨

πŸ“¦ where this page comes from