OK long story short I have a UDP listener using IO::Socket::INET that I am trying to grab the first 4 bytes from the data with a pattern match that is failing on roughly 1 out of 100 packets. First here is my simplified code:

#!/usr/bin/perl -w $|++; use strict; use IO::Socket; use IO::Select; my $response = IO::Socket::INET->new(Proto=>"udp",LocalPort=>5000) or die "Can't make UDP server: $@"; my $sel = new IO::Select( $response ); my ($datagram,$flags); binmode($response); while(my @ready = $sel->can_read) #Make sure socket has something to g +ive us { $datagram = ''; $flags = ''; $response->recv($datagram,1500,$flags); my $length = length($datagram); print "Length : $length\n"; if($datagram =~ /^(\C\C\C\C)(.*)$/s) { #Do some stuff } else { #Didn't match for some reason print "Did not match!\n"; } }

Here is the root of my question: I print out the length of the packet just prior to attempting the pattern match, which always has a length of "1205", but when I attempt the match with $datagram =~ /^(\C\C\C\C)(.*)$/s it sometimes fails. How can this match fail (which I believe is just looking for the first 4 bytes in the datagram) when the data is 1205 bytes long?

Oh, and here is a snipet of the output:

Length : 1205 Length : 1205 Length : 1205 Length : 1205 Length : 1205 Did not match! Length : 1205 Length : 1205

Any guidance would be greatly appreciated! :)


In reply to Pattern match not working sometimes by dantheman1210

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.