in reply to Build a list of defined var's
The Camel book has the following for-loop for enumerating all the variables in a package:
Slightly altering the loop content, will undef all the package variables.package Test; $testing = 'TEST'; @test_array = (qw/OneTest TwoTest/); %test_hash = ('testkey' => 'testvalue'); foreach $symname (sort keys %Test::) { local *sym = $Test::{$symname}; print "\$$symname is defined\n" if defined $sym; print "\@$symname is nonnull\n" if @sym; print "\%$symname is nonnull\n" if %sym; }
package Test; $testing = 'TEST'; @test_array = (qw/OneTest TwoTest/); %test_hash = ('testkey' => 'testvalue'); foreach my $symname (sort keys %Test::) { local *sym = $Test::{$symname}; if (defined $sym) {$sym = undef; print "\$$symname is cleared\n" } +; if (@sym) {@sym = undef; print "\@$symname is cleared\n" } +; if (%sym) {%sym = undef; print "\%$symname is cleared\n" } +; }
It is not a full solution: it will not work under strict (note: lexical variables are not part of any package) and will be dangerous if used in the main package as it will undef also all the special variables
CountZero
A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James
|
|---|