views:

266

answers:

3

I'm monkey patching a package using a technique given at the beginning of "How can I monkey-patch an instance method in Perl?". The problem that I'm running into is that the original subroutine used a package-level my variable which the patched subroutine appears not to have access to, either by full path specification or implicit use.

Is there any way to get at the data scoped in this way for use in the patched subroutine?

Thanks in advance!

+5  A: 

No. The thing you're mistaken in is that they are not package scoped. A lexical variable is by definition limited to its lexical scope, in other words, the block it is in.

Leon Timmermans
Ah, good point -- so if you can't modify the original source file, you're SOL unless some method returns it?
cdleary
That's right. And if you're thinking, "Why have two different ways to subdivide a namespace -- by package and by lexical scope? What a godawful way to run a language!", you're also right.
j_random_hacker
I disagree. Both work in different ways, and have different uses. Lexicals are the best solution for most problems but there are situations where dynamic scoping better.
Leon Timmermans
@Leon: It seems (from this run-in) that lexicals go against the idea of "gentleman's privacy". I'd like to get at those values and have to hack the interpreter to do so. :-(
cdleary
There is a reason all common programing languages have lexical scope (I think emacs lisp is the most common exception). Dynamic scope causes major headaches when not used properly (which is hard). Look up dynamic scope on Wikipedia for more information on the issue.
Leon Timmermans
Lexicals are also scoped to their file, so you're out of luck.
brian d foy
@Leon: I wasn't talking about lexical scoping itself, but the idea that Perl's lexically scoped variables are not visible outside the current package. "our" doesn't make a variable dynamically scoped (that's an incorrect use of the term "dynamic scoping").
cdleary
@cdleary: I think your statement that "lexically scoped variables are not visible outside the current package" is a bit confusing. Lexicals aren't affected by the current package as far as I'm aware.
converter42
@converter42: Does it make more sense to say that lexicals aren't visible outside the package in which they're declared? "current package" seems like bad terminology on my part.
cdleary
@cdleary: No. lexicals have nothing to do with packages at all; packages are only relevant for resolving unqualified use of undeclared globals and for determining the package of something you are declaring. A lexical has scope to the end of a block (and each file is an implicit block).
ysth
+6  A: 

You can obtain lexicals with the PadWalker module. Evil, but it works.

jrockway
http://search.cpan.org/perldoc?PadWalker
Brad Gilbert
Wow, that's interesting. Filthy, but interesting.
j_random_hacker
A: 

Lexicals (ie: declared with 'my') are not visible outside the lexical scope (file or block) in which they are declared. That's the whole point of lexical variables.

If there is a subroutine/method which is in the same scope as the lexical var, then it can return the value of the lexical and that can allow indirect access to the var from outside its scope.

There is no such thing as a 'full path specification' for lexical variables. That's for package variables. If the var was declared with 'our' instead of 'my' you could do that.

noswonky