use / circular dependency issue

J

Jens Luedicke

Hiya...

I have a module Filer, which is the baseclass for
a few other modules in the Filer:: namespace.

those modules have Filer in their @ISA array.

the Filer module itself needs a few of those
modules itself and has use statements for them.

If I try to run my program, I get warnings like:

Global symbol "$var" requires explicit package name at
Filer/FilePane.pm line XX.

$var is a variable which is exported by Filer.

It looks like a circular dependency issue to me,
but I probably miss something.

Any hints?

TIA,
Jens
 
P

Paul Lalli

Jens said:
Hiya...

I have a module Filer, which is the baseclass for
a few other modules in the Filer:: namespace.

those modules have Filer in their @ISA array.

the Filer module itself needs a few of those
modules itself and has use statements for them.

This seems like a remarkably poor design choice. A parent class that
depends on its children? Perhaps you could describe why you think you
need this design?
If I try to run my program, I get warnings like:

Global symbol "$var" requires explicit package name at
Filer/FilePane.pm line XX.

That's not a warning, that's an error. Important distinction.
$var is a variable which is exported by Filer.

Exported to what? How is it being imported, and into where?
It looks like a circular dependency issue to me,
but I probably miss something.

Well, for starters, you missed the part of the Posting Guidelines that
ask you to please post a short-but-complete script that demonstrates
your problem.
Any hints?

I don't see anything in your vague description that would cause a
"circular dependency issue". I think it's far more likely you're A)
Not describing the problem correctly B) Not understanding how @ISA and
'use' actually work (they're really not as related as you seem to think
they are) C) understanding how they work, but still doing something
wrong.

Unfortunately, we have no way of knowing which of these options is more
likely, because you didn't give us anything to go on.

Paul Lalli
 
J

Jens Luedicke

Paul said:
Well, for starters, you missed the part of the Posting Guidelines that
ask you to please post a short-but-complete script that demonstrates
your problem.

I have a relationship between MyA and MyB.

The design is by no means perfect, but I'm in the process of
re-designing a quite complex structure.


This piece of code illustrates my problem:

#!/usr/bin/perl

use MyA;

A::func;

# MyA.pm

package MyA;

use strict;
use warnings;

require Exporter;
our @ISA = qw(Exporter);
our @EXPORT = qw($foo $bar);

use MyB;

our $foo = "foo!\n";
our $bar = "bar!\n";

sub func {
my $b = new B;
$b->func;
}

1;

# MyB.pm
package MyB;

use strict;
use warnings;

use MyA;
require Exporter;
our @ISA = qw(MyA);

sub new {
my ($class) = @_;
my $self = bless {}, $class;

return $self;
}

sub func {
print $foo, "\n";
print $bar, "\n";
}

1;
 
S

Sisyphus

Jens Luedicke said:
I have a relationship between MyA and MyB.

The design is by no means perfect, but I'm in the process of
re-designing a quite complex structure.


This piece of code illustrates my problem:

For me, produces:

D:\pscrpt>perl try.pl
Global symbol "$foo" requires explicit package name at MyB.pm line 18.
Global symbol "$bar" requires explicit package name at MyB.pm line 19.
Compilation failed in require at MyA.pm line 10.
BEGIN failed--compilation aborted at MyA.pm line 10.
Compilation failed in require at try.pl line 2.
BEGIN failed--compilation aborted at try.pl line 2.

I get essentially the same output with a test script that looks like:

use MyB;
__END__

and a MyB.pm that looks like:

package MyB;

use warnings;
use strict;

sub func {
print $foo, "\n";
print $bar, "\n";
}

1;

If you're using 'strict' in MyB.pm, then you can't declare the variables as
global.

Cheers,
Rob
 
J

Jens Luedicke

Hi ..

I could fix my problem by placing the 'use strict' below the 'use MyA'
statement
because MyA exports those global variables into the MyA namespace.


Jens
 
S

Sisyphus

Jens Luedicke said:
Hi ..

I could fix my problem by placing the 'use strict' below the 'use MyA'
statement
because MyA exports those global variables into the MyA namespace.

I take it that means *all* problems are solved. If that's not so (ie if I've
*not* understood you correctly), then post again - in which case it would be
best to provide your revised code, to avoid confusion.

Cheers,
Rob
 
P

Paul Lalli

Jens said:
I have a relationship between MyA and MyB.

The design is by no means perfect, but I'm in the process of
re-designing a quite complex structure.

Then it would be most beneficial to start the complex structure
correctly. Starting with a base that is not designed well will not
help you in the long run.
This piece of code illustrates my problem:

#!/usr/bin/perl

use MyA;

A::func;

There is no class A anywhere in your code. Post real code.
# MyA.pm

package MyA;

use strict;
use warnings;

require Exporter;
our @ISA = qw(Exporter);
our @EXPORT = qw($foo $bar);

use MyB;

our $foo = "foo!\n";
our $bar = "bar!\n";

sub func {
my $b = new B;

There is no class B anywhere in your code. Post real code.
$b->func;
}

1;

# MyB.pm
package MyB;

use strict;
use warnings;

use MyA;
require Exporter;
our @ISA = qw(MyA);

sub new {
my ($class) = @_;
my $self = bless {}, $class;

return $self;
}

sub func {
print $foo, "\n";
print $bar, "\n";
}

1;

Once those two compilation errors are fixed, we see that the
compilation bails out at MyA.pm's "use MyB;" statement. The problem
here is that 'use' is done at compile time. Which means you are
attempting to load all of MyB.pm before the rest of MyA.pm is parsed -
so @MyA::Export is empty at the time MyB attemps to use MyA. Indeed,
if you were to change MyA's, 'use MyB' to 'require MyB; import MyB;'
the compilation errors would disappear and the program would run as
expected.

The *real* problem here is with your design. I do not believe you have
found any *good* reason to make two different packages use each other.
You need to rethink your design, *especially* if it's eventually going
to become more complex than it currently is.

Hope this helps,
Paul Lalli
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top