| [reply] [d/l] [select] |
if i declare my $data : shared within an if { } block, does the shared value go out of scope after the block?
Yes, it will go out of scope--but it will not cease to exists if
- anything holds a reference to it.
- if it is a reference and you have assigned it to something who's scope is wider.
That's not a good explanation. The problem is it depends upon what $data is (contains)?
- If $data is a simple scalar value, then there is no need to share() it in order to assign it to a shared data structure.
This is because assigning a simple scalar just copies its value into the (shared) destination.
- However, if $data is a reference to a hash or array (or nested structure of either or both), then simply sharing that reference is not enough.
This is because (as indicated above) when you share a reference to a compound datastructure (hash or array), it will (silently) empty the referenced structure!
For example:
use threads;
use trheads::shared;
## Non-shared hash assigned some data:
my %d = (1 .. 10 ); print Dumper \%d;
$VAR1 = {
'1' => 2,
'3' => 4,
'7' => 8,
'9' => 10,
'5' => 6
};
## Share a reference to that hash and assign it to a shared scalar
my $r:shared = share( %d );
## And not only will the reference point to an empty hash
print Dumper $r;
$VAR1 = {};
## But also the contents of the original hash will have been silently
+discarded
print Dumper \%d;
$VAR1 = {};
So, if as suggested by your OP code, $data contains a reference (as returned by Storable::retrieve(), then you will need to copy the contents of the referenced thing into a shared equivalent.
To illustrate (Please read the comments carefully!):
our %planets : shared;
our $response = SomeClass::TCP_IP_Response->new(@some_arguments);
[...]
lock %planets;
if (exists $planets{$some_id}) {
lock $planets{$some_id};
$response->content($planets{$some_id}->{'data'});
$planets{$some_id}->{'atime'} = time;
}
else {
my $data;
[...] # read the serialized data from disk
## Assuming $data is a reference to a *simple*, *single-level* hash
## Copy the data into a shared equivalent;
my %sharedHash :shared = %{ $data };
## And then assign a reference to the *shared* copy.
# my %el = ( 'data' => \%sharedHash,
# 'atime' => time,
# 'deleted' => 0,
# 'modified' => 0
# );
## But if you do this, *ALL THE CONTENTS OF %e1 WILL BE DISCARD*
# $planets{$some_id} = share(%el);
## So make the local datastructure shared also
my %el :shared = ( 'data' => \%sharedHash,
'atime' => time,
'deleted' => 0,
'modified' => 0
);
## Now you do not need to use shared()
## as a reference to a shared object is shared.
$planets{$some_id} = \%el;
}
And once you've placed a reference to a local (lexical) variable (shared or not) into a variable who's scope is wider, the contents of that variable (but not the name) will persist (not be GC'd) as long the reference persists.
But also note that if $data is a reference to a hash or array that contains nested references to other hashes or arrays, then you will also need to copy this nested structures manually. That is where a recursive structure traversal tool is needed.
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
| [reply] [d/l] [select] |
| [reply] [d/l] |
hmm... i did what you advised me to do, but with a remarkable result:
my %el : shared = ( 'data' => $scalar_data,
'atime' => time,
'deleted' => 0,
'modified' => 0
);
$planets{$some_id} = \%el;
lock $planets{$some_id};
# results in an error: "lock can only be used on shared values."
--------------------------------
masses are the opiate for religion.
| [reply] [d/l] |