Home: Perl Programming Help: Intermediate:
Sharing object in perl



samir.gambler
New User

Nov 23, 2011, 4:02 AM


Views: 2816
Sharing object in perl

I am creating an object like

Code
$ro_api  = Win32::OLE->new("xxx.xxx");


Now I want to share $ro_api with other thread.
If I am trying to do like scalar variable it is throwing error when i am using the $ro_api at later point.

Quote
PERL ERROR: Invalid value for shared scalar at...


Plzz help urgent...
Thanks


wickedxter
User

Nov 23, 2011, 5:31 PM


Views: 2795
Re: [samir.gambler] Sharing object in perl


Code
 
my $ro_api = Win32::OLE->new("xxx.xxx");

my $something = $ro_api->function;



samir.gambler
New User

Nov 24, 2011, 1:16 AM


Views: 2790
Re: [wickedxter] Sharing object in perl

Hello wickedxter,

What I am doing is something like this.

Code
use threads; 
use threads::shared;
use Win32::OLE;
my $ro_api:shared;

$ro_api = Win32::OLE->new("xxx.xxx");


As soon as controls comes to this line $ro_api = Win32::OLE->new("xxx.xxx");
It throws error PERL ERROR: Invalid value for shared scalar at...


budman
User

Feb 12, 2012, 11:57 AM


Views: 2564
Re: [samir.gambler] Sharing object in perl

Since a blessed object ref is returned, you need to use the shared_clone.
I don't have Windows to test, but did a sample below.

from docs: Shared variables can only store scalars, refs of shared variables, or refs of shared data.


$ro_api = Win32::OLE->new("xxx.xxx");


Code
package Test; 
use Moose;
has 'name' => ( is => 'rw', isa => 'Str');

package main;
use threads;
use threads::shared;

my $testobj = Test->new(name=>'test name');
my $var :shared = shared_clone($testobj);

print "Test: ",$testobj->name()."\n";
print "Var: ",$var->name(),"\n";


Test: test name
Var: test name