NAME

Query::Tags - Raku-inspired query language for attributes

SYNOPSIS

use Query::Tags;

# Select all @books for which the 'title' field matches the regex /Perl/.
my $q = Query::Tags->new(q[:title/Perl/]);
my @shelf = $q->select(@books);

VERSION

This document describes v0.0.3 of Query::Tags.

DESCRIPTION

Query::Tags implements a simple query language for stringy object attributes. Its main features are:

This feature set allows for reasonably flexible filtering of tagged, unstructured data (think of email headers). They also allow for a straightforward query syntax and quick parsing (discussed in detail below).

It does not support:

Methods

new

my $q = Query::Tags->new($query_string, \%opts);

Parses the query string and creates a new query object. The query is internally represented by a syntax tree.

The optional argument \%opts is a hashref containing options. Only one option is supported at the moment:

tree

my $root = $q->tree;

Get the root of the underlying syntax tree representation. It is an object of class Query::Tags::To::AST::Query.

test

$q->test($obj) ? 'PASS' : 'FAIL'

Check if the given object passes all query assertions in $q.

select

my @pass = $q->select(@objs);

Return all objects which pass all query assertions in $q.

Exports

parse_query

my $q = parse_query($query_string);

Optional export which provides a more procedural interface to the constructor of this package.

Query syntax

The query language is specified in a Pegex grammar called query-tags which is included in the distribution's share directory. See that file for detailed technical information. What follows is an overview of the language.

EXAMPLES

Searching a small database

Get all books (co-)authored by /foy/:

use Modern::Perl;
use Query::Tags qw(parse_query);

my @books = (
    { title => 'Programming Perl', authors => 'Tom Christiansen, brian d foy, Larry Wall, Jon Orwant' },
    { title => 'Learning Perl', authors => 'Randal L. Schwartz, Tom Phoenix, brian d foy' },
    { title => 'Intermediate Perl', authors => 'Randal L. Schwartz and brian d foy, with Tom Phoenix' },
    { title => 'Mastering Perl', authors => 'brian d foy' },
    { title => 'Perl Best Practices', authors => 'Damian Conway' },
    { title => 'Higher-Order Perl', authors => 'Mark-Jason Dominus' },
    { title => 'Object Oriented Perl', authors => 'Damian Conway' },
    { title => 'Modern Perl', authors => 'chromatic' }
);

say $_->{title} for parse_query(q[:authors/foy/])->select(@books);

Email headers

Find all work emails from a mailing list that mention /seminar/ or /talk/:

use v5.16;
use Mail::Header;
use Path::Tiny;
use Query::Tags qw(parse_query);

my @mail = map { Mail::Header->new([$_->lines]) } path('~/Mail/work/cur')->children;
my @headers = map { my $mh = $_; +{ map { fc $_ => $mh->get($_) } $mh->tags } } @mail;
say $_->{subject} for
    parse_query(q[:list-id :subject|</(?i)seminar/ /(?i)talk/>])->select(@headers);

AUTHOR

Tobias Boege tobs@taboege.de

COPYRIGHT AND LICENSE

This software is copyright (C) 2025 by Tobias Boege.

This is free software; you can redistribute it and/or modify it under the terms of the Artistic License 2.0.