buttroast has asked for the wisdom of the Perl Monks concerning the following question:
Hi, I am somewhat of a novice, but I am writing a module to access a GEDCOM XML 6.0 file.
# XML::Twig::Gedcom # Copyright (C) 2004 Jeremy J. Biros # Perl module for processing a GEDCOM XML 6.0 file # package XML::Twig::Gedcom; use strict; use warnings; use XML::Twig; BEGIN { use Exporter (); our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS); # set the version for version checking $VERSION = 1.00; @ISA = qw(Exporter); @EXPORT = qw(&given_name &setup &new); %EXPORT_TAGS = ( DEFAULT => [qw(&given_name &setup &new)], Both => [qw(&given_name)]); # your exported package globals go here, # as well as any optionally exported functions @EXPORT_OK = qw(first_name setup new); } our @EXPORT_OK; # exported package globals go here # non-exported package globals go here our $individual; # initialize package globals, first exported ones # then the others (which are still accessible as $Some::Module::stuff) # all file-scoped lexicals must be created before # the functions below that use them. # file-private lexicals go here # here's a file-private function as a closure, # callable as &$priv_func; it cannot be prototyped. #my $priv_func = sub { # stuff goes here. #}; # make all your functions, whether exported or not; # remember to put something interesting in the {} stubs sub new { my ($package) = @_; my %hash = (GIVEN_NAME => "Jeremy", SURNAME => "Biros", MIDDLE_NAME => "John", SEX => "M"); bless \%hash => $package; $self = @_; } # Returns the namepart with level 3 sub given_name { my ($self) = @_; return $self->{GIVEN_NAME}; } # Returns the namepart with level 1 sub surname { my ($self) = @_; return $self->{SURNAME}; } # Returns the namepart with level 4 sub middle_name { my ($self) = @_; return $self->{MIDDLE_NAME}; } # Returns the individual's sex sub sex { my ($self) = @_; return $self->{SEX}; } # Gets the individual's <IndividualRec> # calls load_individual to process it sub setup { my ($individual_id) = @_; # Get the XML for the specified individual my $t = XML::Twig->new( twig_roots => { "IndividualRec[\@Id=\"$individual_id\"]" = +> \&load_individual } ); $t->parsefile( 'gedcom.xml'); # parse the individual's node $t->purge; # free up the memory } # Processes the individual and stores all values sub load_individual{ my( $t, $individual)= @_; # arguments for all twig_handlers ##################### # INDIVIDUAL'S NAME # ##################### # only allows for one name, DTD allows for more than one my $full_name = $individual->first_child( 'IndivName' ); my @nameparts = $full_name->children( 'NamePart' ); foreach my $namepart (@nameparts){ my $level = $namepart->{'att'}->{'Level'}; # surname? if ($level eq '1'){ my $surname = $namepart->text; $self->{SURNAME} = $surname; } else{ # given name? if ($level eq '3'){ my $given_name = $namepart->text; $self->{GIVEN_NAME} = $given_name; } } } #################### # INDIVIDUAL'S SEX # #################### $self->{SEX} = $individual->first_child( 'Gender' )->text; # free the memory $t->purge; } END { } ## YOUR CODE GOES HERE 1; # don't forget to return a true value from the file
My problem is, when I create my twig handler in the "setup" function, I don't know how to pass a reference to the object to the load_individual function, so I can set hash values in it. Any help with this question or any comments or critiques would be much appreciated.
Edited by Chady -- copied code from scratchpad into the post.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Help with XML::Twig::Gedcom
by mirod (Canon) on Oct 18, 2004 at 14:53 UTC | |
by buttroast (Scribe) on Oct 18, 2004 at 16:36 UTC | |
by mirod (Canon) on Oct 18, 2004 at 16:51 UTC | |
by buttroast (Scribe) on Oct 19, 2004 at 01:07 UTC |