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?

4
  • 1
    Yes. You need to learn XS, take a look at perlxstut: 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) Commented Nov 26, 2013 at 12:38
  • so you have external compiled binary or you want to use C inside perl? Commented Nov 26, 2013 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. Commented Nov 26, 2013 at 12:42
  • 3
    how about argv? Commented Nov 26, 2013 at 12:47

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.