I'm in a situation where a module I'm using has a function whose name is exactly the same as one in my own module. When I try to call the function in my module (OO Perl, so $self->function
) it's calling the function from the other module instead.
I've already got around it by renaming my function but as a matter of interest, is there any way of explicitly calling the function from my module?
edit: this is essentially what I'm doing
package Provider::WTO;
use base qw(Provider); # Provider contains a method called date
use utilities::utils; #not my module so don't blame me for the horrendous name :-)
...
sub _get_location
{
my $self = shift;
return $self->date."/some_other_string"; # calls utilities::utils::date()
}
If the name conflict is caused by an import from another module you might consider either Sub::Import
, which allows for easy renaming of imports, even if the exporting module doesn't explicitly support that, or namespace::autoclean
/namespace::clean
.
package YourPackage;
use Sub::Import 'Some::Module' => (
foo => { -as => 'moo' },
); # imports foo as moo
sub foo { # your own foo()
return moo() * 2; # call Some::Module::foo() as moo()
}
The namespace cleaning modules will only be helpful if the import is shadowing any of your methods with a function, not in any other case:
package YourPackage;
use Some::Module; # imports foo
use Method::Signatures::Simple
use namespace::autoclean; # or use namespace::clean -except => 'meta';
method foo {
return foo() * 2; # call imported thing as a function
}
method bar {
return $self->foo; # call own foo() as a method
}
1;
This way the imported function will be removed after compiling your module, when the function calls to foo() are already bound to the import. Later, at your modules runtime, a method called foo
will be installed instead. Method resolution always happens at runtime, so any method calls to ->foo will be resolved to your own method.
Alternatively, you can always call a function by it's fully-qualified name, and don't import it.
use Some::Module ();
Some::Module::foo();
This can also be done for methods, completely disabling runtime method lookup:
$obj->Some::Module::foo();
However, needing to do this is usually a sign of bad design and you should probably step back a little and explain what you did to get you into this situation in the first place.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With