I have to store subref in a thread::shared data structure. It seems not allowed. So I decide to save function's name instead. It seems works well. | [reply] |
I have to store subref in a thread::shared data structure. It seems not allowed.
What makes you think that? Got an electric shock as soon as you typed the code and another one when you ran it?
Come on, you are not new here. Show us the relevant parts of your code and any error messages you get from running it! Random guessing won't help you.
Alexander
--
Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
| [reply] |
What makes you think that? Got an electric shock as soon as you typed the code and another one when you ran it?
Um...
perl -Mthreads -Mthreads::shared -E"my $shared :shared = sub { say 'te
+st' };"
Invalid value for shared scalar at -e line 1.
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] |
The relevant things are pretty big and messy:
In general, I'm attempting to make a event dispatcher class. Obviously, the EventDispatcher has to store event listeners in some form. Moreover, I want the whole thing to be thread compatible, so the EventDispatcher object should be shared.
This is the part of code related to the topic. The whole thing is too big (several files).
package Dispatcher;
use strict;
use threads::shared;
sub new {
my($inv,%arg) = @_;
my $class = ref $inv || $inv;
# I forgot the name of the function.
# It's provided by the new version of threads::shared.
# It returns a shared version of data, very convenient,
# so we don't have to recursively share things.
my $data = shared_data {
%arg,
_listeners => {
'EVENT_FOO' => [],
'EVENT_BAR' => []
}
};
return bless $data,$class;
}
sub addListener {
my($self,$e_name,$ref) = @_;
die if ! exists $self->{_listeners}{$e_name};
# here will throw an error
# as the sub ref cannot be put into shared store
push @{$self->{_listeners}{$e_name}}, $ref;
}
sub dispatchEvent {
my $event = shift;
my $e_name = $event->eventName;
die if ! exists $self->{_listeners}{$e_name};
foreach my $func ( @{$self->{_listeners}{$e_name};} ) {
&$func($event);
}
}
So, I use Package::Function name instead, and works well.
| [reply] [d/l] |
I think the whole process of using function name is not dangerous. The string of names are only affected by system, not touched by user or external input data.
| [reply] |