<?xml version="1.0" encoding="windows-1252"?>
<node id="288039" title="Re: Things you need to know before programming Perl ithreads" created="2003-08-31 13:06:46" updated="2005-07-29 07:03:46">
<type id="11">
note</type>
<author id="82147">
Zaxo</author>
<data>
<field name="doctext">
&lt;p&gt;[liz]++, thanks.&lt;/p&gt;
&lt;p&gt;I got curious about how closures would do, so I tried your technique to see what happens (perl 5.8.1-RC2),
&lt;code&gt;use threads();

{
    my $foo = 'bar';
    sub foo () :lvalue { $foo }
}

threads-&gt;new(
    sub {
        foo = 'quux';
        print 'thread: coderef = ', \&amp;foo, $/;
        print 'foo is ', foo, $/;
    }
)-&gt;join;

print 'main: coderef = ', \&amp;foo, $/;
print 'foo is ', foo, $/;
__END__

$ perl thrcl.pl
thread: coderef = CODE(0x8129538)
foo is quux
main: coderef = CODE(0x8060674)
foo is bar&lt;/code&gt;
so the object of the closure is duplicated - you did say &lt;em&gt;everything&lt;/em&gt;, but I had to check.&lt;/p&gt;
&lt;p&gt;Now, what if I want all threads to see the same thing?
&lt;code&gt;use threads();
use threads::shared;
{
    my $foo :shared = 'bar';
    sub foo () :lvalue { $foo }
}

threads-&gt;new(
    sub {
        foo = 'quux';
        print 'thread: coderef = ', \&amp;foo, $/;
        print 'foo is ', foo, $/;
    }
)-&gt;join;

print 'main: coderef = ', \&amp;foo, $/;
print 'foo is ', foo, $/;
__END__

$ perl thrcl.pl
thread: coderef = CODE(0x8163924)
foo is quux
main: coderef = CODE(0x80fe448)
foo is quux&lt;/code&gt;
so both foo()'s point to the same cloistered object.&lt;/p&gt;
&lt;p&gt;Trying to stick the :shared attribute on &lt;tt&gt;sub foo&lt;/tt&gt; did no good. I expected it to prevent duplication of the code, but perl smacked me down with,
&lt;code&gt;$ perl thrcl.pl
Invalid CODE attribute: shared at thrcl.pl line 5
BEGIN failed--compilation aborted at thrcl.pl line 5.&lt;/code&gt;
I suspect I need to read some more of the fine manual.&lt;/p&gt;
&lt;p&gt;After Compline,&lt;br/&gt;Zaxo&lt;/p&gt;</field>
<field name="root_node">
288022</field>
<field name="parent_node">
288022</field>
</data>
</node>
