On Jan 28, Jan Eden said:

>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!

The only way that hash can get populated outside of this function is if it
you pass it by reference to another function, and populate it THERE.  A
my() variable is scoped lexically -- that is, physically.  It only exists
here, inside this block of code, unless you send its reference to another
scope.

>    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]

You're not filling the same hashes and arrays; you're filling GLOBAL
hashes and arrays that happen to have the same name.  If you were using
'strict', you'd be told about that.

>    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).

It's not, because it exists ONLY in the read_index() function.

>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.

What warnings about undeclared variables are you expecting?  Unless you
have 'strict' on, Perl doesn't stop you from saying, out of the blue,

  push @foo, 'this';

>Can anyone point me to the (probably obvious) problem with the locally
>scoped variables?

One way you can fix your situation is to use a closure -- an anonymous
code block created inside read_index():

  sub read_index {
    my $gedicht_path = shift;
    my (%author_indexlines, %author_headlines, %gedicht_lines,
        @author_letters, @gedicht_letters, @alphabet_index);

    my $read_poem = sub {  # notice, no NAME for the sub
      # 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/##;

      # do stuff with hashes and arrays
    };

    find ($read_poem, $gedicht_path);
    print %author_indexlines; # now it'll work

    # return whatever
  }

-- 
Jeff "japhy" Pinyan      [EMAIL PROTECTED]      http://www.pobox.com/~japhy/
RPI Acacia brother #734   http://www.perlmonks.org/   http://www.cpan.org/
<stu> what does y/// stand for?  <tenderpuss> why, yansliterate of course.
[  I'm looking for programming work.  If you like my work, let me know.  ]


-- 
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