Can't locate object method <var name> via package <file name> error

Viewed 61
# Object.pm
sub update {
  $table = $self->DB_TABLE;
...
}

The update function is triggered when a value is updated and seems to be executed multiple times by other files whose relevant parts look like:

# Status.pm
use constant DB_TABLE => 'Status';
# Flag.pm
use constant DB_TABLE => 'flag';

I don't know the inner workings of this project, but modified Flag.pm and Object.pm as below because I need to use a different table for updating flag.

# Flag.pm
use constant DB_TABLE => 'flag';
use constnat DB_UPDATE_TABLE => '<Table to use when updating flag>';
# Object.pm
sub update {
    my $table = undef;
    if($self->DB_UPDATE_TABLE) {
      $table = $self->DB_UPDATE_TABLE;
    } else {
      $table = $self->DB_TABLE; 
    }
}

When I triggered sub update, I get

Can't locate object method "DB_UPDATE_TABLE" via package "<Status.pm>" at Object.pm.

Is there any way I can check if DB_UPDATE_TABLE exists in each file without error? I can add the following line to Status.pm, but there are a couple dozen of files like Status.pm.

use constant DB_UPDATE_TABLE => '';

I don't know why it is $self->DB_TABLE not $self->{DB_TABLE} but with the assumption that it is a method... tried the following, but it also had its own error.

    if( my $ref = eval { $self->can( DB_UPDATE_TABLE ) } ) {
      $table = $self->DB_UPDATE_TABLE;
    } else {
      $table = $self->DB_TABLE; 
    }
Bareword "DB_UPDATE_TABLE" not allowed while "strict subs" in use at Object.pm => I couldn't find the part to set 'strict subs'
1 Answers

Super close!

$self->can( DB_UPDATE_TABLE )

should be

$self->can("DB_UPDATE_TABLE")
Related