The function returns a formated string with 3 components joined with no token together. The components are 'packed' in to a string
OK so...
my $msg = pack("a".$maxMsgCodeLength."ia*",$msgCode, $port, $ip);
"StartBE" is 7 characters long, this gets postfixed with spaces by
  $msgCode = $msgCode.(" " x ($maxMsgCodeLength-length($msgCode))); We'll say the max length is 10 here to make it easier.
"StartBE   " is now our string.
my $msg = pack( "a10ia*", $msgCode, $port, $ip );
Can be written more clearly as
my $msg = pack( "a20 i a*", $msgCode, $port, $ip );
From pack on perldoc:
a = A string with arbitrary binary data, will be null padded.
i = A signed integer value.

The three arguments ( $msgCode, $port, $ip ) get 'packed' into ( a10, i and a* ) respectively.

10 Number of characters in first arugment
* However many items are left

If you're familar with printf then it's simlar (but more powerfull then that )
printf( "%s %d %s", $msgCode, $port, $ip );




You could use 'A' instead of 'a' in the pack statement. This will pad with spaces hence if you set A20 and only give a string length 10 then 10 spaces are added to the end rendering the line previous to pack useless. You'd probably want to make the function more generic...
print getMessage ( 'StartBE', $ip, $port ), "\n"; sub getMessage { my ( $msgCode, $ip, $port) = @_; return pack "A10 i A*", $msgCode, $port, $ip; }


J,

In reply to Re: Please Explain this Function by binf-jw
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.