in reply to Mysteries of unpack("a", ...)

which indicates that I don't know what the floop unpack("a4") does.

substr($_, 0, 4)
or rather
substr(do { _utf8_off(my $internal = $_); $internal }, 0, 4)

Replies are listed 'Best First'.
Re^2: Mysteries of unpack("a", ...)
by BrowserUk (Patriarch) on Jan 03, 2009 at 06:45 UTC

    Or rather

    substr substr(do { _utf8_off(my $internal = $_); $internal }, 0, 4) . chr(0) x 4, 0, 4;

    How's that! (for out-pedanting the pedant! (Always assuming I got it right :)

    From perlfunc:pack (I'm too tired to fix up the link):

    a A string with arbitrary binary data, will be null padded.

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

      That's wrong.

      While pack (whose documentation you quoted) will add NULs, unpack doesn't.

      >perl -le"print length unpack 'a4', ''" 0

      If anything, unpack should do the opposite (remove trailing NULs), but it doesn't do that either.

      >perl -le"print length unpack 'a4', qq{\0\0\0\0}" 4