Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: How is perl able to handle the null byte?

by Joost (Canon)
on Jun 15, 2006 at 18:52 UTC ( #555581=note: print w/ replies, xml ) Need Help??


in reply to How is perl able to handle the null byte?

Yup, C treats a 0 byte as the end of a string. And yes perl is written in C. However, perl strings are not C strings.

In C a string is nothing more than a pointer to the first character of the string. C can't know how long a string is except by counting all the characters from the start until the first 0 byte. You need to know the length of a string in order to do things like copy or compare them.

Perl's strings are C structs that include (amongst other things) a pointer to the first character, AND the length of the string. Since perl doesn't need a special terminating character you can use 0 characters in the middle of a string too.

Note that internally perl strings are still 0-terminated in order to facilitate interoperation with functions that expect C style strings. Also note that sending strings with embedded 0 characters to system calls or XS libraries does not always work as you'd expect, though all the buildin string and IO operations (regexes, print etc) should work correctly.


Comment on Re: How is perl able to handle the null byte?
Re^2: How is perl able to handle the null byte?
by muba (Priest) on Jun 15, 2006 at 18:56 UTC
    A struct is basically like creating your own data type, right?

    However, I understand now. That was fairly simple, actually...
    Thank you!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://555581]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (6)
As of 2014-11-28 00:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (191 votes), past polls