Mr. Newb has asked for the wisdom of the Perl Monks concerning the following question:

Hello people Mr. Newb here with yet another question on "how can I get Perl and C to interoperate?" I have decided to go with a FIFO/named pipe, but am running into a difficulty that I think arises because Perl stores strings differently than C (ie, not as a simple null-terminated array of characters). But that's just a hunch. Enlightenment?

Replies are listed 'Best First'.
Re: Perl Strings != C strings?
by davido (Cardinal) on Aug 24, 2012 at 21:58 UTC

    Unless you are dealing with Perl's internals (writing XS code, embedding a Perl interpreter, or other forms of semi-Perlish deep magic), it shouldn't matter how Perl stores strings internally. How Perl strings work at the perldata level (ie, at the language level rather than the internals level) is useful knowledge though. Perl strings are allowed to contain pretty much anything, where "anything" includes null characters ('\0').

    This flexibility turns out to be helpful. It means that if you are working with data where '\0' is a legitimate entity throughout the string (such as with binary data), Perl is Ok with that. And if you need for your strings to be '\0' terminated, Perl's Ok with that too. But what it won't do is mutilate your data by appending '\0' to it without your asking it to. So if you want your string to be passed through a pipe to a process that expects its input strings to be null terminated, you will need to append that '\0'.

    Internally Perl strings normally are null terminated, like C-Strings, as they need to be interoperable with some of C's string handling functions. But your Perl program never sees that trailing null. It's an internal representation, and a Perl user isn't exposed directly to that internal detail any more than they're exposed to how Perl automatically and quietly treats a number like a string or a string like a number (probably less, in fact). It's not exposed to the user, and consequently, when passing data from a Perl application through a pipe, you will only confuse yourself if you start thinking in terms of what a PV looks like internally.

    Think in terms of what you want your data to look like when you pass it through the pipe, and construct it to look like that. Fortunately Perl provides tools to simplify this process, such as pack, with its 'a' and 'Z' template metacharacters, and the "\0" literal.


    Dave

Re: Perl Strings != C strings?
by MidLifeXis (Monsignor) on Aug 24, 2012 at 18:59 UTC

    Define the format for your strings, and follow the format on both sides of the FIFO. In perl, \0 is the null character. Also see chr(0). I think that there may also be a way using pack to create a null terminated string.

    --MidLifeXis

      Of course; my format is the null-terminated string, because I'm familiar with it.
Re: Perl Strings != C strings?
by aitap (Curate) on Aug 24, 2012 at 19:43 UTC
    Do you mean XS code? If so, perlapi:

    newSVpv Creates a new SV and copies a string into it. The reference count for the SV is set to 1. If "len" is zero, Perl will compute the length using strlen(). For efficiency, consider using "newSVpvn" instead.
    SV* newSVpv(const char *const s, const STRLEN len)

    This creates a Perl scalar value from C string.
    Sorry if my advice was wrong.