• format a binary string as hex dump

    From Rainer Weikusat@21:1/5 to All on Thu May 6 18:55:38 2021
    Problem: Given a binary string of a principally indeterminate (max 8
    length bytes), turn that into a hex dump with 16 bytes per line (and
    possibly less on the last line.

    Solution:

    ---------
    sub format_bin
    {
    use bytes;
    my @lines;
    local *_ = \$_[0];

    push(@lines, join(' ', unpack('(H2)*', $1)))
    while /\G(.{1,16})/gs;

    return @lines;
    }
    ----------

    I'm especially happy that there's no need to special-case the last line
    as the /\G(.{1,16})/gs will match whatever is remains after the last
    complete 16-character-group.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Henry Law@21:1/5 to Rainer Weikusat on Wed May 12 08:59:25 2021
    On Thu, 06 May 2021 18:55:38 +0100, Rainer Weikusat wrote:

    local *_ = \$_[0];

    What witchcraft is that? I don't even know where to start looking it up.

    --
    Henry Law n e w s @ l a w s h o u s e . o r g
    Manchester, England

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Rainer Weikusat@21:1/5 to Henry Law on Wed May 12 15:10:57 2021
    Henry Law <news@lawshouse.org> writes:
    On Thu, 06 May 2021 18:55:38 +0100, Rainer Weikusat wrote:

    local *_ = \$_[0];

    What witchcraft is that? I don't even know where to start looking it up.

    It's documented in the "Symbol Tables" section of perlmod: *_ is the
    typeglob whose scalar slot is $_. $_[0] is the scalar passed as first
    argument (ie, at the implementation level, it's a SV *). Assigning a
    reference to that to a glob causes the scalar slot of this glob to refer
    to the scalar the reference came from: Afterwards (while the local is in
    scope) $_ 'means' 'the scalar passed as first argument': It now has a
    name but its contents weren't copied.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Kang-min Liu@21:1/5 to Rainer Weikusat on Thu May 13 11:08:34 2021
    Rainer Weikusat <rweikusat@talktalk.net> writes:

    # [1]
    local *_ = \$_[0];

    ... to the scalar the reference came from: Afterwards (while the local is in scope) $_ 'means' 'the scalar passed as first argument': It now has a
    name but its contents weren't copied.

    I wonder how it compares to this alternative:

    # [2]
    local $_ = $_[0];

    I guess [1] is aliasing and [2] is copying, although I'm not 100%
    confident about this (nor the exact meaning of aliasing.)

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Rainer Weikusat@21:1/5 to Kang-min Liu on Thu May 13 22:44:36 2021
    Kang-min Liu <gugod@gugod.org> writes:
    Rainer Weikusat <rweikusat@talktalk.net> writes:

    # [1]
    local *_ = \$_[0];

    ... to the scalar the reference came from: Afterwards (while the local is in >> scope) $_ 'means' 'the scalar passed as first argument': It now has a
    name but its contents weren't copied.

    I wonder how it compares to this alternative:

    # [2]
    local $_ = $_[0];

    I guess [1] is aliasing and [2] is copying, although I'm not 100%
    confident about this (nor the exact meaning of aliasing.)

    ----
    use Devel::Peek;

    $a = 'Hi';

    sub aa
    {
    local $_ = $_[0];
    Dump($_);
    }

    sub bb
    {
    local *_ = \$_[0];
    Dump($_);
    }

    Dump($a);
    aa($a);
    bb($a);
    -----

    For a sufficiently recent perl (tested on 5.24) the output will be


    SV = PV(0x556a0e4dfb20) at 0x556a0e4fc7c8
    REFCNT = 1
    FLAGS = (POK,IsCOW,pPOK)
    PV = 0x556a0e507250 "Hi"\0
    CUR = 2
    LEN = 10
    COW_REFCNT = 1
    SV = PV(0x556a0e4dfc00) at 0x556a0e4def48
    REFCNT = 1
    FLAGS = (POK,IsCOW,pPOK)
    PV = 0x556a0e507250 "Hi"\0
    CUR = 2
    LEN = 10
    COW_REFCNT = 2
    SV = PV(0x556a0e4dfb20) at 0x556a0e4fc7c8
    REFCNT = 2
    FLAGS = (POK,IsCOW,pPOK)
    PV = 0x556a0e507250 "Hi"\0
    CUR = 2
    LEN = 10
    COW_REFCNT = 1

    The aa subroutine creates a new scalar sharing the body of the other via copy-on-write, the bb routine uses the passed scalar directly.

    On older perls, aliasing in this way used to become faster than copying
    the string for long strings (>> 100 characters). This is apparently
    no longer the case when COW is supported.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Henry Law@21:1/5 to Rainer Weikusat on Fri May 14 16:07:02 2021
    On Wed, 12 May 2021 15:10:57 +0100, Rainer Weikusat wrote:

    It's documented in the "Symbol Tables" section of perlmod: *_ is the
    typeglob whose scalar slot is $_

    Thank you. I confess that it's one of the areas of Perl with which I am
    not familiar, so I should go and look at it.

    --
    Henry Law n e w s @ l a w s h o u s e . o r g
    Manchester, England

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)