Link to home
Start Free TrialLog in
Avatar of raghu_vs
raghu_vs

asked on

Make sense of the output

while (my ($key,$value) = each(%main::)) {
local(*gl) = $value;
print "$key - $gl\n";
}

What does the above sample code print?
Choice 1 The names and definitions of all subroutines in the main namespace
Choice 2 A list of all globs contained in the main namespace
Choice 3 The names and scalar contents of all variables in the main namespace
Choice 4 A list of all packages contained underneath the main namespace
Choice 5 The current script indexed by line numbers


I ran the above code in perl and got the following o/p
/ -

stderr -
gl -
utf8:: -
" -  
CORE:: -
DynaLoader:: -
stdout -
attributes:: -
 -
stdin -
ARGV -
INC -
ENV -
PEN - :utf8:utf8
UNIVERSAL:: -
$ - 2007
_<perlio.c - perlio.c
main:: -
- - 0
_<perlmain.c - perlmain.c
PerlIO:: -
_<universal.c - universal.c
0 - ./perl_progs
 - 256
@ -
_<xsutils.c - xsutils.c
STDOUT -
IO:: -
 - /usr/bin/perl
_ -
+ -
STDERR -
Internals:: -
STDIN -
DB:: -
<none>:: -


I am not able to make sense of the output. Can any one tell me if the o/p is one of the following

Choice 1 The names and definitions of all subroutines in the main namespace
Choice 2 A list of all globs contained in the main namespace
Choice 3 The names and scalar contents of all variables in the main namespace
Choice 4 A list of all packages contained underneath the main namespace
Choice 5 The current script indexed by line numbers


Thanks,
Raghu
Avatar of kandura
kandura

Compare:

perl -MData::Dumper -e 'print Dumper(\%main::)'
Sorry, my adsl connection is acting up today.

Anyway, your snippet is attempting to do option 2, more or less. What is doing is getting all keys and values in the %main:: namespace, taking the value as a glob (in an attempt to have *gl contain every possible kind of data), and then coerce it into a string. As you saw in the output, the stringification doesn't work well in most cases.
ASKER CERTIFIED SOLUTION
Avatar of Dave Cross
Dave Cross
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of raghu_vs

ASKER

oh no! This is not home work. I got this question while taking the brainbench perl practice test.
Anyway thanks for all the input!


Raghu