• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 227
  • Last Modified:

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
0
raghu_vs
Asked:
raghu_vs
  • 2
1 Solution
 
kanduraCommented:
Compare:

perl -MData::Dumper -e 'print Dumper(\%main::)'
0
 
kanduraCommented:
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.
0
 
davorgCommented:
This sounds to me like homework :)

Perhaps you can eliminate some things that it definitely isnt?

Choice 1 The names and definitions of all subroutines in the main namespace

Your output includes 'gl' (a variable) andSTDERR (a filehandle) so that doesn't look likely.

Choice 2 A list of all globs contained in the main namespace

Do you know what a glob is? Perhaps you should look that up in your textbook.

Choice 3 The names and scalar contents of all variables in the main namespace

As I said for choice 1, the list contains the names of filehandles - so this doesn't look likely.

Choice 4 A list of all packages contained underneath the main namespace

As with globs, you'll need to know what a package is in order to know if this is what the list contains.

Choice 5 The current script indexed by line numbers

I think it's obvious that it isn't this :)

So you're just left with globs and packages. And if you know the difference then it's easy to find out which it is.

Dave...
0
 
raghu_vsAuthor Commented:
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
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now