1MCE::Shared::Scalar(3)User Contributed Perl DocumentationMCE::Shared::Scalar(3)
2
3
4
6 MCE::Shared::Scalar - Scalar helper class
7
9 This document describes MCE::Shared::Scalar version 1.873
10
12 A scalar helper class for use as a standalone or managed by
13 MCE::Shared.
14
16 # non-shared or local construction for use by a single process
17
18 use MCE::Shared::Scalar;
19
20 my $var = MCE::Shared::Scalar->new( $val );
21
22 # construction for sharing with other threads and processes
23
24 use MCE::Shared;
25
26 my $var = MCE::Shared->scalar( $val );
27
28 # scalar-like dereferencing
29
30 my $val = ${ $var };
31 ${ $var } = $val;
32
33 # OO interface
34
35 $val = $var->set( $val );
36 $val = $var->get();
37 $len = $var->len();
38
39 # included, sugar methods without having to call set/get explicitly
40
41 $val = $var->append( $string ); # $val .= $string
42 $val = $var->decr(); # --$val
43 $val = $var->decrby( $number ); # $val -= $number
44 $val = $var->getdecr(); # $val--
45 $val = $var->getincr(); # $val++
46 $val = $var->incr(); # ++$val
47 $val = $var->incrby( $number ); # $val += $number
48 $old = $var->getset( $new ); # $o = $v, $v = $n, $o
49
50 For normal scalar behavior, the TIE interface is supported.
51
52 # non-shared or local construction for use by a single process
53
54 use MCE::Shared::Scalar;
55
56 tie my $var, "MCE::Shared::Scalar";
57
58 # construction for sharing with other threads and processes
59
60 use MCE::Shared;
61
62 tie my $var, "MCE::Shared";
63
64 # usage
65
66 $var = 0;
67
68 tied($var)->incrby(20);
69
71 This module may involve TIE when accessing the object via scalar
72 dereferencing. Only shared instances are impacted if doing so.
73 Although likely fast enough for many use cases, the OO interface is
74 recommended for best performance.
75
76 MCE::Shared::Scalar->new ( [ value ] )
77 MCE::Shared->scalar ( [ value ] )
78 Constructs a new object. Its value is undefined when "value" is not
79 specified.
80
81 # non-shared or local construction for use by a single process
82
83 use MCE::Shared::Scalar;
84
85 $var = MCE::Shared::Scalar->new( "foo" );
86 $var = MCE::Shared::Scalar->new;
87
88 # construction for sharing with other threads and processes
89
90 use MCE::Shared;
91
92 $var = MCE::Shared->scalar( "bar" );
93 $var = MCE::Shared->scalar;
94
95 set ( value )
96 Preferably, set the value via the OO interface. Otherwise, "TIE" is
97 activated on-demand for setting the value. The new value is returned in
98 scalar context.
99
100 $val = $var->set( "baz" );
101 $var->set( "baz" );
102 ${$var} = "baz";
103
104 get
105 Likewise, obtain the value via the OO interface. "TIE" is utilized for
106 retrieving the value otherwise.
107
108 $val = $var->get;
109 $val = ${$var};
110
111 len
112 Returns the length of the value. It returns the "undef" value if the
113 value is not defined.
114
115 $len = $var->len;
116 length ${$var};
117
119 This module is equipped with sugar methods to not have to call "set"
120 and "get" explicitly. In shared context, the benefit is atomicity and
121 reduction in inter-process communication.
122
123 The API resembles a subset of the Redis primitives
124 <http://redis.io/commands#strings> without the key argument.
125
126 append ( value )
127 Appends a value at the end of the current value and returns its new
128 length.
129
130 $len = $var->append( "foo" );
131
132 decr
133 Decrements the value by one and returns its new value.
134
135 $num = $var->decr;
136
137 decrby ( number )
138 Decrements the value by the given number and returns its new value.
139
140 $num = $var->decrby( 2 );
141
142 getdecr
143 Decrements the value by one and returns its old value.
144
145 $old = $var->getdecr;
146
147 getincr
148 Increments the value by one and returns its old value.
149
150 $old = $var->getincr;
151
152 getset ( value )
153 Sets the value and returns its old value.
154
155 $old = $var->getset( "baz" );
156
157 incr
158 Increments the value by one and returns its new value.
159
160 $num = $var->incr;
161
162 incrby ( number )
163 Increments the value by the given number and returns its new value.
164
165 $num = $var->incrby( 2 );
166
168 The implementation is inspired by Tie::StdScalar.
169
171 MCE, MCE::Hobo, MCE::Shared
172
174 Mario E. Roy, <marioeroy AT gmail DOT com>
175
176
177
178perl v5.32.1 2021-01-27 MCE::Shared::Scalar(3)