Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Export global variables not working correctly

by vinoth.ree (Monsignor)
on Mar 06, 2015 at 05:55 UTC ( [id://1118987]=note: print w/replies, xml ) Need Help??


in reply to Export global variables not working correctly

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...

Replies are listed 'Best First'.
Re^2: Export global variables not working correctly
by Anonymous Monk on Mar 06, 2015 at 06:02 UTC

    thanks I changed the code.

    but $bbb variable is not exported to module BBB

Re^2: Export global variables not working correctly
by Anonymous Monk on Mar 06, 2015 at 06:12 UTC
    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...

        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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1118987]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (4)
As of 2024-04-19 22:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found