Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I'm not good at English, but I believe that when you look at codes you will understand what I meant

package AAA; use 5.20.1; use lib ('.'); use BBB; use parent('Exporter'); our @EXPORT=('$b','$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; $b->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 test.pl line 5. Execution of test.pl aborted due to compilation errors.

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

    You have not exported $bbb variable from AAA package, you have exported $b only.Check your code.

    our @EXPORT=('$b','$msg'); our $bbb=BBB->new();

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

      thanks I changed the code.

      but $bbb variable is not exported to module BBB

      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

        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...
Re: Export global variables not working correctly
by trippledubs (Deacon) on Mar 06, 2015 at 15:04 UTC
    Package AAA
    package AAA; use lib ('.'); use BBB; use Exporter 'import'; our @EXPORT = qw /$bbb/; our $bbb=BBB->new(); 1;
    package 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(); }
    main.pl
    #!/usr/bin/env perl use strict; use warnings; use lib (','); use AAA; use BBB; $bbb->hello; 1;
    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.
    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;
    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 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();
Re: Export global variables not working correctly
by LanX (Saint) on Mar 06, 2015 at 15:13 UTC
    > but I believe that when you look at codes you will understand what I meant

    not really. 3 files blurs what your goal is.

    Cheers Rolf
    (addicted to the Perl Programming Language and ☆☆☆☆ :)

    PS: Je suis Charlie!

Re: Export global variables not working correctly
by cheako (Beadle) on Mar 06, 2015 at 06:41 UTC
    I afraid the best you can do is use Alias. However for object instances you'll have to use a reference to the other object, you should think in these terms and not try to use Packages as Namespaces.

      What's this about not trying to use packages as namespaces? The last time I looked at perlmod, I read this:

      Perl provides a mechanism for alternative namespaces to protect packages from stomping on each other's variables. In fact, there's really no such thing as a global variable in Perl. The package statement declares the compilation unit as being in the given namespace.

      And perlobj says:

      A package is simply a namespace containing variables and subroutines.


      Dave

        This thinking only works if you can stop any one from using bless and your package. Afterwards your namespace is no longer just a namespace.