Hi, all.

This is messy, so let me just fess up to start. Here's a horrible example method:

sub nextrec { local *_ = $_[0]; $_{rec} = <$_{_fh}>; return( (\%_)->errmsg($!) ) if $!; @_{@fields} = unpack $layout, $_{rec}; "FTRL" eq $_{_id} ? 0 : $_{rec}; }
(Note that $obj->errmsg($!) decides according to previous state whether to die with the message, warn() it to STDERR, or just make it available on request and quietly return undef.)

For those who want the details, the object is a blessed hash reference, and the method is invoked by a normal $obj->nextrec syntax. $obj->{_fh} is its open filehandle.

I suspect there's either a small gain for not having allocated lexicals and swapped about the values off the argument stack, or a loss for some esoteric and obscure reason I need to learn. Specifically, however, please note this line:
@_{@fields} = unpack $layout, $_{rec};
I don't know how to do that without aliasing the object to a local()ized hash. I'd like to try something like
$obj->{@fields} = unpack $layout, $_{rec};
but I know that won't work, for fairly obvious reasons.
@{$obj}{@fields} = unpack $layout, $_{rec};
maybe? But that starts getting really ugly again....

Since I have to do the aliasing anyway, I opted for the global default vars, and sprinkled heavily with comments. I know that makes $_ and @_ and %_ all look way too much alike when you access $_[0] and $_{foo}....

....but can someone show me how to assign an array of values to specific fields on an object all at once, so that I don't have to do something like this?
my @vals = unpack $layout, $_{rec}; for my $ndx (0..$#fields) { $obj->{$fields[$ndx]} = $vals[$ndx]; }
I'd rather just use object methods, but I'm squeezing for efficiency, here....but mostly, I just want a cleaner syntax.

Thanks,
Paulie

In reply to unpack() into object fields 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.