CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Using C structures in Perl

 



katomich
New User

Jun 6, 2009, 3:53 PM

Post #1 of 3 (575 views)
Using C structures in Perl Can't Post

I was wondering if it is possible to use arrays created in a C file and then call them in Perl using backticks? Something like:
@random1, @random2 = `./test.exe`;
In this case, I want to store the the two arrays made with the C executable into two separate arrays in Perl. I haven't had much programming experience in C and I know this is a Perl forum, but perhaps someone can help me out with the C code?

This is what I have so far:

/* Driver for ran2 */
main() {
int m;
long idum1=(-1);
long idum2=(-2);
float rand1[3];
float rand2[3];
for(m=0; m<3; m++){
rand1[m]=ran2(&idum1);
}
for(m=0; m<3; m++){
rand2[m]=ran2(&idum2);
}
return rand1, rand2;
}

I need to use pointers and maybe malloc() but I am confused as to how to use them.
** ran2 is a working subroutine that takes idum as a parameter
** I need the two for-loops in this case


FishMonger
Veteran / Moderator

Jun 6, 2009, 4:29 PM

Post #2 of 3 (571 views)
Re: [katomich] Using C structures in Perl [In reply to] Can't Post

I don't know C, but I do know that C arrays are not the same as Perl arrays.

Perl doesn't allow you to assign 2 arrays in that manor, but it might work if you use references.

try:

Code
my ($random1, $random2) = `./test.exe`; 

foreach my $ran1 ( @$random1 ) {
print "random1 number: $ran1\n";
}

foreach my $ran2 ( @$random2 ) {
print "random2 number: $ran2\n";
}


Another possible option would be to use Inline::C
http://search.cpan.org/search?m=all&q=inline&s=1


raxip
Novice

Jun 11, 2009, 11:13 PM

Post #3 of 3 (535 views)
Re: [katomich] Using C structures in Perl [In reply to] Can't Post

If you're attempting to convert something used by C, into something that Perl can use, then you need to learn more about Perl's, pack and unpack.

The important part to remember is that C doesn't really care what kind of data you're dealing with. For all intent and purposes, it's just a chunk of data in memory that has an offset and an associated length (which depends on how that value is cast).

So, in the case of an array of say, 4 integers on an x86 machine. You need to know the following:
1) What endian is the data written in (for numeric values)
2) How many bytes of data are you trying to read (e.g. 16 bytes, 4 bytes at a time).
3) At what byte is each component (e.g. offset 4 byte, length 4 bytes)

Since you stated that you're not familiar with C or Perl, you have a lot of reading to do. Start with the following:

- http://en.wikipedia.org/wiki/Endianness
- perldoc -f pack, perldoc -f unpack
- http://perl.active-venture.com/pod/perlpacktut-packing.html
- http://www.google.com/#q=site%3Asearch.cpan.org+struct+c

Without you being more specific, this is the best I can provide.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives