Syntax...ically(3pm)
π Syntax...ically(3pm) π
okay so bestie, before we even get into it: this is a page from the User Contributed Perl Documentation, and the module's actual name is Syntax::Keyword::Dynamically (the page filename gets cute and truncated, iykyk). it gives you one (1) keyword that lets you β¨dynamicallyβ¨ mess with a variable's value β and then puts it back like nothing ever happened. main character energy fr fr.
πΈ NAME πΈ
Syntax::Keyword::Dynamically β dynamically change the value of a variable
β¨ SYNOPSIS β¨
use Syntax::Keyword::Dynamically;
my $logger = ...;
sub operate
{
dynamically $logger->level = LOG_DEBUG;
do_things();
}
π DESCRIPTION π
This module is a syntax plugin, and it implements exactly one keyword: dynamically. What does it do? It changes up how a scalar assignment behaves. Syntactically and semantically it's giving the built-in perl keyword local, but it's built a little differently underneath, which is how it pulls off two key glow-ups over regular local:
- You can
dynamicallyassign to lvalue functions and accessors. π - You can
dynamicallyassign to regular lexical variables. π¦
Semantically, think of the behaviour as being equivalent to this:
{
my $old = $VAR;
$VAR = "new value";
...
$VAR = $old;
}
...EXCEPT π that the old value also gets restored if there are exceptions, or goto, or next/last/redo, or literally any other way of bailing out of the controlling block scope. no cap.
π₯ KEYWORDS π₯
π dynamically
{
dynamically LVALUE = EXPR;
...
}
The dynamically keyword modifies the behaviour of the expression that follows it, and that expression must be a scalar assignment. Before the new value gets assigned to the lvalue, its current value is captured and stashed internally inside the Perl interpreter. Then when execution leaves the controlling block β for whatever reason, we don't judge β the saved value gets restored as part of block scope cleanup. iconic behaviour honestly.
The LVALUE can be any kind of expression that allows a normal scalar assignment: lexical or package scalar variables, elements of arrays or hashes, or the result of calling an :lvalue function or method.
If the LVALUE has any GET magic attached to it (and yes, that includes a FETCH method of a tied scalar), then that GET magic will run exactly once when the dynamically expression is evaluated.
If the LVALUE has any SET magic attached to it (including a STORE method of a tied scalar), then that will run exactly once when the dynamically expression is evaluated, and then again a second time when the controlling scope is unwound. two (2) times, bestie, that's the deal.
When the LVALUE you're assigning to is a hash element, like one of these forms here:
dynamically $hash{key} = EXPR;
dynamically $href->{key} = EXPR;
the assignment additionally makes sure to remove the key if it's newly-added, and restores by adding the key back again if it had been deleted in the meantime. it's keeping receipts π§
π WITH Future::AsyncAwait π
As of Future::AsyncAwait version 0.32, the cross-module integration tests assert that dynamically correctly works across an await boundary. slay.
use Future::AsyncAwait;
use Syntax::Keyword::Dynamically;
our $var;
async sub trial
{
dynamically $var = "value";
await func();
say "Var is still $var";
}
When you're context-switching between scopes where a variable has been dynamically modified, the value of the variable gets swapped in and out β possibly multiple times if it needs to β so that the visible value stays exactly as you'd expect it to. π
π AUTHOR π
Paul Evans <leonerd@leonerd.org.uk>
and that's the whole page bestie, we ate π
sending you off with the cute little sign-off from the bottom of the manual: this is perl v5.42.3, dated 2026-08-21, and yes the footer really does spell the name Syntax...ically(3pm) because truncaβ anyway. love u, go restore your variables properly π«Άβ¨
π¦ where this page comes from
- ubuntu stonking: libsyntax-keyword-dynamically-perl 0.14-1build1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms