0

Can I transfer a array from perl to c without writhing anything to hdd? I could generate an array in perl, save it in a file and read in c. But is it possible to perform this operation in memory only?

MaMu
  • 1,837
  • 4
  • 20
  • 36
  • 1
    Yes. You need to learn `XS`, take a look at perlxstut: http://perldoc.perl.org/perlxstut.html - alternatively you could transfer the data via a separate process like `memcached` - provided you were happy to extend your C code to add the necessary client and format processing (in Perl this is easier) – Neil Slater Nov 26 '13 at 12:38
  • so you have external compiled binary or you want to use `C` inside `perl`? – mpapec Nov 26 '13 at 12:39
  • I'd prefer to have external binary. But I could also use c inside perl if it would be the only possibility. – MaMu Nov 26 '13 at 12:42
  • 3
    how about [argv](http://stackoverflow.com/questions/2300744/using-argv-in-c)? – mpapec Nov 26 '13 at 12:47

2 Answers2

5

You can use the Perl API to talk between Perl code and C, and this can all happen in memory (no writing out intermediate forms to disc).

The details of the various calls for handling arrays are avaialble from perl.org.

Remember though that the contents of the Perl array are Perl scalar values (SVs) not just ints or pointers as in a C array, so you'll have to use the Perl API to decode the data down to something you can handle in your C program.

Borodin
  • 126,100
  • 9
  • 70
  • 144
Jon Knight
  • 159
  • 9
5

This doesn't sound like a question about Perl or C at all, but a question about how to transfer information from one program to another ("I'd prefer to have external binary.") This is called inter-process communication (IPC).

If the Perl program launches the C program, you could pass the data using the argument list.

If the C program launches the Perl program, you create a pipe and tie it to the child's STDOUT. The Perl program would print the data to STDOUT, and the C program would read it from the pipe.

If neither of the two programs launched the other, a socket is the obvious solution, though there are many other possibilities.

ikegami
  • 367,544
  • 15
  • 269
  • 518