You are making a common c-programmer mistake, one that I regularly made when I started programming in perl. From what I see of your note you equate scalars with strings. This is not the case. Consider the following:

my $a; $a = "Mary had a little lamb"; # $a contains a string $a = 2001; # $a now contains a number $a = '2001'; # $a STILL contains a number $a .= 'AD'; # $a is a string once more :)

How this prints is another matter. print, printf etc all output strings. This is their nature, they know naught else ;). The simplest way to output the byte value of a number is to use the following:

print pack("i", $a);

This will work the same way whiether you represent your data as a number or as a strng of numeric characters; perl interprets both as a number.

print pack("i", 3000); ¨Y¡á print pack("i", '3000'); ¨Y¡á print pack("i", "3000"); ¨Y¡á

Perhaps something like the simple object below will help you

my $data = Data->new("aa", 65, 1.2, "misc", "data string"); print $data->print_secs(); package Data; sub new { my ($class, $id, $secs, $vers, $misc, $data) = @_; my $self = { 'id' => $id, 'secs' => $secs, 'version' => $vers, 'misc' => $misc, 'data' => $data, }; bless $self, $class; return $self; } sub print_secs { # Returns $self->{'secs'} as a byte stream $self = shift; return pack('i', $self->{'secs'}); }

This is a bit crude but perhaps it will give you some ideas.


In reply to Re: Converting a C structure to Perl by Caillte
in thread Converting a C structure to Perl by Anonymous Monk

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.