talexb has asked for the wisdom of the Perl Monks concerning the following question:

I've been working on some ETL stuff, and was getting an unexpected result similar to Trimming unpack'ed strings.

Specifically, why does Perl trim trailing spaces when doing an unpack operation? A simple example follows:

@foo = unpack ( 'A6A4', 'Hello Joe ' )
I'm expecting this to set @foo to the following:
('Hello ','Joe ')
because the spaces help retain the length of the string. However, instead I'm getting
('Hello','Joe')
Why?

Alex / talexb / Toronto

Thanks PJ. We owe you so much. Groklaw -- RIP -- 2003 to 2013.

Replies are listed 'Best First'.
Re: Unpack unexpectedly trimming trailing spaces
by stevieb (Canon) on Nov 19, 2015 at 22:29 UTC

    From perldoc -f pack: "When unpacking, A strips trailing whitespace and nulls, Z strips everything after the first null, and a returns data with no stripping at all."

    For this purpose, you can use either a or Z. Because you're reading in ASCII text, a makes more sense visually as [Aa] is for ASCII text, and Z is used for null terminated ASCII (which you don't have here):

    perl -E '@foo=unpack("a6a4", "Hello Joe "); say ">$_<" for @foo' >Hello < >Joe <
    perl -E '@foo=unpack("Z6Z4", "Hello Joe "); say ">$_<" for @foo' >Hello < >Joe <

      Thanks -- I did look pretty carefully in the Camel, p. 800 and on, for this type of information, because it struck me as unusual that Perl would do this type of cleanup automatically. It's very handy, of course, but not what I expected. And, of course, the expected behavior is available with 'a' instead of 'A'.

      Yet another thing I've learned .. and another tricky interview question to stash away for future use (I'm looking at you, RD. :) ).

      Alex / talexb / Toronto

      Thanks PJ. We owe you so much. Groklaw -- RIP -- 2003 to 2013.

        ... I did look pretty carefully in the Camel ...

        As a general rule, I think it's best to look at the on-line pack documentation or, better yet, at your local perldoc -f pack (and see also perlpacktut).


        Give a man a fish:  <%-{-{-{-<