Generate SQL from Perl data structures.
SQL::Abstract turns Perl hashes and arrays into an SQL statement plus a list of bind values, ready for DBI. Values always end up as bind values, never in the SQL text.
use SQL::Abstract;
my $sql = SQL::Abstract->new;
my ($stmt, @bind) = $sql->select(
'people',
[qw(name email)],
{ status => 'active', age => { '>' => 18 } },
'name',
);
# $stmt: SELECT name, email FROM people WHERE ( age > ? AND status = ? ) ORDER BY name
# @bind: (18, 'active')
my $sth = $dbh->prepare($stmt);
$sth->execute(@bind);The same works for insert, update, delete and plain where.
See the full documentation on MetaCPAN.
The distribution also contains:
- SQL::Abstract::Tree - parses and pretty-prints SQL (for debug output)
- SQL::Abstract::Test - compares SQL and bind values in tests, ignoring formatting
cpanm SQL::AbstractFrom a git checkout:
perl Makefile.PL
make
make test
make installDBIx::Class no longer uses this module: since 0.082842 it uses SQL::Abstract::Classic, a frozen copy of the 1.x line.
DBIO, a fork of DBIx::Class, builds its SQL with SQL::Abstract 2.x and uses SQL::Abstract::Tree for its query trace.
- Bugs: rt.cpan.org
- IRC:
#dbioonirc.perl.org - Maintainer: Torsten Raudssus torsten@raudssus.de
Security issues: please do not report them publicly, see SECURITY.md.
Copyright (c) 2001-2026 Nathan Wiger and the SQL::Abstract contributors.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. See LICENSE.