#!/usr/bin/perl -w use Data::Dumper; use Storable 'freeze', 'thaw'; use threads; use threads::shared; use Net::Ping; # Create hashref, set a value my $hashref = {}; $hashref->{'mine'} = 12; #share hashref - original value gone! share($hashref); print "Shared hashref: ", Dumper($hashref), "\n"; # Create object my $ping = Net::Ping->new(); print "Object: ", Dumper($ping), "\n"; # Share object - values gone share($ping); print "Shared object (empty): ", Dumper($ping), "\n"; # Recreate (bless shared thing, works fine..) $ping = Net::Ping->new(); print "Shared object (renewed): ", Dumper($ping), "\n"; # Create, share, then bless.. works.. my $newping = {}; share($newping); $newping = Net::Ping->new(); print "Shared then bless: ", Dumper($newping), "\n"; # Use in another thread, just to check my $thr = threads->create(\&ping_it); # Wait for ping to complete $thr->join(); sub ping_it { print "ping_it ", Dumper($ping); print "Ping localhost: ", ($ping->ping('localhost') ? "yes" : "no"), "\n"; # Works! } # Put shared thingy in other shared thingy.. (I want(ed) a hash of objects..) my $hashy = {}; share($hashy); # $hashy->{'ping'} = $ping; # print "Hash of obj: ", Dumper($hashy); # Damn, doesnt work - Invalid value for shared scalar at ../threadtest.pl line 53. my %hashy = (); share(%hashy); # $hashy{'ping'} = $ping; # print "Hash of obj: ", Dumper(%hashy); # Damn, doesnt work - Invalid value for shared scalar at ../threadtest.pl line 53. # my $testping = Net::Ping->new(); # $hashy->{'ping'} = $testping; # Can't do that either.. # $hashy->{'ping'} = freeze($ping); # print "Hash of obj: ", Dumper(%hashy); # Nope.. $hashy->{'ping'} = freeze(pingy->new()); print "Hash of obj: ", Dumper($hashy); # hmm, works.. (but only if no globs or code-refs) $thr = threads->create(\&test_me); $thr->join(); sub test_me { print Dumper(thaw($hashy->{'ping'})); } package pingy; use Data::Dumper; sub new { # Create new pingy # Parameter: Class-Name/Reference, Properties my $class = shift; $class = ref($class) || $class; my $self = {'just' => 'testing'}; bless($self, $class); print "Created: ", Dumper($self), "\n"; return $self; }