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

Hello Monks, I'm attempting to use File::Find to generate formatted output of a directory structure, and it appears that scoping issues are killing me. I'm unclear on what to do with this. What I'm doing is something like this (rather snipped up to limit this to the relevent parts):
sub a_moduser { our $user = shift; our $html; [...] $html = '<form action="' . $$config{selfpath} . 'method="post +">'; find(\&wanted, $$config{base_dir}); [...] } sub wanted { [...] $html .= '<td><input type="radio" name="base_dir" value="' . $_ . +'"></td>'; [...] }
So, $user and $html are both apparently out of scope:
Variable "$html" is not imported at fileman.cgi line 707. Variable "$html" is not imported at fileman.cgi line 708. Global symbol "$html" requires explicit package name at fileman.cgi li +ne 707. Global symbol "$html" requires explicit package name at fileman.cgi li +ne 708.
I tried sticking the variables and the find call in a BEGIN block, which shut the error messages up, but caused all sorts of grief with calling other subroutines. I'm a little lost in terms of what to do from here. Any kindly monks with suggestions? Thanks much.

Replies are listed 'Best First'.
Re: Scoping and File::Find
by borisz (Canon) on Jul 19, 2004 at 21:37 UTC
    You can put your our $html; outside of the sub a_moduser. or include the sub wanted in a_moduser and prefix $user and $html with my.
    sub a_moduser { my $user = shift; my $html; [...] $html = '<form action="' . $$config{selfpath} . 'method="post +">'; find( sub { [...] $html .= '<td><input type="radio" name="base_dir" value="' +. $_ . '"></td>'; [...] }, $$config{base_dir}); [...] }
    Boris
      Ah, that was my problem. I've been staring at this for too long... Thank you.