in reply to Export global variables not working correctly
package AAA; use lib ('.'); use BBB; use Exporter 'import'; our @EXPORT = qw /$bbb/; our $bbb=BBB->new(); 1;
main.plpackage BBB; use lib '.'; use AAA; use feature 'say'; sub new{ my $class=shift; my $self=bless {},$class; return $self; } sub hello{ my $self=shift; say 'hello'; } sub hello2{ my $self=shift; $b->hello(); }
I think if you try and execute AAA.pm you get an error because when AAA references BBB, but BBB references AAA and so execution goes back to AAA and BBB->new has not been defined at this point. If you put 'use AAA' at the bottom of BBB, now you can execute AAA.pm, though I'm not sure why you'd want to. main.pl still works as expected though.#!/usr/bin/env perl use strict; use warnings; use lib (','); use AAA; use BBB; $bbb->hello; 1;
If you use correctly it works as expected. In one file this is the minimum I could change your code to make it work as I think you want..package BBB; use lib '.'; use feature 'say'; sub new{ my $class=shift; my $self=bless {},$class; return $self; } sub hello{ my $self=shift; say 'hello'; } sub hello2{ my $self=shift; $b->hello(); } use AAA;
package AAA; use parent('Exporter'); our @EXPORT=('$bbb'); our $bbb=BBB->new(); package BBB; use AAA; use feature 'say'; sub new{ my $class=shift; my $self=bless {},$class; return $self; } sub hello{ my $self=shift; say 'hello'; } sub hello2{ my $self=shift; #$b->hello(); #what is $b? $bbb->hello(); } package main; use AAA; $bbb->hello2();
|
|---|