in reply to Re: Export global variables not working correctly
in thread Export global variables not working correctly

package AAA; use 5.20.1; use lib ('.'); use BBB; use parent('Exporter'); our @EXPORT=('$bbb','$msg'); our $bbb=BBB->new(); return 1; package BBB; use 5.20.1; use lib '.'; use AAA; sub new{ my $class=shift; my $self=bless {},$class; return $self; } sub hello{ my $self=shift; say 'hello'; } sub hello2{ my $self=shift; $bbb->hello(); } return 1; package main; use 5.20.1; use lib ('.'); use AAA; $bbb->hello2(); perl test.pl Global symbol "$bbb" requires explicit package name at BBB.pm line 16. Compilation failed in require at AAA.pm line 4. BEGIN failed--compilation aborted at AAA.pm line 4. Compilation failed in require at test.pl line 4. BEGIN failed--compilation aborted at test.pl line 4.

variable $bbb is exported only to package main, not package BBB. I want to make $bbb global to any packages

Replies are listed 'Best First'.
Re^3: Export global variables not working correctly
by vinoth.ree (Monsignor) on Mar 06, 2015 at 06:18 UTC

    In your function hello2() you need to call hello() function with $self not with $bbb, as $self already has the BBB package object.

    sub hello2{ my $self=shift; #$bbb->hello(); $self->hello(); }

    All is well. I learn by answering your questions...

      I know but that is differnt instance
      What I'm trying to do is to invoke method $AAA::bbb->hello() , but without prefix AAA:: .
      in package main I can invoke $AAA::bbb->hello() without prefix, but I can't in another package like BBB because of error saying " Global symball $bbb requires explicit package name".
      The error is still same if I make another package CCC and invoke $AAA::bbb->hello() without prefix in package CCC.