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.839
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 new ( [ value ] )
77 Constructs a new object. Its value is undefined when "value" is not
78 specified.
79
80 # non-shared or local construction for use by a single process
81
82 use MCE::Shared::Scalar;
83
84 $var = MCE::Shared::Scalar->new( "foo" );
85 $var = MCE::Shared::Scalar->new;
86
87 # construction for sharing with other threads and processes
88
89 use MCE::Shared;
90
91 $var = MCE::Shared->scalar( "bar" );
92 $var = MCE::Shared->scalar;
93
94 set ( value )
95 Preferably, set the value via the OO interface. Otherwise, "TIE" is
96 activated on-demand for setting the value. The new value is returned
97 in scalar context.
98
99 $val = $var->set( "baz" );
100 $var->set( "baz" );
101 ${$var} = "baz";
102
103 get
104 Likewise, obtain the value via the OO interface. "TIE" is utilized
105 for retrieving the value otherwise.
106
107 $val = $var->get;
108 $val = ${$var};
109
110 len
111 Returns the length of the value. It returns the "undef" value if the
112 value is not defined.
113
114 $len = $var->len;
115 length ${$var};
116
118 This module is equipped with sugar methods to not have to call "set"
119 and "get" explicitly. In shared context, the benefit is atomicity and
120 reduction in inter-process communication.
121
122 The API resembles a subset of the Redis primitives
123 <http://redis.io/commands#strings> without the key argument.
124
125 append ( value )
126 Appends a value at the end of the current value and returns its new
127 length.
128
129 $len = $var->append( "foo" );
130
131 decr
132 Decrements the value by one and returns its new value.
133
134 $num = $var->decr;
135
136 decrby ( number )
137 Decrements the value by the given number and returns its new value.
138
139 $num = $var->decrby( 2 );
140
141 getdecr
142 Decrements the value by one and returns its old value.
143
144 $old = $var->getdecr;
145
146 getincr
147 Increments the value by one and returns its old value.
148
149 $old = $var->getincr;
150
151 getset ( value )
152 Sets the value and returns its old value.
153
154 $old = $var->getset( "baz" );
155
156 incr
157 Increments the value by one and returns its new value.
158
159 $num = $var->incr;
160
161 incrby ( number )
162 Increments the value by the given number and returns its new value.
163
164 $num = $var->incrby( 2 );
165
167 The implementation is inspired by Tie::StdScalar.
168
170 MCE, MCE::Hobo, MCE::Shared
171
173 Mario E. Roy, <marioeroy AT gmail DOT com>
174
175
176
177perl v5.28.0 2018-08-25 MCE::Shared::Scalar(3)