[Israel.pm] Lexical veriable copyed with new thread

Shmuel Fomberg semuelf at 012.net.il
Sat Dec 22 10:03:40 PST 2007


Hi All.

Please observe to following program:
--- Start Code
#!/usr/bin/perl -w
use strict;
use threads;

sub thr {
    print "Thread running.. Done.\n";
}

sub run_test {
    my $x = bless {}, 'Obj';
    threads->create(\&thr)->join();
}

sub Obj::DESTROY {
    print "Destroyed from thread ", threads->tid, "\n";
}

run_test();
--- End Code

The output that I get is:
Thread running.. Done.
Destroyed from thread 1
Destroyed from thread 0

It is clear the $x was copied to the new thread, and destroyed when it
ended.
But why? $x is not a global, and not passed as parameter to the thread's
entry point. Why Perl copy it?

Sometimes I think that I don't understand anything at all... T_T

Shmuel. 





More information about the Perl mailing list