Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

I have a little confuse about the variable garbage collection in perl in following example :

#!/bin/env perl use v5.14; package MyTestModule { sub foo { my $fh = shift; for(1..100){ say "PUT"; $fh->autoflush(1); print $fh "Heloo\n"; sleep 1; } } } package main; use AnyEvent; use AnyEvent::Fork::Template; my $cv = AnyEvent->condvar; $AnyEvent::Fork::Template->fork->run("MyTestModule::foo", sub { my $fh_fh_fh = shift; my $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, my $rslt, 10; say "GOT:", $rslt; } ); }); $cv->wait;

In above code, if I remove the $w_w_w, then because of the AnyEvent->io create a object, its reference become zero, then it will be reclaimed by perl, which make the code not work ( the cb will not be called );

... $AnyEvent::Fork::Template->fork->run("MyTestModule::foo", sub { my $fh_fh_fh = shift; AnyEvent->io(fh => $fh_fh_fh, poll => "r", cb => sub { sysread $fh_fh_fh, my $rslt, 10; say "GOT:", $rslt; } ); }); ...

then I assign it to scalar $w_w_w, but I dose not need the $w_w_w in the callback. but the following code not working(callback will not be called):

$AnyEvent::Fork::Template->fork->run("MyTestModule::foo", sub { my $fh_fh_fh = shift; my $w_w_w; $w_w_w = AnyEvent->io(fh => $fh_fh_fh, poll => "r", cb => sub { sysread $fh_fh_fh, my $rslt, 10; say "GOT:", $rslt; } ); });

so I add one line code in the callback(just you can see it in the front of this requestion) :

$w_w_w unless 1;
because the assertion will always be false, so it will not ran, I think the perl compiler/interpreter will optimize this line code ( drop it ), then the $w_w_w 's reference count will be zero. and the callback will not be called also.

but it works. so I hope someone can explain this .(the following is a 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'; my $fh = shift(); foreach $_ (1 .. 100) { say '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; my $cv = 'AnyEvent'->condvar; $AnyEvent::Fork::Template->fork->run('MyTestModule::foo', sub { my $fh_fh_fh = shift(); my $w_w_w; $w_w_w = 'AnyEvent'->io('fh', $fh_fh_fh, 'poll', 'r', 'cb', sub { '???'; sysread $fh_fh_fh, my $rslt, 10; say 'GOT:', $rslt; } ); } ); $cv->wait;

In reply to Perl variable garbage collection by chinaxing

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    [GotToBTru]: un less you're working on write-only memory
    [1nickt]: my $x = $aohoaoh->[0]->{' foo'}->[0]->{'bar' }; should work
    [thepkd]: not indexing
    [thepkd]: getting data out of the ds
    [choroba]: it depends. Give more details, please
    [thepkd]: i used a series of {}'s but it dont work
    [choroba]: You need square brackets for arrays
    [GotToBTru]: you can certainly construct a single expression to access any part of the data structure, without using temp variables. but you might make it easier on yourself and any other poor soul who has to understand your code if you do
    [thepkd]: to dereference i mean
    LanX .oO( oh tempz, oh moretz)

    How do I use this? | Other CB clients
    Other Users?
    Others having an uproarious good time at the Monastery: (14)
    As of 2016-12-06 13:21 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      On a regular basis, I'm most likely to spy upon:













      Results (104 votes). Check out past polls.