Perl variable garbage collection and refcount -


i have little confuse variable garbage collection in perl in following example :

#!/bin/env perl use v5.14;  package mytestmodule { sub foo {     $fh = shift;     for(1..100){         "put";         $fh->autoflush(1);         print $fh "heloo\n";         sleep 1;     } } }   package main;  use anyevent; use anyevent::fork::template;  $cv = anyevent->condvar; $anyevent::fork::template->fork->run("mytestmodule::foo", sub {     $fh_fh_fh = shift;     $w_w_w;     $w_w_w = anyevent->io(fh => $fh_fh_fh, poll => "r", cb => sub {            $w_w_w unless 1;            sysread $fh_fh_fh, $rslt, 10;            "got:", $rslt;             }         );    }); $cv->wait; 

in above code, if remove $w_w_w, because of anyevent->io create object, reference become zero, reclaimed perl, make code not work ( cb not called );

... $anyevent::fork::template->fork->run("mytestmodule::foo", sub {     $fh_fh_fh = shift;      anyevent->io(fh => $fh_fh_fh, poll => "r", cb => sub {             sysread $fh_fh_fh, $rslt, 10;             "got:", $rslt;              }         );    }); ... 

then assign scalar $w_w_w, dose not need $w_w_w in callback. following code not working(callback not called):

$anyevent::fork::template->fork->run("mytestmodule::foo", sub {     $fh_fh_fh = shift;     $w_w_w;     $w_w_w = anyevent->io(fh => $fh_fh_fh, poll => "r", cb => sub {         sysread $fh_fh_fh, $rslt, 10;             "got:", $rslt;         }     );   }); 

so add 1 line code in callback(just can see in front of requestion) :

$w_w_w unless 1;

because assertion false, not ran, think perl compiler/interpreter optimize line code ( drop ), $w_w_w 's reference count zero. , callback not called also. works. hope can explain .(the following dump of perl -odeparse

perl -mo=deparse t2.pl

sub begin {     require v5.14; } package mytestmodule; sub foo {     use strict;     no feature;     use feature ':5.12';     $fh = shift();     foreach $_ (1 .. 100) {         'put';         $fh->autoflush(1);         print $fh "heloo\n";         sleep 1;     } } package main; use strict; no feature; use feature ':5.12'; {;}; use anyevent; use anyevent::fork::template; $cv = 'anyevent'->condvar; $anyevent::fork::template->fork->run('mytestmodule::foo', sub {     $fh_fh_fh = shift();     $w_w_w;     $w_w_w = 'anyevent'->io('fh', $fh_fh_fh, 'poll', 'r', 'cb', sub {         '???';         sysread $fh_fh_fh, $rslt, 10;         'got:', $rslt;     }     ); } ); $cv->wait; 

i found answer in perlmonks.org : http://perlmonks.org/?node_id=1047188


Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

css - Firefox for ubuntu renders wrong colors -