Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Subroutine variables problem

by Michalis (Pilgrim)
on Oct 15, 2002 at 08:17 UTC ( [id://205295]=perlquestion: print w/replies, xml ) Need Help??

Michalis has asked for the wisdom of the Perl Monks concerning the following question:

Hi all,
I have a problem that I don't think I can solve (I don't understand what's wrong), so I decided to ask for your help. I have the following piece of code (which uses an open connection from Mail::Cclient with parameters the connection -$imap- and the foldername -$fname-) :
sub foldertree($$) { my ($imap,$fname) = @_; my $count=0; my $new=0; my $unread=0; my @folders=(); set_callback( list => sub { my @tmp = split ('}',$_[2]); push (@folders, $tmp[1]); }, status => sub { $count = $_[3]; $new = $_[5]; $unread = $_[7]; } ); $imap->list("{hostname:143/imap}$fname",'%'); my @topfolders = @folders; my @folderlist; foreach my $f (sort @topfolders) { $imap->status("{hostname:143/imap}$f", "messages","recent","unseen"); my %foldhash= ( 'name' => $f, 'count' => $count, 'new' => $new, 'unread' => $unread ); my @subfolders=&foldertree($imap,$f); my $fc=@subfolders; $foldhash{'fc'} = $fc; $foldhash{'subfolders'} = @subfolders; @folderlist = (@folderlist, \%foldhash); } return @folderlist; }
Later, when I iterrate the @folderlist, I notice that all the count, new and unread values are set to zero (0) but the numbers are not correct... If, though, I skip the stage where I recursively call the sub in order to get the subfolders, it works like a charm (but I don't have the subfolder info which I need). I also tried modifying the sub as follows (only appropriate part provided):
foreach my $f (sort @topfolders) { $imap->status("{hostname:143/imap}$f", "messages","recent","unseen"); my $new_count=$count; my $new_new=$new; my $new_unread=$unread; my %foldhash= ( 'name' => $f, 'count' => $new_count, 'new' => $new_new, 'unread' => $new_unread ); my @subfolders=&foldertree($imap,$f); my $fc=@subfolders; $foldhash{'fc'} = $fc; $foldhash{'subfolders'} = @subfolders;
but it didn't make any difference. Any good ideas? I think something is wrong with the scope of the variables, but if that's the case, then why does the second example have problem as well? Thanks in advance

Replies are listed 'Best First'.
Re: Subroutine variables problem
by dws (Chancellor) on Oct 15, 2002 at 08:50 UTC
    Is there any chance that set_callback() hangs on to the first callbacks you pass for 'list' and 'status', and ignores subsequent attempts to set them? If so, those callbacks will reference variables in the stack frame of the first invocation of foldertree() and not the variables in the stack frames of recursive invocations. That is, you'd get the right number of references in the array, but the values in the referenced hashes would be bad.

    You could verify this by adding a print to the 'status' callback, and then dump %folderhash right before you recurse. If the numbers don't match, then you'll need to make the callbacks reference package variables instead.

      I would be lying if I said that I had thought of that (or even that I understand the rationale behind that), but you are right. I did the tests you suggested and looked like there was a problem. After that I declared the variables global in the package (before the sub) and now it's working as expected (I Think :)
      Thanks for your valuable help.
      --
      Michalis

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://205295]
Approved by broquaint
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2024-04-25 07:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found