Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
This is messy, so let me just fess up to start. Here's a horrible example method:
(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.)sub nextrec { local *_ = $_[0]; $_{rec} = <$_{_fh}>; return( (\%_)->errmsg($!) ) if $!; @_{@fields} = unpack $layout, $_{rec}; "FTRL" eq $_{_id} ? 0 : $_{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@_{@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....@{$obj}{@fields} = unpack $layout, $_{rec};
I'd rather just use object methods, but I'm squeezing for efficiency, here....but mostly, I just want a cleaner syntax.my @vals = unpack $layout, $_{rec}; for my $ndx (0..$#fields) { $obj->{$fields[$ndx]} = $vals[$ndx]; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: unpack() into object fields
by jasonk (Parson) on Feb 26, 2003 at 22:36 UTC | |
|
Re: unpack() into object fields
by hv (Prior) on Feb 26, 2003 at 23:49 UTC |