File Coverage

blib/lib/Scalar/Dynamizer.pm
Criterion Covered Total %
statement 26 27 96.3
branch 1 2 50.0
condition n/a
subroutine 9 9 100.0
pod 1 1 100.0
total 37 39 94.8


line stmt bran cond sub pod time code
1             package Scalar::Dynamizer;
2              
3 2     2   231231 use Scalar::Dynamizer::Tie;
  2         4  
  2         72  
4              
5 2     2   10 use strict;
  2         4  
  2         37  
6 2     2   4 use warnings;
  2         3  
  2         63  
7              
8 2     2   6 use Exporter 'import';
  2         3  
  2         170  
9             our @EXPORT_OK = qw(dynamize);
10              
11             use overload
12 2     2   589 'bool' => sub { ${ $_[0] } },
  2         6  
13 3     3   2155 '""' => sub { ${ $_[0] } },
  3         10  
14 1     1   268 '0+' => sub { ${ $_[0] } },
  1         9  
15 2     2   936 fallback => 1;
  2         2653  
  2         14  
16              
17             sub dynamize(&) {
18 2     2 1 156826 my ($code) = @_;
19 2         3 my $scalar;
20              
21 2 50       6 if ( ref($code) ne 'CODE' ) {
22 0         0 croak('dynamize{} requires a code reference');
23             }
24              
25 2         13 tie $scalar, 'Scalar::Dynamizer::Tie', $code;
26              
27 2         6 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             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 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 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.
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
120              
121             This error occurs when the argument to C is not a code reference.
122             Ensure that you pass a valid code block.
123              
124             =item C
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
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<< >>
182              
183             =head1 BUGS
184              
185             Please report any bugs or feature requests to C,
186             or through the web interface at L.
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
203              
204             =item * CPAN Ratings
205              
206             L
207              
208             =item * Search CPAN
209              
210             L
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, L
225              
226             =cut
227              
228             1; # End of Scalar::Dynamizer