I've been tinkering with lvalue subroutines.
Initially, they looked like a nice idea (from a users perspective at least), but now I'm not so sure :-)

I'm attempting to create a package that allows copying one field from a line to another field; basically:

substr($_, $fld1_start, $fld1_len) = substr($_,$fld2_start, $fld2_len) +;

Unfortunately, I'm having problems getting my code working...

A simple case (taken from one of merlyn's posts) works:

#!/usr/bin/perl use strict; use warnings; use Data::Dumper; { package Foo; sub new { my $class = shift; return bless( { field => "ABCDEF" }, $class ); } sub field : lvalue { my $self = shift; $self->{'field'}; } } my $f = Foo->new(); print $f->field, "\n"; $f->field = reverse $f->field; print $f->field, "\n"; print Dumper $f;
Output:
ABCDEF FEDCBA $VAR1 = bless( { 'field' => 'FEDCBA' }, 'Foo' );

My code, however only appears to work for the first line of input. The object is being updated, but the substring doesn't appear to return the correct value.
Field positions and lengths are being stored in the module and looked up.

Input:

00001AAAAA 00002AAAAA 00003AAAAA 00004AAAAA 00005AAAAA 00006AAAAA 00007AAAAA 00008AAAAA 00009AAAAA 00010AAAAA

Code:

#!/usr/bin/perl use strict; use warnings; use String; use Data::Dumper; my $str = String->new(); while (<>) { $str->line() = $_; print "BEFORE: "; print Dumper $str; print "A: ", $str->field_int( "a" ), "\n"; print "B: ", $str->field_int( "b" ), "\n"; $str->field_int( "a" ) = $str->field_int( "b" ); print "AFTER: "; print Dumper $str; print "LINE: ", $str->line(), "\n" x4; }

Module:

package String; use strict; use warnings; sub new { my $class = shift; my %arg = @_; my $ddl = { a => { start => 0, length => 5, }, b => { start => 5, length => 5, }, n => { start => 10, length => 1, }, }; my $self = { ddl => $ddl, line => undef, }; bless $self, $class; } sub line : lvalue { my $self = shift; my $line = shift; $self->{'line'}; } sub field_start { my $self = shift; my $field = shift; return $self->{'ddl'}{$field}{'start'}; } sub field_length { my $self = shift; my $field = shift; return $self->{'ddl'}{$field}{'length'}; } sub field_int : lvalue { my $self = shift; my $field = shift; print "SUBSTR: ", substr($self->{'line'}, $self->field_start($field) +, $self->field_length($field)), "\n"; substr($self->{'line'}, $self->field_start($field), $self->field_len +gth($field) ); } 1;

Output:

BEFORE: $VAR1 = bless( { 'line' => '00001AAAAA ', 'ddl' => { 'n' => { 'length' => 1, 'start' => 10 }, 'a' => { 'length' => 5, 'start' => 0 }, 'b' => { 'length' => 5, 'start' => 5 } } }, 'String' ); SUBSTR: 00001 A: 00001 SUBSTR: AAAAA B: AAAAA SUBSTR: AAAAA SUBSTR: 00001 AFTER: $VAR1 = bless( { 'line' => 'AAAAAAAAAA ', 'ddl' => { 'n' => { 'length' => 1, 'start' => 10 }, 'a' => { 'length' => 5, 'start' => 0 }, 'b' => { 'length' => 5, 'start' => 5 } } }, 'String' ); LINE: AAAAAAAAAA BEFORE: $VAR1 = bless( { 'line' => '00002AAAAA ', 'ddl' => { 'n' => { 'length' => 1, 'start' => 10 }, 'a' => { 'length' => 5, 'start' => 0 }, 'b' => { 'length' => 5, 'start' => 5 } } }, 'String' ); SUBSTR: 00002 A: 00002 SUBSTR: AAAAA B: 00002 SUBSTR: AAAAA SUBSTR: 00002 AFTER: $VAR1 = bless( { 'line' => '00002AAAAA ', 'ddl' => { 'n' => { 'length' => 1, 'start' => 10 }, 'a' => { 'length' => 5, 'start' => 0 }, 'b' => { 'length' => 5, 'start' => 5 } } }, 'String' ); LINE: 00002AAAAA etc.
Notice that The last LINE: is "00002AAAAA" I'm expecting "AAAAAAAAAA", as with the first piece of output.

What is going wrong? Have I missed something obvious?

Cheers,

BazB


If the information in this post is inaccurate, or just plain wrong, don't just downvote - please post explaining what's wrong.
That way everyone learns.


In reply to Inconsistent lvalue subroutine behaviour. by BazB

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.