Solved

Make sense of the output

Posted on 2004-08-18
4
212 Views
Last Modified: 2006-11-17
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
Comment
Question by:raghu_vs
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 18

Expert Comment

by:kandura
ID: 11832145
Compare:

perl -MData::Dumper -e 'print Dumper(\%main::)'
0
 
LVL 18

Expert Comment

by:kandura
ID: 11836100
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
 
LVL 8

Accepted Solution

by:
davorg earned 70 total points
ID: 11838836
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
 

Author Comment

by:raghu_vs
ID: 11840264
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
perl to display a matrix report for 3-dimenional array 3 75
Rename file based on contents in the file? 3 108
syslog unix file 20 80
Perl Snippet to Parse String 1 63
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question