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

I feel like I am banging my head against a wall with this code and was hoping someone here could shed some light on what I might be doing wrong here.

The problem is that I am using AUTOLOAD to get/set some properties of an object. However it seems that whenever I set the property to '' and then try to retrieve the object I get undef returned.

The code is below. Thanks for any help you can provide.

sub AUTOLOAD { my $self = shift; my $function = our $AUTOLOAD; $function =~ /::DESTROY/ && return; #ignore the DESTROY call $function =~ s/.*:://; if(@_) { my $new_value = shift; if($new_value ne $self->{$function}) { $self->{$function} = $new_value; #mark this object as changed unless we are modifying the #dirty bit itself $self->{dirty} = 1 if $function ne 'dirty'; } } return $self->{$function}; }

Replies are listed 'Best First'.
Re: AUTOLOAD returns '' as undef
by ioannis (Abbot) on Oct 27, 2005 at 06:59 UTC
    if( "$new_value" ne "$self->{$function}") {
    The attribute can be set to '' only if it was previously set to something else. If the attribute was never previously initialized, and this is the first time you want to set it with the initial value of '', the if statement above will not test true (that is not what you intended) and will not allow you set it.

    It is a simple logical error, for a somewhat rare situation.

      Thanks, I have looked at this code for so long I figured it was some simple logic error....my eyes have just glazed over after trying to debug this for so long...I initially thought it was how DBI was handling empty strings...since that is where I am populating the data from.

      Thanks for the help.