Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

pack auto-chomps/chops?

by bv (Friar)
on Oct 07, 2009 at 18:30 UTC ( #799774=perlquestion: print w/replies, xml ) Need Help??

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

So this one bit me today, and I'm wondering if it's documented anywhere or if I just am missing something in my implementation. I have a sub that sorts some lines based on the IP address at the beginning of the line:

my $fp = \&by_ip; print $fp->(@input); sub by_ip { return map { unpack('x4A*') } sort map { pack('A4A*',inet_aton((split /\s/,$_,2)[0]),$_) } @_; }

The output is printed all on one line, which I do not want. I am sure that every line in the input ends with "\n", since this sub is swappable (via assignment to $fp) with several others that do not exhibit this behavior. The only unique thing about this sub is that it uses pack. Does anyone have a clue as to what is going on here? Many thanks.

print pack("A25",pack("V*",map{1919242272+$_}(34481450,-49737472,6228,0,-285028276,6979,-1380265972)))

Replies are listed 'Best First'.
Re: pack auto-chomps/chops?
by ikegami (Patriarch) on Oct 07, 2009 at 18:49 UTC

    pack:

    A: A text (ASCII) string, will be space padded

    Emphasis mine. If spaces are added by pack, they'll be removed by unpack. I guess it means \s when it says space. Fix:

    sub by_ip { return map substr($_, 4), sort map inet_aton( (split /\s/, $_, 2)[0] ) . $_, @_ }

      Ah, that's tricky. I suppose you're right, though. Do you know if there's a web-browsable version of the Perl source code anywhere I could check into that?

      By the way, your solution works but is slower than just appending a "\n" to the end during the final map.

      print pack("A25",pack("V*",map{1919242272+$_}(34481450,-49737472,6228,0,-285028276,6979,-1380265972)))

        your solution works but is slower than just appending a "\n" to the end during the final map

        If pack is faster — seems odd — you could use Z instead of A.

        Ah, that's tricky. I suppose you're right, though. Do you know if there's a web-browsable version of the Perl source code anywhere I could check into that?

        http://perl5.git.perl.org/perl.git Look for pp_unpack in pp*.c (probably pp.c specifically pp_pack.c).

        The relevant code:

        1458 for (ptr = s+len-1; ptr >= s; ptr--) 1459 if (*ptr != 0 && !isSPACE(*ptr)) break; 1460 ptr++;

        and

        #define isSPACE(c) \ ((c) == ' ' || (c) == '\t' || (c) == '\n' || (c) =='\r' || (c) + == '\f')
        aka \s

        Update: Added relevant code.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2023-12-11 13:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What's your preferred 'use VERSION' for new CPAN modules in 2023?











    Results (41 votes). Check out past polls.

    Notices?