tags:

views:

41

answers:

3

EDIT Sorry for the confusion, here is my updated question.

I am using FindBin in my perl script like this:

use FindBin qw($Bin);
use lib "$Bin/../lib";
use multi_lib qw(say_hello_world);

This works:

multi_lib::say_hello_world();

but this does not:

say_hello_world();

EDIT 2

This is how multi_lib.pm looks:

package multi_lib;

use strict;
use warnings;
use 5.010;

require Exporter;
my @ISA = qw(Exporter); # removing `my` causes an error!
my @EXPORT_OK = qw(say_hello_world); # removing `my` causes an error!

sub say_hello_world {
 say "hello world!";
}

p.s. I have no idea what does @ISA stand for and if adding the my is OK... I followed the preldoc for Exporter.

Edit 3 I think I solved it by moving @EXPORT_OK before use strict. I am used to put use strict right at the beginning of my scripts but I guess this is not the way to go here (?). Anyway, this works:

use Exporter 'import';
@EXPORT_OK = qw(say_hello_world);
use strict;
...

I would still appreciate some explanations as to what exactly is going on here and what is the recommended way of exporting subroutines (like I did?).

A: 

That's not the way libraries work. You need to set your library location then load a module (.pm) from it that contains the subroutine you want.

Oesor
sorry, please see edited post
David B
A: 

I would like to imprt a specific subroutine (aka say_hello_world) from lib, but this does not work:

use lib "$Bin/../lib" qw(say_hello_world);

The use lib just points you to the directory where the files are, you need to specify the file as well. If your subroutine is in a file Example.pm then you need

use Example qw(say_hello_world);

Also note that the FindBin part needs to be inside a BEGIN block:

BEGIN {
    use FindBin qw($Bin);
    use lib "$Bin/../lib";
};
use Example qw(say_hello_world);
Kinopiko
sorry, please see edited post
David B
also, why is the BEGIN block needed here?
David B
+4  A: 

You can't do that. lib's import() routine modifies @INC instead of trying to export anything. But in any case, there are no functions in lib.pm that are suitable for external use. What are you really trying to accomplish?

Updated answer for updated question:

No, you cannot use my() on @EXPORT_OK; it needs to be globally visible so Exporter can use it. Say our @EXPORT_OK; instead. The same is true for @ISA; the package variable @ISA controls inheritance, a lexical @ISA does nothing. I prefer not inheriting from Exporter, though; you do this by just importing Exporter's import routine: use Exporter 'import';

The error you got that prompted you to add my() was because you specified use strict; (which, among other things, requires that variables be properly declared unless they are package variables qualified by package name or special global variables). our() is the equivalent to my() that declares variables as package variables instead of lexicals, so they are accessible from outside the scope in which they are declared. It's better to properly declare them with our() than to just move them above use strict; to get around the error.

ysth
sorry, please see edited post
David B
@David B: updated my answer
ysth