ganeshk has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks,
I have a web application in perl, with lot of perl modules. There is one module which has a hash called %session in the package. I want to find all methods across all modules whose code can change the contents of the hash. The change can be done in any of the following ways
$session{x} = 1;
or
$y = $session{x}; # $session{x} is an object $y->set_z(3); # this changes the state of object $y
or
$y = $session{x}; foo($y); sub foo { $y->set_z(3); # this changes state of object $y }
I would like it, if this can be automated instead of manually going through the code for all methods. What I was trying was to go through all links from the application UI so that all methods will be executed. I injected some debugging code which will compare the serialized version of the hash before and after the execution of the methods. If there was a difference the method name will be logged. But then the code is bound to change in future and so would require repetition of above process. It would be better if there was any automatic way like a script that would inspect the code(also "use" the modules if needed) and find the list. To me this looked quite challenging! But I think there will be really smart minds to crack this.
Thanks very much in advance!
Ganesh

Replies are listed 'Best First'.
Re: Find whether code can change a module variable
by kyle (Abbot) on Oct 25, 2007 at 20:59 UTC

    I wonder if there's an XY Problem here. Why do you want to find all places in the code that access the hash?

    My only suggestion would be to tie the hash to something that will report on access to it. You can create a access methods that will log or otherwise output information taken from caller. Then run your test suite on it and all the points of access should jump out.

Re: Find whether code can change a module variable
by TGI (Parson) on Oct 25, 2007 at 22:52 UTC

    Can you rely on %session being called 'session'? If so, why not just grep it out of your code? It seems like grepping for $session{x} ought to get a short list of suspect regions of code.

    You should be able to do a more sophisticated static analysis of the code with PPI. I haven't ever needed to do this sort of thing, so I can't give anything more than a pointer to the module.


    TGI says moo

Re: Find whether code can change a module variable
by ganeshk (Monk) on Oct 30, 2007 at 02:49 UTC
    I am sorry for the late reply. Thanks for the help Kyle and TGI. I think the best solution can be achieved only through means of test suite that covers the code comprehensively as mentioned in Re: Cleaning up unused subroutines, since Perl code can have dynamic method calls and I am not sure if it is possible to determine through script.

    But as of now I am working on a solution for finding the subroutines that contains the session directly and then also find other subroutines that call the first list of subroutines. Finally analyse the code for the remaining subroutines and also filter them. However, need to get the better solution(using code coverage) in the long run. Also couldn't do much using PPI as I didn't know how to catch the method calls using it. I will have to work on that too later!
    Thanks,
    Ganesh