in reply to Variable access across packages?

I think there's a problem with choosing "B" as the name of the pm file. See if this helps:
## A.pl use BB; $aValue = 10; BB::aSub();
And the pm file:
## BB.pm package BB; sub aSub { print $main::aValue, "\n"; } 1;
When I run A.pl, it simply outputs "10".

Cheers,
Rob

Replies are listed 'Best First'.
Re^2: Variable access across packages?
by porta (Sexton) on Nov 14, 2007 at 14:26 UTC
    Thats's it! is that $main a special variable? Does it refers to the *caller* of the script?

      Yes and no. Yes, $main:: is special. It refers to the package that the "main" script is running in (see perlmod as moritz suggested). However, it is not necessarily the package that called the code that refers to it. For that, look at caller.

      $x = 'main'; Foo::talk(); package Bar; $x = 'Bar'; Foo::talk(); package Foo; sub talk { my ( $package, $filename, $line ) = caller; print "Calling package is '$package'\n"; printf "Caller's \$x is: '%s'\n", ${ $package . '::x' }; } __END__ Calling package is 'main' Caller's $x is: 'main' Calling package is 'Bar' Caller's $x is: 'Bar'
      Yes, the $main::aValue refers back to the calling package, though it's probably better practice to pass the variable instead of using globals. There are always fewer mistakes when you force yourself to be intentional about passing variables.