develooper Front page | perl.perl5.porters | Postings from November 2003

Why doesn't $shared_hash->{thing} = $obj DWIM?

Thread Next
From:
Michael G Schwern
Date:
November 4, 2003 02:47
Subject:
Why doesn't $shared_hash->{thing} = $obj DWIM?
Message ID:
20031104104710.GC12786@localhost.comcast.net
  use threads;
  use threads::shared;

  my $hash = &share({});
  my $obj  = 'foo';
  $hash->{thing} = $obj;

Right?  Fine, no complaints.

  use threads;
  use threads::shared;

  my $hash = &share({});
  my $obj  = bless {}, 'Foo';
  $hash->{thing} = $obj;

"Invalid value for shared scalar at ... line 8."

From my PoV (ie. I don't want to care about threads) why doesn't Perl just 
figure it out?  Why do I have to remember to share every reference I assign
to a shared hash?  Even if there's a good reason, it still makes working
with them a pain in the ass.

There's also an encapsulation piercing.  This problem shows up in
Test::Builder.  If you try to feed it an object with stringification
overloaded for the test name it will blow up because Test::Builder
assigns the object (which it thinks is a string) to the shared hash of
test results.  Part of the point of overloading stringification is to hide 
the fact that its an object.  Now I've got to explicitly force stringification
to avoid putting an unshared object onto a shared hash.

Just make the bad things go away.


-- 
Michael G Schwern        schwern@pobox.com  http://www.pobox.com/~schwern/
My breasts are arousing weapons.

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About