Revision history for Switch-Declare
0.04 2026-06-14
- New patterns:
* `case FOO` - a bareword naming an inlinable `use constant`
folds to its value at compile time and is classified like the literal
it holds (numeric -> ==, string -> eq; dispatch-eligible). Zero runtime
cost. Package-qualified constants (`Pkg::FOO`) are supported.
* `case == $x` / `case eq $x` - compare the topic against a runtime
scalar variable, naming the operator as in Perl itself: `==` is
numeric (looks_like_number-guarded on both sides), `eq` is string.
The operand is a plain scalar ($name or $Pkg::name), including `our`
and package globals. Both are undef/type-safe and warning-free.
* `case =~ $rx` - match the topic against a runtime pattern (a qr// or
a string) held in a scalar, complementing the compile-time /literal/
form. Undef-safe and warning-free. As a runtime membership test it
does not set capture variables ($1, @+); for captures use a predicate
arm, `case sub { $_[0] =~ $rx }`.
0.03 2026-06-14
- New patterns:
* `case undef` - matches an undefined topic.
* `case ref(TYPE)` / `ref` - ref($topic) [eq "TYPE"]; bare = any ref.
* `case reftype(TYPE)` / `reftype` - underlying type, through blessing.
* `case isa(Class)` - blessed object derived from Class (fast @ISA).
- Undef/type safety: every pattern is now warning-free. An undef topic
matches only `case undef` (else falls to default) instead of warning
and mis-matching `case 0`/`case ""`. Numeric patterns are guarded by
looks_like_number, so a non-numeric topic neither warns ("Argument
isn't numeric") nor mis-matches (`"one" == 0` was true; now it is not).
- The numeric guard is computed once per switch (hoisted), so numeric
switches run on par with an equivalently type-safe hand-written chain.
0.02 2026-06-13
- Fix load failure on perl 5.14-5.20.
0.01 2026-06-12
First release.
- `switch (EXPR) { case PAT { ... } ... default { ... } }` as a real
lexical pragma, recognised only within `use Switch::Declare` scope.
- Compile-time keyword plugin; the construct lowers to a native
conditional expression. No source filter, no smartmatch, no CPAN
dependencies (core perl 5.14+ only).
- Pattern kinds: number (==), string (eq), regex /.../imsx, range
[LO..HI], list [a,b,c] membership, and predicates - either \&name
(also package-qualified, \&Pkg::name) or an inline sub { ... } that
closes over the enclosing lexicals. Each lowers to native ops (regex
compiles to a real OP_MATCH at compile time); there are no runtime
helper subs.
- Statement and expression (value-returning) forms; usable infix.
- Scrutinee evaluated exactly once; first matching case wins; optional
trailing default.
- Fast path: a plain variable/constant scrutinee with single-expression
arms compiles to exactly a hand-written if/elsif chain (0-2% in the
bundled benchmark).
- Dispatch mode: a string-keyed lookup table (>= 4 arms, constant
values) compiles to a single O(1) hash lookup against a compile-time
hash - ~2.5x faster than the if/elsif chain at 20 arms. Chosen
automatically; never changes behaviour.