in reply to Re: Variable access across packages?
in thread Variable access across packages?

Thats's it! is that $main a special variable? Does it refers to the *caller* of the script?

Replies are listed 'Best First'.
Re^3: Variable access across packages?
by kyle (Abbot) on Nov 14, 2007 at 15:59 UTC

    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'
Re^3: Variable access across packages?
by Anonymous Monk on Nov 14, 2007 at 14:58 UTC
    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.