Search code examples
perl

In perl, is $self->subname the same as $self->MYPACKAGE::subname?


In perl, is $self->subname the same as $self->MYPACKAGE::subname? It's me the author of Pythonizer with yet another piece of mystery Perl code, this one is again from CGI.pm. Here the code is taking $self, which is an instance variable of the CGI package, and then calling the hidden function, defined within, but the code doesn't just use $self->hidden(...), it uses $self->CGI::hidden(...). When I translate this to Python as self.CGI.hidden(...), I get an error that the CGI class doesn't have a CGI attribute. I'm wondering if I should make a CGI attribute in this case that just points back to the class instance (or should it point to the class)? Anyhow, what exactly does this mean?

package CGI;
...
sub hidden { ... }
...
$self->CGI::hidden(...);

Solution

  • In perl, is $self->subname the same as $self->MYPACKAGE::subname

    No.

    $self->subname will look up subname in the symbol table associated with object $self, which is the symbol table from the package were $self is blessed into.

    $self->MYPACKAGE::subname instead is mostly (but not exactly - see comment below from tobyink) like calling MYPACKAGE::subname($self), i.e. it will not lookup the method in the symbol table associated with $self but instead in the symbol table for MYPACKAGE (and its parent packages). $self might not even have a method subname.