Reputation: 1982
I have this perl script
#!/usr/bin/perl
use lib "/home/gdanko/test/perl";
use Main;
Plugins::Plug1::devices;
The module Main looks like this
package Main;
use lib "/home/gdanko/test/perl";
use Plugins::Plug1;
use DBI;
@ISA = ('Exporter');
@EXPORT = qw();
@EXPORT_OK = qw($dbh &load_devices);
our $dbh = DBI->connect("dbi:SQLite:dbname=/home/gdanko/test/mydb.db", "", "");
sub load_devices {
my $sth = $dbh->prepare("SELECT * FROM devices");
$sth->execute;
my $devices = $dbh->selectall_hashref($sth, "id");
return $devices;
}
1;
The module Plug1 looks like this
package Plug1;
use lib "/home/gdanko/test/perl";
use Data::Dumper;
use Main qw(&load_devices);
@ISA = ('Exporter');
@EXPORT = ();
@EXPORT_OK = qw(&devices);
sub devices {
print "module plug1\n";
my $devices = Main::load_devices;
print Dumper(\$devices->{maguro});
}
1;
When I execute Plugins::Plug::devices from the main script I get this in return: gdanko@apollo:~/test$ ./script.pl module plug1 $VAR1 = \undef;
How can I make the methods and variables in Main available to Plug1?
Upvotes: 0
Views: 273
Reputation: 126722
You describe your module variously as Plugins::Plug1
, Plugins::Plug
, Plug1
and plug1
. You must be clear which it is, and a module Plugins::Plug1
must be in a file named Plugins/Plug1.pm
starting with the statement package Plugins::Plug1
It is unclear from your question what is going wrong. What should happen is that Perl will tell you Plugins::Plug1::devices
doesn't exist, because your package
statement doesn't match. But it looks like you are saying that Main::load_devices
is returning undef
instead of a hash reference.
It looks like the subroutines are probably being called, and you need to debug them. Put some print
statements in your code to see what is being called, but most of all you must use strict
and use warnings
at the top of every file. That will reveal many simple mistakes.
Also note that Exporter
exports symbols from a module into the calling package, and is unnecessary if you intend always to call the subroutines with their fully-qualified names. If you use Exporter
you can omit the package names from the calls
Upvotes: 1
Reputation: 29844
Plugins::Plug1
must declare that it "is a" Main
(hint: Just like they both declare that they have an "is a" relationship to Exporter
.) . Then the methods will be immediately available. However, you should read the perldoc on objects (start with perlobj) if you're asking how to make variables "visible" to subclasses. You can't and you shouldn't.
I take it you're thinking standard OO and the variables in the package represent members of the object. The most common implementation is a blessed hash, where the members are named values in the hash.
Upvotes: 0