Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

How does Perl handle strings with embedded NULLs?

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


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!
  • 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, 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
  • 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.
  • Log In?

    What's my password?
    Create A New User
    [LanX]: yep heard the same origin story
    [Your Mother]: I like it. I tottered on going into hucksterism because I feel like the world deserves it.
    [LanX]: he ... we have a movement here called Anti-Germans based on this
    [Corion]: Your Mother: I think that's because (in the west) the Nazi-Germans are recognized as universally evil. Of course, you could do some number games to calculate other measures of evil than "historic losers of second world war" to come up with other evils:)
    [Corion]: I've heard "Troll" described as the new Punk, and in a way, it can be as destructive as living the Punk lifestyle, and you don't have to sit out in the cold...
    [LanX]: Anti-Germans
    [LanX]: Socrates was a Troll
    [Your Mother]: It's very, very dangerous... Thinking that a group is intrinsically evil... buries the fact that all humans can be so deep that it starts to become likely they will be.
    [Your Mother]: LanX++
    [Corion]: (also the "troll for trolls sake" could seen be much like the "punk for punks beer")

    How do I use this? | Other CB clients
    Other Users?
    Others cooling their heels in the Monastery: (17)
    As of 2018-03-19 13:43 GMT
    Find Nodes?
      Voting Booth?
      When I think of a mole I think of:

      Results (240 votes). Check out past polls.