5

I want to see every place when variable in perl script is created/accessed/destroyed

It is easily reachable using tie or Variable::Magic

But how to apply this magic automatically when variable is created?

Eugen Konkov
  • 22,193
  • 17
  • 108
  • 158
  • You might want to put these all in an associative array and tie that instead so that it's clear in your code where the magic lies. – tadman Feb 24 '17 at 08:41
  • Variables are "_created_" in BEGIN phase. So I don't see how to track that via these methods ... ? – zdim Feb 24 '17 at 08:42
  • @zdim: I thought I may use: `BEGIN { *CORE::GLOBAL::my = sub{ apply_magic_to_new_variable( shift ) } }` However `my` maybe called as bareword only. It will be very handy if called with the reference to new variable when this hook supplied – Eugen Konkov Feb 24 '17 at 08:59
  • 2
    @tadman: I want to create module for [`debugger`](https://metacpan.org/pod/Devel::DebugHooks) which will show all variables which are created in one scope, but accessed/destroyed from others. So I draw dataflow with Graphviz for thirdparty modules. Just running: `perl -d:DebugHooks::DataFlow script.pl` – Eugen Konkov Feb 24 '17 at 09:05

1 Answers1

2

You can take a look at B::Xref which generates a cross reference listing of all variables in your application. Basically, you need to walk the byte code to find all variable declarations/initializations. You can also alter the byte code, i.e. add code to tie the variables. However, I cannot point you to an example, because this is rarely done.

As an alternative, you could use a code filter to add the tie() instructions. However, code filters are not guaranteed to correctly identify variable declarations/initializations.

Matthias
  • 1,005
  • 7
  • 20