Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Unpack unexpectedly trimming trailing spaces

by stevieb (Canon)
on Nov 19, 2015 at 22:29 UTC ( [id://1148182]=note: print w/replies, xml ) Need Help??


in reply to Unpack unexpectedly trimming trailing spaces

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 <

Replies are listed 'Best First'.
Re^2: Unpack unexpectedly trimming trailing spaces
by talexb (Chancellor) on Nov 20, 2015 at 14:24 UTC

    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:  <%-{-{-{-<

        Even though unpack states to look to pack for further info, would it be a sensible use of time if I updated unpack to reflect at least info that's directly relevant? Thoughts?

        My thinking is that it might not be overly clear if someone uses unpack without using pack first.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1148182]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2024-04-19 15:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found