Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Newbie help needed

7 views
Skip to first unread message

Alan Cohen

unread,
Apr 10, 2014, 9:30:25 AM4/10/14
to Gedcom List
I am a Gedcom newbie (also an OO newbie) challenged to get a most basic program using Gedcom.pm to work. Having opened a gedcom file, I can access a Gedcom::Individual and can retrieve scalar attributes from him. However, I fail when it comes to using methods that supposedly return an array. Instead I get things like "Gedcom::Individual=HASH(0x123456789)"

Can someone please tell me where I'm going wrong in (say) the following example?

#!/bin/perl -w

use warnings;
use strict;

use Gedcom;

my $ged = Gedcom->new(grammar_version => "5.5",
                      gedcom_file     => shift,
                      read_only       => 1);
my $indi="I187";
my $i;
my @array;
my $x;
    $i = $ged->get_individual($indi);
####
# These work...
####
    $x = $i->name;           { print("       name = $x\n"); }
    $x = $i->given_names;    { print("given_names = $x\n"); }
    $x = $i->surname;        { print("    surname = $x\n"); }
    $x = $i->get_value("birth date");{print(" birth date = $x\n"); }
####
# The documentation suggests that many methods return arrays but all of
# them seem to produce "values" such as
#     "Gedcom::Individual=HASH(0x2ab6c30)"
# (I looked at Individual.pm itself and "sub siblings" does seem to be
#  returning an array.
####                    WHAT AM I DOING WRONG!
    @array = $i->siblings();
    for $x (@array)            { print("Sibling $x\n"); }

Sincerely,
  Alan Cohen
  416-783-5383

Brent J. Nordquist

unread,
Apr 10, 2014, 9:51:17 AM4/10/14
to Alan Cohen, Gedcom List
You're correct, siblings is returning an array -- the question is, an
array of what? The answer is, an array of Gedcom::Individual objects --
in other words, $x in your loop is just like $i above, you can call the
name, surname, etc. methods on it.

Try this to see it:

for $x (@array) { print("Sibling (class ".ref($x).") = ".$x->name."\n"); }

Whenever you see "HASH(0x2ab6c30)" or "ARRAY(0xdeadbeef)" in your print
output, that's telling you you're trying to print an object as if it was
a string. ref() is a simple way to find out what kind of object you
have, and Data::Dumper is a simple way to print the object out for
debugging purposes ... just note that the Gedcom classes create objects
that are pretty big (deeply nested).
--
Brent J. Nordquist <br...@nordist.net>
Other contact information: http://www.nordist.net/~bjn/contact.html

Stephen Woodbridge

unread,
Apr 10, 2014, 10:52:12 AM4/10/14
to perl-...@perl.org
Another very useful tool is:

use Data::Dumper;
...
print Data::Dumper->Dump([$i->siblings()], ['siblings']);

This will dump out arbitrary objects in a formated structure so you can
understand ho they are constructed. Then use that info to access the sub
components of that structure.

-Steve

Hunter Johnson

unread,
Apr 10, 2014, 9:45:21 AM4/10/14
to Alan Cohen, Gedcom List
Perhaps something like

@array = $i->siblings;
for $x (@array) {print "Sibling ", $x->name, "\n";

?

--
J. Hunter Johnson <><
http://jhunterj.com
0 new messages