Corion is right that if there is an appropriate module to do this for you, you should use that. <update> For example as VinsWorldcom demonstrated here</update>

If it turns out you do need to do this yourself, here are two ideas based on unpack (templates explained in perlpacktut and pack) and regexes. The first is a little more robust in its parsing, the second a little shorter and simpler.

my $data = "\000\012\000\144\023\304\003pbx\007example\003com\000:3600 +"; my ($pri,$weight,$port,$rest) = unpack "S>3A*", $data; my $target=''; $target .= (length($target)?'.':'').$2 while $rest=~/\G([\x01-\xFF])((??{".{".ord($1)."}"}))/saagc; my ($ttl) = $rest=~/\G\x00:(.+)\z/saagc or die "failed to parse"; # - OR - my ($pri,$weight,$port,$target,$ttl) = unpack "S>3Z*A*", $data; $target = join '.', unpack "(C/A)*", $target; $ttl=~s/\A://;

In reply to Re: deconstructing tinydns(djbdns) SRV records (updated) by haukex
in thread deconstructing tinydns(djbdns) SRV records by 0xdeadbad

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.