|
Hello, trying my first steps with this handy lib,
got the samples working,
but having a hard time to find how to loop through different records ...
see my test code here, what am I doing wrong ?
at decoding/reading it only seems to find the last defined record
Thx a lot in advance for any suggestions
#---------------------------------
use Google::ProtocolBuffers;
##### Read definitions
open (PR,"<Person.proto");
@pr=<PR>; $pr=join ' ',@pr;
close(PR);
#print "**$pr**\n";
##### Interprete definitions
Google::ProtocolBuffers->parse($pr,
{create_accessors => 1 },
{ generate_code => 'Foo.pm' }
);
##### Serialize Perl structure and print it to file
open (PRO, ">person.dat");
binmode PRO;
print PRO Person->encode({
name => 'A.U. Thor',
id => 123,
phone => [
{ number => 1234567890 },
{ number => 987654321, type=>Person::PhoneType::WORK() },
],
});
print PRO Person->encode({
name => 'JCM',
id => 456,
phone => [
{ number => 234567890 },
{ number => 87654321, type=>Person::PhoneType::WORK() },
],
});
close PRO;
#### Decode data from serialized form
my $person;
open (PRO, "<person.dat");
binmode PRO;
local $/;
$person = Person->decode(<PRO>);
#while($person)
{
print $person->name, "\n";
print $person->id, "\n";
print @{$person->phone}[0]->number, "\n";
#$person = Person->decode(<PRO>);
}
close PRO;
|