Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Answer: How does Perl handle strings with embedded NULLs?

( #272922=categorized answer: print w/ replies, xml ) Need Help??

Q&A > strings > 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.

Comment on Answer: How does Perl handle strings with embedded NULLs?
Select or Download Code
Log In?

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (7)
As of 2015-11-27 22:48 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (734 votes), past polls