Dumping all Variables

B

Bigus

Is there a way to print out all variables (scalars, hashes & arrays)
currently defined at any point in the execution of a script?

Bigus
 
C

ccc31807

Is there a way to print out all variables (scalars, hashes & arrays)
currently defined at any point in the execution of a script?

Bigus

Look at Data::Dumper
 
U

Uri Guttman

c> Look at Data::Dumper

that won't dump all variables, only the ones that you pass to it.

but i beg to ask why anyone needs to dump all the vars? it is a poor
debugging idea. also the symbol table won't show any lexicals. there are
ways to get at those with modules but again, why does the OP think it is
necessary?

just print the vars you are interested in (plain or with dumper). print
is still (and always will be) the best debugger.

uri
 
B

Bigus

Dr.Ruud said:

Because I'm working on a set of badly coded scripts where loads of global
variables exist (ie: no 'use strict' in effect) that may be modified in any
one of numerous subroutines over three different and fairly large modules.
I'm using Data::Dumper for specific hashes/arrays but it woul dbe nice to
see all the variables and their values at any given point in the script (it
might also help when we eventually get time to sort the scripts out and get
everything scoped properly).

Bigus
 
U

Uri Guttman

B> Because I'm working on a set of badly coded scripts where loads of
B> global variables exist (ie: no 'use strict' in effect) that may be
B> modified in any one of numerous subroutines over three different
B> and fairly large modules. I'm using Data::Dumper for specific
B> hashes/arrays but it woul dbe nice to see all the variables and
B> their values at any given point in the script (it might also help
B> when we eventually get time to sort the scripts out and get
B> everything scoped properly).

ESP::pSI tells me you have a lot of pain in your future!!

ouch!

best to scan the symbol table. you don't even need a module, just loop
over it like a hash tree (which is what it is from the code's point of
view). check each symbol for the types you want (scalars, arrays, etc)
and then call data::dumper on each one you care about.

and then start to refactor as fast as you can and get it strict
clean. for a good book on this get 'perl medic' by peter scott. it is
out of print but you can find used copies. it specificly covers
refactoring legacy perl systems.

uri
 
C

ccc31807

Because I'm working on a set of badly coded scripts where loads of global
variables exist (ie: no 'use strict' in effect) that may be modified in any
one of numerous subroutines over three different and fairly large modules..

If it were me, I might start by using strict (and warnings.)

This will (obviously) throw compile errors for undeclared variables.
Then you can declare your variables, starting with the top scope and
working your way down to the inner scopes. At least see where they are
scoped.

Another thing I might do is make control flow graphs showing the flow
of control for your program. I have found this to be a very useful
tool.

If you are a bottom up developer, I'd write drivers for your modules,
and test the routines in your modules to see if they do what they are
supposed to do. If you are a top down developer, I'd write stubs for
your called routines to see the absent functionality. I've only been
involved in a project like this a couple of times, but I've had luck
doing both and meeting in the middle.

Another thing that's worked for me is a functional style of
programming where routines only accept arguments and return values --
that is, they avoid side effects unless they specifically produce a
side effect (like 'print_comma_delimited_file(\@data_array_ref)'), and
then they only produce the side effect.

Good luck, CC.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,188
Latest member
Crypto TaxSoftware

Latest Threads

Top