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

In reply to Help with XML::Twig::Gedcom by buttroast

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.