Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Help with XML::Twig::Gedcom

by buttroast (Scribe)
on Oct 17, 2004 at 16:48 UTC ( [id://399928]=perlquestion: print w/replies, xml ) Need Help??

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.

Thanks buttroast

Replies are listed 'Best First'.
Re: Help with XML::Twig::Gedcom
by mirod (Canon) on Oct 18, 2004 at 14:53 UTC

    It is very simple:

    my $t = XML::Twig->new( twig_roots => { "IndividualRec[\@Id=\"$individual_id\"]" = +> sub { load_individual( @_, \%hash); } } ); ... sub load_individual { my( $t, $individual, $hashref)= @_; ... }

    What you do here is create a closure, but you really don't have to know that! Achieving Closure has more explanations on what happens under the hood.

      Michael,

      I made the changes you mentioned, and now I get the following error:

      Can't modify string in scalar assignment at /home/tiffanya/public_html/cgi-bin/modules/XML/Twig/Gedcom.pm line 94, near "} }"

      Also, should the %hash variable have our scope and be used in place of all of my $self variables?

      Thanks buttroast

        I don't know what argument you are trying to pass to the handler, so I just put one at random, you will have to replace %hash by your own, there is nothing magic about it. It you need to pass an object, then pass it.

        Using our on the other end would probably defeat the purpose of passing the variable as a parameter.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://399928]
Approved by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (1)
As of 2024-04-25 19:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found