Dynamizer.pm

filter: Statementstmt 96.6% 29 / 30 Branchbran 50.0% 1 / 2 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 9 / 9 Podpod 100% 1 / 1 totaltotal 95.2% 40 / 42 CC 2 SCAR 1.9
CC
2
Cov
94%
dynamize
2.1
BEGIN
1.0
BEGIN
1.0
CRAP 1.2
↑ Summary Dynamizer/Tie.pm »
1 package Scalar::Dynamizer;
2  
3 2 ▶use Scalar::Dynamizer::Tie;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 our @EXPORT_OK = qw(dynamize);
10  
11 use overload
12 2 ▶ 'bool' => sub { ${ $_[0] } },
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
13 3 ▶ '""' => sub { ${ $_[0] } },
Subroutine __ANON__: called CC=1 SCAR=0.0
14 1 ▶ '0+' => sub { ${ $_[0] } },
Subroutine __ANON__: called CC=1 SCAR=0.0
15 2 fallback => 1;
16  
17 2 ▶sub dynamize(&) {
Subroutine dynamize: called CC=2 SCAR=7.2
Pod documented
18 2 my ($code) = @_;
19 2 my $scalar;
20  
21 2 ▶ if ( ref($code) ne 'CODE' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $code ne "CODE") 0 2 2
22 0 croak('dynamize{} requires a code reference');
23 }
24  
25 2 tie $scalar, 'Scalar::Dynamizer::Tie', $code;
26  
27 2 return bless \$scalar, __PACKAGE__;
28 }
29  
30 =head1 NAME
31  
32 Scalar::Dynamizer - Create dynamic, update-on-access scalars
33  
34 =head1 VERSION
35  
36 Version 1.000
37  
38 =cut
39  
40 2 our $VERSION = '1.000';
41  
42 =head1 SYNOPSIS
43  
44 use Scalar::Dynamizer qw(dynamize);
45  
46 my $count = 0;
47  
48 # Contrived example of a simple counter
49 my $counter = dynamize {
50 return ++$count;
51 };
52  
53 print $counter; # 1
54 print $counter * 100; # 200
55 print "Count is $counter"; # "Count is 3"
56  
57 # More realistic example involving a database query
58 my $points = dynamize {
59 return database->quick_count("solved_challenges", {
60 user => session("user")
61 });
62 };
63  
64 =head1 DESCRIPTION
65  
66 C<Scalar::Dynamizer> enables the creation of dynamic scalars whose values are
67 automatically recomputed each time they are accessed. This functionality is
68 powered by Perl's C<tie> mechanism and operator overloading. By providing
69 convenient syntactic sugar that simplies the use of tied scalars, this module
70 makes working with dynamically evaluated scalars transparent and less verbose.
71  
72 Dynamic scalars are particularly useful when a scalar's value depends on the
73 program's current state at the time of access, such as counters, timestamps, or
74 real-time data from a database.
75  
76 =head1 EXPORT
77  
78 The module exports a single method, C<dynamize>.
79  
80 =head1 SUBROUTINES/METHODS
81  
82 =head2 dynamize
83  
84 my $scalar = dynamize { ... };
85  
86 Creates a dynamic, update-on-access scalar. The code block provided must return
87 the value of the scalar. Each time the scalar is accessed, the provided code
88 block is executed and its return value is used as the scalar's new value.
89  
90 =head3 Parameters
91  
92 =over
93  
94 =item * A code block (required)
95  
96 The block of code that returns the scalar's value.
97  
98 =back
99  
100 =head3 Returns
101  
102 A scalar reference that transparently behaves like a scalar in boolean, numeric,
103 and string contexts.
104  
105 =head3 Example
106  
107 use POSIX qw(strftime);
108 my $timestamp = dynamize { strftime("[%Y/%m/%d %H:%M:%S]", localtime) };
109 print "$timestamp something happened\n";
110 sleep(1);
111 print "$timestamp something else happened later\n";
112  
113 See the `examples/` directory for additional examples.
114  
115 =head1 DIAGNOSTICS
116  
117 =over
118  
119 =item C<dynamize{} requires a code reference>
120  
121 This error occurs when the argument to C<dynamize> is not a code reference.
122 Ensure that you pass a valid code block.
123  
124 =item C<Cannot assign to a dynamic scalar>
125  
126 Dynamic scalars are immutable and cannot be assigned a value. Any attempt to do
127 so will result in this error.
128  
129 =back
130  
131 =head1 CONFIGURATION AND ENVIRONMENT
132  
133 Scalar::Dynamizer does not utilize any special configuration or environment
134 variables.
135  
136 =head1 DEPENDENCIES
137  
138 None
139  
140 =head1 INCOMPATIBILITIES
141  
142 There are no known incompatibilities with other modules at this time.
143  
144 =head1 BUGS AND LIMITATIONS
145  
146 =over
147  
148 =item * Overload Limitations
149  
150 In certain contexts, Perl may bypass operator overloading for C<Scalar::Dynamizer>
151 objects. This occurs when the object is accessed in a way that does not explicitly
152 trigger stringification, numeric conversion, or boolean evaluation. For example,
153 passing the variable to a subroutine expecting a raw scalar reference or using
154 it in highly specific scenarios may result in the tied scalar being returned
155 as an object reference instead of the computed value.
156  
157 To mitigate this, ensure the dynamized scalar is used in a context that explicitly
158 resolves it (e.g., string or numeric operations). For example:
159  
160 my $string = "$dynamized_scalar"; # interpolation triggers stringification
161 my $number = 0 + $dynamized_scalar; # explicit numerical context
162  
163  
164 =item * Thread Safety
165  
166 The module does not guarantee thread safety. Use with care in threaded programs.
167  
168 =item * Immutable Scalars
169  
170 Dynamic scalars cannot be assigned a value.
171  
172 =item * Performance
173  
174 Frequent computation of dynamic values may have a performance impact, particularly
175 if the code block involves expensive operations.
176  
177 =back
178  
179 =head1 AUTHOR
180  
181 Jeremi Gosney, C<< <epixoip at cpan.org> >>
182  
183 =head1 BUGS
184  
185 Please report any bugs or feature requests to C<bug-scalar-dynamizer at rt.cpan.org>,
186 or through the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Scalar-Dynamizer>.
187 I will be notified, and then you'll automatically be notified of progress on your
188 bug as I make changes.
189  
190 =head1 SUPPORT
191  
192 You can find documentation for this module with the perldoc command.
193  
194 perldoc Scalar::Dynamizer
195  
196 You can also look for information at:
197  
198 =over 4
199  
200 =item * RT: CPAN's request tracker (report bugs here)
201  
202 L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=Scalar-Dynamizer>
203  
204 =item * CPAN Ratings
205  
206 L<https://cpanratings.perl.org/d/Scalar-Dynamizer>
207  
208 =item * Search CPAN
209  
210 L<https://metacpan.org/release/Scalar-Dynamizer>
211  
212 =back
213  
214 =head1 LICENSE AND COPYRIGHT
215  
216 This software is Copyright (c) 2025 by Jeremi Gosney.
217  
218 This is free software, licensed under:
219  
220 The Artistic License 2.0 (GPL Compatible)
221  
222 =head1 SEE ALSO
223  
224 L<perltie>, L<overload>
225  
226 =cut
227  
228 2 1; # End of Scalar::Dynamizer
↑ Summary Dynamizer/Tie.pm »