NAME

Destructure::Declare - lexically scoped structural destructuring let

SYNOPSIS

use Destructure::Declare;

let [$first, $second, @rest]         = $aref;     # arrayref + slurpy tail
let {name => $n, age => $a}          = $href;     # hashref by key
let {id => $id, %rest}               = $href;     # hashref + remaining keys
let ($x, $y, @more)                  = @list;     # list-context destructure
let [$head, [$x, $y], %opts]         = $nested;   # nested patterns
let {id => $id, tags => [$t, @more]} = $record;   # mixed nesting
let [$status, $body = '']            = $pair;     # per-slot default (//)
let [$a, undef, $c]                  = $triple;   # holes (skip a slot)

DESCRIPTION

Destructure::Declare installs a let keyword that parses an entire let PATTERN = EXPR; construct at compile time and lowers it to ordinary pad lexical declarations with direct element access. All parsing happens once, at compile time - nothing of the parser remains at runtime.

Semantics

  • A [ ... ] pattern treats the right-hand side as an arrayref; a { ... } pattern treats it as a hashref; a ( ... ) pattern destructures the right-hand side as a list (evaluated in list context), like my (...) = LIST but with the extra powers below. The right-hand side is evaluated exactly once.

  • The names introduced are real my lexicals, visible for the rest of the enclosing block.

  • $x = DEFAULT supplies a default applied with // (used when the slot is missing or undef). The default expression is evaluated lazily.

  • undef in a pattern position is a hole: that slot is skipped.

  • A trailing slurpy must be the last element. In an array or list pattern @rest captures the positional tail (and %rest captures that tail as key/value pairs); in a hash pattern %rest captures every key not already named.

AUTHOR

LNATION, <email@lnation.org>

LICENSE AND COPYRIGHT

This software is Copyright (c) 2026 by LNATION <email@lnation.org>.

This is free software, licensed under:

The Artistic License 2.0 (GPL Compatible)