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


In reply to Re: Perl Strings != C strings? by davido
in thread Perl Strings != C strings? by Mr. Newb

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.