Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Accessing a lexical variable of another file.

by Superfox il Volpone (Sexton)
on Sep 10, 2014 at 11:03 UTC ( [id://1100118]=perlquestion: print w/replies, xml ) Need Help??

Superfox il Volpone has asked for the wisdom of the Perl Monks concerning the following question:

According to http://www.perlmonks.org/?node_id=66677 it does not seem possible. However do you know if there is some way to access a variable declared with my from another file?

For instance:
// second.pl my $var = 1; // main.pl require("second.pl") // how do I access $var from here?
Thanks,
S. Fox

Replies are listed 'Best First'.
Re: Accessing a lexical variable of another file.
by AppleFritter (Vicar) on Sep 10, 2014 at 11:42 UTC

    Certainly not possible with pure Perl - and for a good reason, it's not supposed to be. XS modules can play all sorts of dirty tricks with Perl internals, of course, so moritz's suggestion of PadWalker would (likely) work.

    Then again, this feels like a classic XY Problem to me. Why do you want to use lexical variables from different files to begin with?

Re: Accessing a lexical variable of another file.
by moritz (Cardinal) on Sep 10, 2014 at 11:26 UTC
Re: Accessing a lexical variable of another file.
by LanX (Saint) on Sep 10, 2014 at 12:30 UTC
    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.

      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.

Re: Accessing a lexical variable of another file.
by Anonymous Monk on Sep 11, 2014 at 03:10 UTC

    Hello, I'm captain-obvious-here :)

    FWIW this isn't perl

    // second.pl my $var = 1; // main.pl require("second.pl") // how do I access $var from here?

    Also, while you can do things in perl like Accessing a lexical variable of another file. its not a good idea ... you already have the code, just change it :)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1100118]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2024-04-24 11:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found