Beefy Boxes and Bandwidth Generously Provided by pair Networks Cowboy Neal with Hat
Syntactic Confectionery Delight
 
PerlMonks  

Build a list of defined var's

by Karger78 (Beadle)
on Oct 20, 2009 at 18:00 UTC ( #802275=perlquestion: print w/ replies, xml ) Need Help??
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?

Comment on Build a list of defined var's
Re: Build a list of defined var's
by ikegami (Pope) on Oct 20, 2009 at 18:12 UTC

    Not really, not perfectly. What are you trying to do.

      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.

        Why not just restart application?

        If you want to start with a fresh slate, it's easy. Assuming you haven't changed the current directory or clobbered @ARGV,

        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.

        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.

Re: Build a list of defined var's
by CountZero (Chancellor) 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

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.
      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

Re: Build a list of defined var's
by CountZero (Chancellor) on Oct 20, 2009 at 18:46 UTC
    Trying again.

    The Camel book has the following for-loop for enumerating all the variables in a package:

    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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://802275]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (8)
As of 2014-04-18 21:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (472 votes), past polls