Karger78 has asked for the wisdom of the Perl Monks concerning the following question:
Hello Monks,
What i would like to do is build a list of all variables that are defined into an array.
Is that an easy command to do this?
Re: Build a list of defined var's
by CountZero (Bishop) on Oct 20, 2009 at 18:46 UTC
|
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;
}
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 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
| [reply] [d/l] [select] |
Re: Build a list of defined var's
by ramlight (Friar) on Oct 20, 2009 at 18:26 UTC
|
It could be easy with a little preparation. You would have to define all of your variables as entries in a hash. Then you could iterate through all of the keys in the hash.
$variable{'a'} = 1;
$variable{'b'} = $variable{'a'} + 1;
foreach my $k (keys %variable) {$variable{$k} = $reset_value;}
Of course your code would look rather ugly. But sometimes this approach can be useful. | [reply] [d/l] |
|
Nice try! As a matter of fact, all variables are already in a hash: %main:: for the main package.
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
| [reply] [d/l] |
Re: Build a list of defined var's
by ikegami (Patriarch) on Oct 20, 2009 at 18:12 UTC
|
Not really, not perfectly. What are you trying to do.
| [reply] |
|
Well witin my application, i have a reset button. I would like to undef all my var's that are currently defined. Instead of going though each var and undef them, i though it might be eaiser if there was a way to build a list into an array and just irrate though the array and undef them.
| [reply] |
|
Surely you don't want to reset everything, as that would also reset things like @INC, making your program a little brain-dead. I mean, that's one of the reasons that perldoc -f reset includes the paragraph:
Resetting "A-Z" is not recommended because you'll wipe out
+ your
@ARGV and @INC arrays and your %ENV hash. Resets only pack
+age
variables--lexical variables are unaffected, but they clea
+n
themselves up on scope exit anyway, so you'll probably wan
+t to
use them instead. See "my".
The proper way to go about this is to use proper objects in your program, so that you can have a top level object that is associated with a particular session, and everything else hangs off of that. Then, to reset the session, just generate a new session object.
-- Randal L. Schwartz, Perl hacker
The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this document are to be interpreted as described in RFC 2119.
| [reply] [d/l] [select] |
|
|
exec $^X, $0, @ARGV
Note: That won't pass to the new interpreter options that were passed to the first (like -p), but I bet that's not an issue for you.
Update: Added note.
| [reply] [d/l] |
|
| [reply] |
Re: Build a list of defined var's
by CountZero (Bishop) on Oct 20, 2009 at 18:16 UTC
|
Sorry, I misunderstood your question.
This comes close: use strict;
use Data::Dumper;
my @array = ('A', 'b', undef, 0, 1, '', 'THE END');
$array[10] = 'The Real End';
my @defined_array = grep {defined} @array;
print Dumper(\@array, \@defined_array);
Output:$VAR1 = [
'A',
'b',
undef,
0,
1,
'',
'THE END',
undef,
undef,
undef,
'The Real End'
];
$VAR2 = [
'A',
'b',
0,
1,
'',
'THE END',
'The Real End'
];
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
| [reply] [d/l] [select] |
|
|