in reply to odd things with my and our

In library.pm, you declare two scalars that are stored in very different places by Perl. our declares a variable in the symbol table, while my declares a variable in the lexical pad. When you say $library::our, you're asking for the variable declared in the library package's symbol table. Likewise, $library::my gets at the symbol table, but since $my was declared as a lexical, there is no entry for it in the symbol table, so you get undef back (strict 'vars' is meaningless when accessing the symbol table).

Getting at the lexical pad is possible, but very difficult. This should be considered a feature.

----
I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
-- Schemer

: () { :|:& };:

Note: All code is untested, unless otherwise stated

Replies are listed 'Best First'.
Re: Re: odd things with my and our
by Anonymous Monk on Jan 25, 2004 at 05:04 UTC
    You've got a better understanding than jcpunk, but you don't have it quite right. "our" has the same scoping rules as "my" (perldoc -f our).

      our has the same scoping rules as my, but only for the name of the variable. With our, the aliased name of the variable goes away at the end of the lexical scope, but the variable itself is still there. It is still accessible from its fully-qualified name, or with another our declaration.

      Here's a quick demonstration. This fails because the name $foo has gone out of lexical scope:

      $ perl -Mstrict -le '{ our $foo = "foo"; print $foo; } print $foo' Variable "$foo" is not imported at -e line 1. Global symbol "$foo" requires explicit package name at -e line 1. Execution of -e aborted due to compilation errors.

      But this succeeds because the variable itself is still around:

      $ perl -Mstrict -le '{ our $foo = "foo"; print $foo; } print $main::fo +o' foo foo

      That said, I'm not so sure what hardburn has not got "quite right." I don't really see him mention the name scoping of either types of variables anywhere in his post.

Re: Re: odd things with my and our
by jweed (Chaplain) on Jan 25, 2004 at 07:25 UTC
    Getting at the lexical pad is possible, but very difficult.
    How? This looks like it could be interesting! Mind sharing? (That isn't sarcasm. I do really want to know).



    Code is (almost) always untested.
    http://www.justicepoetic.net/

      You can always trawl through /dev/kmem . . .

      PadWalker gives limited access to the pads.

      I remember there being a way to get at any pads, but I don't remember how exactly (this isn't something I do every day . . . ). It was in reference to a node on how to break lexically-declared variables for private object data. The process was so difficult that the equivilent ammount of work in any other OO language could also break object privacy. Sorry, that's all I remember.

      ----
      I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
      -- Schemer

      : () { :|:& };:

      Note: All code is untested, unless otherwise stated