[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

[MacPerl] Scoping question



I have been confused by the following problem related to scope issues.  
The script I include is part of a larger program to analyze an XML data 
file and generate some HTML.  The specific task for this snippet is to 
parse the file and build an array of my "subject" attributes and count 
the unique values.   If I type up the script as follows, it works fine:

   # if we pass to here, the XML file is fine.  now parse to create a
   # list of subjects in the file.
   $nSubjects = 0;

   $p3 = new XML::Parser(Handlers => {Start => \&hStartCnt});
   $p3->parsefile('PBG3:Desktop Folder:photo_sites:RussiaPhotos.xml');

   # this sub will detect each unique subject attribute in 
   # the file and create a list of names. 

   sub hStartCnt
    {
      # code deleted to shorten e-mail; real function uses $nSubjects 
      # to count unique values of 'subject'
   }

   # provide some feedback:
   if($nSubjects == 0) {die ('Found no subjects')};

With my data file, it finds the correct 4 subject entries.  If, on the 
other hand, I move the initial declaration of $nSubjects to the line 
immediately preceding the subroutine definition:

   # the file and create a list of names. 
   $nSubjects = 0;
   sub hStartCnt
   (etc.)

the value of $nSubjects is _always_ zero at the my feedback line at the 
end of the script.  

Since in both cases $nSubjects is defined outside the block of the 
subroutine, it would seem that it should be treated as a global variable 
(this is my understanding after reading MPPE).  Why is this not true?  

Thanks,
Rick

===== Want to unsubscribe from this list?
===== Send mail with body "unsubscribe" to macperl-request@macperl.org