Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I list all variables that are in a given scope?

Tags:

perl

I know I can list all of the package and lexcial variables in a given scope using Padwalker's peek_our and peek_my, but how can I get the names and values of all of the global variables like $" and $/?

#!/usr/bin/perl  use strict; use warnings;  use PadWalker qw/peek_our peek_my/; use Data::Dumper;  our $foo = 1; our $bar = 2;  {     my $foo = 3;     print Dumper in_scope_variables(); }  print Dumper in_scope_variables();  sub in_scope_variables {     my %in_scope = %{peek_our(1)};     my $lexical  = peek_my(1);     #lexicals hide package variables     while (my ($var, $ref) = each %$lexical) {         $in_scope{$var} = $ref;     }     ##############################################     #FIXME: need to add globals to %in_scope here#     ##############################################     return \%in_scope; } 
like image 263
Chas. Owens Avatar asked Apr 14 '09 13:04

Chas. Owens


People also ask

How will you get the information about all variables that are currently in scope?

You can see scopes and their variables in [[Scopes]] , even closure scopes using console. dir() . It shows you the variables in "[[Scopes]] > Closure", "[[Scopes]] > Global" and even "[[Scopes]] > Script" if the page have scripts reachable. Even with nested closures you can see the nested scopes.

How do you list all variables in Python?

dir() is a built-in function to store all the variables inside a program along with the built-in variable functions and methods. It creates a list of all declared and built-in variables.

What do you call a variable that can be accessed from all scopes?

Output: 5 10. In the program, the variable “global” is declared at the top of the program outside all of the functions so it is a global variable and can be accessed or updated from anywhere in the program.


2 Answers

You can access the symbol table, check out p. 293 of "Programming Perl" Also look at "Mastering Perl: http://www252.pair.com/comdog/mastering_perl/ Specifically: http://www252.pair.com/comdog/mastering_perl/Chapters/08.symbol_tables.html

Those variables you are looking for will be under the main namespace

A quick Google search gave me:

{     no strict 'refs';      foreach my $entry ( keys %main:: )     {         print "$entry\n";     } } 

You can also do

*sym = $main::{"/"} 

and likewise for other values

If you want to find the type of the symbol you can do (from mastering perl):

foreach my $entry ( keys %main:: ) {     print "-" x 30, "Name: $entry\n";      print "\tscalar is defined\n" if defined ${$entry};     print "\tarray  is defined\n" if defined @{$entry};     print "\thash   is defined\n" if defined %{$entry};     print "\tsub    is defined\n" if defined &{$entry}; } 
like image 87
fido Avatar answered Oct 23 '22 19:10

fido


And that does it. Thanks to MGoDave and kbosak for providing the answer in front of my face that I was too stupid to see (I looked in %main:: to start with, but missed that they didn't have their sigils). Here is the complete code:

#!/usr/bin/perl  use strict; use warnings;  use PadWalker qw/peek_our peek_my/; use Data::Dumper;  our $foo = 1; our $bar = 2;  {     my $foo = 3;     print Dumper in_scope_variables(); }  print Dumper in_scope_variables();  sub in_scope_variables {     my %in_scope = %{peek_our(1)};     my $lexical  = peek_my(1);     for my $name (keys %main::) {         my $glob = $main::{$name};         if (defined ${$glob}) {             $in_scope{'$' . $name} = ${$glob};         }          if (defined @{$glob}) {             $in_scope{'@' . $name} = [@{$glob}];         }          if (defined %{$glob}) {             $in_scope{'%' . $name} = {%{$glob}};         }     }      #lexicals hide package variables     while (my ($var, $ref) = each %$lexical) {         $in_scope{$var} = $ref;     }     return \%in_scope; } 
like image 37
Chas. Owens Avatar answered Oct 23 '22 18:10

Chas. Owens