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

Re: Pattern match not working sometimes

by bulk88 (Priest)
on Mar 18, 2012 at 21:15 UTC ( [id://960328]=note: print w/replies, xml ) Need Help??


in reply to Pattern match not working sometimes

I think substr would be better for you case, since your just wanted to read 4 bytes. Also a less ideal alternative is unpack. shouldnt binmode be a method call not sub call?
  • Comment on Re: Pattern match not working sometimes

Replies are listed 'Best First'.
Re^2: Pattern match not working sometimes
by GrandFather (Saint) on Mar 18, 2012 at 21:49 UTC
    "I think substr would be better for you case"

    Actually, no. The OP stressed that he is dealing with octets, not characters. unpack could be a reasonable option, but probably not as clear as the regex unless the maintenance programmer is familiar with pack/unpack. The unpack code would (using the OP's sample data) look like:

    my $stuff = '000010100101110001010110010010010100111100111111001001000 +1110110'; my $bytes = pack('B64', $stuff); my ($prefix, $tail) = unpack('a4a*', $bytes); print ">$prefix<\n>$tail<\n";
    True laziness is hard work
      The OP is not introducing unicode or mentioning his locale anywhere in his code. The scalars coming from the OP's socket will have byte semantics. Why would any scalars be upgraded to unicode in his code? OP claims his length() return is the number of bytes in $datagram. $datagram isnt utf marked. He didn't say he is using -C.

        unpack makes it fairly clear that the code is dealing with octet (byte) oriented data without need for any further context. substr implies string handling with the possibility for utf/other encoding confusion. It's not that substr is flat out wrong in the context, just that it doesn't send as clear a message as unpack or the use of \C in a regex.

        True laziness is hard work

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (2)
As of 2024-04-26 00:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found