Jan Eden wrote: > Hi all, > > 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).
Hi Jan, I'm afraid that this represents a basic misunderstanding of the purpose of subroutines. Subroutines and other containment tools protect and simplify problems by reducing the number of variables that may aggect the outcome. If they freely had access to each other's data, there could be no such assurance. Your code as posted show at least two of the three essential front doors for moving data in and out of subroutines. I havent looked closely enough yet to see if any of your arguments are references--that would be a third. Your function both take in arguments or agument lists through the @_ array. This is the appropriate way to get values into a function. The first one also returns a value, the classic means for getting information out of "returned from" an array If you need to affect some object or variable from within the function, you can pass a reference as an argument, and access the original through the reference. The power of subroutines comes from requiring you to explicitly decide, and thus be aware of, what information is being passed i and out, and why. > 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. perldoc perlref perldoc perlreftut Would be good places to start. > > > Can anyone point me to the (probably obvious) problem with the locally scoped > variables? It is not a problem. It is the most powerful tool that we, as programmers, have in making complex problems manageable. The problems arise when variables can be accessed, and corrupted by any code anywhere in a program. > > > Thanks, > > Jan Joseph -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>