in reply to file::find subroutine not getting new values

Because you're declaring the wanted sub inside an enclosing block containing lexical (my) variables, those variables create closures, and when the sub uses these variables, it gets the copies that were in existence when it was first compiled. The next time through the loop, a new variable is created, and the variable in the sub and outside it no longer refer to the same thing.

The easiest fix is to not use lexical variables. Declare $currentstoryid and $exists with our instead of my, and the problem goes away. Another solution is to create a new anonymous sub for each call to find:

my $currentstoryid = $entry; my $exists=0; &find( sub { if (/$currentstoryid/) { $exists = 1; return $exists; } }, $sitedir);

Replies are listed 'Best First'.
Re: Re: file::find subroutine not getting new values
by ilovechristy (Novice) on May 18, 2004 at 18:45 UTC
    Took your anonymous sub advice and it works like a charm. Thanks for everything. -ILC
    Buddha bless ya'll.