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 dynamically assign to lvalue functions and accessors. πŸ’–
  • You can dynamically assign 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