in reply to Managing C structs -with Perl-

Hello. I know of two modules which are capable of doing this. One is C::DynaLoad::Struct and the other is C::Include. You could probably do it using C::Inline as well. C::Include is perl based, but requires Storable.pm for caching (I find this kinda odd). It however, is your best bet. If you wish to do this without installing modules ( a silly requirement), you can probably rip off C::Include real easy (I would at least strip the Storable caching stuff). All of these have a lot of documentation. Knowledge of c is required. I tried out C::Include, and all the samples included work. Good luck (btw - I am looking into the future, and I see a tutorial on one of these, or at least a meditation on how you dealt with it - and code, always code ;D)

update:

#!/usr/bin/perl -w use C::Include; use strict; use vars qw/$inc $send $buffer/; $inc = new C::Include( \*DATA ); # Make struct instance $send = $inc->make_struct('send'); use Data::Dumper; print Dumper $send; # Fill struct fields $$send{prevpass} = '0123456789'; $$send{answer} = 1; # Pack struct to buffer $buffer = $send->pack(); # Struct size printf "Size of struct send: %d bytes\n", $send->size; printf "Size of unsigned long: %d bytes\n", $inc->sizeof('unsigned lon +g'); # Print buffer to STDOUT &hview( \$buffer ); #require 'hview.pl'; ## crazyinsomniac's note ~ hview.pl is distributed with the module, pr +ops to the author sub hview(\$){ my $str = shift; my @lines = unpack 'a16'x(length($$str)/16+(length($$str)%16?1:0)) +, $$str; for( 0..$#lines ){ printf "%08X: %-17sł %-17s %-16s\n", $_*16, (map{ sprintf '%-2s ' x 8, map{ sprintf'%02X',$_ }unpack 'C*', $_ }unpack 'a8a8', $lines[$_]), $lines[$_]; } } 1; __END__ #define PORT 2345 #define CHALLENGE 0 #define CORRECT 1 #define WRONG 2 struct recv { int flag; int b[4]; char nextpass[10]; }; typedef struct recv t_recv; struct send { int answer; char prevpass[10]; }; typedef struct send t_send;
I tried the above, and got
$>perl test4.pl $VAR1 = bless( { '' => { 'length' => 14, 'mask' => 'iZ10', 'tag' => 'send', 'buffers' => [ \undef, \undef ] }, 'prevpass' => ${$VAR1->{''}{'buffers'}[1]}, 'answer' => ${$VAR1->{''}{'buffers'}[0]} }, 'C::Include::Struct' ); Size of struct send: 14 bytes Size of unsigned long: 4 bytes 00000000: 01 00 00 00 30 31 32 33 ¦ 34 35 36 37 38 00  012 +345678 $>
That seems pretty cool.
** Note: I am using C::Include $VERSION = 1.40;

I tried $VERSION = 1.20;, but It didn't like your macro (WARNING: Unashed code ...) so I upgraded ... and all this after plenty of rum and coke (and some Jose Quervo tequila to boot)

 
______crazyinsomniac_____________________________
Of all the things I've lost, I miss my mind the most.
perl -e "$q=$_;map({chr unpack qq;H*;,$_}split(q;;,q*H*));print;$q/$q;"