Re: Newbie help needed
[email protected] (Stephen Woodbridge) Thu, 10 Apr 2014 10:52:12 -0400
| Newsgroups | perl.gedcom |
|---|---|
| Message-ID | <[email protected]> |
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
On 4/10/2014 9:51 AM, Brent J. Nordquist wrote:
> 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).
>
> On Thu, Apr 10, 2014 at 09:30:25AM -0400, Alan Cohen wrote:
>> 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
>