Jan Eden wrote:
>
> the following structure does not work as intended:
>
> sub read_index {
>
>     my $gedicht_path = shift;
>     my (%author_indexlines, %author_headlines, %gedicht_lines, @author_letters, 
> @gedicht_letters, @alphabet_index);
>     find (\&read_poem, $gedicht_path);
>     print %author_indexlines; # nothing is printed out here!
>     return \(%author_indexlines, %author_headlines, %gedicht_lines, @author_letters, 
> @gedicht_letters, @alphabet_index);
> }
>
>
> sub read_poem {
>
>     # scalars to read from the poem page
>     my ($vorname, $nachname, $title, $namekey, $titlesort_string, $nameletter, 
> $titleletter, $page, $relative_path);
>
>     my $pathname = $File::Find::name;
>     return 0 unless $pathname =~ /\.html$/;
>
>     # definition of the file path relative to the $gedicht_path
>     $relative_path = $pathname;
>     $relative_path =~ s#$gedicht_path/##; # Perl complains about uninitialized value 
> here
>     [filling the hashes and arrays declared above]
>     print %author_indexlines; # the hash is printed correctly here
> }
>
> I expect the variable $gedicht_path to be accessible inside the read_poem
> subroutine, since it is called from within read_index, it is not (I get an
> 'uninitialized value' error).
>
> Second, I expect the hashes and arrays in declared in read_index to be filled
> in read_poem. read_poem does not complain about undeclared variables, and I
> can print the appropriate %author_indexlines etc. from within read_poem, but
> not from read_index, as intended.
>
> Can anyone point me to the (probably obvious) problem with the locally scoped
> variables?

Hi Jan.

Lexical ('my') variables are accessible only within the innermost block or
file in which they are declared. As the body of a subroutine is a block
things like your $gedicht_path are accessible only within &read_index.

You need to declare the list of hashes and arrays /before/ and /outside/
any subroutine that needs them. Like this:

  my (%author_indexlines, %author_headlines, ... );

  sub read_index {
      :
  }

  sub read_poem {
      :
  }

Also, think carefully about whether you need so much common data. I would
expect to see the sort of thing you're using contained within a single hash.
Without knowing more about what you're doing I can't help any further.

HTH,

Rob



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to