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; }
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.
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.
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.
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}; }
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; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With