"be consistent" | |
PerlMonks |
comment on |
( [id://3333]=superdoc: print w/replies, xml ) | Need Help?? |
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):
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'
In reply to Re: Porting a C/C++ Structure to Perl
by tobyink
|
|