Beefy Boxes and Bandwidth Generously Provided by pair Networks Joe
There's more than one way to do things
 
PerlMonks  

How does Perl handle strings with embedded NULLs?

( #27635=categorized question: print w/ replies, xml ) Need Help??
Contributed by strredwolf on Aug 12, 2000 at 20:40 UTC
Q&A  > strings


Description:

I have a file (formmail's message-id cache file) that stores it's data as "<msgid>\000" for each entry.
If I slurp it all in, will Perl have a problem with it?

Answer: How does Perl handle strings with embedded NULLs?
contributed by Corion

There are places where embedded \000 characters will get you into trouble, but Perl can read and write such files without problems, since Perl uses counted strings.

Be warned though that passing such strings to anything like a system routine (like open() or -x), that take null-terminated strings as arguments, you will have problems or security holes.

Answer: How does Perl handle strings with embedded NULLs?
contributed by graff

If by "slurp it all in" you mean something like:

$/ = undef; $_ = <>;
then Perl will certainly have no trouble at all reading all the data and assigning it all to $_, no matter what it contains. It will print it all out to STDOUT or any file handle as well (but you may need to be careful if the file handle is actually a pipe to a less forgiving process).

Plain old line-based I/O will also treat nulls just like any other character that isn't (part of) a line terminatation -- lines containing nulls will be fully read and written.

Of course, sometimes it makes sense to use nulls as the input record separator:

# one way to replace nulls with newlines: $/ = "\x00"; while (<>) { s/\x00/\n/; print; }
In addition to matching nulls exactly with "\x00" in regexes, Perl also matches them via the "." wildcard, and the negated character classes "\S", "\D", "\W", and so on.

Please (register and) log in if you wish to add an answer



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others cooling their heels in the Monastery: (5)
    As of 2014-04-20 05:20 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      April first is:







      Results (485 votes), past polls