> Is it even possible?

Yes, with PadWalker it's possible to mess with such internals. See demo.

But as Toby already said, it's probably not a good idea especially for production code.

NB: After calling your patched sub, you'll change $varry for it's whole (file?) scope in that module. This can lead to very unfortunate side effects, if carelessly done.

use v5.12.0; use warnings; # ========= mock original module BEGIN { package zor; my $varry = "DEFAULT"; sub lol { $varry = 'OLD'; return 'whatever'; } sub get_varry { return $varry } # ========= old behaviour warn get_varry; warn lol(); warn get_varry; } package main; # ========= monkey patch sub { package zor; use PadWalker qw/closed_over/; my ($h_closure,$s_varry); # ===== get scalar ref of varry BEGIN { $h_closure = closed_over(\&lol); $s_varry = $h_closure->{'$varry'}; } # ===== new sub no warnings 'redefine'; sub lol { $$s_varry = 'NEW'; return 'whatever'; } } # ========= new behaviour warn zor::lol(); warn zor::get_varry; # OOPS

DEFAULT at d:/Perl/pm/change_my.pl line 21. whatever at d:/Perl/pm/change_my.pl line 22. OLD at d:/Perl/pm/change_my.pl line 23. whatever at d:/Perl/pm/change_my.pl line 54. NEW at d:/Perl/pm/change_my.pl line 55.

UPDATE

changed code to better demonstrate side effects on $varry

Cheers Rolf
(addicted to the 𐍀𐌴𐍂𐌻 Programming Language :)
Wikisyntax for the Monastery


In reply to Re: Changing Local Variable Out of Context (updated) by LanX
in thread Changing Local Variable Out of Context by trigoku

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.