in reply to Accessing a lexical variable of another file.

You need a reference to $var, that's the only possibility.

PadWalker can only help here if you know at least one function using var from the outer scope( see closed_over() )

Sounds very fragile.

Theoretically you could dive into B and try to localize $var from the OP tree but that's a maintenance nightmare.

If its your module just export

our $varref=\$var
this doesn't confuse the ref count and is the cleanest approach.

Update: and if you love source filter hacking you could require manually, that is find the module, read the text, append the above line and eval all.

Interesting discussion, though that was most likely an XY Problem... :)

HTH

Cheers Rolf

(addicted to the Perl Programming Language and ☆☆☆☆ :)

PS: I even doubt you can retrieve the ref of a var using B , but I'd love to know.

Replies are listed 'Best First'.
Re^2: Accessing a lexical variable of another file.
by Superfox il Volpone (Sexton) on Sep 10, 2014 at 13:54 UTC
    Hi,
    thanks for the replies.

    As for the rationale, it is because I am not the maintainer of the external file and I am not supposed to modify it... although it would be helpful to access a db handle it "hides" in the file.

    Kind regards,
    S. Fox

      So your external file has something like "my $dbh=DBI->connect( .... );" and you want to "have a look" at $dbh?

      If that's the case, you really don't care what $dbh is - what you really care about is what DBI->connect returns.

        Do you think of monkey patching connect()? :)

        Cheers Rolf

        (addicted to the Perl Programming Language and ☆☆☆☆ :)

        Hi,
        thanks for the replies.

        Not sure I understood, the reference to the db handle is inside my $dbh.
        Because the file is a wrapper to the db_handle I do not want to use explicitly dbi->connect(), but I prefer to rely on the file methods to connect & disconnect. I need some additional flexibility too, and for this I would like to exploit the same handle used by the wrapper.

        Kind regards,
        S. Fox