[OCLUG-devel] Perl and Recursion

James Colannino james at colannino.org
Mon Aug 15 12:07:45 PDT 2005


Hey everyone.  Here's a Perl question for you guys.  Here's some pseudo 
code to show what I've been working on (it's going backward recursively 
through a list of directories looking for the previous version of a 
specified package - note that the function below, except for the first 
two variables, is private to another function which is why I wrote it in 
this way):

    #These have scope outside of the private function below
    my @dirs2check;
    my @dirs2check_elements;

    $look_for_rpm = sub {

        #If it wasn't found and we've reached the end of directories we 
can search,
        #we must return 0 to indicate our failure to find the package.

        if ($dirs2check_elements <= -1) {return 0;}

        while (files exist in $dirs2check[$dirs2check_elements]) {

            examine file;

            if match found, {
                install older version of package;
                return 1;
            }

            else {
                --$dirs2check_elements;
                &$look_for_rpm();
            }
        }
    };

My problem here is that if a package is found, 1 is only returned to the 
previous instance of &$look_for_rpm() and is not returned to where I 
want it to go (past all the recursion and to the function that 
&$look_for_rpm() is private to.)  If there is an error, than 0 is also 
not returned to where I want it to go.  I want to find a way for either 
1 or 0 to be returned outside of the recursion that's taking place so I 
can tell whether or not a file was found.  I should probably be able to 
figure this out, and I'm sure the answer is a stupid one, but I can't 
for the life of me figure this out.  Thanks in advance.

James


More information about the OCLUG-devel mailing list