4

I'm packaging some common functions in a small Perl module, that I load in the script using

use lib path/to/lib

In the module file I imported some other system installed modules (e.g. Carp qw(confess), but I cannot invoke confess directly, but rather Carp::confess, which is unusual to me.

This is my (non)-working example: https://github.com/telatin/bioinfo/blob/master/mini/script.pl

use 5.012;
use FindBin qw($Bin);
use lib "$Bin/Demo/lib";
use Local::Module;

say "Version: ", $Local::Module::VERSION;

Local::Module->new();

The module: https://github.com/telatin/bioinfo/blob/master/mini/Demo/lib/Local/Module.pm

use 5.012;
use warnings;
use Carp qw(confess);
package Local::Module;
$Local::Module::VERSION = 2;


sub new {
    my ($class, $args) = @_;
    my $self = {
        debug   => $args->{debug}, 
    };
    my $object = bless $self, $class;

    confess "Unable to create fake object";
    return $object;
}

1;

What should I do in the .pm file to avoid this problem?

1
  • 1
    Tip: $Bin should be $RealBin. That way, the program will keep working if you create a symlink to the program Commented Jul 5, 2019 at 21:14

1 Answer 1

8

The problem is here:

use 5.012;
use warnings;
use Carp qw(confess);
package Local::Module;

First you load Carp and import confess, but at that point you're still in package main, so confess is imported into main.

Then you switch packages with package Local::Module, but there is no confess function defined here.

You need to switch packages first:

package Local::Module;
use 5.012;
use warnings;
use Carp qw(confess);

Now all imports and all the following code are in the same package.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.