1Syntax::Keyword::DynamiUcsaelrlyC(o3n)tributed Perl DocuSmyennttaaxt:i:oKneyword::Dynamically(3)
2
3
4

NAME

6       "Syntax::Keyword::Dynamically" - dynamically change the value of a
7       variable
8

SYNOPSIS

10          use Syntax::Keyword::Dynamically;
11
12          my $logger = ...;
13
14          sub operate
15          {
16             dynamically $logger->level = LOG_DEBUG;
17
18             do_things();
19          }
20

DESCRIPTION

22       This module provides a syntax plugin that implements a single keyword,
23       "dynamically", which alters the behaviour of a scalar assignment
24       operation.  Syntactically and semantically it is similar to the built-
25       in perl keyword "local", but is implemented somewhat differently to
26       give two key advantages over regular "local":
27
28       · You can "dynamically" assign to lvalue functions and accessors.
29
30       · You can "dynamically" assign to regular lexical variables.
31
32       Semantically, the behaviour can be considered equivalent to
33
34          {
35             my $old = $VAR;
36             $VAR = "new value";
37
38             ...
39
40             $VAR = $old;
41          }
42
43       Except that the old value will also be restored in the case of
44       exceptions, "goto", "next/last/redo" or similar ways to leave the
45       controlling block scope.
46

KEYWORDS

48   dynamically
49          {
50             dynamically LVALUE = EXPR;
51             ...
52          }
53
54       The "dynamically" keyword modifies the behaviour of the following
55       expression.  which must be a scalar assignment. Before the new value is
56       assigned to the lvalue, its current value is captured and stored
57       internally within the Perl interpreter. When execution leaves the
58       controlling block for whatever reason, as part of block scope cleanup
59       the saved value is restored.
60
61       The LVALUE may be any kind of expression that allows normal scalar
62       assignment; lexical or package scalar variables, elements of arrays or
63       hashes, or the result of calling an ":lvalue" function or method.
64
65       If the LVALUE has any GET magic associated with it (including a "FETCH"
66       method of a tied scalar) then this will be executed exactly once when
67       the "dynamically" expression is evaluated.
68
69       If the LVALUE has any SET magic associated with it (including a "STORE"
70       method of a tied scalar) then this will be executed exactly once when
71       the "dynamically" expression is evaluated, and again a second time when
72       the controlling scope is unwound.
73
74       When the LVALUE being assigned to is a hash element, e.g. one of the
75       following forms
76
77          dynamically $hash{key} = EXPR;
78          dynamically $href->{key} = EXPR;
79
80       the assignment additionally ensures to remove the key if it is newly-
81       added, and restores by adding the key back again if it had been deleted
82       in the meantime.
83

WITH Future::AsyncAwait

85       As of Future::AsyncAwait version 0.32, cross-module integration tests
86       assert that the "dynamically" correctly works across an "await"
87       boundary.
88
89          use Future::AsyncAwait;
90          use Syntax::Keyword::Dynamically;
91
92          our $var;
93
94          async sub trial
95          {
96             dynamically $var = "value";
97
98             await func();
99
100             say "Var is still $var";
101          }
102
103       When context-switching between scopes in which a variable is
104       "dynamically" modified, the value of the variable will be swapped in
105       and out, possibly multiple times if necessary, to ensure the visible
106       value remains as expected.
107

AUTHOR

109       Paul Evans <leonerd@leonerd.org.uk>
110
111
112
113perl v5.32.1                      2021-02-03   Syntax::Keyword::Dynamically(3)
Impressum