Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Re: Porting a C/C++ Structure to Perl

by tobyink (Abbot)
on Jan 17, 2013 at 20:18 UTC ( #1013879=note: print w/replies, xml ) Need Help??

in reply to Porting a C/C++ Structure to Perl

It can be done that way. The trouble is that hashes don't offer any validation. You could mis-spell it as $data{vaule} somewhere leading to errors which can be hard to detect.

An object would be more appropriate. $data->vaule will throw an exception if the vaule method does not exist.

Creating full classes for each struct you need may seem like a lot of work, but there are various CPAN modules to make it easier for you, such as Class::Struct and MooseX::Struct. Here's an example using MooX::Struct (which I am the author of):

use MooX::Struct Point => [ 'x', 'y' ], Point3D => [ -extends => ['Point'], 'z' ], ; my $origin = Point3D[ 0, 0, 0 ];

PS: if you do wish to continue the hash route, check out Hash::Util's lock_keys function which can be used to restrict which keys a hash can take, reducing the risks of typos.

perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1013879]
[choroba]: Will Glasgow have been out of the EU by that time already?
[LanX]: nope
hippo might venture to YAPC::EU.
[choroba]: YAPC::YAEC (yet another European country)
[LanX]: they are proposing an infinite transition phase now ...
[choroba]: infinite transition implies infinite money!
[LanX]: ... ehm ..."open ended" ;-)

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (7)
As of 2018-02-23 10:57 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (301 votes). Check out past polls.