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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |