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

In reply to Subroutine variables problem by Michalis

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, details, 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, summary, 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.