Perl: Removing NULL characters (0x00)

Alan Young alansyoungiii at
Sat Mar 5 10:54:30 MST 2005

On Fri, 04 Mar 2005 17:36:50 -0700, Peter Bowen <peter at> wrote:
>  I suggested it because this is what pack/unpack was designed to do...
> extract data from streams.  In this case a null terminated string.

Seems to me a trivial usage of pack, and not as clear to follow up
programmers.  I'd automatically look for more than was there.  Plus,
he didn't say he wanted to get rid of *all* nulls, just at the end of
the string.  You're example would strip the nulls.

I'd be curious to see what kind of benchmarks we'd see ... hmmm.  The
regex is about 25% faster than the unpack.  See for my
logic and
for the results.
>  I've seen code where this is done with substrs to try to chop off the junk
> at the beginning and lop off the terminator at the end - because the
> programmer didn't realize that the "junk" was a couple of  ints to describe
> the type of message, and the length of the text, and the null was the string

That's not a problem with using the regex, that's a problem with
programmer education.

> to expect.  By unpacking and handling the data, we converted it to an event
> driven model that can Tivo radio in perl... it was pretty cool - it still is

Sounds like it.  Don't suppose you've made that code public?

More information about the PLUG mailing list