http://www.perlmonks.org?node_id=484548

jk2addict has asked for the wisdom of the Perl Monks concerning the following question:

I had reason to use the perl functionality built into a vendors software package today. The package comes with 5.8.3, and it's own set of perl modules to do software related tasks like tweaking data records.

At one point, I have to figure out why this didn't work:

$valobj->value = undef;

but this did:

$valobj->value = '';

After looking into the source code, the object and it's methods use the :lvalue attribute.

sub value : lvalue { my $this = shift; $this->{VALUE}; }

So, that doesn't work if lvalue is undef, but does work if lvalue is an empty string. Is that a 'feature' of the lvalue attribute, or something about the method?

I wouldn't expect this traditional setup to work:

sub value { my ($self, $value) = @_; if (defined $value) { $self->{value} = $value; }; }; $valobj->value(undef);

But I would've expected all the work behind :lvalue to fix that.

Updated: Yes, I've read the pod. It's experimental. Good thing they're using it. The pod doesn't really mention what would happen in the case of setting undef though.

-=Chris

Replies are listed 'Best First'.
Re: :lvalue, How I Hate Thee
by BrowserUk (Patriarch) on Aug 17, 2005 at 19:38 UTC
    At one point, I have to figure out why this didn't work:

    Define "didn't work"? In what way didn't it work?

    This compiles and runs without error:

    #! perl -slw use strict; package thing; sub new { bless {}, $_[0]; } sub value :lvalue { my $this = shift; $this->{VALUE};} package main; my $thing = thing->new; $thing->value = undef;

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.

      Didn't work, as in $self->{VALUE} maintained it's original value of 'Yes', instead of being set to undef.

      What version ot perl was that under?

        5.8.4, but I think you're right that there is something else involved here.

        #! perl -slw use strict; package thing; sub new { bless { VALUE => 'YES' }, $_[0]; } sub value :lvalue { my $this = shift; $this->{VALUE}; } package main; print $]; my $thing = thing->new; print $thing->value; $thing->value = undef; print $thing->value; __END__ P:\test>junk2 5.008004 YES Use of uninitialized value in print at P:\test\junk2.pl line 22.

        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
        "Science is about questioning the status quo. Questioning authority".
        The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.

      It works on one machine, but not the other. One is perl 5.8.3 and the other is 5.8.6, but it feels like something else is afoot....