$msgCode = $msgCode.(" " x ($maxMsgCodeLength-length($msgCode)));

That code pads the string to a set width with spaces. The same could be achieved using sprintf or directly with pack using the 'A' format specifier, which pads a string to a set width with spaces. The 'a' specifier actually used in your code would pad with nulls (ASCII 0) but as the string has already been padded to the necessary width no nulls appear. The 'i' specifier will output a signed integer of at least 4 bytes width and the exact format will be architecture dependant. The following might help you to see what is happening.

$ perl -e ' > $maxMsgCodeLength = 15; > $port = 9000; > $ip = q{192.168.106.60}; > $msgCode = q{StartBE}; > $msg = pack qq{A${maxMsgCodeLength}ia*}, $msgCode, $port, $ip; > print $msg;' | hexdump -C 00000000 53 74 61 72 74 42 45 20 20 20 20 20 20 20 20 28 |StartBE + (| 00000010 23 00 00 31 39 32 2e 31 36 38 2e 31 30 36 2e 36 |#..192.16 +8.106.6| 00000020 30 |0| 00000021 $

You can see in this case that the port no of 9000 has been represented by the four hex bytes 28230000 (running under Cygwin on XP). Reversing the process gives:-

$ perl -le 'print unpack q{i}, qq{\x28\x23\x00\x00};' 9000 $

I hope this is useful.

Cheers,

JohnGG


In reply to Re: Please Explain this Function by johngg
in thread Please Explain this Function by koti688

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.