Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Porting a C/C++ Structure to Perl

by jmlynesjr (Hermit)
on Jan 17, 2013 at 20:09 UTC ( #1013877=perlquestion: print w/replies, xml ) Need Help??
jmlynesjr has asked for the wisdom of the Perl Monks concerning the following question:

In porting yet another C++ wxWidgets example to wxPerl, I had the need to translate a few simple C++ Structures to Perl. I used a hash. Thus C++ data.value becomes Perl $data{value}. Is this the generally accepted method?

Thanks,

James

There's never enough time to do it right, but always enough time to do it over...

Replies are listed 'Best First'.
Re: Porting a C/C++ Structure to Perl
by tobyink (Abbot) on Jan 17, 2013 at 20:18 UTC

    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'
Re: Porting a C/C++ Structure to Perl
by jmlynesjr (Hermit) on Jan 18, 2013 at 03:11 UTC

    Thanks, tobyink.

    I think I will post the working hash version first and then do the reading necessary to learn enough to implement your suggestion. Since this is a learning exercise, I think it would be better to code the next version "long-hand" before trying the MooX method.

    James

    There's never enough time to do it right, but always enough time to do it over...

        Thanks for the references, A.M.,

        I have the download version of Modern Perl as well as Learning Perl, Programming Perl, and the Perl Cookbook. I like docs on paper.

        James

        There's never enough time to do it right, but always enough time to do it over...

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1013877]
Approved by tobyink
Front-paged by Lotus1
help
Chatterbox?
[Lady_Aleena]: Or what Discipulus said.
[shmem]: Lady_Aleena: geany supports ctags.
[Discipulus]: a good compromise can be my ($need, $opts_ref) = @_ a scalar and an hash reference
[Discipulus]: see you monks!
[Lady_Aleena]: shmem, let me get this sub rewritten, then I will look into how to use ctags in geany. Deal? 8)
[shmem]: Discipulus: yeah, that might eventually prepare the path for OO ;-)
[Lady_Aleena]: See you, Discipulus.
[shmem]: Lady_Aleena: that's up to you. I only wanted to show you a path that might be more comforting than command line grep ;-)
[Lady_Aleena]: shmem, I don't think any of my modules could be converted to OO. They are too procedural.
[Lady_Aleena]: Now I will see if my perl-fu is as bad as I think it is.

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (8)
As of 2017-04-27 12:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I'm a fool:











    Results (506 votes). Check out past polls.